Skip to content

Commit 7abc303

Browse files
pengalerwcarlsen
authored andcommitted
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.
1 parent 4e64f39 commit 7abc303

File tree

7 files changed

+154
-2
lines changed

7 files changed

+154
-2
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ venv
1111
.vscode
1212
.coverage
1313
/.tox
14+
15+
# Smoke test artifacts
16+
*.tar.gz
17+
*.charm
18+
test/charms/test_smoke/requirements.txt

test/charms/test_smoke/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# smoke
2+
3+
## Description
4+
5+
A simple test charm for running smoke tests.
6+
7+
## Usage
8+
9+
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.
10+
11+
Then, from the root directory of this repository, execute `tox -e smoke` to build and deploy this charm.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Learn more about charmcraft.yaml configuration at:
2+
# https://juju.is/docs/sdk/charmcraft-config
3+
type: "charm"
4+
bases:
5+
- build-on:
6+
- name: "ubuntu"
7+
channel: "20.04"
8+
run-on:
9+
- name: "ubuntu"
10+
channel: "20.04"
11+
- build-on:
12+
- name: "ubuntu"
13+
channel: "18.04"
14+
run-on:
15+
- name: "ubuntu"
16+
channel: "18.04"
17+
- build-on:
18+
- name: "ubuntu"
19+
channel: "16.04"
20+
run-on:
21+
- name: "ubuntu"
22+
channel: "16.04"

test/charms/test_smoke/metadata.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright 2022 Penelope Valentine Gale
2+
# See LICENSE file for licensing details.
3+
4+
# For a complete list of supported options, see:
5+
# https://juju.is/docs/sdk/metadata-reference
6+
name: smoke
7+
display-name: |
8+
smoke
9+
description: |
10+
smoke test charm
11+
summary: |
12+
basic minimal charm for running smoke tests

test/charms/test_smoke/src/charm.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright 2022 Canonical Ltd.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Charm the service.
18+
19+
Refer to the following post for a quick-start guide that will help you
20+
develop a new k8s charm using the Operator Framework:
21+
22+
https://discourse.charmhub.io/t/4208
23+
"""
24+
25+
import logging
26+
27+
from ops.charm import CharmBase
28+
from ops.main import main
29+
from ops.model import ActiveStatus
30+
31+
logger = logging.getLogger(__name__)
32+
33+
34+
class SmokeCharm(CharmBase):
35+
"""Charm the service."""
36+
37+
def __init__(self, *args):
38+
super().__init__(*args)
39+
self.framework.observe(self.on.install, self._on_install)
40+
41+
def _on_install(self, event):
42+
self.unit.status = ActiveStatus()
43+
44+
45+
if __name__ == "__main__":
46+
main(SmokeCharm)

test/smoke/test_smoke.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2022 Canonical Ltd.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# Learn more about testing at: https://juju.is/docs/sdk/testing
16+
17+
import logging
18+
19+
from pytest_operator.plugin import OpsTest
20+
21+
logger = logging.getLogger(__name__)
22+
23+
24+
async def test_smoke(ops_test: OpsTest):
25+
# Verify that we can deploy charms from supported series.
26+
27+
# Build the charm. (We just build it for focal -- it *should* work to deploy it on
28+
# older versions of Juju.)
29+
charm = await ops_test.build_charm("./test/charms/test_smoke/")
30+
31+
for series in ['focal', 'bionic', 'xenial']:
32+
app = await ops_test.model.deploy(
33+
charm, series=series, application_name="{}-smoke".format(series))
34+
await ops_test.model.wait_for_idle(timeout=600)
35+
36+
assert app.status == "active", "Series {} failed with '{}' status".format(
37+
series, app.status)

tox.ini

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ commands =
6363

6464
[testenv:unit]
6565
description = Run unit tests
66-
passenv =
66+
passenv =
6767
RUN_REAL_PEBBLE_TESTS
6868
PEBBLE
6969
deps =
@@ -74,7 +74,8 @@ deps =
7474
coverage[toml]
7575
-r{toxinidir}/requirements.txt
7676
commands =
77-
coverage run --source={[vars]src_path} -m pytest -v --tb native {posargs}
77+
coverage run --source={[vars]src_path} \
78+
-m pytest --ignore={[vars]tst_path}smoke -v --tb native {posargs}
7879
coverage report
7980

8081
[testenv:pebble]
@@ -91,3 +92,21 @@ deps =
9192
-r{toxinidir}/requirements.txt
9293
commands =
9394
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"
95+
96+
[testenv:smoke]
97+
description = Run a smoke test against a Juju controller.
98+
whitelist_externals = juju
99+
charmcraft
100+
bash
101+
deps =
102+
pytest
103+
pytest-operator
104+
commands =
105+
# Build a source tarball for ops, and drop it into the root directory of the smoke test charm.
106+
bash -c 'rm -vf ./test/charms/test_smoke/*.tar.gz # Cleanup old builds'
107+
python {toxinidir}/setup.py sdist --dist-dir={toxinidir}/test/charms/test_smoke/
108+
# Inject the tarball into the smoke test charm's requirements.
109+
bash -c 'echo "./$(ls -1 ./test/charms/test_smoke/ | grep tar.gz)" > ./test/charms/test_smoke/requirements.txt'
110+
111+
# Run our smoke tests (this will build the charm, then run the tests).
112+
pytest -v --tb native --log-cli-level=INFO -s {posargs} {toxinidir}/test/smoke/

0 commit comments

Comments
 (0)