Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create scenario tests for init templates #1975

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ xfail_strict = true
markers = [
"slow: marks tests as slow (deselect with '-m \"not slow\"')",
]
addopts = "--ignore=tests/spread"

[tool.coverage.run]
branch = true
Expand Down
23 changes: 16 additions & 7 deletions tests/spread/commands/init-extensions/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ environment:
PROFILE/fastapi: fastapi-framework
CHARMCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS: "true"

prepare: |
tests.pkgs install pipx python3-venv
pipx install --force tox

restore: |
pushd test-init
charmcraft clean
popd

rm -rf test-init

execute: |
# Required for fetch-libs to succeed since the libraries are not available on
# the staging environment
Expand All @@ -20,14 +31,12 @@ execute: |

mkdir -p test-init
cd test-init
charmcraft init --profile "${PROFILE}"
charmcraft init --profile "${PROFILE}" --name hello-world
charmcraft fetch-libs
charmcraft pack --verbose
test -f *.charm

restore: |
pushd test-init
charmcraft clean
popd

rm -rf test-init
# expand charmcraft.yaml for the scenario tests to work
charmcraft expand-extensions > charmcraft-expanded.yaml
mv charmcraft-expanded.yaml charmcraft.yaml
~/.local/bin/tox -c ../tox.ini -e unit
28 changes: 28 additions & 0 deletions tests/spread/commands/init-extensions/tests/unit/test_charm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

"""Smoke scenario test for paas-charm based init templates."""

import os
import pathlib

import charm
import scenario
import scenario.errors


def test_smoke():
"""The purpose of this test is that the charm does not raise on a handled event."""
os.chdir(pathlib.Path(charm.__file__).parent.parent)
ctx = scenario.Context(charm.HelloWorldCharm)
container_name = next(iter(ctx.charm_spec.meta["containers"].keys()))
container = scenario.Container(
name=container_name,
can_connect=True,
)
state_in = scenario.State(containers={container})
out = ctx.run(
ctx.on.pebble_ready(container),
state_in,
)
assert type(out.unit_status) in (scenario.WaitingStatus, scenario.BlockedStatus)
34 changes: 34 additions & 0 deletions tests/spread/commands/init-extensions/tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2024 Canonical
# See LICENSE file for licensing details.

[tox]
no_package = True
env_list = unit
min_version = 4.0.0

[vars]
src_path = {tox_root}/test-init/src
tests_path = {tox_root}/tests

[testenv]
set_env =
PYTHONPATH = {tox_root}/test-init/lib:{[vars]src_path}
PYTHONBREAKPOINT=pdb.set_trace
PY_COLORS=1
pass_env =
PYTHONPATH

[testenv:unit]
description = Run unit tests
deps =
pytest
ops-scenario
-r {tox_root}/test-init/requirements.txt
commands =
pytest \
--tb native \
-v \
-s \
--confcutdir={tox_root} \
{posargs} \
{[vars]tests_path}/unit
Loading