Exemplo n.º 1
0
    def test_build_proxy_request_headers(self, get_remote_ip, get_context,
                                         sign_instance_id):
        req = mock.Mock(headers={})

        req.headers = {'X-Instance-ID': 'fake_instance_id',
                       'fake_key': 'fake_value'}

        self.assertThat(self.handler._build_proxy_request_headers(req),
                        matchers.DictMatches(req.headers))

        req.headers = {'fake_key': 'fake_value'}
        get_remote_ip.return_value = 'fake_instance_ip'
        get_context.return_value = 'fake_context'
        sign_instance_id.return_value = 'signed'

        with mock.patch('ec2api.metadata.api.'
                        'get_os_instance_and_project_id') as get_ids:

            get_ids.return_value = ('fake_instance_id', 'fake_project_id')
            self.assertThat(self.handler._build_proxy_request_headers(req),
                            matchers.DictMatches(
                                {'X-Forwarded-For': 'fake_instance_ip',
                                 'X-Instance-ID': 'fake_instance_id',
                                 'X-Tenant-ID': 'fake_project_id',
                                 'X-Instance-ID-Signature': 'signed'}))
            get_remote_ip.assert_called_with(req)
            get_context.assert_called_with()
            sign_instance_id.assert_called_with('fake_instance_id')
            get_ids.assert_called_with('fake_context', 'fake_instance_ip')

            get_ids.side_effect = exception.EC2MetadataNotFound()
            self.assertRaises(exception.EC2MetadataNotFound,
                              self.handler._build_proxy_request_headers, req)
Exemplo n.º 2
0
    def test_add_item(self):
        new_item = {
            'os_id': fakes.random_os_id(),
            'vpc_id': fakes.random_ec2_id('fake_vpc'),
            'str_attr': 'fake_str',
            'int_attr': 1234,
            'bool_attr': True,
            'dict_attr': {
                'key1': 'val1',
                'key2': 'val2'
            },
            'list_attr': ['fake_str', 1234, True, {
                'key': 'val'
            }, []]
        }
        item = db_api.add_item(self.context, 'fake', new_item)
        self.assertIn('id', item)
        self.assertIsNotNone(item['id'])
        item_id = item.pop('id')
        self.assertTrue(validator.validate_ec2_id(item_id, '', ['fake']))
        self.assertThat(item,
                        matchers.DictMatches(new_item, orderless_lists=True))

        item = db_api.get_item_by_id(self.context, item_id)
        new_item['id'] = item_id
        self.assertThat(item,
                        matchers.DictMatches(new_item, orderless_lists=True))
Exemplo n.º 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_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')
Exemplo n.º 5
0
    def test_format_instance_mapping(self):
        retval = api._build_block_device_mappings(
                'fake_context', fakes.EC2_INSTANCE_1, fakes.ID_OS_INSTANCE_1)
        self.assertThat(retval,
                        matchers.DictMatches(
                             {'ami': 'vda',
                              'root': fakes.ROOT_DEVICE_NAME_INSTANCE_1}))

        retval = api._build_block_device_mappings(
                'fake_context', fakes.EC2_INSTANCE_2, fakes.ID_OS_INSTANCE_2)
        expected = {'ami': 'sdb1',
                    'root': fakes.ROOT_DEVICE_NAME_INSTANCE_2}
        expected.update(fakes.EC2_BDM_METADATA_INSTANCE_2)
        self.assertThat(retval,
                        matchers.DictMatches(expected))
