Ejemplo n.º 1
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_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)
Ejemplo n.º 3
0
 def test_get_subnetpool_name(self):
     fake_subnet_cidr = "10.0.0.0/16"
     generated_neutron_subnetpool_name = utils.get_neutron_subnetpool_name(
         fake_subnet_cidr)
     name_prefix = cfg.CONF.subnetpool_name_prefix
     self.assertIn(name_prefix, generated_neutron_subnetpool_name)
     self.assertIn(fake_subnet_cidr, generated_neutron_subnetpool_name)
Ejemplo n.º 4
0
    def test_request_pool_create_failures(self, GivenException,
                                          mock_create_subnetpool,
                                          mock_list_subnetpools,
                                          mock_list_subnets):
        pool_name = lib_utils.get_neutron_subnetpool_name("10.0.0.0/16")
        new_subnetpool = {
            'name': pool_name,
            'default_prefixlen': 16,
            'prefixes': ['10.0.0.0/16'],
            'shared': False
        }

        fake_subnet = {"subnets": []}
        mock_list_subnets.return_value = fake_subnet

        fake_subnet_pools = {'subnetpools': []}
        mock_list_subnetpools.return_value = fake_subnet_pools

        mock_create_subnetpool.side_effect = GivenException
        pool = '10.0.0.0/16'
        response = self._invoke_create_request(pool)

        self.assertEqual(GivenException.status_code, response.status_code)
        mock_list_subnets.assert_called_with(cidr='10.0.0.0/16')
        mock_list_subnetpools.assert_called_with(name=pool_name)
        mock_create_subnetpool.assert_called_with(
            {'subnetpool': new_subnetpool})
        decoded_json = jsonutils.loads(response.data)
        self.assertIn('Err', decoded_json)
        self.assertEqual({'Err': GivenException.message}, decoded_json)
Ejemplo n.º 5
0
    def test_request_pool_create_failures(self, GivenException,
            mock_create_subnetpool, mock_list_subnetpools, mock_list_subnets):
        pool_name = lib_utils.get_neutron_subnetpool_name("10.0.0.0/16")
        new_subnetpool = {
            'name': pool_name,
            'default_prefixlen': 16,
            'prefixes': ['10.0.0.0/16'],
            'shared': False}

        fake_subnet = {"subnets": []}
        mock_list_subnets.return_value = fake_subnet

        fake_subnet_pools = {'subnetpools': []}
        mock_list_subnetpools.return_value = fake_subnet_pools

        mock_create_subnetpool.side_effect = GivenException
        pool = '10.0.0.0/16'
        response = self._invoke_create_request(pool)

        self.assertEqual(GivenException.status_code, response.status_code)
        mock_list_subnets.assert_called_with(cidr='10.0.0.0/16')
        mock_list_subnetpools.assert_called_with(name=pool_name)
        mock_create_subnetpool.assert_called_with(
            {'subnetpool': new_subnetpool})
        decoded_json = jsonutils.loads(response.data)
        self.assertIn('Err', decoded_json)
        self.assertEqual(
            {'Err': GivenException.message}, decoded_json)
Ejemplo n.º 6
0
    def test_request_pool_list_subnetpool_failure(self, mock_list_subnetpools,
                                                  mock_list_subnets):
        fake_subnet = {"subnets": []}
        fake_pool_name = lib_utils.get_neutron_subnetpool_name("10.0.0.0/16")
        mock_list_subnets.return_value = fake_subnet

        ex = exceptions.Unauthorized
        mock_list_subnetpools.side_effect = ex

        pool = '10.0.0.0/16'
        response = self._invoke_create_request(pool)
        mock_list_subnets.assert_called_with(cidr='10.0.0.0/16')
        mock_list_subnetpools.assert_called_with(name=fake_pool_name)
        self.assertEqual(ex.status_code, response.status_code)
