示例#1
0
    def test_port_bind(self, mock_getitem, mock_getattribute,
                       mock_configure_container_iface):
        fake_segmentation_id = 100
        fake_docker_endpoint_id = utils.get_hash()
        fake_docker_network_id = utils.get_hash()
        fake_port_id = uuidutils.generate_uuid()
        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
        fake_vif_details = {"port_filter": True, "ovs_hybrid_plug": False}
        fake_vif_type = "ovs"
        fake_port = self._get_fake_port(fake_docker_endpoint_id,
                                        fake_docker_network_id,
                                        fake_port_id,
                                        constants.PORT_STATUS_ACTIVE,
                                        fake_neutron_v4_subnet_id,
                                        fake_neutron_v6_subnet_id,
                                        vif_details=fake_vif_details,
                                        vif_type=fake_vif_type)
        fake_subnets = self._get_fake_subnets(fake_docker_endpoint_id,
                                              fake_docker_network_id,
                                              fake_neutron_v4_subnet_id,
                                              fake_neutron_v6_subnet_id)

        vlan.port_bind(fake_docker_endpoint_id, fake_port['port'],
                       fake_subnets['subnets'], fake_segmentation_id)

        mock_configure_container_iface.assert_called_once()
示例#2
0
    def test_port_bind(self, mock_getitem, mock_getattribute, mock_execute,
                       mock_path_exists):
        fake_mtu = 1450
        fake_docker_endpoint_id = utils.get_hash()
        fake_docker_network_id = utils.get_hash()
        fake_port_id = uuidutils.generate_uuid()
        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
        fake_vif_details = {"port_filter": True, "ovs_hybrid_plug": False}
        fake_vif_type = "ovs"
        fake_port = self._get_fake_port(fake_docker_endpoint_id,
                                        fake_docker_network_id,
                                        fake_port_id,
                                        constants.PORT_STATUS_ACTIVE,
                                        fake_neutron_v4_subnet_id,
                                        fake_neutron_v6_subnet_id,
                                        vif_details=fake_vif_details,
                                        vif_type=fake_vif_type)
        fake_subnets = self._get_fake_subnets(fake_docker_endpoint_id,
                                              fake_docker_network_id,
                                              fake_neutron_v4_subnet_id,
                                              fake_neutron_v6_subnet_id)
        fake_network = self._get_fake_networks(fake_docker_network_id)
        fake_network['networks'][0]['mtu'] = fake_mtu

        veth.port_bind(fake_docker_endpoint_id, fake_port['port'],
                       fake_subnets['subnets'], fake_network['networks'][0])

        expect_calls = [
            mock.call.__enter__().set_mtu(fake_mtu),
            mock.call.__enter__().up()
        ]
        mock_interface.assert_has_calls(expect_calls, any_order=True)
        mock_path_exists.assert_called_once()
        mock_execute.assert_called_once()
示例#3
0
    def test_port_bind(self, mock_getitem, mock_getattribute,
                       mock_execute, mock_path_exists):
        fake_mtu = 1450
        fake_docker_network_id = utils.get_hash()
        fake_docker_endpoint_id = utils.get_hash()
        fake_port_id = uuidutils.generate_uuid()
        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
        fake_port = self._get_fake_port(
            fake_docker_endpoint_id, fake_docker_network_id,
            fake_port_id, constants.PORT_STATUS_ACTIVE,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id)
        fake_subnets = self._get_fake_subnets(
            fake_docker_endpoint_id, fake_docker_network_id,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id)
        fake_network = self._get_fake_networks(fake_docker_network_id)
        fake_network['networks'][0]['mtu'] = fake_mtu

        binding.port_bind(fake_docker_endpoint_id, fake_port['port'],
                          fake_subnets['subnets'],
                          fake_network['networks'][0])

        expect_calls = [call.__enter__().set_mtu(fake_mtu),
                        call.__enter__().up()]
        mock_interface.assert_has_calls(expect_calls, any_order=True)
        mock_path_exists.assert_called_once()
        mock_execute.assert_called_once()
示例#4
0
    def test_network_driver_program_external_connectivity_disabled(
            self, existing_sg, num_ports, mock_list_ports,
            mock_create_security_group, mock_create_security_group_rule,
            mock_show_port, mock_update_port):
        config.CONF.set_override('process_external_connectivity', False)
        fake_docker_net_id = lib_utils.get_hash()
        fake_docker_endpoint_id = lib_utils.get_hash()

        port_opt = []
        for i in range(num_ports):
            port_opt.append({u'Port': PORT + i, u'Proto': PROTOCOL_TCP})
            port_opt.append({u'Port': PORT + i, u'Proto': PROTOCOL_UDP})
        port_opt.append({u'Port': SINGLE_PORT, u'Proto': PROTOCOL_UDP})
        options = {
            'com.docker.network.endpoint.exposedports': port_opt,
            'com.docker.network.portmap': []
        }
        data = {
            'NetworkID': fake_docker_net_id,
            'EndpointID': fake_docker_endpoint_id,
            'Options': options,
        }
        response = self.app.post('/NetworkDriver.ProgramExternalConnectivity',
                                 content_type='application/json',
                                 data=jsonutils.dumps(data))

        self.assertEqual(200, response.status_code)
        mock_update_port.assert_not_called()
        mock_show_port.assert_not_called()
        mock_create_security_group_rule.assert_not_called()
        mock_create_security_group.assert_not_called()
        mock_list_ports.assert_not_called()
        decoded_json = jsonutils.loads(response.data)
        self.assertEqual(constants.SCHEMA['SUCCESS'], decoded_json)
    def test_network_driver_program_external_connectivity_disabled(
            self, existing_sg,
            num_ports, mock_list_ports, mock_create_security_group,
            mock_create_security_group_rule, mock_show_port,
            mock_update_port):
        config.CONF.set_override('process_external_connectivity', False)
        fake_docker_net_id = lib_utils.get_hash()
        fake_docker_endpoint_id = lib_utils.get_hash()

        port_opt = []
        for i in range(num_ports):
            port_opt.append({u'Port': PORT + i, u'Proto': PROTOCOL_TCP})
            port_opt.append({u'Port': PORT + i, u'Proto': PROTOCOL_UDP})
        port_opt.append({u'Port': SINGLE_PORT, u'Proto': PROTOCOL_UDP})
        options = {'com.docker.network.endpoint.exposedports':
                   port_opt,
                   'com.docker.network.portmap':
                   []}
        data = {
            'NetworkID': fake_docker_net_id,
            'EndpointID': fake_docker_endpoint_id,
            'Options': options,
        }
        response = self.app.post('/NetworkDriver.ProgramExternalConnectivity',
                                 content_type='application/json',
                                 data=jsonutils.dumps(data))

        self.assertEqual(200, response.status_code)
        mock_update_port.assert_not_called()
        mock_show_port.assert_not_called()
        mock_create_security_group_rule.assert_not_called()
        mock_create_security_group.assert_not_called()
        mock_list_ports.assert_not_called()
        decoded_json = jsonutils.loads(response.data)
        self.assertEqual(constants.SCHEMA['SUCCESS'], decoded_json)
    def test_network_driver_revoke_external_connectivity_disabled(
            self, existing_sg,
            removing_sg, mock_list_ports, mock_list_security_groups,
            mock_delete_security_groups, mock_show_port,
            mock_update_port):
        config.CONF.set_override('process_external_connectivity', False)
        fake_docker_net_id = lib_utils.get_hash()
        fake_docker_endpoint_id = lib_utils.get_hash()

        data = {
            'NetworkID': fake_docker_net_id,
            'EndpointID': fake_docker_endpoint_id,
        }
        response = self.app.post('/NetworkDriver.RevokeExternalConnectivity',
                                 content_type='application/json',
                                 data=jsonutils.dumps(data))

        self.assertEqual(200, response.status_code)
        mock_list_ports.assert_not_called()
        mock_list_security_groups.assert_not_called()
        mock_delete_security_groups.assert_not_called()
        mock_show_port.assert_not_called()
        mock_update_port.assert_not_called()
        decoded_json = jsonutils.loads(response.data)
        self.assertEqual(constants.SCHEMA['SUCCESS'], decoded_json)
示例#7
0
 def test_port_unbind(self, mock_execute):
     fake_docker_endpoint_id = utils.get_hash()
     fake_docker_network_id = utils.get_hash()
     fake_port_id = uuidutils.generate_uuid()
     fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
     fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
     fake_vif_type = "ovs"
     fake_port = self._get_fake_port(
         fake_docker_endpoint_id, fake_docker_network_id,
         fake_port_id, constants.PORT_STATUS_ACTIVE,
         fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id,
         vif_type=fake_vif_type)
     fake_pf_ifname = 'eth13'
     fake_vf_num = 1
     hw_veb.port_unbind(fake_docker_endpoint_id, fake_port['port'],
                        pf_ifname=fake_pf_ifname,
                        vf_num=fake_vf_num)
     mock_execute.assert_called_once()
     mock_execute.assert_called_once_with(
         'ip', 'link', 'set', fake_pf_ifname,
         'vf', fake_vf_num,
         'mac', fake_port['port']['mac_address'],
         'vlan', 0,
         run_as_root=True,
         check_exit_code=[0, 2, 254])
示例#8
0
    def test_port_bind(self, mock_getattribute, mock_getitem, mock_execute,
                       mock_path_exists):
        fake_mtu = 1450
        fake_docker_network_id = utils.get_hash()
        fake_docker_endpoint_id = utils.get_hash()
        fake_port_id = str(uuid.uuid4())
        fake_neutron_v4_subnet_id = str(uuid.uuid4())
        fake_neutron_v6_subnet_id = str(uuid.uuid4())
        fake_port = self._get_fake_port(fake_docker_endpoint_id,
                                        fake_docker_network_id, fake_port_id,
                                        constants.PORT_STATUS_ACTIVE,
                                        fake_neutron_v4_subnet_id,
                                        fake_neutron_v6_subnet_id)
        fake_subnets = self._get_fake_subnets(fake_docker_endpoint_id,
                                              fake_docker_network_id,
                                              fake_neutron_v4_subnet_id,
                                              fake_neutron_v6_subnet_id)
        fake_network = self._get_fake_networks(fake_docker_network_id)
        fake_network['networks'][0]['mtu'] = fake_mtu

        binding.port_bind(fake_docker_endpoint_id, fake_port['port'],
                          fake_subnets['subnets'], fake_network['networks'][0])

        expect_calls = [
            call.__enter__().set_mtu(fake_mtu),
            call.__enter__().up()
        ]
        mock_interface.assert_has_calls(expect_calls, any_order=True)
        mock_path_exists.assert_called_once()
        mock_execute.assert_called_once()
