Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_circonus_annotation.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 io
8 import json
9 import re
10 import uuid
11 from unittest.mock import patch
12
13 from ansible.module_utils.common.text.converters import to_bytes
14 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
15 AnsibleExitJson,
16 AnsibleFailJson,
17 ModuleTestCase,
18 set_module_args,
19 )
20 from urllib3.response import HTTPResponse
21
22 from ansible_collections.community.general.plugins.modules import circonus_annotation
23
24
25 class TestCirconusAnnotation(ModuleTestCase):
26 def setUp(self):
27 super().setUp()
28 self.module = circonus_annotation
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 def test_add_annotation(self):
40 """Check that result is changed"""
41 with set_module_args(
42 {
43 "category": "test category",
44 "description": "test description",
45 "title": "test title",
46 "api_key": str(uuid.uuid4()),
47 }
48 ):
49 cid = "/annotation/100000"
50
51 def send(self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None):
52 data = {
53 "_cid": cid,
54 "_created": 1502146995,
55 "_last_modified": 1502146995,
56 "_last_modified_by": "/user/1000",
57 "category": "test category",
58 "description": "test description",
59 "rel_metrics": [],
60 "start": 1502145480,
61 "stop": None,
62 "title": "test title",
63 }
64 raw = to_bytes(json.dumps(data))
65 resp = HTTPResponse(body=io.BytesIO(raw), preload_content=False)
66 resp.status = 200
67 resp.reason = "OK"
68 resp.headers = {"X-Circonus-API-Version": "2.00"}
69 return self.build_response(request, resp)
70
71 with patch("requests.adapters.HTTPAdapter.send", autospec=True, side_effect=send) as send:
72 with self.assertRaises(AnsibleExitJson) as result:
73 self.module.main()
74
75 self.assertTrue(result.exception.args[0]["changed"])
76 self.assertEqual(result.exception.args[0]["annotation"]["_cid"], cid)
77 self.assertEqual(send.call_count, 1)
78
79 def test_add_annotation_unicode(self):
80 """Check that result is changed.
81 Note: it seems there is a bug which prevent to create an annotation
82 with a non-ASCII category if this category already exists, in such
83 case an Internal Server Error (500) occurs."""
84 with set_module_args(
85 {
86 "category": "new catégorÿ",
87 "description": "test description",
88 "title": "test title",
89 "api_key": str(uuid.uuid4()),
90 }
91 ):
92 cid = "/annotation/100000"
93
94 def send(self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None):
95 data = {
96 "_cid": "/annotation/100000",
97 "_created": 1502236928,
98 "_last_modified": 1502236928,
99 "_last_modified_by": "/user/1000",
100 # use res['annotation']['category'].encode('latin1').decode('utf8')
101 "category": "new cat\xc3\xa9gor\xc3\xbf",
102 "description": "test description",
103 "rel_metrics": [],
104 "start": 1502236927,
105 "stop": 1502236927,
106 "title": "test title",
107 }
108
109 raw = to_bytes(json.dumps(data), encoding="latin1")
110 resp = HTTPResponse(body=io.BytesIO(raw), preload_content=False)
111 resp.status = 200
112 resp.reason = "OK"
113 resp.headers = {"X-Circonus-API-Version": "2.00"}
114 return self.build_response(request, resp)
115
116 with patch("requests.adapters.HTTPAdapter.send", autospec=True, side_effect=send) as send:
117 with self.assertRaises(AnsibleExitJson) as result:
118 self.module.main()
119
120 self.assertTrue(result.exception.args[0]["changed"])
121 self.assertEqual(result.exception.args[0]["annotation"]["_cid"], cid)
122 self.assertEqual(send.call_count, 1)
123
124 def test_auth_failure(self):
125 """Check that an error is raised when authentication failed"""
126 with set_module_args(
127 {
128 "category": "test category",
129 "description": "test description",
130 "title": "test title",
131 "api_key": str(uuid.uuid4()),
132 }
133 ):
134 cid = "/annotation/100000"
135
136 def send(self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None):
137 data = {
138 "_cid": cid,
139 "_created": 1502146995,
140 "_last_modified": 1502146995,
141 "_last_modified_by": "/user/1000",
142 "category": "test category",
143 "description": "test description",
144 "rel_metrics": [],
145 "start": 1502145480,
146 "stop": None,
147 "title": "test title",
148 }
149 raw = to_bytes(json.dumps(data))
150 resp = HTTPResponse(body=io.BytesIO(raw), preload_content=False)
151 resp.status = 403
152 resp.reason = "Forbidden"
153 resp.headers = {"X-Circonus-API-Version": "2.00"}
154 return self.build_response(request, resp)
155
156 with patch("requests.adapters.HTTPAdapter.send", autospec=True, side_effect=send) as send:
157 with self.assertRaises(AnsibleFailJson) as result:
158 self.module.main()
159
160 self.assertTrue(result.exception.args[0]["failed"])
161 self.assertTrue(re.match(r"\b403\b", result.exception.args[0]["reason"]))
162 self.assertEqual(send.call_count, 1)