"Fossies" - the Fresh Open Source Software Archive

Member "salt-3002.2/tests/unit/test_client.py" (18 Nov 2020, 8913 Bytes) of package /linux/misc/salt-3002.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_client.py": 3002.1_vs_3002.2.

    1 """
    2     :codeauthor: Mike Place <mp@saltstack.com>
    3 """
    4 
    5 import salt.utils.platform
    6 from salt import client
    7 from salt.exceptions import (
    8     EauthAuthenticationError,
    9     SaltClientError,
   10     SaltInvocationError,
   11     SaltReqTimeoutError,
   12 )
   13 from tests.support.helpers import slowTest
   14 from tests.support.mixins import SaltClientTestCaseMixin
   15 from tests.support.mock import MagicMock, patch
   16 from tests.support.unit import TestCase, skipIf
   17 
   18 
   19 class LocalClientTestCase(TestCase, SaltClientTestCaseMixin):
   20     def test_job_result_return_success(self):
   21         """
   22         Should return the `expected_return`, since there is a job with the right jid.
   23         """
   24         minions = ()
   25         jid = "0815"
   26         raw_return = {"id": "fake-id", "jid": jid, "data": "", "return": "fake-return"}
   27         expected_return = {"fake-id": {"ret": "fake-return"}}
   28         local_client = client.LocalClient(mopts=self.get_temp_config("master"))
   29         local_client.event.get_event = MagicMock(return_value=raw_return)
   30         local_client.returners = MagicMock()
   31         ret = local_client.get_event_iter_returns(jid, minions)
   32         val = next(ret)
   33         self.assertEqual(val, expected_return)
   34 
   35     def test_job_result_return_failure(self):
   36         """
   37         We are _not_ getting a job return, because the jid is different. Instead we should
   38         get a StopIteration exception.
   39         """
   40         minions = ()
   41         jid = "0815"
   42         raw_return = {
   43             "id": "fake-id",
   44             "jid": "0816",
   45             "data": "",
   46             "return": "fake-return",
   47         }
   48         local_client = client.LocalClient(mopts=self.get_temp_config("master"))
   49         local_client.event.get_event = MagicMock()
   50         local_client.event.get_event.side_effect = [raw_return, None]
   51         local_client.returners = MagicMock()
   52         ret = local_client.get_event_iter_returns(jid, minions)
   53         with self.assertRaises(StopIteration):
   54             next(ret)
   55 
   56     def test_create_local_client(self):
   57         local_client = client.LocalClient(mopts=self.get_temp_config("master"))
   58         self.assertIsInstance(
   59             local_client,
   60             client.LocalClient,
   61             "LocalClient did not create a LocalClient instance",
   62         )
   63 
   64     def test_check_pub_data(self):
   65         just_minions = {"minions": ["m1", "m2"]}
   66         jid_no_minions = {"jid": "1234", "minions": []}
   67         valid_pub_data = {"minions": ["m1", "m2"], "jid": "1234"}
   68 
   69         self.assertRaises(EauthAuthenticationError, self.client._check_pub_data, "")
   70         self.assertDictEqual(
   71             {},
   72             self.client._check_pub_data(just_minions),
   73             "Did not handle lack of jid correctly",
   74         )
   75 
   76         self.assertDictEqual(
   77             {},
   78             self.client._check_pub_data({"jid": "0"}),
   79             "Passing JID of zero is not handled gracefully",
   80         )
   81 
   82         with patch.dict(self.client.opts, {}):
   83             self.client._check_pub_data(jid_no_minions)
   84 
   85         self.assertDictEqual(
   86             valid_pub_data, self.client._check_pub_data(valid_pub_data)
   87         )
   88 
   89     def test_cmd_subset(self):
   90         with patch(
   91             "salt.client.LocalClient.cmd",
   92             return_value={
   93                 "minion1": ["first.func", "second.func"],
   94                 "minion2": ["first.func", "second.func"],
   95             },
   96         ):
   97             with patch("salt.client.LocalClient.cmd_cli") as cmd_cli_mock:
   98                 self.client.cmd_subset("*", "first.func", subset=1, cli=True)
   99                 try:
  100                     cmd_cli_mock.assert_called_with(
  101                         ["minion2"],
  102                         "first.func",
  103                         (),
  104                         progress=False,
  105                         kwarg=None,
  106                         tgt_type="list",
  107                         full_return=False,
  108                         ret="",
  109                     )
  110                 except AssertionError:
  111                     cmd_cli_mock.assert_called_with(
  112                         ["minion1"],
  113                         "first.func",
  114                         (),
  115                         progress=False,
  116                         kwarg=None,
  117                         tgt_type="list",
  118                         full_return=False,
  119                         ret="",
  120                     )
  121                 self.client.cmd_subset("*", "first.func", subset=10, cli=True)
  122                 try:
  123                     cmd_cli_mock.assert_called_with(
  124                         ["minion2", "minion1"],
  125                         "first.func",
  126                         (),
  127                         progress=False,
  128                         kwarg=None,
  129                         tgt_type="list",
  130                         full_return=False,
  131                         ret="",
  132                     )
  133                 except AssertionError:
  134                     cmd_cli_mock.assert_called_with(
  135                         ["minion1", "minion2"],
  136                         "first.func",
  137                         (),
  138                         progress=False,
  139                         kwarg=None,
  140                         tgt_type="list",
  141                         full_return=False,
  142                         ret="",
  143                     )
  144 
  145                 ret = self.client.cmd_subset(
  146                     "*", "first.func", subset=1, cli=True, full_return=True
  147                 )
  148                 try:
  149                     cmd_cli_mock.assert_called_with(
  150                         ["minion2"],
  151                         "first.func",
  152                         (),
  153                         progress=False,
  154                         kwarg=None,
  155                         tgt_type="list",
  156                         full_return=True,
  157                         ret="",
  158                     )
  159                 except AssertionError:
  160                     cmd_cli_mock.assert_called_with(
  161                         ["minion1"],
  162                         "first.func",
  163                         (),
  164                         progress=False,
  165                         kwarg=None,
  166                         tgt_type="list",
  167                         full_return=True,
  168                         ret="",
  169                     )
  170 
  171     @skipIf(salt.utils.platform.is_windows(), "Not supported on Windows")
  172     def test_pub(self):
  173         """
  174         Tests that the client cleanly returns when the publisher is not running
  175 
  176         Note: Requires ZeroMQ's IPC transport which is not supported on windows.
  177         """
  178         if self.get_config("minion")["transport"] != "zeromq":
  179             self.skipTest("This test only works with ZeroMQ")
  180         # Make sure we cleanly return if the publisher isn't running
  181         with patch("os.path.exists", return_value=False):
  182             self.assertRaises(
  183                 SaltClientError, lambda: self.client.pub("*", "test.ping")
  184             )
  185 
  186         # Check nodegroups behavior
  187         with patch("os.path.exists", return_value=True):
  188             with patch.dict(
  189                 self.client.opts,
  190                 {
  191                     "nodegroups": {
  192                         "group1": "L@foo.domain.com,bar.domain.com,baz.domain.com or bl*.domain.com"
  193                     }
  194                 },
  195             ):
  196                 # Do we raise an exception if the nodegroup can't be matched?
  197                 self.assertRaises(
  198                     SaltInvocationError,
  199                     self.client.pub,
  200                     "non_existent_group",
  201                     "test.ping",
  202                     tgt_type="nodegroup",
  203                 )
  204 
  205     @skipIf(not salt.utils.platform.is_windows(), "Windows only test")
  206     @slowTest
  207     def test_pub_win32(self):
  208         """
  209         Tests that the client raises a timeout error when using ZeroMQ's TCP
  210         transport and publisher is not running.
  211 
  212         Note: Requires ZeroMQ's TCP transport, this is only the default on Windows.
  213         """
  214         if self.get_config("minion")["transport"] != "zeromq":
  215             self.skipTest("This test only works with ZeroMQ")
  216         # Make sure we cleanly return if the publisher isn't running
  217         with patch("os.path.exists", return_value=False):
  218             self.assertRaises(
  219                 SaltReqTimeoutError, lambda: self.client.pub("*", "test.ping")
  220             )
  221 
  222         # Check nodegroups behavior
  223         with patch("os.path.exists", return_value=True):
  224             with patch.dict(
  225                 self.client.opts,
  226                 {
  227                     "nodegroups": {
  228                         "group1": "L@foo.domain.com,bar.domain.com,baz.domain.com or bl*.domain.com"
  229                     }
  230                 },
  231             ):
  232                 # Do we raise an exception if the nodegroup can't be matched?
  233                 self.assertRaises(
  234                     SaltInvocationError,
  235                     self.client.pub,
  236                     "non_existent_group",
  237                     "test.ping",
  238                     tgt_type="nodegroup",
  239                 )