示例#1
0
    def test_create_vpn_gateway(self):
        self.db_api.add_item.side_effect = (tools.get_db_api_add_item(
            fakes.ID_EC2_VPN_GATEWAY_2))

        resp = self.execute('CreateVpnGateway', {'Type': 'ipsec.1'})
        self.assertEqual({'vpnGateway': fakes.EC2_VPN_GATEWAY_2}, resp)
        self.db_api.add_item.assert_called_once_with(mock.ANY, 'vgw', {})
示例#2
0
    def test_create_vpn_connection_rollback(self):
        self.set_mock_db_items(fakes.DB_VPN_GATEWAY_1,
                               fakes.DB_CUSTOMER_GATEWAY_1)
        self.neutron.create_ikepolicy.side_effect = (tools.get_neutron_create(
            'ikepolicy', fakes.ID_OS_IKEPOLICY_1))
        self.neutron.create_ipsecpolicy.side_effect = (
            tools.get_neutron_create('ipsecpolicy', fakes.ID_OS_IPSECPOLICY_1))
        self.db_api.add_item.side_effect = (tools.get_db_api_add_item(
            fakes.ID_EC2_VPN_CONNECTION_1))
        self.neutron.update_ikepolicy.side_effect = Exception()

        self.assert_execution_error(
            self.ANY_EXECUTE_ERROR, 'CreateVpnConnection', {
                'VpnGatewayId': fakes.ID_EC2_VPN_GATEWAY_1,
                'CustomerGatewayId': fakes.ID_EC2_CUSTOMER_GATEWAY_1,
                'Type': 'ipsec.1',
                'Options.StaticRoutesOnly': 'True'
            })

        self.db_api.delete_item.assert_called_once_with(
            mock.ANY, fakes.ID_EC2_VPN_CONNECTION_1)
        self.neutron.delete_ipsecpolicy.assert_called_once_with(
            fakes.ID_OS_IPSECPOLICY_1)
        self.neutron.delete_ikepolicy.assert_called_once_with(
            fakes.ID_OS_IKEPOLICY_1)
示例#3
0
    def test_register_image_by_s3(self, s3_create):
        s3_create.return_value = fakes.OSImage(fakes.OS_IMAGE_1)
        self.db_api.add_item.side_effect = (tools.get_db_api_add_item(
            fakes.ID_EC2_IMAGE_1))

        resp = self.execute('RegisterImage',
                            {'ImageLocation': fakes.LOCATION_IMAGE_1})
        self.assertThat(
            resp, matchers.DictMatches({'imageId': fakes.ID_EC2_IMAGE_1}))

        s3_create.assert_called_once_with(
            mock.ANY, {
                'name': fakes.LOCATION_IMAGE_1,
                'properties': {
                    'image_location': fakes.LOCATION_IMAGE_1
                }
            })
        s3_create.reset_mock()

        resp = self.execute('RegisterImage', {
            'ImageLocation': fakes.LOCATION_IMAGE_1,
            'Name': 'an image name'
        })
        self.assertThat(
            resp, matchers.DictMatches({'imageId': fakes.ID_EC2_IMAGE_1}))

        s3_create.assert_called_once_with(
            mock.ANY, {
                'name': 'an image name',
                'properties': {
                    'image_location': fakes.LOCATION_IMAGE_1
                }
            })
    def test_create_volume_from_snapshot(self):
        self.cinder.volumes.create.return_value = (fakes.OSVolume(
            fakes.OS_VOLUME_3))
        self.db_api.add_item.side_effect = (tools.get_db_api_add_item(
            fakes.ID_EC2_VOLUME_3))
        self.set_mock_db_items(fakes.DB_SNAPSHOT_1)

        resp = self.execute(
            'CreateVolume', {
                'AvailabilityZone': fakes.NAME_AVAILABILITY_ZONE,
                'SnapshotId': fakes.ID_EC2_SNAPSHOT_1
            })
        self.assertThat(fakes.EC2_VOLUME_3, matchers.DictMatches(resp))
        self.db_api.add_item.assert_called_once_with(mock.ANY,
                                                     'vol',
                                                     tools.purge_dict(
                                                         fakes.DB_VOLUME_3,
                                                         ('id', )),
                                                     project_id=None)

        self.cinder.volumes.create.assert_called_once_with(
            None,
            snapshot_id=fakes.ID_OS_SNAPSHOT_1,
            volume_type=None,
            availability_zone=fakes.NAME_AVAILABILITY_ZONE)
示例#5
0
    def test_register_image_by_s3(self, s3_create):
        s3_create.return_value = fakes.OSImage(fakes.OS_IMAGE_1)
        self.db_api.add_item.side_effect = (
            tools.get_db_api_add_item(fakes.ID_EC2_IMAGE_1))

        resp = self.execute(
            'RegisterImage',
            {'ImageLocation': fakes.LOCATION_IMAGE_1})
        self.assertThat(resp, matchers.DictMatches(
            {'imageId': fakes.ID_EC2_IMAGE_1}))

        s3_create.assert_called_once_with(
            mock.ANY,
            {'name': fakes.LOCATION_IMAGE_1,
             'image_location': fakes.LOCATION_IMAGE_1})
        s3_create.reset_mock()

        resp = self.execute(
            'RegisterImage',
            {'ImageLocation': fakes.LOCATION_IMAGE_1,
             'Name': 'an image name'})
        self.assertThat(resp, matchers.DictMatches(
            {'imageId': fakes.ID_EC2_IMAGE_1}))

        s3_create.assert_called_once_with(
            mock.ANY,
            {'name': 'an image name',
             'image_location': fakes.LOCATION_IMAGE_1})
示例#6
0
    def test_create_subnet(self):
        self.set_mock_db_items(fakes.DB_VPC_1, fakes.DB_ROUTE_TABLE_1)
        self.db_api.add_item.side_effect = (tools.get_db_api_add_item(
            fakes.ID_EC2_SUBNET_1))
        self.neutron.create_network.side_effect = (tools.get_neutron_create(
            'network', fakes.ID_OS_NETWORK_1, {'status': 'available'}))
        self.neutron.create_subnet.side_effect = (tools.get_neutron_create(
            'subnet', fakes.ID_OS_SUBNET_1))
        subnet_1 = tools.purge_dict(fakes.DB_SUBNET_1, ('os_vpnservice_id', ))

        def check_response(resp):
            self.assertThat(fakes.EC2_SUBNET_1,
                            matchers.DictMatches(resp['subnet']))
            self.db_api.add_item.assert_called_once_with(
                mock.ANY, 'subnet', tools.purge_dict(subnet_1, ('id', )))
            self.neutron.create_network.assert_called_once_with(
                {'network': {}})
            self.neutron.update_network.assert_called_once_with(
                fakes.ID_OS_NETWORK_1,
                {'network': {
                    'name': fakes.ID_EC2_SUBNET_1
                }})
            self.neutron.create_subnet.assert_called_once_with({
                'subnet':
                tools.purge_dict(fakes.OS_SUBNET_1,
                                 ('id', 'name', 'gateway_ip'))
            })
            self.neutron.update_subnet.assert_called_once_with(
                fakes.ID_OS_SUBNET_1, {
                    'subnet': {
                        'name': fakes.ID_EC2_SUBNET_1,
                        'gateway_ip': None
                    }
                })
            self.neutron.add_interface_router.assert_called_once_with(
                fakes.ID_OS_ROUTER_1, {'subnet_id': fakes.ID_OS_SUBNET_1})
            self.vpn_gateway_api._start_vpn_in_subnet.assert_called_once_with(
                mock.ANY, self.neutron, mock.ANY, subnet_1, fakes.DB_VPC_1,
                fakes.DB_ROUTE_TABLE_1)
            self.assertIsInstance(
                self.vpn_gateway_api._start_vpn_in_subnet.call_args[0][2],
                common.OnCrashCleaner)

        resp = self.execute('CreateSubnet', {
            'VpcId': fakes.ID_EC2_VPC_1,
            'CidrBlock': fakes.CIDR_SUBNET_1
        })
        check_response(resp)

        self.neutron.reset_mock()
        self.db_api.reset_mock()
        self.vpn_gateway_api.reset_mock()

        resp = self.execute(
            'CreateSubnet', {
                'VpcId': fakes.ID_EC2_VPC_1,
                'CidrBlock': fakes.CIDR_SUBNET_1,
                'AvailabilityZone': 'nova'
            })
        check_response(resp)
