test_ssh_known_hosts.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. # Import Salt Libs
  9. import salt.states.ssh_known_hosts as ssh_known_hosts
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, patch
  13. from tests.support.unit import TestCase
  14. class SshKnownHostsTestCase(TestCase, LoaderModuleMockMixin):
  15. """
  16. Test cases for salt.states.ssh_known_hosts
  17. """
  18. def setup_loader_modules(self):
  19. return {ssh_known_hosts: {}}
  20. # 'present' function tests: 1
  21. def test_present(self):
  22. """
  23. Test to verifies that the specified host is known by the specified user.
  24. """
  25. name = "github.com"
  26. user = "root"
  27. key = "16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48"
  28. fingerprint = [key]
  29. ret = {"name": name, "changes": {}, "result": False, "comment": ""}
  30. with patch.dict(ssh_known_hosts.__opts__, {"test": True}):
  31. with patch.object(os.path, "isabs", MagicMock(return_value=False)):
  32. comt = 'If not specifying a "user", ' 'specify an absolute "config".'
  33. ret.update({"comment": comt})
  34. self.assertDictEqual(ssh_known_hosts.present(name), ret)
  35. comt = 'Specify either "key" or "fingerprint", not both.'
  36. ret.update({"comment": comt})
  37. self.assertDictEqual(
  38. ssh_known_hosts.present(name, user, key=key, fingerprint=[key]), ret
  39. )
  40. comt = 'Required argument "enc" if using "key" argument.'
  41. ret.update({"comment": comt})
  42. self.assertDictEqual(ssh_known_hosts.present(name, user, key=key), ret)
  43. mock = MagicMock(side_effect=["exists", "add", "update"])
  44. with patch.dict(ssh_known_hosts.__salt__, {"ssh.check_known_host": mock}):
  45. comt = "Host github.com is already in .ssh/known_hosts"
  46. ret.update({"comment": comt, "result": True})
  47. self.assertDictEqual(ssh_known_hosts.present(name, user), ret)
  48. comt = "Key for github.com is set to be" " added to .ssh/known_hosts"
  49. ret.update({"comment": comt, "result": None})
  50. self.assertDictEqual(ssh_known_hosts.present(name, user), ret)
  51. comt = "Key for github.com is set to be " "updated in .ssh/known_hosts"
  52. ret.update({"comment": comt})
  53. self.assertDictEqual(ssh_known_hosts.present(name, user), ret)
  54. with patch.dict(ssh_known_hosts.__opts__, {"test": False}):
  55. result = {"status": "exists", "error": ""}
  56. mock = MagicMock(return_value=result)
  57. with patch.dict(ssh_known_hosts.__salt__, {"ssh.set_known_host": mock}):
  58. comt = "github.com already exists in .ssh/known_hosts"
  59. ret.update({"comment": comt, "result": True})
  60. self.assertDictEqual(ssh_known_hosts.present(name, user), ret)
  61. result = {"status": "error", "error": ""}
  62. mock = MagicMock(return_value=result)
  63. with patch.dict(ssh_known_hosts.__salt__, {"ssh.set_known_host": mock}):
  64. ret.update({"comment": "", "result": False})
  65. self.assertDictEqual(ssh_known_hosts.present(name, user), ret)
  66. result = {
  67. "status": "updated",
  68. "error": "",
  69. "new": [{"fingerprint": fingerprint, "key": key}],
  70. "old": "",
  71. }
  72. mock = MagicMock(return_value=result)
  73. with patch.dict(ssh_known_hosts.__salt__, {"ssh.set_known_host": mock}):
  74. comt = "{0}'s key saved to .ssh/known_hosts (key: {1})".format(
  75. name, key
  76. )
  77. ret.update(
  78. {
  79. "comment": comt,
  80. "result": True,
  81. "changes": {
  82. "new": [{"fingerprint": fingerprint, "key": key}],
  83. "old": "",
  84. },
  85. }
  86. )
  87. self.assertDictEqual(ssh_known_hosts.present(name, user, key=key), ret)
  88. comt = "{0}'s key saved to .ssh/known_hosts (fingerprint: {1})".format(
  89. name, fingerprint
  90. )
  91. ret.update({"comment": comt})
  92. self.assertDictEqual(ssh_known_hosts.present(name, user), ret)
  93. # 'absent' function tests: 1
  94. def test_absent(self):
  95. """
  96. Test to verifies that the specified host is not known by the given user.
  97. """
  98. name = "github.com"
  99. user = "root"
  100. ret = {"name": name, "changes": {}, "result": False, "comment": ""}
  101. with patch.object(os.path, "isabs", MagicMock(return_value=False)):
  102. comt = 'If not specifying a "user", ' 'specify an absolute "config".'
  103. ret.update({"comment": comt})
  104. self.assertDictEqual(ssh_known_hosts.absent(name), ret)
  105. mock = MagicMock(return_value=False)
  106. with patch.dict(ssh_known_hosts.__salt__, {"ssh.get_known_host_entries": mock}):
  107. comt = "Host is already absent"
  108. ret.update({"comment": comt, "result": True})
  109. self.assertDictEqual(ssh_known_hosts.absent(name, user), ret)
  110. mock = MagicMock(return_value=True)
  111. with patch.dict(ssh_known_hosts.__salt__, {"ssh.get_known_host_entries": mock}):
  112. with patch.dict(ssh_known_hosts.__opts__, {"test": True}):
  113. comt = (
  114. "Key for github.com is set to be" " removed from .ssh/known_hosts"
  115. )
  116. ret.update({"comment": comt, "result": None})
  117. self.assertDictEqual(ssh_known_hosts.absent(name, user), ret)
  118. with patch.dict(ssh_known_hosts.__opts__, {"test": False}):
  119. result = {"status": "error", "error": ""}
  120. mock = MagicMock(return_value=result)
  121. with patch.dict(ssh_known_hosts.__salt__, {"ssh.rm_known_host": mock}):
  122. ret.update({"comment": "", "result": False})
  123. self.assertDictEqual(ssh_known_hosts.absent(name, user), ret)
  124. result = {"status": "removed", "error": "", "comment": "removed"}
  125. mock = MagicMock(return_value=result)
  126. with patch.dict(ssh_known_hosts.__salt__, {"ssh.rm_known_host": mock}):
  127. ret.update(
  128. {
  129. "comment": "removed",
  130. "result": True,
  131. "changes": {"new": None, "old": True},
  132. }
  133. )
  134. self.assertDictEqual(ssh_known_hosts.absent(name, user), ret)