diff --git a/src/charm.py b/src/charm.py index cfc8977..fda3c03 100755 --- a/src/charm.py +++ b/src/charm.py @@ -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.") diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index 5915047..0d397ca 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -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 @@ -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]], @@ -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 diff --git a/tox.ini b/tox.ini index 20e0744..7a68f4d 100644 --- a/tox.ini +++ b/tox.ini @@ -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