test_puppet.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import errno
  8. import os
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.unit import TestCase
  12. from tests.support.mock import (
  13. mock_open,
  14. MagicMock,
  15. patch,
  16. )
  17. # Import Salt Libs
  18. import salt.utils.args
  19. import salt.utils.files
  20. import salt.modules.puppet as puppet
  21. from salt.exceptions import CommandExecutionError
  22. class PuppetTestCase(TestCase, LoaderModuleMockMixin):
  23. '''
  24. Test cases for salt.modules.puppet
  25. '''
  26. def setup_loader_modules(self):
  27. return {puppet: {}}
  28. def test_run(self):
  29. '''
  30. Test to execute a puppet run
  31. '''
  32. mock = MagicMock(return_value={"A": "B"})
  33. with patch.object(salt.utils.args, 'clean_kwargs', mock):
  34. mock = MagicMock(return_value={'retcode': 0})
  35. mock_lst = MagicMock(return_value=[])
  36. with patch.dict(puppet.__salt__, {'cmd.run_all': mock, 'cmd.run': mock_lst}):
  37. self.assertTrue(puppet.run())
  38. def test_noop(self):
  39. '''
  40. Test to execute a puppet noop run
  41. '''
  42. mock = MagicMock(return_value={"stderr": "A", "stdout": "B"})
  43. with patch.object(puppet, 'run', mock):
  44. self.assertDictEqual(puppet.noop(), {'stderr': 'A', 'stdout': 'B'})
  45. def test_enable(self):
  46. '''
  47. Test to enable the puppet agent
  48. '''
  49. mock_lst = MagicMock(return_value=[])
  50. with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
  51. mock = MagicMock(return_value=True)
  52. with patch.object(os.path, 'isfile', mock):
  53. mock = MagicMock(return_value=True)
  54. with patch.object(os, 'remove', mock):
  55. self.assertTrue(puppet.enable())
  56. with patch.object(os, 'remove', MagicMock(side_effect=IOError)):
  57. self.assertRaises(CommandExecutionError, puppet.enable)
  58. self.assertFalse(puppet.enable())
  59. def test_disable(self):
  60. '''
  61. Test to disable the puppet agent
  62. '''
  63. mock_lst = MagicMock(return_value=[])
  64. with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
  65. mock = MagicMock(side_effect=[True, False])
  66. with patch.object(os.path, 'isfile', mock):
  67. self.assertFalse(puppet.disable())
  68. with patch('salt.utils.files.fopen', mock_open()):
  69. self.assertTrue(puppet.disable())
  70. try:
  71. with patch('salt.utils.files.fopen', mock_open()) as m_open:
  72. m_open.side_effect = IOError(13, 'Permission denied:', '/file')
  73. self.assertRaises(CommandExecutionError, puppet.disable)
  74. except StopIteration:
  75. pass
  76. def test_status(self):
  77. '''
  78. Test to display puppet agent status
  79. '''
  80. mock_lst = MagicMock(return_value=[])
  81. with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
  82. mock = MagicMock(side_effect=[True])
  83. with patch.object(os.path, 'isfile', mock):
  84. self.assertEqual(puppet.status(), "Administratively disabled")
  85. mock = MagicMock(side_effect=[False, True])
  86. with patch.object(os.path, 'isfile', mock):
  87. with patch('salt.utils.files.fopen', mock_open(read_data="1")):
  88. mock = MagicMock(return_value=True)
  89. with patch.object(os, 'kill', mock):
  90. self.assertEqual(puppet.status(), "Applying a catalog")
  91. mock = MagicMock(side_effect=[False, True])
  92. with patch.object(os.path, 'isfile', mock):
  93. with patch('salt.utils.files.fopen', mock_open()):
  94. mock = MagicMock(return_value=True)
  95. with patch.object(os, 'kill', mock):
  96. self.assertEqual(puppet.status(), "Stale lockfile")
  97. mock = MagicMock(side_effect=[False, False, True])
  98. with patch.object(os.path, 'isfile', mock):
  99. with patch('salt.utils.files.fopen', mock_open(read_data="1")):
  100. mock = MagicMock(return_value=True)
  101. with patch.object(os, 'kill', mock):
  102. self.assertEqual(puppet.status(), "Idle daemon")
  103. mock = MagicMock(side_effect=[False, False, True])
  104. with patch.object(os.path, 'isfile', mock):
  105. with patch('salt.utils.files.fopen', mock_open()):
  106. mock = MagicMock(return_value=True)
  107. with patch.object(os, 'kill', mock):
  108. self.assertEqual(puppet.status(), "Stale pidfile")
  109. mock = MagicMock(side_effect=[False, False, False])
  110. with patch.object(os.path, 'isfile', mock):
  111. self.assertEqual(puppet.status(), "Stopped")
  112. def test_summary(self):
  113. '''
  114. Test to show a summary of the last puppet agent run
  115. '''
  116. mock_lst = MagicMock(return_value=[])
  117. with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
  118. with patch('salt.utils.files.fopen',
  119. mock_open(read_data="resources: 1")):
  120. self.assertDictEqual(puppet.summary(), {'resources': 1})
  121. permission_error = IOError(errno.EACCES, 'Permission denied:', '/file')
  122. with patch('salt.utils.files.fopen',
  123. mock_open(read_data=permission_error)) as m_open:
  124. self.assertRaises(CommandExecutionError, puppet.summary)
  125. def test_plugin_sync(self):
  126. '''
  127. Test to runs a plugin synch between the puppet master and agent
  128. '''
  129. mock_lst = MagicMock(return_value=[])
  130. with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
  131. mock_lst = MagicMock(side_effect=[False, True])
  132. with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
  133. self.assertEqual(puppet.plugin_sync(), "")
  134. self.assertTrue(puppet.plugin_sync())
  135. def test_facts(self):
  136. '''
  137. Test to run facter and return the results
  138. '''
  139. mock = MagicMock(return_value={
  140. 'retcode': 0,
  141. 'stdout': "1\n2"
  142. })
  143. with patch.dict(puppet.__salt__, {'cmd.run_all': mock}):
  144. mock = MagicMock(side_effect=[
  145. ['a', 'b'],
  146. ['c', 'd'],
  147. ])
  148. with patch.object(puppet, '_format_fact', mock):
  149. self.assertDictEqual(puppet.facts(), {'a': 'b', 'c': 'd'})
  150. def test_fact(self):
  151. '''
  152. Test to run facter for a specific fact
  153. '''
  154. mock = MagicMock(side_effect=[
  155. {'retcode': 0, 'stdout': False},
  156. {'retcode': 0, 'stdout': True},
  157. ])
  158. with patch.dict(puppet.__salt__, {'cmd.run_all': mock}):
  159. self.assertEqual(puppet.fact('salt'), '')
  160. self.assertTrue(puppet.fact('salt'))