Example #1
0
    def test_init_address_book(self):
        ab = {"webservers": ["192.168.57.101/32", "192.168.57.230/32"]}

        c = models.RouterConfiguration(dict(networks=[], address_book=ab))
        self.assertEqual(
            c.address_book.get('webservers'),
            models.AddressBookEntry('webservers', ab['webservers']))
Example #2
0
 def test_update_default_multiple_v6(self):
     subnet = dict(
         id='id-1',
         cidr='fe80::1/64',
         gateway_ip='fe80::1',
         dhcp_enabled=True,
         dns_nameservers=[],
     )
     subnet2 = dict(
         id='id-2',
         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.RouterConfiguration({'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')
Example #3
0
    def test_custom_host_routes_failure(self):
        subnet = dict(id='theid',
                      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.RouterConfiguration({'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)
Example #4
0
 def get_config_or_default(self):
     # This is a hack to provide compatability with the original API, see
     # Manager.config()
     if not self._config:
         return models.RouterConfiguration()
     else:
         return self._config
Example #5
0
 def test_asn_provided_with_neighbor_different(self):
     c = models.RouterConfiguration({
         'networks': [],
         'asn': 12,
         'neighbor_asn': 34
     })
     self.assertEqual(c.asn, 12)
     self.assertEqual(c.neighbor_asn, 34)
Example #6
0
    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.RouterConfiguration(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')
Example #7
0
 def test_init_metadata_config(self):
     c = models.RouterConfiguration({
         'orchestrator': {
             'address': '192.168.25.30',
             'metadata_port': 9697,
         }
     })
     self.assertEqual(c.metadata_address, '192.168.25.30')
     self.assertEqual(c.metadata_port, 9697)
Example #8
0
    def test_to_dict(self):
        c = models.RouterConfiguration({'networks': []})
        expected = dict(networks=[],
                        address_book={},
                        static_routes=[],
                        anchors=[],
                        vpn=[])

        self.assertEqual(c.to_dict(), expected)
Example #9
0
    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.RouterConfiguration(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])])
Example #10
0
    def test_init_only_networks(self):
        subnet = dict(id='id',
                      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.RouterConfiguration(dict(networks=[network]))
        self.assertEqual(len(c.networks), 1)
        self.assertEqual(c.networks[0], models.Network.from_dict(network))
Example #11
0
    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.RouterConfiguration(
            dict(networks=[network], anchors=[anchor_dict], address_book=ab))

        errors = c.validate()

        if expect_errors:
            return errors
        else:
            self.assertEqual(errors, [])
Example #12
0
        return Response(
            'The system config failed to deserialize.\n' + str(e),
            status=422)

    errors = system_config_candidate.validate()
    if errors:
        return Response(
            'The config failed to validate.\n' + '\n'.join(errors),
            status=422)

    # Config requests to a router appliance will always contain a default ASN,
    # so we can key on that for now.  Later on we need to move router stuff
    # to the extensible list of things the appliance can handle
    if request.json.get('asn'):
        try:
            router_config_candidate = models.RouterConfiguration(request.json)
        except ValueError, e:
            return Response(
                'The router config failed to deserialize.\n' + str(e),
                status=422)

        errors = router_config_candidate.validate()
        if errors:
            return Response(
                'The config failed to validate.\n' + '\n'.join(errors),
                status=422)
    else:
        router_config_candidate = None

    if router_config_candidate:
        advanced_service_configs = [router_config_candidate]
Example #13
0
 def test_update_default_no_inputs(self):
     c = models.RouterConfiguration({})
     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)
Example #14
0
 def test_asn_provided_with_neighbor_fallback(self):
     c = models.RouterConfiguration({'networks': [], 'asn': 12345})
     self.assertEqual(c.asn, 12345)
     self.assertEqual(c.neighbor_asn, 12345)
Example #15
0
 def test_asn_default(self):
     c = models.RouterConfiguration({'networks': []})
     self.assertEqual(c.asn, 64512)
     self.assertEqual(c.neighbor_asn, 64512)
Example #16
0
 def test_init_metadata_config_missing(self):
     c = models.RouterConfiguration({})
     self.assertEqual(c.metadata_address,
                      defaults.ORCHESTRATOR_METADATA_ADDRESS)
     self.assertEqual(c.metadata_port, defaults.ORCHESTRATOR_METADATA_PORT)
Example #17
0
 def test_update_default_v4_from_gateway(self):
     c = models.RouterConfiguration({'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)
Example #18
0
    def test_custom_host_routes(self):
        subnet = dict(id='theid',
                      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.RouterConfiguration({'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.RouterConfiguration({'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.RouterConfiguration({'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.RouterConfiguration({'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(id='add-1',
                     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.RouterConfiguration({'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.RouterConfiguration({'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)
Example #19
0
    def test_init_empty_anchor(self):
        anchor_dict = dict(name='theanchor', rules=[])

        c = models.RouterConfiguration(dict(networks=[],
                                            anchors=[anchor_dict]))
        self.assertEqual(len(c.anchors), 1)
Example #20
0
    def test_init_label(self):
        labels = {"external": ["192.168.57.0/24"]}

        c = models.RouterConfiguration(dict(networks=[], labels=labels))
        self.assertEqual(c.labels[0],
                         models.Label('external', ['192.168.57.0/24']))
Example #21
0
CONFIG = models.RouterConfiguration({
    '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': [{
            'id': 'theid',
            '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': [{
            'id': 'theid',
            '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'
    }]
})
Example #22
0
 def test_valid_default_v4_gateway(self):
     c = models.RouterConfiguration({'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')
Example #23
0
 def test_no_default_v4_gateway(self):
     c = models.RouterConfiguration({})
     self.assertIsNone(c.default_v4_gateway)
Example #24
0
 def test_init_tenant_id(self):
     c = models.RouterConfiguration({'tenant_id': 'abc123'})
     self.assertEqual(c.tenant_id, 'abc123')