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