コード例 #1
0
    def test_get_metadata_integral(self, keystone, network_interface_api,
                                   security_group_api):
        service_catalog = mock.MagicMock()
        service_catalog.get_data.return_value = []
        keystone.return_value = mock.Mock(auth_user_id='fake_user_id',
                                          auth_tenant_id=fakes.ID_OS_PROJECT,
                                          auth_token='fake_token',
                                          service_catalog=service_catalog)
        fake_context = context.get_os_admin_context()

        self.set_mock_db_items(fakes.DB_INSTANCE_1, fakes.DB_INSTANCE_2,
                               fakes.DB_NETWORK_INTERFACE_1,
                               fakes.DB_NETWORK_INTERFACE_2, fakes.DB_IMAGE_1,
                               fakes.DB_IMAGE_2, fakes.DB_IMAGE_ARI_1,
                               fakes.DB_IMAGE_AKI_1, fakes.DB_VOLUME_1,
                               fakes.DB_VOLUME_2, fakes.DB_VOLUME_3)
        self.nova_admin.servers.list.return_value = [
            fakes.OSInstance_full(fakes.OS_INSTANCE_1),
            fakes.OSInstance_full(fakes.OS_INSTANCE_2)
        ]
        self.nova_admin.servers.get.side_effect = tools.get_by_1st_arg_getter({
            fakes.ID_OS_INSTANCE_1:
            fakes.OSInstance_full(fakes.OS_INSTANCE_1),
            fakes.ID_OS_INSTANCE_2:
            fakes.OSInstance_full(fakes.OS_INSTANCE_2)
        })
        keypair = mock.Mock(public_key=fakes.PUBLIC_KEY_KEY_PAIR)
        keypair.configure_mock(name=fakes.NAME_KEY_PAIR)
        self.nova.keypairs.get.return_value = keypair
        self.cinder.volumes.list.return_value = [
            fakes.OSVolume(fakes.OS_VOLUME_1),
            fakes.OSVolume(fakes.OS_VOLUME_2),
            fakes.OSVolume(fakes.OS_VOLUME_3)
        ]
        network_interface_api.describe_network_interfaces.side_effect = (
            lambda *args, **kwargs: copy.deepcopy({
                'networkInterfaceSet':
                [fakes.EC2_NETWORK_INTERFACE_1, fakes.EC2_NETWORK_INTERFACE_2]
            }))
        security_group_api.describe_security_groups.return_value = {
            'securityGroupInfo':
            [fakes.EC2_SECURITY_GROUP_1, fakes.EC2_SECURITY_GROUP_3]
        }

        retval = api.get_metadata_item(fake_context,
                                       ['latest', 'meta-data', 'instance-id'],
                                       fakes.ID_OS_INSTANCE_1,
                                       fakes.IP_NETWORK_INTERFACE_2)
        self.assertEqual(fakes.ID_EC2_INSTANCE_1, retval)

        retval = api.get_metadata_item(fake_context,
                                       ['latest', 'meta-data', 'instance-id'],
                                       fakes.ID_OS_INSTANCE_2, '10.200.1.15')
        self.assertEqual(fakes.ID_EC2_INSTANCE_2, retval)
コード例 #2
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')
コード例 #3
0
    def test_get_metadata_integral(self, network_interface_api,
                                   security_group_api, create_region):
        fake_context = base.create_context(is_os_admin=True)

        self.set_mock_db_items(fakes.DB_INSTANCE_1, fakes.DB_INSTANCE_2,
                               fakes.DB_NETWORK_INTERFACE_1,
                               fakes.DB_NETWORK_INTERFACE_2, fakes.DB_IMAGE_1,
                               fakes.DB_IMAGE_2, fakes.DB_IMAGE_ARI_1,
                               fakes.DB_IMAGE_AKI_1, fakes.DB_VOLUME_1,
                               fakes.DB_VOLUME_2, fakes.DB_VOLUME_3)
        self.nova_admin.servers.list.return_value = [
            fakes.OSInstance_full(fakes.OS_INSTANCE_1),
            fakes.OSInstance_full(fakes.OS_INSTANCE_2)
        ]
        self.nova_admin.servers.get.side_effect = tools.get_by_1st_arg_getter({
            fakes.ID_OS_INSTANCE_1:
            fakes.OSInstance_full(fakes.OS_INSTANCE_1),
            fakes.ID_OS_INSTANCE_2:
            fakes.OSInstance_full(fakes.OS_INSTANCE_2)
        })
        self.nova_admin.keypairs._get.return_value = (fakes.NovaKeyPair(
            fakes.OS_KEY_PAIR))
        self.cinder.volumes.list.return_value = [
            fakes.OSVolume(fakes.OS_VOLUME_1),
            fakes.OSVolume(fakes.OS_VOLUME_2),
            fakes.OSVolume(fakes.OS_VOLUME_3)
        ]
        network_interface_api.describe_network_interfaces.side_effect = (
            lambda *args, **kwargs: copy.deepcopy({
                'networkInterfaceSet':
                [fakes.EC2_NETWORK_INTERFACE_1, fakes.EC2_NETWORK_INTERFACE_2]
            }))
        security_group_api.describe_security_groups.return_value = {
            'securityGroupInfo':
            [fakes.EC2_SECURITY_GROUP_1, fakes.EC2_SECURITY_GROUP_3]
        }
        create_region.get.return_value = cache_core.NO_VALUE

        retval = api.get_metadata_item(fake_context,
                                       ['latest', 'meta-data', 'instance-id'],
                                       fakes.ID_OS_INSTANCE_1,
                                       fakes.IP_NETWORK_INTERFACE_2,
                                       create_region)
        self.assertEqual(fakes.ID_EC2_INSTANCE_1, retval)

        retval = api.get_metadata_item(fake_context,
                                       ['latest', 'meta-data', 'instance-id'],
                                       fakes.ID_OS_INSTANCE_2, '10.200.1.15',
                                       create_region)
        self.assertEqual(fakes.ID_EC2_INSTANCE_2, retval)