示例#9
0
    def test_create_endpoint_port_failures(self, GivenException,
                                           mock_list_ports, mock_list_subnets,
                                           mock_list_networks,
                                           mock_create_port):
        fake_docker_network_id = lib_utils.get_hash()
        fake_docker_endpoint_id = lib_utils.get_hash()
        fake_neutron_network_id = uuidutils.generate_uuid()
        fake_neutron_subnet_v4_id = uuidutils.generate_uuid()
        fake_neutron_subnet_v6_id = uuidutils.generate_uuid()

        fake_v4_subnet = self._get_fake_v4_subnet(fake_neutron_network_id,
                                                  fake_docker_endpoint_id,
                                                  fake_neutron_subnet_v4_id)
        fake_v6_subnet = self._get_fake_v6_subnet(fake_neutron_network_id,
                                                  fake_docker_endpoint_id,
                                                  fake_neutron_subnet_v6_id)
        fake_v4_subnet_response = {"subnets": [fake_v4_subnet['subnet']]}
        fake_v6_subnet_response = {"subnets": [fake_v6_subnet['subnet']]}

        fake_fixed_ips = [
            'subnet_id=%s' % fake_neutron_subnet_v4_id,
            'ip_address=192.168.1.2',
            'subnet_id=%s' % fake_neutron_subnet_v6_id,
            'ip_address=fe80::f816:3eff:fe20:57c4'
        ]
        fake_port_response = {"ports": []}
        t = utils.make_net_tags(fake_docker_network_id)
        fake_neutron_network = self._get_fake_list_network(
            fake_neutron_network_id)
        mock_list_ports.return_value = fake_port_response

        def mock_fake_subnet(*args, **kwargs):
            if kwargs['cidr'] == '192.168.1.0/24':
                return fake_v4_subnet_response
            elif kwargs['cidr'] == 'fe80::/64':
                return fake_v6_subnet_response

        mock_list_subnets.side_effect = mock_fake_subnet
        mock_list_networks.return_value = fake_neutron_network
        mock_create_port.side_effect = GivenException
        fake_port_request = self._get_fake_port_request(
            fake_neutron_network_id, fake_docker_endpoint_id,
            fake_neutron_subnet_v4_id, fake_neutron_subnet_v6_id)

        response = self._invoke_create_request(fake_docker_network_id,
                                               fake_docker_endpoint_id)

        self.assertEqual(GivenException.status_code, response.status_code)
        mock_list_networks.assert_called_with(tags=t)
        mock_create_port.assert_called_with(fake_port_request)
        mock_list_subnets.assert_any_call(network_id=fake_neutron_network_id,
                                          cidr='192.168.1.0/24')
        mock_list_subnets.assert_any_call(network_id=fake_neutron_network_id,
                                          cidr='fe80::/64')
        mock_list_ports.assert_called_with(fixed_ips=fake_fixed_ips)
        decoded_json = jsonutils.loads(response.data)
        self.assertIn('Err', decoded_json)
        self.assertEqual({'Err': GivenException.message}, decoded_json)
示例#10
0
    def test_delete_network_failures(self, GivenException, mock_list_networks,
                                     mock_list_subnets, mock_list_subnetpools,
                                     mock_delete_subnet, mock_delete_network):
        fake_subnetpools_response = {"subnetpools": []}
        docker_network_id = lib_utils.get_hash()
        docker_endpoint_id = lib_utils.get_hash()
        fake_neutron_network_id = "4e8e5957-649f-477b-9e5b-f1f75b21c03c"
        t = utils.make_net_tags(docker_network_id)
        te = t + ',' + utils.existing_net_tag(docker_network_id)
        subnet_v4_id = "9436e561-47bf-436a-b1f1-fe23a926e031"
        subnet_v6_id = "64dd4a98-3d7a-4bfd-acf4-91137a8d2f51"

        def mock_network(*args, **kwargs):
            if kwargs['tags'] == te:
                return self._get_fake_list_network(fake_neutron_network_id,
                                                   check_existing=True)
            elif kwargs['tags'] == t:
                if GivenException == exceptions.NotFound:
                    return self._get_fake_list_network(fake_neutron_network_id,
                                                       check_existing=True)
                return self._get_fake_list_network(fake_neutron_network_id)

        fake_v4_subnet = self._get_fake_v4_subnet(docker_network_id,
                                                  docker_endpoint_id,
                                                  subnet_v4_id)
        fake_v6_subnet = self._get_fake_v6_subnet(docker_network_id,
                                                  docker_endpoint_id,
                                                  subnet_v6_id)
        fake_subnets_response = {
            "subnets": [fake_v4_subnet['subnet'], fake_v6_subnet['subnet']]
        }
        mock_list_networks.side_effect = mock_network
        mock_list_subnets.return_value = fake_subnets_response
        mock_list_subnetpools.return_value = fake_subnetpools_response
        mock_delete_subnet.return_value = None
        mock_delete_network.side_effect = GivenException

        response = self._invoke_delete_request(docker_network_id)
        decoded_json = jsonutils.loads(response.data)
        if GivenException == exceptions.NotFound:
            self.assertEqual(GivenException.status_code, 404)
            self.assertEqual(const.SCHEMA['SUCCESS'], decoded_json)
        else:
            self.assertEqual(GivenException.status_code, response.status_code)
            fake_v4_pool_attrs = {'name': 'kuryr'}
            fake_v6_pool_attrs = {'name': 'kuryr6'}
            mock_list_subnetpools.assert_any_call(**fake_v6_pool_attrs)
            mock_list_subnetpools.assert_any_call(**fake_v4_pool_attrs)
            mock_delete_subnet.assert_any_call(subnet_v4_id)
            mock_delete_subnet.assert_any_call(subnet_v6_id)
            mock_delete_network.assert_called_with(fake_neutron_network_id)
            mock_list_subnets.assert_called_with(
                network_id=fake_neutron_network_id)
            self.assertIn('Err', decoded_json)
            self.assertEqual({'Err': GivenException.message}, decoded_json)
        mock_list_networks.assert_any_call(tags=t)
        mock_list_networks.assert_any_call(tags=te)
    def test_delete_network_with_subnet_deletion_failures(self,
            GivenException, mock_list_networks, mock_list_subnets,
            mock_list_subnetpools, mock_delete_subnet):

        fake_subnetpools_response = {"subnetpools": []}
        docker_network_id = lib_utils.get_hash()
        docker_endpoint_id = lib_utils.get_hash()
        fake_neutron_network_id = "4e8e5957-649f-477b-9e5b-f1f75b21c03c"
        t = utils.make_net_tags(docker_network_id)
        te = t + ',' + utils.existing_net_tag(docker_network_id)
        subnet_v4_id = "9436e561-47bf-436a-b1f1-fe23a926e031"
        subnet_v6_id = "64dd4a98-3d7a-4bfd-acf4-91137a8d2f51"

        def mock_network(*args, **kwargs):
            if kwargs['tags'] == te:
                return self._get_fake_list_network(
                    fake_neutron_network_id,
                    check_existing=True)
            elif kwargs['tags'] == t:
                return self._get_fake_list_network(
                    fake_neutron_network_id)

        fake_v4_subnet = self._get_fake_v4_subnet(
            docker_network_id, docker_endpoint_id, subnet_v4_id)
        fake_v6_subnet = self._get_fake_v6_subnet(
            docker_network_id, docker_endpoint_id, subnet_v6_id)
        fake_subnets_response = {
            "subnets": [
                fake_v4_subnet['subnet'],
                fake_v6_subnet['subnet']
            ]
        }
        mock_list_networks.side_effect = mock_network
        mock_list_subnets.return_value = fake_subnets_response
        mock_list_subnetpools.return_value = fake_subnetpools_response
        mock_delete_subnet.side_effect = GivenException
        response = self._invoke_delete_request(docker_network_id)

        self.assertEqual(GivenException.status_code, response.status_code)
        mock_list_networks.assert_any_call(tags=t)
        mock_list_networks.assert_any_call(tags=te)
        mock_list_subnets.assert_called_with(
            network_id=fake_neutron_network_id)
        fake_v4_pool_attrs = {'name': 'kuryr'}
        fake_v6_pool_attrs = {'name': 'kuryr6'}
        mock_list_subnetpools.assert_any_call(**fake_v6_pool_attrs)
        mock_list_subnetpools.assert_any_call(**fake_v4_pool_attrs)
        mock_delete_subnet.assert_called_with(subnet_v4_id)

        decoded_json = jsonutils.loads(response.data)
        self.assertIn('Err', decoded_json)
        self.assertEqual({'Err': GivenException.message}, decoded_json)
示例#12
0
 def test_port_unbind(self, mock_execute, mock_cleanup_veth):
     fake_docker_network_id = utils.get_hash()
     fake_docker_endpoint_id = utils.get_hash()
     fake_port_id = str(uuid.uuid4())
     fake_neutron_v4_subnet_id = str(uuid.uuid4())
     fake_neutron_v6_subnet_id = str(uuid.uuid4())
     fake_port = self._get_fake_port(
         fake_docker_endpoint_id, fake_docker_network_id,
         fake_port_id, constants.PORT_STATUS_ACTIVE,
         fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id)
     binding.port_unbind(fake_docker_endpoint_id, fake_port['port'])
     mock_execute.assert_called_once()
     mock_cleanup_veth.assert_called_once()
示例#13
0
 def test_port_unbind(self, mock_execute, mock_remove_device):
     fake_docker_network_id = utils.get_hash()
     fake_docker_endpoint_id = utils.get_hash()
     fake_port_id = uuidutils.generate_uuid()
     fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
     fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
     fake_port = self._get_fake_port(
         fake_docker_endpoint_id, fake_docker_network_id,
         fake_port_id, constants.PORT_STATUS_ACTIVE,
         fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id)
     binding.port_unbind(fake_docker_endpoint_id, fake_port['port'])
     mock_execute.assert_called_once()
     mock_remove_device.assert_called_once()
示例#14
0
 def test_port_unbind(self, mock_execute, mock_remove_device):
     fake_docker_network_id = utils.get_hash()
     fake_docker_endpoint_id = utils.get_hash()
     fake_port_id = str(uuid.uuid4())
     fake_neutron_v4_subnet_id = str(uuid.uuid4())
     fake_neutron_v6_subnet_id = str(uuid.uuid4())
     fake_port = self._get_fake_port(fake_docker_endpoint_id,
                                     fake_docker_network_id, fake_port_id,
                                     constants.PORT_STATUS_ACTIVE,
                                     fake_neutron_v4_subnet_id,
                                     fake_neutron_v6_subnet_id)
     binding.port_unbind(fake_docker_endpoint_id, fake_port['port'])
     mock_execute.assert_called_once()
     mock_remove_device.assert_called_once()
