예제 #1
0
    def test_build_config(self):
        methods = {
            'load_provider_rules': mock.DEFAULT,
            'generate_network_config': mock.DEFAULT,
            'generate_floating_config': mock.DEFAULT,
            'get_default_v4_gateway': mock.DEFAULT,
        }

        mock_client = mock.Mock()
        ifaces = []
        provider_rules = {'labels': {'ext': ['192.168.1.1']}}
        network_config = [
            {'interface': 1,
             'network_id': 2,
             'v4_conf_service': 'static',
             'v6_conf_service': 'static',
             'network_type': 'external',
             'subnets': [
                 {'cidr': '192.168.1.0/24',
                  'dhcp_enabled': True,
                  'dns_nameservers': [],
                  'host_routes': [],
                  'gateway_ip': '192.168.1.1',
                  },
                 {'cidr': '10.0.0.0/24',
                  'dhcp_enabled': True,
                  'dns_nameservers': [],
                  'host_routes': [],
                  'gateway_ip': '10.0.0.1',
                  },
                 ],
             'allocations': []}
        ]

        with mock.patch.multiple(conf_mod, **methods) as mocks:
            mocks['load_provider_rules'].return_value = provider_rules
            mocks['generate_network_config'].return_value = network_config
            mocks['generate_floating_config'].return_value = 'floating_config'
            mocks['get_default_v4_gateway'].return_value = 'default_gw'

            config = conf_mod.build_config(mock_client, fake_router,
                                           fake_mgt_port, ifaces)

            expected = {
                'default_v4_gateway': 'default_gw',
                'networks': network_config,
                'labels': {'ext': ['192.168.1.1']},
                'floating_ips': 'floating_config',
                'asn': 64512,
                'neighbor_asn': 64512,
                'tenant_id': 'tenant_id',
                'hostname': 'ak-tenant_id'
            }

            self.assertEqual(config, expected)

            mocks['load_provider_rules'].assert_called_once_with('/the/path')
            mocks['generate_network_config'].assert_called_once_with(
                mock_client, fake_router, fake_mgt_port, ifaces)
예제 #2
0
    def build_config(self, worker_context, mgt_port, iface_map):
        """Builds / rebuilds config

        :param worker_context:
        :param mgt_port:
        :param iface_map:
        :returns: configuration object
        """
        self._ensure_cache(worker_context)
        return configuration.build_config(worker_context.neutron, self._router,
                                          mgt_port, iface_map)
예제 #3
0
    def configure(self, worker_context, failure_state=RESTART, attempts=None):
        self.log.debug('Begin router config')
        self.state = UP
        attempts = attempts or cfg.CONF.max_retries

        # FIXME: This might raise an error, which doesn't mean the
        # *router* is broken, but does mean we can't update it.
        # Change the exception to something the caller can catch
        # safely.
        self._ensure_cache(worker_context)
        if self.state == GONE:
            return

        addr = _get_management_address(self.router_obj)

        # FIXME: This should raise an explicit exception so the caller
        # knows that we could not talk to the router (versus the issue
        # above).
        interfaces = router_api.get_interfaces(
            addr, cfg.CONF.akanda_mgt_service_port)

        if not self._verify_interfaces(self.router_obj, interfaces):
            # FIXME: Need a REPLUG state when we support hot-plugging
            # interfaces.
            self.log.debug("Interfaces aren't plugged as expected.")
            self.state = REPLUG
            return

        # FIXME: Need to catch errors talking to neutron here.
        config = configuration.build_config(worker_context.neutron,
                                            self.router_obj, interfaces)
        self.log.debug('preparing to update config to %r', config)

        for i in xrange(attempts):
            try:
                router_api.update_config(addr,
                                         cfg.CONF.akanda_mgt_service_port,
                                         config)
            except Exception:
                if i == attempts - 1:
                    # Only log the traceback if we encounter it many times.
                    self.log.exception('failed to update config')
                else:
                    self.log.debug('failed to update config, attempt %d', i)
                time.sleep(cfg.CONF.retry_delay)
            else:
                self.state = CONFIGURED
                self.log.info('Router config updated')
                return
        else:
            # FIXME: We failed to configure the router too many times,
            # so restart it.
            self.state = failure_state
