From 62145d087b59ebe1e5041c8f6b926e64157891b3 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Fri, 12 Jul 2024 19:56:04 +0200 Subject: [PATCH] Flock tests: encapsulated config and cleanup --- flock/bgp-secondary/bird_dest.conf | 4 ++-- flock/bgp-secondary/bird_src.conf | 4 ++-- flock/bgp-secondary/test.py | 31 +++--------------------------- flock/lib/BIRD/Test.py | 25 ++++++++++++++++++++++-- 4 files changed, 30 insertions(+), 34 deletions(-) diff --git a/flock/bgp-secondary/bird_dest.conf b/flock/bgp-secondary/bird_dest.conf index f17f3127..fe86c06b 100644 --- a/flock/bgp-secondary/bird_dest.conf +++ b/flock/bgp-secondary/bird_dest.conf @@ -14,8 +14,8 @@ protocol kernel kernel6 { } protocol bgp LINK { - local {{ links["L"]["dest"]["ipv6"].ip }} as 2; - neighbor {{ links["L"]["src"]["ipv6"].ip }} as 1; + local {{ t.links["L"]["dest"]["ipv6"].ip }} as 2; + neighbor {{ t.links["L"]["src"]["ipv6"].ip }} as 1; ipv6 { import all; export none; diff --git a/flock/bgp-secondary/bird_src.conf b/flock/bgp-secondary/bird_src.conf index 445f71ec..bf6f5d75 100644 --- a/flock/bgp-secondary/bird_src.conf +++ b/flock/bgp-secondary/bird_src.conf @@ -89,8 +89,8 @@ function CHECK() -> bool { } protocol bgp LINK { - local {{ links["L"]["src"]["ipv6"].ip }} as 1; - neighbor {{ links["L"]["dest"]["ipv6"].ip }} as 2; + local {{ t.links["L"]["src"]["ipv6"].ip }} as 1; + neighbor {{ t.links["L"]["dest"]["ipv6"].ip }} as 2; ipv6 { import none; export where CHECK(); diff --git a/flock/bgp-secondary/test.py b/flock/bgp-secondary/test.py index 857138a6..a49a0f0c 100644 --- a/flock/bgp-secondary/test.py +++ b/flock/bgp-secondary/test.py @@ -1,8 +1,6 @@ #!/usr/bin/python3 import asyncio -import ipaddress -import jinja2 import os import pathlib import sys @@ -26,30 +24,17 @@ class ThisTest(Test): "L": await self.link("L", "src", "dest") } + await super().start() + async def main(): t = ThisTest(name) + await t.start() h = t.hypervisor print(t.links, t.src, t.dest) - env = jinja2.Environment() - src_conf = open("bird_src.conf", "r").read() - jt = env.from_string(src_conf) - with open(t.src.workdir / "bird.conf", "w") as f: - f.write(jt.render( links=t.links )) - - dest_conf = open("bird_dest.conf", "r").read() - jt = env.from_string(dest_conf) - with open(t.dest.workdir / "bird.conf", "w") as f: - f.write(jt.render( links=t.links )) - - print(await asyncio.gather(*[ - h.control_socket.send_cmd("run_in", where, "./bird", "-l") - for where in ("src", "dest") - ])) - await asyncio.sleep(5) print(await asyncio.gather(*[ @@ -77,17 +62,7 @@ async def main(): for where in (t.src, t.dest) ])) - print(await asyncio.gather(*[ - c.down() - for c in (t.src, t.dest) - ])) - - await asyncio.sleep(5) await t.cleanup() - for q in (t.dest, t.src): - for f in ("bird.conf", "bird.log"): - (q.workdir / f).unlink() - await h.control_socket.send_cmd("stop", True) assert(__name__ == "__main__") diff --git a/flock/lib/BIRD/Test.py b/flock/lib/BIRD/Test.py index 0b813f9d..d02d7d9f 100644 --- a/flock/lib/BIRD/Test.py +++ b/flock/lib/BIRD/Test.py @@ -1,5 +1,6 @@ import asyncio import ipaddress +import jinja2 import os import pathlib import sys @@ -65,10 +66,11 @@ class BIRDBinDir: default_bindir = BIRDBinDir.get("..") class BIRDInstance(CLI): - def __init__(self, mach: Machine, bindir=None): + def __init__(self, mach: Machine, bindir=None, conf=None): self.mach = mach self.workdir = self.mach.workdir self.bindir = BIRDBinDir.get(bindir) if bindir is not None else default_bindir + self.conf = conf if conf is not None else f"bird_{mach.name}.conf" super().__init__( transport=MinimalistTransport( @@ -77,9 +79,24 @@ class BIRDInstance(CLI): ) ) + async def start(self, test): self.bindir.copy(self.workdir) + with (open(self.conf, "r") as s, open(self.workdir / "bird.conf", "w") as f): + f.write(jinja2.Environment().from_string(s.read()).render(t=test)) + + await test.hcom("run_in", self.mach.name, "./bird", "-l") + async def cleanup(self): + # Send down command and wait for BIRD to actually finish + await self.down() + while (self.workdir / "bird.ctl").exists(): + await asyncio.sleep(0.1) + + # Remove known files + for f in ("bird.conf", "bird.log"): + (self.workdir / f).unlink() + self.bindir.cleanup(self.workdir) class Test: @@ -133,7 +150,8 @@ class Test: name=n, hypervisor=self.hypervisor, **i - )) for n,i in zip(names, info) + ), + ) for n,i in zip(names, info) ] for n,i in zip(names, inst): @@ -162,5 +180,8 @@ class Test: case _: raise NotImplementedError("virtual bridge") + async def start(self): + await asyncio.gather(*[ v.start(test=self) for v in self.machine_index.values() ]) + async def cleanup(self): await asyncio.gather(*[ v.cleanup() for v in self.machine_index.values() ])