summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlberto Contreras <alberto.contreras@canonical.com>2022-11-15 18:23:34 +0100
committerGitHub <noreply@github.com>2022-11-15 10:23:34 -0700
commitfe115f971e56d1386d13989de99cc8d2e7742aed (patch)
treecfa01c52db528e373e6f220cb965242077f93f3e
parent7b04985553ed8b0bc29574608e261ee42280f4d0 (diff)
downloadcloud-init-git-fe115f971e56d1386d13989de99cc8d2e7742aed.tar.gz
test: fix pro integration test
Ensure Pro instances are detached before the test run and that the test only runs on LTS releases. As ua.UserFacingError is not properly converted to str, manually pick its msg to provide the user a more informative logging msg.
-rw-r--r--cloudinit/config/cc_ubuntu_advantage.py2
-rw-r--r--tests/integration_tests/modules/test_ubuntu_advantage.py49
-rw-r--r--tests/unittests/config/test_cc_ubuntu_advantage.py3
3 files changed, 39 insertions, 15 deletions
diff --git a/cloudinit/config/cc_ubuntu_advantage.py b/cloudinit/config/cc_ubuntu_advantage.py
index 121a5907..9dd8f3a2 100644
--- a/cloudinit/config/cc_ubuntu_advantage.py
+++ b/cloudinit/config/cc_ubuntu_advantage.py
@@ -450,7 +450,7 @@ def _auto_attach(ua_section: dict):
" ubuntu_advantage: enable and enable_beta"
)
except UserFacingError as ex:
- msg = f"Error during `full_auto_attach`: {ex}"
+ msg = f"Error during `full_auto_attach`: {ex.msg}"
LOG.error(msg)
raise RuntimeError(msg) from ex
diff --git a/tests/integration_tests/modules/test_ubuntu_advantage.py b/tests/integration_tests/modules/test_ubuntu_advantage.py
index 1d99faff..547ec9e7 100644
--- a/tests/integration_tests/modules/test_ubuntu_advantage.py
+++ b/tests/integration_tests/modules/test_ubuntu_advantage.py
@@ -33,9 +33,19 @@ ubuntu_advantage:
- esm-infra
"""
+PRO_AUTO_ATTACH_DISABLED = """\
+#cloud-config
+ubuntu_advantage:
+ features:
+ disable_auto_attach: true
+"""
+
PRO_DAEMON_DISABLED = """\
#cloud-config
# Disable UA daemon (only needed in GCE)
+ubuntu_advantage:
+ features:
+ disable_auto_attach: true
bootcmd:
- sudo systemctl mask ubuntu-advantage.service
"""
@@ -100,17 +110,18 @@ def get_services_status(client: IntegrationInstance) -> dict:
@pytest.mark.adhoc
@pytest.mark.ubuntu
+@pytest.mark.skipif(
+ not CLOUD_INIT_UA_TOKEN, reason="CLOUD_INIT_UA_TOKEN env var not provided"
+)
class TestUbuntuAdvantage:
@pytest.mark.user_data(ATTACH_FALLBACK.format(token=CLOUD_INIT_UA_TOKEN))
def test_valid_token(self, client: IntegrationInstance):
- assert CLOUD_INIT_UA_TOKEN, "CLOUD_INIT_UA_TOKEN env var not provided"
log = client.read_from_file("/var/log/cloud-init.log")
verify_clean_log(log)
assert is_attached(client)
@pytest.mark.user_data(ATTACH.format(token=CLOUD_INIT_UA_TOKEN))
def test_idempotency(self, client: IntegrationInstance):
- assert CLOUD_INIT_UA_TOKEN, "CLOUD_INIT_UA_TOKEN env var not provided"
log = client.read_from_file("/var/log/cloud-init.log")
verify_clean_log(log)
assert is_attached(client)
@@ -144,29 +155,37 @@ def maybe_install_cloud_init(session_cloud: IntegrationCloud):
user_data = (
PRO_DAEMON_DISABLED
if session_cloud.settings.PLATFORM == "gce"
- else None
+ else PRO_AUTO_ATTACH_DISABLED
)
with session_cloud.launch(
user_data=user_data,
launch_kwargs=launch_kwargs,
) as client:
- log = client.read_from_file("/var/log/cloud-init.log")
- verify_clean_log(log)
+ # TODO: Re-enable this check after cloud images contain
+ # cloud-init 23.4.
+ # Explanation: We have to include something under
+ # user-data.ubuntu_advantage to skip the automatic auto-attach
+ # (driven by ua-auto-attach.service and/or ubuntu-advantage.service)
+ # while customizing the instance but in cloud-init < 23.4,
+ # user-data.ubuntu_advantage requires a token key.
+
+ # log = client.read_from_file("/var/log/cloud-init.log")
+ # verify_clean_log(log)
- client.execute("sudo pro detach --assume-yes") # Force detach
assert not is_attached(
client
), "Test precondition error. Instance is auto-attached."
- LOG.info(
- "Restore `ubuntu-advantage.service` original status for next boot"
- )
- assert client.execute(
- "sudo systemctl unmask ubuntu-advantage.service"
- ).ok
+ if session_cloud.settings.PLATFORM == "gce":
+ LOG.info(
+ "Restore `ubuntu-advantage.service` original status for next"
+ " boot"
+ )
+ assert client.execute(
+ "sudo systemctl unmask ubuntu-advantage.service"
+ ).ok
- source = get_validated_source(session_cloud)
client.install_new_cloud_init(source)
client.destroy()
@@ -179,6 +198,10 @@ def maybe_install_cloud_init(session_cloud: IntegrationCloud):
@pytest.mark.ubuntu
class TestUbuntuAdvantagePro:
def test_custom_services(self, session_cloud: IntegrationCloud):
+ release = ImageSpecification.from_os_image().release
+ if release not in {"bionic", "focal", "jammy"}:
+ pytest.skip(f"Cannot run on non LTS release: {release}")
+
launch_kwargs = maybe_install_cloud_init(session_cloud)
with session_cloud.launch(
user_data=AUTO_ATTACH_CUSTOM_SERVICES,
diff --git a/tests/unittests/config/test_cc_ubuntu_advantage.py b/tests/unittests/config/test_cc_ubuntu_advantage.py
index 07d43f1b..34038cca 100644
--- a/tests/unittests/config/test_cc_ubuntu_advantage.py
+++ b/tests/unittests/config/test_cc_ubuntu_advantage.py
@@ -31,7 +31,8 @@ MPATH = "cloudinit.config.cc_ubuntu_advantage"
class FakeUserFacingError(Exception):
- pass
+ def __init__(self, msg: str):
+ self.msg = msg
class FakeAlreadyAttachedError(FakeUserFacingError):