diff --git a/tests/unit/python/utils/tests/swarm/test_mesh_steps.py b/tests/unit/python/utils/tests/swarm/test_mesh_steps.py index da72c74a57..fc3a79fe3b 100644 --- a/tests/unit/python/utils/tests/swarm/test_mesh_steps.py +++ b/tests/unit/python/utils/tests/swarm/test_mesh_steps.py @@ -33,7 +33,11 @@ class TestSwitchToMeshTransport(unittest.TestCase): return host_vars def _run(self, td: str) -> int: - env = {"INFINITO_SWARM_VPN": "true", "KEY_PATH": str(Path(td) / "admin.key")} + env = { + "INFINITO_SWARM_VPN": "true", + "INFINITO_SWARM_MESHES": "", + "KEY_PATH": str(Path(td) / "admin.key"), + } with mock.patch.dict("os.environ", env, clear=False): return mesh_steps.switch_to_mesh_transport(inv_dir=td) @@ -56,5 +60,85 @@ class TestSwitchToMeshTransport(unittest.TestCase): self.assertNotIn("ansible_host", doc) +EXAMPLE = "inventories/examples/wireguard-three-meshes/meshes.yml" + + +class TestDeclarations(unittest.TestCase): + def _env(self, meshes: str): + return mock.patch.dict( + "os.environ", + {"INFINITO_SWARM_VPN": "true", "INFINITO_SWARM_MESHES": meshes}, + clear=False, + ) + + def test_an_unset_choice_deploys_the_development_meshes(self): + with self._env(""): + self.assertEqual(mesh_steps.transport_mesh(), "swarm") + + def test_an_example_moves_the_transport_onto_its_first_mesh(self): + with self._env(EXAMPLE): + self.assertEqual(mesh_steps.transport_mesh(), "alpha") + + def test_the_writer_is_handed_the_chosen_declarations(self): + with ( + self._env(EXAMPLE), + mock.patch.object(mesh_steps, "run_step", return_value=0) as step, + ): + self.assertEqual(mesh_steps.write_mesh(inv_dir="/tmp/inv"), 0) + argv = step.call_args.args[0] + self.assertTrue(argv[argv.index("--group-vars-file") + 1].endswith(EXAMPLE)) + self.assertEqual(argv[argv.index("--controller-mesh") + 1], "alpha") + + +LINKS = """[ + {"ifname": "alpha", "addr_info": [{"local": "10.100.10.11", "prefixlen": 24}]}, + {"ifname": "swarm", "addr_info": [{"local": "10.100.0.11", "prefixlen": 24}]}, + {"ifname": "wg0", "addr_info": [{"local": "10.8.0.2", "prefixlen": 24}]} +]""" + + +class TestStaleControllerInterfaces(unittest.TestCase): + """A controller link left by other declarations still routes the pool. + + The next round's ``wg-quick up`` then fails with ``RTNETLINK answers: File + exists``, which is how a development round died after a three-mesh one. + """ + + def _stale(self) -> list[str]: + listed = mock.Mock(stdout=LINKS) + with ( + mock.patch.dict("os.environ", {"INFINITO_SWARM_MESHES": ""}, clear=False), + mock.patch.object(mesh_steps.subprocess, "run", return_value=listed), + ): + return mesh_steps.stale_controller_interfaces() + + def test_a_pool_link_no_declaration_names_is_stale(self): + self.assertIn("alpha", self._stale()) + + def test_a_declared_mesh_is_kept(self): + self.assertNotIn("swarm", self._stale()) + + def test_a_link_outside_the_pool_is_not_the_harness_to_remove(self): + self.assertNotIn("wg0", self._stale()) + + def test_the_stale_link_goes_before_the_controller_joins(self): + calls: list[list[str]] = [] + with ( + tempfile.TemporaryDirectory() as td, + mock.patch.dict("os.environ", {"INFINITO_SWARM_VPN": "true"}, clear=False), + mock.patch.object( + mesh_steps, "stale_controller_interfaces", return_value=["alpha"] + ), + mock.patch.object( + mesh_steps, + "run_step", + side_effect=lambda cmd, **_: calls.append(cmd) or 0, + ), + ): + self.assertEqual(mesh_steps.mesh_controller(inv_dir=td), 0) + self.assertEqual(calls[0][-4:], ["ip", "link", "delete", "alpha"]) + self.assertEqual(calls[1][0], "ansible-playbook") + + if __name__ == "__main__": unittest.main()