test_client.py 6.3 KB

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