test_ssh_known_hosts.py 7.1 KB

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