Skip to content

Commit

Permalink
test: update the smoke test series/bases (#1318)
Browse files Browse the repository at this point in the history
A collection of fixes for the smoke test:

* Update the smoke test charm to use the preferred "import ops" style.
* Fix tox to use "allowlist_externals" rather than
"whitelist_externals", which doesn't work in recent versions of tox.
* Remove a bash comment in the tox smoke env - this didn't really add
much value and on my system failed (I think tox believed everything
after the `#` was a comment in tox, rather than in the bash statement)
* Drop support for bionic (18.04) and xenial (16.04). Modern charmcraft
can't pack on these systems, because it requires a newer version of pip
than is available for the system Python. We don't really support these
any more either - at least not for using current versions of ops.
* Add support for jammy (22.04)
* Add support for noble (24.04) - this required a substantial change
because of the required changes to `charmcraft.yaml`. Instead of having
all the bases specified in a `charmcraft.yaml` that's in version control
(which works fine for the older three), have the test generate a
`charmcraft.yaml` that matches the required style for that base.
* Use pytest.mark.parametrize rather than putting the build and deploy
in a single test - this is nicer from a logging/progress indication
point of view, and generally cleaner for what are essentially subtests.
Since modern charmcraft is building each charm separately (and
especially now that it's using a separate yaml for each, to handle
Noble) there isn't really any functional difference in terms of the
test.
* When deploying, use `base=` rather than the deprecated `series=`

To test, run `tox -e smoke` (you'll need to have `charmcraft`, `bash`,
and a Juju controller available).
  • Loading branch information
tonyandrewmeyer authored Aug 17, 2024
1 parent 43b5af9 commit 0a4f382
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 43 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 0 additions & 22 deletions test/charms/test_smoke/charmcraft.yaml

This file was deleted.

19 changes: 8 additions & 11 deletions test/charms/test_smoke/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
52 changes: 44 additions & 8 deletions test/smoke/test_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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'
Expand Down

0 comments on commit 0a4f382

Please sign in to comment.