Skip to content

Commit

Permalink
Fix unit tests to properly set leader status
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielArndt committed Oct 27, 2023
1 parent 9c3e53d commit f334198
Showing 1 changed file with 3 additions and 35 deletions.
38 changes: 3 additions & 35 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,14 @@ def setUp(self, patch_k8s_client):
self.namespace = "whatever"
self.harness = testing.Harness(UPFOperatorCharm)
self.harness.set_model_name(name=self.namespace)
self.harness.set_leader(is_leader=True)

self.root = self.harness.get_filesystem_root("bessd")
(self.root / "etc/bess/conf").mkdir(parents=True)
self.addCleanup(self.harness.cleanup)
self.harness.begin()

def test_given_bad_config_when_config_changed_then_status_is_blocked(self):
self.harness.set_leader(is_leader=True)

self.harness.update_config(key_values={"dnn": ""})

self.assertEqual(
Expand All @@ -92,8 +91,6 @@ def test_given_bessd_config_file_not_yet_written_when_bessd_pebble_ready_then_co
self,
patch_is_ready,
):
self.harness.set_leader(is_leader=True)

self.harness.container_pebble_ready(container_name="bessd")

expected_config_file_content = read_file("tests/unit/expected_upf.json")
Expand All @@ -108,8 +105,6 @@ def test_given_bessd_config_file_not_yet_written_when_config_storage_attached_th
self,
patch_is_ready,
):
self.harness.set_leader(is_leader=True)

self.harness.set_can_connect("bessd", True)
(self.root / "etc/bess/conf").rmdir()
self.harness.add_storage(storage_name="config", count=1)
Expand All @@ -127,7 +122,6 @@ def test_given_bessd_config_file_matches_when_bessd_pebble_ready_then_config_fil
self,
patch_is_ready,
):
self.harness.set_leader(is_leader=True)
patch_is_ready.return_value = True
expected_upf_content = read_file("tests/unit/expected_upf.json")
(self.root / "etc/bess/conf/upf.json").write_text(expected_upf_content)
Expand All @@ -142,7 +136,6 @@ def test_given_when_bessd_pebble_ready_then_expected_pebble_plan_is_applied( #
self,
patch_is_ready,
):
self.harness.set_leader(is_leader=True)
patch_is_ready.return_value = True

self.harness.container_pebble_ready(container_name="bessd")
Expand Down Expand Up @@ -179,7 +172,6 @@ def test_given_when_bessd_pebble_ready_then_expected_pebble_plan_is_applied( #
def test_given_can_connect_to_bessd_when_bessd_pebble_ready_then_ip_route_is_created(
self, patch_exec, patch_is_ready
):
self.harness.set_leader(is_leader=True)
patch_is_ready.return_value = True

self.harness.container_pebble_ready(container_name="bessd")
Expand All @@ -204,7 +196,6 @@ def test_given_can_connect_to_bessd_when_bessd_pebble_ready_then_ip_route_is_cre
def test_given_iptables_rule_is_not_yet_created_when_bessd_pebble_ready_then_rule_is_created(
self, patch_exec, patch_is_ready
):
self.harness.set_leader(is_leader=True)
patch_exec.side_effect = [
MagicMock(),
ExecError(command=[], exit_code=1, stdout="", stderr=""),
Expand Down Expand Up @@ -236,7 +227,6 @@ def test_given_iptables_rule_is_not_yet_created_when_bessd_pebble_ready_then_rul
def test_given_iptables_rule_is_created_when_bessd_pebble_ready_then_rule_is_not_re_created(
self, patch_exec, patch_is_ready
):
self.harness.set_leader(is_leader=True)
patch_is_ready.return_value = True

self.harness.container_pebble_ready(container_name="bessd")
Expand Down Expand Up @@ -265,7 +255,6 @@ def test_given_iptables_rule_is_created_when_bessd_pebble_ready_then_rule_is_not
def test_given_can_connect_to_bessd_when_bessd_pebble_ready_then_bessctl_configure_is_executed(
self, patch_exec, patch_is_ready
):
self.harness.set_leader(is_leader=True)
patch_is_ready.return_value = True

self.harness.container_pebble_ready(container_name="bessd")
Expand All @@ -281,7 +270,6 @@ def test_given_can_connect_to_bessd_when_bessd_pebble_ready_then_bessctl_configu
def test_given_connects_and_bessctl_executed_file_exists_then_bessctl_configure_not_executed(
self, patch_exec, patch_is_ready
):
self.harness.set_leader(is_leader=True)
patch_is_ready.return_value = True

self.harness.container_pebble_ready(container_name="bessd")
Expand All @@ -300,7 +288,6 @@ def test_given_connects_and_bessctl_executed_file_exists_then_bessctl_configure_
def test_given_connects_and_bessctl_executed_file_dont_exist_then_bessctl_configure_executed(
self, patch_exec, patch_is_ready
):
self.harness.set_leader(is_leader=True)
patch_is_ready.return_value = True

self.harness.container_pebble_ready(container_name="bessd")
Expand All @@ -317,7 +304,6 @@ def test_given_storage_not_attached_when_bessd_pebble_ready_then_status_is_waiti
self,
patch_is_ready,
):
self.harness.set_leader(is_leader=True)
patch_is_ready.return_value = True
(self.root / "etc/bess/conf").rmdir()

Expand All @@ -333,7 +319,6 @@ def test_given_multus_not_configured_when_bessd_pebble_ready_then_status_is_wait
self,
patch_is_ready,
):
self.harness.set_leader(is_leader=True)
patch_is_ready.return_value = False

