diff --git a/sunbeam-python/sunbeam/commands/prepare_node.py b/sunbeam-python/sunbeam/commands/prepare_node.py index 6009f3598..ea61b8d21 100644 --- a/sunbeam-python/sunbeam/commands/prepare_node.py +++ b/sunbeam-python/sunbeam/commands/prepare_node.py @@ -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 @@ -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 @@ -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 diff --git a/sunbeam-python/tests/unit/sunbeam/commands/test_prepare_node.py b/sunbeam-python/tests/unit/sunbeam/commands/test_prepare_node.py new file mode 100644 index 000000000..2a20ae516 --- /dev/null +++ b/sunbeam-python/tests/unit/sunbeam/commands/test_prepare_node.py @@ -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)