Ejemplo n.º 7
0
    def test_request_pool_list_subnetpool_failure(self,
                        mock_list_subnetpools, mock_list_subnets):
        fake_subnet = {"subnets": []}
        fake_pool_name = lib_utils.get_neutron_subnetpool_name("10.0.0.0/16")
        mock_list_subnets.return_value = fake_subnet

        ex = exceptions.Unauthorized
        mock_list_subnetpools.side_effect = ex

        pool = '10.0.0.0/16'
        response = self._invoke_create_request(pool)
        mock_list_subnets.assert_called_with(cidr='10.0.0.0/16')
        mock_list_subnetpools.assert_called_with(name=fake_pool_name)
        self.assertEqual(ex.status_code, response.status_code)
    def test_create_network_pre_existing(self, use_tags,
            mock_tag, mock_list_subnetpools, mock_list_networks,
            mock_add_tag, mock_update_network,
            mock_list_subnets, mock_create_subnet):
        if not use_tags:
            mock_tag.tag = use_tags

        docker_network_id, fake_neutron_net_id, fake_response = self._ids()
        fake_kuryr_subnetpool_id = uuidutils.generate_uuid()

        network_request = {
            'NetworkID': docker_network_id,
            'IPv4Data': [{
                'AddressSpace': 'foo',
                'Pool': '192.168.42.0/24',
                'Gateway': '192.168.42.1/24',
            }],
            'IPv6Data': [{
                'AddressSpace': 'bar',
                'Pool': 'fe80::/64',
                'Gateway': 'fe80::f816:3eff:fe20:57c3/64',
            }],
            'Options': {
                const.NETWORK_GENERIC_OPTIONS: {
                    const.NEUTRON_UUID_OPTION: fake_neutron_net_id
                }
            }
        }

        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)
        mock_list_subnetpools.side_effect = [
            {'subnetpools': kuryr_v4_subnetpools['subnetpools']},
            {'subnetpools': kuryr_v6_subnetpools['subnetpools']}
        ]
        mock_list_networks.return_value = fake_response

        fake_existing_subnets_response = {
            "subnets": []
        }
        fake_cidr_v4 = '192.168.42.0/24'
        mock_list_subnets.return_value = fake_existing_subnets_response

        fake_v4_subnet_request = {
            "subnets": [{
                'name': utils.make_subnet_name(fake_cidr_v4),
                'network_id': fake_neutron_net_id,
                'ip_version': 4,
                'cidr': fake_cidr_v4,
                'enable_dhcp': mock_tag.enable_dhcp,
                'gateway_ip': '192.168.42.1',
                'subnetpool_id': fake_kuryr_v4_subnetpool_id
            }]
        }
        subnet_v4_id = uuidutils.generate_uuid()
        fake_v4_subnet = self._get_fake_v4_subnet(
            fake_neutron_net_id, subnet_v4_id,
            fake_kuryr_subnetpool_id,
            name=fake_cidr_v4, cidr=fake_cidr_v4)
        fake_cidr_v6 = 'fe80::/64'
        fake_v6_subnet_request = {
            "subnets": [{
                'name': utils.make_subnet_name(fake_cidr_v6),
                'network_id': fake_neutron_net_id,
                'ip_version': 6,
                'cidr': fake_cidr_v6,
                'enable_dhcp': mock_tag.enable_dhcp,
                'gateway_ip': 'fe80::f816:3eff:fe20:57c3',
                'subnetpool_id': fake_kuryr_v6_subnetpool_id
            }]
        }

        subnet_v6_id = uuidutils.generate_uuid()
        fake_v6_subnet = self._get_fake_v6_subnet(
            fake_neutron_net_id, subnet_v6_id,
            fake_kuryr_v6_subnetpool_id,
            name=fake_cidr_v6, cidr=fake_cidr_v6)
        fake_v4_v6_subnets_response = {
            'subnets': [
                fake_v4_subnet['subnet'],
                fake_v6_subnet['subnet']
            ]
        }
        mock_create_subnet.return_value = fake_v4_v6_subnets_response

        response = self.app.post('/NetworkDriver.CreateNetwork',
                                 content_type='application/json',
                                 data=jsonutils.dumps(network_request))

        self.assertEqual(200, response.status_code)
        mock_list_subnetpools.assert_any_call(name=fake_v4_pool_name)
        mock_list_subnetpools.assert_any_call(name=fake_v6_pool_name)
        mock_list_networks.assert_called_with(id=fake_neutron_net_id)
        mock_list_subnets.assert_any_call(
            network_id=fake_neutron_net_id,
            cidr=fake_cidr_v4)
        mock_list_subnets.assert_any_call(network_id=fake_neutron_net_id,
            cidr=fake_cidr_v6)
        mock_create_subnet.assert_any_call(fake_v4_subnet_request)
        mock_create_subnet.assert_any_call(fake_v6_subnet_request)
        if mock_tag.tag:
            tags = utils.create_net_tags(docker_network_id)
            for tag in tags:
                mock_add_tag.assert_any_call('networks',
                    fake_neutron_net_id, tag)
            mock_add_tag.assert_any_call('networks', fake_neutron_net_id,
                utils.existing_net_tag(docker_network_id))
        else:
            mock_update_network.assert_called_with(
                fake_neutron_net_id, {'network':
                {'name': docker_network_id}})

        decoded_json = jsonutils.loads(response.data)
        self.assertEqual(const.SCHEMA['SUCCESS'], decoded_json)
    def test_create_network_and_subnet_pre_existing_add_tag_for_subnet(
            self, use_tags, mock_tag, mock_show_extension,
            mock_list_subnetpools, mock_list_networks,
            mock_add_tag, mock_update_network,
            mock_list_subnets):
        if use_tags:
            mock_tag.tag = use_tags

        fake_tag_extension = {
            "extension":
            {"alias": "tag", "updated": "mock_time",
             "name": "Tag support", "links": [],
             "description": "mock tag on resources ['subnet', 'network']."}}
        mock_show_extension.return_value = fake_tag_extension

        docker_network_id, fake_neutron_net_id, fake_response = self._ids()

        network_request = {
            'NetworkID': docker_network_id,
            'IPv4Data': [{
                'AddressSpace': 'foo',
                'Pool': '192.168.42.0/24',
                'Gateway': '192.168.42.1/24',
            }],
            'IPv6Data': [{
                'AddressSpace': 'bar',
                'Pool': 'fe80::/64',
                'Gateway': 'fe80::f816:3eff:fe20:57c3/64',
            }],
            'Options': {
                const.NETWORK_GENERIC_OPTIONS: {
                    const.NEUTRON_UUID_OPTION: fake_neutron_net_id
                }
            }
        }

        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)
        mock_list_subnetpools.side_effect = [
            {'subnetpools': kuryr_v4_subnetpools['subnetpools']},
            {'subnetpools': kuryr_v6_subnetpools['subnetpools']}
        ]

        subnet_v4_id = uuidutils.generate_uuid()
        subnet_v6_id = uuidutils.generate_uuid()
        fake_cidr_v4 = '192.168.42.0/24'
        fake_cidr_v6 = 'fe80::/64'
        fake_v4_subnet = self._get_fake_v4_subnet(
            fake_neutron_net_id,
            docker_endpoint_id="fake_id",
            subnet_v4_id=subnet_v4_id,
            subnetpool_id=fake_kuryr_v4_subnetpool_id,
            tag_subnetpool_id=False,
            cidr=fake_cidr_v4)
        fake_v6_subnet = self._get_fake_v6_subnet(
            fake_neutron_net_id,
            docker_endpoint_id="fake_id",
            subnet_v6_id=subnet_v6_id,
            subnetpool_id=fake_kuryr_v6_subnetpool_id,
            cidr=fake_cidr_v6)

        fake_existing_subnets_response = [
            {'subnets': [fake_v4_subnet['subnet']]},
            {'subnets': [fake_v6_subnet['subnet']]}
        ]

        mock_list_subnets.side_effect = fake_existing_subnets_response
        fake_response['networks'][0]['subnets'].append(subnet_v4_id)
        fake_response['networks'][0]['subnets'].append(subnet_v6_id)
        mock_list_networks.return_value = fake_response

        def mock_exception(*args, **kwargs):
            if 'subnet' not in kwargs['extension']['description']:
                return n_exceptions.NotFound

        mock_add_tag.side_effect = mock_exception(**fake_tag_extension)
        response = self.app.post('/NetworkDriver.CreateNetwork',
                                 content_type='application/json',
                                 data=jsonutils.dumps(network_request))

        self.assertEqual(200, response.status_code)
        mock_list_subnetpools.assert_any_call(name=fake_v4_pool_name)
        mock_list_subnetpools.assert_any_call(name=fake_v6_pool_name)
        mock_list_networks.assert_called_with(id=fake_neutron_net_id)
        mock_list_subnets.assert_any_call(
            network_id=fake_neutron_net_id,
            cidr=fake_cidr_v4)
        mock_list_subnets.assert_any_call(
            network_id=fake_neutron_net_id,
            cidr=fake_cidr_v6)

        if mock_tag.tag:
            tags = utils.create_net_tags(docker_network_id)
            for tag in tags:
                mock_add_tag.assert_any_call(
                    'networks', fake_neutron_net_id, tag)
            mock_add_tag.assert_any_call('networks', fake_neutron_net_id,
                utils.existing_net_tag(docker_network_id))
            mock_add_tag.assert_any_call('subnets',
                                         subnet_v4_id,
                                         fake_kuryr_v4_subnetpool_id)
            mock_add_tag.assert_any_call('subnets',
                                         subnet_v6_id,
                                         fake_kuryr_v6_subnetpool_id)
        else:
            mock_update_network.assert_called_with(
                fake_neutron_net_id, {'network':
                {'name': docker_network_id}})

        decoded_json = jsonutils.loads(response.data)
        self.assertEqual(const.SCHEMA['SUCCESS'], decoded_json)
    def test_create_network_pre_existing(self, use_tags,
            mock_tag, mock_list_subnetpools, mock_list_networks,
            mock_add_tag, mock_update_network,
            mock_list_subnets, mock_create_subnet):
        if not use_tags:
            mock_tag.tag = use_tags

        docker_network_id, fake_neutron_net_id, fake_response = self._ids()
        fake_kuryr_subnetpool_id = uuidutils.generate_uuid()

        network_request = {
            'NetworkID': docker_network_id,
            'IPv4Data': [{
                'AddressSpace': 'foo',
                'Pool': '192.168.42.0/24',
                'Gateway': '192.168.42.1/24',
            }],
            'IPv6Data': [{
                'AddressSpace': 'bar',
                'Pool': 'fe80::/64',
                'Gateway': 'fe80::f816:3eff:fe20:57c3/64',
            }],
            'Options': {
                const.NETWORK_GENERIC_OPTIONS: {
                    const.NEUTRON_UUID_OPTION: fake_neutron_net_id
                }
            }
        }

        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)
        mock_list_subnetpools.side_effect = [
            {'subnetpools': kuryr_v4_subnetpools['subnetpools']},
            {'subnetpools': kuryr_v6_subnetpools['subnetpools']}
        ]
        mock_list_networks.return_value = fake_response

        fake_existing_subnets_response = {
            "subnets": []
        }
        fake_cidr_v4 = '192.168.42.0/24'
        mock_list_subnets.return_value = fake_existing_subnets_response

        fake_v4_subnet_request = {
            "subnets": [{
                'name': utils.make_subnet_name(fake_cidr_v4),
                'network_id': fake_neutron_net_id,
                'ip_version': 4,
                'cidr': fake_cidr_v4,
                'enable_dhcp': mock_tag.enable_dhcp,
                'gateway_ip': '192.168.42.1',
                'subnetpool_id': fake_kuryr_v4_subnetpool_id
            }]
        }
        subnet_v4_id = uuidutils.generate_uuid()
        fake_v4_subnet = self._get_fake_v4_subnet(
            fake_neutron_net_id, subnet_v4_id,
            fake_kuryr_subnetpool_id,
            name=fake_cidr_v4, cidr=fake_cidr_v4)
        fake_cidr_v6 = 'fe80::/64'
        fake_v6_subnet_request = {
            "subnets": [{
                'name': utils.make_subnet_name(fake_cidr_v6),
                'network_id': fake_neutron_net_id,
                'ip_version': 6,
                'cidr': fake_cidr_v6,
                'enable_dhcp': mock_tag.enable_dhcp,
                'gateway_ip': 'fe80::f816:3eff:fe20:57c3',
                'subnetpool_id': fake_kuryr_v6_subnetpool_id
            }]
        }

        subnet_v6_id = uuidutils.generate_uuid()
        fake_v6_subnet = self._get_fake_v6_subnet(
            fake_neutron_net_id, subnet_v6_id,
            fake_kuryr_v6_subnetpool_id,
            name=fake_cidr_v6, cidr=fake_cidr_v6)
        fake_v4_v6_subnets_response = {
            'subnets': [
                fake_v4_subnet['subnet'],
                fake_v6_subnet['subnet']
            ]
        }
        mock_create_subnet.return_value = fake_v4_v6_subnets_response

        response = self.app.post('/NetworkDriver.CreateNetwork',
                                 content_type='application/json',
                                 data=jsonutils.dumps(network_request))

        self.assertEqual(200, response.status_code)
        mock_list_subnetpools.assert_any_call(name=fake_v4_pool_name)
        mock_list_subnetpools.assert_any_call(name=fake_v6_pool_name)
        mock_list_networks.assert_called_with(id=fake_neutron_net_id)
        mock_list_subnets.assert_any_call(
            network_id=fake_neutron_net_id,
            cidr=fake_cidr_v4)
        mock_list_subnets.assert_any_call(network_id=fake_neutron_net_id,
            cidr=fake_cidr_v6)
        mock_create_subnet.assert_any_call(fake_v4_subnet_request)
        mock_create_subnet.assert_any_call(fake_v6_subnet_request)
        if mock_tag.tag:
            tags = utils.create_net_tags(docker_network_id)
            for tag in tags:
                mock_add_tag.assert_any_call('networks',
                    fake_neutron_net_id, tag)
            mock_add_tag.assert_any_call('networks', fake_neutron_net_id,
                utils.existing_net_tag(docker_network_id))
        else:
            mock_update_network.assert_called_with(
                fake_neutron_net_id, {'network':
                {'name': docker_network_id}})

        decoded_json = jsonutils.loads(response.data)
        self.assertEqual(const.SCHEMA['SUCCESS'], decoded_json)
    def test_create_network_and_subnet_pre_existing_add_tag_for_subnet(
            self, use_tags, mock_tag, mock_show_extension,
            mock_list_subnetpools, mock_list_networks,
            mock_add_tag, mock_update_network,
            mock_list_subnets):
        if use_tags:
            mock_tag.tag = use_tags

        fake_tag_extension = {
            "extension":
            {"alias": "tag", "updated": "mock_time",
             "name": "Tag support", "links": [],
             "description": "mock tag on resources ['subnet', 'network']."}}
        mock_show_extension.return_value = fake_tag_extension

        docker_network_id, fake_neutron_net_id, fake_response = self._ids()

        network_request = {
            'NetworkID': docker_network_id,
            'IPv4Data': [{
                'AddressSpace': 'foo',
                'Pool': '192.168.42.0/24',
                'Gateway': '192.168.42.1/24',
            }],
            'IPv6Data': [{
                'AddressSpace': 'bar',
                'Pool': 'fe80::/64',
                'Gateway': 'fe80::f816:3eff:fe20:57c3/64',
            }],
            'Options': {
                const.NETWORK_GENERIC_OPTIONS: {
                    const.NEUTRON_UUID_OPTION: fake_neutron_net_id
                }
            }
        }

        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)
        mock_list_subnetpools.side_effect = [
            {'subnetpools': kuryr_v4_subnetpools['subnetpools']},
            {'subnetpools': kuryr_v6_subnetpools['subnetpools']}
        ]

        subnet_v4_id = uuidutils.generate_uuid()
        subnet_v6_id = uuidutils.generate_uuid()
        fake_cidr_v4 = '192.168.42.0/24'
        fake_cidr_v6 = 'fe80::/64'
        fake_v4_subnet = self._get_fake_v4_subnet(
            fake_neutron_net_id,
            docker_endpoint_id="fake_id",
            subnet_v4_id=subnet_v4_id,
            subnetpool_id=fake_kuryr_v4_subnetpool_id,
            tag_subnetpool_id=False,
            cidr=fake_cidr_v4)
        fake_v6_subnet = self._get_fake_v6_subnet(
            fake_neutron_net_id,
            docker_endpoint_id="fake_id",
            subnet_v6_id=subnet_v6_id,
            subnetpool_id=fake_kuryr_v6_subnetpool_id,
            cidr=fake_cidr_v6)

        fake_existing_subnets_response = [
            {'subnets': [fake_v4_subnet['subnet']]},
            {'subnets': [fake_v6_subnet['subnet']]}
        ]

        mock_list_subnets.side_effect = fake_existing_subnets_response
        fake_response['networks'][0]['subnets'].append(subnet_v4_id)
        fake_response['networks'][0]['subnets'].append(subnet_v6_id)
        mock_list_networks.return_value = fake_response

        def mock_exception(*args, **kwargs):
            if 'subnet' not in kwargs['extension']['description']:
                return n_exceptions.NotFound

        mock_add_tag.side_effect = mock_exception(**fake_tag_extension)
        response = self.app.post('/NetworkDriver.CreateNetwork',
                                 content_type='application/json',
                                 data=jsonutils.dumps(network_request))

        self.assertEqual(200, response.status_code)
        mock_list_subnetpools.assert_any_call(name=fake_v4_pool_name)
        mock_list_subnetpools.assert_any_call(name=fake_v6_pool_name)
        mock_list_networks.assert_called_with(id=fake_neutron_net_id)
        mock_list_subnets.assert_any_call(
            network_id=fake_neutron_net_id,
            cidr=fake_cidr_v4)
        mock_list_subnets.assert_any_call(
            network_id=fake_neutron_net_id,
            cidr=fake_cidr_v6)

        if mock_tag.tag:
            tags = utils.create_net_tags(docker_network_id)
            for tag in tags:
                mock_add_tag.assert_any_call(
                    'networks', fake_neutron_net_id, tag)
            mock_add_tag.assert_any_call('networks', fake_neutron_net_id,
                utils.existing_net_tag(docker_network_id))
            mock_add_tag.assert_any_call('subnets',
                                         subnet_v4_id,
                                         fake_kuryr_v4_subnetpool_id)
            mock_add_tag.assert_any_call('subnets',
                                         subnet_v6_id,
                                         fake_kuryr_v6_subnetpool_id)
        else:
            mock_update_network.assert_called_with(
                fake_neutron_net_id, {'network':
                {'name': docker_network_id}})

        decoded_json = jsonutils.loads(response.data)
        self.assertEqual(const.SCHEMA['SUCCESS'], decoded_json)