Debian Codegraph @

test/units/modules/test_known_hosts.py

raw
1 from __future__ import annotations
2
3 import os
4 import tempfile
5 from ansible.module_utils import basic
6
7 import unittest
8 from ansible.module_utils.common.text.converters import to_bytes
9 from ansible.module_utils.basic import AnsibleModule
10
11 from ansible.modules.known_hosts import compute_diff, sanity_check
12
13
14 class KnownHostsDiffTestCase(unittest.TestCase):
15
16 def _create_file(self, content):
17 tmp_file = tempfile.NamedTemporaryFile(prefix='ansible-test-', suffix='-known_hosts', delete=False)
18 tmp_file.write(to_bytes(content))
19 tmp_file.close()
20 self.addCleanup(os.unlink, tmp_file.name)
21 return tmp_file.name
22
23 def test_no_existing_file(self):
24 path = "/tmp/this_file_does_not_exists_known_hosts"
25 key = 'example.com ssh-rsa AAAAetc\n'
26 diff = compute_diff(path, found_line=None, replace_or_add=False, state='present', key=key)
27 self.assertEqual(diff, {
28 'before_header': '/dev/null',
29 'after_header': path,
30 'before': '',
31 'after': 'example.com ssh-rsa AAAAetc\n',
32 })
33
34 def test_key_addition(self):
35 path = self._create_file(
36 'two.example.com ssh-rsa BBBBetc\n'
37 )
38 key = 'one.example.com ssh-rsa AAAAetc\n'
39 diff = compute_diff(path, found_line=None, replace_or_add=False, state='present', key=key)
40 self.assertEqual(diff, {
41 'before_header': path,
42 'after_header': path,
43 'before': 'two.example.com ssh-rsa BBBBetc\n',
44 'after': 'two.example.com ssh-rsa BBBBetc\none.example.com ssh-rsa AAAAetc\n',
45 })
46
47 def test_no_change(self):
48 path = self._create_file(
49 'one.example.com ssh-rsa AAAAetc\n'
50 'two.example.com ssh-rsa BBBBetc\n'
51 )
52 key = 'one.example.com ssh-rsa AAAAetc\n'
53 diff = compute_diff(path, found_line=1, replace_or_add=False, state='present', key=key)
54 self.assertEqual(diff, {
55 'before_header': path,
56 'after_header': path,
57 'before': 'one.example.com ssh-rsa AAAAetc\ntwo.example.com ssh-rsa BBBBetc\n',
58 'after': 'one.example.com ssh-rsa AAAAetc\ntwo.example.com ssh-rsa BBBBetc\n',
59 })
60
61 def test_key_change(self):
62 path = self._create_file(
63 'one.example.com ssh-rsa AAAaetc\n'
64 'two.example.com ssh-rsa BBBBetc\n'
65 )
66 key = 'one.example.com ssh-rsa AAAAetc\n'
67 diff = compute_diff(path, found_line=1, replace_or_add=True, state='present', key=key)
68 self.assertEqual(diff, {
69 'before_header': path,
70 'after_header': path,
71 'before': 'one.example.com ssh-rsa AAAaetc\ntwo.example.com ssh-rsa BBBBetc\n',
72 'after': 'two.example.com ssh-rsa BBBBetc\none.example.com ssh-rsa AAAAetc\n',
73 })
74
75 def test_key_removal(self):
76 path = self._create_file(
77 'one.example.com ssh-rsa AAAAetc\n'
78 'two.example.com ssh-rsa BBBBetc\n'
79 )
80 key = 'one.example.com ssh-rsa AAAAetc\n'
81 diff = compute_diff(path, found_line=1, replace_or_add=False, state='absent', key=key)
82 self.assertEqual(diff, {
83 'before_header': path,
84 'after_header': path,
85 'before': 'one.example.com ssh-rsa AAAAetc\ntwo.example.com ssh-rsa BBBBetc\n',
86 'after': 'two.example.com ssh-rsa BBBBetc\n',
87 })
88
89 def test_key_removal_no_change(self):
90 path = self._create_file(
91 'two.example.com ssh-rsa BBBBetc\n'
92 )
93 key = 'one.example.com ssh-rsa AAAAetc\n'
94 diff = compute_diff(path, found_line=None, replace_or_add=False, state='absent', key=key)
95 self.assertEqual(diff, {
96 'before_header': path,
97 'after_header': path,
98 'before': 'two.example.com ssh-rsa BBBBetc\n',
99 'after': 'two.example.com ssh-rsa BBBBetc\n',
100 })
101
102 def test_sanity_check(self):
103 basic._load_params = lambda: {}
104 # Module used internally to execute ssh-keygen system executable
105 module = AnsibleModule(argument_spec={})
106 host = '10.0.0.1'
107 key = '%s ssh-rsa ASDF foo@bar' % (host,)
108 keygen = module.get_bin_path('ssh-keygen')
109 sanity_check(module, host, key, keygen)