Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checking for jid before returning data #55216

Merged
merged 6 commits into from
Dec 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions salt/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 42 additions & 1 deletion tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand Down