mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-29 14:30:52 +00:00
Merge branch 'main' into feature/store-i18n
This commit is contained in:
342 files changed
+7444
-1661
No files matched your search
@@ -55,6 +55,45 @@ class TestMirrorHostVars(unittest.TestCase):
|
||||
"rotated: true\n",
|
||||
)
|
||||
|
||||
def test_a_kept_subtree_stays_with_its_own_host(self):
|
||||
self.source.write_text(
|
||||
"rotated: true\napplications:\n svc-net-wireguard:\n"
|
||||
" meshes:\n swarm:\n host: mgr-01\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
(self.host_vars_dir / "wrk-01.yml").write_text(
|
||||
"rotated: false\napplications:\n svc-net-wireguard:\n"
|
||||
" meshes:\n swarm:\n host: wrk-01\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
_mirror_host_vars(
|
||||
self.host_vars_dir, self.source, keep=["applications.svc-net-wireguard"]
|
||||
)
|
||||
|
||||
content = (self.host_vars_dir / "wrk-01.yml").read_text( # nocheck: cache-read
|
||||
encoding="utf-8"
|
||||
)
|
||||
self.assertIn("rotated: true", content)
|
||||
self.assertIn("host: wrk-01", content)
|
||||
self.assertNotIn("host: mgr-01", content)
|
||||
|
||||
def test_a_host_without_the_kept_subtree_takes_the_source(self):
|
||||
self.source.write_text(
|
||||
"applications:\n svc-net-wireguard:\n meshes:\n"
|
||||
" swarm:\n host: mgr-01\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
_mirror_host_vars(
|
||||
self.host_vars_dir, self.source, keep=["applications.svc-net-wireguard"]
|
||||
)
|
||||
|
||||
content = (self.host_vars_dir / "wrk-01.yml").read_text( # nocheck: cache-read
|
||||
encoding="utf-8"
|
||||
)
|
||||
self.assertIn("host: mgr-01", content)
|
||||
|
||||
def test_a_backup_is_not_mirrored_over(self):
|
||||
backup = _backup(self.source)
|
||||
_mirror_host_vars(self.host_vars_dir, self.source)
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
from pathlib import Path
|
||||
|
||||
PROJECT_ROOT: Path = Path(__file__).resolve().parents[7]
|
||||
@@ -0,0 +1,190 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from cli.administration.inventory.mesh.__main__ import main
|
||||
from utils.cache.yaml import dump_yaml
|
||||
|
||||
from .test_write import (
|
||||
BACKUP_INVENTORY,
|
||||
CLUSTER_INVENTORY,
|
||||
HOSTS,
|
||||
LAYOUT,
|
||||
VAULT_PASSWORD,
|
||||
interface_entries,
|
||||
layout_applications,
|
||||
)
|
||||
|
||||
CONTROLLER = "deploy-controller"
|
||||
SWARM_HOSTS = ("swarm-mgr-01", "swarm-wrk-01", "swarm-wrk-02")
|
||||
|
||||
|
||||
class TestMeshCli(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self._tmp = tempfile.TemporaryDirectory()
|
||||
root = Path(self._tmp.name)
|
||||
self.host_vars = root / "host_vars"
|
||||
self.host_vars.mkdir()
|
||||
layout = layout_applications(LAYOUT)
|
||||
for host in HOSTS:
|
||||
dump_yaml(str(self.host_vars / f"{host}.yml"), {"applications": layout})
|
||||
self.cluster = root / "devices.yml"
|
||||
self.cluster.write_text(CLUSTER_INVENTORY, encoding="utf-8")
|
||||
self.backup = root / "backup.yml"
|
||||
self.backup.write_text(BACKUP_INVENTORY, encoding="utf-8")
|
||||
self.vault_file = root / ".password"
|
||||
self.vault_file.write_text(VAULT_PASSWORD, encoding="utf-8")
|
||||
|
||||
def tearDown(self):
|
||||
self._tmp.cleanup()
|
||||
|
||||
def _text(self, host: str) -> str:
|
||||
path = self.host_vars / f"{host}.yml"
|
||||
return path.read_text(encoding="utf-8") # nocheck: cache-read
|
||||
|
||||
def _argv(self, *extra: str) -> list[str]:
|
||||
return [
|
||||
"--inventory",
|
||||
str(self.cluster),
|
||||
"--inventory",
|
||||
str(self.backup),
|
||||
"--host-vars-dir",
|
||||
str(self.host_vars),
|
||||
"--vault-password-file",
|
||||
str(self.vault_file),
|
||||
*extra,
|
||||
]
|
||||
|
||||
def _mesh_names(self, host: str) -> list[str]:
|
||||
return sorted(
|
||||
entry["name"] for entry in interface_entries(self.host_vars, host)
|
||||
)
|
||||
|
||||
def test_one_invocation_writes_every_member(self):
|
||||
self.assertEqual(main(self._argv()), 0)
|
||||
for host in HOSTS:
|
||||
with self.subTest(host=host):
|
||||
self.assertIn("$ANSIBLE_VAULT", self._text(host))
|
||||
|
||||
def test_a_missing_host_vars_dir_fails_rather_than_writing_nothing(self):
|
||||
argv = self._argv()
|
||||
argv[argv.index("--host-vars-dir") + 1] = str(self.host_vars / "absent")
|
||||
self.assertEqual(main(argv), 1)
|
||||
|
||||
def test_an_unknown_mesh_name_fails(self):
|
||||
self.assertEqual(main(self._argv("--mesh", "no-such-mesh")), 1)
|
||||
|
||||
def test_restricting_to_one_mesh_leaves_the_other_unwritten(self):
|
||||
self.assertEqual(main(self._argv("--mesh", "swarm")), 0)
|
||||
self.assertEqual([], self._mesh_names("nfs-server"))
|
||||
|
||||
def test_the_controller_is_pruned_although_no_group_holds_it(self):
|
||||
argv = self._argv("--controller", CONTROLLER, "--controller-mesh", "swarm")
|
||||
self.assertEqual(main(argv), 0)
|
||||
(self.host_vars / f"{CONTROLLER}.yml").write_text(
|
||||
self._text("swarm-mgr-01"), encoding="utf-8"
|
||||
)
|
||||
|
||||
self.assertEqual(main(argv), 0)
|
||||
self.assertEqual(["swarm"], self._mesh_names(CONTROLLER))
|
||||
|
||||
def test_the_controller_joins_the_first_declared_mesh_by_default(self):
|
||||
self.assertEqual(main(self._argv("--controller", CONTROLLER)), 0)
|
||||
self.assertEqual(["swarm"], self._mesh_names(CONTROLLER))
|
||||
|
||||
def test_a_host_vars_tree_without_declarations_fails(self):
|
||||
for host in HOSTS:
|
||||
(self.host_vars / f"{host}.yml").write_text("---\n", encoding="utf-8")
|
||||
self.assertEqual(main(self._argv()), 1)
|
||||
|
||||
def test_a_controller_mesh_nobody_declared_fails(self):
|
||||
argv = self._argv("--controller", CONTROLLER, "--controller-mesh", "absent")
|
||||
self.assertEqual(main(argv), 1)
|
||||
|
||||
def test_an_endpoint_replaces_the_hostname_peers_dial(self):
|
||||
argv = self._argv("--endpoint", "swarm-mgr-01=192.168.244.10")
|
||||
self.assertEqual(main(argv), 0)
|
||||
text = self._text("swarm-wrk-01")
|
||||
self.assertIn("endpoint: 192.168.244.10", text)
|
||||
self.assertIn("host: swarm-mgr-01", text)
|
||||
|
||||
def test_a_host_without_an_endpoint_is_dialled_by_name(self):
|
||||
self.assertEqual(main(self._argv()), 0)
|
||||
self.assertIn("endpoint: swarm-mgr-01", self._text("swarm-wrk-01"))
|
||||
|
||||
def test_a_malformed_endpoint_aborts(self):
|
||||
with self.assertRaises(SystemExit):
|
||||
main(self._argv("--endpoint", "swarm-mgr-01"))
|
||||
|
||||
def test_a_rerun_keeps_the_controller_key_too(self):
|
||||
argv = self._argv("--controller", CONTROLLER, "--controller-mesh", "swarm")
|
||||
self.assertEqual(main(argv), 0)
|
||||
before = self._text(CONTROLLER)
|
||||
|
||||
self.assertEqual(main(argv), 0)
|
||||
self.assertEqual(self._text(CONTROLLER), before)
|
||||
|
||||
def test_a_rotation_gives_a_host_one_key_across_every_mesh(self):
|
||||
self.assertEqual(main(self._argv()), 0)
|
||||
self.assertEqual(main(self._argv("--rotate")), 0)
|
||||
|
||||
own = [
|
||||
entry["public_key"]
|
||||
for entry in interface_entries(self.host_vars, "swarm-mgr-01")
|
||||
]
|
||||
spoke_peers = [
|
||||
peer["public_key"]
|
||||
for entry in interface_entries(self.host_vars, "swarm-wrk-01")
|
||||
for peer in entry["peers"]
|
||||
]
|
||||
|
||||
self.assertEqual(2, len(own))
|
||||
self.assertEqual(1, len(set(own)))
|
||||
self.assertIn(own[0], spoke_peers)
|
||||
|
||||
def _address(self, host: str, mesh_name: str) -> str:
|
||||
return next(
|
||||
entry["address"]
|
||||
for entry in interface_entries(self.host_vars, host)
|
||||
if entry["name"] == mesh_name
|
||||
)
|
||||
|
||||
def test_growing_the_cluster_leaves_every_member_on_its_address(self):
|
||||
self.assertEqual(main(self._argv()), 0)
|
||||
before = {host: self._address(host, "swarm") for host in SWARM_HOSTS}
|
||||
|
||||
dump_yaml(
|
||||
str(self.host_vars / "alpha-wrk-00.yml"),
|
||||
{"applications": layout_applications(LAYOUT)},
|
||||
)
|
||||
self.cluster.write_text(
|
||||
CLUSTER_INVENTORY.replace(
|
||||
" swarm-wrk-01: {}",
|
||||
" alpha-wrk-00: {}\n swarm-wrk-01: {}",
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
self.assertEqual(main(self._argv()), 0)
|
||||
|
||||
for host, address in before.items():
|
||||
with self.subTest(host=host):
|
||||
self.assertEqual(self._address(host, "swarm"), address)
|
||||
self.assertNotIn(self._address("alpha-wrk-00", "swarm"), set(before.values()))
|
||||
|
||||
def test_a_rotation_leaves_every_member_on_its_address(self):
|
||||
self.assertEqual(main(self._argv()), 0)
|
||||
before = {host: self._address(host, "swarm") for host in SWARM_HOSTS}
|
||||
self.assertEqual(main(self._argv("--rotate")), 0)
|
||||
for host, address in before.items():
|
||||
with self.subTest(host=host):
|
||||
self.assertEqual(self._address(host, "swarm"), address)
|
||||
|
||||
def test_a_rerun_is_a_no_op(self):
|
||||
self.assertEqual(main(self._argv()), 0)
|
||||
before = {host: self._text(host) for host in HOSTS}
|
||||
self.assertEqual(main(self._argv()), 0)
|
||||
for host in HOSTS:
|
||||
with self.subTest(host=host):
|
||||
self.assertEqual(self._text(host), before[host])
|
||||
@@ -0,0 +1,221 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import tempfile
|
||||
import unittest
|
||||
from contextlib import redirect_stdout
|
||||
from pathlib import Path
|
||||
|
||||
from cli.administration.inventory.mesh.__main__ import main
|
||||
from cli.administration.inventory.mesh.inventory import (
|
||||
declared_specs,
|
||||
groups_of,
|
||||
topology_of,
|
||||
)
|
||||
from cli.administration.inventory.mesh.plan import plan_mesh, primary_meshes
|
||||
from utils.cache.yaml import dump_yaml
|
||||
|
||||
from .test_write import VAULT_PASSWORD, interface_entries
|
||||
|
||||
DECLARED = {
|
||||
"svc-net-wireguard": {
|
||||
"networks": {
|
||||
"meshes": [
|
||||
{"name": "alpha", "subnet": "10.100.20.0/24", "listen_port": 51840},
|
||||
{"name": "beta", "subnet": "10.100.21.0/24", "listen_port": 51841},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
INVENTORY = """\
|
||||
all:
|
||||
children:
|
||||
svc-net-wireguard:
|
||||
hosts:
|
||||
host-1: {}
|
||||
host-2: {}
|
||||
host-3: {}
|
||||
host-4: {}
|
||||
web-app-a:
|
||||
hosts:
|
||||
host-1: {}
|
||||
host-2: {}
|
||||
host-3: {}
|
||||
host-4: {}
|
||||
"""
|
||||
|
||||
MEMBERSHIP = {
|
||||
"host-1": ["alpha"],
|
||||
"host-2": ["alpha", "beta"],
|
||||
"host-3": ["beta"],
|
||||
"host-4": ["alpha"],
|
||||
}
|
||||
|
||||
|
||||
class FullMeshOnDisk(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self._tmp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self._tmp.cleanup)
|
||||
self.root = Path(self._tmp.name)
|
||||
self.host_vars = self.root / "host_vars"
|
||||
self.host_vars.mkdir()
|
||||
self.inventory = self.root / "devices.yml"
|
||||
self.inventory.write_text(INVENTORY, encoding="utf-8")
|
||||
self.vault_file = self.root / ".password"
|
||||
self.vault_file.write_text(VAULT_PASSWORD, encoding="utf-8")
|
||||
for host, meshes in MEMBERSHIP.items():
|
||||
self._host(host, meshes)
|
||||
|
||||
def _host(self, host: str, meshes: list[str], declared: dict | None = None):
|
||||
applications = {
|
||||
**(DECLARED if declared is None else declared),
|
||||
"web-app-a": {"networks": {"meshes": meshes}},
|
||||
}
|
||||
dump_yaml(str(self.host_vars / f"{host}.yml"), {"applications": applications})
|
||||
|
||||
def _groups(self):
|
||||
return groups_of([self.inventory])
|
||||
|
||||
def _write(self) -> str:
|
||||
argv = [
|
||||
"--inventory",
|
||||
str(self.inventory),
|
||||
"--host-vars-dir",
|
||||
str(self.host_vars),
|
||||
"--vault-password-file",
|
||||
str(self.vault_file),
|
||||
]
|
||||
out = io.StringIO()
|
||||
with redirect_stdout(out):
|
||||
self.assertEqual(0, main(argv))
|
||||
return out.getvalue()
|
||||
|
||||
def _entry(self, host: str, mesh: str) -> dict:
|
||||
return next(
|
||||
entry
|
||||
for entry in interface_entries(self.host_vars, host)
|
||||
if entry["name"] == mesh
|
||||
)
|
||||
|
||||
|
||||
class TestDeclarations(FullMeshOnDisk, unittest.TestCase):
|
||||
def test_every_host_may_declare_the_meshes(self):
|
||||
specs = declared_specs(self._groups(), self.host_vars, [self.inventory])
|
||||
self.assertEqual(["alpha", "beta"], [spec.name for spec in specs])
|
||||
|
||||
def test_a_host_declaring_nothing_reads_the_others(self):
|
||||
self._host("host-1", ["alpha"], declared={})
|
||||
specs = declared_specs(self._groups(), self.host_vars, [self.inventory])
|
||||
self.assertEqual(["alpha", "beta"], [spec.name for spec in specs])
|
||||
|
||||
def test_hosts_declaring_different_meshes_are_refused(self):
|
||||
other = {
|
||||
"svc-net-wireguard": {
|
||||
"networks": {
|
||||
"meshes": [
|
||||
{
|
||||
"name": "alpha",
|
||||
"subnet": "10.100.30.0/24",
|
||||
"listen_port": 51840,
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
self._host("host-3", ["alpha"], declared=other)
|
||||
with self.assertRaises(ValueError) as raised:
|
||||
declared_specs(self._groups(), self.host_vars, [self.inventory])
|
||||
self.assertIn("host-3", str(raised.exception))
|
||||
|
||||
def test_no_host_declaring_any_mesh_is_refused(self):
|
||||
for host, meshes in MEMBERSHIP.items():
|
||||
self._host(host, meshes, declared={})
|
||||
with self.assertRaises(ValueError) as raised:
|
||||
declared_specs(self._groups(), self.host_vars, [self.inventory])
|
||||
self.assertIn("networks.meshes", str(raised.exception))
|
||||
|
||||
|
||||
class TestPlanning(FullMeshOnDisk, unittest.TestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
groups = self._groups()
|
||||
self.specs = declared_specs(groups, self.host_vars, [self.inventory])
|
||||
self.topology = topology_of(groups, self.host_vars, self.specs)
|
||||
self.alpha = plan_mesh(self.specs[0], self.topology)
|
||||
|
||||
def test_there_is_no_hub(self):
|
||||
self.assertIsNone(self.topology.hub)
|
||||
self.assertTrue(self.alpha.is_full)
|
||||
self.assertFalse(any(member.is_hub for member in self.alpha.members))
|
||||
|
||||
def test_every_member_peers_with_every_other(self):
|
||||
hosts = {member.host for member in self.alpha.members}
|
||||
self.assertEqual({"host-1", "host-2", "host-4"}, hosts)
|
||||
for host in hosts:
|
||||
with self.subTest(host=host):
|
||||
peers = {peer.host for peer in self.alpha.peers_of(host)}
|
||||
self.assertEqual(hosts - {host}, peers)
|
||||
|
||||
def test_no_two_members_share_an_address(self):
|
||||
addresses = [member.address for member in self.alpha.members]
|
||||
self.assertEqual(len(addresses), len(set(addresses)))
|
||||
|
||||
def test_a_host_of_two_meshes_is_primary_on_the_first(self):
|
||||
primary = primary_meshes(self.specs, self.topology)
|
||||
self.assertEqual("alpha", primary["host-2"])
|
||||
self.assertEqual("beta", primary["host-3"])
|
||||
|
||||
|
||||
class TestWritten(FullMeshOnDisk, unittest.TestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.out = self._write()
|
||||
|
||||
def test_the_cli_reports_full_meshes(self):
|
||||
self.assertIn("mesh 'alpha': full mesh of 3 member(s)", self.out)
|
||||
self.assertIn("mesh 'beta': full mesh of 2 member(s)", self.out)
|
||||
|
||||
def test_every_peer_is_pinned_to_its_own_address(self):
|
||||
for host, meshes in MEMBERSHIP.items():
|
||||
for mesh in meshes:
|
||||
entry = self._entry(host, mesh)
|
||||
for peer in entry["peers"]:
|
||||
with self.subTest(host=host, mesh=mesh, peer=peer["host"]):
|
||||
self.assertEqual(f"{peer['address']}/32", peer["allowed_ips"])
|
||||
self.assertFalse(peer["is_hub"])
|
||||
|
||||
def test_no_member_is_a_hub(self):
|
||||
for host, meshes in MEMBERSHIP.items():
|
||||
for mesh in meshes:
|
||||
with self.subTest(host=host, mesh=mesh):
|
||||
self.assertFalse(self._entry(host, mesh)["is_hub"])
|
||||
|
||||
def test_a_host_of_one_mesh_holds_no_peer_of_the_other(self):
|
||||
beta_only = {"host-3"}
|
||||
for host in ("host-1", "host-4"):
|
||||
with self.subTest(host=host):
|
||||
self.assertEqual(
|
||||
["alpha"],
|
||||
[e["name"] for e in interface_entries(self.host_vars, host)],
|
||||
)
|
||||
peers = {p["host"] for p in self._entry(host, "alpha")["peers"]}
|
||||
self.assertFalse(peers & beta_only)
|
||||
|
||||
def test_the_host_in_both_meshes_peers_with_both(self):
|
||||
alpha = {p["host"] for p in self._entry("host-2", "alpha")["peers"]}
|
||||
beta = {p["host"] for p in self._entry("host-2", "beta")["peers"]}
|
||||
self.assertEqual({"host-1", "host-4"}, alpha)
|
||||
self.assertEqual({"host-3"}, beta)
|
||||
|
||||
def test_a_second_run_leaves_every_file_byte_identical(self):
|
||||
before = {
|
||||
path.name: path.read_bytes() # nocheck: cache-read
|
||||
for path in self.host_vars.glob("*.yml")
|
||||
}
|
||||
self._write()
|
||||
after = {
|
||||
path.name: path.read_bytes() # nocheck: cache-read
|
||||
for path in self.host_vars.glob("*.yml")
|
||||
}
|
||||
self.assertEqual(before, after)
|
||||
@@ -0,0 +1,187 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from cli.administration.inventory.mesh.inventory import (
|
||||
groups_of,
|
||||
hub_of,
|
||||
pool_of,
|
||||
specs_of,
|
||||
topology_of,
|
||||
)
|
||||
from utils.cache.yaml import dump_yaml, load_yaml_any
|
||||
|
||||
from . import PROJECT_ROOT
|
||||
|
||||
DECLARED = {
|
||||
"svc-net-wireguard": {
|
||||
"networks": {
|
||||
"meshes": [
|
||||
{"name": "alpha", "subnet": "10.100.10.0/24", "listen_port": 51830},
|
||||
{"name": "beta", "subnet": "10.100.11.0/24", "listen_port": 51831},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GROUPS = {
|
||||
"svc-swarm-manager": ["mgr"],
|
||||
"svc-swarm-node": ["mgr", "wrk-1", "wrk-2"],
|
||||
"svc-storage-nfs-server": ["nfs"],
|
||||
"svc-bkp-nfs-2-local": ["nfs"],
|
||||
}
|
||||
|
||||
|
||||
def _joins(role: str, *meshes: str) -> dict:
|
||||
return {role: {"networks": {"meshes": list(meshes)}}}
|
||||
|
||||
|
||||
class InventoryOnDisk(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self._tmp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self._tmp.cleanup)
|
||||
self.root = Path(self._tmp.name)
|
||||
self.host_vars = self.root / "host_vars"
|
||||
self.host_vars.mkdir()
|
||||
|
||||
def _host(self, host: str, *blocks: dict) -> None:
|
||||
applications: dict = {}
|
||||
for block in blocks:
|
||||
applications.update(block)
|
||||
dump_yaml(str(self.host_vars / f"{host}.yml"), {"applications": applications})
|
||||
|
||||
def _topology(self, groups=None):
|
||||
specs = specs_of(DECLARED, [])
|
||||
return topology_of(groups or GROUPS, self.host_vars, specs)
|
||||
|
||||
|
||||
class TestDeclarations(unittest.TestCase):
|
||||
def test_the_meshes_keep_their_declaration_order(self):
|
||||
self.assertEqual(["alpha", "beta"], [s.name for s in specs_of(DECLARED, [])])
|
||||
|
||||
def test_a_silent_declaration_routes_the_platform_pool(self):
|
||||
self.assertEqual("10.100.0.0/16", specs_of(DECLARED, [])[0].routed_range)
|
||||
|
||||
def test_an_inventory_without_declarations_is_refused(self):
|
||||
with self.assertRaises(ValueError) as raised:
|
||||
specs_of({}, [])
|
||||
self.assertIn("networks.meshes", str(raised.exception))
|
||||
|
||||
def test_a_mesh_declared_twice_is_refused(self):
|
||||
twice = {
|
||||
"svc-net-wireguard": {
|
||||
"networks": {
|
||||
"meshes": [
|
||||
{"name": "a", "subnet": "10.100.1.0/24", "listen_port": 1},
|
||||
{"name": "a", "subnet": "10.100.2.0/24", "listen_port": 2},
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
with self.assertRaises(ValueError):
|
||||
specs_of(twice, [])
|
||||
|
||||
|
||||
class TestPool(InventoryOnDisk, unittest.TestCase):
|
||||
def test_the_platform_pool_applies_by_default(self):
|
||||
self.assertEqual("10.100.0.0/16", pool_of([]))
|
||||
|
||||
def test_a_deployment_narrows_it_in_its_own_group_vars(self):
|
||||
inventory = self.root / "devices.yml"
|
||||
inventory.write_text("all: {}\n", encoding="utf-8")
|
||||
(self.root / "group_vars").mkdir()
|
||||
dump_yaml(
|
||||
str(self.root / "group_vars" / "all.yml"),
|
||||
{"NETWORK_MESH_POOL": "10.100.0.0/17"},
|
||||
)
|
||||
self.assertEqual("10.100.0.0/17", pool_of([inventory]))
|
||||
self.assertEqual(
|
||||
"10.100.0.0/17", specs_of(DECLARED, [inventory])[0].routed_range
|
||||
)
|
||||
|
||||
|
||||
class TestTopology(InventoryOnDisk, unittest.TestCase):
|
||||
def test_a_host_joins_what_its_roles_list(self):
|
||||
self._host("wrk-1", _joins("svc-swarm-node", "alpha"))
|
||||
self._host("nfs", _joins("svc-storage-nfs-server", "beta"))
|
||||
topology = self._topology()
|
||||
self.assertEqual(("wrk-1",), topology.spokes["alpha"])
|
||||
self.assertEqual(("nfs",), topology.spokes["beta"])
|
||||
|
||||
def test_two_hosts_of_one_role_may_sit_in_different_meshes(self):
|
||||
self._host("wrk-1", _joins("svc-swarm-node", "alpha"))
|
||||
self._host("wrk-2", _joins("svc-swarm-node", "beta"))
|
||||
topology = self._topology()
|
||||
self.assertEqual(("wrk-1",), topology.spokes["alpha"])
|
||||
self.assertEqual(("wrk-2",), topology.spokes["beta"])
|
||||
|
||||
def test_two_roles_naming_one_mesh_admit_the_host_once(self):
|
||||
self._host(
|
||||
"nfs",
|
||||
_joins("svc-storage-nfs-server", "beta"),
|
||||
_joins("svc-bkp-nfs-2-local", "beta"),
|
||||
)
|
||||
self.assertEqual(("nfs",), self._topology().spokes["beta"])
|
||||
|
||||
def test_a_role_the_host_does_not_carry_admits_nothing(self):
|
||||
self._host("wrk-1", _joins("svc-storage-nfs-server", "beta"))
|
||||
self.assertEqual((), self._topology().spokes["beta"])
|
||||
|
||||
def test_the_hub_is_in_every_mesh_and_never_a_spoke(self):
|
||||
self._host("mgr", DECLARED, _joins("svc-swarm-node", "alpha"))
|
||||
topology = self._topology()
|
||||
self.assertEqual("mgr", topology.hub)
|
||||
self.assertNotIn("mgr", topology.spokes["alpha"])
|
||||
|
||||
def test_a_mesh_nobody_declared_is_refused(self):
|
||||
self._host("wrk-1", _joins("svc-swarm-node", "omega"))
|
||||
with self.assertRaises(ValueError) as raised:
|
||||
self._topology()
|
||||
self.assertIn("omega", str(raised.exception))
|
||||
self.assertIn("svc-swarm-node", str(raised.exception))
|
||||
|
||||
def test_the_declaring_role_itself_joins_nothing(self):
|
||||
self._host("wrk-1", DECLARED)
|
||||
groups = {**GROUPS, "svc-net-wireguard": ["wrk-1"]}
|
||||
self.assertEqual((), self._topology(groups).spokes["alpha"])
|
||||
|
||||
def test_a_hub_role_with_two_hosts_is_refused(self):
|
||||
with self.assertRaises(ValueError):
|
||||
hub_of({**GROUPS, "svc-swarm-manager": ["a", "b"]})
|
||||
|
||||
def test_an_inventory_without_a_hub_host_has_no_hub(self):
|
||||
self.assertIsNone(hub_of({**GROUPS, "svc-swarm-manager": []}))
|
||||
|
||||
|
||||
class TestTheShippedExample(InventoryOnDisk, unittest.TestCase):
|
||||
EXAMPLE = PROJECT_ROOT / "inventories" / "examples" / "wireguard-three-meshes"
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
layout = load_yaml_any(str(self.EXAMPLE / "wireguard.yml"))["applications"]
|
||||
self.groups = groups_of([self.EXAMPLE / "inventory.yml"])
|
||||
for host in {h for members in self.groups.values() for h in members}:
|
||||
self._host(host, layout)
|
||||
self.specs = specs_of(layout, [])
|
||||
self.topology = topology_of(self.groups, self.host_vars, self.specs)
|
||||
|
||||
def test_it_declares_more_than_two_meshes(self):
|
||||
self.assertEqual(["alpha", "beta", "gamma"], [s.name for s in self.specs])
|
||||
|
||||
def test_every_mesh_routes_the_whole_pool_so_the_planes_can_reach_each_other(self):
|
||||
for spec in self.specs:
|
||||
with self.subTest(mesh=spec.name):
|
||||
self.assertEqual("10.100.0.0/16", spec.routed_range)
|
||||
|
||||
def test_every_mesh_gets_the_members_its_table_promises(self):
|
||||
self.assertEqual("mesh-hub-01", self.topology.hub)
|
||||
self.assertEqual(
|
||||
{
|
||||
"alpha": ("mesh-wrk-01", "mesh-wrk-02"),
|
||||
"beta": ("mesh-nfs-01",),
|
||||
"gamma": ("mesh-bkp-01",),
|
||||
},
|
||||
self.topology.spokes,
|
||||
)
|
||||
@@ -0,0 +1,363 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from cli.administration.inventory.mesh.inventory import assert_no_subnet_overlap
|
||||
from cli.administration.inventory.mesh.keys import (
|
||||
generate_private_key,
|
||||
is_valid_public_key,
|
||||
public_key_of,
|
||||
)
|
||||
from cli.administration.inventory.mesh.model import MeshSpec, Topology
|
||||
from cli.administration.inventory.mesh.plan import plan_mesh, primary_meshes
|
||||
|
||||
SWARM = MeshSpec(name="swarm", subnet="10.100.0.0/24", listen_port=51820)
|
||||
|
||||
DATA = MeshSpec(name="data", subnet="10.100.1.0/24", listen_port=51821)
|
||||
|
||||
TOPOLOGY = Topology(
|
||||
hub="swarm-mgr-01",
|
||||
spokes={
|
||||
"swarm": ("swarm-wrk-01", "swarm-wrk-02"),
|
||||
"data": ("nfs-server", "swarm-bkp-01"),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def _with_spokes(**spokes: tuple[str, ...]) -> Topology:
|
||||
return Topology(hub=TOPOLOGY.hub, spokes={**TOPOLOGY.spokes, **spokes})
|
||||
|
||||
|
||||
class TestMembership(unittest.TestCase):
|
||||
def test_the_hub_is_not_also_listed_as_its_own_spoke(self):
|
||||
mesh = plan_mesh(SWARM, _with_spokes(swarm=("swarm-mgr-01", "swarm-wrk-01")))
|
||||
self.assertEqual(mesh.hub.host, "swarm-mgr-01")
|
||||
self.assertEqual([s.host for s in mesh.spokes], ["swarm-wrk-01"])
|
||||
|
||||
def test_a_host_two_roles_put_into_one_mesh_is_one_spoke(self):
|
||||
topology = _with_spokes(data=("nfs-server", "swarm-bkp-01", "nfs-server"))
|
||||
mesh = plan_mesh(DATA, topology)
|
||||
self.assertEqual([s.host for s in mesh.spokes], ["nfs-server", "swarm-bkp-01"])
|
||||
|
||||
def test_membership_is_stable_against_inventory_ordering(self):
|
||||
shuffled = _with_spokes(swarm=("swarm-wrk-02", "swarm-wrk-01"))
|
||||
self.assertEqual(
|
||||
[m.address for m in plan_mesh(SWARM, TOPOLOGY).members],
|
||||
[m.address for m in plan_mesh(SWARM, shuffled).members],
|
||||
)
|
||||
|
||||
|
||||
class TestAddressing(unittest.TestCase):
|
||||
def test_every_member_is_addressed_inside_the_mesh_subnet(self):
|
||||
for spec in (SWARM, DATA):
|
||||
with self.subTest(mesh=spec.name):
|
||||
mesh = plan_mesh(spec, TOPOLOGY)
|
||||
prefix = spec.subnet.rsplit(".", 1)[0]
|
||||
for member in mesh.members:
|
||||
self.assertTrue(member.address.startswith(prefix + "."))
|
||||
|
||||
def test_no_two_members_share_an_address(self):
|
||||
mesh = plan_mesh(SWARM, TOPOLOGY)
|
||||
addresses = [m.address for m in mesh.members]
|
||||
self.assertEqual(len(addresses), len(set(addresses)))
|
||||
|
||||
def test_the_two_meshes_do_not_share_an_address(self):
|
||||
swarm = {m.address for m in plan_mesh(SWARM, TOPOLOGY).members}
|
||||
data = {m.address for m in plan_mesh(DATA, TOPOLOGY).members}
|
||||
self.assertEqual(swarm & data, set())
|
||||
|
||||
|
||||
class TestAddressPersistence(unittest.TestCase):
|
||||
def _held(self, spec, topology) -> dict[str, str]:
|
||||
return {m.host: m.address for m in plan_mesh(spec, topology).members}
|
||||
|
||||
def test_a_member_that_sorts_first_does_not_renumber_the_others(self):
|
||||
before = self._held(SWARM, TOPOLOGY)
|
||||
grown = _with_spokes(swarm=("alpha-wrk-00", *TOPOLOGY.spokes["swarm"]))
|
||||
|
||||
mesh = plan_mesh(SWARM, grown, addresses=before)
|
||||
after = {m.host: m.address for m in mesh.members}
|
||||
for host, address in before.items():
|
||||
with self.subTest(host=host):
|
||||
self.assertEqual(after[host], address)
|
||||
self.assertNotIn(after["alpha-wrk-00"], set(before.values()))
|
||||
|
||||
def test_rotation_replaces_identities_and_keeps_addresses(self):
|
||||
before = self._held(SWARM, TOPOLOGY)
|
||||
mesh = plan_mesh(SWARM, TOPOLOGY, rotate=True, addresses=before)
|
||||
self.assertEqual({m.host: m.address for m in mesh.members}, before)
|
||||
|
||||
def test_a_stored_address_outside_the_subnet_is_replaced(self):
|
||||
mesh = plan_mesh(SWARM, TOPOLOGY, addresses={"swarm-wrk-01": "10.99.0.50"})
|
||||
address = mesh.member("swarm-wrk-01").address
|
||||
self.assertTrue(address.startswith("10.100.0."))
|
||||
|
||||
def test_a_spoke_never_adopts_the_hub_address(self):
|
||||
hub = plan_mesh(SWARM, TOPOLOGY).hub.address
|
||||
mesh = plan_mesh(SWARM, TOPOLOGY, addresses={"swarm-wrk-01": hub})
|
||||
self.assertNotEqual(mesh.member("swarm-wrk-01").address, hub)
|
||||
|
||||
def test_two_members_claiming_one_address_do_not_both_keep_it(self):
|
||||
taken = plan_mesh(SWARM, TOPOLOGY).member("swarm-wrk-01").address
|
||||
mesh = plan_mesh(
|
||||
SWARM,
|
||||
TOPOLOGY,
|
||||
addresses={"swarm-wrk-01": taken, "swarm-wrk-02": taken},
|
||||
)
|
||||
addresses = [m.address for m in mesh.members]
|
||||
self.assertEqual(len(addresses), len(set(addresses)))
|
||||
|
||||
|
||||
class TestPeering(unittest.TestCase):
|
||||
def test_the_hub_peers_with_every_spoke(self):
|
||||
mesh = plan_mesh(SWARM, TOPOLOGY)
|
||||
self.assertEqual(
|
||||
{p.host for p in mesh.peers_of(mesh.hub.host)},
|
||||
{s.host for s in mesh.spokes},
|
||||
)
|
||||
|
||||
def test_a_spoke_peers_only_with_the_hub(self):
|
||||
mesh = plan_mesh(SWARM, TOPOLOGY)
|
||||
for spoke in mesh.spokes:
|
||||
with self.subTest(spoke=spoke.host):
|
||||
peers = mesh.peers_of(spoke.host)
|
||||
self.assertEqual([p.host for p in peers], [mesh.hub.host])
|
||||
|
||||
def test_a_host_outside_the_mesh_has_no_peers(self):
|
||||
mesh = plan_mesh(SWARM, TOPOLOGY)
|
||||
self.assertEqual(mesh.peers_of("nfs-server"), ())
|
||||
|
||||
|
||||
class TestPrimaryMesh(unittest.TestCase):
|
||||
def test_the_first_declared_mesh_a_host_belongs_to_is_its_primary(self):
|
||||
self.assertEqual(
|
||||
primary_meshes([SWARM, DATA], TOPOLOGY),
|
||||
{
|
||||
"swarm-mgr-01": "swarm",
|
||||
"swarm-wrk-01": "swarm",
|
||||
"swarm-wrk-02": "swarm",
|
||||
"nfs-server": "data",
|
||||
"swarm-bkp-01": "data",
|
||||
},
|
||||
)
|
||||
|
||||
def test_reordering_the_declarations_moves_the_hubs_primary(self):
|
||||
self.assertEqual(
|
||||
primary_meshes([DATA, SWARM], TOPOLOGY)["swarm-mgr-01"], "data"
|
||||
)
|
||||
|
||||
def test_a_spoke_of_two_meshes_keeps_the_first(self):
|
||||
topology = _with_spokes(beta=("nfs-server",))
|
||||
beta = MeshSpec(name="beta", subnet="10.100.11.0/24", listen_port=51831)
|
||||
self.assertEqual(primary_meshes([beta, DATA], topology)["nfs-server"], "beta")
|
||||
|
||||
def test_the_controller_counts_only_where_it_joins(self):
|
||||
primary = primary_meshes(
|
||||
[SWARM, DATA], TOPOLOGY, controller="localhost", controller_mesh="data"
|
||||
)
|
||||
self.assertEqual(primary["localhost"], "data")
|
||||
self.assertNotIn("localhost", primary_meshes([SWARM, DATA], TOPOLOGY))
|
||||
|
||||
|
||||
class TestKeyPairing(unittest.TestCase):
|
||||
def test_every_public_key_answers_for_its_private_key(self):
|
||||
mesh = plan_mesh(SWARM, TOPOLOGY)
|
||||
for member in mesh.members:
|
||||
with self.subTest(host=member.host):
|
||||
self.assertIsNotNone(member.private_key)
|
||||
self.assertEqual(public_key_of(member.private_key), member.public_key)
|
||||
|
||||
def test_no_two_members_share_a_keypair(self):
|
||||
mesh = plan_mesh(SWARM, TOPOLOGY)
|
||||
self.assertEqual(len({m.private_key for m in mesh.members}), len(mesh.members))
|
||||
|
||||
def test_a_peer_entry_never_carries_a_private_key(self):
|
||||
mesh = plan_mesh(SWARM, TOPOLOGY)
|
||||
secrets = {m.private_key for m in mesh.members}
|
||||
for member in mesh.members:
|
||||
for peer in mesh.peers_of(member.host):
|
||||
with self.subTest(host=member.host, peer=peer.host):
|
||||
self.assertNotIn(peer.public_key, secrets)
|
||||
self.assertTrue(is_valid_public_key(peer.public_key))
|
||||
|
||||
def test_a_malformed_private_key_is_refused(self):
|
||||
for bad in ("", "not-base64!", "c2hvcnQ="):
|
||||
with self.subTest(value=bad), self.assertRaises(ValueError):
|
||||
public_key_of(bad)
|
||||
|
||||
|
||||
class TestRotation(unittest.TestCase):
|
||||
def test_an_existing_member_keeps_its_key_and_is_not_rewritten(self):
|
||||
held = {"swarm-wrk-01": public_key_of(generate_private_key())}
|
||||
mesh = plan_mesh(SWARM, TOPOLOGY, held)
|
||||
member = mesh.member("swarm-wrk-01")
|
||||
self.assertEqual(member.public_key, held["swarm-wrk-01"])
|
||||
self.assertIsNone(member.private_key)
|
||||
|
||||
def test_a_new_member_is_keyed_while_the_others_are_untouched(self):
|
||||
held = {
|
||||
host: public_key_of(generate_private_key())
|
||||
for host in ("swarm-mgr-01", "swarm-wrk-01")
|
||||
}
|
||||
mesh = plan_mesh(SWARM, TOPOLOGY, held)
|
||||
self.assertIsNone(mesh.member("swarm-wrk-01").private_key)
|
||||
self.assertIsNotNone(mesh.member("swarm-wrk-02").private_key)
|
||||
|
||||
def test_rotation_replaces_every_key_including_held_ones(self):
|
||||
held = {
|
||||
host: public_key_of(generate_private_key())
|
||||
for host in ("swarm-mgr-01", "swarm-wrk-01", "swarm-wrk-02")
|
||||
}
|
||||
mesh = plan_mesh(SWARM, TOPOLOGY, held, rotate=True)
|
||||
for member in mesh.members:
|
||||
with self.subTest(host=member.host):
|
||||
self.assertIsNotNone(member.private_key)
|
||||
self.assertNotEqual(member.public_key, held[member.host])
|
||||
|
||||
def test_rotation_keeps_the_mesh_internally_consistent(self):
|
||||
mesh = plan_mesh(SWARM, TOPOLOGY, rotate=True)
|
||||
published = {m.host: m.public_key for m in mesh.members}
|
||||
for member in mesh.members:
|
||||
for peer in mesh.peers_of(member.host):
|
||||
with self.subTest(host=member.host, peer=peer.host):
|
||||
self.assertEqual(peer.public_key, published[peer.host])
|
||||
|
||||
|
||||
class TestSubnetGuards(unittest.TestCase):
|
||||
def test_overlapping_meshes_are_refused(self):
|
||||
clash = MeshSpec(name="clash", subnet="10.100.0.0/25", listen_port=51822)
|
||||
with self.assertRaises(ValueError):
|
||||
assert_no_subnet_overlap([SWARM, clash], "10.100.0.0/16")
|
||||
|
||||
def test_a_subnet_outside_the_pool_is_refused(self):
|
||||
escapee = MeshSpec(name="escapee", subnet="192.168.244.0/24", listen_port=51823)
|
||||
with self.assertRaises(ValueError):
|
||||
assert_no_subnet_overlap([escapee], "10.100.0.0/16")
|
||||
|
||||
def test_a_shared_listen_port_is_refused(self):
|
||||
twin = MeshSpec(
|
||||
name="twin", subnet="10.100.9.0/24", listen_port=SWARM.listen_port
|
||||
)
|
||||
with self.assertRaises(ValueError):
|
||||
assert_no_subnet_overlap([SWARM, twin], "10.100.0.0/16")
|
||||
|
||||
def test_the_declared_meshes_pass_their_own_guards(self):
|
||||
assert_no_subnet_overlap([SWARM, DATA], "10.100.0.0/16")
|
||||
|
||||
|
||||
MESH_POOL = "10.100.0.0/16"
|
||||
|
||||
|
||||
def topology(mesh_count: int, seed: int) -> tuple[list[MeshSpec], Topology]:
|
||||
"""A hub-and-spoke topology of ``mesh_count`` meshes around one hub.
|
||||
|
||||
Args:
|
||||
mesh_count: how many meshes to declare.
|
||||
seed: picks one arrangement of hosts over the meshes.
|
||||
"""
|
||||
hosts = [f"node-{index:02d}" for index in range(12)]
|
||||
spokes: dict[str, tuple[str, ...]] = {}
|
||||
specs: list[MeshSpec] = []
|
||||
for index in range(mesh_count):
|
||||
name = f"mesh-{index}"
|
||||
spokes[name] = tuple(
|
||||
host
|
||||
for position, host in enumerate(hosts)
|
||||
if (position + seed) % (index + 2)
|
||||
)
|
||||
specs.append(
|
||||
MeshSpec(
|
||||
name=name, subnet=f"10.100.{index}.0/24", listen_port=51820 + index
|
||||
)
|
||||
)
|
||||
return specs, Topology(hub="hub-01", spokes=spokes)
|
||||
|
||||
|
||||
class TestAnyNumberOfMeshes(unittest.TestCase):
|
||||
COUNTS = range(1, 9)
|
||||
SEEDS = range(3)
|
||||
|
||||
def _planned(self, count: int, seed: int):
|
||||
specs, layout = topology(count, seed)
|
||||
return specs, layout, [plan_mesh(spec, layout) for spec in specs]
|
||||
|
||||
def test_every_declared_count_plans_without_collision(self):
|
||||
for count in self.COUNTS:
|
||||
for seed in self.SEEDS:
|
||||
with self.subTest(meshes=count, seed=seed):
|
||||
specs, _, meshes = self._planned(count, seed)
|
||||
assert_no_subnet_overlap(specs, MESH_POOL)
|
||||
self.assertEqual(len(meshes), count)
|
||||
|
||||
def test_every_member_is_addressed_inside_its_own_subnet(self):
|
||||
for count in self.COUNTS:
|
||||
for seed in self.SEEDS:
|
||||
specs, _, meshes = self._planned(count, seed)
|
||||
for spec, mesh in zip(specs, meshes, strict=True):
|
||||
prefix = spec.subnet.rsplit(".", 1)[0] + "."
|
||||
for member in mesh.members:
|
||||
with self.subTest(meshes=count, seed=seed, host=member.host):
|
||||
self.assertTrue(member.address.startswith(prefix))
|
||||
|
||||
def test_no_two_members_of_one_mesh_share_an_address(self):
|
||||
for count in self.COUNTS:
|
||||
for seed in self.SEEDS:
|
||||
_, _, meshes = self._planned(count, seed)
|
||||
for mesh in meshes:
|
||||
with self.subTest(meshes=count, seed=seed, mesh=mesh.spec.name):
|
||||
addresses = [member.address for member in mesh.members]
|
||||
self.assertEqual(len(addresses), len(set(addresses)))
|
||||
|
||||
def test_the_hub_joins_every_mesh_so_the_planes_can_reach_each_other(self):
|
||||
for count in self.COUNTS:
|
||||
for seed in self.SEEDS:
|
||||
_, _, meshes = self._planned(count, seed)
|
||||
for mesh in meshes:
|
||||
with self.subTest(meshes=count, seed=seed, mesh=mesh.spec.name):
|
||||
self.assertEqual("hub-01", mesh.hub.host)
|
||||
|
||||
def test_the_hub_peers_with_every_spoke_and_a_spoke_only_with_the_hub(self):
|
||||
for count in self.COUNTS:
|
||||
for seed in self.SEEDS:
|
||||
_, _, meshes = self._planned(count, seed)
|
||||
for mesh in meshes:
|
||||
with self.subTest(meshes=count, seed=seed, mesh=mesh.spec.name):
|
||||
self.assertEqual(
|
||||
sorted(peer.host for peer in mesh.peers_of(mesh.hub.host)),
|
||||
sorted(spoke.host for spoke in mesh.spokes),
|
||||
)
|
||||
for spoke in mesh.spokes:
|
||||
self.assertEqual(
|
||||
["hub-01"],
|
||||
[peer.host for peer in mesh.peers_of(spoke.host)],
|
||||
)
|
||||
|
||||
def test_a_host_in_several_meshes_holds_one_address_per_mesh(self):
|
||||
shared = 0
|
||||
for count in self.COUNTS:
|
||||
for seed in self.SEEDS:
|
||||
_, _, meshes = self._planned(count, seed)
|
||||
held: dict[str, set[str]] = {}
|
||||
for mesh in meshes:
|
||||
for member in mesh.members:
|
||||
held.setdefault(member.host, set()).add(member.address)
|
||||
for host, addresses in held.items():
|
||||
count_in = sum(
|
||||
1
|
||||
for mesh in meshes
|
||||
if any(m.host == host for m in mesh.members)
|
||||
)
|
||||
if count_in > 1:
|
||||
shared += 1
|
||||
with self.subTest(meshes=count, seed=seed, host=host):
|
||||
self.assertEqual(count_in, len(addresses))
|
||||
self.assertGreater(shared, 0, "no host landed in several meshes")
|
||||
|
||||
def test_every_peer_entry_carries_a_usable_public_key(self):
|
||||
for count in self.COUNTS:
|
||||
_, _, meshes = self._planned(count, 0)
|
||||
for mesh in meshes:
|
||||
for member in mesh.members:
|
||||
for peer in mesh.peers_of(member.host):
|
||||
with self.subTest(meshes=count, host=member.host):
|
||||
self.assertTrue(is_valid_public_key(peer.public_key))
|
||||
@@ -0,0 +1,177 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from cli.administration.inventory.mesh.inventory import (
|
||||
groups_of,
|
||||
specs_of,
|
||||
topology_of,
|
||||
)
|
||||
from cli.administration.inventory.mesh.plan import members_of, plan_mesh
|
||||
from cli.administration.inventory.mesh.transport import (
|
||||
MESH_CONTROL_PERSIST,
|
||||
mesh_address,
|
||||
mesh_ssh_args,
|
||||
repo_ssh_args,
|
||||
switch_to_mesh,
|
||||
)
|
||||
from cli.administration.inventory.mesh.write import write_mesh
|
||||
from utils.cache.yaml import dump_yaml
|
||||
|
||||
from .test_write import LAYOUT, layout_applications
|
||||
|
||||
CONTROLLER = "localhost"
|
||||
HOSTS = ("swarm-mgr-01", "swarm-wrk-01", "nfs-server", CONTROLLER)
|
||||
|
||||
CLUSTER_INVENTORY = """\
|
||||
all:
|
||||
children:
|
||||
svc-swarm-manager:
|
||||
hosts:
|
||||
swarm-mgr-01: {}
|
||||
svc-swarm-node:
|
||||
hosts:
|
||||
swarm-mgr-01: {}
|
||||
swarm-wrk-01: {}
|
||||
svc-storage-nfs-server:
|
||||
hosts:
|
||||
nfs-server: {}
|
||||
"""
|
||||
|
||||
|
||||
class MeshOnDisk(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self._tmp = tempfile.TemporaryDirectory()
|
||||
root = Path(self._tmp.name)
|
||||
self.host_vars = root / "host_vars"
|
||||
self.host_vars.mkdir()
|
||||
layout = layout_applications(LAYOUT)
|
||||
for host in HOSTS:
|
||||
dump_yaml(str(self.host_vars / f"{host}.yml"), {"applications": layout})
|
||||
self.cluster = root / "devices.yml"
|
||||
self.cluster.write_text(CLUSTER_INVENTORY, encoding="utf-8")
|
||||
self.vault_file = root / ".password"
|
||||
self.vault_file.write_text("mesh-test-password\n", encoding="utf-8")
|
||||
self.groups = groups_of([self.cluster])
|
||||
self.specs = specs_of(layout, [])
|
||||
self.topology = topology_of(self.groups, self.host_vars, self.specs)
|
||||
self.swarm = next(spec for spec in self.specs if spec.name == "swarm")
|
||||
|
||||
def tearDown(self):
|
||||
self._tmp.cleanup()
|
||||
|
||||
def _write_swarm(self):
|
||||
mesh = plan_mesh(self.swarm, self.topology, rotate=True, controller=CONTROLLER)
|
||||
write_mesh(mesh, self.host_vars, self.vault_file)
|
||||
return mesh
|
||||
|
||||
def _text(self, host: str) -> str:
|
||||
return (self.host_vars / f"{host}.yml").read_text( # nocheck: cache-read
|
||||
encoding="utf-8"
|
||||
)
|
||||
|
||||
|
||||
class TestTheControllerJoinsTheMesh(MeshOnDisk, unittest.TestCase):
|
||||
def test_it_is_admitted_as_an_ordinary_spoke(self):
|
||||
_, spokes = members_of(self.swarm, self.topology, CONTROLLER)
|
||||
self.assertIn(CONTROLLER, spokes)
|
||||
|
||||
def test_it_stays_out_when_it_is_not_named(self):
|
||||
_, spokes = members_of(self.swarm, self.topology)
|
||||
self.assertNotIn(CONTROLLER, spokes)
|
||||
|
||||
def test_it_is_never_made_the_hub(self):
|
||||
hub, _ = members_of(self.swarm, self.topology, CONTROLLER)
|
||||
self.assertNotEqual(hub, CONTROLLER)
|
||||
|
||||
def test_it_gets_an_address_of_its_own(self):
|
||||
mesh = self._write_swarm()
|
||||
addresses = {member.host: member.address for member in mesh.members}
|
||||
self.assertIn(CONTROLLER, addresses)
|
||||
self.assertEqual(len(set(addresses.values())), len(addresses))
|
||||
|
||||
|
||||
class TestSwitchingTheTransport(MeshOnDisk, unittest.TestCase):
|
||||
def _switch(self):
|
||||
return switch_to_mesh(
|
||||
self.host_vars,
|
||||
list(HOSTS),
|
||||
"swarm",
|
||||
user="administrator",
|
||||
private_key_file="/tmp/key",
|
||||
)
|
||||
|
||||
def test_every_member_is_pointed_at_its_mesh_address(self):
|
||||
mesh = self._write_swarm()
|
||||
switched = self._switch()
|
||||
for member in mesh.members:
|
||||
with self.subTest(host=member.host):
|
||||
self.assertEqual(switched[member.host], member.address)
|
||||
|
||||
def test_a_non_member_is_left_reachable(self):
|
||||
self._write_swarm()
|
||||
switched = self._switch()
|
||||
self.assertNotIn("nfs-server", switched)
|
||||
|
||||
def test_the_connection_moves_to_ssh(self):
|
||||
self._write_swarm()
|
||||
self._switch()
|
||||
text = self._text("swarm-wrk-01")
|
||||
self.assertIn("ansible_connection: ssh", text)
|
||||
self.assertIn("ansible_user: administrator", text)
|
||||
|
||||
def test_a_switched_host_carries_the_meshs_own_ssh_args(self):
|
||||
self._write_swarm()
|
||||
self._switch()
|
||||
text = self._text("swarm-wrk-01")
|
||||
self.assertIn("ansible_ssh_args:", text)
|
||||
self.assertIn(f"ControlPersist={MESH_CONTROL_PERSIST}", text)
|
||||
|
||||
def test_a_switched_host_asks_for_no_tty(self):
|
||||
self._write_swarm()
|
||||
self._switch()
|
||||
self.assertIn("ansible_ssh_use_tty: false", self._text("swarm-wrk-01"))
|
||||
|
||||
def test_switching_before_the_mesh_exists_changes_nothing(self):
|
||||
self.assertEqual(self._switch(), {})
|
||||
|
||||
def test_the_address_is_only_read_from_the_host_that_owns_it(self):
|
||||
self._write_swarm()
|
||||
(self.host_vars / "swarm-wrk-01.yml").write_text(
|
||||
self._text("swarm-mgr-01"), encoding="utf-8"
|
||||
)
|
||||
self.assertIsNone(mesh_address(self.host_vars, "swarm-wrk-01", "swarm"))
|
||||
|
||||
|
||||
class TestTheMeshRetunesItsOwnTransport(unittest.TestCase):
|
||||
BASE = (
|
||||
"-o ControlMaster=auto -o ControlPersist=20s "
|
||||
"-o ControlPath=~/.ssh/ansible-%h-%p-%r -o ServerAliveInterval=15"
|
||||
)
|
||||
|
||||
def test_the_window_is_the_meshs_own(self):
|
||||
args = mesh_ssh_args(self.BASE)
|
||||
self.assertIn(f"-o ControlPersist={MESH_CONTROL_PERSIST}", args)
|
||||
self.assertNotIn("ControlPersist=20s", args)
|
||||
|
||||
def test_the_socket_is_left_to_ansible(self):
|
||||
self.assertNotIn("ControlPath", mesh_ssh_args(self.BASE))
|
||||
|
||||
def test_every_other_option_survives(self):
|
||||
args = mesh_ssh_args(self.BASE)
|
||||
self.assertIn("-o ControlMaster=auto", args)
|
||||
self.assertIn("-o ServerAliveInterval=15", args)
|
||||
|
||||
def test_a_bare_flag_is_not_dropped(self):
|
||||
self.assertIn("-C", mesh_ssh_args("-C -o ControlPersist=20s"))
|
||||
|
||||
def test_the_repository_value_is_the_one_retuned(self):
|
||||
base = repo_ssh_args()
|
||||
self.assertIn("ControlMaster", base)
|
||||
for option in base.split(" -o "):
|
||||
name = option.split("=")[0].lstrip("-o ").strip()
|
||||
if name and not name.startswith(("ControlPersist", "ControlPath")):
|
||||
with self.subTest(option=name):
|
||||
self.assertIn(name, mesh_ssh_args(base))
|
||||
@@ -0,0 +1,396 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
|
||||
from cli.administration.inventory.mesh.inventory import (
|
||||
groups_of,
|
||||
specs_of,
|
||||
topology_of,
|
||||
)
|
||||
from cli.administration.inventory.mesh.keys import generate_private_key, public_key_of
|
||||
from cli.administration.inventory.mesh.plan import plan_mesh, primary_meshes
|
||||
from cli.administration.inventory.mesh.write import (
|
||||
existing_addresses,
|
||||
existing_public_keys,
|
||||
private_key_name,
|
||||
prune_foreign_meshes,
|
||||
write_mesh,
|
||||
)
|
||||
from utils.cache.yaml import dump_yaml, load_yaml_any
|
||||
|
||||
from . import PROJECT_ROOT
|
||||
from .test_plan import MESH_POOL, topology
|
||||
|
||||
LAYOUT = PROJECT_ROOT / "inventories/development/wireguard.yml"
|
||||
EXAMPLE_LAYOUT = (
|
||||
PROJECT_ROOT / "inventories/examples/wireguard-three-meshes/wireguard.yml"
|
||||
)
|
||||
|
||||
|
||||
def layout_applications(path: Path) -> dict:
|
||||
return load_yaml_any(str(path))["applications"]
|
||||
|
||||
|
||||
def interface_entries(host_vars: Path, host: str) -> list[dict]:
|
||||
document = load_yaml_any(str(host_vars / f"{host}.yml")) or {}
|
||||
return (
|
||||
document.get("applications", {})
|
||||
.get("svc-net-wireguard", {})
|
||||
.get("networks", {})
|
||||
.get("interfaces", [])
|
||||
)
|
||||
|
||||
|
||||
HOSTS = ("swarm-mgr-01", "swarm-wrk-01", "swarm-wrk-02", "nfs-server", "swarm-bkp-01")
|
||||
|
||||
CLUSTER_INVENTORY = """\
|
||||
all:
|
||||
children:
|
||||
svc-swarm-manager:
|
||||
hosts:
|
||||
swarm-mgr-01: {}
|
||||
svc-swarm-node:
|
||||
hosts:
|
||||
swarm-mgr-01: {}
|
||||
swarm-wrk-01: {}
|
||||
swarm-wrk-02: {}
|
||||
svc-storage-nfs-server:
|
||||
hosts:
|
||||
nfs-server: {}
|
||||
"""
|
||||
|
||||
BACKUP_INVENTORY = """\
|
||||
all:
|
||||
children:
|
||||
svc-bkp-remote-2-local:
|
||||
hosts:
|
||||
swarm-bkp-01: {}
|
||||
"""
|
||||
|
||||
VAULT_PASSWORD = "mesh-test-password\n"
|
||||
|
||||
|
||||
class MeshOnDisk(unittest.TestCase):
|
||||
LAYOUT = LAYOUT
|
||||
|
||||
def setUp(self):
|
||||
self._tmp = tempfile.TemporaryDirectory()
|
||||
root = Path(self._tmp.name)
|
||||
self.host_vars = root / "host_vars"
|
||||
self.host_vars.mkdir()
|
||||
self.layout = layout_applications(self.LAYOUT)
|
||||
for host in HOSTS:
|
||||
dump_yaml(
|
||||
str(self.host_vars / f"{host}.yml"), {"applications": self.layout}
|
||||
)
|
||||
|
||||
self.cluster = root / "devices.yml"
|
||||
self.cluster.write_text(self.cluster_inventory(), encoding="utf-8")
|
||||
self.backup = root / "backup.yml"
|
||||
self.backup.write_text(BACKUP_INVENTORY, encoding="utf-8")
|
||||
|
||||
self.vault_file = root / ".password"
|
||||
self.vault_file.write_text(VAULT_PASSWORD, encoding="utf-8")
|
||||
|
||||
self.groups = groups_of([self.cluster, self.backup])
|
||||
self.specs = specs_of(self.layout, [])
|
||||
self.topology = topology_of(self.groups, self.host_vars, self.specs)
|
||||
|
||||
def cluster_inventory(self) -> str:
|
||||
return CLUSTER_INVENTORY
|
||||
|
||||
def tearDown(self):
|
||||
self._tmp.cleanup()
|
||||
|
||||
def _text(self, host: str) -> str:
|
||||
path = self.host_vars / f"{host}.yml"
|
||||
return path.read_text(encoding="utf-8") # nocheck: cache-read
|
||||
|
||||
def _mesh_names(self, host: str) -> list[str]:
|
||||
return sorted(
|
||||
entry["name"] for entry in interface_entries(self.host_vars, host)
|
||||
)
|
||||
|
||||
def _entry(self, host: str, mesh_name: str) -> dict:
|
||||
return next(
|
||||
(
|
||||
entry
|
||||
for entry in interface_entries(self.host_vars, host)
|
||||
if entry["name"] == mesh_name
|
||||
),
|
||||
{},
|
||||
)
|
||||
|
||||
def _write_all(self, *, rotate: bool = False) -> None:
|
||||
for spec in self.specs:
|
||||
mesh = plan_mesh(
|
||||
spec,
|
||||
self.topology,
|
||||
None
|
||||
if rotate
|
||||
else existing_public_keys(self.host_vars, list(HOSTS), spec.name),
|
||||
rotate=rotate,
|
||||
)
|
||||
write_mesh(mesh, self.host_vars, self.vault_file)
|
||||
|
||||
|
||||
class TestBothInventoriesAreRead(MeshOnDisk, unittest.TestCase):
|
||||
def test_the_backup_host_is_found_in_its_sibling_inventory(self):
|
||||
self.assertIn("swarm-bkp-01", self.groups.get("svc-bkp-remote-2-local", []))
|
||||
cluster_only = groups_of([self.cluster])
|
||||
self.assertNotIn("svc-bkp-remote-2-local", cluster_only)
|
||||
|
||||
|
||||
class TestPrivateKeyContainment(MeshOnDisk, unittest.TestCase):
|
||||
def test_a_private_key_reaches_exactly_one_host_vars_file(self):
|
||||
for spec in self.specs:
|
||||
mesh = plan_mesh(spec, self.topology, rotate=True)
|
||||
write_mesh(mesh, self.host_vars, self.vault_file)
|
||||
with self.subTest(mesh=spec.name):
|
||||
for member in mesh.members:
|
||||
others = [h for h in HOSTS if h != member.host]
|
||||
for other in others:
|
||||
self.assertNotIn(member.private_key, self._text(other))
|
||||
|
||||
def test_no_plaintext_private_key_is_written_anywhere(self):
|
||||
for spec in self.specs:
|
||||
mesh = plan_mesh(spec, self.topology, rotate=True)
|
||||
write_mesh(mesh, self.host_vars, self.vault_file)
|
||||
with self.subTest(mesh=spec.name):
|
||||
for member in mesh.members:
|
||||
self.assertNotIn(member.private_key, self._text(member.host))
|
||||
|
||||
def test_the_stored_private_key_is_vault_encrypted(self):
|
||||
self._write_all()
|
||||
text = self._text("swarm-mgr-01")
|
||||
self.assertIn(private_key_name("swarm"), text)
|
||||
self.assertIn("$ANSIBLE_VAULT", text)
|
||||
|
||||
|
||||
class TestMeshShapeOnDisk(MeshOnDisk, unittest.TestCase):
|
||||
def test_every_member_receives_the_mesh_it_belongs_to(self):
|
||||
self._write_all()
|
||||
for host in ("swarm-mgr-01", "swarm-wrk-01", "swarm-wrk-02"):
|
||||
with self.subTest(host=host):
|
||||
self.assertIn("swarm", self._mesh_names(host))
|
||||
for host in ("nfs-server", "swarm-bkp-01"):
|
||||
with self.subTest(host=host):
|
||||
self.assertIn("data", self._mesh_names(host))
|
||||
|
||||
def test_a_worker_is_not_given_the_data_mesh(self):
|
||||
self._write_all()
|
||||
self.assertEqual(self._mesh_names("swarm-wrk-01"), ["swarm"])
|
||||
|
||||
def test_the_hub_carries_both_meshes(self):
|
||||
self._write_all()
|
||||
self.assertEqual(self._mesh_names("swarm-mgr-01"), ["data", "swarm"])
|
||||
|
||||
def test_a_spoke_routes_the_whole_pool_through_the_hub(self):
|
||||
self._write_all()
|
||||
for spec in self.specs:
|
||||
mesh = plan_mesh(spec, self.topology)
|
||||
self.assertEqual(spec.routed_range, "10.100.0.0/16")
|
||||
for spoke in mesh.spokes:
|
||||
with self.subTest(mesh=spec.name, spoke=spoke.host):
|
||||
peers = self._entry(spoke.host, spec.name)["peers"]
|
||||
self.assertEqual([p["host"] for p in peers], [mesh.hub.host])
|
||||
self.assertEqual(
|
||||
[p["allowed_ips"] for p in peers], [spec.routed_range]
|
||||
)
|
||||
|
||||
def test_the_hub_pins_each_spoke_to_a_single_address(self):
|
||||
self._write_all()
|
||||
for spec in self.specs:
|
||||
hub = plan_mesh(spec, self.topology).hub.host
|
||||
for peer in self._entry(hub, spec.name)["peers"]:
|
||||
with self.subTest(mesh=spec.name, peer=peer["host"]):
|
||||
self.assertFalse(peer["is_hub"])
|
||||
self.assertEqual(peer["allowed_ips"], peer["address"] + "/32")
|
||||
|
||||
|
||||
class TestStoredAddresses(MeshOnDisk, unittest.TestCase):
|
||||
def test_every_written_address_is_read_back(self):
|
||||
self._write_all()
|
||||
for spec in self.specs:
|
||||
mesh = plan_mesh(spec, self.topology)
|
||||
with self.subTest(mesh=spec.name):
|
||||
self.assertEqual(
|
||||
existing_addresses(self.host_vars, list(HOSTS), spec.name),
|
||||
{m.host: m.address for m in mesh.members},
|
||||
)
|
||||
|
||||
def test_a_host_with_no_entry_is_absent_rather_than_empty(self):
|
||||
self._write_all()
|
||||
stored = existing_addresses(self.host_vars, list(HOSTS), "data")
|
||||
self.assertNotIn("swarm-wrk-01", stored)
|
||||
|
||||
def test_a_mirrored_entry_is_not_read_as_the_hosts_own_address(self):
|
||||
self._write_all()
|
||||
hub_text = self._text("swarm-mgr-01")
|
||||
(self.host_vars / "swarm-wrk-01.yml").write_text(hub_text, encoding="utf-8")
|
||||
stored = existing_addresses(self.host_vars, list(HOSTS), "swarm")
|
||||
self.assertNotIn("swarm-wrk-01", stored)
|
||||
|
||||
|
||||
class TestIdempotence(MeshOnDisk, unittest.TestCase):
|
||||
def test_a_second_run_leaves_every_file_byte_identical(self):
|
||||
self._write_all()
|
||||
before = {host: self._text(host) for host in HOSTS}
|
||||
self._write_all()
|
||||
for host in HOSTS:
|
||||
with self.subTest(host=host):
|
||||
self.assertEqual(self._text(host), before[host])
|
||||
|
||||
def test_rotation_changes_every_member_file(self):
|
||||
self._write_all()
|
||||
before = {host: self._text(host) for host in HOSTS}
|
||||
self._write_all(rotate=True)
|
||||
for host in HOSTS:
|
||||
with self.subTest(host=host):
|
||||
self.assertNotEqual(self._text(host), before[host])
|
||||
|
||||
def test_a_mirrored_host_vars_is_reminted_rather_than_adopted(self):
|
||||
self._write_all()
|
||||
hub_text = self._text("swarm-mgr-01")
|
||||
for host in ("swarm-wrk-01", "swarm-wrk-02"):
|
||||
(self.host_vars / f"{host}.yml").write_text(hub_text, encoding="utf-8")
|
||||
|
||||
keyed = existing_public_keys(self.host_vars, list(HOSTS), "swarm")
|
||||
self.assertNotIn("swarm-wrk-01", keyed)
|
||||
self.assertNotIn("swarm-wrk-02", keyed)
|
||||
self.assertIn("swarm-mgr-01", keyed)
|
||||
|
||||
self._write_all()
|
||||
addresses = {
|
||||
host: plan_mesh(
|
||||
self.specs[0],
|
||||
self.topology,
|
||||
existing_public_keys(self.host_vars, list(HOSTS), "swarm"),
|
||||
)
|
||||
.member(host)
|
||||
.address
|
||||
for host in ("swarm-mgr-01", "swarm-wrk-01", "swarm-wrk-02")
|
||||
}
|
||||
self.assertEqual(len(set(addresses.values())), 3)
|
||||
|
||||
def test_a_mesh_a_host_does_not_own_is_pruned(self):
|
||||
self._write_all()
|
||||
hub_text = self._text("swarm-mgr-01")
|
||||
(self.host_vars / "nfs-server.yml").write_text(hub_text, encoding="utf-8")
|
||||
self.assertEqual(self._mesh_names("nfs-server"), ["data", "swarm"])
|
||||
|
||||
removed = prune_foreign_meshes(self.host_vars, list(HOSTS))
|
||||
self.assertEqual(removed.get("nfs-server"), ["data", "swarm"])
|
||||
self.assertEqual(self._mesh_names("nfs-server"), [])
|
||||
|
||||
self._write_all()
|
||||
self.assertEqual(self._mesh_names("nfs-server"), ["data"])
|
||||
|
||||
def test_pruning_leaves_an_owned_mesh_alone(self):
|
||||
self._write_all()
|
||||
before = {host: self._text(host) for host in HOSTS}
|
||||
self.assertEqual(prune_foreign_meshes(self.host_vars, list(HOSTS)), {})
|
||||
for host in HOSTS:
|
||||
with self.subTest(host=host):
|
||||
self.assertEqual(self._text(host), before[host])
|
||||
|
||||
def test_a_member_whose_credential_vanished_is_reminted(self):
|
||||
self._write_all()
|
||||
keyed = existing_public_keys(self.host_vars, list(HOSTS), "swarm")
|
||||
self.assertIn("swarm-wrk-01", keyed)
|
||||
|
||||
stripped = self._text("swarm-wrk-01").replace(
|
||||
private_key_name("swarm"), "unrelated_key"
|
||||
)
|
||||
(self.host_vars / "swarm-wrk-01.yml").write_text(stripped, encoding="utf-8")
|
||||
|
||||
keyed_after = existing_public_keys(self.host_vars, list(HOSTS), "swarm")
|
||||
self.assertNotIn("swarm-wrk-01", keyed_after)
|
||||
|
||||
|
||||
def _hub_routes(entries: list[dict]) -> dict[str, list[str]]:
|
||||
return {
|
||||
entry["name"]: [
|
||||
peer["allowed_ips"] for peer in entry["peers"] if peer["is_hub"]
|
||||
]
|
||||
for entry in entries
|
||||
if not entry["is_hub"]
|
||||
}
|
||||
|
||||
|
||||
class TestTheLabUnderTheThreeMeshExample(MeshOnDisk, unittest.TestCase):
|
||||
LAYOUT = EXAMPLE_LAYOUT
|
||||
|
||||
def cluster_inventory(self) -> str:
|
||||
return (
|
||||
CLUSTER_INVENTORY
|
||||
+ " svc-bkp-nfs-2-local:\n hosts:\n nfs-server: {}\n"
|
||||
)
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
primary = primary_meshes(self.specs, self.topology)
|
||||
for spec in self.specs:
|
||||
write_mesh(
|
||||
plan_mesh(spec, self.topology),
|
||||
self.host_vars,
|
||||
self.vault_file,
|
||||
primary=primary,
|
||||
)
|
||||
|
||||
def test_the_nfs_server_is_a_spoke_of_beta_and_gamma(self):
|
||||
self.assertEqual(self._mesh_names("nfs-server"), ["beta", "gamma"])
|
||||
|
||||
def test_it_routes_the_pool_through_its_first_declared_mesh_only(self):
|
||||
self.assertEqual(
|
||||
_hub_routes(interface_entries(self.host_vars, "nfs-server")),
|
||||
{"beta": [MESH_POOL], "gamma": ["10.100.12.0/24"]},
|
||||
)
|
||||
|
||||
def test_exactly_one_entry_per_host_is_primary(self):
|
||||
for host in HOSTS:
|
||||
with self.subTest(host=host):
|
||||
flags = [
|
||||
entry["primary"]
|
||||
for entry in interface_entries(self.host_vars, host)
|
||||
]
|
||||
self.assertEqual(flags.count(True), 1)
|
||||
|
||||
def test_the_hub_is_primary_on_the_first_declared_mesh(self):
|
||||
self.assertTrue(self._entry("swarm-mgr-01", "alpha")["primary"])
|
||||
self.assertFalse(self._entry("swarm-mgr-01", "beta")["primary"])
|
||||
|
||||
|
||||
class TestNoTwoInterfacesClaimOneRoute(unittest.TestCase):
|
||||
def test_every_host_routes_each_prefix_through_one_interface(self):
|
||||
public_key = public_key_of(generate_private_key())
|
||||
for count in range(1, 9):
|
||||
for seed in range(3):
|
||||
specs, layout = topology(count, seed)
|
||||
specs = [replace(spec, routed_range=MESH_POOL) for spec in specs]
|
||||
hosts = sorted(
|
||||
{layout.hub}
|
||||
| {host for members in layout.spokes.values() for host in members}
|
||||
)
|
||||
primary = primary_meshes(specs, layout)
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
root = Path(td)
|
||||
for spec in specs:
|
||||
mesh = plan_mesh(spec, layout, dict.fromkeys(hosts, public_key))
|
||||
write_mesh(mesh, root, root / ".password", primary=primary)
|
||||
for host in hosts:
|
||||
if not (root / f"{host}.yml").exists():
|
||||
continue
|
||||
entries = interface_entries(root, host)
|
||||
routes = [
|
||||
peer["allowed_ips"]
|
||||
for entry in entries
|
||||
for peer in entry["peers"]
|
||||
]
|
||||
with self.subTest(meshes=count, seed=seed, host=host):
|
||||
self.assertEqual(len(routes), len(set(routes)))
|
||||
if _hub_routes(entries):
|
||||
self.assertEqual(routes.count(MESH_POOL), 1)
|
||||
@@ -22,6 +22,7 @@ def _entry(
|
||||
"variant": variant,
|
||||
"mode": mode,
|
||||
"tor": "true" if variant == "0" else "false",
|
||||
"vpn": "true" if mode == "swarm" else "false",
|
||||
"disable": "",
|
||||
"priority": "true" if priority else "false",
|
||||
"instructions": instructions,
|
||||
|
||||
Reference in new issue
Block a user