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 22, 2023
1 parent a2d0ebc commit c179c65
Showing 1 changed file with 56 additions and 17 deletions.
73 changes: 56 additions & 17 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 Down Expand Up @@ -475,13 +490,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 +541,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 +600,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 +644,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 +754,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 +912,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 +958,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 +1011,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 +1046,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 +1078,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 +1130,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 +1190,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 c179c65

Please sign in to comment.