test_ssh_auth.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch
  13. )
  14. # Import Salt Libs
  15. import salt.states.ssh_auth as ssh_auth
  16. class SshAuthTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Test cases for salt.states.ssh_auth
  19. '''
  20. def setup_loader_modules(self):
  21. return {ssh_auth: {}}
  22. # 'present' function tests: 1
  23. def test_present(self):
  24. '''
  25. Test to verifies that the specified SSH key
  26. is present for the specified user.
  27. '''
  28. name = 'sshkeys'
  29. user = 'root'
  30. source = 'salt://ssh_keys/id_rsa.pub'
  31. ret = {'name': name,
  32. 'changes': {},
  33. 'result': True,
  34. 'comment': ''}
  35. mock = MagicMock(return_value='exists')
  36. mock_data = MagicMock(side_effect=['replace', 'new'])
  37. with patch.dict(ssh_auth.__salt__, {'ssh.check_key': mock,
  38. 'ssh.set_auth_key': mock_data}):
  39. with patch.dict(ssh_auth.__opts__, {'test': True}):
  40. comt = ('The authorized host key sshkeys is already '
  41. 'present for user root')
  42. ret.update({'comment': comt})
  43. self.assertDictEqual(ssh_auth.present(name, user, source), ret)
  44. with patch.dict(ssh_auth.__opts__, {'test': False}):
  45. comt = ('The authorized host key sshkeys '
  46. 'for user root was updated')
  47. ret.update({'comment': comt, 'changes': {name: 'Updated'}})
  48. self.assertDictEqual(ssh_auth.present(name, user, source), ret)
  49. comt = ('The authorized host key sshkeys '
  50. 'for user root was added')
  51. ret.update({'comment': comt, 'changes': {name: 'New'}})
  52. self.assertDictEqual(ssh_auth.present(name, user, source), ret)
  53. # 'absent' function tests: 1
  54. def test_absent(self):
  55. '''
  56. Test to verifies that the specified SSH key is absent.
  57. '''
  58. name = 'sshkeys'
  59. user = 'root'
  60. source = 'salt://ssh_keys/id_rsa.pub'
  61. ret = {'name': name,
  62. 'changes': {},
  63. 'result': None,
  64. 'comment': ''}
  65. mock = MagicMock(side_effect=['User authorized keys file not present',
  66. 'Key removed'])
  67. mock_up = MagicMock(side_effect=['update', 'updated'])
  68. with patch.dict(ssh_auth.__salt__, {'ssh.rm_auth_key': mock,
  69. 'ssh.check_key': mock_up}):
  70. with patch.dict(ssh_auth.__opts__, {'test': True}):
  71. comt = ('Key sshkeys for user root is set for removal')
  72. ret.update({'comment': comt})
  73. self.assertDictEqual(ssh_auth.absent(name, user, source), ret)
  74. comt = ('Key is already absent')
  75. ret.update({'comment': comt, 'result': True})
  76. self.assertDictEqual(ssh_auth.absent(name, user, source), ret)
  77. with patch.dict(ssh_auth.__opts__, {'test': False}):
  78. comt = ('User authorized keys file not present')
  79. ret.update({'comment': comt, 'result': False})
  80. self.assertDictEqual(ssh_auth.absent(name, user, source), ret)
  81. comt = ('Key removed')
  82. ret.update({'comment': comt, 'result': True,
  83. 'changes': {name: 'Removed'}})
  84. self.assertDictEqual(ssh_auth.absent(name, user, source), ret)
  85. def test_manage(self):
  86. '''
  87. Test to verifies that the specified SSH key is absent.
  88. '''
  89. user = 'root'
  90. ret = {'name': '',
  91. 'changes': {},
  92. 'result': None,
  93. 'comment': ''}
  94. mock_rm = MagicMock(side_effect=['User authorized keys file not present',
  95. 'Key removed'])
  96. mock_up = MagicMock(side_effect=['update', 'updated'])
  97. mock_set = MagicMock(side_effect=['replace', 'new'])
  98. mock_keys = MagicMock(return_value={'somekey': {
  99. "enc": "ssh-rsa",
  100. "comment": "user@host",
  101. "options": [],
  102. "fingerprint": "b7"}})
  103. with patch.dict(ssh_auth.__salt__, {'ssh.rm_auth_key': mock_rm,
  104. 'ssh.set_auth_key': mock_set,
  105. 'ssh.check_key': mock_up,
  106. 'ssh.auth_keys': mock_keys}):
  107. with patch('salt.states.ssh_auth.present') as call_mocked_present:
  108. mock_present = {'comment': '',
  109. 'changes': {},
  110. 'result': None
  111. }
  112. call_mocked_present.return_value = mock_present
  113. with patch.dict(ssh_auth.__opts__, {'test': True}):
  114. # test: expected keys found. No chanages
  115. self.assertDictEqual(ssh_auth.manage('sshid', ['somekey'], user), ret)
  116. comt = ('somekey Key set for removal')
  117. ret.update({'comment': comt})
  118. # test: unexpected sshkey found. Should be removed.
  119. self.assertDictEqual(ssh_auth.manage('sshid', [], user), ret)
  120. with patch('salt.states.ssh_auth.present') as call_mocked_present:
  121. mock_present = {'comment': '',
  122. 'changes': {},
  123. 'result': True
  124. }
  125. call_mocked_present.return_value = mock_present
  126. with patch.dict(ssh_auth.__opts__, {'test': False}):
  127. # expected keys found. No changes
  128. ret = {'name': '',
  129. 'changes': {},
  130. 'result': True,
  131. 'comment': ''}
  132. self.assertDictEqual(ssh_auth.manage('sshid', ['somekey'], user), ret)
  133. with patch('salt.states.ssh_auth.absent') as call_mocked_absent:
  134. mock_absent = {'comment': 'Key removed'}
  135. call_mocked_absent.return_value = mock_absent
  136. ret.update({'comment': '', 'result': True,
  137. 'changes': {'somekey': 'Key removed'}})
  138. # unexpected sshkey found. Was removed.
  139. self.assertDictEqual(ssh_auth.manage('sshid', ['addkey'], user), ret)
  140. # add a key
  141. with patch('salt.states.ssh_auth.present') as call_mocked_present:
  142. mock_present = {'comment': 'The authorized host key newkey for user {} was added'.format(user),
  143. 'changes': {'newkey': 'New'},
  144. 'result': True
  145. }
  146. call_mocked_present.return_value = mock_present
  147. with patch.dict(ssh_auth.__opts__, {'test': False}):
  148. # added key newkey
  149. ret = {'name': '',
  150. 'changes': {'newkey': 'New'},
  151. 'result': True,
  152. 'comment': ''}
  153. self.assertDictEqual(ssh_auth.manage('sshid', ['newkey', 'somekey'], user), ret)