def test_describe_addresses_vpc(self): self.neutron.list_floatingips.return_value = ({ 'floatingips': [fakes.OS_FLOATING_IP_1, fakes.OS_FLOATING_IP_2] }) self.neutron.list_ports.return_value = ({ 'ports': [fakes.OS_PORT_1, fakes.OS_PORT_2] }) self.set_mock_db_items(fakes.DB_ADDRESS_1, fakes.DB_ADDRESS_2, fakes.DB_INSTANCE_1, fakes.DB_NETWORK_INTERFACE_1, fakes.DB_NETWORK_INTERFACE_2) resp = self.execute('DescribeAddresses', {}) self.assertThat( resp['addressesSet'], matchers.ListMatches([fakes.EC2_ADDRESS_1, fakes.EC2_ADDRESS_2])) self.db_api.get_items_by_ids = tools.CopyingMock( return_value=[fakes.DB_ADDRESS_1]) resp = self.execute('DescribeAddresses', {'AllocationId.1': fakes.ID_EC2_ADDRESS_1}) self.assertThat(resp['addressesSet'], matchers.ListMatches([fakes.EC2_ADDRESS_1])) self.db_api.get_items_by_ids.assert_called_once_with( mock.ANY, set([fakes.ID_EC2_ADDRESS_1])) self.check_filtering( 'DescribeAddresses', 'addressesSet', [('allocation-id', fakes.ID_EC2_ADDRESS_1), ('association-id', fakes.ID_EC2_ASSOCIATION_2), ('domain', 'vpc'), ('instance-id', fakes.ID_EC2_INSTANCE_1), ('network-interface-id', fakes.ID_EC2_NETWORK_INTERFACE_2), ('network-interface-owner-id', fakes.ID_OS_PROJECT), ('private-ip-address', fakes.IP_NETWORK_INTERFACE_2), ('public-ip', fakes.IP_ADDRESS_2)])
def test_describe_security_groups(self): security_group.security_group_engine = ( security_group.SecurityGroupEngineNeutron()) self.set_mock_db_items(fakes.DB_SECURITY_GROUP_1, fakes.DB_SECURITY_GROUP_2) self.neutron.list_security_groups.return_value = ({ 'security_groups': [ copy.deepcopy(fakes.OS_SECURITY_GROUP_1), fakes.OS_SECURITY_GROUP_2, fakes.OS_SECURITY_GROUP_3 ] }) resp = self.execute('DescribeSecurityGroups', {}) self.assertThat( resp['securityGroupInfo'], matchers.ListMatches([ fakes.EC2_SECURITY_GROUP_1, fakes.EC2_SECURITY_GROUP_2, fakes.EC2_SECURITY_GROUP_3 ], orderless_lists=True)) resp = self.execute('DescribeSecurityGroups', {'GroupName.1': 'groupname2'}) self.assertThat( resp['securityGroupInfo'], matchers.ListMatches([fakes.EC2_SECURITY_GROUP_2], orderless_lists=True)) self.db_api.get_items_by_ids = tools.CopyingMock( return_value=[fakes.DB_SECURITY_GROUP_2]) resp = self.execute('DescribeSecurityGroups', {'GroupId.1': fakes.ID_EC2_SECURITY_GROUP_2}) self.assertThat( resp['securityGroupInfo'], matchers.ListMatches([fakes.EC2_SECURITY_GROUP_2], orderless_lists=True)) self.db_api.get_items_by_ids.assert_called_once_with( mock.ANY, set([fakes.ID_EC2_SECURITY_GROUP_2])) self.check_filtering( 'DescribeSecurityGroups', 'securityGroupInfo', [ ('vpc-id', fakes.ID_EC2_VPC_1), ('group-name', fakes.NAME_DEFAULT_OS_SECURITY_GROUP), ('group-id', fakes.ID_EC2_SECURITY_GROUP_1), ('description', fakes.EC2_SECURITY_GROUP_1['groupDescription']), ('ip-permission.protocol', 'tcp'), ('ip-permission.to-port', 10), ('ip-permission.from-port', 10), ('ip-permission.cidr', '192.168.1.0/24'), # TODO(andrey-mp): declare this data in fakes # ('ip-permission.group-id', fakes.ID_EC2_SECURITY_GROUP_1), # ('ip-permission.group-name', 'default'), # ('ip-permission.user-id', fakes.ID_OS_PROJECT), ('owner-id', fakes.ID_OS_PROJECT) ]) self.check_tag_support('DescribeSecurityGroups', 'securityGroupInfo', fakes.ID_EC2_SECURITY_GROUP_2, 'groupId')
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')
def check_tag_support(self, operation, resultset_key, sample_item_id, id_key, item_kinds=[]): self.db_api.get_tags = tools.CopyingMock( return_value=[{ 'item_id': sample_item_id, 'key': 'fake_key', 'value': 'fake_value' }]) ec2_tags = [{'key': 'fake_key', 'value': 'fake_value'}] resp = self.execute(operation, {}) tag_found = False if type(resultset_key) is list: resp_items = itertools.chain(*(r[resultset_key[1]] for r in resp[resultset_key[0]])) else: resp_items = resp[resultset_key] resultset_key = [resultset_key] for resp_item in resp_items: if resp_item.get(id_key) == sample_item_id: self.assertIn('tagSet', resp_item) self.assertThat(resp_item['tagSet'], matchers.ListMatches(ec2_tags)) tag_found = True else: self.assertTrue('tagSet' not in resp_item or resp_item['tagSet'] == []) self.assertTrue(tag_found) if not item_kinds: item_kinds = (ec2utils.get_ec2_id_kind(sample_item_id), ) self.assertTrue(self.db_api.get_tags.call_count == 1 and (self.db_api.get_tags.mock_calls[0] in (mock.call( mock.ANY, item_kinds, set()), mock.call(mock.ANY, item_kinds, None)))) self.db_api.reset_mock() id_param = '%s%s.1' % (id_key[0].capitalize(), id_key[1:]) resp = self.execute(operation, {id_param: sample_item_id}) self.assertTrue( self.db_api.get_tags.call_count == 1 and (self.db_api.get_tags.mock_calls[0] in (mock.call(mock.ANY, item_kinds, set([sample_item_id])), mock.call(mock.ANY, item_kinds, [sample_item_id])))) self.check_filtering(operation, resultset_key[0], [('tag-key', 'fake_key'), ('tag-value', 'fake_value'), ('tag:fake_key', 'fake_value')])
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'))
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')
def test_describe_subnets(self): self.set_mock_db_items(fakes.DB_SUBNET_1, fakes.DB_SUBNET_2) self.neutron.list_subnets.return_value = ({ 'subnets': [fakes.OS_SUBNET_1, fakes.OS_SUBNET_2] }) self.neutron.list_networks.return_value = ({ 'networks': [fakes.OS_NETWORK_1, fakes.OS_NETWORK_2] }) resp = self.execute('DescribeSubnets', {}) self.assertThat( resp['subnetSet'], matchers.ListMatches([fakes.EC2_SUBNET_1, fakes.EC2_SUBNET_2])) self.db_api.get_items_by_ids = tools.CopyingMock( return_value=[fakes.DB_SUBNET_2]) resp = self.execute('DescribeSubnets', {'SubnetId.1': fakes.ID_EC2_SUBNET_2}) self.assertThat(resp['subnetSet'], matchers.ListMatches([fakes.EC2_SUBNET_2])) self.db_api.get_items_by_ids.assert_called_once_with( mock.ANY, set([fakes.ID_EC2_SUBNET_2])) self.check_filtering( 'DescribeSubnets', 'subnetSet', [ # TODO(ft): declare a constant for the count in fakes ('available-ip-address-count', 253), ('cidr', fakes.CIDR_SUBNET_2), ('cidrBlock', fakes.CIDR_SUBNET_2), ('cidr-block', fakes.CIDR_SUBNET_2), ('subnet-id', fakes.ID_EC2_SUBNET_2), ('state', 'available'), ('vpc-id', fakes.ID_EC2_VPC_1) ]) self.check_tag_support('DescribeSubnets', 'subnetSet', fakes.ID_EC2_SUBNET_2, 'subnetId')
def test_describe_network_interfaces(self): self.set_mock_db_items(fakes.DB_NETWORK_INTERFACE_1, fakes.DB_NETWORK_INTERFACE_2, fakes.DB_ADDRESS_1, fakes.DB_ADDRESS_2, fakes.DB_INSTANCE_1, fakes.DB_INSTANCE_2, fakes.DB_SECURITY_GROUP_1) self.neutron.list_ports.return_value = ({ 'ports': [fakes.OS_PORT_1, fakes.OS_PORT_2] }) self.neutron.list_floatingips.return_value = ({ 'floatingips': [fakes.OS_FLOATING_IP_1, fakes.OS_FLOATING_IP_2] }) self.neutron.list_security_groups.return_value = ({ 'security_groups': [copy.deepcopy(fakes.OS_SECURITY_GROUP_1)] }) resp = self.execute('DescribeNetworkInterfaces', {}) self.assertThat( resp['networkInterfaceSet'], matchers.ListMatches( [fakes.EC2_NETWORK_INTERFACE_1, fakes.EC2_NETWORK_INTERFACE_2], orderless_lists=True), verbose=True) self.db_api.get_items_by_ids = tools.CopyingMock( return_value=[fakes.DB_NETWORK_INTERFACE_1]) resp = self.execute( 'DescribeNetworkInterfaces', {'NetworkInterfaceId.1': fakes.ID_EC2_NETWORK_INTERFACE_1}) self.assertThat(resp['networkInterfaceSet'], matchers.ListMatches([fakes.EC2_NETWORK_INTERFACE_1])) self.db_api.get_items_by_ids.assert_called_once_with( mock.ANY, set([fakes.ID_EC2_NETWORK_INTERFACE_1])) self.check_filtering( 'DescribeNetworkInterfaces', 'networkInterfaceSet', [( 'addresses.private-ip-address', fakes.IP_NETWORK_INTERFACE_2_EXT_1, ), ('addresses.primary', False), ('addresses.association.public-ip', fakes.IP_ADDRESS_2), ('addresses.association.owner-id', fakes.ID_OS_PROJECT), ('association.association-id', fakes.ID_EC2_ASSOCIATION_2), ('association.allocation-id', fakes.ID_EC2_ADDRESS_2), ('association.ip-owner-id', fakes.ID_OS_PROJECT), ('association.public-ip', fakes.IP_ADDRESS_2), ('attachment.attachment-id', fakes.ID_EC2_NETWORK_INTERFACE_2_ATTACH), ('attachment.instance-id', fakes.ID_EC2_INSTANCE_1), ('attachment.instance-owner-id', fakes.ID_OS_PROJECT), ('attachment.device-index', 0), ('attachment.status', 'attached'), ('attachment.attach.time', fakes.TIME_ATTACH_NETWORK_INTERFACE), ('attachment.delete-on-termination', False), ('description', fakes.DESCRIPTION_NETWORK_INTERFACE_1), ('group-id', fakes.ID_EC2_SECURITY_GROUP_1), ('group-name', fakes.NAME_DEFAULT_OS_SECURITY_GROUP), ('mac-address', fakes.MAC_ADDRESS), ('network-interface-id', fakes.ID_EC2_NETWORK_INTERFACE_1), ('owner-id', fakes.ID_OS_PROJECT), ('private-ip-address', fakes.IP_NETWORK_INTERFACE_1), ('requester-managed', False), ('source-dest-check', True), ('status', 'available'), ('vpc-id', fakes.ID_EC2_VPC_1), ('subnet-id', fakes.ID_EC2_SUBNET_2)]) self.check_tag_support('DescribeNetworkInterfaces', 'networkInterfaceSet', fakes.ID_EC2_NETWORK_INTERFACE_1, 'networkInterfaceId')