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

Storage tests passing locally #5

Merged
merged 3 commits into from
Jul 31, 2024
Merged
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
6 changes: 3 additions & 3 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ def _generate_self_signed_certificates(self) -> None:
)
cert = generate_certificate(csr=csr, ca=ca, ca_key=ca_pk)

self.container.push(f"{WORKLOAD_PATH}/{CONFIG_MOUNT}/ca.pem", ca)
self.container.push(f"{WORKLOAD_PATH}/{CONFIG_MOUNT}/certificate.pem", cert)
self.container.push(f"{WORKLOAD_PATH}/{CONFIG_MOUNT}/private_key.pem", pk)
self.container.push(f"{WORKLOAD_PATH}/{CONFIG_MOUNT}/ca.pem", ca, make_dirs=True)
self.container.push(f"{WORKLOAD_PATH}/{CONFIG_MOUNT}/certificate.pem", cert, make_dirs=True)
self.container.push(f"{WORKLOAD_PATH}/{CONFIG_MOUNT}/private_key.pem", pk, make_dirs=True)

logger.info("[GoCert] Created self signed certificates.")

Expand Down
54 changes: 47 additions & 7 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# See LICENSE file for licensing details.

from typing import Dict, List
from unittest.mock import patch

import ops
import ops.testing
Expand All @@ -26,12 +27,10 @@ def context(self):
@pytest.mark.parametrize(
"containers_state",
[
pytest.param([], id="no-containers"),
pytest.param([{"name": "gocert", "can_connect": False}], id="container-cant-connect"),
pytest.param([{"name": "gocert", "can_connect": True}], id="container-can-connect"),
],
)
def test_storage_attached_event(
def test_storage_attached_event_happy_path(
self,
storages_state: List[Dict[str, str]],
containers_state: List[Dict[str, str]],
Expand All @@ -43,19 +42,60 @@ def test_storage_attached_event(
Container(name=container.get("name"), can_connect=container.get("can_connect"))
for container in containers_state
],
relations=[Relation(endpoint="juju-info", interface="juju-info")],
networks={"juju-info": Network.default(private_address="4.4.4.4")},
leader=True,
)
for storage in storages_state:
out = context.run(
Event(
f"{storage.get("name")}-storage-attached",
f"{storage.get('name')}-storage-attached",
storage=Storage(name=storage.get("name")),
),
state,
)
assert out.unit_status == ops.BlockedStatus()
assert out.unit_status == ops.BlockedStatus("storages not yet available")

@pytest.mark.parametrize(
"storages_state",
[
pytest.param([{"name": "config"}], id="config-storage"),
pytest.param([{"name": "database"}], id="database-storage"),
pytest.param([{"name": "config"}, {"name": "database"}], id="both-storages"),
],
)
@pytest.mark.parametrize(
"containers_state",
[
# this SHOULD fail, it's ok
pytest.param([], id="no-containers"),
# this SHOULD NOT fail, it currently fails because your charm is missing a leader guard (there's a bug!)
pytest.param([{"name": "gocert", "can_connect": False}], id="container-cant-connect"),
],
)
def test_storage_attached_event_errors(
self,
storages_state: List[Dict[str, str]],
containers_state: List[Dict[str, str]],
context: Context,
):
state = State(
storage=[Storage(name=storage.get("name")) for storage in storages_state],
containers=[
Container(name=container.get("name"), can_connect=container.get("can_connect"))
for container in containers_state
],
leader=True,
)
for storage in storages_state:
# todo catch specific exception
with pytest.raises(Exception):
out = context.run(
Event(
f"{storage.get('name')}-storage-attached",
storage=Storage(name=storage.get("name")),
),
state,
)


def test_config_changed_event(self, context: Context):
pass
Expand Down
15 changes: 15 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,18 @@ commands =
--log-cli-level=INFO \
{posargs} \
{[vars]tests_path}/integration

[testenv:scenario]
description = Scenario tests
deps =
coverage[toml]
pytest
# FIXME unpin as soon as that pr merges
ops-scenario>=6.1.4
git+http://github.com/canonical/ops-scenario@fix-juju-info-network-get
-r{toxinidir}/requirements.txt
commands =
coverage run \
--source={[vars]src_path} \
-m pytest -v --tb native --log-cli-level=INFO -s {posargs} {[vars]tests_path}/unit
coverage report