From 7abc3039c485dd11741f33fa9e75eaef5a73db42 Mon Sep 17 00:00:00 2001 From: Pen Gale Date: Wed, 10 Aug 2022 16:04:40 -0400 Subject: [PATCH] Added a very basic smoke test. Run it with `tox -e smoke` (#800) 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 | 11 ++++++ test/charms/test_smoke/charmcraft.yaml | 22 ++++++++++++ test/charms/test_smoke/metadata.yaml | 12 +++++++ test/charms/test_smoke/src/charm.py | 46 ++++++++++++++++++++++++++ test/smoke/test_smoke.py | 37 +++++++++++++++++++++ tox.ini | 23 +++++++++++-- 7 files changed, 154 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/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..ff4d55bb5 --- /dev/null +++ b/test/charms/test_smoke/README.md @@ -0,0 +1,11 @@ +# smoke + +## Description + +A simple test charm for running smoke tests. + +## Usage + +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/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/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..29c982325 --- /dev/null +++ b/test/charms/test_smoke/src/charm.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +# +# 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. + +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..b658ccaf3 --- /dev/null +++ b/test/smoke/test_smoke.py @@ -0,0 +1,37 @@ +# 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 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. + + # 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..8e9487820 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,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] @@ -91,3 +92,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/