self.harness.container_pebble_ready(container_name="bessd")
Expand All @@ -354,7 +339,6 @@ def test_given_config_file_is_written_and_all_services_are_running_when_bessd_pe
service_info_mock = Mock()
service_info_mock.is_running.return_value = True
patch_get_service.return_value = service_info_mock
self.harness.set_leader(is_leader=True)
patch_is_ready.return_value = True
self.harness.set_can_connect(container="pfcp-agent", val=True)

Expand All @@ -370,7 +354,6 @@ def test_given_bessd_service_is_running_when_pfcp_agent_pebble_ready_then_pebble
service_info_mock = Mock()
service_info_mock.is_running.return_value = True
patch_get_service.return_value = service_info_mock
self.harness.set_leader(is_leader=True)
self.harness.set_can_connect(container="bessd", val=True)

self.harness.container_pebble_ready(container_name="pfcp-agent")
Expand All @@ -392,7 +375,6 @@ def test_given_bessd_service_is_running_when_pfcp_agent_pebble_ready_then_pebble
def test_given_cant_connect_to_bessd_container_when_pfcp_agent_pebble_ready_then_status_is_waiting( # noqa: E501
self,
):
self.harness.set_leader(is_leader=True)
self.harness.set_can_connect(container="bessd", val=False)

self.harness.container_pebble_ready(container_name="pfcp-agent")
Expand All @@ -409,7 +391,6 @@ def test_given_fiveg_n3_relation_created_when_fiveg_n3_request_then_upf_ip_addre
patch_hugepages_is_patched,
):
patch_hugepages_is_patched.return_value = True
self.harness.set_leader(is_leader=True)
test_upf_access_ip_cidr = "1.2.3.4/21"
self.harness.update_config(key_values={"access-ip": test_upf_access_ip_cidr})

Expand All @@ -424,6 +405,7 @@ def test_given_fiveg_n3_relation_created_when_fiveg_n3_request_then_upf_ip_addre
def test_given_unit_is_not_leader_when_fiveg_n3_request_then_upf_ip_address_is_not_published(
self, patched_publish_upf_information
):
self.harness.set_leader(is_leader=False)
test_upf_access_ip_cidr = "1.2.3.4/21"
self.harness.update_config(key_values={"access-ip": test_upf_access_ip_cidr})

