From 614f6c6a910f5d097b3e4b927e55986ea2e8b55c Mon Sep 17 00:00:00 2001 From: Pen Gale Date: Thu, 14 Jul 2022 18:16:48 +0000 Subject: [PATCH 1/4] Added a very basic smoke test. Run it with `tox -e smoke` This requires a development box with an active juju controller, and charmcraft installed. The smoke test will build the "smoke" charm in test/charms/test_smoke and try to deploy xenial, bionic and focal versions of the charm. There are a few wonky things about this, including how we inject the current ops package, and the fact that we test with just the focal version of the charm. Future versions of this smoke test should clear those wonky bits up, as well as add automation. --- .gitignore | 5 +++ test/charms/test_smoke/README.md | 9 ++++++ test/charms/test_smoke/charmcraft.yaml | 22 ++++++++++++++ test/charms/test_smoke/config.yaml | 10 ++++++ test/charms/test_smoke/metadata.yaml | 12 ++++++++ test/charms/test_smoke/src/charm.py | 35 +++++++++++++++++++++ test/smoke/test_smoke.py | 42 ++++++++++++++++++++++++++ tox.ini | 22 ++++++++++++-- 8 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 test/charms/test_smoke/README.md create mode 100644 test/charms/test_smoke/charmcraft.yaml create mode 100644 test/charms/test_smoke/config.yaml create mode 100644 test/charms/test_smoke/metadata.yaml create mode 100755 test/charms/test_smoke/src/charm.py create mode 100644 test/smoke/test_smoke.py diff --git a/.gitignore b/.gitignore index 708db59d9..28d775ad1 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,8 @@ venv .vscode .coverage /.tox + +# Smoke test artifacts +*.tar.gz +*.charm +test/charms/test_smoke/requirements.txt diff --git a/test/charms/test_smoke/README.md b/test/charms/test_smoke/README.md new file mode 100644 index 000000000..84766ff07 --- /dev/null +++ b/test/charms/test_smoke/README.md @@ -0,0 +1,9 @@ +# smoke + +## Description + +A simple test charm for running smoke tests. + +## Usage + +Run `tox -e smoke` in the root directory of this repository to build and deploy this charm. diff --git a/test/charms/test_smoke/charmcraft.yaml b/test/charms/test_smoke/charmcraft.yaml new file mode 100644 index 000000000..abc945fa4 --- /dev/null +++ b/test/charms/test_smoke/charmcraft.yaml @@ -0,0 +1,22 @@ +# 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/config.yaml b/test/charms/test_smoke/config.yaml new file mode 100644 index 000000000..c74d53015 --- /dev/null +++ b/test/charms/test_smoke/config.yaml @@ -0,0 +1,10 @@ +# Copyright 2022 Penelope Valentine Gale +# See LICENSE file for licensing details. +# +# TEMPLATE-TODO: change this example to suit your needs. +# If you don't need a config, you can remove the file entirely. +# It ties in to the example _on_config_changed handler in src/charm.py +# +# Learn more about config at: https://juju.is/docs/sdk/config + +options: {} diff --git a/test/charms/test_smoke/metadata.yaml b/test/charms/test_smoke/metadata.yaml new file mode 100644 index 000000000..917eaf038 --- /dev/null +++ b/test/charms/test_smoke/metadata.yaml @@ -0,0 +1,12 @@ +# Copyright 2022 Penelope Valentine Gale +# See LICENSE file for licensing details. + +# For a complete list of supported options, see: +# https://juju.is/docs/sdk/metadata-reference +name: smoke +display-name: | + smoke +description: | + smoke test charm +summary: | + basic minimal charm for running smoke tests diff --git a/test/charms/test_smoke/src/charm.py b/test/charms/test_smoke/src/charm.py new file mode 100755 index 000000000..a5c92b5a5 --- /dev/null +++ b/test/charms/test_smoke/src/charm.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +# Copyright 2022 Penelope Valentine Gale +# See LICENSE file for licensing details. +# +# Learn more at: https://juju.is/docs/sdk + +"""Charm the service. + +Refer to the following post for a quick-start guide that will help you +develop a new k8s charm using the Operator Framework: + + https://discourse.charmhub.io/t/4208 +""" + +import logging + +from ops.charm import CharmBase +from ops.main import main +from ops.model import ActiveStatus + +logger = logging.getLogger(__name__) + + +class SmokeCharm(CharmBase): + """Charm the service.""" + + def __init__(self, *args): + super().__init__(*args) + self.framework.observe(self.on.install, self._on_install) + + def _on_install(self, event): + self.unit.status = ActiveStatus() + +if __name__ == "__main__": + main(SmokeCharm) diff --git a/test/smoke/test_smoke.py b/test/smoke/test_smoke.py new file mode 100644 index 000000000..f0209f53e --- /dev/null +++ b/test/smoke/test_smoke.py @@ -0,0 +1,42 @@ +# Copyright 2022 Canonical Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Learn more about testing at: https://juju.is/docs/sdk/testing + +import logging +from pathlib import Path + +from pytest_operator.plugin import OpsTest + + +logger = logging.getLogger(__name__) + + +CURRENT_CHANNEL = '20.04' # Focal + + +async def test_smoke(ops_test: OpsTest): + # Verify that we can deploy charms from supported series. + + # Build the charm. (We just build it for focal -- it *should* work to deploy it on + # older versions of Juju.) + 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="{}-smoke".format(series)) + await ops_test.model.wait_for_idle(timeout=600) + + assert app.status == "active", "Series {} failed with '{}' status".format( + series, app.status) diff --git a/tox.ini b/tox.ini index bffc674cb..b0f9228bc 100644 --- a/tox.ini +++ b/tox.ini @@ -63,7 +63,7 @@ commands = [testenv:unit] description = Run unit tests -passenv = +passenv = RUN_REAL_PEBBLE_TESTS PEBBLE deps = @@ -74,7 +74,7 @@ deps = coverage[toml] -r{toxinidir}/requirements.txt commands = - coverage run --source={[vars]src_path} -m pytest -v --tb native {posargs} + coverage run --source={[vars]src_path} -m pytest -v --tb native {posargs} coverage report [testenv:pebble] @@ -91,3 +91,21 @@ deps = -r{toxinidir}/requirements.txt commands = bash -c "umask 0; (pebble run --http=':4000' --create-dirs &>/dev/null & ) ; sleep 1; pytest -v --tb native -k RealPebble {posargs} ; killall -y 3m pebble" + +[testenv:smoke] +description = Run a smoke test against a Juju controller. +whitelist_externals = juju + charmcraft + bash +deps = + pytest + pytest-operator +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' + python {toxinidir}/setup.py sdist --dist-dir={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' + + # Run our smoke tests (this will build the charm, then run the tests). + pytest -v --tb native --log-cli-level=INFO -s {posargs} {toxinidir}/test/smoke/ From 10b1e2ee1999e22708eda259b248f016b38c1ece Mon Sep 17 00:00:00 2001 From: Pen Gale Date: Tue, 19 Jul 2022 17:03:43 -0400 Subject: [PATCH 2/4] Fixed up linter errors, and excluded smoke from unit tests. --- test/charms/test_smoke/src/charm.py | 17 ++++++++++++++--- test/smoke/test_smoke.py | 2 -- tox.ini | 3 ++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/test/charms/test_smoke/src/charm.py b/test/charms/test_smoke/src/charm.py index a5c92b5a5..29c982325 100755 --- a/test/charms/test_smoke/src/charm.py +++ b/test/charms/test_smoke/src/charm.py @@ -1,8 +1,18 @@ #!/usr/bin/env python3 -# Copyright 2022 Penelope Valentine Gale -# See LICENSE file for licensing details. # -# Learn more at: https://juju.is/docs/sdk +# Copyright 2022 Canonical Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. """Charm the service. @@ -31,5 +41,6 @@ def __init__(self, *args): def _on_install(self, event): self.unit.status = ActiveStatus() + if __name__ == "__main__": main(SmokeCharm) diff --git a/test/smoke/test_smoke.py b/test/smoke/test_smoke.py index f0209f53e..99702ca5a 100644 --- a/test/smoke/test_smoke.py +++ b/test/smoke/test_smoke.py @@ -15,11 +15,9 @@ # Learn more about testing at: https://juju.is/docs/sdk/testing import logging -from pathlib import Path from pytest_operator.plugin import OpsTest - logger = logging.getLogger(__name__) diff --git a/tox.ini b/tox.ini index b0f9228bc..8e9487820 100644 --- a/tox.ini +++ b/tox.ini @@ -74,7 +74,8 @@ deps = coverage[toml] -r{toxinidir}/requirements.txt commands = - coverage run --source={[vars]src_path} -m pytest -v --tb native {posargs} + coverage run --source={[vars]src_path} \ + -m pytest --ignore={[vars]tst_path}smoke -v --tb native {posargs} coverage report [testenv:pebble] From 286290427ed393aa9e4fc9093f47023f761bc3e2 Mon Sep 17 00:00:00 2001 From: Pen Gale Date: Tue, 9 Aug 2022 20:46:00 +0000 Subject: [PATCH 3/4] Addressed PR comments. --- test/charms/test_smoke/README.md | 4 +++- test/charms/test_smoke/config.yaml | 8 +------- test/smoke/test_smoke.py | 3 --- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/test/charms/test_smoke/README.md b/test/charms/test_smoke/README.md index 84766ff07..ff4d55bb5 100644 --- a/test/charms/test_smoke/README.md +++ b/test/charms/test_smoke/README.md @@ -6,4 +6,6 @@ A simple test charm for running smoke tests. ## Usage -Run `tox -e smoke` in the root directory of this repository to build and deploy this charm. +Make sure that you are on a box with charmcraft and juju installed, and that you are connected to a "machine" controller, such as a local lxd cloud. + +Then, from the root directory of this repository, execute `tox -e smoke` to build and deploy this charm. diff --git a/test/charms/test_smoke/config.yaml b/test/charms/test_smoke/config.yaml index c74d53015..523a67625 100644 --- a/test/charms/test_smoke/config.yaml +++ b/test/charms/test_smoke/config.yaml @@ -1,10 +1,4 @@ -# Copyright 2022 Penelope Valentine Gale +# Copyright 2022 Canonical # See LICENSE file for licensing details. -# -# TEMPLATE-TODO: change this example to suit your needs. -# If you don't need a config, you can remove the file entirely. -# It ties in to the example _on_config_changed handler in src/charm.py -# -# Learn more about config at: https://juju.is/docs/sdk/config options: {} diff --git a/test/smoke/test_smoke.py b/test/smoke/test_smoke.py index 99702ca5a..b658ccaf3 100644 --- a/test/smoke/test_smoke.py +++ b/test/smoke/test_smoke.py @@ -21,9 +21,6 @@ logger = logging.getLogger(__name__) -CURRENT_CHANNEL = '20.04' # Focal - - async def test_smoke(ops_test: OpsTest): # Verify that we can deploy charms from supported series. From 0e287830fb7e2d0e30b1e7739e8b3011e920d2ba Mon Sep 17 00:00:00 2001 From: Pen Gale Date: Wed, 10 Aug 2022 19:34:37 +0000 Subject: [PATCH 4/4] Dropped config file -- not necessary. --- test/charms/test_smoke/config.yaml | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 test/charms/test_smoke/config.yaml diff --git a/test/charms/test_smoke/config.yaml b/test/charms/test_smoke/config.yaml deleted file mode 100644 index 523a67625..000000000 --- a/test/charms/test_smoke/config.yaml +++ /dev/null @@ -1,4 +0,0 @@ -# Copyright 2022 Canonical -# See LICENSE file for licensing details. - -options: {}