[stable-9] Unit tests: make set_module_args() a context manager, and remove copies of it in some tests (#9840)

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.

(cherry picked from commit a1781d09dd)
This commit is contained in:
Felix Fontein 2025-03-07 07:31:42 +01:00 committed by GitHub
parent 9a6bd80613
commit 013fb9c006
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
80 changed files with 3745 additions and 3977 deletions

View file

@ -42,36 +42,36 @@ class TestStatsDModule(ModuleTestCase):
"""Test udp without parameters"""
with self.patch_udp_statsd_client(side_effect=FakeStatsD) as fake_statsd:
with self.assertRaises(AnsibleFailJson) as result:
set_module_args({})
self.module.main()
with set_module_args({}):
self.module.main()
def test_tcp_without_parameters(self):
"""Test tcp without parameters"""
with self.patch_tcp_statsd_client(side_effect=FakeStatsD) as fake_statsd:
with self.assertRaises(AnsibleFailJson) as result:
set_module_args({})
self.module.main()
with set_module_args({}):
self.module.main()
def test_udp_with_parameters(self):
"""Test udp with parameters"""
with self.patch_udp_statsd_client(side_effect=FakeStatsD) as fake_statsd:
with self.assertRaises(AnsibleExitJson) as result:
set_module_args({
with set_module_args({
'metric': 'my_counter',
'metric_type': 'counter',
'value': 1,
})
self.module.main()
}):
self.module.main()
self.assertEqual(result.exception.args[0]['msg'], 'Sent counter my_counter -> 1 to StatsD')
self.assertEqual(result.exception.args[0]['changed'], True)
with self.patch_udp_statsd_client(side_effect=FakeStatsD) as fake_statsd:
with self.assertRaises(AnsibleExitJson) as result:
set_module_args({
with set_module_args({
'metric': 'my_gauge',
'metric_type': 'gauge',
'value': 3,
})
self.module.main()
}):
self.module.main()
self.assertEqual(result.exception.args[0]['msg'], 'Sent gauge my_gauge -> 3 (delta=False) to StatsD')
self.assertEqual(result.exception.args[0]['changed'], True)
@ -79,23 +79,23 @@ class TestStatsDModule(ModuleTestCase):
"""Test tcp with parameters"""
with self.patch_tcp_statsd_client(side_effect=FakeStatsD) as fake_statsd:
with self.assertRaises(AnsibleExitJson) as result:
set_module_args({
with set_module_args({
'protocol': 'tcp',
'metric': 'my_counter',
'metric_type': 'counter',
'value': 1,
})
self.module.main()
}):
self.module.main()
self.assertEqual(result.exception.args[0]['msg'], 'Sent counter my_counter -> 1 to StatsD')
self.assertEqual(result.exception.args[0]['changed'], True)
with self.patch_tcp_statsd_client(side_effect=FakeStatsD) as fake_statsd:
with self.assertRaises(AnsibleExitJson) as result:
set_module_args({
with set_module_args({
'protocol': 'tcp',
'metric': 'my_gauge',
'metric_type': 'gauge',
'value': 3,
})
self.module.main()
}):
self.module.main()
self.assertEqual(result.exception.args[0]['msg'], 'Sent gauge my_gauge -> 3 (delta=False) to StatsD')
self.assertEqual(result.exception.args[0]['changed'], True)