示例#7
0
        def _prepare_and_check(vpc=None, ec2_vpc=None, route_table=None):
            self.neutron.create_router.side_effect = (tools.get_neutron_create(
                'router', vpc['os_id']))
            self.db_api.add_item.side_effect = (tools.get_db_api_add_item(
                {'vpc': vpc['id']}))
            self.db_api.set_mock_items(vpc)
            create_route_table.return_value = route_table

            resp = vpc_api._create_vpc(self.context, vpc['cidr_block'],
                                       vpc['is_default'])

            # Check creation of vpc
            self.neutron.create_router.assert_called_with({'router': {}})
            self.neutron.update_router.assert_called_once_with(
                vpc['os_id'], {'router': {
                    'name': ec2_vpc['vpcId']
                }})
            self.db_api.add_item.assert_called_once_with(
                mock.ANY, 'vpc',
                tools.purge_dict(vpc, ('id', 'vpc_id', 'route_table_id')))
            self.db_api.update_item.assert_called_once_with(mock.ANY, vpc)

            create_route_table.assert_called_once_with(mock.ANY, vpc)
            create_default_security_group.assert_called_once_with(
                mock.ANY, vpc)
    def test_register_image_by_bdm(self, get_os_image):
        self.glance.images.create.return_value = (fakes.OSImage(
            fakes.OS_IMAGE_2))
        self.db_api.add_item.side_effect = (tools.get_db_api_add_item(
            fakes.ID_EC2_IMAGE_2))
        self.set_mock_db_items(fakes.DB_SNAPSHOT_1, fakes.DB_IMAGE_AKI_1,
                               fakes.DB_IMAGE_ARI_1)
        get_os_image.side_effect = [
            fakes.OSImage(fakes.OS_IMAGE_AKI_1),
            fakes.OSImage(fakes.OS_IMAGE_ARI_1)
        ]

        resp = self.execute(
            'RegisterImage', {
                'RootDeviceName': fakes.ROOT_DEVICE_NAME_IMAGE_2,
                'Name': 'fake_name',
                'KernelId': fakes.ID_EC2_IMAGE_AKI_1,
                'RamdiskId': fakes.ID_EC2_IMAGE_ARI_1,
                'BlockDeviceMapping.1.DeviceName':
                fakes.ROOT_DEVICE_NAME_IMAGE_2,
                'BlockDeviceMapping.1.Ebs.SnapshotId': fakes.ID_EC2_SNAPSHOT_1
            })
        self.assertThat(
            resp, matchers.DictMatches({'imageId': fakes.ID_EC2_IMAGE_2}))
        self.db_api.add_item.assert_called_once_with(
            mock.ANY,
            'ami', {
                'os_id': fakes.ID_OS_IMAGE_2,
                'is_public': False,
                'description': None
            },
            project_id=None)
        self.assertEqual(1, self.glance.images.create.call_count)
        self.assertEqual((), self.glance.images.create.call_args[0])
        self.assertIn('properties', self.glance.images.create.call_args[1])
        self.assertIsInstance(
            self.glance.images.create.call_args[1]['properties'], dict)
        bdm = self.glance.images.create.call_args[1]['properties'].pop(
            'block_device_mapping', None)
        self.assertEqual(
            {
                'is_public': False,
                'size': 0,
                'name': 'fake_name',
                'properties': {
                    'root_device_name': fakes.ROOT_DEVICE_NAME_IMAGE_2,
                    'kernel_id': fakes.ID_OS_IMAGE_AKI_1,
                    'ramdisk_id': fakes.ID_OS_IMAGE_ARI_1
                }
            }, self.glance.images.create.call_args[1])
        self.assertEqual([{
            'device_name': fakes.ROOT_DEVICE_NAME_IMAGE_2,
            'delete_on_termination': True,
            'snapshot_id': fakes.ID_OS_SNAPSHOT_1
        }], json.loads(bdm))
        get_os_image.assert_has_calls([
            mock.call(mock.ANY, fakes.ID_EC2_IMAGE_AKI_1),
            mock.call(mock.ANY, fakes.ID_EC2_IMAGE_ARI_1)
        ])
    def test_create_vpn_gateway(self):
        self.db_api.add_item.side_effect = (
            tools.get_db_api_add_item(fakes.ID_EC2_VPN_GATEWAY_2))

        resp = self.execute('CreateVpnGateway',
                            {'Type': 'ipsec.1'})
        self.assertEqual({'vpnGateway': fakes.EC2_VPN_GATEWAY_2}, resp)
        self.db_api.add_item.assert_called_once_with(mock.ANY, 'vgw', {})
    def test_describe_volumes(self):
        self.cinder.volumes.list.return_value = [
            fakes.OSVolume(fakes.OS_VOLUME_1),
            fakes.OSVolume(fakes.OS_VOLUME_2),
            fakes.OSVolume(fakes.OS_VOLUME_3)
        ]

        self.set_mock_db_items(fakes.DB_VOLUME_1, fakes.DB_VOLUME_2,
                               fakes.DB_INSTANCE_1, fakes.DB_INSTANCE_2,
                               fakes.DB_SNAPSHOT_1, fakes.DB_SNAPSHOT_2)
        self.db_api.add_item.side_effect = (tools.get_db_api_add_item(
            fakes.ID_EC2_VOLUME_3))

        resp = self.execute('DescribeVolumes', {})
        self.assertThat(
            resp,
            matchers.DictMatches(
                {
                    'volumeSet': [
                        fakes.EC2_VOLUME_1, fakes.EC2_VOLUME_2,
                        fakes.EC2_VOLUME_3
                    ]
                },
                orderless_lists=True))

        self.db_api.get_items.assert_any_call(mock.ANY, 'vol')

        self.db_api.get_items_by_ids = tools.CopyingMock(
            return_value=[fakes.DB_VOLUME_1])
        resp = self.execute('DescribeVolumes',
                            {'VolumeId.1': fakes.ID_EC2_VOLUME_1})
        self.assertThat(
            resp,
            matchers.DictMatches({'volumeSet': [fakes.EC2_VOLUME_1]},
                                 orderless_lists=True))
        self.db_api.get_items_by_ids.assert_any_call(
            mock.ANY, set([fakes.ID_EC2_VOLUME_1]))

        self.check_filtering(
            'DescribeVolumes',
            'volumeSet',
            [
                ('availability-zone', fakes.NAME_AVAILABILITY_ZONE),
                ('create-time', fakes.TIME_CREATE_VOLUME_2),
                ('encrypted', False),
                # TODO(ft): declare a constant for the volume size in fakes
                ('size', 1),
                ('snapshot-id', fakes.ID_EC2_SNAPSHOT_1),
                ('status', 'available'),
                ('volume-id', fakes.ID_EC2_VOLUME_1),
                # TODO(ft): support filtering by none/empty value
                # ('volume-type', ''),
                ('attachment.device', fakes.ROOT_DEVICE_NAME_INSTANCE_2),
                ('attachment.instance-id', fakes.ID_EC2_INSTANCE_2),
                ('attachment.status', 'attached')
            ])
        self.check_tag_support('DescribeVolumes', 'volumeSet',
                               fakes.ID_EC2_VOLUME_1, 'volumeId')
示例#11
0
    def test_describe_volumes(self):
        self.cinder.volumes.list.return_value = [
            fakes.OSVolume(fakes.OS_VOLUME_1),
            fakes.OSVolume(fakes.OS_VOLUME_2),
            fakes.OSVolume(fakes.OS_VOLUME_3),
        ]
        self.nova_admin.servers.list.return_value = [
            fakes.OSInstance_full(fakes.OS_INSTANCE_1),
            fakes.OSInstance_full(fakes.OS_INSTANCE_2),
        ]

        self.set_mock_db_items(
            fakes.DB_VOLUME_1,
            fakes.DB_VOLUME_2,
            fakes.DB_INSTANCE_1,
            fakes.DB_INSTANCE_2,
            fakes.DB_SNAPSHOT_1,
            fakes.DB_SNAPSHOT_2,
        )
        self.db_api.add_item.side_effect = tools.get_db_api_add_item(fakes.ID_EC2_VOLUME_3)

        resp = self.execute("DescribeVolumes", {})
        self.assertThat(
            resp,
            matchers.DictMatches(
                {"volumeSet": [fakes.EC2_VOLUME_1, fakes.EC2_VOLUME_2, fakes.EC2_VOLUME_3]}, orderless_lists=True
            ),
        )

        self.db_api.get_items.assert_any_call(mock.ANY, "vol")

        self.db_api.get_items_by_ids = tools.CopyingMock(return_value=[fakes.DB_VOLUME_1])
        resp = self.execute("DescribeVolumes", {"VolumeId.1": fakes.ID_EC2_VOLUME_1})
        self.assertThat(resp, matchers.DictMatches({"volumeSet": [fakes.EC2_VOLUME_1]}, orderless_lists=True))
        self.db_api.get_items_by_ids.assert_any_call(mock.ANY, set([fakes.ID_EC2_VOLUME_1]))

        self.check_filtering(
            "DescribeVolumes",
            "volumeSet",
            [
                ("availability-zone", fakes.NAME_AVAILABILITY_ZONE),
                ("create-time", fakes.TIME_CREATE_VOLUME_2),
                ("encrypted", False),
                # TODO(ft): declare a constant for the volume size in fakes
                ("size", 1),
                ("snapshot-id", fakes.ID_EC2_SNAPSHOT_1),
                ("status", "available"),
                ("volume-id", fakes.ID_EC2_VOLUME_1),
                # TODO(ft): support filtering by none/empty value
                # ('volume-type', ''),
                ("attachment.delete-on-termination", False),
                ("attachment.device", fakes.ROOT_DEVICE_NAME_INSTANCE_2),
                ("attachment.instance-id", fakes.ID_EC2_INSTANCE_2),
                ("attachment.status", "attached"),
            ],
        )
        self.check_tag_support("DescribeVolumes", "volumeSet", fakes.ID_EC2_VOLUME_1, "volumeId")
示例#12
0
    def _test_create_image(self, instance_status, no_reboot, is_ebs_instance):
        self.set_mock_db_items(fakes.DB_INSTANCE_2)
        os_instance = mock.MagicMock()
        os_instance.configure_mock(id=fakes.ID_OS_INSTANCE_2,
                                   status=instance_status)
        stop_called = iter([False, True])
        os_instance.stop.side_effect = lambda: next(stop_called)
        os_instance.get.side_effect = lambda: (setattr(os_instance, 'status',
                                                       'SHUTOFF')
                                               if next(stop_called) else None)
        image_id = fakes.random_ec2_id('ami')
        os_image_id = fakes.random_os_id()
        os_instance.create_image.return_value = os_image_id
        self.glance.images.get.return_value = fakes.OSImage(
            {'id': os_image_id},
            from_get=True)
        self.nova.servers.get.return_value = os_instance
        is_ebs_instance.return_value = True
        self.db_api.add_item.side_effect = tools.get_db_api_add_item(image_id)

        resp = self.execute('CreateImage',
                            {'InstanceId': fakes.ID_EC2_INSTANCE_2,
                             'Name': 'fake_name',
                             'Description': 'fake desc',
                             'NoReboot': str(no_reboot)})
        self.assertEqual({'imageId': image_id},
                         resp)
        self.db_api.get_item_by_id.assert_called_once_with(
            mock.ANY, fakes.ID_EC2_INSTANCE_2)
        self.nova.servers.get.assert_called_once_with(fakes.ID_OS_INSTANCE_2)
        is_ebs_instance.assert_called_once_with(mock.ANY, os_instance.id)
        expected_image = {'is_public': False,
                          'description': 'fake desc'}
        if no_reboot:
            expected_image['os_id'] = os_image_id
        self.db_api.add_item.assert_called_once_with(
            mock.ANY, 'ami', expected_image)
        if not no_reboot:
            eventlet.sleep()
        if not no_reboot:
            os_instance.stop.assert_called_once_with()
            os_instance.get.assert_called_once_with()
            os_instance.start.assert_called_once_with()
        if no_reboot:
            os_instance.create_image.assert_called_once_with('fake_name')
        else:
            os_instance.create_image.assert_called_once_with(
                'fake_name', metadata={'ec2_id': image_id})
            self.db_api.update_item.assert_called_once_with(
                mock.ANY, {'id': image_id,
                           'is_public': False,
                           'description': 'fake desc',
                           'os_id': os_image_id,
                           'vpc_id': None})

        self.db_api.reset_mock()
        self.nova.servers.reset_mock()
