test_client.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Mike Place <mp@saltstack.com>
  4. '''
  5. # Import python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Testing libs
  8. from tests.support.mixins import SaltClientTestCaseMixin
  9. from tests.support.mock import patch, MagicMock
  10. from tests.support.unit import TestCase, skipIf
  11. # Import Salt libs
  12. from salt import client
  13. import salt.utils.platform
  14. from salt.exceptions import (
  15. EauthAuthenticationError, SaltInvocationError, SaltClientError, SaltReqTimeoutError
  16. )
  17. class LocalClientTestCase(TestCase,
  18. SaltClientTestCaseMixin):
  19. def test_job_result_return_success(self):
  20. """
  21. Should return the `expected_return`, since there is a job with the right jid.
  22. """
  23. minions = ()
  24. jid = '0815'
  25. raw_return = {
  26. 'id': 'fake-id',
  27. 'jid': jid,
  28. 'data': '',
  29. 'return': 'fake-return'
  30. }
  31. expected_return = {'fake-id': {'ret': 'fake-return'}}
  32. local_client = client.LocalClient(mopts=self.get_temp_config('master'))
  33. local_client.event.get_event = MagicMock(return_value=raw_return)
  34. local_client.returners = MagicMock()
  35. ret = local_client.get_event_iter_returns(jid, minions)
  36. val = next(ret)
  37. self.assertEqual(val, expected_return)
  38. def test_job_result_return_failure(self):
  39. """
  40. We are _not_ getting a job return, because the jid is different. Instead we should
  41. get a StopIteration exception.
  42. """
  43. minions = ()
  44. jid = '0815'
  45. raw_return = {
  46. 'id': 'fake-id',
  47. 'jid': '0816',
  48. 'data': '',
  49. 'return': 'fake-return'
  50. }
  51. local_client = client.LocalClient(mopts=self.get_temp_config('master'))
  52. local_client.event.get_event = MagicMock()
  53. local_client.event.get_event.side_effect = [raw_return, None]
  54. local_client.returners = MagicMock()
  55. ret = local_client.get_event_iter_returns(jid, minions)
  56. with self.assertRaises(StopIteration):
  57. next(ret)
  58. def test_create_local_client(self):
  59. local_client = client.LocalClient(mopts=self.get_temp_config('master'))
  60. self.assertIsInstance(local_client, client.LocalClient, 'LocalClient did not create a LocalClient instance')
  61. def test_check_pub_data(self):
  62. just_minions = {'minions': ['m1', 'm2']}
  63. jid_no_minions = {'jid': '1234', 'minions': []}
  64. valid_pub_data = {'minions': ['m1', 'm2'], 'jid': '1234'}
  65. self.assertRaises(EauthAuthenticationError, self.client._check_pub_data, '')
  66. self.assertDictEqual({},
  67. self.client._check_pub_data(just_minions),
  68. 'Did not handle lack of jid correctly')
  69. self.assertDictEqual(
  70. {},
  71. self.client._check_pub_data({'jid': '0'}),
  72. 'Passing JID of zero is not handled gracefully')
  73. with patch.dict(self.client.opts, {}):
  74. self.client._check_pub_data(jid_no_minions)
  75. self.assertDictEqual(valid_pub_data, self.client._check_pub_data(valid_pub_data))
  76. def test_cmd_subset(self):
  77. with patch('salt.client.LocalClient.cmd', return_value={'minion1': ['first.func', 'second.func'],
  78. 'minion2': ['first.func', 'second.func']}):
  79. with patch('salt.client.LocalClient.cmd_cli') as cmd_cli_mock:
  80. self.client.cmd_subset('*', 'first.func', sub=1, cli=True)
  81. try:
  82. cmd_cli_mock.assert_called_with(['minion2'], 'first.func', (), progress=False,
  83. kwarg=None, tgt_type='list', full_return=False,
  84. ret='')
  85. except AssertionError:
  86. cmd_cli_mock.assert_called_with(['minion1'], 'first.func', (), progress=False,
  87. kwarg=None, tgt_type='list', full_return=False,
  88. ret='')
  89. self.client.cmd_subset('*', 'first.func', sub=10, cli=True)
  90. try:
  91. cmd_cli_mock.assert_called_with(['minion2', 'minion1'], 'first.func', (), progress=False,
  92. kwarg=None, tgt_type='list', full_return=False,
  93. ret='')
  94. except AssertionError:
  95. cmd_cli_mock.assert_called_with(['minion1', 'minion2'], 'first.func', (), progress=False,
  96. kwarg=None, tgt_type='list', full_return=False,
  97. ret='')
  98. ret = self.client.cmd_subset('*', 'first.func', sub=1, cli=True, full_return=True)
  99. try:
  100. cmd_cli_mock.assert_called_with(['minion2'], 'first.func', (), progress=False,
  101. kwarg=None, tgt_type='list', full_return=True,
  102. ret='')
  103. except AssertionError:
  104. cmd_cli_mock.assert_called_with(['minion1'], 'first.func', (), progress=False,
  105. kwarg=None, tgt_type='list', full_return=True,
  106. ret='')
  107. @skipIf(salt.utils.platform.is_windows(), 'Not supported on Windows')
  108. def test_pub(self):
  109. '''
  110. Tests that the client cleanly returns when the publisher is not running
  111. Note: Requires ZeroMQ's IPC transport which is not supported on windows.
  112. '''
  113. if self.get_config('minion')['transport'] != 'zeromq':
  114. self.skipTest('This test only works with ZeroMQ')
  115. # Make sure we cleanly return if the publisher isn't running
  116. with patch('os.path.exists', return_value=False):
  117. self.assertRaises(SaltClientError, lambda: self.client.pub('*', 'test.ping'))
  118. # Check nodegroups behavior
  119. with patch('os.path.exists', return_value=True):
  120. with patch.dict(self.client.opts,
  121. {'nodegroups':
  122. {'group1': 'L@foo.domain.com,bar.domain.com,baz.domain.com or bl*.domain.com'}}):
  123. # Do we raise an exception if the nodegroup can't be matched?
  124. self.assertRaises(SaltInvocationError,
  125. self.client.pub,
  126. 'non_existent_group', 'test.ping', tgt_type='nodegroup')
  127. @skipIf(not salt.utils.platform.is_windows(), 'Windows only test')
  128. def test_pub_win32(self):
  129. '''
  130. Tests that the client raises a timeout error when using ZeroMQ's TCP
  131. transport and publisher is not running.
  132. Note: Requires ZeroMQ's TCP transport, this is only the default on Windows.
  133. '''
  134. if self.get_config('minion')['transport'] != 'zeromq':
  135. self.skipTest('This test only works with ZeroMQ')
  136. # Make sure we cleanly return if the publisher isn't running
  137. with patch('os.path.exists', return_value=False):
  138. self.assertRaises(SaltReqTimeoutError, lambda: self.client.pub('*', 'test.ping'))
  139. # Check nodegroups behavior
  140. with patch('os.path.exists', return_value=True):
  141. with patch.dict(self.client.opts,
  142. {'nodegroups':
  143. {'group1': 'L@foo.domain.com,bar.domain.com,baz.domain.com or bl*.domain.com'}}):
  144. # Do we raise an exception if the nodegroup can't be matched?
  145. self.assertRaises(SaltInvocationError,
  146. self.client.pub,
  147. 'non_existent_group', 'test.ping', tgt_type='nodegroup')