Implement tests for tags
Change-Id: I581ab5868ae45d9ac1db4ac05cf463928a0c34e3
This commit is contained in:
@@ -236,7 +236,7 @@ class UniversalDescriber(object):
|
||||
# NOTE(Alex): some requested items are not found
|
||||
if self.ids or self.names:
|
||||
params = {'id': next(iter(self.ids or self.names))}
|
||||
raise ec2utils._NOT_FOUND_EXCEPTION_MAP[self.KIND](**params)
|
||||
raise ec2utils.NOT_FOUND_EXCEPTION_MAP[self.KIND](**params)
|
||||
return formatted_items
|
||||
|
||||
|
||||
|
||||
@@ -159,7 +159,7 @@ def change_ec2_id_kind(obj_id, new_kind):
|
||||
return '%(kind)s-%(id)s' % {'kind': new_kind,
|
||||
'id': obj_id.split('-')[-1]}
|
||||
|
||||
_NOT_FOUND_EXCEPTION_MAP = {
|
||||
NOT_FOUND_EXCEPTION_MAP = {
|
||||
'vpc': exception.InvalidVpcIDNotFound,
|
||||
'igw': exception.InvalidInternetGatewayIDNotFound,
|
||||
'subnet': exception.InvalidSubnetIDNotFound,
|
||||
@@ -181,7 +181,7 @@ def get_db_item(context, kind, ec2_id):
|
||||
item = db_api.get_item_by_id(context, kind, ec2_id)
|
||||
if item is None:
|
||||
params = {'id': ec2_id}
|
||||
raise _NOT_FOUND_EXCEPTION_MAP[kind](**params)
|
||||
raise NOT_FOUND_EXCEPTION_MAP[kind](**params)
|
||||
return item
|
||||
|
||||
|
||||
@@ -195,7 +195,7 @@ def get_db_items(context, kind, ec2_ids):
|
||||
if len(items) < len(ec2_ids):
|
||||
missed_ids = ec2_ids - set((item['id'] for item in items))
|
||||
params = {'id': next(iter(missed_ids))}
|
||||
raise _NOT_FOUND_EXCEPTION_MAP[kind](**params)
|
||||
raise NOT_FOUND_EXCEPTION_MAP[kind](**params)
|
||||
return items
|
||||
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ from ec2api.openstack.common.gettextutils import _
|
||||
|
||||
|
||||
RESOURCE_TYPES = {
|
||||
'dhcp': 'dhcp-options',
|
||||
'dopt': 'dhcp-options',
|
||||
'ami': 'image',
|
||||
'aki': 'image',
|
||||
'ari': 'image',
|
||||
|
||||
@@ -40,7 +40,8 @@ class EC2UtilsTestCase(testtools.TestCase):
|
||||
check_normal_flow('vpc', 'vpc-001234af')
|
||||
check_normal_flow('igw', 'igw-00000022')
|
||||
|
||||
def check_not_found(kind, ec2_id, ex_class):
|
||||
def check_not_found(kind, ex_class):
|
||||
ec2_id = fakes.random_ec2_id(kind)
|
||||
self.assertRaises(ex_class,
|
||||
ec2utils.get_db_item,
|
||||
'fake_context', kind, ec2_id)
|
||||
@@ -49,24 +50,18 @@ class EC2UtilsTestCase(testtools.TestCase):
|
||||
db_api.reset_mock()
|
||||
|
||||
db_api.get_item_by_id.return_value = None
|
||||
check_not_found('vpc', 'vpc-00000022',
|
||||
exception.InvalidVpcIDNotFound)
|
||||
check_not_found('igw', 'igw-00000022',
|
||||
exception.InvalidInternetGatewayIDNotFound)
|
||||
check_not_found('subnet', 'subnet-00000022',
|
||||
exception.InvalidSubnetIDNotFound)
|
||||
check_not_found('eni', 'eni-00000022',
|
||||
exception.InvalidNetworkInterfaceIDNotFound)
|
||||
check_not_found('dopt', 'dopt-00000022',
|
||||
exception.InvalidDhcpOptionsIDNotFound)
|
||||
check_not_found('eipalloc', 'eipalloc-00000022',
|
||||
exception.InvalidAllocationIDNotFound)
|
||||
check_not_found('sg', 'sg-00000022',
|
||||
exception.InvalidGroupNotFound)
|
||||
check_not_found('rtb', 'rtb--00000022',
|
||||
exception.InvalidRouteTableIDNotFound)
|
||||
check_not_found('i', 'i-00000022',
|
||||
exception.InvalidInstanceIDNotFound)
|
||||
check_not_found('vpc', exception.InvalidVpcIDNotFound)
|
||||
check_not_found('igw', exception.InvalidInternetGatewayIDNotFound)
|
||||
check_not_found('subnet', exception.InvalidSubnetIDNotFound)
|
||||
check_not_found('eni', exception.InvalidNetworkInterfaceIDNotFound)
|
||||
check_not_found('dopt', exception.InvalidDhcpOptionsIDNotFound)
|
||||
check_not_found('eipalloc', exception.InvalidAllocationIDNotFound)
|
||||
check_not_found('sg', exception.InvalidGroupNotFound)
|
||||
check_not_found('rtb', exception.InvalidRouteTableIDNotFound)
|
||||
check_not_found('i', exception.InvalidInstanceIDNotFound)
|
||||
check_not_found('vol', exception.InvalidVolumeNotFound)
|
||||
check_not_found('snap', exception.InvalidSnapshotNotFound)
|
||||
check_not_found('ami', exception.InvalidAMIIDNotFound)
|
||||
|
||||
@mock.patch('ec2api.db.api.IMPL')
|
||||
def test_get_db_items(self, db_api):
|
||||
@@ -116,6 +111,9 @@ class EC2UtilsTestCase(testtools.TestCase):
|
||||
check_not_found('sg', exception.InvalidGroupNotFound)
|
||||
check_not_found('rtb', exception.InvalidRouteTableIDNotFound)
|
||||
check_not_found('i', exception.InvalidInstanceIDNotFound)
|
||||
check_not_found('vol', exception.InvalidVolumeNotFound)
|
||||
check_not_found('snap', exception.InvalidSnapshotNotFound)
|
||||
check_not_found('ami', exception.InvalidAMIIDNotFound)
|
||||
|
||||
"""Unit test api xml conversion."""
|
||||
def test_number_conversion(self):
|
||||
|
||||
223
ec2api/tests/test_tag.py
Normal file
223
ec2api/tests/test_tag.py
Normal file
@@ -0,0 +1,223 @@
|
||||
# Copyright 2014
|
||||
# The Cloudscaling Group, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import mock
|
||||
|
||||
from ec2api.api import ec2utils
|
||||
from ec2api.api import tag as tag_api
|
||||
from ec2api.tests import base
|
||||
from ec2api.tests import fakes
|
||||
from ec2api.tests import matchers
|
||||
|
||||
|
||||
class TagTestCase(base.ApiTestCase):
|
||||
|
||||
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({'http_status_code': 200,
|
||||
'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', 'i', 'igw',
|
||||
'eni', 'rtb', 'snap', 'subnet', 'sg',
|
||||
'vol', 'vpc']]
|
||||
self.assertEqual(len(resource_ids), len(tag_api.RESOURCE_TYPES))
|
||||
|
||||
params = dict(('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({'http_status_code': 200,
|
||||
'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({'http_status_code': 200,
|
||||
'return': True},
|
||||
resp)
|
||||
|
||||
def test_create_tags_invlaid_parameters(self):
|
||||
# NOTE(ft): check tag validity checks
|
||||
resp = self.execute('CreateTags',
|
||||
{'ResourceId.1': fakes.ID_EC2_VPC_1,
|
||||
'Tag.1.Value': ''})
|
||||
self.assertEqual(400, resp['http_status_code'])
|
||||
self.assertEqual('InvalidParameterValue', resp['Error']['Code'])
|
||||
|
||||
resp = self.execute('CreateTags',
|
||||
{'ResourceId.1': fakes.ID_EC2_VPC_1,
|
||||
'Tag.1.Key': ''})
|
||||
self.assertEqual(400, resp['http_status_code'])
|
||||
self.assertEqual('InvalidParameterValue', resp['Error']['Code'])
|
||||
|
||||
resp = self.execute('CreateTags',
|
||||
{'ResourceId.1': fakes.ID_EC2_VPC_1,
|
||||
'Tag.1.Key': 'a' * 128})
|
||||
self.assertEqual(400, resp['http_status_code'])
|
||||
self.assertEqual('InvalidParameterValue', resp['Error']['Code'])
|
||||
|
||||
resp = self.execute('CreateTags',
|
||||
{'ResourceId.1': fakes.ID_EC2_VPC_1,
|
||||
'Tag.1.Key': 'fake-key',
|
||||
'Tag.1.Value': 'a' * 256})
|
||||
self.assertEqual(400, resp['http_status_code'])
|
||||
self.assertEqual('InvalidParameterValue', resp['Error']['Code'])
|
||||
|
||||
# NOTE(ft): check resource type check
|
||||
resp = self.execute('CreateTags',
|
||||
{'ResourceId.1': fakes.random_ec2_id('fake'),
|
||||
'Tag.1.Key': 'fake-key',
|
||||
'Tag.1.Value': 'fake-value'})
|
||||
self.assertEqual(400, resp['http_status_code'])
|
||||
self.assertEqual('InvalidId', resp['Error']['Code'])
|
||||
|
||||
# NOTE(ft): check resource existence check
|
||||
self.db_api.get_item_by_id.return_value = None
|
||||
for r_id in tag_api.RESOURCE_TYPES:
|
||||
if r_id in ('ami', 'ari', 'aki'):
|
||||
continue
|
||||
resp = self.execute('CreateTags',
|
||||
{'ResourceId.1': fakes.random_ec2_id(r_id),
|
||||
'Tag.1.Key': 'fake-key',
|
||||
'Tag.1.Value': 'fake-value'})
|
||||
self.assertEqual(400, resp['http_status_code'])
|
||||
exc_class = ec2utils.NOT_FOUND_EXCEPTION_MAP[r_id]
|
||||
try:
|
||||
error_code = exc_class.ec2_code
|
||||
except AttributeError:
|
||||
error_code = exc_class.__name__
|
||||
self.assertEqual(error_code, resp['Error']['Code'])
|
||||
|
||||
def check_delete_tag(self):
|
||||
resp = self.execute('DeleteTags',
|
||||
{'ResourceId.1': fakes.ID_EC2_VPC_1,
|
||||
'ResourceId.2': fakes.ID_EC2_SUBNET_1,
|
||||
'Tag.1.Key': 'key1',
|
||||
'Tag.2.Value': 'value2',
|
||||
'Tag.3.Key': 'key3',
|
||||
'Tag.3.Value': 'value3'})
|
||||
self.assertEqual({'http_status_code': 200,
|
||||
'return': True},
|
||||
resp)
|
||||
self.db_api.delete_tags.assert_called_once_with(
|
||||
mock.ANY, [fakes.ID_EC2_VPC_1, fakes.ID_EC2_SUBNET_2],
|
||||
[{'key': 'key1'},
|
||||
{'value': 'value2'},
|
||||
{'key': 'key3',
|
||||
'value': 'value3'}])
|
||||
|
||||
def test_describe_tags(self):
|
||||
self.db_api.get_tags.return_value = [{'item_id': fakes.ID_EC2_VPC_1,
|
||||
'key': 'key1',
|
||||
'value': ''},
|
||||
{'item_id': fakes.ID_EC2_VPC_2,
|
||||
'key': 'key2',
|
||||
'value': 'value2'},
|
||||
{'item_id': fakes.ID_EC2_VPC_2,
|
||||
'key': 'key1',
|
||||
'value': 'value3'}
|
||||
]
|
||||
resp = self.execute('DescribeTags', {})
|
||||
self.assertEqual(200, resp['http_status_code'])
|
||||
resp.pop('http_status_code')
|
||||
self.assertThat(resp,
|
||||
matchers.DictMatches(
|
||||
{'tagSet': [{'resourceType': 'vpc',
|
||||
'resourceId': fakes.ID_EC2_VPC_1,
|
||||
'key': 'key1',
|
||||
'value': None},
|
||||
{'resourceType': 'vpc',
|
||||
'resourceId': fakes.ID_EC2_VPC_2,
|
||||
'key': 'key2',
|
||||
'value': 'value2'},
|
||||
{'resourceType': 'vpc',
|
||||
'resourceId': fakes.ID_EC2_VPC_2,
|
||||
'key': 'key1',
|
||||
'value': 'value3'}
|
||||
]},
|
||||
orderless_lists=True),
|
||||
verbose=True)
|
||||
|
||||
# NOTE(ft): check filtering is plugged
|
||||
filter_fields = ['resource-type', 'resource-id', 'key', 'value']
|
||||
filter_param = dict(('Filter.%s.Name' % num, field)
|
||||
for num, field in enumerate(filter_fields))
|
||||
filter_param.update(dict(('Filter.%s.Value' % num, 'fake')
|
||||
for num, field in enumerate(filter_fields)))
|
||||
resp = self.execute('DescribeTags', filter_param)
|
||||
self.assertEqual({'http_status_code': 200,
|
||||
'tagSet': []},
|
||||
resp)
|
||||
|
||||
# NOTE(ft): check all resource types are displayed correctly
|
||||
for r_id, r_type in [('dopt', 'dhcp-options'),
|
||||
('ami', 'image'),
|
||||
('aki', 'image'),
|
||||
('ari', 'image'),
|
||||
('i', 'instance'),
|
||||
('igw', 'internet-gateway'),
|
||||
('eni', 'network-interface'),
|
||||
('rtb', 'route-table'),
|
||||
('snap', 'snapshot'),
|
||||
('subnet', 'subnet'),
|
||||
('sg', 'security-group'),
|
||||
('vol', 'volume'),
|
||||
('vpc', 'vpc')]:
|
||||
item_id = fakes.random_ec2_id(r_id)
|
||||
self.db_api.get_tags.return_value = [{'item_id': item_id,
|
||||
'key': 'fake-key',
|
||||
'value': 'fake-value'}]
|
||||
resp = self.execute('DescribeTags', {})
|
||||
self.assertEqual({'http_status_code': 200,
|
||||
'tagSet': [{'resourceType': r_type,
|
||||
'resourceId': item_id,
|
||||
'key': 'fake-key',
|
||||
'value': 'fake-value'}]},
|
||||
resp)
|
||||
Reference in New Issue
Block a user