Skip to content
Permalink
Browse files
Add tests for tag actions, catch tag related errors
  • Loading branch information
brogand93 committed Jun 1, 2014
1 parent 8fad175 commit f54926c7c62b6bdc8d7a4e0a98be15705c6b94ea
Showing 6 changed files with 112 additions and 10 deletions.
@@ -150,6 +150,19 @@ def duplicate_keypair_name():
)


def invalid_resource_id():
"""
Resource with this ID does not exist.
@raise Ec2stackError: Defining a bad request and message.
"""
raise Ec2stackError(
'400',
'InvalidID',
'The specified ID for the resource you are trying to tag is not valid.'
)


def duplicate_security_group():
"""
Duplicate Security Group.
@@ -32,12 +32,8 @@ def make_request(args):

request_url = _generate_request_url(args, secretkey)

print request_url

response = requests.get(request_url)

print response.text

if response.status_code in [401, 432]:
abort(400)
else:
@@ -17,8 +17,8 @@ def create_tags():
@return: Response.
"""
_create_tag_request()
return _create_tag_response()
response = _create_tag_request()
return _create_tag_response(response)


def _create_tag_request():
@@ -51,12 +51,16 @@ def _create_tag_request():
return response


def _create_tag_response():
def _create_tag_response(response):
"""
Generates a response for a create tag request.
@return: Response.
"""
if 'errortext' in response:
if 'Unable to find resource by id' in response['errortext']:
errors.invalid_resource_id()

return {
'template_name_or_list': 'status.xml',
'response_type': 'CreateTagsResponse',
@@ -71,8 +75,8 @@ def delete_tags():
@return: Response.
"""
_delete_tag_request()
return _delete_tag_response()
response = _delete_tag_request()
return _delete_tag_response(response)


def _delete_tag_request():
@@ -102,12 +106,16 @@ def _delete_tag_request():
return response


def _delete_tag_response():
def _delete_tag_response(response):
"""
Generates a response for a delete tag request.
@return: Response.
"""
if 'errortext' in response:
if 'Unable to find resource by id' in response['errortext']:
errors.invalid_resource_id()

return {
'template_name_or_list': 'status.xml',
'response_type': 'DeleteTagsResponse',
@@ -0,0 +1,17 @@
{
"queryasyncjobresultresponse": {
"jobprocstatus": 0,
"created": "2014-06-01T13:43:16+0200",
"cmd": "org.apache.cloudstack.api.command.user.tag.CreateTagsCmd",
"userid": "26a772da-dc25-4f2b-b0f1-e095e3717a30",
"jobstatus": 2,
"jobid": "3cca108f-53df-4a57-8975-3caf29e9d313",
"jobresultcode": 530,
"jobresulttype": "object",
"jobresult": {
"errorcode": 530,
"errortext": "Unable to find resource by id a0ae3a53-1f4d-42d3-bbb8-7227bb0e0d and type UserVm"
},
"accountid": "ddbdf378-e8d9-47e0-964b-661d0d8414b8"
}
}
@@ -0,0 +1,17 @@
{
"queryasyncjobresultresponse": {
"jobprocstatus": 0,
"created": "2014-06-01T13:43:16+0200",
"cmd": "org.apache.cloudstack.api.command.user.tag.DeleteTagsCmd",
"userid": "26a772da-dc25-4f2b-b0f1-e095e3717a30",
"jobstatus": 2,
"jobid": "3cca108f-53df-4a57-8975-3caf29e9d313",
"jobresultcode": 530,
"jobresulttype": "object",
"jobresult": {
"errorcode": 530,
"errortext": "Unable to find resource by id a0ae3a53-1f4d-42d3-bbb8-7227bb0e0d and type UserVm"
},
"accountid": "ddbdf378-e8d9-47e0-964b-661d0d8414b8"
}
}
@@ -61,6 +61,31 @@ def test_create_tag_resource_id_not_in_config(self):
self.assert_bad_request(response)
assert ' not found in configuration' in response.data

def test_create_tag_resource_id_not_found(self):
data = self.get_example_data()
data['Action'] = 'CreateTags'
data['Tag.1.Key'] = 'examplekey'
data['Tag.1.value'] = 'examplevalue'
data['ResourceId.1'] = 'exampleresourceid'


data['Signature'] = generate_signature(data, 'POST', 'localhost', '/')

get = mock.Mock()
get.return_value.text = read_file(
'tests/data/invalid_create_tag.json'
)
get.return_value.status_code = 200

with mock.patch('requests.get', get):
response = self.post(
'/',
data=data
)

self.assert_bad_request(response)
assert 'The specified ID for the resource you are trying to tag is not valid.' in response.data

def test_delete_tag(self):
data = self.get_example_data()
data['Action'] = 'DeleteTags'
@@ -109,6 +134,32 @@ def test_delete_tag_resource_id_not_in_config(self):
self.assert_bad_request(response)
assert ' not found in configuration' in response.data

def test_delete_tag_resource_id_not_found(self):
data = self.get_example_data()
data['Action'] = 'DeleteTags'
data['Tag.1.Key'] = 'examplekey'
data['ResourceId.1'] = 'exampleresourceid'


data['Signature'] = generate_signature(data, 'POST', 'localhost', '/')

get = mock.Mock()
get.return_value.text = read_file(
'tests/data/invalid_delete_tag.json'
)
get.return_value.status_code = 200

with mock.patch('requests.get', get):
response = self.post(
'/',
data=data
)

print response.data

self.assert_bad_request(response)
assert 'The specified ID for the resource you are trying to tag is not valid.' in response.data

def test_delete_keypair(self):
data = self.get_example_data()
data['Action'] = 'DeleteKeyPair'

0 comments on commit f54926c

Please sign in to comment.