Skip to content

Commit

Permalink
Fix more schedule tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dwoz committed Dec 28, 2023
1 parent 62774c3 commit 449cca2
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 43 deletions.
34 changes: 18 additions & 16 deletions tests/pytests/unit/modules/state/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1227,14 +1227,15 @@ def test_event():

_expected = '"body": "{\\"text\\": \\"Hello World\\"}"'
with patch.object(SaltEvent, "get_event", return_value=event_returns):
print_cli_mock = MagicMock()
with patch.object(salt.utils.stringutils, "print_cli", print_cli_mock):
found = False
state.event(count=1)
for x in print_cli_mock.mock_calls:
if _expected in x.args[0]:
found = True
assert found is True
with patch.object(SaltEvent, "connect_pub", return_value=True):
print_cli_mock = MagicMock()
with patch.object(salt.utils.stringutils, "print_cli", print_cli_mock):
found = False
state.event(count=1)
for x in print_cli_mock.mock_calls:
if _expected in x.args[0]:
found = True
assert found is True

now = datetime.datetime.now().isoformat()
event_returns = {
Expand All @@ -1244,14 +1245,15 @@ def test_event():

_expected = f'"date": "{now}"'
with patch.object(SaltEvent, "get_event", return_value=event_returns):
print_cli_mock = MagicMock()
with patch.object(salt.utils.stringutils, "print_cli", print_cli_mock):
found = False
state.event(count=1)
for x in print_cli_mock.mock_calls:
if _expected in x.args[0]:
found = True
assert found is True
with patch.object(SaltEvent, "connect_pub", return_value=True):
print_cli_mock = MagicMock()
with patch.object(salt.utils.stringutils, "print_cli", print_cli_mock):
found = False
state.event(count=1)
for x in print_cli_mock.mock_calls:
if _expected in x.args[0]:
found = True
assert found is True


@pytest.mark.parametrize(
Expand Down
97 changes: 70 additions & 27 deletions tests/pytests/unit/modules/test_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ def test_delete(job1):
patch_schedule_get_event = patch.object(
SaltEvent, "get_event", return_value={"complete": True, "schedule": {}}
)
patch_schedule_connect_pub = patch.object(
SaltEvent, "connect_pub", return_value=True
)

with patch_makedirs, patch_schedule_opts, patch_schedule_event_fire, patch_schedule_get_event:
with patch_makedirs, patch_schedule_opts, patch_schedule_event_fire, patch_schedule_connect_pub, patch_schedule_get_event:
assert schedule.delete("job1") == {
"comment": "Job job1 does not exist.",
"changes": {},
Expand Down Expand Up @@ -258,11 +261,14 @@ def test_add():
patch_schedule_event_fire = patch.dict(
schedule.__salt__, {"event.fire": MagicMock(return_value=True)}
)
patch_schedule_connect_pub = patch.object(
SaltEvent, "connect_pub", return_value=True
)
patch_schedule_get_event = patch.object(
SaltEvent, "get_event", return_value={"complete": True, "schedule": {}}
)

with patch_makedirs, patch_schedule_opts, patch_schedule_event_fire:
with patch_makedirs, patch_schedule_opts, patch_schedule_connect_pub, patch_schedule_event_fire:

_ret_value = {"complete": True, "schedule": {"job1": {"salt": "salt"}}}
patch_schedule_get_event = patch.object(
Expand Down Expand Up @@ -340,13 +346,16 @@ def test_run_job(job1):
patch_schedule_event_fire = patch.dict(
schedule.__salt__, {"event.fire": MagicMock(return_value=True)}
)
patch_schedule_connect_pub = patch.object(
SaltEvent, "connect_pub", return_value=True
)
patch_schedule_get_event = patch.object(
SaltEvent,
"get_event",
return_value={"complete": True, "schedule": {"job1": job1}},
)

with patch_makedirs, patch_schedule_opts, patch_schedule_event_fire, patch_schedule_get_event:
with patch_makedirs, patch_schedule_opts, patch_schedule_event_fire, patch_schedule_connect_pub, patch_schedule_get_event:
assert schedule.run_job("job1") == {
"comment": "Scheduling Job job1 on minion.",
"result": True,
Expand All @@ -371,8 +380,11 @@ def test_enable_job():
patch_schedule_get_event = patch.object(
SaltEvent, "get_event", return_value={"complete": True, "schedule": {}}
)
patch_schedule_connect_pub = patch.object(
SaltEvent, "connect_pub", return_value=True
)

with patch_makedirs, patch_schedule_opts, patch_schedule_event_fire, patch_schedule_get_event:
with patch_makedirs, patch_schedule_opts, patch_schedule_event_fire, patch_schedule_connect_pub, patch_schedule_get_event:
assert schedule.enable_job("job1") == {
"comment": "Job job1 does not exist.",
"changes": {},
Expand All @@ -398,8 +410,11 @@ def test_disable_job():
patch_schedule_get_event = patch.object(
SaltEvent, "get_event", return_value={"complete": True, "schedule": {}}
)
patch_schedule_connect_pub = patch.object(
SaltEvent, "connect_pub", return_value=True
)

with patch_makedirs, patch_schedule_opts, patch_schedule_event_fire, patch_schedule_get_event:
with patch_makedirs, patch_schedule_opts, patch_schedule_event_fire, patch_schedule_connect_pub, patch_schedule_get_event:
assert schedule.disable_job("job1") == {
"comment": "Job job1 does not exist.",
"changes": {},
Expand All @@ -416,16 +431,20 @@ def test_save():
Test if it save all scheduled jobs on the minion.
"""
comm1 = "Schedule (non-pillar items) saved."
with patch.dict(
schedule.__opts__,
{"default_include": "/tmp"},
):
with patch("os.makedirs", MagicMock(return_value=True)):
mock = MagicMock(return_value=True)
with patch.dict(schedule.__salt__, {"event.fire": mock}):
_ret_value = {"complete": True, "schedule": {}}
with patch.object(SaltEvent, "get_event", return_value=_ret_value):
assert schedule.save() == {"comment": comm1, "result": True}
patch_schedule_connect_pub = patch.object(
SaltEvent, "connect_pub", return_value=True
)
with patch_schedule_connect_pub:
with patch.dict(
schedule.__opts__,
{"default_include": "/tmp"},
):
with patch("os.makedirs", MagicMock(return_value=True)):
mock = MagicMock(return_value=True)
with patch.dict(schedule.__salt__, {"event.fire": mock}):
_ret_value = {"complete": True, "schedule": {}}
with patch.object(SaltEvent, "get_event", return_value=_ret_value):
assert schedule.save() == {"comment": comm1, "result": True}


# 'enable' function tests: 1
Expand Down Expand Up @@ -475,13 +494,16 @@ def test_move(job1):
patch_schedule_event_fire = patch.dict(
schedule.__salt__, {"event.fire": MagicMock(return_value=True)}
)
patch_schedule_connect_pub = patch.object(
SaltEvent, "connect_pub", return_value=True
)
patch_schedule_get_event = patch.object(
SaltEvent,
"get_event",
return_value={"complete": True, "schedule": {"job1": job1}},
)

with patch_makedirs, patch_schedule_opts, patch_schedule_event_fire, patch_schedule_get_event:
with patch_makedirs, patch_schedule_opts, patch_schedule_event_fire, patch_schedule_connect_pub, patch_schedule_get_event:
mock = MagicMock(return_value={})
patch_schedule_publish = patch.dict(
schedule.__salt__, {"publish.publish": mock}
Expand Down Expand Up @@ -523,7 +545,7 @@ def test_move(job1):

mock = MagicMock(side_effect=[{}, {"job1": {}}])

with patch_makedirs, patch_schedule_opts, patch_schedule_event_fire, patch_schedule_get_event:
with patch_makedirs, patch_schedule_opts, patch_schedule_event_fire, patch_schedule_connect_pub, patch_schedule_get_event:
with patch.dict(schedule.__pillar__, {"schedule": {"job1": job1}}):
mock = MagicMock(return_value={})
patch_schedule_publish = patch.dict(
Expand Down Expand Up @@ -582,8 +604,11 @@ def test_copy(job1):
"get_event",
return_value={"complete": True, "schedule": {"job1": job1}},
)
patch_schedule_connect_pub = patch.object(
SaltEvent, "connect_pub", return_value=True
)

with patch_makedirs, patch_schedule_opts, patch_schedule_event_fire, patch_schedule_get_event:
with patch_makedirs, patch_schedule_opts, patch_schedule_event_fire, patch_schedule_get_event, patch_schedule_connect_pub:
mock = MagicMock(return_value={})
patch_schedule_publish = patch.dict(
schedule.__salt__, {"publish.publish": mock}
Expand Down Expand Up @@ -623,7 +648,7 @@ def test_copy(job1):
"result": False,
}

with patch_makedirs, patch_schedule_opts, patch_schedule_event_fire, patch_schedule_get_event:
with patch_makedirs, patch_schedule_opts, patch_schedule_event_fire, patch_schedule_get_event, patch_schedule_connect_pub:
mock = MagicMock(return_value={})
patch_schedule_publish = patch.dict(
schedule.__salt__, {"publish.publish": mock}
Expand Down Expand Up @@ -733,8 +758,11 @@ def test_modify(job1):
patch_schedule_event_fire = patch.dict(
schedule.__salt__, {"event.fire": MagicMock(return_value=True)}
)
patch_schedule_connect_pub = patch.object(
SaltEvent, "connect_pub", return_value=True
)

with patch_makedirs, patch_schedule_opts, patch_schedule_event_fire:
with patch_makedirs, patch_schedule_opts, patch_schedule_event_fire, patch_schedule_connect_pub:

_ret_value = {"complete": True, "schedule": {"job1": current_job1}}
patch_schedule_get_event = patch.object(
Expand Down Expand Up @@ -888,13 +916,16 @@ def test_is_enabled():
schedule.__salt__,
{"event.fire": MagicMock(return_value=True), "schedule.list": mock_lst},
)
patch_schedule_connect_pub = patch.object(
SaltEvent, "connect_pub", return_value=True
)
patch_schedule_get_event = patch.object(
SaltEvent,
"get_event",
return_value={"complete": True, "schedule": {"job1": job1}},
)

with patch_makedirs, patch_schedule_opts, patch_schedule_get_event, patch_schedule_salt:
with patch_makedirs, patch_schedule_opts, patch_schedule_get_event, patch_schedule_connect_pub, patch_schedule_salt:
ret = schedule.is_enabled("job1")
assert ret == job1

Expand Down Expand Up @@ -931,8 +962,11 @@ def test_job_status():
patch_schedule_get_event = patch.object(
SaltEvent, "get_event", return_value={"complete": True, "data": job1}
)
patch_schedule_connect_pub = patch.object(
SaltEvent, "connect_pub", return_value=True
)

with patch_makedirs, patch_schedule_opts, patch_schedule_get_event, patch_schedule_salt:
with patch_makedirs, patch_schedule_opts, patch_schedule_get_event, patch_schedule_connect_pub, patch_schedule_salt:
ret = schedule.job_status("job1")
assert ret == {
"_last_run": "2021-11-01T12:36:57",
Expand Down Expand Up @@ -981,11 +1015,14 @@ def test_list(job1):
patch_schedule_get_event = patch.object(
SaltEvent, "get_event", return_value=_ret_value
)
patch_schedule_connect_pub = patch.object(
SaltEvent, "connect_pub", return_value=True
)
patch_schedule_os_path_exists = patch(
"os.path.exists", MagicMock(return_value=True)
)

with patch_schedule_opts, patch_schedule_event_fire, patch_schedule_get_event, patch_schedule_os_path_exists, patch_makedirs:
with patch_schedule_opts, patch_schedule_event_fire, patch_schedule_get_event, patch_schedule_connect_pub, patch_schedule_os_path_exists, patch_makedirs:
with patch(
"salt.utils.files.fopen", mock_open(read_data=saved_schedule)
) as fopen_mock:
Expand Down Expand Up @@ -1013,7 +1050,7 @@ def test_list(job1):
seconds: 10
"""

with patch_schedule_opts, patch_makedirs, patch_schedule_event_fire, patch_schedule_get_event, patch_schedule_os_path_exists:
with patch_schedule_opts, patch_makedirs, patch_schedule_event_fire, patch_schedule_get_event, patch_schedule_connect_pub, patch_schedule_os_path_exists:
with patch("salt.utils.files.fopen", mock_open(read_data="")) as fopen_mock:
ret = schedule.list_()
assert ret == expected
Expand Down Expand Up @@ -1045,7 +1082,7 @@ def test_list(job1):
seconds: 10
"""

with patch_schedule_opts, patch_schedule_event_fire, patch_schedule_get_event, patch_schedule_os_path_exists, patch_makedirs:
with patch_schedule_opts, patch_schedule_event_fire, patch_schedule_get_event, patch_schedule_connect_pub, patch_schedule_os_path_exists, patch_makedirs:
with patch(
"salt.utils.files.fopen", mock_open(read_data=saved_schedule)
) as fopen_mock:
Expand Down Expand Up @@ -1097,11 +1134,14 @@ def test_list_global_enabled(job1):
patch_schedule_get_event = patch.object(
SaltEvent, "get_event", return_value=_ret_value
)
patch_schedule_connect_pub = patch.object(
SaltEvent, "connect_pub", return_value=True
)
patch_schedule_os_path_exists = patch(
"os.path.exists", MagicMock(return_value=True)
)

with patch_schedule_opts, patch_schedule_event_fire, patch_schedule_get_event, patch_schedule_os_path_exists, patch_makedirs:
with patch_schedule_opts, patch_schedule_event_fire, patch_schedule_get_event, patch_schedule_connect_pub, patch_schedule_os_path_exists, patch_makedirs:
with patch(
"salt.utils.files.fopen", mock_open(read_data=saved_schedule)
) as fopen_mock:
Expand Down Expand Up @@ -1154,11 +1194,14 @@ def test_list_global_disabled(job1):
patch_schedule_get_event = patch.object(
SaltEvent, "get_event", return_value=_ret_value
)
patch_schedule_connect_pub = patch.object(
SaltEvent, "connect_pub", return_value=True
)
patch_schedule_os_path_exists = patch(
"os.path.exists", MagicMock(return_value=True)
)

with patch_schedule_opts, patch_schedule_event_fire, patch_schedule_get_event, patch_schedule_os_path_exists, patch_makedirs:
with patch_schedule_opts, patch_schedule_event_fire, patch_schedule_get_event, patch_schedule_connect_pub, patch_schedule_os_path_exists, patch_makedirs:
with patch(
"salt.utils.files.fopen", mock_open(read_data=saved_schedule)
) as fopen_mock:
Expand Down

0 comments on commit 449cca2

Please sign in to comment.