1
0

test_client.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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, NO_MOCK, NO_MOCK_REASON
  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. @skipIf(NO_MOCK, NO_MOCK_REASON)
  18. class LocalClientTestCase(TestCase,
  19. SaltClientTestCaseMixin):
  20. def test_create_local_client(self):
  21. local_client = client.LocalClient(mopts=self.get_temp_config('master'))
  22. self.assertIsInstance(local_client, client.LocalClient, 'LocalClient did not create a LocalClient instance')
  23. def test_check_pub_data(self):
  24. just_minions = {'minions': ['m1', 'm2']}
  25. jid_no_minions = {'jid': '1234', 'minions': []}
  26. valid_pub_data = {'minions': ['m1', 'm2'], 'jid': '1234'}
  27. self.assertRaises(EauthAuthenticationError, self.client._check_pub_data, '')
  28. self.assertDictEqual({},
  29. self.client._check_pub_data(just_minions),
  30. 'Did not handle lack of jid correctly')
  31. self.assertDictEqual(
  32. {},
  33. self.client._check_pub_data({'jid': '0'}),
  34. 'Passing JID of zero is not handled gracefully')
  35. with patch.dict(self.client.opts, {}):
  36. self.client._check_pub_data(jid_no_minions)
  37. self.assertDictEqual(valid_pub_data, self.client._check_pub_data(valid_pub_data))
  38. def test_cmd_subset(self):
  39. with patch('salt.client.LocalClient.cmd', return_value={'minion1': ['first.func', 'second.func'],
  40. 'minion2': ['first.func', 'second.func']}):
  41. with patch('salt.client.LocalClient.cmd_cli') as cmd_cli_mock:
  42. self.client.cmd_subset('*', 'first.func', sub=1, cli=True)
  43. try:
  44. cmd_cli_mock.assert_called_with(['minion2'], 'first.func', (), progress=False,
  45. kwarg=None, tgt_type='list', full_return=False,
  46. ret='')
  47. except AssertionError:
  48. cmd_cli_mock.assert_called_with(['minion1'], 'first.func', (), progress=False,
  49. kwarg=None, tgt_type='list', full_return=False,
  50. ret='')
  51. self.client.cmd_subset('*', 'first.func', sub=10, cli=True)
  52. try:
  53. cmd_cli_mock.assert_called_with(['minion2', 'minion1'], 'first.func', (), progress=False,
  54. kwarg=None, tgt_type='list', full_return=False,
  55. ret='')
  56. except AssertionError:
  57. cmd_cli_mock.assert_called_with(['minion1', 'minion2'], 'first.func', (), progress=False,
  58. kwarg=None, tgt_type='list', full_return=False,
  59. ret='')
  60. ret = self.client.cmd_subset('*', 'first.func', sub=1, cli=True, full_return=True)
  61. try:
  62. cmd_cli_mock.assert_called_with(['minion2'], 'first.func', (), progress=False,
  63. kwarg=None, tgt_type='list', full_return=True,
  64. ret='')
  65. except AssertionError:
  66. cmd_cli_mock.assert_called_with(['minion1'], 'first.func', (), progress=False,
  67. kwarg=None, tgt_type='list', full_return=True,
  68. ret='')
  69. @skipIf(salt.utils.platform.is_windows(), 'Not supported on Windows')
  70. def test_pub(self):
  71. '''
  72. Tests that the client cleanly returns when the publisher is not running
  73. Note: Requires ZeroMQ's IPC transport which is not supported on windows.
  74. '''
  75. if self.get_config('minion')['transport'] != 'zeromq':
  76. self.skipTest('This test only works with ZeroMQ')
  77. # Make sure we cleanly return if the publisher isn't running
  78. with patch('os.path.exists', return_value=False):
  79. self.assertRaises(SaltClientError, lambda: self.client.pub('*', 'test.ping'))
  80. # Check nodegroups behavior
  81. with patch('os.path.exists', return_value=True):
  82. with patch.dict(self.client.opts,
  83. {'nodegroups':
  84. {'group1': 'L@foo.domain.com,bar.domain.com,baz.domain.com or bl*.domain.com'}}):
  85. # Do we raise an exception if the nodegroup can't be matched?
  86. self.assertRaises(SaltInvocationError,
  87. self.client.pub,
  88. 'non_existent_group', 'test.ping', tgt_type='nodegroup')
  89. @skipIf(not salt.utils.platform.is_windows(), 'Windows only test')
  90. def test_pub_win32(self):
  91. '''
  92. Tests that the client raises a timeout error when using ZeroMQ's TCP
  93. transport and publisher is not running.
  94. Note: Requires ZeroMQ's TCP transport, this is only the default on Windows.
  95. '''
  96. if self.get_config('minion')['transport'] != 'zeromq':
  97. self.skipTest('This test only works with ZeroMQ')
  98. # Make sure we cleanly return if the publisher isn't running
  99. with patch('os.path.exists', return_value=False):
  100. self.assertRaises(SaltReqTimeoutError, lambda: self.client.pub('*', 'test.ping'))
  101. # Check nodegroups behavior
  102. with patch('os.path.exists', return_value=True):
  103. with patch.dict(self.client.opts,
  104. {'nodegroups':
  105. {'group1': 'L@foo.domain.com,bar.domain.com,baz.domain.com or bl*.domain.com'}}):
  106. # Do we raise an exception if the nodegroup can't be matched?
  107. self.assertRaises(SaltInvocationError,
  108. self.client.pub,
  109. 'non_existent_group', 'test.ping', tgt_type='nodegroup')