Skip to content

Commit

Permalink
global port number, _configure, and move to scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
kayra1 committed Jul 9, 2024
1 parent a5409e7 commit 26c9187
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
18 changes: 10 additions & 8 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
Expand All @@ -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."""
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ops-scenario
11 changes: 6 additions & 5 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down

0 comments on commit 26c9187

Please sign in to comment.