Exemplo n.º 6
0
    def test_describe_images(self):
        self._setup_model()

        resp = self.execute('DescribeImages', {})
        self.assertThat(
            resp,
            matchers.DictMatches(
                {'imagesSet': [fakes.EC2_IMAGE_1, fakes.EC2_IMAGE_2]},
                orderless_lists=True),
            verbose=True)

        self.db_api.get_items.assert_any_call(mock.ANY, 'ami')
        self.db_api.get_items.assert_any_call(mock.ANY, 'aki')
        self.db_api.get_items.assert_any_call(mock.ANY, 'ari')

        self.db_api.get_items_by_ids = tools.CopyingMock(
            side_effect=self.db_api.get_items_by_ids.side_effect)

        resp = self.execute('DescribeImages',
                            {'ImageId.1': fakes.ID_EC2_IMAGE_1})
        self.assertThat(resp,
                        matchers.DictMatches(
                            {'imagesSet': [fakes.EC2_IMAGE_1]},
                            orderless_lists=True))
        self.db_api.get_items_by_ids.assert_any_call(
            mock.ANY, set([fakes.ID_EC2_IMAGE_1]))

        self.check_filtering(
            'DescribeImages', 'imagesSet',
            [('architecture', 'x86_64'),
             ('block-device-mapping.device-name', '/dev/sdb2'),
             ('block-device-mapping.snapshot-id', fakes.ID_EC2_SNAPSHOT_1),
             ('block-device-mapping.volume-size', 22),
             ('description', 'fake desc'),
             ('image-id', fakes.ID_EC2_IMAGE_1),
             ('image-type', 'machine'),
             ('is-public', True),
             ('kernel_id', fakes.ID_EC2_IMAGE_AKI_1,),
             ('name', 'fake_name'),
             ('owner-id', fakes.ID_OS_PROJECT),
             ('ramdisk-id', fakes.ID_EC2_IMAGE_ARI_1),
             ('root-device-name', fakes.ROOT_DEVICE_NAME_IMAGE_1),
             ('root-device-type', 'instance-store'),
             ('state', 'available')])
        self.check_tag_support(
            'DescribeImages', 'imagesSet',
            fakes.ID_EC2_IMAGE_1, 'imageId',
            ('ami', 'ari', 'aki'))
Exemplo n.º 7
0
 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': {
             'name': 'subnet-0'
         }})
     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)
    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)
Exemplo n.º 9
0
 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})
 def check_normal_flow(kind, ec2_id):
     item['id'] = ec2_id
     res = ec2utils.get_db_item('fake_context', ec2_id)
     self.assertThat(res, matchers.DictMatches(item))
     db_api.get_item_by_id.assert_called_once_with('fake_context',
                                                   ec2_id)
     db_api.reset_mock()
Exemplo n.º 11
0
        def check_response(response):
            self.assertIn('vpc', response)
            vpc = resp['vpc']
            self.assertThat(fakes.EC2_VPC_1, matchers.DictMatches(vpc))
            create_vpc.assert_called_once_with(mock.ANY, fakes.CIDR_VPC_1)

            create_vpc.reset_mock()
    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 do_check(attr, ec2_image_id, response):
     resp = self.execute('DescribeImageAttribute', {
         'ImageId': ec2_image_id,
         'Attribute': attr
     })
     response['imageId'] = ec2_image_id
     self.assertThat(
         resp, matchers.DictMatches(response, orderless_lists=True))
Exemplo n.º 14
0
    def test_describe_snapshots(self):
        self.cinder.volume_snapshots.list.return_value = [
            fakes.OSSnapshot(fakes.OS_SNAPSHOT_1),
            fakes.OSSnapshot(fakes.OS_SNAPSHOT_2)
        ]

        self.set_mock_db_items(fakes.DB_SNAPSHOT_1, fakes.DB_SNAPSHOT_2,
                               fakes.DB_VOLUME_2)

        resp = self.execute('DescribeSnapshots', {})
        self.assertThat(
            resp,
            matchers.DictMatches(
                {'snapshotSet': [fakes.EC2_SNAPSHOT_1, fakes.EC2_SNAPSHOT_2]},
                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_SNAPSHOT_1])
        resp = self.execute('DescribeSnapshots',
                            {'SnapshotId.1': fakes.ID_EC2_SNAPSHOT_1})
        self.assertThat(
            resp,
            matchers.DictMatches({'snapshotSet': [fakes.EC2_SNAPSHOT_1]},
                                 orderless_lists=True))
        self.db_api.get_items_by_ids.assert_called_once_with(
            mock.ANY, set([fakes.ID_EC2_SNAPSHOT_1]))

        self.check_filtering(
            'DescribeSnapshots',
            'snapshotSet',
            [
                # TODO(ft): declare a constant for the description in fakes
                ('description', 'fake description'),
                ('owner-id', fakes.ID_OS_PROJECT),
                ('progress', '100%'),
                ('snapshot-id', fakes.ID_EC2_SNAPSHOT_1),
                ('start-time', fakes.TIME_CREATE_SNAPSHOT_2),
                ('status', 'completed'),
                ('volume-id', fakes.ID_EC2_VOLUME_2),
                # TODO(ft): declare a constant for the volume size in fakes
                ('volume-size', 1)
            ])
        self.check_tag_support('DescribeSnapshots', 'snapshotSet',
                               fakes.ID_EC2_SNAPSHOT_1, 'snapshotId')
