2017-02-08 15:56:03 +01:00
|
|
|
import os
|
|
|
|
import tempfile
|
2018-07-24 05:45:48 +02:00
|
|
|
import ansible.module_utils.basic as basic
|
2017-02-08 15:56:03 +01:00
|
|
|
|
2018-10-13 05:01:14 +02:00
|
|
|
from units.compat import unittest
|
2017-02-08 15:56:03 +01:00
|
|
|
from ansible.module_utils._text import to_bytes
|
2018-07-24 05:45:48 +02:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2017-02-08 15:56:03 +01:00
|
|
|
|
2018-07-24 05:45:48 +02:00
|
|
|
from ansible.modules.system.known_hosts import compute_diff, sanity_check
|
2017-02-08 15:56:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
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 = tempfile.mktemp(prefix='ansible-test-', suffix='-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,
|
2017-05-30 19:05:19 +02:00
|
|
|
'before': 'two.example.com ssh-rsa BBBBetc\n',
|
|
|
|
'after': 'two.example.com ssh-rsa BBBBetc\none.example.com ssh-rsa AAAAetc\n',
|
2017-02-08 15:56:03 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
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,
|
2017-05-30 19:05:19 +02:00
|
|
|
'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',
|
2017-02-08 15:56:03 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
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,
|
2017-05-30 19:05:19 +02:00
|
|
|
'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',
|
2017-02-08 15:56:03 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
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,
|
2017-05-30 19:05:19 +02:00
|
|
|
'before': 'one.example.com ssh-rsa AAAAetc\ntwo.example.com ssh-rsa BBBBetc\n',
|
|
|
|
'after': 'two.example.com ssh-rsa BBBBetc\n',
|
2017-02-08 15:56:03 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
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,
|
2017-05-30 19:05:19 +02:00
|
|
|
'before': 'two.example.com ssh-rsa BBBBetc\n',
|
|
|
|
'after': 'two.example.com ssh-rsa BBBBetc\n',
|
2017-02-08 15:56:03 +01:00
|
|
|
})
|
2018-07-24 05:45:48 +02:00
|
|
|
|
|
|
|
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)
|