def test_init_address_book(self): ab = {"webservers": ["192.168.57.101/32", "192.168.57.230/32"]} c = models.Configuration(dict(networks=[], address_book=ab)) self.assertEqual( c.address_book.get('webservers'), models.AddressBookEntry('webservers', ab['webservers']))
def test_update_default_multiple_v6(self): subnet = dict( cidr='fe80::1/64', gateway_ip='fe80::1', dhcp_enabled=True, dns_nameservers=[], ) subnet2 = dict( cidr='fe89::1/64', gateway_ip='fe89::1', dhcp_enabled=True, dns_nameservers=[], ) network = dict( network_id='netid', name='thenet', interface=dict(ifname='ge0', addresses=['fe80::2']), allocations=[], subnets=[subnet, subnet2], network_type='external', ) c = models.Configuration({'networks': [network]}) with mock.patch.object(self.mgr, '_set_default_gateway') as set: self.mgr.update_default_gateway(c) net = c.networks[0] snet = net.subnets[0] set.assert_called_once_with(snet.gateway_ip, 'ge0')
def test_asn_provided_with_neighbor_different(self): c = models.Configuration({ 'networks': [], 'asn': 12, 'neighbor_asn': 34 }) self.assertEqual(c.asn, 12) self.assertEqual(c.neighbor_asn, 34)
def test_to_dict(self): c = models.Configuration({'networks': []}) expected = dict(networks=[], address_book={}, static_routes=[], anchors=[]) self.assertEqual(c.to_dict(), expected)
def test_init_anchor(self): test_rule = dict(action='block', source='192.168.1.1/32') anchor_dict = dict(name='theanchor', rules=[test_rule]) c = models.Configuration(dict(networks=[], anchors=[anchor_dict])) self.assertEqual(len(c.anchors), 1) self.assertEqual(len(c.anchors[0].rules), 1) self.assertEqual(c.anchors[0].rules[0].action, 'block')
def put_configuration(): if request.content_type != 'application/json': abort(415) try: config_candidate = models.Configuration(request.json) except ValueError, e: return Response('The config failed to deserialize.\n' + str(e), status=422)
def test_init_only_static_routes(self): routes = [('0.0.0.0/0', '192.168.1.1'), ('172.16.77.0/16', '192.168.1.254')] c = models.Configuration(dict(networks=[], static_routes=routes)) self.assertEqual(len(c.static_routes), 2) self.assertEqual( c.static_routes, [models.StaticRoute(*routes[0]), models.StaticRoute(*routes[1])])
def test_init_only_networks(self): subnet = dict(cidr='192.168.1.0/24', gateway_ip='192.168.1.1', dhcp_enabled=True, dns_nameservers=['8.8.8.8']) network = dict(network_id='netid', name='thenet', interface=dict(ifname='ge0', addresses=['192.168.1.1/24']), allocations=[], subnets=[subnet]) c = models.Configuration(dict(networks=[network])) self.assertEqual(len(c.networks), 1) self.assertEqual(c.networks[0], models.Network.from_dict(network))
def _validate_test_helper(self, rule_dict, expect_errors=False): network = dict(network_id='netid', name='thenet', interface=dict(ifname='ge0', addresses=['192.168.1.1/24']), allocations=[]) ab = {"webservers": ["192.168.57.101/32", "192.168.57.230/32"]} anchor_dict = dict(name='theanchor', rules=[rule_dict]) c = models.Configuration( dict(networks=[network], anchors=[anchor_dict], address_book=ab)) errors = c.validate() if expect_errors: return errors else: self.assertEqual(errors, [])
def test_send_gratuitous_arp_for_config(self): config = models.Configuration({ 'networks': [{ 'network_id': 'ABC456', 'interface': { 'ifname': 'ge1', 'name': 'ext', }, 'subnets': [{ 'cidr': '172.16.77.0/24', 'gateway_ip': '172.16.77.1', 'dhcp_enabled': True, 'dns_nameservers': [] }], 'network_type': models.Network.TYPE_EXTERNAL, }], 'floating_ips': [{ 'fixed_ip': '192.168.0.2', 'floating_ip': '172.16.77.50' }, { 'fixed_ip': '192.168.0.3', 'floating_ip': '172.16.77.51' }, { 'fixed_ip': '192.168.0.4', 'floating_ip': '172.16.77.52' }, { 'fixed_ip': '192.168.0.5', 'floating_ip': '172.16.77.53' }] }) with mock.patch.object(self.mgr, 'send_gratuitous_arp') as send_garp: self.mgr.send_gratuitous_arp_for_floating_ips( config, lambda x: x.replace('ge', 'eth')) assert send_garp.call_args_list == [ mock.call('eth1', '172.16.77.50'), mock.call('eth1', '172.16.77.51'), mock.call('eth1', '172.16.77.52'), mock.call('eth1', '172.16.77.53') ]
def test_custom_host_routes_failure(self): subnet = dict(cidr='192.168.89.0/24', gateway_ip='192.168.89.1', dhcp_enabled=True, dns_nameservers=[], host_routes=[{ 'destination': '192.240.128.0/20', 'nexthop': '192.168.89.2' }]) network = dict(network_id='netid', interface=dict(ifname='ge0', addresses=['fe80::2']), subnets=[subnet]) c = models.Configuration({'networks': [network]}) cache = make_region().configure('dogpile.cache.memory') with mock.patch.object(self.mgr, 'sudo') as sudo: sudo.side_effect = RuntimeError("Kaboom!") self.mgr.update_host_routes(c, cache) sudo.assert_called_once_with('-4', 'route', 'add', '192.240.128.0/20', 'via', '192.168.89.2', 'dev', 'eth0') self.assertEqual(len(cache.get('host_routes')), 0)
def test_custom_host_routes(self): subnet = dict(cidr='192.168.89.0/24', gateway_ip='192.168.89.1', dhcp_enabled=True, dns_nameservers=[], host_routes=[{ 'destination': '192.240.128.0/20', 'nexthop': '192.168.89.2' }]) network = dict(network_id='netid', interface=dict(ifname='ge0', addresses=['fe80::2']), subnets=[subnet]) c = models.Configuration({'networks': [network]}) cache = make_region().configure('dogpile.cache.memory') with mock.patch.object(self.mgr, 'sudo') as sudo: # ...so let's add one! self.mgr.update_host_routes(c, cache) sudo.assert_called_once_with('-4', 'route', 'add', '192.240.128.0/20', 'via', '192.168.89.2', 'dev', 'eth0') # db[subnet.cidr] should contain the above route expected = set() expected.add((netaddr.IPNetwork('192.240.138.0/20'), netaddr.IPAddress('192.168.89.2'))) self.assertEqual(len(cache.get('host_routes')), 1) self.assertEqual( cache.get('host_routes')[subnet['cidr']] - expected, set()) # Empty the host_routes list sudo.reset_mock() subnet['host_routes'] = [] c = models.Configuration({'networks': [network]}) self.mgr.update_host_routes(c, cache) sudo.assert_called_once_with('-4', 'route', 'del', '192.240.128.0/20', 'via', '192.168.89.2', 'dev', 'eth0') self.assertEqual(len(cache.get('host_routes')), 0) # ...this time, let's add multiple routes and ensure they're added sudo.reset_mock() subnet['host_routes'] = [{ 'destination': '192.240.128.0/20', 'nexthop': '192.168.89.2' }, { 'destination': '192.220.128.0/20', 'nexthop': '192.168.89.3' }] c = models.Configuration({'networks': [network]}) self.mgr.update_host_routes(c, cache) self.assertEqual(sudo.call_args_list, [ mock.call('-4', 'route', 'add', '192.240.128.0/20', 'via', '192.168.89.2', 'dev', 'eth0'), mock.call('-4', 'route', 'add', '192.220.128.0/20', 'via', '192.168.89.3', 'dev', 'eth0'), ]) # ...let's remove one and add another... sudo.reset_mock() subnet['host_routes'] = [{ 'destination': '192.240.128.0/20', 'nexthop': '192.168.89.2' }, { 'destination': '192.185.128.0/20', 'nexthop': '192.168.89.4' }] c = models.Configuration({'networks': [network]}) self.mgr.update_host_routes(c, cache) self.assertEqual(sudo.call_args_list, [ mock.call('-4', 'route', 'del', '192.220.128.0/20', 'via', '192.168.89.3', 'dev', 'eth0'), mock.call('-4', 'route', 'add', '192.185.128.0/20', 'via', '192.168.89.4', 'dev', 'eth0') ]) # ...let's add another subnet... self.assertEqual(len(cache.get('host_routes')), 1) sudo.reset_mock() network['subnets'].append( dict(cidr='192.168.90.0/24', gateway_ip='192.168.90.1', dhcp_enabled=True, dns_nameservers=[], host_routes=[{ 'destination': '192.240.128.0/20', 'nexthop': '192.168.90.1' }])) c = models.Configuration({'networks': [network]}) self.mgr.update_host_routes(c, cache) self.assertEqual(sudo.call_args_list, [ mock.call('-4', 'route', 'add', '192.240.128.0/20', 'via', '192.168.90.1', 'dev', 'eth0') ]) self.assertEqual(len(cache.get('host_routes')), 2) # ...and finally, delete all custom host_routes... sudo.reset_mock() network['subnets'][0]['host_routes'] = [] network['subnets'][1]['host_routes'] = [] c = models.Configuration({'networks': [network]}) self.mgr.update_host_routes(c, cache) self.assertEqual(sudo.call_args_list, [ mock.call('-4', 'route', 'del', '192.185.128.0/20', 'via', '192.168.89.4', 'dev', 'eth0'), mock.call('-4', 'route', 'del', '192.240.128.0/20', 'via', '192.168.89.2', 'dev', 'eth0'), mock.call('-4', 'route', 'del', '192.240.128.0/20', 'via', '192.168.90.1', 'dev', 'eth0'), ]) self.assertEqual(len(cache.get('host_routes')), 0)
def test_update_default_v4_from_gateway(self): c = models.Configuration({'default_v4_gateway': '172.16.77.1'}) with mock.patch.object(self.mgr, '_set_default_gateway') as set: self.mgr.update_default_gateway(c) set.assert_called_once_with(c.default_v4_gateway, None)
def test_update_default_no_inputs(self): c = models.Configuration({}) with mock.patch.object(self.mgr, '_set_default_gateway') as set: set.side_effect = AssertionError( 'should not try to set default gw') self.mgr.update_default_gateway(c)
def test_init_tenant_id(self): c = models.Configuration({'tenant_id': 'abc123'}) self.assertEqual(c.tenant_id, 'abc123')
def test_no_default_v4_gateway(self): c = models.Configuration({}) self.assertIsNone(c.default_v4_gateway)
def test_asn_provided_with_neighbor_fallback(self): c = models.Configuration({'networks': [], 'asn': 12345}) self.assertEqual(c.asn, 12345) self.assertEqual(c.neighbor_asn, 12345)
CONFIG = models.Configuration({ 'networks': [{ 'network_id': 'ABC123', 'interface': { 'ifname': 'eth0', 'addresses': [ 'fdca:3ba5:a17a:acda:f816:3eff:fe66:33b6/64', 'fe80::f816:3eff:fe66:33b6/64' ] }, 'name': 'mgt', 'network_type': models.Network.TYPE_MANAGEMENT, }, { 'network_id': 'ABC456', 'interface': { 'ifname': 'eth1', 'addresses': ['172.16.77.2/24', 'fdee:9f85:83be:0:f816:3eff:fe42:a9f/48'] }, 'name': 'ext', 'network_type': models.Network.TYPE_EXTERNAL, 'subnets': [{ 'cidr': '172.16.77.0/24', 'gateway_ip': '172.16.77.1', 'dhcp_enabled': True, 'dns_nameservers': [] }] }, { 'network_id': 'ABC789', 'interface': { 'ifname': 'eth2', 'addresses': ['192.168.0.1/24', 'fdd6:a1fa:cfa8:9df::1/64'] }, 'name': 'internal', 'network_type': models.Network.TYPE_INTERNAL, 'subnets': [{ 'cidr': '192.168.0.0/24', 'gateway_ip': '192.168.0.1', 'dhcp_enabled': True, 'dns_nameservers': [] }] }], 'floating_ips': [{ 'fixed_ip': '192.168.0.2', 'floating_ip': '172.16.77.50' }] })
def test_init_empty_anchor(self): anchor_dict = dict(name='theanchor', rules=[]) c = models.Configuration(dict(networks=[], anchors=[anchor_dict])) self.assertEqual(len(c.anchors), 1)
def test_init_label(self): labels = {"external": ["192.168.57.0/24"]} c = models.Configuration(dict(networks=[], labels=labels)) self.assertEqual(c.labels[0], models.Label('external', ['192.168.57.0/24']))
def test_valid_default_v4_gateway(self): c = models.Configuration({'default_v4_gateway': '172.16.77.1'}) self.assertEqual(c.default_v4_gateway.version, 4) self.assertEqual(str(c.default_v4_gateway), '172.16.77.1')
def __init__(self, state_path='.'): self.state_path = os.path.abspath(state_path) self.ip_mgr = ip.IPManager() self.ip_mgr.ensure_mapping() self._config = models.Configuration()
def test_asn_default(self): c = models.Configuration({'networks': []}) self.assertEqual(c.asn, 64512) self.assertEqual(c.neighbor_asn, 64512)