test_ssh_known_hosts.py 5.1 KB

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