Skip to content

Commit 4d38619

Browse files
committed
Fix most unit tests.
Missing are everything using 'patch_ansible_module', which is likely something that has to be removed.
1 parent c6b474b commit 4d38619

File tree

83 files changed

+4047
-4300
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+4047
-4300
lines changed

tests/unit/plugins/modules/test_alerta_customer.py

+69-78
Original file line numberDiff line numberDiff line change
@@ -82,169 +82,160 @@ def fetch_url_mock(self, mocker):
8282
def test_without_parameters(self):
8383
"""Failure if no parameters set"""
8484
with self.assertRaises(AnsibleFailJson):
85-
set_module_args({})
86-
self.module.main()
85+
with set_module_args({}):
86+
self.module.main()
8787

8888
def test_without_content(self):
8989
"""Failure if customer and match are missing"""
90-
set_module_args({
90+
with set_module_args({
9191
'alerta_url': "http://localhost:8080",
9292
'api_username': "[email protected]",
9393
'api_password': "password"
94-
})
95-
with self.assertRaises(AnsibleFailJson):
96-
self.module.main()
94+
}):
95+
with self.assertRaises(AnsibleFailJson):
96+
self.module.main()
9797

9898
def test_successful_existing_customer_creation(self):
9999
"""Test the customer creation (already exists)."""
100-
set_module_args({
100+
with set_module_args({
101101
'alerta_url': "http://localhost:8080",
102102
'api_username': "[email protected]",
103103
'api_password': "password",
104104
'customer': 'Developer',
105105
'match': '[email protected]'
106-
})
107-
108-
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
109-
fetch_url_mock.return_value = customer_response_page1()
110-
with self.assertRaises(AnsibleExitJson):
111-
self.module.main()
112-
self.assertTrue(fetch_url_mock.call_count, 1)
106+
}):
107+
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
108+
fetch_url_mock.return_value = customer_response_page1()
109+
with self.assertRaises(AnsibleExitJson):
110+
self.module.main()
111+
self.assertTrue(fetch_url_mock.call_count, 1)
113112

114113
def test_successful_customer_creation(self):
115114
"""Test the customer creation."""
116-
set_module_args({
115+
with set_module_args({
117116
'alerta_url': "http://localhost:8080",
118117
'api_username': "[email protected]",
119118
'api_password': "password",
120119
'customer': 'Developer',
121120
'match': '[email protected]'
122-
})
121+
}):
122+
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
123+
fetch_url_mock.return_value = customer_response_page1()
124+
with self.assertRaises(AnsibleExitJson):
125+
self.module.main()
123126

124-
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
125-
fetch_url_mock.return_value = customer_response_page1()
126-
with self.assertRaises(AnsibleExitJson):
127-
self.module.main()
128-
129-
self.assertTrue(fetch_url_mock.call_count, 1)
130-
call_data = json.loads(fetch_url_mock.call_args[1]['data'])
131-
assert call_data['match'] == "[email protected]"
132-
assert call_data['customer'] == "Developer"
127+
self.assertTrue(fetch_url_mock.call_count, 1)
128+
call_data = json.loads(fetch_url_mock.call_args[1]['data'])
129+
assert call_data['match'] == "[email protected]"
130+
assert call_data['customer'] == "Developer"
133131

134132
def test_successful_customer_creation_key(self):
135133
"""Test the customer creation using api_key."""
136-
set_module_args({
134+
with set_module_args({
137135
'alerta_url': "http://localhost:8080",
138136
'api_key': "demo-key",
139137
'customer': 'Developer',
140138
'match': '[email protected]'
141-
})
142-
143-
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
144-
fetch_url_mock.return_value = customer_response_page1()
145-
with self.assertRaises(AnsibleExitJson):
146-
self.module.main()
139+
}):
140+
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
141+
fetch_url_mock.return_value = customer_response_page1()
142+
with self.assertRaises(AnsibleExitJson):
143+
self.module.main()
147144

148-
self.assertTrue(fetch_url_mock.call_count, 1)
149-
call_data = json.loads(fetch_url_mock.call_args[1]['data'])
150-
assert call_data['match'] == "[email protected]"
151-
assert call_data['customer'] == "Developer"
145+
self.assertTrue(fetch_url_mock.call_count, 1)
146+
call_data = json.loads(fetch_url_mock.call_args[1]['data'])
147+
assert call_data['match'] == "[email protected]"
148+
assert call_data['customer'] == "Developer"
152149

153150
def test_failed_not_found(self):
154151
"""Test failure with wrong URL."""
155152

156-
set_module_args({
153+
with set_module_args({
157154
'alerta_url': "http://localhost:8080/s",
158155
'api_username': "[email protected]",
159156
'api_password': "password",
160157
'customer': 'Developer',
161158
'match': '[email protected]'
162-
})
163-
164-
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
165-
fetch_url_mock.return_value = (None, {"status": 404, 'msg': 'Not found for request GET on http://localhost:8080/a/api/customers'})
166-
with self.assertRaises(AnsibleFailJson):
167-
self.module.main()
159+
}):
160+
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
161+
fetch_url_mock.return_value = (None, {"status": 404, 'msg': 'Not found for request GET on http://localhost:8080/a/api/customers'})
162+
with self.assertRaises(AnsibleFailJson):
163+
self.module.main()
168164

169165
def test_failed_forbidden(self):
170166
"""Test failure with wrong user."""
171167