Exemplo n.º 15
0
    def test_deregister_image(self):
        self._setup_model()

        resp = self.execute('DeregisterImage',
                            {'ImageId': fakes.ID_EC2_IMAGE_1})
        self.assertThat(resp, matchers.DictMatches({'return': True}))
        self.db_api.delete_item.assert_called_once_with(
            mock.ANY, fakes.ID_EC2_IMAGE_1)
        self.glance.images.delete.assert_called_once_with(fakes.ID_OS_IMAGE_1)
Exemplo n.º 16
0
    def test_create_igw(self):
        self.db_api.add_item.return_value = fakes.DB_IGW_2

        resp = self.execute('CreateInternetGateway', {})

        self.assertIn('internetGateway', resp)
        igw = resp['internetGateway']
        self.assertThat(fakes.EC2_IGW_2, matchers.DictMatches(igw))
        self.db_api.add_item.assert_called_with(mock.ANY, 'igw', {})
    def test_describe_volumes_auto_remove(self):
        self.cinder.volumes.list.return_value = []
        self.set_mock_db_items(fakes.DB_VOLUME_1, fakes.DB_VOLUME_2)
        resp = self.execute('DescribeVolumes', {})
        self.assertThat(resp, matchers.DictMatches({'volumeSet': []}))

        self.db_api.delete_item.assert_any_call(mock.ANY,
                                                fakes.ID_EC2_VOLUME_1)
        self.db_api.delete_item.assert_any_call(mock.ANY,
                                                fakes.ID_EC2_VOLUME_2)
Exemplo n.º 18
0
 def do_check(new_item):
     item = db_api.add_item(self.context, 'fake', new_item)
     item_id = item.pop('id')
     if 'id' in new_item:
         new_item_id = new_item.pop('id')
         self.assertNotEqual(new_item_id, item_id)
     new_item.setdefault('os_id', None)
     new_item.setdefault('vpc_id', None)
     self.assertThat(
         item, matchers.DictMatches(new_item, orderless_lists=True))
Exemplo n.º 19
0
 def _compare_aws_xml(self, root_tag, xmlns, request_id, dict_data,
                      observed):
     # NOTE(ft): we cann't use matchers.XMLMatches since it makes comparison
     # based on the order of tags
     xml = etree.fromstring(observed)
     self.assertEqual(xmlns, xml.nsmap.get(None))
     observed_data = tools.parse_xml(observed)
     expected = {root_tag: tools.update_dict(dict_data,
                                             {'requestId': request_id})}
     self.assertThat(observed_data, matchers.DictMatches(expected))
Exemplo n.º 20
0
    def test_s3_parse_manifest(self):
        db_api = self.mock_db()
        glance = self.mock_glance()
        db_api.set_mock_items(fakes.DB_IMAGE_AKI_1, fakes.DB_IMAGE_ARI_1)
        glance.images.get.side_effect = (tools.get_by_1st_arg_getter({
            fakes.ID_OS_IMAGE_AKI_1:
            fakes.OSImage(fakes.OS_IMAGE_AKI_1),
            fakes.ID_OS_IMAGE_ARI_1:
            fakes.OSImage(fakes.OS_IMAGE_ARI_1)
        }))

        metadata, image_parts, key, iv = image_api._s3_parse_manifest(
            base.create_context(), AMI_MANIFEST_XML)

        expected_metadata = {
            'disk_format': 'ami',
            'container_format': 'ami',
            'properties': {
                'architecture':
                'x86_64',
                'kernel_id':
                fakes.ID_OS_IMAGE_AKI_1,
                'ramdisk_id':
                fakes.ID_OS_IMAGE_ARI_1,
                'mappings': [{
                    "device": "sda1",
                    "virtual": "ami"
                }, {
                    "device": "/dev/sda1",
                    "virtual": "root"
                }, {
                    "device": "sda2",
                    "virtual": "ephemeral0"
                }, {
                    "device": "sda3",
                    "virtual": "swap"
                }]
            }
        }
        self.assertThat(
            metadata,
            matchers.DictMatches(expected_metadata, orderless_lists=True))
        self.assertThat(image_parts, matchers.ListMatches(['foo']))
        self.assertEqual('foo', key)
        self.assertEqual('foo', iv)
        db_api.get_items_ids.assert_any_call(
            mock.ANY,
            'aki',
            item_ids=(fakes.ID_EC2_IMAGE_AKI_1, ),
            item_os_ids=None)
        db_api.get_items_ids.assert_any_call(
            mock.ANY,
            'ari',
            item_ids=(fakes.ID_EC2_IMAGE_ARI_1, ),
            item_os_ids=None)
