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_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_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 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)