示例#15
0
    def test_update_port_with_device_id(self, mock_get_port_name,
                                        mock_update_port):
        fake_endpoint_id = lib_utils.get_hash()
        fake_neutron_port_id = uuidutils.generate_uuid()
        fake_neutron_net_id = uuidutils.generate_uuid()
        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
        fake_mac_address1 = 'fa:16:3e:20:57:c3'
        fake_mac_address2 = 'fa:16:3e:20:57:c4'
        fake_neutron_port = self._get_fake_port(
            fake_endpoint_id, fake_neutron_net_id, fake_neutron_port_id,
            lib_const.PORT_STATUS_ACTIVE, fake_neutron_v4_subnet_id,
            fake_neutron_v6_subnet_id, '192.168.1.3',
            'fe80::f816:3eff:fe1c:36a9', fake_mac_address1)['port']
        fake_neutron_port.pop('device_id')
        fake_port_name = '-'.join([fake_endpoint_id, lib_utils.PORT_POSTFIX])
        mock_get_port_name.return_value = fake_port_name

        veth_driver = veth.VethDriver()
        veth_driver.update_port(fake_neutron_port, fake_endpoint_id,
                                fake_mac_address2)

        mock_get_port_name.assert_called_with(fake_endpoint_id)
        expected_update_port = {
            'port': {
                'name': fake_port_name,
                'device_owner': lib_const.DEVICE_OWNER,
                'binding:host_id': lib_utils.get_hostname(),
                'device_id': fake_endpoint_id,
                'mac_address': fake_mac_address2
            }
        }
        mock_update_port.assert_called_with(fake_neutron_port_id,
                                            expected_update_port)
示例#16
0
    def test_create_network_unauthorized(self, mock_list_subnetpools,
                                         mock_get_default_network,
                                         mock_create_network):
        docker_network_id = lib_utils.get_hash()
        network_request = {
            'NetworkID':
            docker_network_id,
            'IPv4Data': [{
                'AddressSpace': 'foo',
                'Pool': '192.168.42.0/24',
                'Gateway': '192.168.42.1/24',
                'AuxAddresses': {}
            }],
            'IPv6Data': [{
                'AddressSpace': 'bar',
                'Pool': 'fe80::/64',
                'Gateway': 'fe80::f816:3eff:fe20:57c3/64',
                'AuxAddresses': {}
            }],
            'Options': {}
        }

        fake_kuryr_v4_subnetpool_id = uuidutils.generate_uuid()
        fake_v4_pool_name = lib_utils.get_neutron_subnetpool_name(
            network_request['IPv4Data'][0]['Pool'])
        kuryr_v4_subnetpools = self._get_fake_v4_subnetpools(
            fake_kuryr_v4_subnetpool_id, name=fake_v4_pool_name)

        fake_kuryr_v6_subnetpool_id = uuidutils.generate_uuid()
        fake_v6_pool_name = lib_utils.get_neutron_subnetpool_name(
            network_request['IPv6Data'][0]['Pool'])
        kuryr_v6_subnetpools = self._get_fake_v6_subnetpools(
            fake_kuryr_v6_subnetpool_id, name=fake_v6_pool_name)
        fake_v4_pool_attrs = {'name': fake_v4_pool_name}
        fake_v6_pool_attrs = {'name': fake_v6_pool_name}
        mock_list_subnetpools.side_effect = [{
            'subnetpools':
            kuryr_v4_subnetpools['subnetpools']
        }, {
            'subnetpools':
            kuryr_v6_subnetpools['subnetpools']
        }]

        fake_request = {
            "network": {
                "name": utils.make_net_name(docker_network_id),
                "admin_state_up": True,
                "shared": False
            }
        }
        mock_create_network.side_effect = exceptions.Unauthorized
        response = self._invoke_create_request(network_request)
        self.assertEqual(401, response.status_code)
        decoded_json = jsonutils.loads(response.data)
        mock_list_subnetpools.assert_any_call(**fake_v4_pool_attrs)
        mock_list_subnetpools.assert_any_call(**fake_v6_pool_attrs)
        mock_create_network.assert_called_with(fake_request)
        self.assertIn('Err', decoded_json)
        self.assertEqual({'Err': exceptions.Unauthorized.message},
                         decoded_json)
    def test_release_vif(self):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        m_driver = mock.Mock(spec=cls)
        neutron = self.useFixture(k_fix.MockNeutronClient()).client

        port_id = lib_utils.get_hash()
        pod = mock.sentinel.pod
        vif = mock.Mock()
        vif.id = port_id

        container_mac = mock.sentinel.mac_address
        container_ip = mock.sentinel.ip_address
        container_port = self._get_fake_port(port_id, container_ip,
                                             container_mac)
        neutron.show_port.return_value = container_port

        vm_port = self._get_fake_port()
        m_driver._get_parent_port.return_value = vm_port
        m_driver._try_update_port.return_value = 0
        m_driver.lock = mock.MagicMock(spec=threading.Lock())

        cls.release_vif(m_driver, pod, vif)

        neutron.show_port.assert_called_once_with(port_id)
        m_driver._get_parent_port.assert_called_once_with(neutron, pod)
        m_driver._try_update_port.assert_called_once()
        neutron.delete_port.assert_called_once_with(vif.id)
示例#18
0
    def test_update_port_with_no_mac_address(self, mock_get_port_name,
                                             mock_update_port):
        fake_endpoint_id = lib_utils.get_hash()
        fake_neutron_port_id = uuidutils.generate_uuid()
        fake_neutron_net_id = uuidutils.generate_uuid()
        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
        fake_neutron_port = self._get_fake_port(fake_endpoint_id,
                                                fake_neutron_net_id,
                                                fake_neutron_port_id,
                                                lib_const.PORT_STATUS_ACTIVE,
                                                fake_neutron_v4_subnet_id,
                                                fake_neutron_v6_subnet_id,
                                                '192.168.1.3',
                                                'fe80::f816:3eff:fe1c:36a9',
                                                admin_state_up=False,
                                                binding_host='')['port']
        fake_port_name = '-'.join([fake_endpoint_id, lib_utils.PORT_POSTFIX])
        mock_get_port_name.return_value = fake_port_name

        veth_driver = veth.VethDriver()
        veth_driver.update_port(fake_neutron_port, fake_endpoint_id, '')

        mock_get_port_name.assert_called_with(fake_endpoint_id)
        expected_update_port = {
            'port': {
                'device_owner': lib_const.DEVICE_OWNER,
                'binding:host_id': lib_utils.get_hostname(),
                'admin_state_up': True,
            }
        }
        mock_update_port.assert_called_with(fake_neutron_port_id,
                                            expected_update_port)
    def test_try_update_port(self, aaapf_mock):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        m_driver = mock.Mock(spec=cls)
        m_driver.lock = mock.MagicMock(spec=threading.Lock())
        self.useFixture(k_fix.MockNetworkClient()).client

        port_id = lib_utils.get_hash()
        vm_port = fake.get_port_obj(port_id)

        mac_addr = 'fa:16:3e:1b:30:00'
        address_pairs = [
            {
                'ip_address': '10.0.0.30',
                'mac_address': mac_addr
            },
            {
                'ip_address': 'fe80::f816:3eff:fe1c:36a9',
                'mac_address': mac_addr
            },
        ]
        vm_port['allowed_address_pairs'].extend(address_pairs)

        ip_addr = ['10.0.0.29']
        attempts = cls._try_update_port(m_driver, 3,
                                        cls._add_to_allowed_address_pairs,
                                        vm_port, frozenset(ip_addr), mac_addr)
        self.assertEqual(attempts, 0)
        aaapf_mock.assert_called_once()
    def test_try_update_port_failure(self, aaapf_mock):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        m_driver = mock.Mock(spec=cls)
        m_driver.lock = mock.MagicMock(spec=threading.Lock())
        self.useFixture(k_fix.MockNetworkClient()).client

        port_id = lib_utils.get_hash()
        vm_port = fake.get_port_obj(port_id)

        mac_addr = 'fa:16:3e:1b:30:00'
        address_pairs = [
            {
                'ip_address': '10.0.0.30',
                'mac_address': mac_addr
            },
            {
                'ip_address': 'fe80::f816:3eff:fe1c:36a9',
                'mac_address': mac_addr
            },
        ]
        vm_port['allowed_address_pairs'].extend(address_pairs)

        ip_addr = ['10.0.0.29']

        aaapf_mock.side_effect = o_exc.SDKException
        self.assertRaises(o_exc.SDKException, cls._try_update_port, m_driver,
                          1, cls._add_to_allowed_address_pairs, vm_port,
                          frozenset(ip_addr), mac_addr)
示例#21
0
    def test_delete_host_iface(self, mock_get_port_from_host, mock_update_port,
                               mock_port_unbind, mock_conf):
        mock_conf.binding.link_iface = 'eth0'

        fake_endpoint_id = lib_utils.get_hash()
        fake_neutron_port_id = uuidutils.generate_uuid()
        fake_neutron_net_id = uuidutils.generate_uuid()
        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
        fake_vm_port_id = uuidutils.generate_uuid()

        fake_neutron_port = self._get_fake_port(
            fake_endpoint_id, fake_neutron_net_id, fake_neutron_port_id,
            lib_const.PORT_STATUS_ACTIVE, fake_neutron_v4_subnet_id,
            fake_neutron_v6_subnet_id, '192.168.1.3',
            'fe80::f816:3eff:fe1c:36a9')['port']
        fake_neutron_port['mac_address'] = 'fa:16:3e:20:57:c3'
        fake_vm_port = self._get_fake_port(
            fake_endpoint_id, fake_neutron_net_id, fake_vm_port_id,
            lib_const.PORT_STATUS_ACTIVE, fake_neutron_v4_subnet_id,
            fake_neutron_v6_subnet_id, '192.168.1.2',
            'fe80::f816:3eff:fe20:57c4')['port']
        fake_vm_port['allowed_address_pairs'] = [{
            'ip_address':
            '192.168.1.3',
            'mac_address':
            fake_neutron_port['mac_address']
        }, {
            'ip_address':
            'fe80::f816:3eff:fe1c:36a9',
            'mac_address':
            fake_neutron_port['mac_address']
        }]
        updated_allowed_pairs = [{
            'ip_address': '192.168.1.2',
            'mac_address': fake_vm_port['mac_address']
        }, {
            'ip_address': 'fe80::f816:3eff:fe20:57c4',
            'mac_address': fake_vm_port['mac_address']
        }]
        fake_vm_port['allowed_address_pairs'].extend(updated_allowed_pairs)

        fake_unbind_response = ('fake_stdout', '')
        mock_get_port_from_host.return_value = fake_vm_port
        mock_port_unbind.return_value = fake_unbind_response

        nested_driver = nested.NestedDriver()
        response = nested_driver.delete_host_iface(fake_endpoint_id,
                                                   fake_neutron_port)

        mock_get_port_from_host.assert_called_with('eth0')
        mock_update_port.assert_called_with(
            fake_vm_port['id'],
            {'port': {
                'allowed_address_pairs': updated_allowed_pairs
            }})
        mock_port_unbind.assert_called_with(fake_endpoint_id,
                                            fake_neutron_port)

        self.assertEqual(response, fake_unbind_response)