Exemplo n.º 21
0
 def test_build_proxy_request_headers(self, sign_instance_id):
     sign_instance_id.return_value = mock.sentinel.signed
     requester = {'os_instance_id': mock.sentinel.os_instance_id,
                  'project_id': mock.sentinel.project_id,
                  'private_ip': mock.sentinel.private_ip}
     result = self.handler._build_proxy_request_headers(requester)
     expected = {'X-Forwarded-For': mock.sentinel.private_ip,
                 'X-Instance-ID': mock.sentinel.os_instance_id,
                 'X-Tenant-ID': mock.sentinel.project_id,
                 'X-Instance-ID-Signature': mock.sentinel.signed}
     self.assertThat(result, matchers.DictMatches(expected))
Exemplo n.º 22
0
 def check(ec2_fake, db_fake):
     self.db_api.add_item.return_value = db_fake
     resp = self.execute('CreateDhcpOptions',
                         gen_ec2_param_dhcp_options(ec2_fake))
     self.assertThat(
         ec2_fake,
         matchers.DictMatches(resp['dhcpOptions'],
                              orderless_lists=True))
     self.assert_any_call(self.db_api.add_item, mock.ANY, 'dopt',
                          tools.purge_dict(db_fake, ('id', )))
     self.db_api.reset_mock()
Exemplo n.º 23
0
    def test_modify_image_attributes(self):
        self._setup_model()

        resp = self.execute('ModifyImageAttribute',
                            {'imageId': fakes.ID_EC2_IMAGE_1,
                             'attribute': 'launchPermission',
                             'operationType': 'add',
                             'userGroup.1': 'all'})
        self.assertThat(resp, matchers.DictMatches({'return': True}))
        self.glance.images.update.assert_called_once_with(
                fakes.ID_OS_IMAGE_1, visibility='public')
 def test_get_router_objects(self):
     self.set_mock_db_items(fakes.DB_IGW_1, fakes.DB_NETWORK_INTERFACE_2)
     host_routes = route_table._get_router_objects('fake_context',
                                                   fakes.DB_ROUTE_TABLE_2)
     self.assertThat(
         host_routes,
         matchers.DictMatches({
             fakes.ID_EC2_IGW_1:
             fakes.DB_IGW_1,
             fakes.ID_EC2_NETWORK_INTERFACE_2:
             fakes.DB_NETWORK_INTERFACE_2
         }))
    def test_format_instance_mapping(self):
        self.instance_api._block_device_strip_dev.return_value = 'vda'
        retval = api._build_block_device_mappings(
                'fake_context', fakes.EC2_INSTANCE_1, fakes.ID_OS_INSTANCE_1)
        self.assertThat(retval,
                        matchers.DictMatches(
                             {'ami': 'vda',
                              'root': fakes.ROOT_DEVICE_NAME_INSTANCE_1}))
        self.instance_api._block_device_strip_dev.assert_called_with(
                fakes.EC2_INSTANCE_1['rootDeviceName'])

        self.instance_api._block_device_strip_dev.return_value = 'sdb1'
        retval = api._build_block_device_mappings(
                'fake_context', fakes.EC2_INSTANCE_2, fakes.ID_OS_INSTANCE_2)
        expected = {'ami': 'sdb1',
                    'root': fakes.ROOT_DEVICE_NAME_INSTANCE_2}
        expected.update(fakes.EC2_BDM_METADATA_INSTANCE_2)
        self.assertThat(retval,
                        matchers.DictMatches(expected))
        self.instance_api._block_device_strip_dev.assert_called_with(
                fakes.EC2_INSTANCE_2['rootDeviceName'])