コード例 #4
0
    def test_describe_new_instance_then_its_alien_image_attribute(self):
        os_instance_id = fakes.random_os_id()
        os_image_id = fakes.random_os_id()
        alien_project_id = fakes.random_os_id()
        os_instance = {
            'id': os_instance_id,
            'flavor': {'id': 'fake'},
            'image': {'id': os_image_id},
        }
        os_image = {
            'id': os_image_id,
            'owner': alien_project_id,
            'is_public': True,
        }
        self.nova_admin.servers.list.return_value = [
            fakes.OSInstance_full(os_instance)]
        self.glance.images.get.return_value = fakes.OSImage(os_image)

        reservations = instance_api.describe_instances(self.context)
        instance = reservations['reservationSet'][0]['instancesSet'][0]
        image_id = instance['imageId']

        # NOTE(ft): ensure that InvalidAMIID.NotFound is not raised
        self.assertRaises(exception.AuthFailure,
                          image_api.describe_image_attribute,
                          self.context, image_id, 'description')
コード例 #5
0
    def _test_describe_new_instance_then_its_image(self, image_project_id):
        os_instance_id = fakes.random_os_id()
        os_image_id = fakes.random_os_id()
        os_instance = {
            'id': os_instance_id,
            'flavor': {'id': 'fake'},
            'image': {'id': os_image_id},
        }
        os_image = {
            'id': os_image_id,
            'owner': image_project_id,
            'is_public': True,
        }
        self.nova_admin.servers.list.return_value = [
            fakes.OSInstance_full(os_instance)]
        self.glance.images.list.return_value = [fakes.OSImage(os_image)]

        reservations = instance_api.describe_instances(self.context)
        instance = reservations['reservationSet'][0]['instancesSet'][0]
        image_id = instance['imageId']
        image = (image_api.describe_images(self.context, image_id=[image_id])
                 ['imagesSet'][0])
        self.assertEqual(image_id, image['imageId'])
        self.assertEqual(image_project_id, image['imageOwnerId'])
        expected_project_id = (fakes.ID_OS_PROJECT
                               if image_project_id == fakes.ID_OS_PROJECT else
                               None)
        self.assert_image_project(expected_project_id, image['imageId'])
コード例 #6
0
    def test_describe_new_instance_then_its_volume(self):
        os_instance_id = fakes.random_os_id()
        os_volume_id = fakes.random_os_id()
        os_instance = {
            'id': os_instance_id,
            'flavor': {
                'id': 'fake'
            },
            'volumes_attached': [{
                'id': os_volume_id
            }],
        }
        os_volume = {
            'id': os_volume_id,
            'status': 'in-use',
            'attachments': [{
                'device': '/dev/vdb',
                'server_id': os_instance_id
            }],
        }
        self.nova_admin.servers.list.return_value = [
            fakes.OSInstance_full(os_instance)
        ]
        self.cinder.volumes.list.return_value = [fakes.OSVolume(os_volume)]

        reservations = instance_api.describe_instances(self.context)
        instance = reservations['reservationSet'][0]['instancesSet'][0]
        volume_id = instance['blockDeviceMapping'][0]['ebs']['volumeId']
        volume_api.describe_volumes(self.context, [volume_id])
コード例 #7
0
    def test_describe_volumes_invalid_parameters(self):
        self.cinder.volumes.list.return_value = [
            fakes.OSVolume(fakes.OS_VOLUME_1),
            fakes.OSVolume(fakes.OS_VOLUME_2)]
        self.nova.servers.list.return_value = [
            fakes.OSInstance_full(fakes.OS_INSTANCE_2)]

        self.assert_execution_error(
            'InvalidVolume.NotFound', 'DescribeVolumes',
            {'VolumeId.1': fakes.random_ec2_id('vol')})

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

        self.assert_execution_error(
            'InvalidVolume.NotFound', 'DescribeVolumes',
            {'VolumeId.1': fakes.ID_EC2_VOLUME_1})