示例#13
0
    def _test_create_image(self, instance_status, no_reboot, is_ebs_instance):
        self.set_mock_db_items(fakes.DB_INSTANCE_2)
        os_instance = mock.MagicMock()
        os_instance.configure_mock(id=fakes.ID_OS_INSTANCE_2,
                                   status=instance_status)
        stop_called = iter([False, True])
        os_instance.stop.side_effect = lambda: next(stop_called)
        os_instance.get.side_effect = lambda: (setattr(
            os_instance, 'status', 'SHUTOFF') if next(stop_called) else None)
        image_id = fakes.random_ec2_id('ami')
        os_image_id = fakes.random_os_id()
        os_instance.create_image.return_value = os_image_id
        self.glance.images.get.return_value = fakes.OSImage(
            {'id': os_image_id}, from_get=True)
        self.nova.servers.get.return_value = os_instance
        is_ebs_instance.return_value = True
        self.db_api.add_item.side_effect = tools.get_db_api_add_item(image_id)

        resp = self.execute(
            'CreateImage', {
                'InstanceId': fakes.ID_EC2_INSTANCE_2,
                'Name': 'fake_name',
                'Description': 'fake desc',
                'NoReboot': str(no_reboot)
            })
        self.assertEqual({'imageId': image_id}, resp)
        self.db_api.get_item_by_id.assert_called_once_with(
            mock.ANY, fakes.ID_EC2_INSTANCE_2)
        self.nova.servers.get.assert_called_once_with(fakes.ID_OS_INSTANCE_2)
        is_ebs_instance.assert_called_once_with(mock.ANY, os_instance.id)
        expected_image = {'is_public': False, 'description': 'fake desc'}
        if no_reboot:
            expected_image['os_id'] = os_image_id
        self.db_api.add_item.assert_called_once_with(mock.ANY, 'ami',
                                                     expected_image)
        if not no_reboot:
            eventlet.sleep()
        if not no_reboot:
            os_instance.stop.assert_called_once_with()
            os_instance.get.assert_called_once_with()
            os_instance.start.assert_called_once_with()
        if no_reboot:
            os_instance.create_image.assert_called_once_with('fake_name')
        else:
            os_instance.create_image.assert_called_once_with(
                'fake_name', metadata={'ec2_id': image_id})
            self.db_api.update_item.assert_called_once_with(
                mock.ANY, {
                    'id': image_id,
                    'is_public': False,
                    'description': 'fake desc',
                    'os_id': os_image_id,
                    'vpc_id': None
                })

        self.db_api.reset_mock()
        self.nova.servers.reset_mock()
示例#14
0
    def test_create_vpc_overlimit(self):
        self.neutron.create_router.side_effect = (
            neutron_exception.OverQuotaClient)
        self.db_api.add_item.side_effect = tools.get_db_api_add_item(
            fakes.ID_EC2_VPC_1)

        self.assert_execution_error('VpcLimitExceeded', 'CreateVpc',
                                    {'CidrBlock': fakes.CIDR_VPC_1})
        self.neutron.create_router.assert_called_with({'router': {}})
        self.assertEqual(0, self.db_api.add_item.call_count)
示例#15
0
    def test_create_vpc_overlimit(self):
        self.neutron.create_router.side_effect = (
            neutron_exception.OverQuotaClient)
        self.db_api.add_item.side_effect = tools.get_db_api_add_item(
            fakes.ID_EC2_VPC_1)

        self.assert_execution_error('VpcLimitExceeded', 'CreateVpc',
                                    {'CidrBlock': fakes.CIDR_VPC_1})
        self.neutron.create_router.assert_called_with({'router': {}})
        self.assertEqual(0, self.db_api.add_item.call_count)
示例#16
0
    def test_register_image_by_url(self, is_ebs_instance):
        self.set_mock_db_items(fakes.DB_INSTANCE_2)
        is_ebs_instance.return_value = True

        # Setup the mock parameters
        image_id = fakes.random_ec2_id('ami')
        os_image_id = fakes.random_os_id()
        self.glance.images.create.return_value = fakes.OSImage(
            {'id': os_image_id},
            from_get=True)
        self.db_api.add_item.side_effect = tools.get_db_api_add_item(image_id)

        # Setup Import Command
        import_command = 'RegisterImage'

        # Setup the import arguments
        args = {
            'Name': 'TestImage123',
            'ImageLocation':
                fakes.LOCATION_IMAGE_2,
            'Architecture': 'x86_64'
        }

        # Execute the import image process
        resp = self.execute(import_command, args)

        # Assert that the image returned is equal to what was expected
        self.assertEqual({'imageId': image_id}, resp)

        # Assert that Glance Image Create was called
        self.glance.images.create.assert_called_once_with(
            name='TestImage123',
            disk_format='raw',
            container_format='bare',
            visibility='private',
            architecture='x86_64',
            image_location=fakes.LOCATION_IMAGE_2)

        # Assert that Glance Image Import was called
        self.glance.images.image_import.assert_called_once_with(
            os_image_id,
            method='web-download',
            uri=fakes.LOCATION_IMAGE_2)

        # Assert that the image was created
        expected_image = {'is_public': False,
                          'os_id': mock.ANY,
                          'description': None}
        self.db_api.add_item.assert_called_once_with(
            mock.ANY, 'ami', expected_image)

        # Reset all test settings/state
        self.db_api.reset_mock()
        self.glance.reset_mock()
示例#17
0
    def test_create_volume(self):
        self.cinder.volumes.create.return_value = fakes.OSVolume(fakes.OS_VOLUME_1)
        self.db_api.add_item.side_effect = tools.get_db_api_add_item(fakes.ID_EC2_VOLUME_1)

        resp = self.execute("CreateVolume", {"AvailabilityZone": fakes.NAME_AVAILABILITY_ZONE})
        self.assertThat(fakes.EC2_VOLUME_1, matchers.DictMatches(resp))
        self.db_api.add_item.assert_called_once_with(mock.ANY, "vol", tools.purge_dict(fakes.DB_VOLUME_1, ("id",)))

        self.cinder.volumes.create.assert_called_once_with(
            None, snapshot_id=None, volume_type=None, availability_zone=fakes.NAME_AVAILABILITY_ZONE
        )
示例#18
0
    def test_register_image_by_url(self, is_ebs_instance):
        self.set_mock_db_items(fakes.DB_INSTANCE_2)
        is_ebs_instance.return_value = True

        # Setup the mock parameters
        image_id = fakes.random_ec2_id('ami')
        os_image_id = fakes.random_os_id()
        self.glance.images.create.return_value = fakes.OSImage(
            {'id': os_image_id},
            from_get=True)
        self.db_api.add_item.side_effect = tools.get_db_api_add_item(image_id)

        # Setup Import Command
        import_command = 'RegisterImage'

        # Setup the import arguments
        args = {
            'Name': 'TestImage123',
            'ImageLocation':
                fakes.LOCATION_IMAGE_2,
            'Architecture': 'x86_64'
        }

        # Execute the import image process
        resp = self.execute(import_command, args)

        # Assert that the image returned is equal to what was expected
        self.assertEqual({'imageId': image_id}, resp)

        # Assert that Glance Image Create was called
        self.glance.images.create.assert_called_once_with(
            name='TestImage123',
            disk_format='raw',
            container_format='bare',
            visibility='private',
            architecture='x86_64',
            image_location=fakes.LOCATION_IMAGE_2)

        # Assert that Glance Image Import was called
        self.glance.images.image_import.assert_called_once_with(
            os_image_id,
            method='web-download',
            uri=fakes.LOCATION_IMAGE_2)

        # Assert that the image was created
        expected_image = {'is_public': False,
                          'os_id': mock.ANY,
                          'description': None}
        self.db_api.add_item.assert_called_once_with(
            mock.ANY, 'ami', expected_image)

        # Reset all test settings/state
        self.db_api.reset_mock()
        self.glance.reset_mock()
