test_ssh_known_hosts.py 5.1 KB

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