Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions sunbeam-python/sunbeam/commands/prepare_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
# Ensure dependency packages are installed
for pkg in openssh-server curl sed; do
dpkg -s $pkg &> /dev/null || {{
sudo apt install -y $pkg
sudo apt install -y $pkg || exit $?
}}
done

Expand Down Expand Up @@ -123,7 +123,7 @@
echo "Using existing Juju installation."
else
# Install the Juju snap
sudo snap install --channel {JUJU_CHANNEL} juju
sudo snap install --channel {JUJU_CHANNEL} juju || exit $?
fi

# Workaround a bug between snapd and juju
Expand Down Expand Up @@ -163,7 +163,7 @@

BOOTSTRAP_TEMPLATE = f"""
# Install the lxd snap
sudo snap install lxd --channel {LXD_CHANNEL}
sudo snap install lxd --channel {LXD_CHANNEL} || exit $?
USER=$(whoami)
# Ensure current user is part of the LXD group
sudo usermod --append --groups lxd $USER
Expand Down
86 changes: 86 additions & 0 deletions sunbeam-python/tests/unit/sunbeam/commands/test_prepare_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# SPDX-FileCopyrightText: 2026 - Canonical Ltd
# SPDX-License-Identifier: Apache-2.0

import subprocess

import pytest

from sunbeam.commands.prepare_node import (
BOOTSTRAP_TEMPLATE,
COMMON_TEMPLATE,
PREPARE_NODE_TEMPLATE,
)
from sunbeam.versions import SUPPORTED_RELEASE


def run_script(script, tmp_path, install_status):
return subprocess.run(
["bash"],
input=script,
text=True,
capture_output=True,
env={
"HOME": str(tmp_path),
"PATH": "/usr/bin:/bin",
"INSTALL_STATUS": str(install_status),
},
check=False,
)


@pytest.mark.parametrize("install_status", [0, 42])
def test_juju_install_status(tmp_path, install_status):
script = (
"""
command() { if [ "$1 $2" = '-v juju' ]; then return 1; fi; builtin command "$@"; }
sudo() {
if [ "$1 $2" = 'snap install' ]; then return "$INSTALL_STATUS"; fi
echo "sudo $*"
}
snap() { echo 'openstack 1 1 2026.1/stable'; }
"""
+ COMMON_TEMPLATE
)
result = run_script(script, tmp_path, install_status)
assert result.returncode == install_status
assert ("snap refresh --hold openstack" in result.stdout) == (install_status == 0)


@pytest.mark.parametrize("install_status", [0, 42])
def test_dependency_install_status(tmp_path, install_status):
script = (
f"""
lsb_release() {{ echo '{SUPPORTED_RELEASE}'; }}
id() {{ echo 1000; }}
whoami() {{ echo ubuntu; }}
dpkg() {{ return 1; }}
sudo() {{
if [ "$1" = 'grep' ]; then echo 'ubuntu ALL=(ALL) NOPASSWD:ALL'; return 0; fi
if [ "$1 $2" = 'apt install' ]; then return "$INSTALL_STATUS"; fi
if [ "$1" = 'usermod' ]; then echo 'reached usermod'; exit 0; fi
echo "sudo $*"
}}
"""
+ PREPARE_NODE_TEMPLATE
)
result = run_script(script, tmp_path, install_status)
assert result.returncode == install_status
assert ("reached usermod" in result.stdout) == (install_status == 0)


@pytest.mark.parametrize("install_status", [0, 42])
def test_lxd_install_status(tmp_path, install_status):
script = (
"""
whoami() { echo ubuntu; }
sudo() {
if [ "$1 $2 $3" = 'snap install lxd' ]; then return "$INSTALL_STATUS"; fi
if [ "$1" = 'usermod' ]; then echo 'reached usermod'; exit 0; fi
echo "sudo $*"
}
"""
+ BOOTSTRAP_TEMPLATE
)
result = run_script(script, tmp_path, install_status)
assert result.returncode == install_status
assert ("reached usermod" in result.stdout) == (install_status == 0)
Loading