示例#19
0
    def test_create_subnet(self):
        self.set_mock_db_items(fakes.DB_VPC_1, fakes.DB_ROUTE_TABLE_1)
        self.db_api.add_item.side_effect = (tools.get_db_api_add_item(
            fakes.ID_EC2_SUBNET_1))
        self.neutron.create_network.side_effect = (tools.get_neutron_create(
            'network', fakes.ID_OS_NETWORK_1, {'status': 'available'}))
        self.neutron.create_subnet.side_effect = (tools.get_neutron_create(
            'subnet', fakes.ID_OS_SUBNET_1))

        def check_response(resp):
            self.assertThat(fakes.EC2_SUBNET_1,
                            matchers.DictMatches(resp['subnet']))
            self.db_api.add_item.assert_called_once_with(mock.ANY,
                                                         'subnet',
                                                         tools.purge_dict(
                                                             fakes.DB_SUBNET_1,
                                                             ('id', )),
                                                         project_id=None)
            self.neutron.create_network.assert_called_once_with(
                {'network': {}})
            self.neutron.update_network.assert_called_once_with(
                fakes.ID_OS_NETWORK_1,
                {'network': {
                    'name': fakes.ID_EC2_SUBNET_1
                }})
            self.neutron.create_subnet.assert_called_once_with({
                'subnet':
                tools.purge_dict(fakes.OS_SUBNET_1, ('id', 'name'))
            })
            self.neutron.update_subnet.assert_called_once_with(
                fakes.ID_OS_SUBNET_1,
                {'subnet': {
                    'name': fakes.ID_EC2_SUBNET_1
                }})
            self.neutron.add_interface_router.assert_called_once_with(
                fakes.ID_OS_ROUTER_1, {'subnet_id': fakes.ID_OS_SUBNET_1})

        resp = self.execute('CreateSubnet', {
            'VpcId': fakes.ID_EC2_VPC_1,
            'CidrBlock': fakes.CIDR_SUBNET_1
        })
        check_response(resp)

        self.neutron.reset_mock()
        self.db_api.reset_mock()

        resp = self.execute(
            'CreateSubnet', {
                'VpcId': fakes.ID_EC2_VPC_1,
                'CidrBlock': fakes.CIDR_SUBNET_1,
                'AvailabilityZone': 'nova'
            })
        check_response(resp)
    def test_create_vpn_connection(self, random_choice, reset_vpn_connections,
                                   describe_vpn_connections):
        self.set_mock_db_items(
            fakes.DB_VPN_GATEWAY_1, fakes.DB_VPN_GATEWAY_2,
            fakes.DB_CUSTOMER_GATEWAY_1, fakes.DB_CUSTOMER_GATEWAY_2,
            fakes.DB_VPC_1)
        self.neutron.create_ikepolicy.side_effect = (
            tools.get_neutron_create('ikepolicy', fakes.ID_OS_IKEPOLICY_1))
        self.neutron.create_ipsecpolicy.side_effect = (
            tools.get_neutron_create('ipsecpolicy', fakes.ID_OS_IPSECPOLICY_1))
        self.db_api.add_item.side_effect = (
            tools.get_db_api_add_item(fakes.ID_EC2_VPN_CONNECTION_1))
        random_choice.side_effect = iter(fakes.PRE_SHARED_KEY_1)
        describe_vpn_connections.return_value = {
            'vpnConnectionSet': [fakes.EC2_VPN_CONNECTION_1]}

        resp = self.execute(
            'CreateVpnConnection',
            {'VpnGatewayId': fakes.ID_EC2_VPN_GATEWAY_1,
             'CustomerGatewayId': fakes.ID_EC2_CUSTOMER_GATEWAY_1,
             'Type': 'ipsec.1',
             'Options.StaticRoutesOnly': 'True'})
        self.assertThat(
            resp,
            matchers.DictMatches(
                {'vpnConnection': fakes.EC2_VPN_CONNECTION_1}))

        self.neutron.create_ikepolicy.assert_called_once_with(
            {'ikepolicy': tools.purge_dict(fakes.OS_IKEPOLICY_1, ('id',))})
        self.neutron.create_ipsecpolicy.assert_called_once_with(
            {'ipsecpolicy': tools.purge_dict(fakes.OS_IPSECPOLICY_1, ('id',))})
        random_choice.assert_called_with(vpn_connection_api.SHARED_KEY_CHARS)
        new_vpn_connection_1 = tools.update_dict(
            fakes.DB_VPN_CONNECTION_1, {'cidrs': [],
                                        'os_ipsec_site_connections': {}})
        self.db_api.add_item.assert_called_once_with(
            mock.ANY, 'vpn',
            tools.purge_dict(new_vpn_connection_1, ('id', 'vpc_id', 'os_id')))
        self.neutron.update_ikepolicy.assert_called_once_with(
            fakes.ID_OS_IKEPOLICY_1,
            {'ikepolicy': {'name': fakes.ID_EC2_VPN_CONNECTION_1}})
        self.neutron.update_ipsecpolicy.assert_called_once_with(
            fakes.ID_OS_IPSECPOLICY_1,
            {'ipsecpolicy': {'name': fakes.ID_EC2_VPN_CONNECTION_1}})
        reset_vpn_connections.assert_called_once_with(
            mock.ANY, self.neutron, mock.ANY, fakes.DB_VPN_GATEWAY_1,
            vpn_connections=[new_vpn_connection_1])
        self.assertIsInstance(reset_vpn_connections.call_args[0][2],
                              common.OnCrashCleaner)
        describe_vpn_connections.assert_called_once_with(
            mock.ANY, vpn_connection_id=[fakes.ID_EC2_VPN_CONNECTION_1])
示例#21
0
    def test_create_vpc_rollback(self, create_route_table,
                                 create_default_security_group,
                                 create_internet_gateway,
                                 attach_internet_gateway, create_subnet,
                                 create_route, detach_internet_gateway):
        self.configure(disable_ec2_classic=True)

        self.neutron.create_router.side_effect = (tools.get_neutron_create(
            'router', fakes.ID_OS_ROUTER_DEFAULT))

        self.db_api.add_item.side_effect = (tools.get_db_api_add_item(
            {'vpc': fakes.ID_EC2_VPC_DEFAULT}))

        DB_IGW_DEFAULT_DETACHED = ({
            'id': fakes.ID_EC2_IGW_DEFAULT,
            'os_id': None,
            'vpc_id': None
        })
        self.db_api.get_item_by_id.side_effect = (
            tools.get_db_api_get_item_by_id(fakes.DB_VPC_DEFAULT,
                                            fakes.DB_SUBNET_DEFAULT,
                                            fakes.DB_SECURITY_GROUP_DEFAULT,
                                            DB_IGW_DEFAULT_DETACHED))
        create_route_table.return_value = fakes.DB_ROUTE_TABLE_DEFAULT
        create_internet_gateway.return_value = {
            'internetGateway': fakes.EC2_IGW_DEFAULT
        }
        create_subnet.return_value = {'subnet': fakes.EC2_SUBNET_DEFAULT}
        create_default_security_group.return_value = (
            fakes.ID_EC2_SECURITY_GROUP_DEFAULT)

        # exception during attaching internet gateway
        create_route.side_effect = Exception()

        vpc_api._check_and_create_default_vpc(self.context)

        detach_internet_gateway.assert_any_call(mock.ANY,
                                                fakes.ID_EC2_IGW_DEFAULT,
                                                fakes.ID_EC2_VPC_DEFAULT)
        self.db_api.delete_item.assert_any_call(mock.ANY,
                                                fakes.ID_EC2_SUBNET_DEFAULT)
        self.db_api.delete_item.assert_any_call(mock.ANY,
                                                fakes.ID_EC2_IGW_DEFAULT)
        self.neutron.delete_security_group.assert_any_call(
            fakes.ID_OS_SECURITY_GROUP_DEFAULT)
        self.db_api.delete_item.assert_any_call(
            mock.ANY, fakes.ID_EC2_ROUTE_TABLE_DEFAULT)
        self.db_api.delete_item.assert_any_call(mock.ANY,
                                                fakes.ID_EC2_VPC_DEFAULT)
        self.neutron.delete_router.assert_called_once_with(
            fakes.ID_OS_ROUTER_DEFAULT)
示例#22
0
    def test_create_subnet(self):
        self.set_mock_db_items(fakes.DB_VPC_1, fakes.DB_ROUTE_TABLE_1)
        self.db_api.add_item.side_effect = (
                tools.get_db_api_add_item(fakes.ID_EC2_SUBNET_1))
        self.neutron.create_network.side_effect = (
                tools.get_neutron_create('network', fakes.ID_OS_NETWORK_1,
                                         {'status': 'available'}))
        self.neutron.create_subnet.side_effect = (
                tools.get_neutron_create('subnet', fakes.ID_OS_SUBNET_1))
        subnet_1 = tools.purge_dict(fakes.DB_SUBNET_1, ('os_vpnservice_id',))

        def check_response(resp):
            self.assertThat(fakes.EC2_SUBNET_1, matchers.DictMatches(
                    resp['subnet']))
            self.db_api.add_item.assert_called_once_with(
                    mock.ANY, 'subnet',
                    tools.purge_dict(subnet_1, ('id',)))
            self.neutron.create_network.assert_called_once_with(
                    {'network': {}})
            self.neutron.update_network.assert_called_once_with(
                    fakes.ID_OS_NETWORK_1,
                    {'network': {'name': fakes.ID_EC2_SUBNET_1}})
            self.neutron.create_subnet.assert_called_once_with(
                    {'subnet': tools.purge_dict(fakes.OS_SUBNET_1,
                                                ('id', 'name', 'gateway_ip'))})
            self.neutron.update_subnet.assert_called_once_with(
                    fakes.ID_OS_SUBNET_1,
                    {'subnet': {'name': fakes.ID_EC2_SUBNET_1,
                                'gateway_ip': None}})
            self.neutron.add_interface_router.assert_called_once_with(
                    fakes.ID_OS_ROUTER_1,
                    {'subnet_id': fakes.ID_OS_SUBNET_1})
            self.vpn_gateway_api._start_vpn_in_subnet.assert_called_once_with(
                mock.ANY, self.neutron, mock.ANY, subnet_1,
                fakes.DB_VPC_1, fakes.DB_ROUTE_TABLE_1)
            self.assertIsInstance(
                self.vpn_gateway_api._start_vpn_in_subnet.call_args[0][2],
                common.OnCrashCleaner)

        resp = self.execute('CreateSubnet', {'VpcId': fakes.ID_EC2_VPC_1,
                                             'CidrBlock': fakes.CIDR_SUBNET_1})
        check_response(resp)

        self.neutron.reset_mock()
        self.db_api.reset_mock()
        self.vpn_gateway_api.reset_mock()

        resp = self.execute('CreateSubnet', {'VpcId': fakes.ID_EC2_VPC_1,
                                             'CidrBlock': fakes.CIDR_SUBNET_1,
                                             'AvailabilityZone': 'nova'})
        check_response(resp)
