Skip to content

Commit f54926c

Browse files
committed
Add tests for tag actions, catch tag related errors
1 parent 8fad175 commit f54926c

6 files changed

Lines changed: 112 additions & 10 deletions

File tree

ec2stack/errors.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ def duplicate_keypair_name():
150150
)
151151

152152

153+
def invalid_resource_id():
154+
"""
155+
Resource with this ID does not exist.
156+
157+
@raise Ec2stackError: Defining a bad request and message.
158+
"""
159+
raise Ec2stackError(
160+
'400',
161+
'InvalidID',
162+
'The specified ID for the resource you are trying to tag is not valid.'
163+
)
164+
165+
153166
def duplicate_security_group():
154167
"""
155168
Duplicate Security Group.

ec2stack/providers/cloudstack/requester.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,8 @@ def make_request(args):
3232

3333
request_url = _generate_request_url(args, secretkey)
3434

35-
print request_url
36-
3735
response = requests.get(request_url)
3836

39-
print response.text
40-
4137
if response.status_code in [401, 432]:
4238
abort(400)
4339
else:

ec2stack/providers/cloudstack/tags.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def create_tags():
1717
1818
@return: Response.
1919
"""
20-
_create_tag_request()
21-
return _create_tag_response()
20+
response = _create_tag_request()
21+
return _create_tag_response(response)
2222

2323

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

5353

54-
def _create_tag_response():
54+
def _create_tag_response(response):
5555
"""
5656
Generates a response for a create tag request.
5757
5858
@return: Response.
5959
"""
60+
if 'errortext' in response:
61+
if 'Unable to find resource by id' in response['errortext']:
62+
errors.invalid_resource_id()
63+
6064
return {
6165
'template_name_or_list': 'status.xml',
6266
'response_type': 'CreateTagsResponse',
@@ -71,8 +75,8 @@ def delete_tags():
7175
7276
@return: Response.
7377
"""
74-
_delete_tag_request()
75-
return _delete_tag_response()
78+
response = _delete_tag_request()
79+
return _delete_tag_response(response)
7680

7781

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

104108

105-
def _delete_tag_response():
109+
def _delete_tag_response(response):
106110
"""
107111
Generates a response for a delete tag request.
108112
109113
@return: Response.
110114
"""
115+
if 'errortext' in response:
116+
if 'Unable to find resource by id' in response['errortext']:
117+
errors.invalid_resource_id()
118+
111119
return {
112120
'template_name_or_list': 'status.xml',
113121
'response_type': 'DeleteTagsResponse',

tests/data/invalid_create_tag.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"queryasyncjobresultresponse": {
3+
"jobprocstatus": 0,
4+
"created": "2014-06-01T13:43:16+0200",
5+
"cmd": "org.apache.cloudstack.api.command.user.tag.CreateTagsCmd",
6+
"userid": "26a772da-dc25-4f2b-b0f1-e095e3717a30",
7+
"jobstatus": 2,
8+
"jobid": "3cca108f-53df-4a57-8975-3caf29e9d313",
9+
"jobresultcode": 530,
10+
"jobresulttype": "object",
11+
"jobresult": {
12+
"errorcode": 530,
13+
"errortext": "Unable to find resource by id a0ae3a53-1f4d-42d3-bbb8-7227bb0e0d and type UserVm"
14+
},
15+
"accountid": "ddbdf378-e8d9-47e0-964b-661d0d8414b8"
16+
}
17+
}

tests/data/invalid_delete_tag.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"queryasyncjobresultresponse": {
3+
"jobprocstatus": 0,
4+
"created": "2014-06-01T13:43:16+0200",
5+
"cmd": "org.apache.cloudstack.api.command.user.tag.DeleteTagsCmd",
6+
"userid": "26a772da-dc25-4f2b-b0f1-e095e3717a30",
7+
"jobstatus": 2,
8+
"jobid": "3cca108f-53df-4a57-8975-3caf29e9d313",
9+
"jobresultcode": 530,
10+
"jobresulttype": "object",
11+
"jobresult": {
12+
"errorcode": 530,
13+
"errortext": "Unable to find resource by id a0ae3a53-1f4d-42d3-bbb8-7227bb0e0d and type UserVm"
14+
},
15+
"accountid": "ddbdf378-e8d9-47e0-964b-661d0d8414b8"
16+
}
17+
}

tests/tags_tests.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,31 @@ def test_create_tag_resource_id_not_in_config(self):
6161
self.assert_bad_request(response)
6262
assert ' not found in configuration' in response.data
6363

64+
def test_create_tag_resource_id_not_found(self):
65+
data = self.get_example_data()
66+
data['Action'] = 'CreateTags'
67+
data['Tag.1.Key'] = 'examplekey'
68+
data['Tag.1.value'] = 'examplevalue'
69+
data['ResourceId.1'] = 'exampleresourceid'
70+
71+
72+
data['Signature'] = generate_signature(data, 'POST', 'localhost', '/')
73+
74+
get = mock.Mock()
75+
get.return_value.text = read_file(
76+
'tests/data/invalid_create_tag.json'
77+
)
78+
get.return_value.status_code = 200
79+
80+
with mock.patch('requests.get', get):
81+
response = self.post(
82+
'/',
83+
data=data
84+
)
85+
86+
self.assert_bad_request(response)
87+
assert 'The specified ID for the resource you are trying to tag is not valid.' in response.data
88+
6489
def test_delete_tag(self):
6590
data = self.get_example_data()
6691
data['Action'] = 'DeleteTags'
@@ -109,6 +134,32 @@ def test_delete_tag_resource_id_not_in_config(self):
109134
self.assert_bad_request(response)
110135
assert ' not found in configuration' in response.data
111136

137+
def test_delete_tag_resource_id_not_found(self):
138+
data = self.get_example_data()
139+
data['Action'] = 'DeleteTags'
140+
data['Tag.1.Key'] = 'examplekey'
141+
data['ResourceId.1'] = 'exampleresourceid'
142+
143+
144+
data['Signature'] = generate_signature(data, 'POST', 'localhost', '/')
145+
146+
get = mock.Mock()
147+
get.return_value.text = read_file(
148+
'tests/data/invalid_delete_tag.json'
149+
)
150+
get.return_value.status_code = 200
151+
152+
with mock.patch('requests.get', get):
153+
response = self.post(
154+
'/',
155+
data=data
156+
)
157+
158+
print response.data
159+
160+
self.assert_bad_request(response)
161+
assert 'The specified ID for the resource you are trying to tag is not valid.' in response.data
162+
112163
def test_delete_keypair(self):
113164
data = self.get_example_data()
114165
data['Action'] = 'DeleteKeyPair'

0 commit comments

Comments
 (0)