示例#22
0
    def test_join_bad_request(self):
        fake_docker_network_id = lib_utils.get_hash()
        invalid_docker_endpoint_id = 'id-should-be-hexdigits'
        fake_container_id = lib_utils.get_hash()

        response = self._invoke_join_request(
            fake_docker_network_id, invalid_docker_endpoint_id,
            fake_container_id)

        self.assertEqual(
            w_exceptions.BadRequest.code, response.status_code)
        decoded_json = jsonutils.loads(response.data)
        self.assertIn('Err', decoded_json)
        # TODO(tfukushima): Add the better error message validation.
        self.assertIn(invalid_docker_endpoint_id, decoded_json['Err'])
        self.assertIn('EndpointID', decoded_json['Err'])
    def test_remove_from_allowed_address_pairs_no_update(self, m_mac):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        m_driver = mock.Mock(spec=cls)
        self.useFixture(k_fix.MockNetworkClient()).client

        port_id = lib_utils.get_hash()
        vm_port = fake.get_port_obj(port_id)

        mac_addr = 'fa:16:3e:1b:30:00' if m_mac else vm_port['mac_address']
        address_pairs = [
            {
                'ip_address': '10.0.0.30',
                'mac_address': mac_addr
            },
            {
                'ip_address': 'fe80::f816:3eff:fe1c:36a9',
                'mac_address': mac_addr
            },
        ]
        vm_port['allowed_address_pairs'].extend(address_pairs)

        ip_addr = ['10.0.0.29']

        cls._remove_from_allowed_address_pairs(m_driver, vm_port,
                                               frozenset(ip_addr), m_mac)

        m_driver._update_port_address_pairs.assert_not_called()
示例#24
0
    def test_update_port_with_no_changes(self, mock_get_port_name,
                                         mock_update_port):
        fake_endpoint_id = lib_utils.get_hash()
        fake_neutron_port_id = uuidutils.generate_uuid()
        fake_neutron_net_id = uuidutils.generate_uuid()
        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
        fake_neutron_port = self._get_fake_port(
            fake_endpoint_id,
            fake_neutron_net_id,
            fake_neutron_port_id,
            lib_const.PORT_STATUS_ACTIVE,
            fake_neutron_v4_subnet_id,
            fake_neutron_v6_subnet_id,
            '192.168.1.3',
            'fe80::f816:3eff:fe1c:36a9',
            binding_host=lib_utils.get_hostname())['port']
        fake_port_name = '-'.join([fake_endpoint_id, lib_utils.PORT_POSTFIX])
        mock_get_port_name.return_value = fake_port_name

        veth_driver = veth.VethDriver()
        veth_driver.update_port(fake_neutron_port, fake_endpoint_id, '')

        mock_get_port_name.assert_called_with(fake_endpoint_id)
        mock_update_port.assert_not_called()
    def test_remove_from_allowed_address_pairs_missing(self, m_mac):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        m_driver = mock.Mock(spec=cls)
        neutron = self.useFixture(k_fix.MockNeutronClient()).client

        port_id = lib_utils.get_hash()
        vm_port = self._get_fake_port(port_id)['port']

        mac_addr = 'fa:16:3e:1b:30:00' if m_mac else vm_port['mac_address']
        address_pairs = [
            {
                'ip_address': '10.0.0.30',
                'mac_address': mac_addr
            },
            {
                'ip_address': 'fe80::f816:3eff:fe1c:36a9',
                'mac_address': mac_addr
            },
        ]
        mac_addr = m_mac if m_mac else vm_port['mac_address']
        vm_port['allowed_address_pairs'].extend(address_pairs)
        vm_port['allowed_address_pairs'].append({
            'ip_address': '10.0.0.28',
            'mac_address': mac_addr
        })
        ip_addr = ['10.0.0.29', '10.0.0.28']

        cls._remove_from_allowed_address_pairs(m_driver, neutron, vm_port,
                                               frozenset(ip_addr), m_mac)

        m_driver._update_port_address_pairs.assert_called_once_with(
            neutron, port_id, address_pairs)
    def test_release_vif_parent_not_found(self):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        m_driver = mock.Mock(spec=cls)
        os_net = self.useFixture(k_fix.MockNetworkClient()).client

        port_id = lib_utils.get_hash()
        pod = mock.sentinel.pod
        vif = mock.Mock()
        vif.id = port_id

        container_mac = mock.sentinel.mac_address
        container_ip = mock.sentinel.ip_address
        container_port = fake.get_port_obj(port_id=port_id,
                                           ip_address=container_ip,
                                           mac_address=container_mac)
        os_net.get_port.return_value = container_port

        m_driver.lock = mock.MagicMock(spec=threading.Lock())
        m_driver._get_parent_port.side_effect = o_exc.SDKException

        self.assertRaises(o_exc.SDKException, cls.release_vif, m_driver, pod,
                          vif)
        os_net.get_port.assert_called_with(port_id)
        self.assertEqual(os_net.get_port.call_count, 1)
        m_driver._get_parent_port.assert_called_with(pod)
        self.assertEqual(m_driver._get_parent_port.call_count, 1)
        m_driver._remove_from_allowed_address_pairs.assert_not_called()
        os_net.delete_port.assert_not_called()
示例#27
0
    def test_create_host_iface(self, mock_port_bind, mock_listdir,
                               mock_iglob, mock_readlink):
        sriov_driver = sriov.SriovDriver()
        fake_endpoint_id = lib_utils.get_hash()
        fake_neutron_port_id = uuidutils.generate_uuid()
        fake_neutron_port = self._get_fake_port(
            fake_endpoint_id, uuidutils.generate_uuid(),
            fake_neutron_port_id,
            binding_profile={'pci_slot': '0000:0a:00.1'})['port']
        fake_subnets = mock.sentinel.binding_subnets
        fake_pf_ifname = 'eth3'
        mock_listdir.return_value = [fake_pf_ifname]
        mock_iglob.return_value = [
            '/sys/bus/pci/devices/0000:0a:00.1/physfn/virtfn3',
        ]
        mock_readlink.return_value = '../../0000:0a:00.1'
        fake_exec_response = ('fake_stdout', '')
        mock_port_bind.return_value = ('fake_host_ifname',
            'fake_container_ifname', fake_exec_response)

        response = sriov_driver.create_host_iface(fake_endpoint_id,
            fake_neutron_port, fake_subnets)
        self.assertEqual(response, fake_exec_response)
        mock_port_bind.assert_called_with(fake_endpoint_id,
            fake_neutron_port, fake_subnets,
            pf_ifname=fake_pf_ifname, vf_num='3',
            driver='kuryr.lib.binding.drivers.hw_veb')
        mock_listdir.assert_called_with(
            '/sys/bus/pci/devices/0000:0a:00.1/physfn/net')
        mock_iglob.assert_called_with(
            '/sys/bus/pci/devices/0000:0a:00.1/physfn/virtfn*')
        mock_readlink.assert_called_with(
            '/sys/bus/pci/devices/0000:0a:00.1/physfn/virtfn3')
示例#28
0
    def test_update_port_with_mac_address(self, mock_get_port_name,
                                          mock_update_port):
        fake_endpoint_id = lib_utils.get_hash()
        fake_neutron_port_id = uuidutils.generate_uuid()
        fake_neutron_net_id = uuidutils.generate_uuid()
        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
        fake_mac_address1 = 'fa:16:3e:20:57:c3'
        fake_mac_address2 = 'fa:16:3e:20:57:c4'
        fake_neutron_port = self._get_fake_port(
            fake_endpoint_id, fake_neutron_net_id,
            fake_neutron_port_id, lib_const.PORT_STATUS_ACTIVE,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id,
            '192.168.1.3', 'fe80::f816:3eff:fe1c:36a9',
            fake_mac_address1,
            admin_state_up=False, binding_host='')['port']
        fake_port_name = '-'.join([fake_endpoint_id, lib_utils.PORT_POSTFIX])
        mock_get_port_name.return_value = fake_port_name

        veth_driver = veth.VethDriver()
        veth_driver.update_port(fake_neutron_port, fake_endpoint_id,
                                fake_mac_address2)

        mock_get_port_name.assert_called_with(fake_endpoint_id)
        expected_update_port = {
            'port': {
                'device_owner': lib_const.DEVICE_OWNER,
                'binding:host_id': lib_utils.get_hostname(),
                'mac_address': fake_mac_address2,
                'admin_state_up': True,
            }
        }
        mock_update_port.assert_called_with(fake_neutron_port_id,
                                            expected_update_port)
    def _get_fake_port(self, port_id=None, ip_address=None, mac_address=None):
        fake_port = {
            'port': {
                "mac_address": "fa:16:3e:20:57:c4",
                "fixed_ips": [],
                "id": "07b21ebf-b105-4720-9f2e-95670c4032e4",
                "allowed_address_pairs": []
            }
        }

        if port_id:
            fake_port['port']['id'] = port_id

        if ip_address:
            fake_port['port']['fixed_ips'].append({
                "subnet_id":
                lib_utils.get_hash(),
                "ip_address":
                ip_address
            })

        if mac_address:
            fake_port['port']['mac_address'] = mac_address

        return fake_port
    def test_release_vif_delete_failed(self):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        m_driver = mock.Mock(spec=cls)
        os_net = self.useFixture(k_fix.MockNetworkClient()).client

        port_id = lib_utils.get_hash()
        pod = mock.sentinel.pod
        vif = mock.Mock()
        vif.id = port_id

        container_mac = mock.sentinel.mac_address
        container_ip = mock.sentinel.ip_addresses
        container_port = fake.get_port_obj(port_id=port_id,
                                           ip_address=container_ip,
                                           mac_address=container_mac)
        os_net.get_port.return_value = container_port
        os_net.delete_port.side_effect = o_exc.NotFoundException

        vm_port = fake.get_port_obj()
        m_driver._get_parent_port.return_value = vm_port
        m_driver._try_update_port.return_value = 0
        m_driver.lock = mock.MagicMock(spec=threading.Lock())

        cls.release_vif(m_driver, pod, vif)

        os_net.get_port.assert_called_once_with(port_id)
        m_driver._get_parent_port.assert_called_once_with(pod)
        m_driver._try_update_port.assert_called_once()
        os_net.delete_port.assert_called_once_with(vif.id,
                                                   ignore_missing=False)
    def test_release_vif_delete_failed(self):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        m_driver = mock.Mock(spec=cls)
        neutron = self.useFixture(k_fix.MockNeutronClient()).client

        port_id = lib_utils.get_hash()
        pod = mock.sentinel.pod
        vif = mock.Mock()
        vif.id = port_id

        container_mac = mock.sentinel.mac_address
        container_ip = mock.sentinel.ip_addresses
        container_port = self._get_fake_port(port_id, container_ip,
                                             container_mac)
        neutron.show_port.return_value = container_port
        neutron.delete_port.side_effect = n_exc.PortNotFoundClient

        vm_port = self._get_fake_port()
        m_driver._get_parent_port.return_value = vm_port
        m_driver.lock = mock.MagicMock(spec=threading.Lock())

        cls.release_vif(m_driver, pod, vif)

        neutron.show_port.assert_called_once_with(port_id)
        m_driver._get_parent_port.assert_called_once_with(neutron, pod)
        m_driver._remove_from_allowed_address_pairs.assert_called_once_with(
            neutron, vm_port, frozenset([container_ip]), container_mac)
        neutron.delete_port.assert_called_once_with(vif.id)
    def test_add_to_allowed_address_pairs_same_ip(self):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        m_driver = mock.Mock(spec=cls)
        self.useFixture(k_fix.MockNetworkClient()).client

        port_id = lib_utils.get_hash()
        vm_port = fake.get_port_obj(port_id)
        address_pairs = [
            {
                'ip_address': '10.0.0.30',
                'mac_address': 'fa:16:3e:1b:30:00'
            },
            {
                'ip_address': 'fe80::f816:3eff:fe1c:36a9',
                'mac_address': 'fa:16:3e:1b:30:00'
            },
        ]
        vm_port['allowed_address_pairs'].extend(address_pairs)

        mac_addr = 'fa:16:3e:71:cb:80'
        ip_addr = '10.0.0.30'
        address_pairs.append({'ip_address': ip_addr, 'mac_address': mac_addr})

        cls._add_to_allowed_address_pairs(m_driver, vm_port,
                                          frozenset([ip_addr]), mac_addr)

        m_driver._update_port_address_pairs.assert_called_once_with(
            port_id, address_pairs, revision_number=9)