Exemplo n.º 26
0
 def test_import_key_pair(self):
     self.nova.keypairs.create.return_value = (fakes.NovaKeyPair(
         fakes.OS_KEY_PAIR))
     resp = self.execute(
         'ImportKeyPair', {
             'KeyName': fakes.NAME_KEY_PAIR,
             'PublicKeyMaterial': base64.b64encode(
                 fakes.PUBLIC_KEY_KEY_PAIR)
         })
     self.assertThat(tools.purge_dict(fakes.EC2_KEY_PAIR, {'keyMaterial'}),
                     matchers.DictMatches(resp))
     self.nova.keypairs.create.assert_called_once_with(
         fakes.NAME_KEY_PAIR, fakes.PUBLIC_KEY_KEY_PAIR)
Exemplo n.º 27
0
 def test_format_vpn_connection(self):
     db_vpn_connection_1 = tools.update_dict(fakes.DB_VPN_CONNECTION_1,
                                             {'cidrs': []})
     ec2_vpn_connection_1 = tools.patch_dict(fakes.EC2_VPN_CONNECTION_1, {
         'routes': [],
         'vgwTelemetry': []
     }, ('customerGatewayConfiguration', ))
     formatted = vpn_connection_api._format_vpn_connection(
         db_vpn_connection_1,
         {fakes.ID_EC2_CUSTOMER_GATEWAY_1: fakes.DB_CUSTOMER_GATEWAY_1}, {},
         {}, {}, {})
     formatted.pop('customerGatewayConfiguration')
     self.assertThat(ec2_vpn_connection_1, matchers.DictMatches(formatted))
Exemplo n.º 28
0
    def test_modify_image_attributes(self, osimage_update):
        self._setup_model()

        resp = self.execute(
            'ModifyImageAttribute', {
                'imageId': fakes.ID_EC2_IMAGE_1,
                'attribute': 'launchPermission',
                'operationType': 'add',
                'userGroup.1': 'all'
            })
        self.assertThat(resp, matchers.DictMatches({'return': True}))
        osimage_update.assert_called_once_with(mock.ANY, is_public=True)
        self.assertEqual(fakes.ID_OS_IMAGE_1,
                         osimage_update.call_args[0][0].id)
Exemplo n.º 29
0
    def test_deregister_image(self):
        self._setup_model()

        # normal flow
        resp = self.execute('DeregisterImage',
                            {'ImageId': fakes.ID_EC2_IMAGE_1})
        self.assertThat(resp, matchers.DictMatches({'return': True}))
        self.db_api.delete_item.assert_called_once_with(
            mock.ANY, fakes.ID_EC2_IMAGE_1)
        self.glance.images.delete.assert_called_once_with(fakes.ID_OS_IMAGE_1)

        # deregister image which failed on asynchronously creation
        self.glance.reset_mock()
        image_id = fakes.random_ec2_id('ami')
        self.add_mock_db_items({
            'id': image_id,
            'os_id': None,
            'state': 'failed'
        })
        resp = self.execute('DeregisterImage', {'ImageId': image_id})
        self.assertThat(resp, matchers.DictMatches({'return': True}))
        self.db_api.delete_item.assert_called_with(mock.ANY, image_id)
        self.assertFalse(self.glance.images.delete.called)
Exemplo n.º 30
0
    def test_describe_snapshots_auto_remove(self):
        self.cinder.volume_snapshots.list.return_value = []

        self.set_mock_db_items(fakes.DB_SNAPSHOT_1, fakes.DB_VOLUME_2)

        resp = self.execute('DescribeSnapshots', {})
        self.assertThat(
            resp,
            matchers.DictMatches({'snapshotSet': []}, orderless_lists=True))

        self.db_api.get_items.assert_any_call(mock.ANY, 'vol')
        self.db_api.get_items.assert_any_call(mock.ANY, 'snap')
        self.db_api.delete_item.assert_any_call(mock.ANY,
                                                fakes.ID_EC2_SNAPSHOT_1)