示例#23
0
    def test_create_vpn_connection(self, random_choice, reset_vpn_connections, describe_vpn_connections):
        self.set_mock_db_items(
            fakes.DB_VPN_GATEWAY_1,
            fakes.DB_VPN_GATEWAY_2,
            fakes.DB_CUSTOMER_GATEWAY_1,
            fakes.DB_CUSTOMER_GATEWAY_2,
            fakes.DB_VPC_1,
        )
        self.neutron.create_ikepolicy.side_effect = tools.get_neutron_create("ikepolicy", fakes.ID_OS_IKEPOLICY_1)
        self.neutron.create_ipsecpolicy.side_effect = tools.get_neutron_create("ipsecpolicy", fakes.ID_OS_IPSECPOLICY_1)
        self.db_api.add_item.side_effect = tools.get_db_api_add_item(fakes.ID_EC2_VPN_CONNECTION_1)
        random_choice.side_effect = iter(fakes.PRE_SHARED_KEY_1)
        describe_vpn_connections.return_value = {"vpnConnectionSet": [fakes.EC2_VPN_CONNECTION_1]}

        resp = self.execute(
            "CreateVpnConnection",
            {
                "VpnGatewayId": fakes.ID_EC2_VPN_GATEWAY_1,
                "CustomerGatewayId": fakes.ID_EC2_CUSTOMER_GATEWAY_1,
                "Type": "ipsec.1",
                "Options.StaticRoutesOnly": "True",
            },
        )
        self.assertThat(resp, matchers.DictMatches({"vpnConnection": fakes.EC2_VPN_CONNECTION_1}))

        self.neutron.create_ikepolicy.assert_called_once_with(
            {"ikepolicy": tools.purge_dict(fakes.OS_IKEPOLICY_1, ("id",))}
        )
        self.neutron.create_ipsecpolicy.assert_called_once_with(
            {"ipsecpolicy": tools.purge_dict(fakes.OS_IPSECPOLICY_1, ("id",))}
        )
        random_choice.assert_called_with(vpn_connection_api.SHARED_KEY_CHARS)
        new_vpn_connection_1 = tools.update_dict(
            fakes.DB_VPN_CONNECTION_1, {"cidrs": [], "os_ipsec_site_connections": {}}
        )
        self.db_api.add_item.assert_called_once_with(
            mock.ANY, "vpn", tools.purge_dict(new_vpn_connection_1, ("id", "vpc_id", "os_id"))
        )
        self.neutron.update_ikepolicy.assert_called_once_with(
            fakes.ID_OS_IKEPOLICY_1, {"ikepolicy": {"name": fakes.ID_EC2_VPN_CONNECTION_1}}
        )
        self.neutron.update_ipsecpolicy.assert_called_once_with(
            fakes.ID_OS_IPSECPOLICY_1, {"ipsecpolicy": {"name": fakes.ID_EC2_VPN_CONNECTION_1}}
        )
        reset_vpn_connections.assert_called_once_with(
            mock.ANY, self.neutron, mock.ANY, fakes.DB_VPN_GATEWAY_1, vpn_connections=[new_vpn_connection_1]
        )
        self.assertIsInstance(reset_vpn_connections.call_args[0][2], common.OnCrashCleaner)
        describe_vpn_connections.assert_called_once_with(mock.ANY, vpn_connection_id=[fakes.ID_EC2_VPN_CONNECTION_1])
示例#24
0
    def test_create_vpc_invalid_cidr(self):
        self.neutron.create_router.side_effect = (tools.get_neutron_create(
            'router', fakes.ID_OS_ROUTER_1))
        self.db_api.add_item.side_effect = tools.get_db_api_add_item(
            fakes.ID_EC2_VPC_1)

        def do_check(args, error_code):
            self.assert_execution_error(error_code, 'CreateVpc', args)
            self.assertEqual(0, self.neutron.create_router.call_count)

            self.neutron.reset_mock()
            self.db_api.reset_mock()

        do_check({'CidrBlock': 'bad_cidr'}, 'InvalidParameterValue')
        do_check({'CidrBlock': '10.0.0.0/8'}, 'InvalidVpc.Range')
示例#25
0
    def test_create_vpc_invalid_cidr(self):
        self.neutron.create_router.side_effect = (
            tools.get_neutron_create('router', fakes.ID_OS_ROUTER_1))
        self.db_api.add_item.side_effect = tools.get_db_api_add_item(
            fakes.ID_EC2_VPC_1)

        def do_check(args, error_code):
            self.assert_execution_error(error_code, 'CreateVpc', args)
            self.assertEqual(0, self.neutron.create_router.call_count)

            self.neutron.reset_mock()
            self.db_api.reset_mock()

        do_check({'CidrBlock': 'bad_cidr'}, 'InvalidParameterValue')
        do_check({'CidrBlock': '10.0.0.0/8'}, 'InvalidVpc.Range')
示例#26
0
    def test_create_snapshot_from_volume(self):
        self.cinder.volume_snapshots.create.return_value = fakes.OSSnapshot(fakes.OS_SNAPSHOT_1)
        self.db_api.add_item.side_effect = tools.get_db_api_add_item(fakes.ID_EC2_SNAPSHOT_1)
        self.set_mock_db_items(fakes.DB_VOLUME_2)
        self.cinder.volumes.get.side_effect = lambda vol_id: (
            fakes.OSVolume(fakes.OS_VOLUME_2) if vol_id == fakes.ID_OS_VOLUME_2 else None
        )

        resp = self.execute("CreateSnapshot", {"VolumeId": fakes.ID_EC2_VOLUME_2})
        self.assertThat(fakes.EC2_SNAPSHOT_1, matchers.DictMatches(resp))
        self.db_api.add_item.assert_called_once_with(mock.ANY, "snap", tools.purge_dict(fakes.DB_SNAPSHOT_1, ("id",)))

        self.cinder.volume_snapshots.create.assert_called_once_with(
            fakes.ID_OS_VOLUME_2, force=True, display_description=None
        )
示例#27
0
    def test_create_vpc_rollback(self):
        self.neutron.create_router.side_effect = (
            tools.get_neutron_create('router', fakes.ID_OS_ROUTER_1))
        self.db_api.add_item.side_effect = (
            tools.get_db_api_add_item({
                'vpc': fakes.ID_EC2_VPC_1,
                'rtb': fakes.ID_EC2_ROUTE_TABLE_1}))
        self.neutron.update_router.side_effect = Exception()

        self.assert_execution_error(self.ANY_EXECUTE_ERROR, 'CreateVpc',
                                    {'CidrBlock': fakes.CIDR_VPC_1})

        self.neutron.delete_router.assert_called_once_with(
            fakes.ID_OS_ROUTER_1)
        self.db_api.delete_item.assert_any_call(mock.ANY, fakes.ID_EC2_VPC_1)
        self.db_api.delete_item.assert_any_call(mock.ANY,
                                                fakes.ID_EC2_ROUTE_TABLE_1)
示例#28
0
    def test_create_vpc_rollback(self):
        self.neutron.create_router.side_effect = (
            tools.get_neutron_create('router', fakes.ID_OS_ROUTER_1))
        self.db_api.add_item.side_effect = (
            tools.get_db_api_add_item({
                'vpc': fakes.ID_EC2_VPC_1,
                'rtb': fakes.ID_EC2_ROUTE_TABLE_1}))
        self.neutron.update_router.side_effect = Exception()

        self.assert_execution_error(self.ANY_EXECUTE_ERROR, 'CreateVpc',
                                    {'CidrBlock': fakes.CIDR_VPC_1})

        self.neutron.delete_router.assert_called_once_with(
            fakes.ID_OS_ROUTER_1)
        self.db_api.delete_item.assert_any_call(mock.ANY, fakes.ID_EC2_VPC_1)
        self.db_api.delete_item.assert_any_call(mock.ANY,
                                                fakes.ID_EC2_ROUTE_TABLE_1)
示例#29
0
    def test_create_snapshot_from_volume(self):
        self.cinder.volume_snapshots.create.return_value = (fakes.OSSnapshot(
            fakes.OS_SNAPSHOT_1))
        self.db_api.add_item.side_effect = (tools.get_db_api_add_item(
            fakes.ID_EC2_SNAPSHOT_1))
        self.set_mock_db_items(fakes.DB_VOLUME_2)
        self.cinder.volumes.get.side_effect = (
            lambda vol_id: (fakes.OSVolume(fakes.OS_VOLUME_2)
                            if vol_id == fakes.ID_OS_VOLUME_2 else None))

        resp = self.execute('CreateSnapshot',
                            {'VolumeId': fakes.ID_EC2_VOLUME_2})
        self.assertThat(fakes.EC2_SNAPSHOT_1, matchers.DictMatches(resp))
        self.db_api.add_item.assert_called_once_with(
            mock.ANY, 'snap', tools.purge_dict(fakes.DB_SNAPSHOT_1, ('id', )))

        self.cinder.volume_snapshots.create.assert_called_once_with(
            fakes.ID_OS_VOLUME_2, force=True)
示例#30
0
    def test_create_vpc(self):
        self.neutron.create_router.side_effect = (
            tools.get_neutron_create('router', fakes.ID_OS_ROUTER_1))
        self.db_api.add_item.side_effect = (
            tools.get_db_api_add_item({
                'vpc': fakes.ID_EC2_VPC_1,
                'rtb': fakes.ID_EC2_ROUTE_TABLE_1,
                'sg': fakes.ID_EC2_SECURITY_GROUP_1}))
        self.set_mock_db_items(fakes.DB_VPC_1)
        self.nova.security_groups.create.return_value = (
            fakes.NovaSecurityGroup(fakes.NOVA_SECURITY_GROUP_1))

        def check_response(response):
            self.assertIn('vpc', response)
            vpc = resp['vpc']
            self.assertThat(fakes.EC2_VPC_1, matchers.DictMatches(vpc))
            self.neutron.create_router.assert_called_with({'router': {}})
            self.neutron.update_router.assert_called_once_with(
                fakes.ID_OS_ROUTER_1,
                {'router': {'name': fakes.EC2_VPC_1['vpcId']}})
            self.db_api.add_item.assert_any_call(
                mock.ANY, 'vpc',
                tools.purge_dict(fakes.DB_VPC_1,
                                 ('id', 'vpc_id', 'route_table_id')),
                project_id=None)
            self.db_api.add_item.assert_any_call(
                mock.ANY, 'rtb',
                tools.purge_dict(fakes.DB_ROUTE_TABLE_1,
                                 ('id',)),
                project_id=None)
            self.db_api.update_item.assert_called_once_with(
                mock.ANY,
                fakes.DB_VPC_1)

            self.neutron.reset_mock()
            self.db_api.reset_mock()
            self.db_api.update_item.reset_mock()

        resp = self.execute('CreateVpc', {'CidrBlock': fakes.CIDR_VPC_1})
        check_response(resp)

        resp = self.execute('CreateVpc', {'CidrBlock': fakes.CIDR_VPC_1,
                                          'instanceTenancy': 'default'})
        check_response(resp)
    def test_create_customer_gateway(self):
        self.db_api.add_item.side_effect = (
            tools.get_db_api_add_item(fakes.ID_EC2_CUSTOMER_GATEWAY_2))

        resp = self.execute('CreateCustomerGateway',
                            {'IpAddress': fakes.IP_CUSTOMER_GATEWAY_ADDRESS_2,
                             'Type': 'ipsec.1'})
        self.assertEqual({'customerGateway': fakes.EC2_CUSTOMER_GATEWAY_2},
                         resp)
        self.db_api.add_item.assert_called_once_with(
                mock.ANY, 'cgw',
                {'ip_address': fakes.IP_CUSTOMER_GATEWAY_ADDRESS_2})

        resp = self.execute('CreateCustomerGateway',
                            {'IpAddress': fakes.IP_CUSTOMER_GATEWAY_ADDRESS_2,
                             'Type': 'ipsec.1',
                             'BgpAsn': '65000'})
        self.assertEqual({'customerGateway': fakes.EC2_CUSTOMER_GATEWAY_2},
                         resp)