示例#33
0
 def test_port_unbind(self, mock_execute, mock_remove_device):
     fake_docker_endpoint_id = utils.get_hash()
     fake_docker_network_id = utils.get_hash()
     fake_port_id = uuidutils.generate_uuid()
     fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
     fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
     fake_vif_details = {"port_filter": True, "ovs_hybrid_plug": False}
     fake_vif_type = "ovs"
     fake_port = self._get_fake_port(
         fake_docker_endpoint_id, fake_docker_network_id,
         fake_port_id, constants.PORT_STATUS_ACTIVE,
         fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id,
         vif_details=fake_vif_details, vif_type=fake_vif_type)
     veth.port_unbind(fake_docker_endpoint_id, fake_port['port'])
     mock_execute.assert_called_once()
     mock_remove_device.assert_called_once()
    def test_add_to_allowed_address_pairs_already_present(self):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        m_driver = mock.Mock(spec=cls)
        self.useFixture(k_fix.MockNetworkClient()).client

        port_id = lib_utils.get_hash()
        vm_port = fake.get_port_obj(port_id)
        address_pairs = [
            {
                'ip_address': '10.0.0.30',
                'mac_address': 'fa:16:3e:1b:30:00'
            },
            {
                'ip_address': 'fe80::f816:3eff:fe1c:36a9',
                'mac_address': 'fa:16:3e:1b:30:00'
            },
        ]
        vm_port['allowed_address_pairs'].extend(address_pairs)

        mac_addr = 'fa:16:3e:1b:30:00'
        ip_addr = '10.0.0.30'

        self.assertRaises(k_exc.AllowedAddressAlreadyPresent,
                          cls._add_to_allowed_address_pairs, m_driver, vm_port,
                          frozenset([ip_addr]), mac_addr)
示例#35
0
    def test_create_host_iface_vf_num_not_found(
            self, mock_port_bind, mock_listdir, mock_iglob, mock_readlink):
        sriov_driver = sriov.SriovDriver()
        fake_endpoint_id = lib_utils.get_hash()
        fake_neutron_port_id = uuidutils.generate_uuid()
        fake_neutron_port = self._get_fake_port(
            fake_endpoint_id, uuidutils.generate_uuid(),
            fake_neutron_port_id,
            binding_profile={'pci_slot': '0000:0a:00.1'})['port']
        fake_subnets = mock.sentinel.binding_subnets
        fake_pf_ifname = 'eth3'
        mock_listdir.return_value = [fake_pf_ifname]
        mock_iglob.return_value = [
            '/sys/bus/pci/devices/0000:0a:00.1/physfn/virtfn3',
        ]
        mock_readlink.return_value = '../../0000:0a:00.2'

        self.assertRaises(exceptions.KuryrException,
                          sriov_driver.create_host_iface,
                          fake_endpoint_id, fake_neutron_port, fake_subnets)
        mock_port_bind.assert_not_called()
        mock_listdir.assert_called_with(
            '/sys/bus/pci/devices/0000:0a:00.1/physfn/net')
        mock_iglob.assert_called_with(
            '/sys/bus/pci/devices/0000:0a:00.1/physfn/virtfn*')
        mock_readlink.assert_called_with(
            '/sys/bus/pci/devices/0000:0a:00.1/physfn/virtfn3')
    def test_add_to_allowed_address_pairs(self, m_mac):
        cls = nested_macvlan_vif.NestedMacvlanPodVIFDriver
        m_driver = mock.Mock(spec=cls)
        self.useFixture(k_fix.MockNeutronClient()).client

        port_id = lib_utils.get_hash()
        vm_port = self._get_fake_port(port_id)['port']

        mac_addr = 'fa:16:3e:1b:30:00' if m_mac else vm_port['mac_address']
        address_pairs = [
            {
                'ip_address': '10.0.0.30',
                'mac_address': mac_addr
            },
            {
                'ip_address': 'fe80::f816:3eff:fe1c:36a9',
                'mac_address': mac_addr
            },
        ]
        vm_port['allowed_address_pairs'].extend(address_pairs)

        ip_addr = '10.0.0.29'
        address_pairs.append({
            'ip_address':
            ip_addr,
            'mac_address':
            m_mac if m_mac else vm_port['mac_address']
        })

        cls._add_to_allowed_address_pairs(m_driver, vm_port,
                                          frozenset([ip_addr]), m_mac)

        m_driver._update_port_address_pairs.assert_called_once_with(
            port_id, address_pairs, revision_number=1)
示例#37
0
    def test_create_host_iface(
            self, mock_get_port_from_host,
            mock_update_port, mock_port_bind, mock_conf):
        mock_conf.binding.link_iface = 'eth0'

        fake_endpoint_id = lib_utils.get_hash()
        fake_neutron_port_id = uuidutils.generate_uuid()
        fake_neutron_net_id = uuidutils.generate_uuid()
        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
        fake_vm_port_id = uuidutils.generate_uuid()

        fake_neutron_port = self._get_fake_port(
            fake_endpoint_id, fake_neutron_net_id,
            fake_neutron_port_id, lib_const.PORT_STATUS_ACTIVE,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id,
            '192.168.1.3', 'fe80::f816:3eff:fe1c:36a9')['port']
        fake_neutron_port['mac_address'] = 'fa:16:3e:20:57:c3'
        fake_vm_port = self._get_fake_port(
            fake_endpoint_id, fake_neutron_net_id,
            fake_vm_port_id, lib_const.PORT_STATUS_ACTIVE,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id,
            '192.168.1.2', 'fe80::f816:3eff:fe20:57c4')['port']
        fake_vm_port['allowed_address_pairs'] = [
            {'ip_address': '192.168.1.2',
             'mac_address': fake_vm_port['mac_address']},
            {'ip_address': 'fe80::f816:3eff:fe20:57c4',
             'mac_address': fake_vm_port['mac_address']}]
        updated_allowed_pairs = fake_vm_port['allowed_address_pairs']
        updated_allowed_pairs.extend([
            {'ip_address': '192.168.1.3',
             'mac_address': fake_neutron_port['mac_address']},
            {'ip_address': 'fe80::f816:3eff:fe1c:36a9',
             'mac_address': fake_neutron_port['mac_address']}])

        fake_subnets = self._get_fake_subnets(
            fake_endpoint_id, fake_neutron_net_id,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id)['subnets']

        fake_network = mock.sentinel.binding_network
        fake_exec_response = ('fake_stdout', '')
        mock_port_bind.return_value = ('fake_host_ifname',
            'fake_container_ifname', fake_exec_response)
        mock_get_port_from_host.return_value = fake_vm_port

        nested_driver = nested.NestedDriver()
        response = nested_driver.create_host_iface(fake_endpoint_id,
            fake_neutron_port, fake_subnets, fake_network)

        mock_get_port_from_host.assert_called_with('eth0')
        mock_port_bind.assert_called_with(fake_endpoint_id,
            fake_neutron_port, fake_subnets, fake_network, fake_vm_port)
        mock_update_port.assert_called_with(
            fake_vm_port['id'],
            {'port': {
                'allowed_address_pairs': updated_allowed_pairs
            }})

        self.assertEqual(response, fake_exec_response)
