Debian Codegraph @

ansible_collections/community/hrobot/tests/unit/plugins/modules/test_ssh_key.py

raw
1 # Copyright (c) 2021 Felix Fontein <[email protected]>
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 (absolute_import, division, print_function)
6 __metaclass__ = type
7
8
9 from ansible_collections.community.internal_test_tools.tests.unit.utils.fetch_url_module_framework import (
10 FetchUrlCall,
11 BaseTestModule,
12 )
13
14 from ansible_collections.community.hrobot.plugins.module_utils.robot import BASE_URL
15 from ansible_collections.community.hrobot.plugins.modules import ssh_key
16
17 from ..data import (
18 SSH_PUBLIC_KEY_1,
19 SSH_FINGERPRINT_1,
20 SSH_TYPE_1,
21 SSH_SIZE_1,
22 )
23
24
25 class TestHetznerSSHKey(BaseTestModule):
26 MOCK_ANSIBLE_MODULEUTILS_BASIC_ANSIBLEMODULE = 'ansible_collections.community.hrobot.plugins.modules.ssh_key.AnsibleModule'
27 MOCK_ANSIBLE_MODULEUTILS_URLS_FETCH_URL = 'ansible_collections.community.hrobot.plugins.module_utils.robot.fetch_url'
28
29 # Absent with fingerprint
30
31 def test_absent_fp(self, mocker):
32 result = self.run_module_success(mocker, ssh_key, {
33 'hetzner_user': 'test',
34 'hetzner_password': 'hunter2',
35 'state': 'absent',
36 'fingerprint': SSH_FINGERPRINT_1,
37 }, [
38 FetchUrlCall('DELETE', 200)
39 .expect_basic_auth('test', 'hunter2')
40 .expect_force_basic_auth(True)
41 .expect_url('{0}/key/{1}'.format(BASE_URL, SSH_FINGERPRINT_1)),
42 ])
43 assert result['changed'] is True
44 assert result['fingerprint'] == SSH_FINGERPRINT_1
45
46 def test_absent_fp_idempotent(self, mocker):
47 result = self.run_module_success(mocker, ssh_key, {
48 'hetzner_user': '',
49 'hetzner_password': '',
50 'state': 'absent',
51 'fingerprint': SSH_FINGERPRINT_1,
52 }, [
53 FetchUrlCall('DELETE', 404)
54 .result_error_json('Not found', {
55 'error': {
56 'status': 404,
57 'code': 'NOT_FOUND',
58 'message': 'Key not found',
59 },
60 })
61 .expect_url('{0}/key/{1}'.format(BASE_URL, SSH_FINGERPRINT_1)),
62 ])
63 assert result['changed'] is False
64 assert result['fingerprint'] == SSH_FINGERPRINT_1
65
66 def test_absent_fp_check_mode(self, mocker):
67 result = self.run_module_success(mocker, ssh_key, {
68 'hetzner_user': '',
69 'hetzner_password': '',
70 'state': 'absent',
71 'fingerprint': SSH_FINGERPRINT_1,
72 '_ansible_check_mode': True,
73 }, [
74 FetchUrlCall('GET', 200)
75 .result_json({
76 'key': {
77 'name': 'My Test Key',
78 'fingerprint': SSH_FINGERPRINT_1,
79 'type': SSH_TYPE_1,
80 'size': SSH_SIZE_1,
81 'data': SSH_PUBLIC_KEY_1,
82 },
83 })
84 .expect_url('{0}/key/{1}'.format(BASE_URL, SSH_FINGERPRINT_1)),
85 ])
86 assert result['changed'] is True
87 assert result['fingerprint'] == SSH_FINGERPRINT_1
88
89 def test_absent_fp_idempotent_check_mode(self, mocker):
90 result = self.run_module_success(mocker, ssh_key, {
91 'hetzner_user': '',
92 'hetzner_password': '',
93 'state': 'absent',
94 'fingerprint': SSH_FINGERPRINT_1,
95 '_ansible_check_mode': True,
96 }, [
97 FetchUrlCall('GET', 404)
98 .result_json({
99 'error': {
100 'status': 404,
101 'code': 'NOT_FOUND',
102 'message': 'Key not found',
103 },
104 })
105 .expect_url('{0}/key/{1}'.format(BASE_URL, SSH_FINGERPRINT_1)),
106 ])
107 assert result['changed'] is False
108 assert result['fingerprint'] == SSH_FINGERPRINT_1
109
110 # Absent with public key
111
112 def test_absent_key(self, mocker):
113 result = self.run_module_success(mocker, ssh_key, {
114 'hetzner_user': '',
115 'hetzner_password': '',
116 'state': 'absent',
117 'public_key': SSH_PUBLIC_KEY_1,
118 }, [
119 FetchUrlCall('DELETE', 200)
120 .expect_url('{0}/key/{1}'.format(BASE_URL, SSH_FINGERPRINT_1)),
121 ])
122 assert result['changed'] is True
123 assert result['fingerprint'] == SSH_FINGERPRINT_1
124
125 def test_absent_key_idempotent(self, mocker):
126 result = self.run_module_success(mocker, ssh_key, {
127 'hetzner_user': '',
128 'hetzner_password': '',
129 'state': 'absent',
130 'public_key': SSH_PUBLIC_KEY_1,
131 }, [
132 FetchUrlCall('DELETE', 404)
133 .result_json({
134 'error': {
135 'status': 404,
136 'code': 'NOT_FOUND',
137 'message': 'Key not found',
138 },
139 })
140 .expect_url('{0}/key/{1}'.format(BASE_URL, SSH_FINGERPRINT_1)),
141 ])
142 assert result['changed'] is False
143 assert result['fingerprint'] == SSH_FINGERPRINT_1
144
145 def test_absent_key_check_mode(self, mocker):
146 result = self.run_module_success(mocker, ssh_key, {
147 'hetzner_user': '',
148 'hetzner_password': '',
149 'state': 'absent',
150 'public_key': SSH_PUBLIC_KEY_1,
151 '_ansible_check_mode': True,
152 }, [
153 FetchUrlCall('GET', 200)
154 .result_json({
155 'key': {
156 'name': 'My Test Key',
157 'fingerprint': SSH_FINGERPRINT_1,
158 'type': SSH_TYPE_1,
159 'size': SSH_SIZE_1,
160 'data': SSH_PUBLIC_KEY_1,
161 },
162 })
163 .expect_url('{0}/key/{1}'.format(BASE_URL, SSH_FINGERPRINT_1)),
164 ])
165 assert result['changed'] is True
166 assert result['fingerprint'] == SSH_FINGERPRINT_1
167
168 def test_absent_key_idempotent_check_mode(self, mocker):
169 result = self.run_module_success(mocker, ssh_key, {
170 'hetzner_user': '',
171 'hetzner_password': '',
172 'state': 'absent',
173 'public_key': SSH_PUBLIC_KEY_1,
174 '_ansible_check_mode': True,
175 }, [
176 FetchUrlCall('GET', 404)
177 .result_json({
178 'error': {
179 'status': 404,
180 'code': 'NOT_FOUND',
181 'message': 'Key not found',
182 },
183 })
184 .expect_url('{0}/key/{1}'.format(BASE_URL, SSH_FINGERPRINT_1)),
185 ])
186 assert result['changed'] is False
187 assert result['fingerprint'] == SSH_FINGERPRINT_1
188
189 # Present
190
191 def test_present_create_check_mode(self, mocker):
192 result = self.run_module_success(mocker, ssh_key, {
193 'hetzner_user': '',
194 'hetzner_password': '',
195 'state': 'present',
196 'name': 'foo',
197 'public_key': SSH_PUBLIC_KEY_1,
198 '_ansible_check_mode': True,
199 }, [
200 FetchUrlCall('GET', 404)
201 .result_json({
202 'error': {
203 'status': 404,
204 'code': 'NOT_FOUND',
205 'message': 'Key not found',
206 },
207 })
208 .expect_url('{0}/key/{1}'.format(BASE_URL, SSH_FINGERPRINT_1)),
209 ])
210 assert result['changed'] is True
211 assert result['fingerprint'] == SSH_FINGERPRINT_1
212
213 def test_present_create(self, mocker):
214 result = self.run_module_success(mocker, ssh_key, {
215 'hetzner_user': '',
216 'hetzner_password': '',
217 'state': 'present',
218 'name': 'foo',
219 'public_key': SSH_PUBLIC_KEY_1,
220 }, [
221 FetchUrlCall('GET', 404)
222 .result_json({
223 'error': {
224 'status': 404,
225 'code': 'NOT_FOUND',
226 'message': 'Key not found',
227 },
228 })
229 .expect_url('{0}/key/{1}'.format(BASE_URL, SSH_FINGERPRINT_1)),
230 FetchUrlCall('POST', 200)
231 .expect_form_value('name', 'foo')
232 .expect_form_value('data', SSH_PUBLIC_KEY_1)
233 .result_json({
234 'key': {
235 'name': 'foo',
236 'fingerprint': SSH_FINGERPRINT_1,
237 'type': SSH_TYPE_1,
238 'size': SSH_SIZE_1,
239 'data': SSH_PUBLIC_KEY_1,
240 },
241 })
242 .expect_url('{0}/key'.format(BASE_URL)),
243 ])
244 assert result['changed'] is True
245 assert result['fingerprint'] == SSH_FINGERPRINT_1
246
247 def test_present_idempotent_check_mode(self, mocker):
248 result = self.run_module_success(mocker, ssh_key, {
249 'hetzner_user': '',
250 'hetzner_password': '',
251 'state': 'present',
252 'name': 'foo',
253 'public_key': SSH_PUBLIC_KEY_1,
254 '_ansible_check_mode': True,
255 }, [
256 FetchUrlCall('GET', 200)
257 .result_json({
258 'key': {
259 'name': 'foo',
260 'fingerprint': SSH_FINGERPRINT_1,
261 'type': SSH_TYPE_1,
262 'size': SSH_SIZE_1,
263 'data': SSH_PUBLIC_KEY_1,
264 },
265 })
266 .expect_url('{0}/key/{1}'.format(BASE_URL, SSH_FINGERPRINT_1)),
267 ])
268 assert result['changed'] is False
269 assert result['fingerprint'] == SSH_FINGERPRINT_1
270
271 def test_present_idempotent(self, mocker):
272 result = self.run_module_success(mocker, ssh_key, {
273 'hetzner_user': '',
274 'hetzner_password': '',
275 'state': 'present',
276 'name': 'foo',
277 'public_key': SSH_PUBLIC_KEY_1,
278 }, [
279 FetchUrlCall('GET', 200)
280 .result_json({
281 'key': {
282 'name': 'foo',
283 'fingerprint': SSH_FINGERPRINT_1,
284 'type': SSH_TYPE_1,
285 'size': SSH_SIZE_1,
286 'data': SSH_PUBLIC_KEY_1,
287 },
288 })
289 .expect_url('{0}/key/{1}'.format(BASE_URL, SSH_FINGERPRINT_1)),
290 ])
291 assert result['changed'] is False
292 assert result['fingerprint'] == SSH_FINGERPRINT_1
293
294 def test_present_change_check_mode(self, mocker):
295 result = self.run_module_success(mocker, ssh_key, {
296 'hetzner_user': '',
297 'hetzner_password': '',
298 'state': 'present',
299 'name': 'bar',
300 'public_key': SSH_PUBLIC_KEY_1,
301 '_ansible_check_mode': True,
302 }, [
303 FetchUrlCall('GET', 200)
304 .result_json({
305 'key': {
306 'name': 'foo',
307 'fingerprint': SSH_FINGERPRINT_1,
308 'type': SSH_TYPE_1,
309 'size': SSH_SIZE_1,
310 'data': SSH_PUBLIC_KEY_1,
311 },
312 })
313 .expect_url('{0}/key/{1}'.format(BASE_URL, SSH_FINGERPRINT_1)),
314 ])
315 assert result['changed'] is True
316 assert result['fingerprint'] == SSH_FINGERPRINT_1
317
318 def test_present_change(self, mocker):
319 result = self.run_module_success(mocker, ssh_key, {
320 'hetzner_user': '',
321 'hetzner_password': '',
322 'state': 'present',
323 'name': 'bar',
324 'public_key': SSH_PUBLIC_KEY_1,
325 }, [
326 FetchUrlCall('GET', 200)
327 .result_json({
328 'key': {
329 'name': 'foo',
330 'fingerprint': SSH_FINGERPRINT_1,
331 'type': SSH_TYPE_1,
332 'size': SSH_SIZE_1,
333 'data': SSH_PUBLIC_KEY_1,
334 },
335 })
336 .expect_url('{0}/key/{1}'.format(BASE_URL, SSH_FINGERPRINT_1)),
337 FetchUrlCall('POST', 200)
338 .expect_form_value('name', 'bar')
339 .expect_form_value_absent('data')
340 .result_json({
341 'key': {
342 'name': 'bar',
343 'fingerprint': SSH_FINGERPRINT_1,
344 'type': SSH_TYPE_1,
345 'size': SSH_SIZE_1,
346 'data': SSH_PUBLIC_KEY_1,
347 },
348 })
349 .expect_url('{0}/key/{1}'.format(BASE_URL, SSH_FINGERPRINT_1)),
350 ])
351 assert result['changed'] is True
352 assert result['fingerprint'] == SSH_FINGERPRINT_1
353
354 # Error
355
356 def test_invalid_public_key(self, mocker):
357 result = self.run_module_failed(mocker, ssh_key, {
358 'hetzner_user': '',
359 'hetzner_password': '',
360 'state': 'present',
361 'name': 'bar',
362 'public_key': 'asdf',
363 }, [])
364 assert result['msg'] == 'Error while extracting fingerprint from public key data: cannot split public key into at least two parts'