Skip to content

Commit

Permalink
fix: set charm to waitingStatus if missing ingress auth data (#342)
Browse files Browse the repository at this point in the history
* fix: set charm to waitingStatus if missing ingress auth data

* Add unit tests

---------

Co-authored-by: Orfeas Kourkakis <[email protected]>
  • Loading branch information
DnPlas and orfeas-k committed Nov 21, 2023
1 parent 198ad52 commit 96a0c66
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions charms/istio-pilot/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,10 @@ def _get_ingress_auth_data(self, event) -> dict:
if app != self.app
}

# Go into waiting status if there is no ingress-auth data
if not ingress_auth_data:
raise ErrorWithStatus("Waiting for the auth provider data.", WaitingStatus)

# We only support a single ingress-auth relation, so we can unpack and return just the
# contents
if len(ingress_auth_data) > 1:
Expand Down
22 changes: 22 additions & 0 deletions charms/istio-pilot/tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ def test_ingress_auth_and_gateway(

harness.begin()

harness.charm.log = MagicMock()

# Do a reconcile
harness.charm.on.config_changed.emit()

Expand All @@ -228,9 +230,18 @@ def test_ingress_auth_and_gateway(

# Add "broken" ingress_auth (empty data) and check that we remove the gateway
rel_id = harness.add_relation("ingress-auth", "other")
add_data_to_sdi_relation(harness, rel_id, "other", {})
mocked_remove_gateway.assert_called_once
mocked_remove_gateway.reset_mock()

assert harness.charm.model.unit.status == WaitingStatus(
"Execution handled 1 errors. See logs for details."
)
assert (
"Handled error 0/1: WaitingStatus('Waiting for the auth provider data.')"
in harness.charm.log.info.call_args.args
)

# Remove ingress_auth relation and check that we re-add the gateway
harness.remove_relation(rel_id)
assert krh_lightkube_client.apply.call_count == 1
Expand Down Expand Up @@ -570,6 +581,17 @@ def test_get_ingress_auth_data_empty(

assert len(ingress_auth_data) == 0

def test_get_ingress_auth_data_empty_error(self, harness):
"""Tests that the _get_ingress_auth_data helper returns the correct relation data."""
harness.begin()
rel_id = harness.add_relation("ingress-auth", "other")
add_data_to_sdi_relation(harness, rel_id, "other", {})
with pytest.raises(ErrorWithStatus) as err:
harness.charm._get_ingress_auth_data("not-relation-broken-event")

assert err.value.status_type.name == "waiting"
assert "Waiting for the auth provider data." == err.value.msg

def test_get_ingress_auth_data_too_many_relations(
self,
harness,
Expand Down

0 comments on commit 96a0c66

Please sign in to comment.