示例#38
0
    def test_delete_host_iface(self, mock_get_port_from_host,
                               mock_trunk_remove_subports, mock_port_unbind,
                               mock_release_seg_id, mock_vlan_check,
                               mock_conf):
        mock_vlan_check.return_value = None
        fake_endpoint_id = lib_utils.get_hash()
        fake_neutron_port_id = uuidutils.generate_uuid()
        fake_neutron_net_id = uuidutils.generate_uuid()
        fake_neutron_trunk_id = uuidutils.generate_uuid()
        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
        fake_vm_port_id = uuidutils.generate_uuid()

        fake_neutron_port = self._get_fake_port(
            fake_endpoint_id, fake_neutron_net_id, fake_neutron_port_id,
            lib_const.PORT_STATUS_ACTIVE, fake_neutron_v4_subnet_id,
            fake_neutron_v6_subnet_id, '192.168.1.3',
            'fe80::f816:3eff:fe1c:36a9')['port']
        fake_neutron_port['mac_address'] = 'fa:16:3e:20:57:c3'
        fake_vm_port = self._get_fake_port(
            fake_endpoint_id, fake_neutron_net_id, fake_vm_port_id,
            lib_const.PORT_STATUS_ACTIVE, fake_neutron_v4_subnet_id,
            fake_neutron_v6_subnet_id, '192.168.1.2',
            'fe80::f816:3eff:fe20:57c4', 'fa:16:3e:20:57:c3', None,
            fake_neutron_trunk_id)['port']
        fake_vm_port['allowed_address_pairs'] = [{
            'ip_address':
            '192.168.1.3',
            'mac_address':
            fake_neutron_port['mac_address']
        }, {
            'ip_address':
            'fe80::f816:3eff:fe1c:36a9',
            'mac_address':
            fake_neutron_port['mac_address']
        }]

        mock_conf.binding.link_iface = 'eth0'
        fake_unbind_response = ('fake_stdout', '')
        mock_get_port_from_host.return_value = fake_vm_port
        mock_port_unbind.return_value = fake_unbind_response

        vlan_driver = vlan.VlanDriver()

        response = vlan_driver.delete_host_iface(fake_endpoint_id,
                                                 fake_neutron_port)

        mock_get_port_from_host.assert_called_with(
            mock_conf.binding.link_iface)
        mock_port_unbind.assert_called_with(fake_endpoint_id,
                                            fake_neutron_port)
        mock_trunk_remove_subports.assert_called_with(
            fake_neutron_trunk_id,
            {'sub_ports': [{
                'port_id': fake_neutron_port_id
            }]})
        mock_release_seg_id.assert_called_with(fake_neutron_port_id)

        self.assertEqual(response, fake_unbind_response)
    def test_create_network_unauthorized(self, mock_list_subnetpools,
            mock_get_default_network, mock_create_network):
        docker_network_id = lib_utils.get_hash()
        network_request = {
            'NetworkID': docker_network_id,
            'IPv4Data': [{
                'AddressSpace': 'foo',
                'Pool': '192.168.42.0/24',
                'Gateway': '192.168.42.1/24',
                'AuxAddresses': {}
            }],
            'IPv6Data': [{
                'AddressSpace': 'bar',
                'Pool': 'fe80::/64',
                'Gateway': 'fe80::f816:3eff:fe20:57c3/64',
                'AuxAddresses': {}
            }],
            'Options': {}
        }

        fake_kuryr_v4_subnetpool_id = uuidutils.generate_uuid()
        fake_v4_pool_name = lib_utils.get_neutron_subnetpool_name(
            network_request['IPv4Data'][0]['Pool'])
        kuryr_v4_subnetpools = self._get_fake_v4_subnetpools(
            fake_kuryr_v4_subnetpool_id, name=fake_v4_pool_name)

        fake_kuryr_v6_subnetpool_id = uuidutils.generate_uuid()
        fake_v6_pool_name = lib_utils.get_neutron_subnetpool_name(
            network_request['IPv6Data'][0]['Pool'])
        kuryr_v6_subnetpools = self._get_fake_v6_subnetpools(
            fake_kuryr_v6_subnetpool_id, name=fake_v6_pool_name)
        fake_v4_pool_attrs = {'name': fake_v4_pool_name}
        fake_v6_pool_attrs = {'name': fake_v6_pool_name}
        mock_list_subnetpools.side_effect = [
            {'subnetpools': kuryr_v4_subnetpools['subnetpools']},
            {'subnetpools': kuryr_v6_subnetpools['subnetpools']}
        ]

        fake_request = {
            "network": {
                "name": utils.make_net_name(docker_network_id),
                "admin_state_up": True,
                "shared": False
            }
        }
        mock_create_network.side_effect = exceptions.Unauthorized
        response = self._invoke_create_request(network_request)
        self.assertEqual(401, response.status_code)
        decoded_json = jsonutils.loads(response.data)
        mock_list_subnetpools.assert_any_call(**fake_v4_pool_attrs)
        mock_list_subnetpools.assert_any_call(**fake_v6_pool_attrs)
        mock_create_network.assert_called_with(fake_request)
        self.assertIn('Err', decoded_json)
        self.assertEqual(
            {'Err': exceptions.Unauthorized.message}, decoded_json)
示例#40
0
    def test_update_port(self, mock_get_seg_id, mock_attach_subport,
                         mock_get_port_name, mock_update_port,
                         mock_get_port_from_host, mock_vlan_check):
        fake_endpoint_id = lib_utils.get_hash()
        fake_neutron_port_id = uuidutils.generate_uuid()
        fake_neutron_net_id = uuidutils.generate_uuid()
        fake_neutron_trunk_id = uuidutils.generate_uuid()
        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
        fake_vm_port_id = uuidutils.generate_uuid()

        fake_neutron_mac_address1 = 'fa:16:3e:20:57:c3'
        fake_neutron_mac_address2 = 'fa:16:3e:20:57:c4'
        fake_vm_mac_address = 'fa:16:3e:20:57:c5'
        fake_neutron_port = self._get_fake_port(
            fake_endpoint_id, fake_neutron_net_id,
            fake_neutron_port_id, lib_const.PORT_STATUS_ACTIVE,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id,
            '192.168.1.3', 'fe80::f816:3eff:fe1c:36a9',
            fake_neutron_mac_address1,
            binding_host='', admin_state_up=False)['port']
        fake_vm_port = self._get_fake_port(
            fake_endpoint_id, fake_neutron_net_id,
            fake_vm_port_id, lib_const.PORT_STATUS_ACTIVE,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id,
            '192.168.1.2', 'fe80::f816:3eff:fe20:57c4', fake_vm_mac_address,
            None, fake_neutron_trunk_id)['port']
        fake_segmentation_id = 1
        fake_port_name = 'port1'

        mock_get_seg_id.return_value = fake_segmentation_id
        mock_get_port_name.return_value = fake_port_name
        mock_get_port_from_host.return_value = fake_vm_port
        mock_vlan_check.return_value = None

        vlan_driver = vlan.VlanDriver()

        vlan_driver.update_port(fake_neutron_port, fake_endpoint_id,
                                fake_neutron_mac_address2)

        mock_get_seg_id.assert_called_with(fake_neutron_port_id)

        mock_get_port_name.assert_called_with(fake_endpoint_id)

        mock_attach_subport.assert_called_with(fake_neutron_trunk_id,
                                               fake_neutron_port_id,
                                               fake_segmentation_id)

        mock_update_port.assert_called_with(fake_neutron_port_id,
                {'port': {
                    'device_owner': lib_const.DEVICE_OWNER,
                    'binding:host_id': lib_utils.get_hostname(),
                    'mac_address': fake_neutron_mac_address2,
                    'admin_state_up': True,
                }})
示例#41
0
    def test_create_host_iface(self, mock_get_port_from_host,
                               mock_segmentation_id,
                               mock_port_bind, mock_vlan_check, mock_conf):
        mock_vlan_check.return_value = None
        fake_endpoint_id = lib_utils.get_hash()
        fake_neutron_port_id = uuidutils.generate_uuid()
        fake_neutron_net_id = uuidutils.generate_uuid()
        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
        fake_vm_port_id = uuidutils.generate_uuid()

        fake_neutron_port = self._get_fake_port(
            fake_endpoint_id, fake_neutron_net_id,
            fake_neutron_port_id, lib_const.PORT_STATUS_ACTIVE,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id,
            '192.168.1.3', 'fe80::f816:3eff:fe1c:36a9')['port']
        fake_neutron_port['mac_address'] = 'fa:16:3e:20:57:c3'
        fake_vm_port = self._get_fake_port(
            fake_endpoint_id, fake_neutron_net_id,
            fake_vm_port_id, lib_const.PORT_STATUS_ACTIVE,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id,
            '192.168.1.2', 'fe80::f816:3eff:fe20:57c4')['port']
        fake_vm_port['allowed_address_pairs'] = [
            {'ip_address': '192.168.1.2',
             'mac_address': fake_vm_port['mac_address']},
            {'ip_address': 'fe80::f816:3eff:fe20:57c4',
             'mac_address': fake_vm_port['mac_address']}]

        fake_subnets = self._get_fake_subnets(
            fake_endpoint_id, fake_neutron_net_id,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id)['subnets']

        fake_network = mock.sentinel.binding_network
        mock_conf.binding.link_iface = 'eth0'
        fake_exec_response = ('fake_stdout', '')
        fake_segmentation_id = 1
        mock_port_bind.return_value = ('fake_host_ifname',
            'fake_container_ifname', fake_exec_response)
        mock_segmentation_id.return_value = fake_segmentation_id
        mock_get_port_from_host.return_value = fake_vm_port

        vlan_driver = vlan.VlanDriver()

        response = vlan_driver.create_host_iface(fake_endpoint_id,
            fake_neutron_port, fake_subnets, fake_network)

        mock_get_port_from_host.assert_called_with(
            mock_conf.binding.link_iface)
        mock_port_bind.assert_called_with(fake_endpoint_id,
            fake_neutron_port, fake_subnets, fake_network, fake_vm_port,
            fake_segmentation_id)
        mock_segmentation_id.assert_called_with(fake_neutron_port['id'])

        self.assertEqual(response, fake_exec_response)
示例#42
0
    def test_delete_host_iface(self, mock_port_unbind):
        veth_driver = veth.VethDriver()
        fake_endpoint_id = lib_utils.get_hash()
        fake_neutron_port = uuidutils.generate_uuid()
        fake_unbind_response = ('fake_stdout', '')
        mock_port_unbind.return_value = fake_unbind_response

        response = veth_driver.delete_host_iface(fake_endpoint_id,
                                                 fake_neutron_port)
        mock_port_unbind.assert_called_with(fake_endpoint_id,
                                            fake_neutron_port)
        self.assertEqual(response, fake_unbind_response)
示例#43
0
    def test_delete_host_iface(self, mock_get_port_from_host,
                               mock_trunk_remove_subports, mock_port_unbind,
                               mock_release_seg_id, mock_vlan_check,
                               mock_conf):
        mock_vlan_check.return_value = None
        fake_endpoint_id = lib_utils.get_hash()
        fake_neutron_port_id = uuidutils.generate_uuid()
        fake_neutron_net_id = uuidutils.generate_uuid()
        fake_neutron_trunk_id = uuidutils.generate_uuid()
        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
        fake_vm_port_id = uuidutils.generate_uuid()

        fake_neutron_port = self._get_fake_port(
            fake_endpoint_id, fake_neutron_net_id, fake_neutron_port_id,
            lib_const.PORT_STATUS_ACTIVE,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id,
            '192.168.1.3', 'fe80::f816:3eff:fe1c:36a9')['port']
        fake_neutron_port['mac_address'] = 'fa:16:3e:20:57:c3'
        fake_vm_port = self._get_fake_port(
            fake_endpoint_id, fake_neutron_net_id,
            fake_vm_port_id, lib_const.PORT_STATUS_ACTIVE,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id,
            '192.168.1.2', 'fe80::f816:3eff:fe20:57c4', 'fa:16:3e:20:57:c3',
            None, fake_neutron_trunk_id)['port']
        fake_vm_port['allowed_address_pairs'] = [
            {'ip_address': '192.168.1.3',
             'mac_address': fake_neutron_port['mac_address']},
            {'ip_address': 'fe80::f816:3eff:fe1c:36a9',
             'mac_address': fake_neutron_port['mac_address']}]

        mock_conf.binding.link_iface = 'eth0'
        fake_unbind_response = ('fake_stdout', '')
        mock_get_port_from_host.return_value = fake_vm_port
        mock_port_unbind.return_value = fake_unbind_response

        vlan_driver = vlan.VlanDriver()

        response = vlan_driver.delete_host_iface(fake_endpoint_id,
                                                 fake_neutron_port)

        mock_get_port_from_host.assert_called_with(
            mock_conf.binding.link_iface)
        mock_port_unbind.assert_called_with(fake_endpoint_id,
                                            fake_neutron_port)
        mock_trunk_remove_subports.assert_called_with(fake_neutron_trunk_id,
            {'sub_ports': [{
                'port_id': fake_neutron_port_id
            }]})
        mock_release_seg_id.assert_called_with(fake_neutron_port_id)

        self.assertEqual(response, fake_unbind_response)
