From 268ce6d0a33cd730d8ea6f9f363aa6bca8d478c9 Mon Sep 17 00:00:00 2001 From: Tony Meyer Date: Fri, 16 Aug 2024 12:49:26 +1200 Subject: [PATCH] An assortment of test_smoke fixes. --- .gitignore | 3 ++ test/charms/test_smoke/charmcraft.yaml | 22 ----------- test/charms/test_smoke/src/charm.py | 19 ++++------ test/smoke/test_smoke.py | 52 ++++++++++++++++++++++---- tox.ini | 4 +- 5 files changed, 57 insertions(+), 43 deletions(-) delete mode 100644 test/charms/test_smoke/charmcraft.yaml diff --git a/.gitignore b/.gitignore index 47edccb41..30a07a5b6 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,7 @@ venv # Smoke test artifacts *.tar.gz *.charm +test/charms/test_smoke/.charmcraft_output_packages.txt test/charms/test_smoke/requirements.txt +test/charms/test_smoke/charmcraft.yaml +juju-crashdump*.tar.xz diff --git a/test/charms/test_smoke/charmcraft.yaml b/test/charms/test_smoke/charmcraft.yaml deleted file mode 100644 index abc945fa4..000000000 --- a/test/charms/test_smoke/charmcraft.yaml +++ /dev/null @@ -1,22 +0,0 @@ -# Learn more about charmcraft.yaml configuration at: -# https://juju.is/docs/sdk/charmcraft-config -type: "charm" -bases: - - build-on: - - name: "ubuntu" - channel: "20.04" - run-on: - - name: "ubuntu" - channel: "20.04" - - build-on: - - name: "ubuntu" - channel: "18.04" - run-on: - - name: "ubuntu" - channel: "18.04" - - build-on: - - name: "ubuntu" - channel: "16.04" - run-on: - - name: "ubuntu" - channel: "16.04" diff --git a/test/charms/test_smoke/src/charm.py b/test/charms/test_smoke/src/charm.py index 029480126..b965cec00 100755 --- a/test/charms/test_smoke/src/charm.py +++ b/test/charms/test_smoke/src/charm.py @@ -23,25 +23,22 @@ """ import logging -import typing -from ops.charm import CharmBase, EventBase -from ops.main import main -from ops.model import ActiveStatus +import ops logger = logging.getLogger(__name__) -class SmokeCharm(CharmBase): +class SmokeCharm(ops.CharmBase): """Charm the service.""" - def __init__(self, *args: typing.Any): - super().__init__(*args) - self.framework.observe(self.on.install, self._on_install) + def __init__(self, framework: ops.Framework): + super().__init__(framework) + framework.observe(self.on.install, self._on_install) - def _on_install(self, event: EventBase): - self.unit.status = ActiveStatus() + def _on_install(self, event: ops.EventBase): + self.unit.status = ops.ActiveStatus() if __name__ == '__main__': - main(SmokeCharm) + ops.main(SmokeCharm) diff --git a/test/smoke/test_smoke.py b/test/smoke/test_smoke.py index a1ff79d56..acf049722 100644 --- a/test/smoke/test_smoke.py +++ b/test/smoke/test_smoke.py @@ -16,20 +16,56 @@ import logging +import pytest from pytest_operator.plugin import OpsTest logger = logging.getLogger(__name__) -async def test_smoke(ops_test: OpsTest): - # Verify that we can deploy charms from supported series. +CHARMCRAFT2_YAML = """ +type: "charm" +bases: + - build-on: + - name: "ubuntu" + channel: "{base}" + run-on: + - name: "ubuntu" + channel: "{base}" +""" - # Build the charm. (We just build it for focal -- it *should* work to deploy it on - # older versions of Juju.) +CHARMCRAFT3_YAML = """ +type: "charm" +base: ubuntu@{base} +platforms: + amd64: +parts: + charm: + plugin: charm + source: . +""" + + +@pytest.mark.parametrize( + 'base,charmcraft_version,name', + ( + ('20.04', 2, 'focal'), + ('22.04', 2, 'jammy'), + ('24.04', 3, 'noble'), + ), +) +async def test_smoke(ops_test: OpsTest, base: str, charmcraft_version: int, name: str): + """Verify that we can build and deploy charms from supported bases.""" + charmcraft_yaml = { + 2: CHARMCRAFT2_YAML, + 3: CHARMCRAFT3_YAML, + }[charmcraft_version].format(base=base) + with open('./test/charms/test_smoke/charmcraft.yaml', 'w') as outf: + outf.write(charmcraft_yaml) charm = await ops_test.build_charm('./test/charms/test_smoke/') - for series in ['focal', 'bionic', 'xenial']: - app = await ops_test.model.deploy(charm, series=series, application_name=f'{series}-smoke') - await ops_test.model.wait_for_idle(timeout=600) + app = await ops_test.model.deploy( + charm, base=f'ubuntu@{base}', application_name=f'{name}-smoke' + ) + await ops_test.model.wait_for_idle(timeout=600) - assert app.status == 'active', f"Series {series} failed with '{app.status}' status" + assert app.status == 'active', f"Base ubuntu@{base} failed with '{app.status}' status" diff --git a/tox.ini b/tox.ini index 803b2a579..da2193d3c 100644 --- a/tox.ini +++ b/tox.ini @@ -98,7 +98,7 @@ commands = [testenv:smoke] description = Run a smoke test against a Juju controller. -whitelist_externals = juju +allowlist_externals = juju charmcraft bash deps = @@ -108,7 +108,7 @@ deps = pytest-operator~=0.23 commands = # Build a source tarball for ops, and drop it into the root directory of the smoke test charm. - bash -c 'rm -vf ./test/charms/test_smoke/*.tar.gz # Cleanup old builds' + bash -c 'rm -vf ./test/charms/test_smoke/*.tar.gz' python -m build --sdist --outdir={toxinidir}/test/charms/test_smoke/ # Inject the tarball into the smoke test charm's requirements. bash -c 'echo "./$(ls -1 ./test/charms/test_smoke/ | grep tar.gz)" > ./test/charms/test_smoke/requirements.txt'