Files
mikrotik-olsr/tests/test_container.py
T
feedc0de 6aa407950a
Check source / check (push) Successful in 15s
Build OLSR container / image (push) Successful in 41s
Put BGP control endpoint on container VETH
2026-11-19 12:11:22 +01:00

88 lines
3.8 KiB
Python

import importlib.util
from pathlib import Path
import unittest
ROOT = Path(__file__).resolve().parents[1]
def module(name, file):
spec = importlib.util.spec_from_file_location(name, ROOT / 'container' / file)
result = importlib.util.module_from_spec(spec)
spec.loader.exec_module(result)
return result
start = module('container_start', 'start.py')
filters = module('container_filter', 'routeros-filter.py')
class ContainerRouting(unittest.TestCase):
def env(self):
return dict(OLSR_NODE_IP='10.12.4.173', BGP_LOCAL_IP='172.31.255.2',
BGP_PEER_IP='172.31.255.1')
def test_two_links_keep_identity(self):
env = self.env()
env['OLSR_LINKS'] = '[{"interface":"mesh4","address":"10.12.4.173","id":4},{"interface":"mesh5","address":"10.12.5.173","id":5}]'
olsr, bird = start.configuration(env)
self.assertIn('Interface "mesh4"', olsr)
self.assertIn('Interface "mesh5"', olsr)
self.assertIn('ifname = "mesh4" then bgp_community.add((64514, 4))', bird)
self.assertIn('ifname = "mesh5" then bgp_community.add((64514, 5))', bird)
self.assertNotIn('next hop self', bird)
self.assertIn('bgp_next_hop = gw', bird)
rule = filters.rule([(4, 'vlan4'), (5, 'ether2')], ['10.0.0.0/8'])
self.assertIn('set gw-interface vlan4', rule)
self.assertIn('set gw-interface ether2', rule)
self.assertTrue(rule.endswith('reject;'))
def test_mesh_identity_and_private_bgp_endpoint_are_distinct(self):
env = self.env()
olsr, bird = start.configuration(env)
self.assertIn('MainIp 10.12.4.173', olsr)
self.assertIn('IPv4Src 10.12.4.173', olsr)
self.assertIn('local 172.31.255.2 port 1179', bird)
self.assertIn('neighbor 172.31.255.1 as 64512', bird)
self.assertEqual(start.network_setup(env), [
['ip', 'address', 'flush', 'dev', 'veth-olsr', 'scope', 'global'],
['ip', 'address', 'add', '198.18.0.1/32', 'dev', 'veth-olsr'],
['ip', 'route', 'replace', 'local', '10.12.4.173/32', 'dev', 'lo'],
['ip', 'address', 'add', '172.31.255.2/32', 'dev', 'veth-olsr'],
['ip', 'route', 'replace', '172.31.255.1/32', 'dev', 'veth-olsr'],
])
def test_link_identity_is_separate_from_container_placeholder(self):
env = self.env()
olsr, _ = start.configuration(env)
setup = start.network_setup(env)
self.assertIn('MainIp 10.12.4.173', olsr)
self.assertIn('IPv4Src 10.12.4.173', olsr)
self.assertNotIn('10.12.4.173/32', [arg for command in setup
if command[1] == 'address'
for arg in command])
self.assertIn(['ip', 'route', 'replace', 'local',
'10.12.4.173/32', 'dev', 'lo'], setup)
def test_node_identity_must_belong_to_one_mesh_link(self):
env = self.env()
env['OLSR_LINKS'] = '[{"interface":"mesh5","address":"10.12.5.173","id":5}]'
with self.assertRaisesRegex(ValueError, 'one of the RouterOS-owned link addresses'):
start.configuration(env)
def test_ambiguous_links_rejected(self):
env = self.env()
env['OLSR_LINKS'] = '[{"interface":"a","address":"10.1.1.1","id":1},{"interface":"b","address":"10.1.1.2","id":1}]'
with self.assertRaises(ValueError):
start.configuration(env)
def test_no_default_or_injected_interface(self):
for links, prefixes in [([(1, 'vlan4')], ['0.0.0.0/0']),
([(1, 'vlan4; accept;')], ['10.0.0.0/8'])]:
with self.assertRaises(ValueError):
filters.rule(links, prefixes)
def test_hna_stays_blocked(self):
env = dict(self.env(), OLSR_HNA='10.1.2.0/24')
with self.assertRaises(ValueError):
start.configuration(env)