Initial commit

This commit is contained in:
2026-02-06 23:52:26 +01:00
commit f1c539519e
14 changed files with 1320 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
# Copyright 2026 Valentin Haudiquet
# See LICENSE file for licensing details.
#
# The integration tests use the Jubilant library. See https://documentation.ubuntu.com/jubilant/
# To learn more about testing, see https://documentation.ubuntu.com/ops/latest/explanation/testing/
import logging
import os
import pathlib
import sys
import time
import jubilant
import pytest
logger = logging.getLogger(__name__)
@pytest.fixture(scope="module")
def juju(request: pytest.FixtureRequest):
"""Create a temporary Juju model for running tests."""
with jubilant.temp_model() as juju:
yield juju
if request.session.testsfailed:
logger.info("Collecting Juju logs...")
time.sleep(0.5) # Wait for Juju to process logs.
log = juju.debug_log(limit=1000)
print(log, end="", file=sys.stderr)
@pytest.fixture(scope="session")
def charm():
"""Return the path of the charm under test."""
if "CHARM_PATH" in os.environ:
charm_path = pathlib.Path(os.environ["CHARM_PATH"])
if not charm_path.exists():
raise FileNotFoundError(f"Charm does not exist: {charm_path}")
return charm_path
# Modify below if you're building for multiple bases or architectures.
charm_paths = list(pathlib.Path(".").glob("*.charm"))
if not charm_paths:
raise FileNotFoundError("No .charm file in current directory")
if len(charm_paths) > 1:
path_list = ", ".join(str(path) for path in charm_paths)
raise ValueError(f"More than one .charm file in current directory: {path_list}")
return charm_paths[0]

View File

@@ -0,0 +1,26 @@
# Copyright 2026 Valentin Haudiquet
# See LICENSE file for licensing details.
#
# The integration tests use the Jubilant library. See https://documentation.ubuntu.com/jubilant/
# To learn more about testing, see https://documentation.ubuntu.com/ops/latest/explanation/testing/
import logging
import pathlib
import jubilant
logger = logging.getLogger(__name__)
def test_deploy(charm: pathlib.Path, juju: jubilant.Juju):
"""Deploy the charm under test."""
juju.deploy(charm.resolve(), app="charmed-server")
juju.wait(jubilant.all_active)
def test_workload_version_is_set(charm: pathlib.Path, juju: jubilant.Juju):
"""Check that the correct version of the workload is running."""
version = juju.status().apps["charmed-server"].version
assert version is not None
assert len(version) > 0
assert version == "nginx/1.18.0 (Ubuntu)"

29
tests/unit/test_charm.py Normal file
View File

@@ -0,0 +1,29 @@
# Copyright 2026 Valentin Haudiquet
# See LICENSE file for licensing details.
#
# To learn more about testing, see https://documentation.ubuntu.com/ops/latest/explanation/testing/
import pytest
from ops import testing
from charm import CharmedServerCharm
def mock_get_version():
"""Get a mock version string without executing the workload code."""
return "1.0.0"
def test_start(monkeypatch: pytest.MonkeyPatch):
"""Test that the charm has the correct state after handling the start event."""
# Arrange:
ctx = testing.Context(CharmedServerCharm)
monkeypatch.setattr("charm.charmed_server.get_version", mock_get_version)
monkeypatch.setattr("charm.charmed_server.is_installed", lambda: True)
monkeypatch.setattr("charm.charmed_server.ensure_config", lambda: False)
monkeypatch.setattr("charm.charmed_server.is_running", lambda: True)
# Act:
state_out = ctx.run(ctx.on.start(), testing.State())
# Assert:
assert state_out.workload_version is not None
assert state_out.unit_status == testing.ActiveStatus()