예제 #4
0
    def configure(self, worker_context, failure_state=RESTART, attempts=None):
        self.log.debug("Begin router config")
        self.state = UP
        attempts = attempts or cfg.CONF.max_retries

        # FIXME: This might raise an error, which doesn't mean the
        # *router* is broken, but does mean we can't update it.
        # Change the exception to something the caller can catch
        # safely.
        self._ensure_cache(worker_context)
        if self.state == GONE:
            return

        addr = _get_management_address(self.router_obj)

        # FIXME: This should raise an explicit exception so the caller
        # knows that we could not talk to the router (versus the issue
        # above).
        interfaces = router_api.get_interfaces(addr, cfg.CONF.akanda_mgt_service_port)

        if not self._verify_interfaces(self.router_obj, interfaces):
            # FIXME: Need a REPLUG state when we support hot-plugging
            # interfaces.
            self.log.debug("Interfaces aren't plugged as expected.")
            self.state = REPLUG
            return

        # FIXME: Need to catch errors talking to neutron here.
        config = configuration.build_config(worker_context.neutron, self.router_obj, interfaces)
        self.log.debug("preparing to update config to %r", config)

        for i in xrange(attempts):
            try:
                router_api.update_config(addr, cfg.CONF.akanda_mgt_service_port, config)
            except Exception:
                if i == attempts - 1:
                    # Only log the traceback if we encounter it many times.
                    self.log.exception("failed to update config")
                else:
                    self.log.debug("failed to update config, attempt %d", i)
                time.sleep(cfg.CONF.retry_delay)
            else:
                self.state = CONFIGURED
                self.log.info("Router config updated")
                return
        else:
            # FIXME: We failed to configure the router too many times,
            # so restart it.
            self.state = failure_state
예제 #5
0
    def build_config(self, worker_context, mgt_port, iface_map):
        """Builds / rebuilds config

        :param worker_context:
        :param mgt_port:
        :param iface_map:
        :returns: configuration object
        """
        self._ensure_cache(worker_context)
        return configuration.build_config(
            worker_context.neutron,
            self._router,
            mgt_port,
            iface_map
        )
예제 #6
0
    def test_build_config(self):
        methods = {
            'load_provider_rules': mock.DEFAULT,
            'generate_network_config': mock.DEFAULT,
            'generate_floating_config': mock.DEFAULT,
            'get_default_v4_gateway': mock.DEFAULT,
        }

        mock_client = mock.Mock()
        ifaces = []
        provider_rules = {'labels': {'ext': ['192.168.1.1']}}
        network_config = [{
            'interface':
            1,
            'network_id':
            2,
            'v4_conf_service':
            'static',
            'v6_conf_service':
            'static',
            'network_type':
            'external',
            'subnets': [
                {
                    'cidr': '192.168.1.0/24',
                    'dhcp_enabled': True,
                    'dns_nameservers': [],
                    'host_routes': [],
                    'gateway_ip': '192.168.1.1',
                },
                {
                    'cidr': '10.0.0.0/24',
                    'dhcp_enabled': True,
                    'dns_nameservers': [],
                    'host_routes': [],
                    'gateway_ip': '10.0.0.1',
                },
            ],
            'allocations': []
        }]

        with mock.patch.multiple(conf_mod, **methods) as mocks:
            mocks['load_provider_rules'].return_value = provider_rules
            mocks['generate_network_config'].return_value = network_config
            mocks['generate_floating_config'].return_value = 'floating_config'
            mocks['get_default_v4_gateway'].return_value = 'default_gw'

            config = conf_mod.build_config(mock_client, fake_router,
                                           fake_mgt_port, ifaces)

            expected = {
                'default_v4_gateway': 'default_gw',
                'networks': network_config,
                'labels': {
                    'ext': ['192.168.1.1']
                },
                'floating_ips': 'floating_config',
                'asn': 64512,
                'neighbor_asn': 64512,
                'tenant_id': 'tenant_id',
                'hostname': 'ak-tenant_id'
            }

            self.assertEqual(config, expected)

            mocks['load_provider_rules'].assert_called_once_with('/the/path')
            mocks['generate_network_config'].assert_called_once_with(
                mock_client, fake_router, fake_mgt_port, ifaces)
    def configure(self, worker_context, failure_state=RESTART, attempts=None):
        self.log.debug("Begin router config")
        self.state = UP
        attempts = attempts or cfg.CONF.max_retries

        # FIXME: This might raise an error, which doesn't mean the
        # *router* is broken, but does mean we can't update it.
        # Change the exception to something the caller can catch
        # safely.
        self._ensure_cache(worker_context)
        if self.state == GONE:
            return

        # FIXME: This should raise an explicit exception so the caller

        # knows that we could not talk to the router (versus the issue
        # above).
        interfaces = router_api.get_interfaces(self.instance_info.management_address, cfg.CONF.akanda_mgt_service_port)

        if not self._verify_interfaces(self.router_obj, interfaces):
            # FIXME: Need a REPLUG state when we support hot-plugging
            # interfaces.
            self.log.debug("Interfaces aren't plugged as expected.")
            self.state = REPLUG
            return

        # TODO(mark): We're in the first phase of VRRP, so we need
        # map the interface to the network ID.
        # Eventually we'll send VRRP data and real interface data
        port_mac_to_net = {p.mac_address: p.network_id for p in self.instance_info.ports}
        # Add in the management port
        mgt_port = self.instance_info.management_port
        port_mac_to_net[mgt_port.mac_address] = mgt_port.network_id

        # this is a network to logical interface id
        iface_map = {port_mac_to_net[i["lladdr"]]: i["ifname"] for i in interfaces if i["lladdr"] in port_mac_to_net}

        # FIXME: Need to catch errors talking to neutron here.
        config = configuration.build_config(worker_context.neutron, self.router_obj, mgt_port, iface_map)
        self.log.debug("preparing to update config to %r", config)

        for i in xrange(attempts):
            try:
                router_api.update_config(
                    self.instance_info.management_address, cfg.CONF.akanda_mgt_service_port, config
                )
            except Exception:
                if i == attempts - 1:
                    # Only log the traceback if we encounter it many times.
                    self.log.exception(_LE("Failed to update config"))
                else:
                    self.log.debug("failed to update config, attempt %d", i)
                time.sleep(cfg.CONF.retry_delay)
            else:
                self.state = CONFIGURED
                self.log.info(_LI("Router config updated"))
                return
        else:
            # FIXME: We failed to configure the router too many times,
            # so restart it.
            self.state = failure_state