示例#44
0
    def test_create_host_iface(self, mock_get_port_from_host):
        fake_endpoint_id = lib_utils.get_hash()
        fake_neutron_port_id = uuidutils.generate_uuid()
        fake_neutron_net_id = uuidutils.generate_uuid()

        fake_neutron_port = self._get_fake_port(
            fake_endpoint_id, fake_neutron_net_id,
            fake_neutron_port_id, lib_const.PORT_STATUS_ACTIVE)['port']

        nested_driver = nested.NestedDriver()
        self.assertRaises(exceptions.KuryrException,
            nested_driver.create_host_iface, fake_endpoint_id,
            fake_neutron_port, None)
    def test_delete_endpoint_delete_host_iface_failure(self, GivenException,
            mock_list_networks, mock_list_ports, mock_delete_host_iface):
        fake_docker_network_id = lib_utils.get_hash()
        fake_docker_endpoint_id = lib_utils.get_hash()

        fake_neutron_network_id = uuidutils.generate_uuid()
        fake_neutron_port_id = uuidutils.generate_uuid()
        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
        t = utils.make_net_tags(fake_docker_network_id)
        mock_list_networks.return_value = self._get_fake_list_network(
            fake_neutron_network_id)
        port_tags = utils.make_port_tags(fake_docker_endpoint_id)
        fake_neutron_ports_response = self._get_fake_ports(
            fake_docker_endpoint_id, fake_neutron_network_id,
            fake_neutron_port_id, lib_const.PORT_STATUS_ACTIVE,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id)
        mock_list_ports.return_value = fake_neutron_ports_response
        fake_neutron_port = fake_neutron_ports_response['ports'][0]

        fake_message = "fake message"
        if GivenException == n_exceptions.NeutronClientException:
            fake_exception = GivenException(fake_message, status_code=500)
        else:
            fake_exception = GivenException(fake_message)
        mock_delete_host_iface.side_effect = fake_exception
        response = self._invoke_delete_request(
            fake_docker_network_id, fake_docker_endpoint_id)

        self.assertEqual(
            w_exceptions.InternalServerError.code, response.status_code)
        mock_list_networks.assert_called_with(tags=t)
        mock_list_ports.assert_called_with(tags=port_tags)
        mock_delete_host_iface.assert_called_with(fake_docker_endpoint_id,
            fake_neutron_port)
        decoded_json = jsonutils.loads(response.data)
        self.assertIn('Err', decoded_json)
        self.assertIn(fake_message, decoded_json['Err'])
示例#46
0
    def test_get_default_network_id(self, mock_get_port_from_host, mock_conf):
        mock_conf.binding.link_iface = 'eth0'

        fake_endpoint_id = lib_utils.get_hash()
        fake_neutron_port_id = uuidutils.generate_uuid()
        fake_neutron_net_id = uuidutils.generate_uuid()
        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()

        fake_vm_port = self._get_fake_port(
            fake_endpoint_id, fake_neutron_net_id,
            fake_neutron_port_id, lib_const.PORT_STATUS_ACTIVE,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id)['port']
        mock_get_port_from_host.return_value = fake_vm_port

        nested_driver = nested.NestedDriver()
        host_network_id = nested_driver.get_default_network_id()
        mock_get_port_from_host.assert_called_with('eth0')
        self.assertEqual(host_network_id, fake_vm_port['network_id'])
 def _ids(self):
     docker_network_id = lib_utils.get_hash()
     fake_neutron_net_id = "4e8e5957-649f-477b-9e5b-f1f75b21c03c"
     fake_response = {
         'networks':
         [
             {
                 "status": "ACTIVE",
                 "subnets": [],
                 "admin_state_up": True,
                 "tenant_id": "9bacb3c5d39d41a79512987f338cf177",
                 "router:external": False,
                 "segments": [],
                 "shared": False,
                 "id": fake_neutron_net_id,
                 "tags": [],
             }
         ]
     }
     return docker_network_id, fake_neutron_net_id, fake_response
示例#48
0
    def test_create_host_iface(self, mock_port_bind):
        veth_driver = veth.VethDriver()
        fake_endpoint_id = lib_utils.get_hash()
        fake_neutron_port = uuidutils.generate_uuid()
        fake_neutron_net_id = uuidutils.generate_uuid()
        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()

        fake_subnets = self._get_fake_subnets(
            fake_endpoint_id, fake_neutron_net_id,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id)
        fake_network = mock.sentinel.binding_network
        fake_exec_response = ('fake_stdout', '')
        mock_port_bind.return_value = ('fake_host_ifname',
            'fake_container_ifname', fake_exec_response)

        response = veth_driver.create_host_iface(fake_endpoint_id,
            fake_neutron_port, fake_subnets, fake_network)
        mock_port_bind.assert_called_with(fake_endpoint_id,
            fake_neutron_port, fake_subnets, fake_network)
        self.assertEqual(response, fake_exec_response)
示例#49
0
    def test_update_port_with_no_changes(self, mock_get_port_name,
                                         mock_update_port):
        fake_endpoint_id = lib_utils.get_hash()
        fake_neutron_port_id = uuidutils.generate_uuid()
        fake_neutron_net_id = uuidutils.generate_uuid()
        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
        fake_neutron_port = self._get_fake_port(
            fake_endpoint_id, fake_neutron_net_id,
            fake_neutron_port_id, lib_const.PORT_STATUS_ACTIVE,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id,
            '192.168.1.3', 'fe80::f816:3eff:fe1c:36a9',
            binding_host=lib_utils.get_hostname())['port']
        fake_port_name = '-'.join([fake_endpoint_id, lib_utils.PORT_POSTFIX])
        mock_get_port_name.return_value = fake_port_name

        veth_driver = veth.VethDriver()
        veth_driver.update_port(fake_neutron_port, fake_endpoint_id, '')

        mock_get_port_name.assert_called_with(fake_endpoint_id)
        mock_update_port.assert_not_called()
