diff --git a/salt/client/__init__.py b/salt/client/__init__.py index 70f5180a2c12..df986cf99044 100644 --- a/salt/client/__init__.py +++ b/salt/client/__init__.py @@ -1589,8 +1589,12 @@ def get_event_iter_returns(self, jid, minions, timeout=None): if 'minions' in raw.get('data', {}): continue try: - found.add(raw['id']) - ret = {raw['id']: {'ret': raw['return']}} + # There might be two jobs for the same minion, so we have to check for the jid + if jid == raw['jid']: + found.add(raw['id']) + ret = {raw['id']: {'ret': raw['return']}} + else: + continue except KeyError: # Ignore other erroneous messages continue diff --git a/tests/unit/test_client.py b/tests/unit/test_client.py index c6ec5ee2dd17..94704be116f0 100644 --- a/tests/unit/test_client.py +++ b/tests/unit/test_client.py @@ -8,7 +8,7 @@ # Import Salt Testing libs from tests.support.mixins import SaltClientTestCaseMixin -from tests.support.mock import patch +from tests.support.mock import patch, MagicMock from tests.support.unit import TestCase, skipIf # Import Salt libs @@ -22,6 +22,47 @@ class LocalClientTestCase(TestCase, SaltClientTestCaseMixin): + def test_job_result_return_success(self): + """ + Should return the `expected_return`, since there is a job with the right jid. + """ + minions = () + jid = '0815' + raw_return = { + 'id': 'fake-id', + 'jid': jid, + 'data': '', + 'return': 'fake-return' + } + expected_return = {'fake-id': {'ret': 'fake-return'}} + local_client = client.LocalClient(mopts=self.get_temp_config('master')) + local_client.event.get_event = MagicMock(return_value=raw_return) + local_client.returners = MagicMock() + ret = local_client.get_event_iter_returns(jid, minions) + val = next(ret) + self.assertEqual(val, expected_return) + + def test_job_result_return_failure(self): + """ + We are _not_ getting a job return, because the jid is different. Instead we should + get a StopIteration exception. + """ + minions = () + jid = '0815' + raw_return = { + 'id': 'fake-id', + 'jid': '0816', + 'data': '', + 'return': 'fake-return' + } + local_client = client.LocalClient(mopts=self.get_temp_config('master')) + local_client.event.get_event = MagicMock() + local_client.event.get_event.side_effect = [raw_return, None] + local_client.returners = MagicMock() + ret = local_client.get_event_iter_returns(jid, minions) + with self.assertRaises(StopIteration): + next(ret) + def test_create_local_client(self): local_client = client.LocalClient(mopts=self.get_temp_config('master')) self.assertIsInstance(local_client, client.LocalClient, 'LocalClient did not create a LocalClient instance')