test_ssh_known_hosts.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Test the ssh_known_hosts states
  4. '''
  5. # Import python libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. import os
  8. import shutil
  9. import sys
  10. # Import Salt Testing libs
  11. from tests.support.case import ModuleCase
  12. from tests.support.mixins import SaltReturnAssertsMixin
  13. from tests.support.runtests import RUNTIME_VARS
  14. from tests.support.helpers import skip_if_binaries_missing
  15. # Import 3rd-party libs
  16. from salt.ext import six
  17. KNOWN_HOSTS = os.path.join(RUNTIME_VARS.TMP, 'known_hosts')
  18. GITHUB_FINGERPRINT = '9d:38:5b:83:a9:17:52:92:56:1a:5e:c4:d4:81:8e:0a:ca:51:a2:64:f1:74:20:11:2e:f8:8a:c3:a1:39:49:8f'
  19. GITHUB_IP = '192.30.253.113'
  20. @skip_if_binaries_missing(['ssh', 'ssh-keygen'], check_all=True)
  21. class SSHKnownHostsStateTest(ModuleCase, SaltReturnAssertsMixin):
  22. '''
  23. Validate the ssh state
  24. '''
  25. def tearDown(self):
  26. if os.path.isfile(KNOWN_HOSTS):
  27. os.remove(KNOWN_HOSTS)
  28. super(SSHKnownHostsStateTest, self).tearDown()
  29. def test_present(self):
  30. '''
  31. ssh_known_hosts.present
  32. '''
  33. kwargs = {
  34. 'name': 'github.com',
  35. 'user': 'root',
  36. 'fingerprint': GITHUB_FINGERPRINT,
  37. 'config': KNOWN_HOSTS
  38. }
  39. # test first
  40. ret = self.run_state('ssh_known_hosts.present', test=True, **kwargs)
  41. self.assertSaltNoneReturn(ret)
  42. # save once, new key appears
  43. ret = self.run_state('ssh_known_hosts.present', **kwargs)
  44. try:
  45. self.assertSaltTrueReturn(ret)
  46. except AssertionError as err:
  47. try:
  48. self.assertInSaltComment(
  49. 'Unable to receive remote host key', ret
  50. )
  51. self.skipTest('Unable to receive remote host key')
  52. except AssertionError:
  53. six.reraise(*sys.exc_info())
  54. self.assertSaltStateChangesEqual(
  55. ret, GITHUB_FINGERPRINT, keys=('new', 0, 'fingerprint')
  56. )
  57. # save twice, no changes
  58. self.run_state('ssh_known_hosts.present', **kwargs)
  59. # test again, nothing is about to be changed
  60. ret = self.run_state('ssh_known_hosts.present', test=True, **kwargs)
  61. self.assertSaltTrueReturn(ret)
  62. # then add a record for IP address
  63. ret = self.run_state('ssh_known_hosts.present', # pylint: disable=repeated-keyword
  64. **dict(kwargs, name=GITHUB_IP))
  65. try:
  66. self.assertSaltStateChangesEqual(
  67. ret, GITHUB_FINGERPRINT, keys=('new', 0, 'fingerprint')
  68. )
  69. except AssertionError as err:
  70. try:
  71. self.assertInSaltComment(
  72. 'Unable to receive remote host key', ret
  73. )
  74. self.skipTest('Unable to receive remote host key')
  75. except AssertionError:
  76. six.reraise(*sys.exc_info())
  77. # record for every host must be available
  78. ret = self.run_function(
  79. 'ssh.get_known_host_entries', ['root', 'github.com'], config=KNOWN_HOSTS
  80. )[0]
  81. try:
  82. self.assertNotIn(ret, ('', None))
  83. except AssertionError:
  84. raise AssertionError(
  85. 'Salt return \'{0}\' is in (\'\', None).'.format(ret)
  86. )
  87. ret = self.run_function(
  88. 'ssh.get_known_host_entries', ['root', GITHUB_IP], config=KNOWN_HOSTS
  89. )[0]
  90. try:
  91. self.assertNotIn(ret, ('', None, {}))
  92. except AssertionError:
  93. raise AssertionError(
  94. 'Salt return \'{0}\' is in (\'\', None,'.format(ret) + ' {})'
  95. )
  96. def test_present_fail(self):
  97. # save something wrong
  98. ret = self.run_state(
  99. 'ssh_known_hosts.present',
  100. name='github.com',
  101. user='root',
  102. fingerprint='aa:bb:cc:dd',
  103. config=KNOWN_HOSTS
  104. )
  105. self.assertSaltFalseReturn(ret)
  106. def test_absent(self):
  107. '''
  108. ssh_known_hosts.absent
  109. '''
  110. known_hosts = os.path.join(RUNTIME_VARS.FILES, 'ssh', 'known_hosts')
  111. shutil.copyfile(known_hosts, KNOWN_HOSTS)
  112. if not os.path.isfile(KNOWN_HOSTS):
  113. self.skipTest(
  114. 'Unable to copy {0} to {1}'.format(
  115. known_hosts, KNOWN_HOSTS
  116. )
  117. )
  118. kwargs = {'name': 'github.com', 'user': 'root', 'config': KNOWN_HOSTS}
  119. # test first
  120. ret = self.run_state('ssh_known_hosts.absent', test=True, **kwargs)
  121. self.assertSaltNoneReturn(ret)
  122. # remove once, the key is gone
  123. ret = self.run_state('ssh_known_hosts.absent', **kwargs)
  124. self.assertSaltStateChangesEqual(
  125. ret, GITHUB_FINGERPRINT, keys=('old', 0, 'fingerprint')
  126. )
  127. # remove twice, nothing has changed
  128. ret = self.run_state('ssh_known_hosts.absent', **kwargs)
  129. self.assertSaltStateChangesEqual(ret, {})
  130. # test again
  131. ret = self.run_state('ssh_known_hosts.absent', test=True, **kwargs)
  132. self.assertSaltTrueReturn(ret)