예제 #8
0
    def test_build_config(self):
        methods = {
            "load_provider_rules": mock.DEFAULT,
            "generate_network_config": mock.DEFAULT,
            "generate_address_book_config": mock.DEFAULT,
            "generate_anchor_config": mock.DEFAULT,
            "generate_floating_config": mock.DEFAULT,
            "get_default_v4_gateway": mock.DEFAULT,
        }

        mock_client = mock.Mock()
        ifaces = []
        provider_rules = {"labels": {"ext": ["192.168.1.1"]}}
        network_config = [
            {
                "interface": 1,
                "network_id": 2,
                "v4_conf_service": "static",
                "v6_conf_service": "static",
                "network_type": "external",
                "subnets": [
                    {
                        "cidr": "192.168.1.0/24",
                        "dhcp_enabled": True,
                        "dns_nameservers": [],
                        "host_routes": [],
                        "gateway_ip": "192.168.1.1",
                    },
                    {
                        "cidr": "10.0.0.0/24",
                        "dhcp_enabled": True,
                        "dns_nameservers": [],
                        "host_routes": [],
                        "gateway_ip": "10.0.0.1",
                    },
                ],
                "allocations": [],
            }
        ]

        with mock.patch.multiple(conf_mod, **methods) as mocks:
            mocks["load_provider_rules"].return_value = provider_rules
            mocks["generate_network_config"].return_value = network_config
            mocks["generate_address_book_config"].return_value = "ab_config"
            mocks["generate_anchor_config"].return_value = "anchor_config"
            mocks["generate_floating_config"].return_value = "floating_config"
            mocks["get_default_v4_gateway"].return_value = "default_gw"

            config = conf_mod.build_config(mock_client, fake_router, ifaces)

            expected = {
                "default_v4_gateway": "default_gw",
                "networks": network_config,
                "address_book": "ab_config",
                "anchors": "anchor_config",
                "labels": {"ext": ["192.168.1.1"]},
                "floating_ips": "floating_config",
                "asn": 64512,
                "neighbor_asn": 64512,
                "tenant_id": "tenant_id",
                "hostname": "router_name",
            }

            self.assertEqual(config, expected)

            mocks["load_provider_rules"].assert_called_once_with("/the/path")
            mocks["generate_network_config"].assert_called_once_with(mock_client, fake_router, ifaces)
            mocks["generate_address_book_config"].assert_called_once_with(mock_client, fake_router)
            mocks["generate_anchor_config"].assert_called_once_with(mock_client, provider_rules, fake_router)