Esempio n. 1
0
    def test_describe_snapshots_invalid_parameters(self):
        self.cinder.volume_snapshots.list.return_value = [
            fakes.OSSnapshot(fakes.OS_SNAPSHOT_1),
            fakes.OSSnapshot(fakes.OS_SNAPSHOT_2)]

        self.assert_execution_error(
            'InvalidSnapshot.NotFound', 'DescribeSnapshots',
            {'SnapshotId.1': fakes.random_ec2_id('snap')})

        self.cinder.volume_snapshots.list.side_effect = lambda: []

        self.assert_execution_error(
            'InvalidSnapshot.NotFound', 'DescribeSnapshots',
            {'SnapshotId.1': fakes.ID_EC2_SNAPSHOT_1})
Esempio n. 2
0
    def test_format_snapshot_maps_status(self):
        fake_snapshot = fakes.OSSnapshot(fakes.OS_SNAPSHOT_1)
        self.cinder.volume_snapshots.list.return_value = [fake_snapshot]
        self.set_mock_db_items(fakes.DB_SNAPSHOT_1, fakes.DB_VOLUME_2)

        fake_snapshot.status = 'new'
        resp = self.execute('DescribeSnapshots', {})
        self.assertEqual('pending', resp['snapshotSet'][0]['status'])

        fake_snapshot.status = 'creating'
        resp = self.execute('DescribeSnapshots', {})
        self.assertEqual('pending', resp['snapshotSet'][0]['status'])

        fake_snapshot.status = 'available'
        resp = self.execute('DescribeSnapshots', {})
        self.assertEqual('completed', resp['snapshotSet'][0]['status'])

        fake_snapshot.status = 'active'
        resp = self.execute('DescribeSnapshots', {})
        self.assertEqual('completed', resp['snapshotSet'][0]['status'])

        fake_snapshot.status = 'deleting'
        resp = self.execute('DescribeSnapshots', {})
        self.assertEqual('pending', resp['snapshotSet'][0]['status'])

        fake_snapshot.status = 'error'
        resp = self.execute('DescribeSnapshots', {})
        self.assertEqual('error', resp['snapshotSet'][0]['status'])

        fake_snapshot.status = 'banana'
        resp = self.execute('DescribeSnapshots', {})
        self.assertEqual('banana', resp['snapshotSet'][0]['status'])
Esempio n. 3
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')
Esempio n. 4
0
    def _get_new_ebs_image(self,
                           image_project_id=None,
                           bdm_image_project_id=None):
        os_image_id = fakes.random_os_id()
        os_snapshot_id = fakes.random_os_id()
        os_bdm_image_id = fakes.random_os_id()
        os_image = {
            'id':
            os_image_id,
            'owner':
            image_project_id,
            'is_public':
            True,
            'container_format':
            'ami',
            'bdm_v2':
            True,
            'block_device_mapping': [{
                'device_name': '/dev/vds',
                'source_type': 'snapshot',
                'destination_type': 'volume',
                'snapshot_id': os_snapshot_id
            }],
        }
        if os_bdm_image_id:
            os_image['block_device_mapping'].append({
                'device_name': '/dev/vdi',
                'source_type': 'image',
                'destination_type': 'volume',
                'image_id': os_bdm_image_id,
                'size': 100
            })
        os_snapshot = {
            'id': os_snapshot_id,
        }
        os_bdm_image = {
            'id': os_bdm_image_id,
            'owner': bdm_image_project_id,
            'is_public': True,
        }
        os_images = [fakes.OSImage(os_image)]
        if bdm_image_project_id:
            os_images.append(fakes.OSImage(os_bdm_image))
        self.glance.images.list.return_value = os_images
        self.cinder.volume_snapshots.list.return_value = ([
            fakes.OSSnapshot(os_snapshot)
        ] if image_project_id == fakes.ID_OS_PROJECT else [])

        images = image_api.describe_images(self.context)
        return next(i for i in images['imagesSet'] if i['blockDeviceMapping'])
Esempio n. 5
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)
Esempio n. 6
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)