Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_datadog_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 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 from pytest import importorskip
16
17 from ansible_collections.community.general.plugins.modules import datadog_downtime
18
19 # Skip this test if datadog_api_client cannot be installed
20 datadog_api_client = importorskip("datadog_api_client")
21 Downtime = datadog_api_client.v1.model.downtime.Downtime
22 DowntimeRecurrence = datadog_api_client.v1.model.downtime_recurrence.DowntimeRecurrence
23
24
25 class TestDatadogDowntime(ModuleTestCase):
26 def setUp(self):
27 super().setUp()
28 self.module = datadog_downtime
29
30 def tearDown(self):
31 super().tearDown()
32
33 def test_without_required_parameters(self):
34 """Failure must occurs when all parameters are missing"""
35 with self.assertRaises(AnsibleFailJson):
36 with set_module_args({}):
37 self.module.main()
38
39 @patch("ansible_collections.community.general.plugins.modules.datadog_downtime.DowntimesApi")
40 def test_create_downtime_when_no_id(self, downtimes_api_mock):
41 with set_module_args(
42 {
43 "monitor_tags": ["foo:bar"],
44 "scope": ["*"],
45 "monitor_id": 12345,
46 "downtime_message": "Message",
47 "start": 1111,
48 "end": 2222,
49 "timezone": "UTC",
50 "rrule": "rrule",
51 "api_key": "an_api_key",
52 "app_key": "an_app_key",
53 }
54 ):
55 downtime = Downtime()
56 downtime.monitor_tags = ["foo:bar"]
57 downtime.scope = ["*"]
58 downtime.monitor_id = 12345
59 downtime.message = "Message"
60 downtime.start = 1111
61 downtime.end = 2222
62 downtime.timezone = "UTC"
63 downtime.recurrence = DowntimeRecurrence(rrule="rrule", type="rrule")
64
65 create_downtime_mock = MagicMock(return_value=self.__downtime_with_id(12345))
66 downtimes_api_mock.return_value = MagicMock(create_downtime=create_downtime_mock)
67 with self.assertRaises(AnsibleExitJson) as result:
68 self.module.main()
69
70 self.assertTrue(result.exception.args[0]["changed"])
71 self.assertEqual(result.exception.args[0]["downtime"]["id"], 12345)
72 create_downtime_mock.assert_called_once_with(downtime)
73
74 @patch("ansible_collections.community.general.plugins.modules.datadog_downtime.DowntimesApi")
75 def test_create_downtime_when_id_and_disabled(self, downtimes_api_mock):
76 with set_module_args(
77 {
78 "id": 1212,
79 "monitor_tags": ["foo:bar"],
80 "scope": ["*"],
81 "monitor_id": 12345,
82 "downtime_message": "Message",
83 "start": 1111,
84 "end": 2222,
85 "timezone": "UTC",
86 "rrule": "rrule",
87 "api_key": "an_api_key",
88 "app_key": "an_app_key",
89 }
90 ):
91 downtime = Downtime()
92 downtime.monitor_tags = ["foo:bar"]
93 downtime.scope = ["*"]
94 downtime.monitor_id = 12345
95 downtime.message = "Message"
96 downtime.start = 1111
97 downtime.end = 2222
98 downtime.timezone = "UTC"
99 downtime.recurrence = DowntimeRecurrence(rrule="rrule", type="rrule")
100
101 disabled_downtime = Downtime()
102 disabled_downtime.disabled = True
103 disabled_downtime.id = 1212
104
105 create_downtime_mock = MagicMock(return_value=self.__downtime_with_id(12345))
106 get_downtime_mock = MagicMock(return_value=disabled_downtime)
107 downtimes_api_mock.return_value = MagicMock(
108 create_downtime=create_downtime_mock, get_downtime=get_downtime_mock
109 )
110 with self.assertRaises(AnsibleExitJson) as result:
111 self.module.main()
112
113 self.assertTrue(result.exception.args[0]["changed"])
114 self.assertEqual(result.exception.args[0]["downtime"]["id"], 12345)
115 create_downtime_mock.assert_called_once_with(downtime)
116 get_downtime_mock.assert_called_once_with(1212)
117
118 @patch("ansible_collections.community.general.plugins.modules.datadog_downtime.DowntimesApi")
119 def test_update_downtime_when_not_disabled(self, downtimes_api_mock):
120 with set_module_args(
121 {
122 "id": 1212,
123 "monitor_tags": ["foo:bar"],
124 "scope": ["*"],
125 "monitor_id": 12345,
126 "downtime_message": "Message",
127 "start": 1111,
128 "end": 2222,
129 "timezone": "UTC",
130 "rrule": "rrule",
131 "api_key": "an_api_key",
132 "app_key": "an_app_key",
133 }
134 ):
135 downtime = Downtime()
136 downtime.monitor_tags = ["foo:bar"]
137 downtime.scope = ["*"]
138 downtime.monitor_id = 12345
139 downtime.message = "Message"
140 downtime.start = 1111
141 downtime.end = 2222
142 downtime.timezone = "UTC"
143 downtime.recurrence = DowntimeRecurrence(rrule="rrule", type="rrule")
144
145 enabled_downtime = Downtime()
146 enabled_downtime.disabled = False
147 enabled_downtime.id = 1212
148
149 update_downtime_mock = MagicMock(return_value=self.__downtime_with_id(1212))
150 get_downtime_mock = MagicMock(return_value=enabled_downtime)
151 downtimes_api_mock.return_value = MagicMock(
152 update_downtime=update_downtime_mock, get_downtime=get_downtime_mock
153 )
154 with self.assertRaises(AnsibleExitJson) as result:
155 self.module.main()
156
157 self.assertTrue(result.exception.args[0]["changed"])
158 self.assertEqual(result.exception.args[0]["downtime"]["id"], 1212)
159 update_downtime_mock.assert_called_once_with(1212, downtime)
160 get_downtime_mock.assert_called_once_with(1212)
161
162 @patch("ansible_collections.community.general.plugins.modules.datadog_downtime.DowntimesApi")
163 def test_update_downtime_no_change(self, downtimes_api_mock):
164 with set_module_args(
165 {
166 "id": 1212,
167 "monitor_tags": ["foo:bar"],
168 "scope": ["*"],
169 "monitor_id": 12345,
170 "downtime_message": "Message",
171 "start": 1111,
172 "end": 2222,
173 "timezone": "UTC",
174 "rrule": "rrule",
175 "api_key": "an_api_key",
176 "app_key": "an_app_key",
177 }
178 ):
179 downtime = Downtime()
180 downtime.monitor_tags = ["foo:bar"]
181 downtime.scope = ["*"]
182 downtime.monitor_id = 12345
183 downtime.message = "Message"
184 downtime.start = 1111
185 downtime.end = 2222
186 downtime.timezone = "UTC"
187 downtime.recurrence = DowntimeRecurrence(rrule="rrule", type="rrule")
188
189 downtime_get = Downtime()
190 downtime_get.id = 1212
191 downtime_get.disabled = False
192 downtime_get.monitor_tags = ["foo:bar"]
193 downtime_get.scope = ["*"]
194 downtime_get.monitor_id = 12345
195 downtime_get.message = "Message"
196 downtime_get.start = 1111
197 downtime_get.end = 2222
198 downtime_get.timezone = "UTC"
199 downtime_get.recurrence = DowntimeRecurrence(rrule="rrule")
200
201 update_downtime_mock = MagicMock(return_value=downtime_get)
202 get_downtime_mock = MagicMock(return_value=downtime_get)
203 downtimes_api_mock.return_value = MagicMock(
204 update_downtime=update_downtime_mock, get_downtime=get_downtime_mock
205 )
206 with self.assertRaises(AnsibleExitJson) as result:
207 self.module.main()
208
209 self.assertFalse(result.exception.args[0]["changed"])
210 self.assertEqual(result.exception.args[0]["downtime"]["id"], 1212)
211 update_downtime_mock.assert_called_once_with(1212, downtime)
212 get_downtime_mock.assert_called_once_with(1212)
213
214 @patch("ansible_collections.community.general.plugins.modules.datadog_downtime.DowntimesApi")
215 def test_delete_downtime(self, downtimes_api_mock):
216 with set_module_args(
217 {
218 "id": 1212,
219 "state": "absent",
220 "api_key": "an_api_key",
221 "app_key": "an_app_key",
222 }
223 ):
224 cancel_downtime_mock = MagicMock()
225 downtimes_api_mock.return_value = MagicMock(
226 get_downtime=self.__downtime_with_id, cancel_downtime=cancel_downtime_mock
227 )
228 with self.assertRaises(AnsibleExitJson) as result:
229 self.module.main()
230
231 self.assertTrue(result.exception.args[0]["changed"])
232 cancel_downtime_mock.assert_called_once_with(1212)
233
234 def __downtime_with_id(self, id):
235 downtime = Downtime()
236 downtime.id = id
237 return downtime