2024-07-10 10:44:53 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import ipaddress
|
|
|
|
import jinja2
|
|
|
|
import os
|
|
|
|
import pathlib
|
|
|
|
import sys
|
|
|
|
|
2024-07-11 20:31:06 +00:00
|
|
|
selfpath = pathlib.Path(__file__)
|
|
|
|
name = selfpath.parent.stem
|
2024-07-10 10:44:53 +00:00
|
|
|
|
2024-07-11 20:31:06 +00:00
|
|
|
sys.path.insert(0, str(selfpath.parent.parent / "lib"))
|
2024-07-10 13:41:20 +00:00
|
|
|
|
|
|
|
from BIRD.CLI import CLI, Transport
|
2024-07-11 20:31:06 +00:00
|
|
|
from BIRD.Test import Test
|
2024-07-10 13:41:20 +00:00
|
|
|
|
|
|
|
os.chdir(pathlib.Path(__file__).parent)
|
|
|
|
|
|
|
|
class MinimalistTransport(Transport):
|
|
|
|
def __init__(self, socket, machine):
|
|
|
|
self.sock = socket
|
|
|
|
self.machine = machine
|
|
|
|
|
|
|
|
async def send_cmd(self, *args):
|
|
|
|
return await self.sock.send_cmd("run_in", self.machine, "./birdc", "-l", *args)
|
2024-07-10 10:44:53 +00:00
|
|
|
|
2024-07-11 20:31:06 +00:00
|
|
|
class ThisTest(Test):
|
|
|
|
async def start(self):
|
|
|
|
self.src, self.dest = await self.machines("src", "dest")
|
|
|
|
|
2024-07-10 10:44:53 +00:00
|
|
|
async def main():
|
2024-07-11 20:31:06 +00:00
|
|
|
t = ThisTest(name)
|
|
|
|
await t.start()
|
2024-07-10 10:44:53 +00:00
|
|
|
|
2024-07-11 20:31:06 +00:00
|
|
|
h = t.hypervisor
|
2024-07-10 10:44:53 +00:00
|
|
|
|
|
|
|
link, = await asyncio.gather(
|
|
|
|
h.control_socket.send_cmd("link", "L", {
|
|
|
|
"machines": {
|
|
|
|
"src": { "name": "L" },
|
|
|
|
"dest": { "name": "L" },
|
|
|
|
},
|
|
|
|
"ipv6": "2001:db8:0:1::/64",
|
|
|
|
"ipv4": "10.0.1.0/29",
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
for m in link:
|
2024-07-11 20:31:06 +00:00
|
|
|
for i in ("ipv4", "ipv6"):
|
|
|
|
link[m][i] = ipaddress.ip_interface(link[m][i])
|
2024-07-10 10:44:53 +00:00
|
|
|
|
2024-07-11 20:31:06 +00:00
|
|
|
print(link, t.src, t.dest)
|
2024-07-10 10:44:53 +00:00
|
|
|
|
|
|
|
env = jinja2.Environment()
|
|
|
|
src_conf = open("bird_src.conf", "r").read()
|
|
|
|
jt = env.from_string(src_conf)
|
2024-07-11 20:31:06 +00:00
|
|
|
with open(t.src.workdir / "bird.conf", "w") as f:
|
2024-07-10 10:44:53 +00:00
|
|
|
f.write(jt.render( link=link ))
|
|
|
|
|
|
|
|
dest_conf = open("bird_dest.conf", "r").read()
|
|
|
|
jt = env.from_string(dest_conf)
|
2024-07-11 20:31:06 +00:00
|
|
|
with open(t.dest.workdir / "bird.conf", "w") as f:
|
2024-07-10 10:44:53 +00:00
|
|
|
f.write(jt.render( link=link ))
|
|
|
|
|
|
|
|
with open(pathlib.Path.cwd() / ".." / ".." / "bird", "rb") as b:
|
2024-07-11 20:31:06 +00:00
|
|
|
with open(t.dest.workdir / "bird", "wb") as d:
|
2024-07-10 10:44:53 +00:00
|
|
|
d.write(dta := b.read())
|
|
|
|
|
2024-07-11 20:31:06 +00:00
|
|
|
with open(t.src.workdir / "bird", "wb") as d:
|
2024-07-10 10:44:53 +00:00
|
|
|
d.write(dta)
|
|
|
|
|
|
|
|
with open(pathlib.Path.cwd() / ".." / ".." / "birdc", "rb") as b:
|
2024-07-11 20:31:06 +00:00
|
|
|
with open(t.dest.workdir / "birdc", "wb") as d:
|
2024-07-10 10:44:53 +00:00
|
|
|
d.write(dta := b.read())
|
|
|
|
|
2024-07-11 20:31:06 +00:00
|
|
|
with open(t.src.workdir / "birdc", "wb") as d:
|
2024-07-10 10:44:53 +00:00
|
|
|
d.write(dta)
|
|
|
|
|
2024-07-11 20:31:06 +00:00
|
|
|
os.chmod(t.dest.workdir / "bird", 0o755)
|
|
|
|
os.chmod(t.src.workdir / "bird", 0o755)
|
|
|
|
os.chmod(t.dest.workdir / "birdc", 0o755)
|
|
|
|
os.chmod(t.src.workdir / "birdc", 0o755)
|
2024-07-10 10:44:53 +00:00
|
|
|
|
|
|
|
print(await asyncio.gather(*[
|
|
|
|
h.control_socket.send_cmd("run_in", where, "./bird", "-l")
|
|
|
|
for where in ("src", "dest")
|
|
|
|
]))
|
|
|
|
|
2024-07-10 13:41:20 +00:00
|
|
|
await asyncio.sleep(5)
|
2024-07-10 10:44:53 +00:00
|
|
|
|
2024-07-10 13:41:20 +00:00
|
|
|
src_cli = CLI(MinimalistTransport(h.control_socket, "src"))
|
|
|
|
dest_cli = CLI(MinimalistTransport(h.control_socket, "dest"))
|
2024-07-10 10:44:53 +00:00
|
|
|
|
|
|
|
print(await asyncio.gather(*[
|
2024-07-11 13:29:28 +00:00
|
|
|
where.show_route()
|
|
|
|
for where in (src_cli, dest_cli)
|
2024-07-10 10:44:53 +00:00
|
|
|
]))
|
|
|
|
|
|
|
|
await asyncio.sleep(1)
|
2024-07-10 13:41:20 +00:00
|
|
|
|
|
|
|
for p in ("p170", "p180", "p190", "p200"):
|
2024-07-11 13:29:28 +00:00
|
|
|
await src_cli.enable(p)
|
2024-07-10 13:41:20 +00:00
|
|
|
await asyncio.sleep(1)
|
|
|
|
|
|
|
|
shr = await asyncio.gather(*[
|
2024-07-11 13:29:28 +00:00
|
|
|
where.show_route()
|
|
|
|
for where in (src_cli, dest_cli)
|
2024-07-10 13:41:20 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
print(shr[0]["out"].decode(), shr[1]["out"].decode())
|
|
|
|
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
|
|
|
|
print(await asyncio.gather(*[
|
2024-07-11 13:29:28 +00:00
|
|
|
where.show_route()
|
|
|
|
for where in (src_cli, dest_cli)
|
2024-07-10 13:41:20 +00:00
|
|
|
]))
|
|
|
|
|
|
|
|
print(await asyncio.gather(*[
|
|
|
|
c.down()
|
|
|
|
for c in (src_cli, dest_cli)
|
|
|
|
]))
|
|
|
|
|
|
|
|
await asyncio.sleep(5)
|
2024-07-11 20:31:06 +00:00
|
|
|
for q in (t.dest, t.src):
|
2024-07-10 13:41:20 +00:00
|
|
|
for f in ("bird", "birdc", "bird.conf", "bird.log"):
|
2024-07-11 20:31:06 +00:00
|
|
|
(q.workdir / f).unlink()
|
2024-07-10 13:41:20 +00:00
|
|
|
|
2024-07-10 10:44:53 +00:00
|
|
|
await h.control_socket.send_cmd("stop", True)
|
|
|
|
|
|
|
|
assert(__name__ == "__main__")
|
|
|
|
asyncio.run(main())
|