示例#32
0
    def test_create_subnet(self):
        self.set_mock_db_items(fakes.DB_VPC_1, fakes.DB_ROUTE_TABLE_1)
        self.db_api.add_item.side_effect = (
                tools.get_db_api_add_item(fakes.ID_EC2_SUBNET_1))
        self.neutron.create_network.side_effect = (
                tools.get_neutron_create('network', fakes.ID_OS_NETWORK_1,
                                         {'status': 'available'}))
        self.neutron.create_subnet.side_effect = (
                tools.get_neutron_create('subnet', fakes.ID_OS_SUBNET_1))

        def check_response(resp):
            self.assertThat(fakes.EC2_SUBNET_1, matchers.DictMatches(
                    resp['subnet']))
            self.db_api.add_item.assert_called_once_with(
                    mock.ANY, 'subnet',
                    tools.purge_dict(fakes.DB_SUBNET_1, ('id',)),
                    project_id=None)
            self.neutron.create_network.assert_called_once_with(
                    {'network': {}})
            self.neutron.update_network.assert_called_once_with(
                    fakes.ID_OS_NETWORK_1,
                    {'network': {'name': fakes.ID_EC2_SUBNET_1}})
            self.neutron.create_subnet.assert_called_once_with(
                    {'subnet': tools.purge_dict(fakes.OS_SUBNET_1,
                                                ('id', 'name'))})
            self.neutron.update_subnet.assert_called_once_with(
                    fakes.ID_OS_SUBNET_1,
                    {'subnet': {'name': fakes.ID_EC2_SUBNET_1}})
            self.neutron.add_interface_router.assert_called_once_with(
                    fakes.ID_OS_ROUTER_1,
                    {'subnet_id': fakes.ID_OS_SUBNET_1})

        resp = self.execute('CreateSubnet', {'VpcId': fakes.ID_EC2_VPC_1,
                                             'CidrBlock': fakes.CIDR_SUBNET_1})
        check_response(resp)

        self.neutron.reset_mock()
        self.db_api.reset_mock()

        resp = self.execute('CreateSubnet', {'VpcId': fakes.ID_EC2_VPC_1,
                                             'CidrBlock': fakes.CIDR_SUBNET_1,
                                             'AvailabilityZone': 'nova'})
        check_response(resp)
示例#33
0
    def test_create_vpc(self):
        self.neutron.create_router.side_effect = (
            tools.get_neutron_create('router', fakes.ID_OS_ROUTER_1))
        self.db_api.add_item.side_effect = (
            tools.get_db_api_add_item({
                'vpc': fakes.ID_EC2_VPC_1,
                'rtb': fakes.ID_EC2_ROUTE_TABLE_1,
                'sg': fakes.ID_EC2_SECURITY_GROUP_1}))
        self.set_mock_db_items(fakes.DB_VPC_1)
        self.nova.security_groups.create.return_value = (
            fakes.NovaSecurityGroup(fakes.NOVA_SECURITY_GROUP_1))

        def check_response(response):
            self.assertIn('vpc', response)
            vpc = resp['vpc']
            self.assertThat(fakes.EC2_VPC_1, matchers.DictMatches(vpc))
            self.neutron.create_router.assert_called_with({'router': {}})
            self.neutron.update_router.assert_called_once_with(
                fakes.ID_OS_ROUTER_1,
                {'router': {'name': fakes.EC2_VPC_1['vpcId']}})
            self.db_api.add_item.assert_any_call(
                mock.ANY, 'vpc',
                tools.purge_dict(fakes.DB_VPC_1,
                                 ('id', 'vpc_id', 'route_table_id')))
            self.db_api.add_item.assert_any_call(
                mock.ANY, 'rtb',
                tools.purge_dict(fakes.DB_ROUTE_TABLE_1,
                                 ('id',)))
            self.db_api.update_item.assert_called_once_with(
                mock.ANY,
                fakes.DB_VPC_1)

            self.neutron.reset_mock()
            self.db_api.reset_mock()
            self.db_api.update_item.reset_mock()

        resp = self.execute('CreateVpc', {'CidrBlock': fakes.CIDR_VPC_1})
        check_response(resp)

        resp = self.execute('CreateVpc', {'CidrBlock': fakes.CIDR_VPC_1,
                                          'instanceTenancy': 'default'})
        check_response(resp)
示例#34
0
    def test_create_vpn_connection_rollback(self):
        self.set_mock_db_items(fakes.DB_VPN_GATEWAY_1, fakes.DB_CUSTOMER_GATEWAY_1)
        self.neutron.create_ikepolicy.side_effect = tools.get_neutron_create("ikepolicy", fakes.ID_OS_IKEPOLICY_1)
        self.neutron.create_ipsecpolicy.side_effect = tools.get_neutron_create("ipsecpolicy", fakes.ID_OS_IPSECPOLICY_1)
        self.db_api.add_item.side_effect = tools.get_db_api_add_item(fakes.ID_EC2_VPN_CONNECTION_1)
        self.neutron.update_ikepolicy.side_effect = Exception()

        self.assert_execution_error(
            self.ANY_EXECUTE_ERROR,
            "CreateVpnConnection",
            {
                "VpnGatewayId": fakes.ID_EC2_VPN_GATEWAY_1,
                "CustomerGatewayId": fakes.ID_EC2_CUSTOMER_GATEWAY_1,
                "Type": "ipsec.1",
                "Options.StaticRoutesOnly": "True",
            },
        )

        self.db_api.delete_item.assert_called_once_with(mock.ANY, fakes.ID_EC2_VPN_CONNECTION_1)
        self.neutron.delete_ipsecpolicy.assert_called_once_with(fakes.ID_OS_IPSECPOLICY_1)
        self.neutron.delete_ikepolicy.assert_called_once_with(fakes.ID_OS_IKEPOLICY_1)
示例#35
0
    def test_create_subnet_rollback(self):
        self.set_mock_db_items(fakes.DB_VPC_1, fakes.DB_ROUTE_TABLE_1)
        self.db_api.add_item.side_effect = (
                tools.get_db_api_add_item(fakes.ID_EC2_SUBNET_1))
        self.neutron.create_network.side_effect = (
                tools.get_neutron_create('network', fakes.ID_OS_NETWORK_1,
                                         {'status': 'available'}))
        self.neutron.create_subnet.side_effect = (
                tools.get_neutron_create('subnet', fakes.ID_OS_SUBNET_1))
        self.neutron.update_network.side_effect = Exception()

        self.assert_execution_error(self.ANY_EXECUTE_ERROR, 'CreateSubnet',
                                    {'VpcId': fakes.ID_EC2_VPC_1,
                                     'CidrBlock': fakes.CIDR_SUBNET_1})

        self.neutron.assert_has_calls([
            mock.call.remove_interface_router(
                fakes.ID_OS_ROUTER_1, {'subnet_id': fakes.ID_OS_SUBNET_1}),
            mock.call.delete_subnet(fakes.ID_OS_SUBNET_1),
            mock.call.delete_network(fakes.ID_OS_NETWORK_1)])
        self.db_api.delete_item.assert_called_once_with(
                mock.ANY, fakes.ID_EC2_SUBNET_1)
示例#36
0
    def test_create_customer_gateway(self):
        self.db_api.add_item.side_effect = (tools.get_db_api_add_item(
            fakes.ID_EC2_CUSTOMER_GATEWAY_2))

        resp = self.execute('CreateCustomerGateway', {
            'IpAddress': fakes.IP_CUSTOMER_GATEWAY_ADDRESS_2,
            'Type': 'ipsec.1'
        })
        self.assertEqual({'customerGateway': fakes.EC2_CUSTOMER_GATEWAY_2},
                         resp)
        self.db_api.add_item.assert_called_once_with(
            mock.ANY, 'cgw',
            {'ip_address': fakes.IP_CUSTOMER_GATEWAY_ADDRESS_2})

        resp = self.execute(
            'CreateCustomerGateway', {
                'IpAddress': fakes.IP_CUSTOMER_GATEWAY_ADDRESS_2,
                'Type': 'ipsec.1',
                'BgpAsn': '65000'
            })
        self.assertEqual({'customerGateway': fakes.EC2_CUSTOMER_GATEWAY_2},
                         resp)
示例#37
0
    def test_create_snapshot_from_volume(self):
        self.cinder.volume_snapshots.create.return_value = (
            fakes.OSSnapshot(fakes.OS_SNAPSHOT_1))
        self.db_api.add_item.side_effect = (
            tools.get_db_api_add_item(fakes.ID_EC2_SNAPSHOT_1))
        self.set_mock_db_items(fakes.DB_VOLUME_2)
        self.cinder.volumes.get.side_effect = (
            lambda vol_id: (
                fakes.OSVolume(fakes.OS_VOLUME_2)
                if vol_id == fakes.ID_OS_VOLUME_2
                else None))

        resp = self.execute(
            'CreateSnapshot',
            {'VolumeId': fakes.ID_EC2_VOLUME_2})
        self.assertThat(fakes.EC2_SNAPSHOT_1, matchers.DictMatches(resp))
        self.db_api.add_item.assert_called_once_with(
            mock.ANY, 'snap',
            tools.purge_dict(fakes.DB_SNAPSHOT_1, ('id',)))

        self.cinder.volume_snapshots.create.assert_called_once_with(
            fakes.ID_OS_VOLUME_2, force=True)
