Skip to content

Commit

Permalink
test: add unit-tests for chroot fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoppenheimer committed Jan 25, 2024
1 parent 4f69c87 commit 7e47a0b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
24 changes: 24 additions & 0 deletions tests/unit/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pytest
import yaml
from ops import RelationBrokenEvent
from ops.testing import Harness

from charm import ZooKeeperCharm
Expand Down Expand Up @@ -80,6 +81,29 @@ def test_client_relation_updated_creates_passwords_with_chroot(harness):
assert harness.charm.state.cluster.client_passwords


def test_client_relation_broken_sets_acls_with_broken_events(harness):
with harness.hooks_disabled():
app_id = harness.add_relation(REL_NAME, "application")
harness.set_leader(True)

with (
patch("core.cluster.ClusterState.stable", new_callable=PropertyMock, return_value=True),
patch("managers.quorum.QuorumManager.update_acls") as patched_update_acls,
patch("charms.rolling_ops.v0.rollingops.RollingOpsManager._on_acquire_lock"),
):
harness.update_relation_data(app_id, "application", {"chroot": "balrog"})
patched_update_acls.assert_called_with(event=None)

with (
patch("core.cluster.ClusterState.stable", new_callable=PropertyMock, return_value=True),
patch("managers.quorum.QuorumManager.update_acls") as patched_update_acls,
patch("charms.rolling_ops.v0.rollingops.RollingOpsManager._on_acquire_lock"),
):
harness.remove_relation(app_id)

isinstance(patched_update_acls.call_args_list[0], RelationBrokenEvent)


def test_client_relation_broken_removes_passwords(harness):
with harness.hooks_disabled():
harness.set_leader(True)
Expand Down
55 changes: 53 additions & 2 deletions tests/unit/test_quorum.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

import logging
from pathlib import Path
from unittest.mock import DEFAULT, MagicMock, patch

import pytest
import yaml
from ops.testing import Harness

from charm import ZooKeeperCharm
from literals import CHARM_KEY
from literals import CHARM_KEY, PEER, REL_NAME

logger = logging.getLogger(__name__)

Expand All @@ -22,9 +23,10 @@
@pytest.fixture
def harness():
harness = Harness(ZooKeeperCharm, meta=METADATA, config=CONFIG, actions=ACTIONS)
harness.add_relation("restart", CHARM_KEY)
upgrade_rel_id = harness.add_relation("upgrade", CHARM_KEY)
harness.add_relation("restart", CHARM_KEY)
harness.update_relation_data(upgrade_rel_id, f"{CHARM_KEY}/0", {"state": "idle"})
harness.add_relation(PEER, CHARM_KEY)
harness._update_config({"init-limit": 5, "sync-limit": 2, "tick-time": 2000})
harness.begin()
return harness
Expand Down Expand Up @@ -57,3 +59,52 @@ def test_is_child_of_not(harness):
chroots = {"/gandalf", "/saruman"}

assert not harness.charm.quorum_manager._is_child_of(path=chroot, chroots=chroots)


def test_update_acls_correctly_handles_relation_chroots(harness):
dummy_leader_znodes = {
"/fellowship",
"/fellowship/men",
"/fellowship/dwarves",
"/fellowship/men/aragorn",
"/fellowship/men/boromir",
"/fellowship/elves/legolas",
"/fellowship/dwarves/gimli",
"/fellowship/men/aragorn/anduril",
}

with harness.hooks_disabled():
app_id = harness.add_relation(REL_NAME, "application")
harness.update_relation_data(app_id, "application", {"chroot": "/rohan"})
harness.set_leader(True)

with patch.multiple(
"charms.zookeeper.v0.client.ZooKeeperManager",
get_leader=DEFAULT,
leader_znodes=MagicMock(return_value=dummy_leader_znodes),
create_znode_leader=DEFAULT,
set_acls_znode_leader=DEFAULT,
delete_znode_leader=DEFAULT,
) as patched_manager:
harness.charm.quorum_manager.update_acls()

for _, kwargs in patched_manager["create_znode_leader"].call_args_list:
assert "/rohan" in kwargs["path"]

for _, kwargs in patched_manager["set_acls_znode_leader"].call_args_list:
assert "/rohan" in kwargs["path"]

removed_men = False
for counter, call in enumerate(patched_manager["delete_znode_leader"].call_args_list):
_, kwargs = call

if "/fellowship/men" in kwargs["path"]:
assert not removed_men, "Parent zNode removed before all it's children"

if kwargs["path"] == "/fellowship/men":
removed_men = True

# ensure last node to go is the parent
assert (
patched_manager["delete_znode_leader"].call_args_list[-1][1]["path"] == "/fellowship"
)

0 comments on commit 7e47a0b

Please sign in to comment.