示例#50
0
 def test_get_port_name(self):
     fake_docker_endpoint_id = lib_utils.get_hash()
     generated_neutron_port_name = utils.get_neutron_port_name(
         fake_docker_endpoint_id)
     self.assertIn(lib_utils.PORT_POSTFIX, generated_neutron_port_name)
     self.assertIn(fake_docker_endpoint_id, generated_neutron_port_name)
    def test_network_driver_program_external_connectivity(self, existing_sg,
            num_ports, mock_list_ports, mock_create_security_group,
            mock_create_security_group_rule, mock_show_port,
            mock_update_port):
        config.CONF.set_override('process_external_connectivity', True)
        fake_docker_net_id = lib_utils.get_hash()
        fake_docker_endpoint_id = lib_utils.get_hash()

        fake_neutron_net_id = uuidutils.generate_uuid()
        fake_neutron_port_id = uuidutils.generate_uuid()
        neutron_port_name = utils.get_neutron_port_name(
            fake_docker_endpoint_id)
        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
        fake_neutron_ports_response = self._get_fake_ports(
            fake_docker_endpoint_id, fake_neutron_net_id,
            fake_neutron_port_id, lib_const.PORT_STATUS_ACTIVE,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id)
        if existing_sg:
            fake_neutron_existing_sec_group_id = uuidutils.generate_uuid()
            fake_neutron_ports_response['ports'][0]['security_groups'] = [
                fake_neutron_existing_sec_group_id]

        mock_list_ports.return_value = fake_neutron_ports_response

        sec_group = {
            'name': utils.get_sg_expose_name(fake_neutron_port_id),
            'description': 'Docker exposed ports created by Kuryr.'
        }
        fake_neutron_sec_group_id = lib_utils.get_hash()
        fake_neutron_sec_group_response = {'security_group':
                                           {'id': fake_neutron_sec_group_id}}
        mock_create_security_group.return_value = (
            fake_neutron_sec_group_response)

        proto_port_dict = defaultdict(list)
        for i in range(num_ports):
            proto_port_dict[constants.PROTOCOLS[PROTOCOL_TCP]].append(PORT + i)
            proto_port_dict[constants.PROTOCOLS[PROTOCOL_UDP]].append(PORT + i)
        proto_port_dict[constants.PROTOCOLS[PROTOCOL_UDP]].append(SINGLE_PORT)

        for proto, port_list in proto_port_dict.items():
            for key, group in groupby(enumerate(sorted(port_list)),
                                      lambda ix: ix[0] - ix[1]):
                port_range_list = list(map(itemgetter(1), group))

                port_range_min = min(port_range_list)
                port_range_max = max(port_range_list)
                sec_group_rule = {
                    'security_group_id': fake_neutron_sec_group_id,
                    'direction': 'ingress',
                    'port_range_min': port_range_min,
                    'port_range_max': port_range_max,
                    'protocol': proto
                }

        sgs = [fake_neutron_sec_group_id]
        if existing_sg:
            sgs.append(fake_neutron_existing_sec_group_id)
        mock_show_port.return_value = {'port':
            fake_neutron_ports_response['ports'][0]}

        port_opt = []
        for i in range(num_ports):
            port_opt.append({u'Port': PORT + i, u'Proto': PROTOCOL_TCP})
            port_opt.append({u'Port': PORT + i, u'Proto': PROTOCOL_UDP})
        port_opt.append({u'Port': SINGLE_PORT, u'Proto': PROTOCOL_UDP})
        options = {'com.docker.network.endpoint.exposedports':
                   port_opt,
                   'com.docker.network.portmap':
                   []}
        data = {
            'NetworkID': fake_docker_net_id,
            'EndpointID': fake_docker_endpoint_id,
            'Options': options,
        }
        response = self.app.post('/NetworkDriver.ProgramExternalConnectivity',
                                 content_type='application/json',
                                 data=jsonutils.dumps(data))

        self.assertEqual(200, response.status_code)
        mock_update_port.assert_called_with(fake_neutron_port_id,
                                {'port': {'security_groups': sgs}})
        mock_show_port.assert_called_with(fake_neutron_port_id)
        mock_create_security_group_rule.assert_called_with(
            {'security_group_rule': sec_group_rule})
        mock_create_security_group.assert_called_with(
            {'security_group': sec_group})
        mock_list_ports.assert_called_with(name=neutron_port_name)
        decoded_json = jsonutils.loads(response.data)
        self.assertEqual(constants.SCHEMA['SUCCESS'], decoded_json)
    def test_network_driver_revoke_external_connectivity(self, existing_sg,
            removing_sg, mock_list_ports, mock_list_security_groups,
            mock_delete_security_groups, mock_show_port,
            mock_update_port):
        config.CONF.set_override('process_external_connectivity', True)
        fake_docker_net_id = lib_utils.get_hash()
        fake_docker_endpoint_id = lib_utils.get_hash()

        fake_neutron_net_id = uuidutils.generate_uuid()
        fake_neutron_port_id = uuidutils.generate_uuid()
        fake_neutron_sec_group_id = lib_utils.get_hash()
        neutron_port_name = utils.get_neutron_port_name(
            fake_docker_endpoint_id)
        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
        fake_neutron_ports_response = self._get_fake_ports(
            fake_docker_endpoint_id, fake_neutron_net_id,
            fake_neutron_port_id, lib_const.PORT_STATUS_ACTIVE,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id)
        if existing_sg:
            fake_neutron_existing_sec_group_id = uuidutils.generate_uuid()
            fake_neutron_ports_response['ports'][0]['security_groups'] = [
                fake_neutron_sec_group_id, fake_neutron_existing_sec_group_id]
        else:
            fake_neutron_ports_response['ports'][0]['security_groups'] = [
                fake_neutron_sec_group_id]
        if removing_sg:
            fake_neutron_sec_group_response = {
                'security_groups': [{'id': fake_neutron_sec_group_id}]}
        else:
            fake_neutron_sec_group_response = {
                'security_groups': []}

        mock_list_ports.return_value = fake_neutron_ports_response
        mock_list_security_groups.return_value = (
            fake_neutron_sec_group_response)
        mock_show_port.return_value = {'port':
            fake_neutron_ports_response['ports'][0]}

        if existing_sg:
            sgs = [fake_neutron_existing_sec_group_id]
        else:
            sgs = []
        data = {
            'NetworkID': fake_docker_net_id,
            'EndpointID': fake_docker_endpoint_id,
        }
        response = self.app.post('/NetworkDriver.RevokeExternalConnectivity',
                                 content_type='application/json',
                                 data=jsonutils.dumps(data))

        self.assertEqual(200, response.status_code)
        mock_list_ports.assert_called_with(name=neutron_port_name)
        if removing_sg:
            mock_list_security_groups.assert_called_with(
                name=utils.get_sg_expose_name(fake_neutron_port_id))
            mock_delete_security_groups.assert_called_with(
                fake_neutron_sec_group_id)
            mock_show_port.assert_called_with(fake_neutron_port_id)
            mock_update_port.assert_called_with(fake_neutron_port_id,
                            {'port': {'security_groups': sgs}})
        else:
            mock_list_security_groups.assert_called_with(
                name=utils.get_sg_expose_name(fake_neutron_port_id))
            mock_delete_security_groups.assert_not_called()
            mock_show_port.assert_not_called()
            mock_update_port.assert_not_called()
        decoded_json = jsonutils.loads(response.data)
        self.assertEqual(constants.SCHEMA['SUCCESS'], decoded_json)
    def test_create_host_iface_failures(self, GivenException,
            mock_list_networks, mock_list_ports, mock_list_subnets,
            mock_update_port, mock_create_host_iface):
        fake_docker_network_id = lib_utils.get_hash()
        fake_docker_endpoint_id = lib_utils.get_hash()
        fake_neutron_network_id = uuidutils.generate_uuid()

        fake_neutron_network = self._get_fake_list_network(
            fake_neutron_network_id)
        t = utils.make_net_tags(fake_docker_network_id)
        mock_list_networks.return_value = fake_neutron_network

        fake_neutron_v4_subnet_id = uuidutils.generate_uuid()
        fake_neutron_v6_subnet_id = uuidutils.generate_uuid()
        fake_v4_subnet = self._get_fake_v4_subnet(fake_docker_network_id,
                                                  fake_docker_endpoint_id,
                                                  fake_neutron_v4_subnet_id)
        fake_v6_subnet = self._get_fake_v6_subnet(fake_docker_network_id,
                                                  fake_docker_endpoint_id,
                                                  fake_neutron_v6_subnet_id)
        fake_v4_subnet_response = {
            "subnets": [
                fake_v4_subnet['subnet']
            ]
        }
        fake_v6_subnet_response = {
            "subnets": [
                fake_v6_subnet['subnet']
            ]
        }

        def fake_subnet_response(network_id, cidr):
            if cidr == '192.168.1.0/24':
                return fake_v4_subnet_response
            elif cidr == 'fe80::/64':
                return fake_v6_subnet_response
            else:
                return {'subnets': []}

        mock_list_subnets.side_effect = fake_subnet_response

        fake_neutron_port_id = uuidutils.generate_uuid()
        fake_fixed_ips = ['subnet_id=%s' % fake_neutron_v4_subnet_id,
                          'ip_address=192.168.1.2',
                          'subnet_id=%s' % fake_neutron_v6_subnet_id,
                          'ip_address=fe80::f816:3eff:fe20:57c4']
        fake_port_response = self._get_fake_port(
            fake_docker_endpoint_id, fake_neutron_network_id,
            fake_neutron_port_id, lib_const.PORT_STATUS_ACTIVE,
            fake_neutron_v4_subnet_id, fake_neutron_v6_subnet_id,
            tags=utils.create_port_tags(fake_docker_endpoint_id))
        fake_ports_response = {
            "ports": [
                fake_port_response['port']
            ]
        }
        mock_list_ports.return_value = fake_ports_response
        fake_updated_port = fake_port_response['port']
        fake_updated_port['name'] = utils.get_neutron_port_name(
            fake_docker_endpoint_id)
        mock_update_port.return_value = fake_port_response['port']

        fake_neutron_subnets = [fake_v4_subnet['subnet'],
                                fake_v6_subnet['subnet']]

        fake_message = "fake message"
        if GivenException == n_exceptions.NeutronClientException:
            fake_exception = GivenException(fake_message, status_code=500)
        else:
            fake_exception = GivenException(fake_message)
        mock_create_host_iface.side_effect = fake_exception

        response = self._invoke_create_request(
            fake_docker_network_id, fake_docker_endpoint_id)

        self.assertEqual(
            w_exceptions.InternalServerError.code, response.status_code)
        mock_list_networks.assert_called_with(tags=t)
        expect_calls = [mock.call(cidr='192.168.1.0/24',
            network_id=fake_neutron_network_id),
            mock.call(cidr='fe80::/64', network_id=fake_neutron_network_id)]
        mock_list_subnets.assert_has_calls(expect_calls, any_order=True)
        mock_list_ports.assert_called_with(fixed_ips=fake_fixed_ips)
        mock_update_port.assert_called_with(fake_port_response['port'],
                                            fake_docker_endpoint_id,
                                            "fa:16:3e:20:57:c3",
                                            tags=True)
        mock_create_host_iface.assert_called_with(
            fake_docker_endpoint_id, fake_updated_port, fake_neutron_subnets,
            fake_neutron_network['networks'][0])

        decoded_json = jsonutils.loads(response.data)
        self.assertIn('Err', decoded_json)
        self.assertIn(fake_message, decoded_json['Err'])
    def test_create_endpoint_port_failures(self, GivenException,
            mock_list_ports, mock_list_subnets, mock_list_networks,
            mock_create_port):
        fake_docker_network_id = lib_utils.get_hash()
        fake_docker_endpoint_id = lib_utils.get_hash()
        fake_neutron_network_id = uuidutils.generate_uuid()
        fake_neutron_subnet_v4_id = uuidutils.generate_uuid()
        fake_neutron_subnet_v6_id = uuidutils.generate_uuid()

        fake_v4_subnet = self._get_fake_v4_subnet(
            fake_neutron_network_id,
            fake_docker_endpoint_id,
            fake_neutron_subnet_v4_id)
        fake_v6_subnet = self._get_fake_v6_subnet(
            fake_neutron_network_id,
            fake_docker_endpoint_id,
            fake_neutron_subnet_v6_id)
        fake_v4_subnet_response = {
            "subnets": [
                fake_v4_subnet['subnet']
            ]
        }
        fake_v6_subnet_response = {
            "subnets": [
                fake_v6_subnet['subnet']
            ]
        }

        fake_fixed_ips = ['subnet_id=%s' % fake_neutron_subnet_v4_id,
                          'ip_address=192.168.1.2',
                          'subnet_id=%s' % fake_neutron_subnet_v6_id,
                          'ip_address=fe80::f816:3eff:fe20:57c4']
        fake_port_response = {"ports": []}
        t = utils.make_net_tags(fake_docker_network_id)
        fake_neutron_network = self._get_fake_list_network(
            fake_neutron_network_id)
        mock_list_ports.return_value = fake_port_response

        def mock_fake_subnet(*args, **kwargs):
            if kwargs['cidr'] == '192.168.1.0/24':
                return fake_v4_subnet_response
            elif kwargs['cidr'] == 'fe80::/64':
                return fake_v6_subnet_response
        mock_list_subnets.side_effect = mock_fake_subnet
        mock_list_networks.return_value = fake_neutron_network
        mock_create_port.side_effect = GivenException
        fake_port_request = self._get_fake_port_request(
            fake_neutron_network_id, fake_docker_endpoint_id,
            fake_neutron_subnet_v4_id, fake_neutron_subnet_v6_id)

        response = self._invoke_create_request(
            fake_docker_network_id, fake_docker_endpoint_id)

        self.assertEqual(GivenException.status_code, response.status_code)
        mock_list_networks.assert_called_with(tags=t)
        mock_create_port.assert_called_with(fake_port_request)
        mock_list_subnets.assert_any_call(
            network_id=fake_neutron_network_id, cidr='192.168.1.0/24')
        mock_list_subnets.assert_any_call(
            network_id=fake_neutron_network_id, cidr='fe80::/64')
        mock_list_ports.assert_called_with(
            fixed_ips=fake_fixed_ips)
        decoded_json = jsonutils.loads(response.data)
        self.assertIn('Err', decoded_json)
        self.assertEqual({'Err': GivenException.message}, decoded_json)