diff --git a/src/charm.py b/src/charm.py index 040eb1a..2235a7b 100755 --- a/src/charm.py +++ b/src/charm.py @@ -24,13 +24,18 @@ def __init__(self, framework: ops.Framework): self.container = self.unit.get_container("gocert") self.tls = TLSCertificatesProvidesV3(self, relationship_name="certificates") - framework.observe(self.on["gocert"].pebble_ready, self._on_gocert_pebble_ready) + self.port = 2111 + + framework.observe(self.on["gocert"].pebble_ready, self._configure) + framework.observe(self.on.config_changed, self._configure) + + framework.observe(self.on.collect_unit_status, self._on_collect_status) framework.observe(self.on["gocert"].pebble_custom_notice, self._on_gocert_notify) framework.observe(self.tls.on.certificate_creation_request, self._on_new_certificate) - framework.observe(self.on.config_changed, self._on_config_changed) - framework.observe(self.on.collect_unit_status, self._on_collect_status) - def _on_gocert_pebble_ready(self, event: ops.PebbleReadyEvent): + def _configure(self, event: ops.EventBase): + if not self.container.can_connect(): + return try: self.container.pull("/etc/config/config.yaml") except ops.pebble.PathError: @@ -50,7 +55,7 @@ def _on_collect_status(self, event: ops.CollectStatusEvent): def _on_new_certificate(self, event: CertificateCreationRequestEvent): csr = event.certificate_signing_request requests.post( - url="https://localhost:2111/api/v1/certificate_requests", + url=f"https://localhost:{self.port}/api/v1/certificate_requests", data=csr, headers={"Content-Type": "text/plain"}, verify=False, @@ -76,9 +81,6 @@ def _on_gocert_notify(self, event: ops.PebbleCustomNoticeEvent): relation_id=relation.id, ) - def _on_config_changed(self, event: ops.ConfigChangedEvent): - self.unit.status = ops.ActiveStatus() - @property def _pebble_layer(self) -> ops.pebble.LayerDict: """Return a dictionary representing a Pebble layer.""" diff --git a/test-requirements.txt b/test-requirements.txt new file mode 100644 index 0000000..cec5718 --- /dev/null +++ b/test-requirements.txt @@ -0,0 +1 @@ +ops-scenario \ No newline at end of file diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index ba057db..50c50a5 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -5,13 +5,14 @@ import ops.testing import pytest from charm import GocertCharm +from scenario import Context, Event, State class TestCharm: @pytest.fixture(scope="function", autouse=True) - def setUp(self): - self.harness = ops.testing.Harness(GocertCharm) + def context(self): + yield Context(GocertCharm) - def test_start_charm(self): - self.harness.begin_with_initial_hooks() - assert self.harness.model.unit.status == ops.ActiveStatus() + def test_start_charm(self, context): + out = context.run(Event("start"), State()) + assert out.unit_status == ops.ActiveStatus() diff --git a/tox.ini b/tox.ini index 55362be..20e0744 100644 --- a/tox.ini +++ b/tox.ini @@ -46,6 +46,7 @@ deps = pytest coverage[toml] -r {tox_root}/requirements.txt + -r {tox_root}/test-requirements.txt commands = coverage run --source={[vars]src_path} \ -m pytest \