mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Add ability for wait_for_event to handle list event_id values
This commit is contained in:
parent
1c4beef6e0
commit
72bd29cdc8
3 changed files with 115 additions and 19 deletions
1
changelog/60430.added
Normal file
1
changelog/60430.added
Normal file
|
@ -0,0 +1 @@
|
|||
Added ability for `salt.wait_for_event` to handle `event_id`s that have a list value.
|
|
@ -689,26 +689,50 @@ def wait_for_event(name, id_list, event_id="id", timeout=300, node="master"):
|
|||
val = event["data"]["data"].get(event_id)
|
||||
|
||||
if val is not None:
|
||||
try:
|
||||
val_idx = id_list.index(val)
|
||||
except ValueError:
|
||||
log.trace(
|
||||
"wait_for_event: Event identifier '%s' not in "
|
||||
"id_list; skipping.",
|
||||
event_id,
|
||||
)
|
||||
else:
|
||||
del id_list[val_idx]
|
||||
del_counter += 1
|
||||
minions_seen = ret["changes"].setdefault("minions_seen", [])
|
||||
minions_seen.append(val)
|
||||
if isinstance(val, list):
|
||||
|
||||
log.debug(
|
||||
"wait_for_event: Event identifier '%s' removed "
|
||||
"from id_list; %s items remaining.",
|
||||
val,
|
||||
len(id_list),
|
||||
)
|
||||
val_list = [id for id in id_list if id in val]
|
||||
|
||||
if len(val_list) == 0:
|
||||
log.trace(
|
||||
"wait_for_event: Event identifier '%s' not in "
|
||||
"id_list; skipping",
|
||||
event_id,
|
||||
)
|
||||
elif len(val_list) > 0:
|
||||
minions_seen = ret["changes"].setdefault("minions_seen", [])
|
||||
for found_val in val_list:
|
||||
id_list.remove(found_val)
|
||||
del_counter += 1
|
||||
minions_seen.append(found_val)
|
||||
log.debug(
|
||||
"wait_for_event: Event identifier '%s' removed "
|
||||
"from id_list; %s items remaining.",
|
||||
found_val,
|
||||
len(id_list),
|
||||
)
|
||||
|
||||
else:
|
||||
try:
|
||||
val_idx = id_list.index(val)
|
||||
except ValueError:
|
||||
log.trace(
|
||||
"wait_for_event: Event identifier '%s' not in "
|
||||
"id_list; skipping.",
|
||||
event_id,
|
||||
)
|
||||
else:
|
||||
del id_list[val_idx]
|
||||
del_counter += 1
|
||||
minions_seen = ret["changes"].setdefault("minions_seen", [])
|
||||
minions_seen.append(val)
|
||||
|
||||
log.debug(
|
||||
"wait_for_event: Event identifier '%s' removed "
|
||||
"from id_list; %s items remaining.",
|
||||
val,
|
||||
len(id_list),
|
||||
)
|
||||
else:
|
||||
log.trace(
|
||||
"wait_for_event: Event identifier '%s' not in event "
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
|
||||
"""
|
||||
|
||||
import copy
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
|
@ -310,6 +311,76 @@ class SaltmodTestCase(TestCase, LoaderModuleMockMixin):
|
|||
saltmod.wait_for_event(name, tgt, timeout=-1.0), ret
|
||||
)
|
||||
|
||||
# 'wait_for_event' function tests: 2
|
||||
|
||||
def test_wait_for_event_list_single_event(self):
|
||||
"""
|
||||
Test to watch Salt's event bus and block until a condition is met
|
||||
"""
|
||||
name = "presence"
|
||||
event_id = "lost"
|
||||
tgt = ["minion_1", "minion_2", "minion_3"]
|
||||
|
||||
comt = "Timeout value reached."
|
||||
|
||||
ret = {"name": name, "changes": {}, "result": False, "comment": comt}
|
||||
|
||||
class Mockevent:
|
||||
"""
|
||||
Mock event class
|
||||
"""
|
||||
|
||||
flag = None
|
||||
|
||||
def __init__(self):
|
||||
self.full = None
|
||||
|
||||
def get_event(self, full):
|
||||
"""
|
||||
Mock get_event method
|
||||
"""
|
||||
self.full = full
|
||||
if self.flag:
|
||||
return {"tag": name, "data": {"lost": tgt}}
|
||||
return None
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
pass
|
||||
|
||||
with patch.object(
|
||||
salt.utils.event, "get_event", MagicMock(return_value=Mockevent())
|
||||
):
|
||||
with patch.dict(saltmod.__opts__, {"sock_dir": True, "transport": True}):
|
||||
with patch(
|
||||
"salt.states.saltmod.time.time", MagicMock(return_value=1.0)
|
||||
):
|
||||
ret.update({"comment": "Timeout value reached.", "result": False})
|
||||
self.assertDictEqual(
|
||||
saltmod.wait_for_event(
|
||||
name, tgt, event_id=event_id, timeout=-1.0
|
||||
),
|
||||
ret,
|
||||
)
|
||||
|
||||
Mockevent.flag = True
|
||||
ret.update(
|
||||
{
|
||||
"name": name,
|
||||
"changes": {"minions_seen": tgt},
|
||||
"result": True,
|
||||
"comment": "All events seen in 0.0 seconds.",
|
||||
}
|
||||
)
|
||||
self.assertDictEqual(
|
||||
saltmod.wait_for_event(
|
||||
name, copy.deepcopy(tgt), event_id="lost", timeout=1.0
|
||||
),
|
||||
ret,
|
||||
)
|
||||
|
||||
# 'runner' function tests: 1
|
||||
|
||||
def test_runner(self):
|
||||
|
|
Loading…
Add table
Reference in a new issue