Unit tests: make set_module_args() a context manager, and remove copies of it in some tests (#9838)

Make set_module_args() a context manager, and remove copies of set_module_args().

Prepares for Data Tagging.
This commit is contained in:
Felix Fontein 2025-03-07 07:21:03 +01:00 committed by GitHub
parent 402f725424
commit a1781d09dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
84 changed files with 4043 additions and 4302 deletions

View file

@ -27,70 +27,67 @@ class TestCampfireModule(ModuleTestCase):
def test_without_required_parameters(self):
"""Failure must occurs when all parameters are missing"""
with self.assertRaises(AnsibleFailJson):
set_module_args({})
self.module.main()
with set_module_args({}):
self.module.main()
def test_successful_message(self):
"""Test failure message"""
set_module_args({
with set_module_args({
'subscription': 'test',
'token': 'abc',
'room': 'test',
'msg': 'test'
})
}):
with patch.object(campfire, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 200})
with self.assertRaises(AnsibleExitJson):
self.module.main()
with patch.object(campfire, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 200})
with self.assertRaises(AnsibleExitJson):
self.module.main()
assert fetch_url_mock.call_count == 1
url = fetch_url_mock.call_args[0][1]
data = fetch_url_mock.call_args[1]['data']
assert fetch_url_mock.call_count == 1
url = fetch_url_mock.call_args[0][1]
data = fetch_url_mock.call_args[1]['data']
assert url == 'https://test.campfirenow.com/room/test/speak.xml'
assert data == '<message><body>test</body></message>'
assert url == 'https://test.campfirenow.com/room/test/speak.xml'
assert data == '<message><body>test</body></message>'
def test_successful_message_with_notify(self):
"""Test failure message"""
set_module_args({
with set_module_args({
'subscription': 'test',
'token': 'abc',
'room': 'test',
'msg': 'test',
'notify': 'bell'
})
}):
with patch.object(campfire, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 200})
with self.assertRaises(AnsibleExitJson):
self.module.main()
with patch.object(campfire, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 200})
with self.assertRaises(AnsibleExitJson):
self.module.main()
assert fetch_url_mock.call_count == 2
notify_call = fetch_url_mock.mock_calls[0]
url = notify_call[1][1]
data = notify_call[2]['data']
assert fetch_url_mock.call_count == 2
notify_call = fetch_url_mock.mock_calls[0]
url = notify_call[1][1]
data = notify_call[2]['data']
assert url == 'https://test.campfirenow.com/room/test/speak.xml'
assert data == '<message><type>SoundMessage</type><body>bell</body></message>'
assert url == 'https://test.campfirenow.com/room/test/speak.xml'
assert data == '<message><type>SoundMessage</type><body>bell</body></message>'
message_call = fetch_url_mock.mock_calls[1]
url = message_call[1][1]
data = message_call[2]['data']
message_call = fetch_url_mock.mock_calls[1]
url = message_call[1][1]
data = message_call[2]['data']
assert url == 'https://test.campfirenow.com/room/test/speak.xml'
assert data == '<message><body>test</body></message>'
assert url == 'https://test.campfirenow.com/room/test/speak.xml'
assert data == '<message><body>test</body></message>'
def test_failure_message(self):
"""Test failure message"""
set_module_args({
with set_module_args({
'subscription': 'test',
'token': 'abc',
'room': 'test',
'msg': 'test'
})
with patch.object(campfire, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 403})
with self.assertRaises(AnsibleFailJson):
self.module.main()
}):
with patch.object(campfire, "fetch_url") as fetch_url_mock:
fetch_url_mock.return_value = (None, {"status": 403})
with self.assertRaises(AnsibleFailJson):
self.module.main()