Skip to content

Commit

Permalink
test: update charm + provider tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoppenheimer committed Oct 10, 2023
1 parent e67739e commit cace648
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
18 changes: 18 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def test_relation_changed_starts_units(harness):
with harness.hooks_disabled():
peer_rel_id = harness.add_relation(PEER, CHARM_KEY)
harness.add_relation_unit(peer_rel_id, f"{CHARM_KEY}/0")
harness.set_planned_units(1)

with (
patch("charm.ZooKeeperCharm.init_server") as patched,
Expand Down Expand Up @@ -215,6 +216,7 @@ def test_relation_changed_updates_quorum(harness):
with harness.hooks_disabled():
peer_rel_id = harness.add_relation(PEER, CHARM_KEY)
harness.add_relation_unit(peer_rel_id, f"{CHARM_KEY}/0")
harness.set_planned_units(1)

with (
patch("charm.ZooKeeperCharm.update_quorum") as patched,
Expand All @@ -229,6 +231,8 @@ def test_relation_changed_restarts(harness):
peer_rel_id = harness.add_relation(PEER, CHARM_KEY)
harness.add_relation_unit(peer_rel_id, f"{CHARM_KEY}/0")
harness.update_relation_data(peer_rel_id, f"{CHARM_KEY}/0", {"state": "started"})
harness.update_relation_data(peer_rel_id, CHARM_KEY, {"0": "added"})
harness.set_planned_units(1)

with (
patch("charms.rolling_ops.v0.rollingops.RollingOpsManager._on_acquire_lock") as patched,
Expand All @@ -244,6 +248,8 @@ def test_relation_changed_defers_upgrading_single_unit(harness):
harness.add_relation_unit(peer_rel_id, f"{CHARM_KEY}/0")
harness.update_relation_data(peer_rel_id, f"{CHARM_KEY}/0", {"state": "started"})
harness.update_relation_data(peer_rel_id, CHARM_KEY, {"upgrading": "started"})
harness.update_relation_data(peer_rel_id, CHARM_KEY, {"0": "added"})
harness.set_planned_units(1)

with (
patch("ops.framework.EventBase.defer") as patched,
Expand Down Expand Up @@ -526,6 +532,7 @@ def test_update_quorum_skips_relation_departed(harness):
with harness.hooks_disabled():
peer_rel_id = harness.add_relation(PEER, CHARM_KEY)
harness.add_relation_unit(peer_rel_id, f"{CHARM_KEY}/0")
harness.set_planned_units(1)

with (
patch("charm.ZooKeeperCharm.add_init_leader") as patched_init_leader,
Expand All @@ -542,6 +549,9 @@ def test_update_quorum_updates_cluster_for_relation_departed(harness):
harness.add_relation_unit(peer_rel_id, f"{CHARM_KEY}/0")
harness.add_relation_unit(peer_rel_id, f"{CHARM_KEY}/1")
harness.set_leader(True)
harness.set_planned_units(1)
harness.update_relation_data(peer_rel_id, f"{CHARM_KEY}/0", {"ip": "gandalf-the-grey"})
harness.update_relation_data(peer_rel_id, f"{CHARM_KEY}/1", {"ip": "gandalf-the-white"})

with (
patch("cluster.ZooKeeperCluster.update_cluster") as patched_update_cluster,
Expand All @@ -556,6 +566,9 @@ def test_update_quorum_updates_cluster_for_leader_elected(harness):
peer_rel_id = harness.add_relation(PEER, CHARM_KEY)
harness.add_relation_unit(peer_rel_id, f"{CHARM_KEY}/0")
harness.add_relation_unit(peer_rel_id, f"{CHARM_KEY}/1")
harness.set_planned_units(2)
harness.update_relation_data(peer_rel_id, f"{CHARM_KEY}/0", {"ip": "gandalf-the-grey"})
harness.update_relation_data(peer_rel_id, f"{CHARM_KEY}/1", {"ip": "gandalf-the-white"})

with (
patch("cluster.ZooKeeperCluster.update_cluster") as patched_update_cluster,
Expand All @@ -574,6 +587,8 @@ def test_update_quorum_adds_init_leader(harness):
patch("charm.ZooKeeperCharm.add_init_leader") as patched_init_leader,
):
harness.add_relation_unit(peer_rel_id, f"{CHARM_KEY}/0")
harness.set_planned_units(1)
harness.update_relation_data(peer_rel_id, f"{CHARM_KEY}/0", {"ip": "gandalf-the-grey"})
harness.set_leader(True)

patched_init_leader.assert_called_once()
Expand Down Expand Up @@ -794,7 +809,10 @@ def test_init_leader_is_added(harness):
peer_rel_id = harness.add_relation(PEER, CHARM_KEY)
harness.set_leader(True)
harness.update_relation_data(peer_rel_id, f"{CHARM_KEY}/0", {"state": "started"})
harness.set_planned_units(2)
harness.add_relation_unit(peer_rel_id, f"{CHARM_KEY}/1")
harness.set_planned_units(2)
harness.update_relation_data(peer_rel_id, f"{CHARM_KEY}/0", {"ip": "gandalf-the-grey"})
harness.update_relation_data(peer_rel_id, f"{CHARM_KEY}/1", {"ip": "gandalf-the-white"})

assert harness.charm.app_peer_data.get("0", None) == "added"
9 changes: 8 additions & 1 deletion tests/unit/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,19 @@ def test_provider_relation_data_updates_port_if_stable_and_ready(harness):
patch("cluster.ZooKeeperCluster.stable", return_value=True),
patch("provider.ZooKeeperProvider.ready", return_value=True),
patch("charm.ZooKeeperCharm.config_changed", return_value=True),
patch("cluster.ZooKeeperCluster.all_units_related", return_value=True),
):
harness.set_leader(True)
harness.update_relation_data(
harness.charm.peer_relation.id,
"zookeeper/0",
{"ip": "treebeard"},
)

harness.update_relation_data(
harness.charm.peer_relation.id,
REL_NAME,
{"quorum": "non-ssl"},
{"quorum": "non-ssl", "0": "added"},
)

patched.assert_called_once()
Expand Down

0 comments on commit cace648

Please sign in to comment.