172-
set_module_args({
168+
with set_module_args({
173169
'alerta_url': "http://localhost:8080",
174170
'api_username': "[email protected]",
175171
'api_password': "password",
176172
'customer': 'Developer',
177173
'match': '[email protected]'
178-
})
179-
180-
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
181-
fetch_url_mock.return_value = (None, {"status": 403, 'msg': 'Permission Denied for GET on http://localhost:8080/api/customers'})
182-
with self.assertRaises(AnsibleFailJson):
183-
self.module.main()
174+
}):
175+
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
176+
fetch_url_mock.return_value = (None, {"status": 403, 'msg': 'Permission Denied for GET on http://localhost:8080/api/customers'})
177+
with self.assertRaises(AnsibleFailJson):
178+
self.module.main()
184179

185180
def test_failed_unauthorized(self):
186181
"""Test failure with wrong username or password."""
187182

188-
set_module_args({
183+
with set_module_args({
189184
'alerta_url': "http://localhost:8080",
190185
'api_username': "[email protected]",
191186
'api_password': "password_wrong",
192187
'customer': 'Developer',
193188
'match': '[email protected]'
194-
})
195-
196-
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
197-
fetch_url_mock.return_value = (None, {"status": 401, 'msg': 'Unauthorized to request GET on http://localhost:8080/api/customers'})
198-
with self.assertRaises(AnsibleFailJson):
199-
self.module.main()
189+
}):
190+
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
191+
fetch_url_mock.return_value = (None, {"status": 401, 'msg': 'Unauthorized to request GET on http://localhost:8080/api/customers'})
192+
with self.assertRaises(AnsibleFailJson):
193+
self.module.main()
200194

201195
def test_successful_customer_deletion(self):
202196
"""Test the customer deletion."""
203197

204-
set_module_args({
198+
with set_module_args({
205199
'alerta_url': "http://localhost:8080",
206200
'api_username': "[email protected]",
207201
'api_password': "password",
208202
'customer': 'Developer',
209203
'match': '[email protected]',
210204
'state': 'absent'
211-
})
212-
213-
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
214-
fetch_url_mock.return_value = customer_response_page1()
215-
with self.assertRaises(AnsibleExitJson):
216-
self.module.main()
205+
}):
206+
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
207+
fetch_url_mock.return_value = customer_response_page1()
208+
with self.assertRaises(AnsibleExitJson):
209+
self.module.main()
217210

218211
def test_successful_customer_deletion_page2(self):
219212
"""Test the customer deletion on the second page."""
220213

221-
set_module_args({
214+
with set_module_args({
222215
'alerta_url': "http://localhost:8080",
223216
'api_username': "[email protected]",
224217
'api_password': "password",
225218
'customer': 'Developer',
226219
'match': '[email protected]',
227220
'state': 'absent'
228-
})
229-
230-
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
231-
fetch_url_mock.return_value = customer_response_page2()
232-
with self.assertRaises(AnsibleExitJson):
233-
self.module.main()
221+
}):
222+
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
223+
fetch_url_mock.return_value = customer_response_page2()
224+
with self.assertRaises(AnsibleExitJson):
225+
self.module.main()
234226

235227
def test_successful_nonexisting_customer_deletion(self):
236228
"""Test the customer deletion (non existing)."""
237229

238-
set_module_args({
230+
with set_module_args({
239231
'alerta_url': "http://localhost:8080",
240232
'api_username': "[email protected]",
241233
'api_password': "password",
242234
'customer': 'Billing',
243235
'match': '[email protected]',
244236
'state': 'absent'
245-
})
246-
247-
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
248-
fetch_url_mock.return_value = customer_response_page1()
249-
with self.assertRaises(AnsibleExitJson):
250-
self.module.main()
237+
}):
238+
with patch.object(alerta_customer, "fetch_url") as fetch_url_mock:
239+
fetch_url_mock.return_value = customer_response_page1()
240+
with self.assertRaises(AnsibleExitJson):
241+
self.module.main()

tests/unit/plugins/modules/test_archive.py

+15-16
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,26 @@ def tearDown(self):
2525
self.os_path_isdir = self.mock_os_path_isdir.stop()
2626

2727
def test_archive_removal_safety(self):
28-
set_module_args(
28+
with set_module_args(
2929
dict(
3030
path=['/foo', '/bar', '/baz'],
3131
dest='/foo/destination.tgz',
3232
remove=True
3333
)
34-
)
35-
36-
module = AnsibleModule(
37-
argument_spec=dict(
38-
path=dict(type='list', elements='path', required=True),
39-
format=dict(type='str', default='gz', choices=['bz2', 'gz', 'tar', 'xz', 'zip']),
40-
dest=dict(type='path'),
41-
exclude_path=dict(type='list', elements='path', default=[]),
42-
exclusion_patterns=dict(type='list', elements='path'),
43-
force_archive=dict(type='bool', default=False),
44-
remove=dict(type='bool', default=False),
45-
),
46-
add_file_common_args=True,
47-
supports_check_mode=True,
48-
)
34+
):
35+
module = AnsibleModule(
36+
argument_spec=dict(
37+
path=dict(type='list', elements='path', required=True),
38+
format=dict(type='str', default='gz', choices=['bz2', 'gz', 'tar', 'xz', 'zip']),
39+
dest=dict(type='path'),
40+
exclude_path=dict(type='list', elements='path', default=[]),
41+
exclusion_patterns=dict(type='list', elements='path'),
42+
force_archive=dict(type='bool', default=False),
43+
remove=dict(type='bool', default=False),
44+
),
45+
add_file_common_args=True,
46+
supports_check_mode=True,
47+
)
4948

5049
self.os_path_isdir.side_effect = [True, False, False, True]
5150

0 commit comments

Comments
 (0)