Expand All @@ -447,7 +429,6 @@ def test_given_fiveg_n3_relation_exists_when_access_ip_config_changed_then_new_u
patch_hugepages_is_patched.return_value = True
self.harness.set_can_connect(container="bessd", val=True)
self.harness.set_can_connect(container="pfcp-agent", val=True)
self.harness.set_leader(is_leader=True)
n3_relation_id = self.harness.add_relation("fiveg_n3", "n3_requirer_app")
self.harness.add_relation_unit(n3_relation_id, "n3_requirer_app/0")
test_upf_access_ip_cidr = "1.2.3.4/21"
Expand All @@ -469,7 +450,6 @@ def test_given_fiveg_n3_relation_exists_when_access_ip_config_changed_to_invalid
patch_multus_is_ready.return_value = True
self.harness.set_can_connect(container="bessd", val=True)
self.harness.set_can_connect(container="pfcp-agent", val=True)
self.harness.set_leader(is_leader=True)
n3_relation_id = self.harness.add_relation("fiveg_n3", "n3_requirer_app")
self.harness.add_relation_unit(n3_relation_id, "n3_requirer_app/0")
invalid_test_upf_access_ip_cidr = "1111.2.3.4/21"
Expand All @@ -484,6 +464,7 @@ def test_given_fiveg_n3_relation_exists_when_access_ip_config_changed_to_invalid
def test_given_unit_is_not_leader_when_fiveg_n4_request_then_upf_hostname_is_not_published(
self, patched_publish_upf_n4_information
):
self.harness.set_leader(is_leader=False)
test_external_upf_hostname = "test-upf.external.hostname.com"
self.harness.update_config(
key_values={"external-upf-hostname": test_external_upf_hostname}
Expand All @@ -503,7 +484,6 @@ def test_given_external_upf_hostname_config_set_and_fiveg_n4_relation_created_wh
patch_hugepages_is_patched,
):
patch_hugepages_is_patched.return_value = True
self.harness.set_leader(is_leader=True)
test_external_upf_hostname = "test-upf.external.hostname.com"
self.harness.update_config(
key_values={"external-upf-hostname": test_external_upf_hostname}
Expand All @@ -525,7 +505,6 @@ def test_given_external_upf_hostname_config_set_and_fiveg_n4_relation_created_wh
def test_given_external_upf_hostname_config_not_set_but_external_upf_service_hostname_available_and_fiveg_n4_relation_created_when_fiveg_n4_request_then_upf_hostname_and_n4_port_is_published( # noqa: E501
self, patched_publish_upf_n4_information, patched_lightkube_client_get
):
self.harness.set_leader(is_leader=True)
test_external_upf_service_hostname = "test-upf.external.service.hostname.com"
service = Mock(
status=Mock(
Expand All @@ -552,7 +531,6 @@ def test_given_external_upf_hostname_config_not_set_but_external_upf_service_hos
def test_given_external_upf_hostname_config_not_set_and_external_upf_service_hostname_not_available_and_fiveg_n4_relation_created_when_fiveg_n4_request_then_upf_hostname_and_n4_port_is_published( # noqa: E501
self, patched_publish_upf_n4_information, patched_lightkube_client_get
):
self.harness.set_leader(is_leader=True)
service = Mock(status=Mock(loadBalancer=Mock(ingress=[Mock(ip="1.1.1.1", spec=["ip"])])))
patched_lightkube_client_get.return_value = service

Expand Down Expand Up @@ -582,7 +560,6 @@ def test_given_fiveg_n4_relation_exists_when_external_upf_hostname_config_change
patch_hugepages_is_ready.return_value = True
self.harness.set_can_connect(container="bessd", val=True)
self.harness.set_can_connect(container="pfcp-agent", val=True)
self.harness.set_leader(is_leader=True)
self.harness.update_config(key_values={"external-upf-hostname": "whatever.com"})
n4_relation_id = self.harness.add_relation("fiveg_n4", "n4_requirer_app")
self.harness.add_relation_unit(n4_relation_id, "n4_requirer_app/0")
Expand All @@ -609,7 +586,6 @@ def test_given_default_config_when_network_attachment_definitions_from_config_is
patch_hugepages_is_patched,
):
patch_hugepages_is_patched.return_value = True
self.harness.set_leader(is_leader=True)
self.harness.update_config(
key_values={
"access-ip": DEFAULT_ACCESS_IP,
Expand Down Expand Up @@ -713,7 +689,6 @@ def test_given_default_config_when_network_attachment_definitions_from_config_is
patch_hugepages_is_patched,
):
patch_hugepages_is_patched.return_value = True
self.harness.set_leader(is_leader=True)
self.harness.update_config(
key_values={
"access-ip": "192.168.252.3/24",
Expand All @@ -734,7 +709,6 @@ def test_given_default_config_with_interfaces_mtu_sizes_when_network_attachment_
patch_hugepages_is_patched,
):
patch_hugepages_is_patched.return_value = True
self.harness.set_leader(is_leader=True)
self.harness.update_config(
key_values={
"access-ip": "192.168.252.3/24",
Expand All @@ -754,7 +728,6 @@ def test_given_default_config_with_interfaces_mtu_sizes_when_network_attachment_
def test_given_default_config_with_interfaces_too_small_and_too_big_mtu_sizes_when_network_attachment_definitions_from_config_is_called_then_status_is_blocked( # noqa: E501
self,
):
self.harness.set_leader(is_leader=True)
self.harness.update_config(
key_values={
"access-interface-mtu-size": TOO_SMALL_MTU_SIZE,
Expand All @@ -771,7 +744,6 @@ def test_given_default_config_with_interfaces_too_small_and_too_big_mtu_sizes_wh
def test_given_default_config_with_interfaces_zero_mtu_sizes_when_network_attachment_definitions_from_config_is_called_then_status_is_blocked( # noqa: E501
self,
):
self.harness.set_leader(is_leader=True)
self.harness.update_config(
key_values={
"access-interface-mtu-size": ZERO_MTU_SIZE,
Expand Down Expand Up @@ -802,7 +774,6 @@ def test_given_container_can_connect_bessd_pebble_ready_when_core_net_mtu_config
patch_multus_is_ready.return_value = True
self.harness.set_can_connect(container="bessd", val=True)
self.harness.set_can_connect(container="pfcp-agent", val=True)
self.harness.set_leader(is_leader=True)
original_nads = self.harness.charm._network_attachment_definitions_from_config()
update_nad_labels(original_nads, self.harness.charm.app.name)
patch_list_na_definitions.return_value = original_nads
Expand All @@ -826,7 +797,6 @@ def test_given_container_can_connect_bessd_pebble_ready_when_core_net_mtu_config
patch_multus_is_ready.return_value = True
self.harness.set_can_connect(container="bessd", val=True)
self.harness.set_can_connect(container="pfcp-agent", val=True)
self.harness.set_leader(is_leader=True)
original_nads = self.harness.charm._network_attachment_definitions_from_config()
update_nad_labels(original_nads, self.harness.charm.app.name)
patch_list_na_definitions.return_value = original_nads
Expand Down Expand Up @@ -855,7 +825,6 @@ def test_given_container_can_connect_bessd_pebble_ready_when_core_net_mtu_config
patch_multus_is_ready.return_value = True
self.harness.set_can_connect(container="bessd", val=True)
self.harness.set_can_connect(container="pfcp-agent", val=True)
self.harness.set_leader(is_leader=True)
original_nads = self.harness.charm._network_attachment_definitions_from_config()
update_nad_labels(original_nads, self.harness.charm.app.name)
patch_list_na_definitions.return_value = original_nads
Expand Down Expand Up @@ -892,7 +861,6 @@ def test_given_container_can_connect_bessd_pebble_ready_when_core_net_mtu_config
patch_multus_is_ready.return_value = True
self.harness.set_can_connect(container="bessd", val=True)
self.harness.set_can_connect(container="pfcp-agent", val=True)
self.harness.set_leader(is_leader=True)
original_nads = self.harness.charm._network_attachment_definitions_from_config()
update_nad_labels(original_nads, self.harness.charm.app.name)
patch_list_na_definitions.return_value = original_nads
Expand Down

0 comments on commit f334198

Please sign in to comment.