def test_get_items_ids(self): self._setup_items() item = db_api.get_items(self.context, 'fake1')[0] other_item = db_api.get_items(self.other_context, 'fake1')[0] items_ids = db_api.get_items_ids( self.context, 'fake1', item_os_ids=[item['os_id'], other_item['os_id']]) self.assertThat( items_ids, matchers.ListMatches([(item['id'], item['os_id']), (other_item['id'], other_item['os_id'])], orderless_lists=True)) items_ids = db_api.get_items_ids(self.context, 'fake', item_os_ids=[item['os_id']]) self.assertEqual(0, len(items_ids)) item_ids = db_api.get_items_ids( self.context, 'fake1', item_ids=[item['id'], other_item['id']]) self.assertThat( item_ids, matchers.ListMatches([(item['id'], item['os_id']), (other_item['id'], other_item['os_id'])], orderless_lists=True)) items_ids = db_api.get_items_ids(self.context, 'fake', item_ids=[item['id']]) self.assertEqual(0, len(items_ids))
def test_describe_dhcp_options(self): self.set_mock_db_items(fakes.DB_DHCP_OPTIONS_1, fakes.DB_DHCP_OPTIONS_2) resp = self.execute('DescribeDhcpOptions', {}) self.assertThat( resp['dhcpOptionsSet'], matchers.ListMatches( [fakes.EC2_DHCP_OPTIONS_1, fakes.EC2_DHCP_OPTIONS_2], orderless_lists=True)) resp = self.execute('DescribeDhcpOptions', {'DhcpOptionsId.1': fakes.ID_EC2_DHCP_OPTIONS_1}) self.assertThat( resp['dhcpOptionsSet'], matchers.ListMatches([fakes.EC2_DHCP_OPTIONS_1], orderless_lists=True)) self.db_api.get_items_by_ids.assert_called_once_with( mock.ANY, set([fakes.ID_EC2_DHCP_OPTIONS_1])) self.check_filtering('DescribeDhcpOptions', 'dhcpOptionsSet', [('dhcp_options_id', fakes.ID_EC2_DHCP_OPTIONS_1), ('key', 'netbios-node-type'), ('value', '8.8.8.8')]) self.check_tag_support('DescribeDhcpOptions', 'dhcpOptionsSet', fakes.ID_EC2_DHCP_OPTIONS_1, 'dhcpOptionsId')
def test_describe_vpn_gateways(self): self.set_mock_db_items(fakes.DB_VPN_GATEWAY_1, fakes.DB_VPN_GATEWAY_2) resp = self.execute('DescribeVpnGateways', {}) self.assertThat(resp['vpnGatewaySet'], matchers.ListMatches([fakes.EC2_VPN_GATEWAY_1, fakes.EC2_VPN_GATEWAY_2])) resp = self.execute('DescribeVpnGateways', {'VpnGatewayId.1': fakes.ID_EC2_VPN_GATEWAY_2}) self.assertThat(resp['vpnGatewaySet'], matchers.ListMatches([fakes.EC2_VPN_GATEWAY_2])) self.db_api.get_items_by_ids.assert_called_once_with( mock.ANY, set([fakes.ID_EC2_VPN_GATEWAY_2])) self.check_filtering( 'DescribeVpnGateways', 'vpnGatewaySet', [('attachment.state', 'attached'), ('attachment.vpc-id', fakes.ID_EC2_VPC_1), ('state', 'available'), ('type', 'ipsec.1'), ('vpn-gateway-id', fakes.ID_EC2_VPN_GATEWAY_2)]) self.check_tag_support( 'DescribeVpnGateways', 'vpnGatewaySet', fakes.ID_EC2_VPN_GATEWAY_2, 'vpnGatewayId')
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_get_subnet_host_routes(self): self.set_mock_db_items(fakes.DB_NETWORK_INTERFACE_1, fakes.DB_NETWORK_INTERFACE_2, fakes.DB_IGW_1) host_routes = route_table._get_subnet_host_routes( mock.ANY, fakes.DB_ROUTE_TABLE_1, fakes.IP_GATEWAY_SUBNET_1) self.assertThat( host_routes, matchers.ListMatches([{ 'destination': fakes.CIDR_VPC_1, 'nexthop': fakes.IP_GATEWAY_SUBNET_1 }, { 'destination': '0.0.0.0/0', 'nexthop': '127.0.0.1' }])) host_routes = route_table._get_subnet_host_routes( mock.ANY, fakes.DB_ROUTE_TABLE_2, fakes.IP_GATEWAY_SUBNET_1) self.assertThat( host_routes, matchers.ListMatches([{ 'destination': fakes.CIDR_VPC_1, 'nexthop': fakes.IP_GATEWAY_SUBNET_1 }, { 'destination': fakes.CIDR_EXTERNAL_NETWORK, 'nexthop': fakes.IP_NETWORK_INTERFACE_2 }, { 'destination': '0.0.0.0/0', 'nexthop': fakes.IP_GATEWAY_SUBNET_1 }]))
def test_describe_customer_gateways(self): self.set_mock_db_items(fakes.DB_CUSTOMER_GATEWAY_1, fakes.DB_CUSTOMER_GATEWAY_2) resp = self.execute('DescribeCustomerGateways', {}) self.assertThat( resp['customerGatewaySet'], matchers.ListMatches( [fakes.EC2_CUSTOMER_GATEWAY_1, fakes.EC2_CUSTOMER_GATEWAY_2])) resp = self.execute( 'DescribeCustomerGateways', {'CustomerGatewayId.1': fakes.ID_EC2_CUSTOMER_GATEWAY_2}) self.assertThat(resp['customerGatewaySet'], matchers.ListMatches([fakes.EC2_CUSTOMER_GATEWAY_2])) self.db_api.get_items_by_ids.assert_called_once_with( mock.ANY, set([fakes.ID_EC2_CUSTOMER_GATEWAY_2])) self.check_filtering( 'DescribeCustomerGateways', 'customerGatewaySet', [('bgp-asn', 65000), ('customer-gateway-id', fakes.ID_EC2_CUSTOMER_GATEWAY_2), ('ip-address', fakes.IP_CUSTOMER_GATEWAY_ADDRESS_2), ('state', 'available'), ('type', 'ipsec.1')]) self.check_tag_support('DescribeCustomerGateways', 'customerGatewaySet', fakes.ID_EC2_CUSTOMER_GATEWAY_2, 'customerGatewayId')
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_vpcs(self): self.neutron.list_routers.return_value = ({ 'routers': [fakes.OS_ROUTER_DEFAULT, fakes.OS_ROUTER_1, fakes.OS_ROUTER_2] }) self.set_mock_db_items(fakes.DB_VPC_DEFAULT, fakes.DB_VPC_1, fakes.DB_VPC_2) resp = self.execute('DescribeVpcs', {}) self.assertThat( resp['vpcSet'], matchers.ListMatches( [fakes.EC2_VPC_DEFAULT, fakes.EC2_VPC_1, fakes.EC2_VPC_2])) resp = self.execute('DescribeVpcs', {'VpcId.1': fakes.ID_EC2_VPC_1}) self.assertThat(resp['vpcSet'], matchers.ListMatches([fakes.EC2_VPC_1])) self.db_api.get_items_by_ids.assert_called_once_with( mock.ANY, set([fakes.ID_EC2_VPC_1])) self.check_filtering('DescribeVpcs', 'vpcSet', [('cidr', fakes.CIDR_VPC_1), ('dhcp-options-id', 'default'), ('is-default', False), ('state', 'available'), ('vpc-id', fakes.ID_EC2_VPC_1)]) self.check_tag_support('DescribeVpcs', 'vpcSet', fakes.ID_EC2_VPC_1, 'vpcId')
def test_describe_account_attributes(self, check_and_create): self.nova.quotas.get.return_value = mock.Mock(instances=77) resp = self.execute('DescribeAccountAttributes', {}) self.assertThat( resp['accountAttributeSet'], matchers.ListMatches( [{ 'attributeName': 'supported-platforms', 'attributeValueSet': [{ 'attributeValue': 'EC2' }, { 'attributeValue': 'VPC' }] }, { 'attributeName': 'default-vpc', 'attributeValueSet': [{ 'attributeValue': 'none' }] }, { 'attributeName': 'max-instances', 'attributeValueSet': [{ 'attributeValue': 77 }] }], orderless_lists=True)) self.nova.quotas.get.assert_called_once_with(fakes.ID_OS_PROJECT, fakes.ID_OS_USER) self.configure(disable_ec2_classic=True) check_and_create.return_value = fakes.DB_VPC_DEFAULT resp = self.execute('DescribeAccountAttributes', {}) self.assertThat( resp['accountAttributeSet'], matchers.ListMatches( [{ 'attributeName': 'supported-platforms', 'attributeValueSet': [{ 'attributeValue': 'VPC' }] }, { 'attributeName': 'default-vpc', 'attributeValueSet': [{ 'attributeValue': fakes.ID_EC2_VPC_DEFAULT }] }, { 'attributeName': 'max-instances', 'attributeValueSet': [{ 'attributeValue': 77 }] }], orderless_lists=True)) check_and_create.assert_called_once_with(mock.ANY)
def test_describe_account_attributes(self): self.nova.quotas.get.return_value = mock.Mock(instances=77) availability_zone.account_attribute_engine = ( availability_zone.AccountAttributeEngineNeutron()) resp = self.execute('DescribeAccountAttributes', {}) self.assertThat(resp['accountAttributeSet'], matchers.ListMatches( [{'attributeName': 'supported-platforms', 'attributeValueSet': [ {'attributeValue': 'EC2'}, {'attributeValue': 'VPC'}]}, {'attributeName': 'default-vpc', 'attributeValueSet': [ {'attributeValue': 'none'}]}, {'attributeName': 'max-instances', 'attributeValueSet': [ {'attributeValue': 77}]}], orderless_lists=True)) self.nova.quotas.get.assert_called_once_with( fakes.ID_OS_PROJECT, fakes.ID_OS_USER) availability_zone.account_attribute_engine = ( availability_zone.AccountAttributeEngineNova()) resp = self.execute('DescribeAccountAttributes', {}) self.assertThat(resp['accountAttributeSet'], matchers.ListMatches( [{'attributeName': 'supported-platforms', 'attributeValueSet': [ {'attributeValue': 'EC2'}]}, {'attributeName': 'default-vpc', 'attributeValueSet': [ {'attributeValue': 'none'}]}, {'attributeName': 'max-instances', 'attributeValueSet': [ {'attributeValue': 77}]}], orderless_lists=True)) resp = self.execute('DescribeAccountAttributes', {'AttributeName.1': 'default-vpc', 'AttributeName.2': 'max-instances'}) self.assertThat(resp['accountAttributeSet'], matchers.ListMatches( [{'attributeName': 'default-vpc', 'attributeValueSet': [ {'attributeValue': 'none'}]}, {'attributeName': 'max-instances', 'attributeValueSet': [ {'attributeValue': 77}]}], orderless_lists=True)) self.assert_execution_error('InvalidParameter', 'DescribeAccountAttributes', {'AttributeName.1': 'fake'})
def test_describe_addresses_ec2_classic(self): address.address_engine = ( address.AddressEngineNova()) self.set_mock_db_items(fakes.DB_INSTANCE_1) self.nova.floating_ips.list.return_value = [ fakes.NovaFloatingIp(fakes.NOVA_FLOATING_IP_1), fakes.NovaFloatingIp(fakes.NOVA_FLOATING_IP_2)] resp = self.execute('DescribeAddresses', {}) self.assertThat(resp['addressesSet'], matchers.ListMatches([fakes.EC2_ADDRESS_CLASSIC_1, fakes.EC2_ADDRESS_CLASSIC_2])) resp = self.execute('DescribeAddresses', {'PublicIp.1': fakes.IP_ADDRESS_2}) self.assertThat(resp['addressesSet'], matchers.ListMatches([fakes.EC2_ADDRESS_CLASSIC_2]))
def assert_any_call(self, func, *args, **kwargs): calls = func.mock_calls for call in calls: call_args = call[1] if matchers.ListMatches(call_args, args, orderless_lists=True): return self.assertEqual(False, True)
def test_describe_addresses_ec2_classic(self): self.set_mock_db_items(fakes.DB_INSTANCE_1) 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] }) resp = self.execute('DescribeAddresses', {}) self.assertThat( resp['addressesSet'], matchers.ListMatches( [fakes.EC2_ADDRESS_CLASSIC_1, fakes.EC2_ADDRESS_CLASSIC_2])) resp = self.execute('DescribeAddresses', {'PublicIp.1': fakes.IP_ADDRESS_2}) self.assertThat(resp['addressesSet'], matchers.ListMatches([fakes.EC2_ADDRESS_CLASSIC_2]))
def test_get_tags(self): item1_id = fakes.random_ec2_id('fake') item2_id = fakes.random_ec2_id('fake') item3_id = fakes.random_ec2_id('fake1') tag1 = {'item_id': item1_id, 'key': 'key1', 'value': 'val1'} tag2 = {'item_id': item2_id, 'key': 'key2', 'value': 'val2'} tag3 = {'item_id': item3_id, 'key': 'key3', 'value': 'val3'} db_api.add_tags(self.context, [tag1, tag2, tag3]) self.assertThat( db_api.get_tags(self.context), matchers.ListMatches([tag1, tag2, tag3], orderless_lists=True)) self.assertThat( db_api.get_tags(self.context, ('fake', )), matchers.ListMatches([tag1, tag2], orderless_lists=True)) self.assertThat( db_api.get_tags(self.context, ('fake', ), [item1_id, item2_id]), matchers.ListMatches([tag1, tag2], orderless_lists=True)) self.assertThat( db_api.get_tags(self.context, ('fake', ), (item1_id, )), matchers.ListMatches([tag1], orderless_lists=True)) self.assertThat( db_api.get_tags(self.context, ('fake', ), (item3_id, )), matchers.ListMatches([])) self.assertThat( db_api.get_tags(self.context, item_ids=(item1_id, item3_id)), matchers.ListMatches([tag1, tag3], orderless_lists=True)) self.assertThat( db_api.get_tags(self.context, ('fake', 'fake1'), (item2_id, item3_id)), matchers.ListMatches([tag2, tag3], orderless_lists=True))
def test_describe_vpcs_no_router(self): self.neutron.list_routers.return_value = {'routers': []} self.set_mock_db_items(fakes.DB_VPC_1) resp = self.execute('DescribeVpcs', {}) self.assertThat(resp['vpcSet'], matchers.ListMatches([fakes.EC2_VPC_1])) self.db_api.get_items.assert_called_once_with(mock.ANY, 'vpc')
def test_delete_tags_isolation(self): item_id = fakes.random_ec2_id('fake') tag1 = {'item_id': item_id, 'key': 'key', 'value': 'val1'} db_api.add_tags(self.context, [tag1]) tag2 = {'item_id': item_id, 'key': 'key', 'value': 'val2'} db_api.add_tags(self.other_context, [tag2]) db_api.delete_tags(self.context, item_id) self.assertThat(db_api.get_tags(self.other_context), matchers.ListMatches([tag2]))
def test_describe_vpcs_no_router(self): self.neutron.list_routers.return_value = {'routers': []} self.set_mock_db_items(fakes.DB_VPC_DEFAULT, fakes.DB_VPC_1) resp = self.execute('DescribeVpcs', {}) self.assertThat( resp['vpcSet'], matchers.ListMatches([fakes.EC2_VPC_DEFAULT, fakes.EC2_VPC_1]))
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)
def test_describe_route_tables(self): self.set_mock_db_items(fakes.DB_ROUTE_TABLE_1, fakes.DB_ROUTE_TABLE_2, fakes.DB_ROUTE_TABLE_3, fakes.DB_SUBNET_1, fakes.DB_SUBNET_2, fakes.DB_VPC_1, fakes.DB_VPC_2, fakes.DB_IGW_1, fakes.DB_IGW_2, fakes.DB_NETWORK_INTERFACE_1, fakes.DB_NETWORK_INTERFACE_2, fakes.DB_INSTANCE_1) self.nova.servers.get.return_value = (mock.NonCallableMock( status='ACTIVE')) resp = self.execute('DescribeRouteTables', {}) self.assertThat( resp['routeTableSet'], matchers.ListMatches([ fakes.EC2_ROUTE_TABLE_1, fakes.EC2_ROUTE_TABLE_2, fakes.EC2_ROUTE_TABLE_3 ])) resp = self.execute('DescribeRouteTables', {'RouteTableId.1': fakes.ID_EC2_ROUTE_TABLE_1}) self.assertThat(resp['routeTableSet'], matchers.ListMatches([fakes.EC2_ROUTE_TABLE_1])) self.db_api.get_items_by_ids.assert_called_once_with( mock.ANY, set([fakes.ID_EC2_ROUTE_TABLE_1])) self.check_filtering( 'DescribeRouteTables', 'routeTableSet', [('association.route-table-association-id', fakes.ID_EC2_ROUTE_TABLE_ASSOCIATION_1), ('association.route-table-id', fakes.ID_EC2_ROUTE_TABLE_1), ('association.subnet-id', fakes.ID_EC2_SUBNET_2), ('association.main', True), ('route-table-id', fakes.ID_EC2_ROUTE_TABLE_1), ('route.destination-cidr-block', fakes.CIDR_EXTERNAL_NETWORK), ('route.gateway-id', 'local'), ('route.instance-id', fakes.ID_EC2_INSTANCE_1), ('route.origin', 'CreateRouteTable'), ('route.state', 'active'), ('vpc-id', fakes.ID_EC2_VPC_1)]) self.check_tag_support('DescribeRouteTables', 'routeTableSet', fakes.ID_EC2_ROUTE_TABLE_1, 'routeTableId')
def test_create_tags(self): self.db_api.get_item_by_id.return_value = {'id': 'fake'} # NOTE(ft): check create several tags for several resources resp = self.execute('CreateTags', {'ResourceId.1': fakes.ID_EC2_VPC_1, 'ResourceId.2': fakes.ID_EC2_SUBNET_1, 'Tag.1.Key': 'private', 'Tag.1.Value': '', 'Tag.2.Key': 'admin', 'Tag.2.Value': 'John Smith'}) self.assertEqual({'return': True}, resp) self.assertEqual(1, self.db_api.add_tags.call_count) self.assertEqual(2, len(self.db_api.add_tags.call_args)) self.assertThat(self.db_api.add_tags.call_args[0][1], matchers.ListMatches( [{'item_id': fakes.ID_EC2_VPC_1, 'key': 'private', 'value': ''}, {'item_id': fakes.ID_EC2_SUBNET_1, 'key': 'private', 'value': ''}, {'item_id': fakes.ID_EC2_VPC_1, 'key': 'admin', 'value': 'John Smith'}, {'item_id': fakes.ID_EC2_SUBNET_1, 'key': 'admin', 'value': 'John Smith'}], orderless_lists=True)) # NOTE(ft): check a tag can be created for all valid resource types resource_ids = [fakes.random_ec2_id(r_t) for r_t in ['dopt', 'ami', 'aki', 'ari', 'cgw', 'i', 'igw', 'eni', 'rtb', 'snap', 'subnet', 'sg', 'vgw', 'vol', 'vpc', 'vpn']] self.assertEqual(len(resource_ids), len(tag_api.RESOURCE_TYPES)) params = {'ResourceId.%s' % num: r_id for num, r_id in enumerate(resource_ids)} params.update({'Tag.1.Key': 'tag', 'Tag.1.Value': 'value'}) resp = self.execute('CreateTags', params) self.assertEqual({'return': True}, resp) # NOTE(ft): check create a tag for non-existing images self.db_api.get_item_by_id.return_value = None resp = self.execute('CreateTags', {'ResourceId.1': fakes.ID_EC2_IMAGE_1, 'ResourceId.2': fakes.ID_EC2_IMAGE_AKI_1, 'ResourceId.3': fakes.ID_EC2_IMAGE_ARI_1, 'Tag.1.Key': 'Oracle RAC node', 'Tag.1.Value': ''}) self.assertEqual({'return': True}, resp)
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_igw(self): self.set_mock_db_items(fakes.DB_IGW_1, fakes.DB_IGW_2) resp = self.execute('DescribeInternetGateways', {}) self.assertThat( resp['internetGatewaySet'], matchers.ListMatches([fakes.EC2_IGW_1, fakes.EC2_IGW_2])) resp = self.execute('DescribeInternetGateways', {'InternetGatewayId.1': fakes.ID_EC2_IGW_2}) self.assertThat(resp['internetGatewaySet'], matchers.ListMatches([fakes.EC2_IGW_2])) self.db_api.get_items_by_ids.assert_called_once_with( mock.ANY, set([fakes.ID_EC2_IGW_2])) self.check_filtering('DescribeInternetGateways', 'internetGatewaySet', [('internet-gateway-id', fakes.ID_EC2_IGW_2), ('attachment.state', 'available'), ('attachment.vpc-id', fakes.ID_EC2_VPC_1)]) self.check_tag_support('DescribeInternetGateways', 'internetGatewaySet', fakes.ID_EC2_IGW_2, 'internetGatewayId')
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_key_pairs(self): self.nova.keypairs.list.return_value = [ fakes.NovaKeyPair(fakes.OS_KEY_PAIR) ] resp = self.execute('DescribeKeyPairs', {}) self.assertThat( resp['keySet'], matchers.ListMatches( [tools.purge_dict(fakes.EC2_KEY_PAIR, {'keyMaterial'})])) self.nova.keypairs.list.assert_called_once_with() self.check_filtering('DescribeKeyPairs', 'keySet', [('fingerprint', fakes.FINGERPRINT_KEY_PAIR), ('key-name', fakes.NAME_KEY_PAIR)])
def test_describe_availability_zones(self): self.nova.availability_zones.list.return_value = [ fakes.NovaAvailabilityZone(fakes.OS_AVAILABILITY_ZONE), fakes.NovaAvailabilityZone(fakes.OS_AVAILABILITY_ZONE_INTERNAL)] resp = self.execute('DescribeAvailabilityZones', {}) self.assertThat(resp['availabilityZoneInfo'], matchers.ListMatches([fakes.EC2_AVAILABILITY_ZONE])) self.nova.availability_zones.list.assert_called_once_with( detailed=False) self.check_filtering( 'DescribeAvailabilityZones', 'availabilityZoneInfo', [('state', 'available'), ('zone-name', fakes.NAME_AVAILABILITY_ZONE)])
def test_describe_security_groups_nova(self): security_group.security_group_engine = ( security_group.SecurityGroupEngineNova()) self.set_mock_db_items() self.nova.security_groups.list.return_value = ([ fakes.NovaSecurityGroup(fakes.NOVA_SECURITY_GROUP_1), fakes.NovaSecurityGroup(fakes.NOVA_SECURITY_GROUP_2) ]) resp = self.execute('DescribeSecurityGroups', {}) self.assertThat( resp['securityGroupInfo'], matchers.ListMatches([ fakes.EC2_NOVA_SECURITY_GROUP_1, fakes.EC2_NOVA_SECURITY_GROUP_2 ], orderless_lists=True))
def test_add_tags_isolation(self): item_id = fakes.random_ec2_id('fake') tag1 = {'item_id': item_id, 'key': 'key1', 'value': 'val1'} tag2 = {'item_id': item_id, 'key': 'key2', 'value': 'val2'} db_api.add_tags(self.context, [tag1, tag2]) db_api.add_tags(self.other_context, [{ 'item_id': item_id, 'key': 'key1', 'value': 'val1_1' }, { 'item_id': item_id, 'key': 'key3', 'value': 'val3' }]) tags = db_api.get_tags(self.context) self.assertThat( tags, matchers.ListMatches([tag1, tag2], orderless_lists=True))
def test_describe_security_groups_no_default_vpc(self, check_and_create): self.configure(disable_ec2_classic=True) def mock_check_and_create(context): self.set_mock_db_items(fakes.DB_VPC_DEFAULT, fakes.DB_SECURITY_GROUP_DEFAULT) self.neutron.list_security_groups.return_value = ({ 'security_groups': [fakes.OS_SECURITY_GROUP_DEFAULT] }) check_and_create.side_effect = mock_check_and_create resp = self.execute('DescribeSecurityGroups', {}) self.assertThat( resp['securityGroupInfo'], matchers.ListMatches([fakes.EC2_SECURITY_GROUP_DEFAULT], orderless_lists=True)) check_and_create.assert_called_once_with(mock.ANY)
def test_add_tags(self): item1_id = fakes.random_ec2_id('fake') item2_id = fakes.random_ec2_id('fake') item3_id = fakes.random_ec2_id('fake') tag1_01 = {'item_id': item1_id, 'key': 'key1', 'value': None} tag1_1 = {'item_id': item1_id, 'key': 'key1', 'value': 'val'} tag1_2 = {'item_id': item1_id, 'key': 'key2', 'value': 'val'} tag1_3 = {'item_id': item1_id, 'key': 'key3', 'value': 'val'} tag2_1 = {'item_id': item2_id, 'key': 'key1', 'value': None} tag2_2 = {'item_id': item2_id, 'key': 'key2', 'value': 'val'} tag3_1 = {'item_id': item3_id, 'key': 'key1', 'value': 'val'} tag3_3 = {'item_id': item3_id, 'key': 'key3', 'value': 'val'} db_api.add_tags(self.context, [tag1_01, tag2_1, tag1_2, tag2_2]) db_api.add_tags(self.context, [tag1_1, tag3_1, tag1_3, tag3_3]) tags = db_api.get_tags(self.context) self.assertThat( tags, matchers.ListMatches( [tag1_1, tag1_2, tag1_3, tag2_1, tag2_2, tag3_1, tag3_3], orderless_lists=True))
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')