示例#38
0
    def test_create_subnet_rollback(self):
        self.set_mock_db_items(fakes.DB_VPC_1, fakes.DB_ROUTE_TABLE_1)
        self.db_api.add_item.side_effect = (tools.get_db_api_add_item(
            fakes.ID_EC2_SUBNET_1))
        self.neutron.create_network.side_effect = (tools.get_neutron_create(
            'network', fakes.ID_OS_NETWORK_1, {'status': 'available'}))
        self.neutron.create_subnet.side_effect = (tools.get_neutron_create(
            'subnet', fakes.ID_OS_SUBNET_1))
        self.neutron.update_network.side_effect = Exception()

        self.assert_execution_error(self.ANY_EXECUTE_ERROR, 'CreateSubnet', {
            'VpcId': fakes.ID_EC2_VPC_1,
            'CidrBlock': fakes.CIDR_SUBNET_1
        })

        self.neutron.assert_has_calls([
            mock.call.remove_interface_router(
                fakes.ID_OS_ROUTER_1, {'subnet_id': fakes.ID_OS_SUBNET_1}),
            mock.call.delete_subnet(fakes.ID_OS_SUBNET_1),
            mock.call.delete_network(fakes.ID_OS_NETWORK_1)
        ])
        self.db_api.delete_item.assert_called_once_with(
            mock.ANY, fakes.ID_EC2_SUBNET_1)
 def test_route_table_create(self):
     self.set_mock_db_items(fakes.DB_VPC_1)
     self.db_api.add_item.side_effect = (tools.get_db_api_add_item(
         fakes.ID_EC2_ROUTE_TABLE_1))
     resp = self.execute('CreateRouteTable', {'VpcId': fakes.ID_EC2_VPC_1})
     self.assertThat(
         resp['routeTable'],
         matchers.DictMatches(
             tools.purge_dict(fakes.EC2_ROUTE_TABLE_1,
                              ('associationSet', ))))
     self.db_api.add_item.assert_called_once_with(
         mock.ANY,
         'rtb', {
             'vpc_id':
             fakes.ID_EC2_VPC_1,
             'routes': [{
                 'destination_cidr_block': fakes.CIDR_VPC_1,
                 'gateway_id': None
             }]
         },
         project_id=None)
     self.db_api.get_item_by_id.assert_called_once_with(
         mock.ANY, fakes.ID_EC2_VPC_1)
    def test_create_vpn_connection_rollback(self):
        self.set_mock_db_items(fakes.DB_VPN_GATEWAY_1,
                               fakes.DB_CUSTOMER_GATEWAY_1)
        self.neutron.create_ikepolicy.side_effect = (
            tools.get_neutron_create('ikepolicy', fakes.ID_OS_IKEPOLICY_1))
        self.neutron.create_ipsecpolicy.side_effect = (
            tools.get_neutron_create('ipsecpolicy', fakes.ID_OS_IPSECPOLICY_1))
        self.db_api.add_item.side_effect = (
            tools.get_db_api_add_item(fakes.ID_EC2_VPN_CONNECTION_1))
        self.neutron.update_ikepolicy.side_effect = Exception()

        self.assert_execution_error(
            self.ANY_EXECUTE_ERROR, 'CreateVpnConnection',
            {'VpnGatewayId': fakes.ID_EC2_VPN_GATEWAY_1,
             'CustomerGatewayId': fakes.ID_EC2_CUSTOMER_GATEWAY_1,
             'Type': 'ipsec.1',
             'Options.StaticRoutesOnly': 'True'})

        self.db_api.delete_item.assert_called_once_with(
            mock.ANY, fakes.ID_EC2_VPN_CONNECTION_1)
        self.neutron.delete_ipsecpolicy.assert_called_once_with(
            fakes.ID_OS_IPSECPOLICY_1)
        self.neutron.delete_ikepolicy.assert_called_once_with(
            fakes.ID_OS_IKEPOLICY_1)
示例#41
0
    def test_register_image_by_bdm(self, get_os_image):
        self.glance.images.create.return_value = (
            fakes.OSImage(fakes.OS_IMAGE_2))
        self.db_api.add_item.side_effect = (
            tools.get_db_api_add_item(fakes.ID_EC2_IMAGE_2))
        self.set_mock_db_items(fakes.DB_SNAPSHOT_1,
                               fakes.DB_IMAGE_AKI_1, fakes.DB_IMAGE_ARI_1)
        get_os_image.side_effect = [fakes.OSImage(fakes.OS_IMAGE_AKI_1),
                                    fakes.OSImage(fakes.OS_IMAGE_ARI_1)]

        resp = self.execute(
            'RegisterImage',
            {'RootDeviceName': fakes.ROOT_DEVICE_NAME_IMAGE_2,
             'Name': 'fake_name',
             'KernelId': fakes.ID_EC2_IMAGE_AKI_1,
             'RamdiskId': fakes.ID_EC2_IMAGE_ARI_1,
             'BlockDeviceMapping.1.DeviceName': fakes.ROOT_DEVICE_NAME_IMAGE_2,
             'BlockDeviceMapping.1.Ebs.SnapshotId': fakes.ID_EC2_SNAPSHOT_1,
             'BlockDeviceMapping.2.DeviceName': '/dev/vdf',
             'BlockDeviceMapping.2.Ebs.VolumeSize': '100',
             'BlockDeviceMapping.2.Ebs.DeleteOnTermination': 'False'})
        self.assertThat(resp, matchers.DictMatches(
            {'imageId': fakes.ID_EC2_IMAGE_2}))
        self.db_api.add_item.assert_called_once_with(
            mock.ANY, 'ami', {'os_id': fakes.ID_OS_IMAGE_2,
                              'is_public': False,
                              'description': None})
        self.assertEqual(1, self.glance.images.create.call_count)
        self.assertEqual((), self.glance.images.create.call_args[0])
        self.assertIn('properties', self.glance.images.create.call_args[1])
        self.assertIsInstance(
            self.glance.images.create.call_args[1]['properties'],
            dict)
        bdm = self.glance.images.create.call_args[1]['properties'].pop(
            'block_device_mapping', 'null')
        self.assertEqual(
            {'is_public': False,
             'size': 0,
             'name': 'fake_name',
             'properties': {
                 'root_device_name': fakes.ROOT_DEVICE_NAME_IMAGE_2,
                 'kernel_id': fakes.ID_OS_IMAGE_AKI_1,
                 'ramdisk_id': fakes.ID_OS_IMAGE_ARI_1,
                 'bdm_v2': True}},
            self.glance.images.create.call_args[1])
        self.assertEqual([{'boot_index': 0,
                           'delete_on_termination': True,
                           'destination_type': 'volume',
                           'device_name': fakes.ROOT_DEVICE_NAME_IMAGE_2,
                           'source_type': 'snapshot',
                           'snapshot_id': fakes.ID_OS_SNAPSHOT_1},
                          {'boot_index': -1,
                           'delete_on_termination': False,
                           'destination_type': 'volume',
                           'device_name': '/dev/vdf',
                           'source_type': 'blank',
                           'volume_size': 100}],
                         json.loads(bdm))
        get_os_image.assert_has_calls(
            [mock.call(mock.ANY, fakes.ID_EC2_IMAGE_AKI_1),
             mock.call(mock.ANY, fakes.ID_EC2_IMAGE_ARI_1)])
示例#42
0
    def test_register_image_by_bdm(self, get_os_image):
        self.glance.images.create.return_value = (fakes.OSImage(
            fakes.OS_IMAGE_2))
        self.cinder.volume_snapshots.get.side_effect = (
            tools.get_by_1st_arg_getter(
                {
                    fakes.ID_OS_SNAPSHOT_1:
                    (fakes.OSSnapshot(fakes.OS_SNAPSHOT_1))
                },
                notfound_exception=cinder_exception.NotFound(404)))
        self.db_api.add_item.side_effect = (tools.get_db_api_add_item(
            fakes.ID_EC2_IMAGE_2))
        self.set_mock_db_items(fakes.DB_SNAPSHOT_1, fakes.DB_SNAPSHOT_2,
                               fakes.DB_IMAGE_AKI_1, fakes.DB_IMAGE_ARI_1)
        get_os_image.side_effect = [
            fakes.OSImage(fakes.OS_IMAGE_AKI_1),
            fakes.OSImage(fakes.OS_IMAGE_ARI_1)
        ]

        resp = self.execute(
            'RegisterImage', {
                'RootDeviceName': fakes.ROOT_DEVICE_NAME_IMAGE_2,
                'Name': 'fake_name',
                'KernelId': fakes.ID_EC2_IMAGE_AKI_1,
                'RamdiskId': fakes.ID_EC2_IMAGE_ARI_1,
                'BlockDeviceMapping.1.DeviceName':
                fakes.ROOT_DEVICE_NAME_IMAGE_2,
                'BlockDeviceMapping.1.Ebs.SnapshotId': fakes.ID_EC2_SNAPSHOT_1,
                'BlockDeviceMapping.2.DeviceName': '/dev/vdf',
                'BlockDeviceMapping.2.Ebs.VolumeSize': '100',
                'BlockDeviceMapping.2.Ebs.DeleteOnTermination': 'False',
                'BlockDeviceMapping.3.DeviceName': '/dev/vdg',
                'BlockDeviceMapping.3.Ebs.SnapshotId': fakes.ID_EC2_SNAPSHOT_1,
                'BlockDeviceMapping.3.Ebs.VolumeSize': '55',
                'BlockDeviceMapping.3.Ebs.DeleteOnTermination': 'True',
                'BlockDeviceMapping.4.DeviceName': '/dev/vdh',
                'BlockDeviceMapping.4.Ebs.SnapshotId': fakes.ID_EC2_SNAPSHOT_2
            })
        self.assertThat(
            resp, matchers.DictMatches({'imageId': fakes.ID_EC2_IMAGE_2}))
        self.db_api.add_item.assert_called_once_with(mock.ANY, 'ami', {
            'os_id': fakes.ID_OS_IMAGE_2,
            'is_public': False,
            'description': None
        })
        self.assertEqual(1, self.glance.images.create.call_count)
        self.assertEqual((), self.glance.images.create.call_args[0])
        self.assertIn('properties', self.glance.images.create.call_args[1])
        self.assertIsInstance(
            self.glance.images.create.call_args[1]['properties'], dict)
        bdm = self.glance.images.create.call_args[1]['properties'].pop(
            'block_device_mapping', 'null')
        self.assertEqual(
            {
                'is_public': False,
                'size': 0,
                'name': 'fake_name',
                'properties': {
                    'root_device_name': fakes.ROOT_DEVICE_NAME_IMAGE_2,
                    'kernel_id': fakes.ID_OS_IMAGE_AKI_1,
                    'ramdisk_id': fakes.ID_OS_IMAGE_ARI_1,
                    'bdm_v2': True
                }
            }, self.glance.images.create.call_args[1])
        self.assertEqual([{
            'boot_index': 0,
            'delete_on_termination': True,
            'destination_type': 'volume',
            'device_name': fakes.ROOT_DEVICE_NAME_IMAGE_2,
            'source_type': 'snapshot',
            'snapshot_id': fakes.ID_OS_SNAPSHOT_1,
            'volume_size': 1
        }, {
            'boot_index': -1,
            'delete_on_termination': False,
            'destination_type': 'volume',
            'device_name': '/dev/vdf',
            'source_type': 'blank',
            'volume_size': 100
        }, {
            'boot_index': -1,
            'delete_on_termination': True,
            'destination_type': 'volume',
            'device_name': '/dev/vdg',
            'source_type': 'snapshot',
            'snapshot_id': fakes.ID_OS_SNAPSHOT_1,
            'volume_size': 55
        }, {
            'boot_index': -1,
            'delete_on_termination': True,
            'destination_type': 'volume',
            'device_name': '/dev/vdh',
            'source_type': 'snapshot',
            'snapshot_id': fakes.ID_OS_SNAPSHOT_2
        }], json.loads(bdm))
        get_os_image.assert_has_calls([
            mock.call(mock.ANY, fakes.ID_EC2_IMAGE_AKI_1),
            mock.call(mock.ANY, fakes.ID_EC2_IMAGE_ARI_1)
        ])
        self.cinder.volume_snapshots.get.assert_any_call(
            fakes.ID_OS_SNAPSHOT_1)
