def test_create_delete_network_with_kuryr_driver(self): """Create and Delete docker network with Kuryr This method creates a docker network with Kuryr driver and tests it was created in Neutron. It then deletes the docker network and tests that it was deleted from Neutron. """ fake_ipam = { "Driver": "kuryr", "Options": {}, "Config": [ { "Subnet": "10.0.0.0/16", "IPRange": "10.0.0.0/24", "Gateway": "10.0.0.1" } ] } net_name = lib_utils.get_random_string(8) res = self.docker_client.create_network(name=net_name, driver='kuryr', ipam=fake_ipam) net_id = res['Id'] try: network = self.neutron_client.list_networks( tags=utils.make_net_tags(net_id)) except Exception as e: self.docker_client.remove_network(net_id) message = ("Failed to list neutron networks: %s") self.fail(message % e.args[0]) self.assertEqual(1, len(network['networks'])) self.docker_client.remove_network(net_id) network = self.neutron_client.list_networks( tags=utils.make_net_tags(net_id)) self.assertEqual(0, len(network['networks']))
def test_create_delete_network_with_kuryr_driver(self): """Create and Delete docker network with Kuryr This method creates a docker network with Kuryr driver and tests it was created in Neutron. It then deletes the docker network and tests that it was deleted from Neutron. """ fake_ipam = { "Driver": "kuryr", "Options": {}, "Config": [{ "Subnet": "10.0.0.0/16", "IPRange": "10.0.0.0/24", "Gateway": "10.0.0.1" }] } net_name = lib_utils.get_random_string(8) res = self.docker_client.create_network(name=net_name, driver='kuryr', ipam=fake_ipam) net_id = res['Id'] try: network = self.neutron_client.list_networks( tags=utils.make_net_tags(net_id)) except Exception as e: self.docker_client.remove_network(net_id) message = ("Failed to list neutron networks: %s") self.fail(message % e.args[0]) self.assertEqual(1, len(network['networks'])) self.docker_client.remove_network(net_id) network = self.neutron_client.list_networks( tags=utils.make_net_tags(net_id)) self.assertEqual(0, len(network['networks']))
def test_create_network_with_same_name(self): """Create docker network with same name Create two docker networks with same name, delete them and see that neutron networks are deleted as well """ fake_ipam_1 = { "Driver": "kuryr", "Options": {}, "Config": [{ "Subnet": "10.1.0.0/16", "IPRange": "10.1.0.0/24", "Gateway": "10.1.0.1" }] } fake_ipam_2 = { "Driver": "kuryr", "Options": {}, "Config": [{ "Subnet": "10.2.0.0/16", "IPRange": "10.2.0.0/24", "Gateway": "10.2.0.1" }] } net_name = lib_utils.get_random_string(8) res = self.docker_client.create_network(name=net_name, driver='kuryr', ipam=fake_ipam_1) net_id1 = res['Id'] res = self.docker_client.create_network(name=net_name, driver='kuryr', ipam=fake_ipam_2) net_id2 = res['Id'] try: network = self.neutron_client.list_networks( tags=utils.make_net_tags(net_id1)) self.assertEqual(1, len(network['networks'])) network = self.neutron_client.list_networks( tags=utils.make_net_tags(net_id2)) self.assertEqual(1, len(network['networks'])) except Exception as e: self.docker_client.remove_network(net_id1) self.docker_client.remove_network(net_id2) message = ("Failed to list neutron networks: %s") self.fail(message % e.args[0]) self.docker_client.remove_network(net_id1) self.docker_client.remove_network(net_id2) network = self.neutron_client.list_networks( tags=utils.make_net_tags(net_id1)) self.assertEqual(0, len(network['networks'])) network = self.neutron_client.list_networks( tags=utils.make_net_tags(net_id2)) self.assertEqual(0, len(network['networks']))
def test_create_network_with_same_name(self): """Create docker network with same name Create two docker networks with same name, delete them and see that neutron networks are deleted as well """ fake_ipam_1 = { "Driver": "kuryr", "Options": {}, "Config": [ { "Subnet": "10.1.0.0/16", "IPRange": "10.1.0.0/24", "Gateway": "10.1.0.1" } ] } fake_ipam_2 = { "Driver": "kuryr", "Options": {}, "Config": [ { "Subnet": "10.2.0.0/16", "IPRange": "10.2.0.0/24", "Gateway": "10.2.0.1" } ] } net_name = lib_utils.get_random_string(8) res = self.docker_client.create_network(name=net_name, driver='kuryr', ipam=fake_ipam_1) net_id1 = res['Id'] res = self.docker_client.create_network(name=net_name, driver='kuryr', ipam=fake_ipam_2) net_id2 = res['Id'] try: network = self.neutron_client.list_networks( tags=utils.make_net_tags(net_id1)) self.assertEqual(1, len(network['networks'])) network = self.neutron_client.list_networks( tags=utils.make_net_tags(net_id2)) self.assertEqual(1, len(network['networks'])) except Exception as e: self.docker_client.remove_network(net_id1) self.docker_client.remove_network(net_id2) message = ("Failed to list neutron networks: %s") self.fail(message % e.args[0]) self.docker_client.remove_network(net_id1) self.docker_client.remove_network(net_id2) network = self.neutron_client.list_networks( tags=utils.make_net_tags(net_id1)) self.assertEqual(0, len(network['networks'])) network = self.neutron_client.list_networks( tags=utils.make_net_tags(net_id2)) self.assertEqual(0, len(network['networks']))
def test_create_delete_dualstack_network_with_kuryr_driver(self): """Create and Delete docker dual-stack network with Kuryr This method creates a docker network with Kuryr driver and tests it was created in Neutron. It then deletes the docker network and tests that it was deleted from Neutron. """ fake_ipam = { "Driver": "kuryr", "Options": {}, "Config": [{ "Subnet": "10.4.0.0/16", "IPRange": "10.4.0.0/24", "Gateway": "10.4.0.1" }, { "Subnet": "2001:db8:a0b:12f0::/64", "IPRange": "2001:db8:a0b:12f0::/64", "Gateway": "2001:db8:a0b:12f0::1" }] } net_name = lib_utils.get_random_string(8) res = self.docker_client.create_network(name=net_name, driver='kuryr', enable_ipv6=True, ipam=fake_ipam) net_id = res['Id'] try: network = self.neutron_client.list_networks( tags=utils.make_net_tags(net_id)) except Exception as e: self.docker_client.remove_network(net_id) message = ("Failed to list neutron networks: %s") self.fail(message % e.args[0]) self.assertEqual(1, len(network['networks'])) neutron_netid = network['networks'][0]['id'] subnets = self.neutron_client.list_subnets(network_id=neutron_netid) self.assertEqual(2, len(subnets['subnets'])) subnet_version = [] subnet_name = [] for subnet in subnets['subnets']: subnet_version.append(subnet['ip_version']) subnet_name.append(subnet['name']) self.assertIn(4, subnet_version) self.assertIn(6, subnet_version) self.assertIn(constants.SUBNET_NAME_PREFIX + '10.4.0.0/24', subnet_name) self.assertIn(constants.SUBNET_NAME_PREFIX + '2001:db8:a0b:12f0::/64', subnet_name) self.docker_client.remove_network(net_id) network = self.neutron_client.list_networks( tags=utils.make_net_tags(net_id)) self.assertEqual(0, len(network['networks'])) subnets = self.neutron_client.list_subnets(network_id=neutron_netid) self.assertEqual(0, len(subnets['subnets']))
def setUp(self): super(ContainerTest, self).setUp() fake_ipam = { "Driver": "kuryr", "Options": {}, "Config": [ { "Subnet": "10.3.0.0/16", "IPRange": "10.3.0.0/24", "Gateway": "10.3.0.1" } ] } net_name = lib_utils.get_random_string(8) res = self.docker_client.create_network(name=net_name, driver='kuryr', ipam=fake_ipam) self.net_id = res.get('Id') try: networks = self.neutron_client.list_networks( tags=utils.make_net_tags(self.net_id)) except Exception as e: self.docker_client.remove_network(self.net_id) message = ("Failed to list neutron networks: %s") self.fail(message % e.args[0]) self.assertEqual(1, len(networks['networks'])) self.neutron_net_id = networks['networks'][0]['id']
def _mock_out_network(self, neutron_network_id, docker_network_id, check_existing=False): no_networks_response = { "networks": [] } fake_list_response = { "networks": [{ "status": "ACTIVE", "subnets": [], "admin_state_up": True, "tenant_id": "9bacb3c5d39d41a79512987f338cf177", "router:external": False, "segments": [], "shared": False, "id": neutron_network_id }] } self.mox.StubOutWithMock(app.neutron, 'list_networks') t = utils.make_net_tags(docker_network_id) if check_existing: te = t + ',' + const.KURYR_EXISTING_NEUTRON_NET app.neutron.list_networks(tags=te).AndReturn( no_networks_response) app.neutron.list_networks(tags=t).AndReturn(fake_list_response) self.mox.ReplayAll() return neutron_network_id
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)
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 _delete_network_with_subnet_exception(self, network_id, ex): fake_neutron_network_id = "4e8e5957-649f-477b-9e5b-f1f75b21c03c" no_networks_response = { "networks": [] } fake_networks_response = { "networks": [{ "status": "ACTIVE", "subnets": [], "name": network_id, "admin_state_up": True, "tenant_id": "9bacb3c5d39d41a79512987f338cf177", "router:external": False, "segments": [], "shared": False, "id": fake_neutron_network_id }] } self.mox.StubOutWithMock(app.neutron, 'list_networks') t = utils.make_net_tags(network_id) te = t + ',' + const.KURYR_EXISTING_NEUTRON_NET app.neutron.list_networks(tags=te).AndReturn(no_networks_response) app.neutron.list_networks(tags=t).AndReturn(fake_networks_response) subnet_v4_id = "9436e561-47bf-436a-b1f1-fe23a926e031" subnet_v6_id = "64dd4a98-3d7a-4bfd-acf4-91137a8d2f51" docker_network_id = utils.get_hash() docker_endpoint_id = utils.get_hash() 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'] ] } self.mox.StubOutWithMock(app.neutron, 'list_subnets') app.neutron.list_subnets(network_id=fake_neutron_network_id).AndReturn( fake_subnets_response) self.mox.StubOutWithMock(app.neutron, 'list_subnetpools') fake_subnetpools_response = {"subnetpools": []} app.neutron.list_subnetpools(name='kuryr').AndReturn( fake_subnetpools_response) app.neutron.list_subnetpools(name='kuryr6').AndReturn( fake_subnetpools_response) self.mox.StubOutWithMock(app.neutron, 'delete_subnet') app.neutron.delete_subnet(subnet_v4_id).AndRaise(ex) self.mox.ReplayAll()
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)
def test_delete_network_pre_existing(self, use_tags): if not use_tags: self.mox.StubOutWithMock(app, "tag") app.tag = use_tags docker_network_id, fake_neutron_net_id, fake_response = self._ids() if app.tag: self.mox.StubOutWithMock(app.neutron, 'list_networks') t = utils.make_net_tags(docker_network_id) te = t + ',' + const.KURYR_EXISTING_NEUTRON_NET app.neutron.list_networks(tags=te).AndReturn( fake_response) self.mox.StubOutWithMock(app.neutron, "remove_tag") tags = utils.create_net_tags(docker_network_id) for tag in tags: app.neutron.remove_tag('networks', fake_neutron_net_id, tag) app.neutron.remove_tag('networks', fake_neutron_net_id, const.KURYR_EXISTING_NEUTRON_NET) else: self.mox.StubOutWithMock(app.neutron, 'list_networks') app.neutron.list_networks(name=docker_network_id).AndReturn( fake_response) self.mox.StubOutWithMock(app.neutron, 'list_subnets') fake_existing_subnets_response = { "subnets": [] } app.neutron.list_subnets( network_id=fake_neutron_net_id).AndReturn( fake_existing_subnets_response) self.mox.StubOutWithMock(app.neutron, 'delete_network') app.neutron.delete_network(fake_neutron_net_id).AndReturn(None) self.mox.ReplayAll() data = {'NetworkID': docker_network_id} response = self.app.post('/NetworkDriver.DeleteNetwork', content_type='application/json', data=jsonutils.dumps(data)) self.assertEqual(200, response.status_code) decoded_json = jsonutils.loads(response.data) self.assertEqual(const.SCHEMA['SUCCESS'], decoded_json)
def test_create_delete_network_without_kuryr_driver(self): """Create and Delete docker network without Kuryr This method create a docker network with the default docker driver, It tests that it was created correctly, but not added to Neutron """ net_name = lib_utils.get_random_string(8) res = self.docker_client.create_network(name=net_name) net_id = res['Id'] network = self.neutron_client.list_networks( tags=utils.make_net_tags(net_id)) self.assertEqual(0, len(network['networks'])) docker_networks = self.docker_client.networks() network_found = False for docker_net in docker_networks: if docker_net['Id'] == net_id: network_found = True self.assertTrue(network_found) self.docker_client.remove_network(net_id)
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) neutron_port_name = utils.get_neutron_port_name( 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(name=neutron_port_name) 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'])
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'])
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_container_ipam_request_address(self): fake_ipam = { "Driver": "kuryr", "Options": {}, "Config": [{ "Subnet": "10.12.0.0/16", "IPRange": "10.12.0.0/24", "Gateway": "10.12.0.1" }] } container_net_name = lib_utils.get_random_string(8) container_net = self.docker_client.create_network( name=container_net_name, driver='kuryr', ipam=fake_ipam) container_net_id = container_net.get('Id') try: networks = self.neutron_client.list_networks( tags=utils.make_net_tags(container_net_id)) except Exception as e: self.docker_client.remove_network(container_net_id) message = ("Failed to list neutron networks: %s") self.fail(message % e.args[0]) # Currently we cannot get IPAM pool from docker client. pool_name = "kuryrPool-" + "10.12.0.0/24" subnetpools = self.neutron_client.list_subnetpools(name=pool_name) self.assertEqual(1, len(subnetpools['subnetpools'])) subnets = self.neutron_client.list_subnets( network_id=networks['networks'][0]['id'], cidr="10.12.0.0/24") self.assertEqual(1, len(subnets['subnets'])) # Boot a container, and connect to the docker network. container_name = lib_utils.get_random_string(8) container = self.docker_client.create_container( image='kuryr/busybox', command='/bin/sleep 600', hostname='kuryr_test_container', name=container_name) warn_msg = container.get('Warning') container_id = container.get('Id') self.assertIsNone(warn_msg, 'Warn in creating container') self.assertIsNotNone(container_id, 'Create container id must not ' 'be None') self.docker_client.start(container=container_id) self.docker_client.connect_container_to_network( container_id, container_net_id) try: ports = self.neutron_client.list_ports( network_id=networks['networks'][0]['id']) except Exception as e: self.docker_client.disconnect_container_from_network( container_id, container_net_id) message = ("Failed to list neutron ports: %s") self.fail(message % e.args[0]) # DHCP port and container endpoint self.assertEqual(2, len(ports['ports'])) # Find the kuryr port kuryr_port_param = {"network_id": networks['networks'][0]['id']} kuryr_ports = self.neutron_client.list_ports(**kuryr_port_param) kuryr_port = [ port for port in kuryr_ports['ports'] if (lib_const.DEVICE_OWNER in port['tags'] or port['name'] == utils.get_neutron_port_name(port['device_id'])) ] self.assertEqual(1, len(kuryr_port)) # Disconnect container from network, this release ip address. self.docker_client.disconnect_container_from_network( container_id, container_net_id) # Cleanup resources self.docker_client.stop(container=container_id) self.docker_client.remove_network(container_net_id)
def test_container_ipam_request_address_with_existing_port(self): # pre-created Neutron network and subnet and port neutron_net_name = lib_utils.get_random_string(8) neutron_network = self.neutron_client.create_network( {'network': { 'name': neutron_net_name, "admin_state_up": True }}) neutron_subnet_name = lib_utils.get_random_string(8) subnet_param = [{ 'name': neutron_subnet_name, 'network_id': neutron_network['network']['id'], 'ip_version': 4, 'cidr': "10.14.0.0/24", }] neutron_subnet = self.neutron_client.create_subnet( {'subnets': subnet_param}) neutron_v6_subnet_name = lib_utils.get_random_string(8) v6_subnet_param = [{ 'name': neutron_v6_subnet_name, 'network_id': neutron_network['network']['id'], 'ip_version': 6, 'cidr': "fe81::/64", }] neutron_v6_subnet = self.neutron_client.create_subnet( {'subnets': v6_subnet_param}) existing_neutron_port = self.neutron_client.create_port( {'port': { 'network_id': neutron_network['network']['id'] }}) fixed_ips = { fip['subnet_id']: fip['ip_address'] for fip in existing_neutron_port['port']['fixed_ips'] } ipv4_address = fixed_ips[neutron_subnet['subnets'][0]['id']] ipv6_address = fixed_ips[neutron_v6_subnet['subnets'][0]['id']] fake_ipam = { "Driver": "kuryr", "Options": {}, "Config": [ { "Subnet": "10.14.0.0/24", "Gateway": "10.14.0.1" }, { "Subnet": "fe81::/64", "Gateway": "fe81::1" }, ] } # Create docker network using existing Neutron network options = {'neutron.net.name': neutron_net_name} container_net_name = lib_utils.get_random_string(8) container_net = self.docker_client.create_network( name=container_net_name, driver='kuryr', enable_ipv6=True, options=options, ipam=fake_ipam) container_net_id = container_net.get('Id') try: networks = self.neutron_client.list_networks( tags=utils.make_net_tags(container_net_id)) except Exception as e: self.docker_client.remove_network(container_net_id) message = ("Failed to list neutron networks: %s") self.fail(message % e.args[0]) self.assertEqual(1, len(networks['networks'])) # Boot a container, and connect to the docker network. container_name = lib_utils.get_random_string(8) container = self.docker_client.create_container( image='kuryr/busybox', command='/bin/sleep 600', hostname='kuryr_test_container', name=container_name) warn_msg = container.get('Warning') container_id = container.get('Id') self.assertIsNone(warn_msg, 'Warn in creating container') self.assertIsNotNone(container_id, 'Create container id must not ' 'be None') self.docker_client.start(container=container_id) self.docker_client.connect_container_to_network( container_id, container_net_id, ipv4_address=ipv4_address, ipv6_address=ipv6_address) try: ports = self.neutron_client.list_ports( network_id=neutron_network['network']['id']) except Exception as e: self.docker_client.disconnect_container_from_network( container_id, container_net_id) message = ("Failed to list neutron ports: %s") self.fail(message % e.args[0]) # A dhcp port gets created as well; dhcp is enabled by default self.assertEqual(2, len(ports['ports'])) # Find the existing neutron port neutron_port_param = {"network_id": neutron_network['network']['id']} neutron_ports = self.neutron_client.list_ports(**neutron_port_param) neutron_port = [ port for port in neutron_ports['ports'] if (const.KURYR_EXISTING_NEUTRON_PORT in port['tags'] or port['name'] == utils.get_neutron_port_name(port['device_id'])) ] self.assertEqual(1, len(neutron_port)) # Disconnect container from network. self.docker_client.disconnect_container_from_network( container_id, container_net_id) ports = self.neutron_client.list_ports( network_id=neutron_network['network']['id']) self.assertEqual(2, len(ports['ports'])) neutron_ports = self.neutron_client.list_ports(**neutron_port_param) neutron_port = [ port for port in neutron_ports['ports'] if (const.KURYR_EXISTING_NEUTRON_PORT in port['tags'] or port['name'] == utils.get_neutron_port_name(port['device_id'])) ] self.assertEqual(0, len(neutron_port)) # Cleanup resources self.docker_client.stop(container=container_id) self.docker_client.remove_network(container_net_id) self.neutron_client.delete_port(existing_neutron_port['port']['id']) self.neutron_client.delete_subnet(neutron_subnet['subnets'][0]['id']) self.neutron_client.delete_subnet( neutron_v6_subnet['subnets'][0]['id']) self.neutron_client.delete_network(neutron_network['network']['id'])
def test_container_ipam_release_address_with_existing_network(self): # pre-created Neutron network and subnet neutron_net_name = lib_utils.get_random_string(8) neutron_network = self.neutron_client.create_network( {'network': { 'name': neutron_net_name, "admin_state_up": True }}) neutron_subnet_name = lib_utils.get_random_string(8) subnet_param = [{ 'name': neutron_subnet_name, 'network_id': neutron_network['network']['id'], 'ip_version': 4, 'cidr': "10.10.0.0/24", 'enable_dhcp': True, }] self.neutron_client.create_subnet({'subnets': subnet_param}) fake_ipam = { "Driver": "kuryr", "Options": {}, "Config": [{ "Subnet": "10.10.0.0/16", "IPRange": "10.10.0.0/24", "Gateway": "10.10.0.1" }] } # Create docker network using existing Neutron network options = {'neutron.net.name': neutron_net_name} container_net_name = lib_utils.get_random_string(8) container_net = self.docker_client.create_network( name=container_net_name, driver='kuryr', options=options, ipam=fake_ipam) container_net_id = container_net.get('Id') try: networks = self.neutron_client.list_networks( tags=utils.make_net_tags(container_net_id)) except Exception as e: self.docker_client.remove_network(container_net_id) message = ("Failed to list neutron networks: %s") self.fail(message % e.args[0]) self.assertEqual(1, len(networks['networks'])) # Boot a container, and connect to the docker network. container_name = lib_utils.get_random_string(8) container = self.docker_client.create_container( image='kuryr/busybox', command='/bin/sleep 600', hostname='kuryr_test_container', name=container_name) warn_msg = container.get('Warning') container_id = container.get('Id') self.assertIsNone(warn_msg, 'Warn in creating container') self.assertIsNotNone(container_id, 'Create container id must not ' 'be None') self.docker_client.start(container=container_id) self.docker_client.connect_container_to_network( container_id, container_net_id) try: ports = self.neutron_client.list_ports( network_id=neutron_network['network']['id']) except Exception as e: self.docker_client.disconnect_container_from_network( container_id, container_net_id) message = ("Failed to list neutron ports: %s") self.fail(message % e.args[0]) # A dhcp port gets created as well; dhcp is enabled by default self.assertEqual(2, len(ports['ports'])) # Find the kuryr port kuryr_port_param = {"network_id": neutron_network['network']['id']} kuryr_ports = self.neutron_client.list_ports(**kuryr_port_param) kuryr_port = [ port for port in kuryr_ports['ports'] if (lib_const.DEVICE_OWNER in port['tags'] or port['name'] == utils.get_neutron_port_name(port['device_id'])) ] self.assertEqual(1, len(kuryr_port)) # Disconnect container from network, this release ip address. self.docker_client.disconnect_container_from_network( container_id, container_net_id) ports = self.neutron_client.list_ports( network_id=neutron_network['network']['id']) self.assertEqual(1, len(ports['ports'])) kuryr_ports = self.neutron_client.list_ports(**kuryr_port_param) kuryr_port = [ port for port in kuryr_ports['ports'] if (lib_const.DEVICE_OWNER in port['tags'] or port['name'] == utils.get_neutron_port_name(port['device_id'])) ] self.assertEqual(0, len(kuryr_port)) # Cleanup resources self.docker_client.stop(container=container_id) self.docker_client.remove_network(container_net_id) self.neutron_client.delete_network(neutron_network['network']['id'])
def test_container_ipam_request_release_pool(self): fake_ipam = { "Driver": "kuryr", "Options": {}, "Config": [{ "Subnet": "10.11.0.0/16", "IPRange": "10.11.0.0/24", "Gateway": "10.11.0.1" }] } container_net_name = lib_utils.get_random_string(8) container_net = self.docker_client.create_network( name=container_net_name, driver='kuryr', ipam=fake_ipam) container_net_id = container_net.get('Id') try: networks = self.neutron_client.list_networks( tags=utils.make_net_tags(container_net_id)) except Exception as e: self.docker_client.remove_network(container_net_id) message = ("Failed to list neutron networks: %s") self.fail(message % e.args[0]) # Currently we cannot get IPAM pool from docker client. pool_name = "kuryrPool-" + "10.11.0.0/24" subnetpools = self.neutron_client.list_subnetpools(name=pool_name) self.assertEqual(1, len(subnetpools['subnetpools'])) # Boot a container, and connect to the docker network. container_name = lib_utils.get_random_string(8) container = self.docker_client.create_container( image='kuryr/busybox', command='/bin/sleep 600', hostname='kuryr_test_container', name=container_name) warn_msg = container.get('Warning') container_id = container.get('Id') self.assertIsNone(warn_msg, 'Warn in creating container') self.assertIsNotNone(container_id, 'Create container id must not ' 'be None') self.docker_client.start(container=container_id) self.docker_client.connect_container_to_network( container_id, container_net_id) try: self.neutron_client.list_ports( network_id=networks['networks'][0]['id']) except Exception as e: self.docker_client.disconnect_container_from_network( container_id, container_net_id) message = ("Failed to list neutron ports: %s") self.fail(message % e.args[0]) # Disconnect container from network, this release ip address. self.docker_client.disconnect_container_from_network( container_id, container_net_id) # Delete docker network, if no endpoint, will release the pool # and delete the subnetpool in Neutron. self.docker_client.stop(container=container_id) self.docker_client.remove_network(container_net_id) subnetpools = self.neutron_client.list_subnetpools(name=pool_name) self.assertEqual(0, len(subnetpools['subnetpools']))
def test_create_delete_dualstack_network_with_kuryr_driver(self): """Create and Delete docker dual-stack network with Kuryr This method creates a docker network with Kuryr driver and tests it was created in Neutron. It then deletes the docker network and tests that it was deleted from Neutron. """ fake_ipam = { "Driver": "kuryr", "Options": {}, "Config": [ { "Subnet": "10.4.0.0/16", "IPRange": "10.4.0.0/24", "Gateway": "10.4.0.1" }, { "Subnet": "2001:db8:a0b:12f0::/64", "IPRange": "2001:db8:a0b:12f0::/64", "Gateway": "2001:db8:a0b:12f0::1" } ] } net_name = lib_utils.get_random_string(8) res = self.docker_client.create_network(name=net_name, driver='kuryr', enable_ipv6=True, ipam=fake_ipam) net_id = res['Id'] try: network = self.neutron_client.list_networks( tags=utils.make_net_tags(net_id)) except Exception as e: self.docker_client.remove_network(net_id) message = ("Failed to list neutron networks: %s") self.fail(message % e.args[0]) self.assertEqual(1, len(network['networks'])) neutron_netid = network['networks'][0]['id'] subnets = self.neutron_client.list_subnets( network_id=neutron_netid) self.assertEqual(2, len(subnets['subnets'])) subnet_version = [] subnet_name = [] for subnet in subnets['subnets']: subnet_version.append(subnet['ip_version']) subnet_name.append(subnet['name']) self.assertIn(4, subnet_version) self.assertIn(6, subnet_version) self.assertIn( constants.SUBNET_NAME_PREFIX + '10.4.0.0/24', subnet_name) self.assertIn( constants.SUBNET_NAME_PREFIX + '2001:db8:a0b:12f0::/64', subnet_name) self.docker_client.remove_network(net_id) network = self.neutron_client.list_networks( tags=utils.make_net_tags(net_id)) self.assertEqual(0, len(network['networks'])) subnets = self.neutron_client.list_subnets( network_id=neutron_netid) self.assertEqual(0, len(subnets['subnets']))
def test_container_ipam_release_address_with_existing_network(self): # pre-created Neutron network and subnet neutron_net_name = lib_utils.get_random_string(8) neutron_network = self.neutron_client.create_network( {'network': {'name': neutron_net_name, "admin_state_up": True}}) neutron_subnet_name = lib_utils.get_random_string(8) subnet_param = [{ 'name': neutron_subnet_name, 'network_id': neutron_network['network']['id'], 'ip_version': 4, 'cidr': "10.10.0.0/24", 'enable_dhcp': True, }] self.neutron_client.create_subnet({'subnets': subnet_param}) fake_ipam = { "Driver": "kuryr", "Options": {}, "Config": [ { "Subnet": "10.10.0.0/16", "IPRange": "10.10.0.0/24", "Gateway": "10.10.0.1" } ] } # Create docker network using existing Neutron network options = {'neutron.net.name': neutron_net_name} container_net_name = lib_utils.get_random_string(8) container_net = self.docker_client.create_network( name=container_net_name, driver='kuryr', options=options, ipam=fake_ipam) container_net_id = container_net.get('Id') try: networks = self.neutron_client.list_networks( tags=utils.make_net_tags(container_net_id)) except Exception as e: self.docker_client.remove_network(container_net_id) message = ("Failed to list neutron networks: %s") self.fail(message % e.args[0]) self.assertEqual(1, len(networks['networks'])) # Boot a container, and connect to the docker network. container_name = lib_utils.get_random_string(8) container = self.docker_client.create_container( image='kuryr/busybox', command='/bin/sleep 600', hostname='kuryr_test_container', name=container_name) warn_msg = container.get('Warning') container_id = container.get('Id') self.assertIsNone(warn_msg, 'Warn in creating container') self.assertIsNotNone(container_id, 'Create container id must not ' 'be None') self.docker_client.start(container=container_id) self.docker_client.connect_container_to_network(container_id, container_net_id) try: ports = self.neutron_client.list_ports( network_id=neutron_network['network']['id']) except Exception as e: self.docker_client.disconnect_container_from_network( container_id, container_net_id) message = ("Failed to list neutron ports: %s") self.fail(message % e.args[0]) # A dhcp port gets created as well; dhcp is enabled by default self.assertEqual(2, len(ports['ports'])) # Find the kuryr port kuryr_port_param = {"network_id": neutron_network['network']['id']} kuryr_ports = self.neutron_client.list_ports( **kuryr_port_param) kuryr_port = [port for port in kuryr_ports['ports'] if (lib_const.DEVICE_OWNER in port['tags'] or port['name'] == utils.get_neutron_port_name(port['device_id']))] self.assertEqual(1, len(kuryr_port)) # Disconnect container from network, this release ip address. self.docker_client.disconnect_container_from_network(container_id, container_net_id) ports = self.neutron_client.list_ports( network_id=neutron_network['network']['id']) self.assertEqual(1, len(ports['ports'])) kuryr_ports = self.neutron_client.list_ports( **kuryr_port_param) kuryr_port = [port for port in kuryr_ports['ports'] if (lib_const.DEVICE_OWNER in port['tags'] or port['name'] == utils.get_neutron_port_name(port['device_id']))] self.assertEqual(0, len(kuryr_port)) # Cleanup resources self.docker_client.stop(container=container_id) self.docker_client.remove_network(container_net_id) self.neutron_client.delete_network(neutron_network['network']['id'])
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) 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") 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_container_ipam_request_address_with_existing_port(self): # pre-created Neutron network and subnet and port neutron_net_name = lib_utils.get_random_string(8) neutron_network = self.neutron_client.create_network( {'network': {'name': neutron_net_name, "admin_state_up": True}}) neutron_subnet_name = lib_utils.get_random_string(8) subnet_param = [{ 'name': neutron_subnet_name, 'network_id': neutron_network['network']['id'], 'ip_version': 4, 'cidr': "10.14.0.0/24", }] neutron_subnet = self.neutron_client.create_subnet( {'subnets': subnet_param}) neutron_v6_subnet_name = lib_utils.get_random_string(8) v6_subnet_param = [{ 'name': neutron_v6_subnet_name, 'network_id': neutron_network['network']['id'], 'ip_version': 6, 'cidr': "fe81::/64", }] neutron_v6_subnet = self.neutron_client.create_subnet( {'subnets': v6_subnet_param}) existing_neutron_port = self.neutron_client.create_port( {'port': {'network_id': neutron_network['network']['id']}}) fixed_ips = {fip['subnet_id']: fip['ip_address'] for fip in existing_neutron_port['port']['fixed_ips']} ipv4_address = fixed_ips[neutron_subnet['subnets'][0]['id']] ipv6_address = fixed_ips[neutron_v6_subnet['subnets'][0]['id']] fake_ipam = { "Driver": "kuryr", "Options": {}, "Config": [ { "Subnet": "10.14.0.0/24", "Gateway": "10.14.0.1" }, { "Subnet": "fe81::/64", "Gateway": "fe81::1" }, ] } # Create docker network using existing Neutron network options = {'neutron.net.name': neutron_net_name} container_net_name = lib_utils.get_random_string(8) container_net = self.docker_client.create_network( name=container_net_name, driver='kuryr', enable_ipv6=True, options=options, ipam=fake_ipam) container_net_id = container_net.get('Id') try: networks = self.neutron_client.list_networks( tags=utils.make_net_tags(container_net_id)) except Exception as e: self.docker_client.remove_network(container_net_id) message = ("Failed to list neutron networks: %s") self.fail(message % e.args[0]) self.assertEqual(1, len(networks['networks'])) # Boot a container, and connect to the docker network. container_name = lib_utils.get_random_string(8) container = self.docker_client.create_container( image='kuryr/busybox', command='/bin/sleep 600', hostname='kuryr_test_container', name=container_name) warn_msg = container.get('Warning') container_id = container.get('Id') self.assertIsNone(warn_msg, 'Warn in creating container') self.assertIsNotNone(container_id, 'Create container id must not ' 'be None') self.docker_client.start(container=container_id) self.docker_client.connect_container_to_network( container_id, container_net_id, ipv4_address=ipv4_address, ipv6_address=ipv6_address) try: ports = self.neutron_client.list_ports( network_id=neutron_network['network']['id']) except Exception as e: self.docker_client.disconnect_container_from_network( container_id, container_net_id) message = ("Failed to list neutron ports: %s") self.fail(message % e.args[0]) # A dhcp port gets created as well; dhcp is enabled by default self.assertEqual(2, len(ports['ports'])) # Find the existing neutron port neutron_port_param = {"network_id": neutron_network['network']['id']} neutron_ports = self.neutron_client.list_ports( **neutron_port_param) neutron_port = [port for port in neutron_ports['ports'] if (const.KURYR_EXISTING_NEUTRON_PORT in port['tags'] or port['name'] == utils.get_neutron_port_name(port['device_id']))] self.assertEqual(1, len(neutron_port)) # Disconnect container from network. self.docker_client.disconnect_container_from_network(container_id, container_net_id) ports = self.neutron_client.list_ports( network_id=neutron_network['network']['id']) self.assertEqual(2, len(ports['ports'])) neutron_ports = self.neutron_client.list_ports( **neutron_port_param) neutron_port = [port for port in neutron_ports['ports'] if (const.KURYR_EXISTING_NEUTRON_PORT in port['tags'] or port['name'] == utils.get_neutron_port_name(port['device_id']))] self.assertEqual(0, len(neutron_port)) # Cleanup resources self.docker_client.stop(container=container_id) self.docker_client.remove_network(container_net_id) self.neutron_client.delete_port(existing_neutron_port['port']['id']) self.neutron_client.delete_subnet(neutron_subnet['subnets'][0]['id']) self.neutron_client.delete_subnet( neutron_v6_subnet['subnets'][0]['id']) self.neutron_client.delete_network(neutron_network['network']['id'])
def test_container_ipam_request_address(self): fake_ipam = { "Driver": "kuryr", "Options": {}, "Config": [ { "Subnet": "10.12.0.0/16", "IPRange": "10.12.0.0/24", "Gateway": "10.12.0.1" } ] } container_net_name = lib_utils.get_random_string(8) container_net = self.docker_client.create_network( name=container_net_name, driver='kuryr', ipam=fake_ipam) container_net_id = container_net.get('Id') try: networks = self.neutron_client.list_networks( tags=utils.make_net_tags(container_net_id)) except Exception as e: self.docker_client.remove_network(container_net_id) message = ("Failed to list neutron networks: %s") self.fail(message % e.args[0]) # Currently we cannot get IPAM pool from docker client. pool_name = "kuryrPool-" + "10.12.0.0/24" subnetpools = self.neutron_client.list_subnetpools(name=pool_name) self.assertEqual(1, len(subnetpools['subnetpools'])) subnets = self.neutron_client.list_subnets( network_id=networks['networks'][0]['id'], cidr="10.12.0.0/24") self.assertEqual(1, len(subnets['subnets'])) # Boot a container, and connect to the docker network. container_name = lib_utils.get_random_string(8) container = self.docker_client.create_container( image='kuryr/busybox', command='/bin/sleep 600', hostname='kuryr_test_container', name=container_name) warn_msg = container.get('Warning') container_id = container.get('Id') self.assertIsNone(warn_msg, 'Warn in creating container') self.assertIsNotNone(container_id, 'Create container id must not ' 'be None') self.docker_client.start(container=container_id) self.docker_client.connect_container_to_network(container_id, container_net_id) try: ports = self.neutron_client.list_ports( network_id=networks['networks'][0]['id']) except Exception as e: self.docker_client.disconnect_container_from_network( container_id, container_net_id) message = ("Failed to list neutron ports: %s") self.fail(message % e.args[0]) # DHCP port and container endpoint self.assertEqual(2, len(ports['ports'])) # Find the kuryr port kuryr_port_param = {"network_id": networks['networks'][0]['id']} kuryr_ports = self.neutron_client.list_ports( **kuryr_port_param) kuryr_port = [port for port in kuryr_ports['ports'] if (lib_const.DEVICE_OWNER in port['tags'] or port['name'] == utils.get_neutron_port_name(port['device_id']))] self.assertEqual(1, len(kuryr_port)) # Disconnect container from network, this release ip address. self.docker_client.disconnect_container_from_network(container_id, container_net_id) # Cleanup resources self.docker_client.stop(container=container_id) self.docker_client.remove_network(container_net_id)
def test_delete_network_pre_existing_kuryr_subnets_pre_created( self, use_tags, mock_tag, mock_list_networks, mock_remove_tag, mock_delete_subnet, mock_delete_network, mock_list_subnets): if not use_tags: mock_tag.tag = use_tags docker_network_id, fake_neutron_net_id, _ = self._ids() # fake pre-existed kuryr subnet kuryr_subnet_v4_id = uuidutils.generate_uuid() kuryr_fake_cidr_v4 = '192.168.4.0/24' kuryr_fake_v4_subnet = self._get_fake_v4_subnet( fake_neutron_net_id, subnet_v4_id=kuryr_subnet_v4_id, subnetpool_id=uuidutils.generate_uuid(), cidr=kuryr_fake_cidr_v4, name=utils.make_subnet_name(kuryr_fake_cidr_v4)) fake_existing_subnets_response = { "subnets": [ kuryr_fake_v4_subnet['subnet'] ] } if mock_tag.tag: t = utils.make_net_tags(docker_network_id) te = t + ',' + utils.existing_net_tag(docker_network_id) tags = utils.create_net_tags(docker_network_id) tags += [utils.existing_net_tag(docker_network_id)] else: fake_existing_subnets_response = { "subnets": [] } mock_delete_network.return_value = None fake_neutron_existing_network_response = { 'networks': [ { "status": "ACTIVE", "subnets": fake_existing_subnets_response["subnets"], "admin_state_up": True, "tenant_id": "9bacb3c5d39d41a79512987f338cf177", "router:external": False, "segments": [], "shared": False, "id": fake_neutron_net_id, "tags": tags if mock_tag.tag else [], } ] } mock_list_networks.return_value = ( fake_neutron_existing_network_response) mock_list_subnets.return_value = fake_existing_subnets_response data = {'NetworkID': docker_network_id} response = self.app.post('/NetworkDriver.DeleteNetwork', content_type='application/json', data=jsonutils.dumps(data)) self.assertEqual(200, response.status_code) if mock_tag.tag: mock_list_networks.assert_any_call(tags=te) for tag in tags: mock_remove_tag.assert_any_call('networks', fake_neutron_net_id, tag) mock_list_subnets.assert_called_with( network_id=fake_neutron_net_id) mock_delete_subnet.assert_called_once_with(kuryr_subnet_v4_id) else: mock_list_networks.assert_any_call(name=docker_network_id) mock_list_subnets.assert_called_with( network_id=fake_neutron_net_id) mock_delete_network.assert_called_with(fake_neutron_net_id) decoded_json = jsonutils.loads(response.data) self.assertEqual(const.SCHEMA['SUCCESS'], decoded_json)
def tearDown(self): self.docker_client.remove_network(self.net_id) networks = self.neutron_client.list_networks( tags=utils.make_net_tags(self.net_id)) self.assertEqual(0, len(networks['networks'])) super(ContainerTest, self).tearDown()
def test_container_ipam_request_release_pool(self): fake_ipam = { "Driver": "kuryr", "Options": {}, "Config": [ { "Subnet": "10.11.0.0/16", "IPRange": "10.11.0.0/24", "Gateway": "10.11.0.1" } ] } container_net_name = lib_utils.get_random_string(8) container_net = self.docker_client.create_network( name=container_net_name, driver='kuryr', ipam=fake_ipam) container_net_id = container_net.get('Id') try: networks = self.neutron_client.list_networks( tags=utils.make_net_tags(container_net_id)) except Exception as e: self.docker_client.remove_network(container_net_id) message = ("Failed to list neutron networks: %s") self.fail(message % e.args[0]) # Currently we cannot get IPAM pool from docker client. pool_name = "kuryrPool-" + "10.11.0.0/24" subnetpools = self.neutron_client.list_subnetpools(name=pool_name) self.assertEqual(1, len(subnetpools['subnetpools'])) # Boot a container, and connect to the docker network. container_name = lib_utils.get_random_string(8) container = self.docker_client.create_container( image='kuryr/busybox', command='/bin/sleep 600', hostname='kuryr_test_container', name=container_name) warn_msg = container.get('Warning') container_id = container.get('Id') self.assertIsNone(warn_msg, 'Warn in creating container') self.assertIsNotNone(container_id, 'Create container id must not ' 'be None') self.docker_client.start(container=container_id) self.docker_client.connect_container_to_network(container_id, container_net_id) try: self.neutron_client.list_ports( network_id=networks['networks'][0]['id']) except Exception as e: self.docker_client.disconnect_container_from_network( container_id, container_net_id) message = ("Failed to list neutron ports: %s") self.fail(message % e.args[0]) # Disconnect container from network, this release ip address. self.docker_client.disconnect_container_from_network(container_id, container_net_id) # Delete docker network, if no endpoint, will release the pool # and delete the subnetpool in Neutron. self.docker_client.stop(container=container_id) self.docker_client.remove_network(container_net_id) subnetpools = self.neutron_client.list_subnetpools(name=pool_name) self.assertEqual(0, len(subnetpools['subnetpools']))
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)