2014-01-02 23:23:16 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2018-05-28 21:13:12 +00:00
|
|
|
:codeauthor: Mike Place <mp@saltstack.com>
|
2014-01-02 23:23:16 +00:00
|
|
|
'''
|
|
|
|
|
2014-11-21 19:05:13 +00:00
|
|
|
# Import python libs
|
2017-12-15 18:14:18 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2014-11-21 19:05:13 +00:00
|
|
|
|
2014-01-02 23:23:16 +00:00
|
|
|
# Import Salt Testing libs
|
2017-02-27 15:59:04 +00:00
|
|
|
import tests.integration as integration
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
|
|
|
from tests.support.mock import patch, NO_MOCK, NO_MOCK_REASON
|
2014-01-03 00:30:15 +00:00
|
|
|
|
|
|
|
# Import Salt libs
|
2014-06-14 14:00:14 +00:00
|
|
|
from salt import client
|
2018-04-03 18:46:56 +00:00
|
|
|
import salt.utils.platform
|
2018-03-23 22:33:54 +00:00
|
|
|
from salt.exceptions import (
|
|
|
|
EauthAuthenticationError, SaltInvocationError, SaltClientError, SaltReqTimeoutError
|
|
|
|
)
|
2014-01-02 23:23:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2014-01-03 00:30:15 +00:00
|
|
|
class LocalClientTestCase(TestCase,
|
2017-04-02 15:21:06 +00:00
|
|
|
integration.SaltClientTestCaseMixin):
|
2014-01-02 23:23:16 +00:00
|
|
|
|
|
|
|
def test_create_local_client(self):
|
2017-04-01 13:30:27 +00:00
|
|
|
local_client = client.LocalClient(mopts=self.get_temp_config('master'))
|
2014-01-03 00:30:15 +00:00
|
|
|
self.assertIsInstance(local_client, client.LocalClient, 'LocalClient did not create a LocalClient instance')
|
2014-01-02 23:23:16 +00:00
|
|
|
|
|
|
|
def test_check_pub_data(self):
|
|
|
|
just_minions = {'minions': ['m1', 'm2']}
|
|
|
|
jid_no_minions = {'jid': '1234', 'minions': []}
|
|
|
|
valid_pub_data = {'minions': ['m1', 'm2'], 'jid': '1234'}
|
|
|
|
|
2016-11-11 02:37:47 +00:00
|
|
|
self.assertRaises(EauthAuthenticationError, self.client._check_pub_data, '')
|
2014-01-02 23:23:16 +00:00
|
|
|
self.assertDictEqual({},
|
2014-06-14 12:10:01 +00:00
|
|
|
self.client._check_pub_data(just_minions),
|
2014-01-03 00:30:15 +00:00
|
|
|
'Did not handle lack of jid correctly')
|
2014-01-02 23:23:16 +00:00
|
|
|
|
|
|
|
self.assertDictEqual(
|
|
|
|
{},
|
2014-06-14 12:10:01 +00:00
|
|
|
self.client._check_pub_data({'jid': '0'}),
|
2014-01-03 00:30:15 +00:00
|
|
|
'Passing JID of zero is not handled gracefully')
|
2014-01-02 23:23:16 +00:00
|
|
|
|
2014-06-14 12:10:01 +00:00
|
|
|
with patch.dict(self.client.opts, {}):
|
|
|
|
self.client._check_pub_data(jid_no_minions)
|
2014-01-02 23:23:16 +00:00
|
|
|
|
2014-06-14 12:10:01 +00:00
|
|
|
self.assertDictEqual(valid_pub_data, self.client._check_pub_data(valid_pub_data))
|
2014-01-02 23:23:16 +00:00
|
|
|
|
2017-04-10 13:00:57 +00:00
|
|
|
def test_cmd_subset(self):
|
|
|
|
with patch('salt.client.LocalClient.cmd', return_value={'minion1': ['first.func', 'second.func'],
|
|
|
|
'minion2': ['first.func', 'second.func']}):
|
|
|
|
with patch('salt.client.LocalClient.cmd_cli') as cmd_cli_mock:
|
|
|
|
self.client.cmd_subset('*', 'first.func', sub=1, cli=True)
|
|
|
|
try:
|
|
|
|
cmd_cli_mock.assert_called_with(['minion2'], 'first.func', (), progress=False,
|
2018-04-13 13:01:41 +00:00
|
|
|
kwarg=None, tgt_type='list', full_return=False,
|
2017-04-10 13:00:57 +00:00
|
|
|
ret='')
|
|
|
|
except AssertionError:
|
|
|
|
cmd_cli_mock.assert_called_with(['minion1'], 'first.func', (), progress=False,
|
2018-04-13 13:01:41 +00:00
|
|
|
kwarg=None, tgt_type='list', full_return=False,
|
2017-04-10 13:00:57 +00:00
|
|
|
ret='')
|
|
|
|
self.client.cmd_subset('*', 'first.func', sub=10, cli=True)
|
|
|
|
try:
|
|
|
|
cmd_cli_mock.assert_called_with(['minion2', 'minion1'], 'first.func', (), progress=False,
|
2018-04-13 13:01:41 +00:00
|
|
|
kwarg=None, tgt_type='list', full_return=False,
|
2017-04-10 13:00:57 +00:00
|
|
|
ret='')
|
|
|
|
except AssertionError:
|
|
|
|
cmd_cli_mock.assert_called_with(['minion1', 'minion2'], 'first.func', (), progress=False,
|
2018-04-13 13:01:41 +00:00
|
|
|
kwarg=None, tgt_type='list', full_return=False,
|
|
|
|
ret='')
|
|
|
|
|
|
|
|
ret = self.client.cmd_subset('*', 'first.func', sub=1, cli=True, full_return=True)
|
|
|
|
try:
|
|
|
|
cmd_cli_mock.assert_called_with(['minion2'], 'first.func', (), progress=False,
|
|
|
|
kwarg=None, tgt_type='list', full_return=True,
|
|
|
|
ret='')
|
|
|
|
except AssertionError:
|
|
|
|
cmd_cli_mock.assert_called_with(['minion1'], 'first.func', (), progress=False,
|
|
|
|
kwarg=None, tgt_type='list', full_return=True,
|
2017-04-10 13:00:57 +00:00
|
|
|
ret='')
|
2014-01-02 23:23:16 +00:00
|
|
|
|
2018-04-03 18:46:56 +00:00
|
|
|
@skipIf(salt.utils.platform.is_windows(), 'Not supported on Windows')
|
2014-01-02 23:23:16 +00:00
|
|
|
def test_pub(self):
|
2018-03-23 22:33:54 +00:00
|
|
|
'''
|
|
|
|
Tests that the client cleanly returns when the publisher is not running
|
|
|
|
|
2018-03-27 17:35:51 +00:00
|
|
|
Note: Requires ZeroMQ's IPC transport which is not supported on windows.
|
2018-03-23 22:33:54 +00:00
|
|
|
'''
|
2017-03-30 09:43:55 +00:00
|
|
|
if self.get_config('minion')['transport'] != 'zeromq':
|
2017-04-12 23:37:09 +00:00
|
|
|
self.skipTest('This test only works with ZeroMQ')
|
2014-01-02 23:23:16 +00:00
|
|
|
# Make sure we cleanly return if the publisher isn't running
|
|
|
|
with patch('os.path.exists', return_value=False):
|
2015-03-11 19:43:55 +00:00
|
|
|
self.assertRaises(SaltClientError, lambda: self.client.pub('*', 'test.ping'))
|
2014-01-02 23:23:16 +00:00
|
|
|
|
|
|
|
# Check nodegroups behavior
|
|
|
|
with patch('os.path.exists', return_value=True):
|
2014-06-14 12:10:01 +00:00
|
|
|
with patch.dict(self.client.opts,
|
2014-01-02 23:23:16 +00:00
|
|
|
{'nodegroups':
|
|
|
|
{'group1': 'L@foo.domain.com,bar.domain.com,baz.domain.com or bl*.domain.com'}}):
|
|
|
|
# Do we raise an exception if the nodegroup can't be matched?
|
|
|
|
self.assertRaises(SaltInvocationError,
|
2014-06-14 12:10:01 +00:00
|
|
|
self.client.pub,
|
2016-12-01 22:21:49 +00:00
|
|
|
'non_existent_group', 'test.ping', tgt_type='nodegroup')
|
2018-03-23 22:33:54 +00:00
|
|
|
|
2018-04-03 18:46:56 +00:00
|
|
|
@skipIf(not salt.utils.platform.is_windows(), 'Windows only test')
|
2018-03-23 22:33:54 +00:00
|
|
|
def test_pub_win32(self):
|
|
|
|
'''
|
2018-03-27 17:35:51 +00:00
|
|
|
Tests that the client raises a timeout error when using ZeroMQ's TCP
|
|
|
|
transport and publisher is not running.
|
2018-03-23 22:33:54 +00:00
|
|
|
|
2018-03-27 17:35:51 +00:00
|
|
|
Note: Requires ZeroMQ's TCP transport, this is only the default on Windows.
|
2018-03-23 22:33:54 +00:00
|
|
|
'''
|
|
|
|
if self.get_config('minion')['transport'] != 'zeromq':
|
|
|
|
self.skipTest('This test only works with ZeroMQ')
|
|
|
|
# Make sure we cleanly return if the publisher isn't running
|
|
|
|
with patch('os.path.exists', return_value=False):
|
|
|
|
self.assertRaises(SaltReqTimeoutError, lambda: self.client.pub('*', 'test.ping'))
|
|
|
|
|
|
|
|
# Check nodegroups behavior
|
|
|
|
with patch('os.path.exists', return_value=True):
|
|
|
|
with patch.dict(self.client.opts,
|
|
|
|
{'nodegroups':
|
|
|
|
{'group1': 'L@foo.domain.com,bar.domain.com,baz.domain.com or bl*.domain.com'}}):
|
|
|
|
# Do we raise an exception if the nodegroup can't be matched?
|
|
|
|
self.assertRaises(SaltInvocationError,
|
|
|
|
self.client.pub,
|
|
|
|
'non_existent_group', 'test.ping', tgt_type='nodegroup')
|