Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_statsd.py

raw
1 # Copyright (c) Ansible project
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 from unittest.mock import MagicMock, patch
8
9 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
10 AnsibleExitJson,
11 AnsibleFailJson,
12 ModuleTestCase,
13 set_module_args,
14 )
15
16 from ansible_collections.community.general.plugins.modules import statsd
17
18
19 class FakeStatsD(MagicMock):
20 def incr(self, *args, **kwargs):
21 pass
22
23 def gauge(self, *args, **kwargs):
24 pass
25
26 def close(self, *args, **kwargs):
27 pass
28
29
30 class TestStatsDModule(ModuleTestCase):
31 def setUp(self):
32 super().setUp()
33 statsd.HAS_STATSD = True
34 self.module = statsd
35
36 def tearDown(self):
37 super().tearDown()
38
39 def patch_udp_statsd_client(self, **kwargs):
40 return patch(
41 "ansible_collections.community.general.plugins.modules.statsd.udp_statsd_client", autospec=True, **kwargs
42 )
43
44 def patch_tcp_statsd_client(self, **kwargs):
45 return patch(
46 "ansible_collections.community.general.plugins.modules.statsd.tcp_statsd_client", autospec=True, **kwargs
47 )
48
49 def test_udp_without_parameters(self):
50 """Test udp without parameters"""
51 with self.patch_udp_statsd_client(side_effect=FakeStatsD):
52 with self.assertRaises(AnsibleFailJson):
53 with set_module_args({}):
54 self.module.main()
55
56 def test_tcp_without_parameters(self):
57 """Test tcp without parameters"""
58 with self.patch_tcp_statsd_client(side_effect=FakeStatsD):
59 with self.assertRaises(AnsibleFailJson):
60 with set_module_args({}):
61 self.module.main()
62
63 def test_udp_with_parameters(self):
64 """Test udp with parameters"""
65 with self.patch_udp_statsd_client(side_effect=FakeStatsD):
66 with self.assertRaises(AnsibleExitJson) as result:
67 with set_module_args(
68 {
69 "metric": "my_counter",
70 "metric_type": "counter",
71 "value": 1,
72 }
73 ):
74 self.module.main()
75 self.assertEqual(result.exception.args[0]["msg"], "Sent counter my_counter -> 1 to StatsD")
76 self.assertEqual(result.exception.args[0]["changed"], True)
77 with self.patch_udp_statsd_client(side_effect=FakeStatsD):
78 with self.assertRaises(AnsibleExitJson) as result:
79 with set_module_args(
80 {
81 "metric": "my_gauge",
82 "metric_type": "gauge",
83 "value": 3,
84 }
85 ):
86 self.module.main()
87 self.assertEqual(result.exception.args[0]["msg"], "Sent gauge my_gauge -> 3 (delta=False) to StatsD")
88 self.assertEqual(result.exception.args[0]["changed"], True)
89
90 def test_tcp_with_parameters(self):
91 """Test tcp with parameters"""
92 with self.patch_tcp_statsd_client(side_effect=FakeStatsD):
93 with self.assertRaises(AnsibleExitJson) as result:
94 with set_module_args(
95 {
96 "protocol": "tcp",
97 "metric": "my_counter",
98 "metric_type": "counter",
99 "value": 1,
100 }
101 ):
102 self.module.main()
103 self.assertEqual(result.exception.args[0]["msg"], "Sent counter my_counter -> 1 to StatsD")
104 self.assertEqual(result.exception.args[0]["changed"], True)
105 with self.patch_tcp_statsd_client(side_effect=FakeStatsD):
106 with self.assertRaises(AnsibleExitJson) as result:
107 with set_module_args(
108 {
109 "protocol": "tcp",
110 "metric": "my_gauge",
111 "metric_type": "gauge",
112 "value": 3,
113 }
114 ):
115 self.module.main()
116 self.assertEqual(result.exception.args[0]["msg"], "Sent gauge my_gauge -> 3 (delta=False) to StatsD")
117 self.assertEqual(result.exception.args[0]["changed"], True)