示例#43
0
    def test_register_image_by_bdm(self, get_os_image):
        self.glance.images.create.return_value = (
            fakes.OSImage(fakes.OS_IMAGE_2))
        self.glance.images.upload.return_value = (
            fakes.OSImage(fakes.OS_IMAGE_2))
        self.cinder.volume_snapshots.get.side_effect = (
            tools.get_by_1st_arg_getter(
                {fakes.ID_OS_SNAPSHOT_1: (
                    fakes.OSSnapshot(fakes.OS_SNAPSHOT_1))},
                notfound_exception=cinder_exception.NotFound(404)))
        self.db_api.add_item.side_effect = (
            tools.get_db_api_add_item(fakes.ID_EC2_IMAGE_2))
        self.set_mock_db_items(fakes.DB_SNAPSHOT_1, fakes.DB_SNAPSHOT_2,
                               fakes.DB_IMAGE_AKI_1, fakes.DB_IMAGE_ARI_1)
        get_os_image.side_effect = [fakes.OSImage(fakes.OS_IMAGE_AKI_1),
                                    fakes.OSImage(fakes.OS_IMAGE_ARI_1)]

        resp = self.execute(
            'RegisterImage',
            {'RootDeviceName': fakes.ROOT_DEVICE_NAME_IMAGE_2,
             'Name': 'fake_name',
             'KernelId': fakes.ID_EC2_IMAGE_AKI_1,
             'RamdiskId': fakes.ID_EC2_IMAGE_ARI_1,
             'BlockDeviceMapping.1.DeviceName': fakes.ROOT_DEVICE_NAME_IMAGE_2,
             'BlockDeviceMapping.1.Ebs.SnapshotId': fakes.ID_EC2_SNAPSHOT_1,
             'BlockDeviceMapping.2.DeviceName': '/dev/vdf',
             'BlockDeviceMapping.2.Ebs.VolumeSize': '100',
             'BlockDeviceMapping.2.Ebs.DeleteOnTermination': 'False',
             'BlockDeviceMapping.3.DeviceName': '/dev/vdg',
             'BlockDeviceMapping.3.Ebs.SnapshotId': fakes.ID_EC2_SNAPSHOT_1,
             'BlockDeviceMapping.3.Ebs.VolumeSize': '55',
             'BlockDeviceMapping.3.Ebs.DeleteOnTermination': 'True',
             'BlockDeviceMapping.4.DeviceName': '/dev/vdh',
             'BlockDeviceMapping.4.Ebs.SnapshotId': fakes.ID_EC2_SNAPSHOT_2})
        self.assertThat(resp, matchers.DictMatches(
            {'imageId': fakes.ID_EC2_IMAGE_2}))
        self.db_api.add_item.assert_called_once_with(
            mock.ANY, 'ami', {'os_id': fakes.ID_OS_IMAGE_2,
                              'is_public': False,
                              'description': None})
        self.assertEqual(1, self.glance.images.create.call_count)
        self.assertEqual((), self.glance.images.create.call_args[0])
        self.assertIsInstance(
            self.glance.images.create.call_args[1], dict)
        bdm = self.glance.images.create.call_args[1].pop(
            'block_device_mapping', 'null')
        self.assertEqual(
            {'visibility': 'private',
             'name': 'fake_name',
             'kernel_id': fakes.ID_OS_IMAGE_AKI_1,
             'ramdisk_id': fakes.ID_OS_IMAGE_ARI_1,
             'root_device_name': fakes.ROOT_DEVICE_NAME_IMAGE_2,
             'container_format': 'bare',
             'disk_format': 'raw',
             'bdm_v2': 'True'},
            self.glance.images.create.call_args[1])
        self.assertEqual([{'boot_index': 0,
                           'delete_on_termination': True,
                           'destination_type': 'volume',
                           'device_name': fakes.ROOT_DEVICE_NAME_IMAGE_2,
                           'source_type': 'snapshot',
                           'snapshot_id': fakes.ID_OS_SNAPSHOT_1,
                           'volume_size': 1},
                          {'boot_index': -1,
                           'delete_on_termination': False,
                           'destination_type': 'volume',
                           'device_name': '/dev/vdf',
                           'source_type': 'blank',
                           'volume_size': 100},
                          {'boot_index': -1,
                           'delete_on_termination': True,
                           'destination_type': 'volume',
                           'device_name': '/dev/vdg',
                           'source_type': 'snapshot',
                           'snapshot_id': fakes.ID_OS_SNAPSHOT_1,
                           'volume_size': 55},
                          {'boot_index': -1,
                           'delete_on_termination': True,
                           'destination_type': 'volume',
                           'device_name': '/dev/vdh',
                           'source_type': 'snapshot',
                           'snapshot_id': fakes.ID_OS_SNAPSHOT_2}],
                         json.loads(bdm))
        get_os_image.assert_has_calls(
            [mock.call(mock.ANY, fakes.ID_EC2_IMAGE_AKI_1),
             mock.call(mock.ANY, fakes.ID_EC2_IMAGE_ARI_1)])
        self.cinder.volume_snapshots.get.assert_any_call(
            fakes.ID_OS_SNAPSHOT_1)
示例#44
0
    def test_create_vpn_connection(self, random_choice, reset_vpn_connections,
                                   describe_vpn_connections):
        self.set_mock_db_items(fakes.DB_VPN_GATEWAY_1, fakes.DB_VPN_GATEWAY_2,
                               fakes.DB_CUSTOMER_GATEWAY_1,
                               fakes.DB_CUSTOMER_GATEWAY_2, fakes.DB_VPC_1)
        self.neutron.create_ikepolicy.side_effect = (tools.get_neutron_create(
            'ikepolicy', fakes.ID_OS_IKEPOLICY_1))
        self.neutron.create_ipsecpolicy.side_effect = (
            tools.get_neutron_create('ipsecpolicy', fakes.ID_OS_IPSECPOLICY_1))
        self.db_api.add_item.side_effect = (tools.get_db_api_add_item(
            fakes.ID_EC2_VPN_CONNECTION_1))
        random_choice.side_effect = iter(fakes.PRE_SHARED_KEY_1)
        describe_vpn_connections.return_value = {
            'vpnConnectionSet': [fakes.EC2_VPN_CONNECTION_1]
        }

        resp = self.execute(
            'CreateVpnConnection', {
                'VpnGatewayId': fakes.ID_EC2_VPN_GATEWAY_1,
                'CustomerGatewayId': fakes.ID_EC2_CUSTOMER_GATEWAY_1,
                'Type': 'ipsec.1',
                'Options.StaticRoutesOnly': 'True'
            })
        self.assertThat(
            resp,
            matchers.DictMatches({'vpnConnection':
                                  fakes.EC2_VPN_CONNECTION_1}))

        self.neutron.create_ikepolicy.assert_called_once_with(
            {'ikepolicy': tools.purge_dict(fakes.OS_IKEPOLICY_1, ('id', ))})
        self.neutron.create_ipsecpolicy.assert_called_once_with({
            'ipsecpolicy':
            tools.purge_dict(fakes.OS_IPSECPOLICY_1, ('id', ))
        })
        random_choice.assert_called_with(vpn_connection_api.SHARED_KEY_CHARS)
        new_vpn_connection_1 = tools.update_dict(fakes.DB_VPN_CONNECTION_1, {
            'cidrs': [],
            'os_ipsec_site_connections': {}
        })
        self.db_api.add_item.assert_called_once_with(
            mock.ANY, 'vpn',
            tools.purge_dict(new_vpn_connection_1, ('id', 'vpc_id', 'os_id')))
        self.neutron.update_ikepolicy.assert_called_once_with(
            fakes.ID_OS_IKEPOLICY_1,
            {'ikepolicy': {
                'name': fakes.ID_EC2_VPN_CONNECTION_1
            }})
        self.neutron.update_ipsecpolicy.assert_called_once_with(
            fakes.ID_OS_IPSECPOLICY_1,
            {'ipsecpolicy': {
                'name': fakes.ID_EC2_VPN_CONNECTION_1
            }})
        reset_vpn_connections.assert_called_once_with(
            mock.ANY,
            self.neutron,
            mock.ANY,
            fakes.DB_VPN_GATEWAY_1,
            vpn_connections=[new_vpn_connection_1])
        self.assertIsInstance(reset_vpn_connections.call_args[0][2],
                              common.OnCrashCleaner)
        describe_vpn_connections.assert_called_once_with(
            mock.ANY, vpn_connection_id=[fakes.ID_EC2_VPN_CONNECTION_1])