67 lines
2.7 KiB
Python
67 lines
2.7 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', 'add', '172.31.255.2/32', 'dev', 'lo'],
|
|
['ip', 'route', 'add', '172.31.255.1/32', 'dev', 'veth-olsr'],
|
|
])
|
|
|
|
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)
|