Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_icinga2_downtime.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 import json
8 from unittest.mock import MagicMock, patch
9 from urllib.error import HTTPError
10
11 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
12 AnsibleExitJson,
13 AnsibleFailJson,
14 ModuleTestCase,
15 set_module_args,
16 )
17
18 from ansible_collections.community.general.plugins.module_utils._mh.deco import no_handle_exceptions
19 from ansible_collections.community.general.plugins.modules import icinga2_downtime
20
21
22 class TestIcinga2Downtime(ModuleTestCase):
23 def setUp(self):
24 super().setUp()
25 self.module = icinga2_downtime
26
27 @patch("ansible_collections.community.general.plugins.modules.icinga2_downtime.Icinga2Client")
28 def test_schedule_downtime_successfully(self, client_mock):
29 module_args = {
30 "url": "http://icinga2.example.com:5665",
31 "url_username": "icingaadmin",
32 "url_password": "secret",
33 "author": "Ansible",
34 "comment": "This is a test comment.",
35 "state": "present",
36 "start_time": 1769954400,
37 "end_time": 1769958000,
38 "duration": 3600,
39 "fixed": True,
40 "object_type": "Host",
41 "filter": 'host.name=="host.example.com"',
42 }
43 with set_module_args(module_args):
44 info = {
45 "content-type": "application/json",
46 "server": "Icinga/r2.15.1-1",
47 "status": 200,
48 "url": "https://icinga2.example.com:5665/v1/actions/schedule-downtime",
49 }
50 response = {
51 "results": [
52 {
53 "code": 200,
54 "legacy_id": 28911,
55 "name": "host.example.com!e19c705a-54c2-49c5-8014-70ff624f9e51",
56 "status": "Successfully scheduled downtime 'host.example.com!e19c705a-54c2-49c5-8014-70ff624f9e51' for object 'host.example.com'.",
57 }
58 ]
59 }
60 response_read_mock = MagicMock(return_value=json.dumps(response))
61 response_mock = MagicMock(read=response_read_mock)
62 schedule_downtime_mock = MagicMock(return_value=(response_mock, info))
63 actions_mock = MagicMock(schedule_downtime=schedule_downtime_mock)
64 client_mock.return_value = MagicMock(actions=actions_mock)
65
66 with no_handle_exceptions(AnsibleExitJson, AnsibleFailJson):
67 with self.assertRaises(AnsibleExitJson) as result:
68 self.module.main()
69
70 self.assertFalse(result.exception.args[0]["failed"])
71 self.assertTrue(result.exception.args[0]["changed"])
72 self.assertEqual(result.exception.args[0]["results"], response["results"])
73 schedule_downtime_mock.assert_called_once_with(
74 all_services=None,
75 author=module_args["author"],
76 child_options=None,
77 comment=module_args["comment"],
78 duration=module_args["duration"],
79 end_time=module_args["end_time"],
80 filter=module_args["filter"],
81 filter_vars=None,
82 fixed=module_args["fixed"],
83 object_type=module_args["object_type"],
84 start_time=module_args["start_time"],
85 trigger_name=None,
86 )
87
88 @patch("ansible_collections.community.general.plugins.modules.icinga2_downtime.Icinga2Client")
89 def test_schedule_downtime_failed(self, client_mock):
90 module_args = {
91 "url": "http://icinga2.example.com:5665",
92 "url_username": "icingaadmin",
93 "url_password": "secret",
94 "author": "Ansible",
95 "comment": "This is a test comment.",
96 "state": "present",
97 "start_time": 1769954400,
98 "end_time": 1769958000,
99 "duration": 3600,
100 "fixed": True,
101 "object_type": "Host",
102 "filter": 'host.name=="unknown.example.com"',
103 }
104 with set_module_args(module_args):
105 info = {
106 "body": json.dumps({"error": 404, "status": "No objects found."}),
107 "content-length": "42",
108 "content-type": "application/json",
109 "msg": "HTTP Error 404: Not Found",
110 "server": "Icinga/r2.15.1-1",
111 "status": 404,
112 "url": "https://icinga2.example.com:5665/v1/actions/remove-downtime",
113 }
114 response = HTTPError(url=info["url"], code=404, msg=info["msg"], hdrs={}, fp=None)
115 schedule_downtime_mock = MagicMock(return_value=(response, info))
116 actions_mock = MagicMock(schedule_downtime=schedule_downtime_mock)
117 client_mock.return_value = MagicMock(actions=actions_mock)
118
119 with no_handle_exceptions(AnsibleExitJson, AnsibleFailJson):
120 with self.assertRaises(AnsibleFailJson) as result:
121 self.module.main()
122
123 self.assertTrue(result.exception.args[0]["failed"])
124 self.assertEqual(
125 result.exception.args[0]["error"],
126 {"error": 404, "status": "No objects found."},
127 )
128 schedule_downtime_mock.assert_called_once_with(
129 all_services=None,
130 author=module_args["author"],
131 child_options=None,
132 comment=module_args["comment"],
133 duration=module_args["duration"],
134 end_time=module_args["end_time"],
135 filter=module_args["filter"],
136 filter_vars=None,
137 fixed=module_args["fixed"],
138 object_type=module_args["object_type"],
139 start_time=module_args["start_time"],
140 trigger_name=None,
141 )
142
143 @patch("ansible_collections.community.general.plugins.modules.icinga2_downtime.Icinga2Client")
144 def test_remove_existing_downtime(self, client_mock):
145 module_args = {
146 "url": "http://icinga2.example.com:5665",
147 "url_username": "icingaadmin",
148 "url_password": "secret",
149 "state": "absent",
150 "name": "host.example.com!e19c705a-54c2-49c5-8014-70ff624f9e51",
151 "object_type": "Downtime",
152 }
153 with set_module_args(module_args):
154 info = {
155 "content-type": "application/json",
156 "server": "Icinga/r2.15.1-1",
157 "status": 200,
158 "url": "https://icinga2.example.com:5665/v1/actions/remove-downtime",
159 }
160 response = {
161 "results": [
162 {
163 "code": 200,
164 "status": "Successfully removed downtime 'host.example.com!e19c705a-54c2-49c5-8014-70ff624f9e51' and 0 child downtimes.",
165 }
166 ]
167 }
168 response_read_mock = MagicMock(return_value=json.dumps(response))
169 response_mock = MagicMock(read=response_read_mock)
170 remove_downtime_mock = MagicMock(return_value=(response_mock, info))
171 actions_mock = MagicMock(remove_downtime=remove_downtime_mock)
172 client_mock.return_value = MagicMock(actions=actions_mock)
173
174 with no_handle_exceptions(AnsibleExitJson, AnsibleFailJson):
175 with self.assertRaises(AnsibleExitJson) as result:
176 self.module.main()
177
178 self.assertFalse(result.exception.args[0]["failed"])
179 self.assertTrue(result.exception.args[0]["changed"])
180 self.assertEqual(result.exception.args[0]["results"], response["results"])
181 remove_downtime_mock.assert_called_once_with(
182 filter=None,
183 filter_vars=None,
184 name=module_args["name"],
185 object_type=module_args["object_type"],
186 )
187
188 @patch("ansible_collections.community.general.plugins.modules.icinga2_downtime.Icinga2Client")
189 def test_remove_non_existing_downtime(self, client_mock):
190 module_args = {
191 "url": "http://icinga2.example.com:5665",
192 "url_username": "icingaadmin",
193 "url_password": "secret",
194 "state": "absent",
195 "name": "unknown.example.com!e19c705a-54c2-49c5-8014-70ff624f9e51",
196 "object_type": "Downtime",
197 }
198 with set_module_args(module_args):
199 info = {
200 "body": json.dumps({"error": 404, "status": "No objects found."}),
201 "content-length": "42",
202 "content-type": "application/json",
203 "msg": "HTTP Error 404: Not Found",
204 "server": "Icinga/r2.15.1-1",
205 "status": 404,
206 "url": "https://icinga2.example.com:5665/v1/actions/remove-downtime",
207 }
208 response = HTTPError(url=info["url"], code=404, msg=info["msg"], hdrs={}, fp=None)
209 remove_downtime_mock = MagicMock(return_value=(response, info))
210 actions_mock = MagicMock(remove_downtime=remove_downtime_mock)
211 client_mock.return_value = MagicMock(actions=actions_mock)
212
213 with no_handle_exceptions(AnsibleExitJson, AnsibleFailJson):
214 with self.assertRaises(AnsibleExitJson) as result:
215 self.module.main()
216
217 self.assertFalse(result.exception.args[0]["failed"])
218 self.assertFalse(result.exception.args[0]["changed"])
219 remove_downtime_mock.assert_called_once_with(
220 filter=None,
221 filter_vars=None,
222 name=module_args["name"],
223 object_type=module_args["object_type"],
224 )