Skip to content

Commit

Permalink
Fix mock calls
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
  • Loading branch information
s0undt3ch committed Jun 14, 2023
1 parent 3c415b2 commit 3506e7f
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 42 deletions.
12 changes: 6 additions & 6 deletions tests/pytests/unit/modules/test_aptpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1290,17 +1290,17 @@ def test_call_apt_dpkg_lock():
]

cmd_mock = MagicMock(side_effect=cmd_side_effect)
cmd_call = (
cmd_call = [
call(
["dpkg", "-l", "python"],
env={},
ignore_retcode=False,
output_loglevel="quiet",
python_shell=True,
env={},
ignore_retcode=False,
username="Darth Vader",
),
)
expected_calls = [cmd_call * 5]
]
expected_calls = cmd_call * 5

with patch.dict(
aptpkg.__salt__,
Expand All @@ -1320,7 +1320,7 @@ def test_call_apt_dpkg_lock():

# We should attempt to call the cmd 5 times
assert cmd_mock.call_count == 5
cmd_mock.has_calls(expected_calls)
cmd_mock.assert_has_calls(expected_calls)


def test_services_need_restart_checkrestart_missing():
Expand Down
8 changes: 4 additions & 4 deletions tests/pytests/unit/modules/test_linux_sysctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def test_persist_no_conf_failure():
):
with pytest.raises(CommandExecutionError):
linux_sysctl.persist("net.ipv4.ip_forward", 42, config=None)
fopen_mock.called_once()
fopen_mock.assert_called_once()


def test_persist_no_conf_success():
Expand Down Expand Up @@ -353,7 +353,7 @@ def test_persist_value_with_spaces_already_set(tmp_path):
"""
config = str(tmp_path / "existing_sysctl_with_spaces.conf")
value = "|/usr/share/kdump-tools/dump-core %p %s %t %e"
config_file_content = "kernel.core_pattern = {}\n".format(value)
config_file_content = f"kernel.core_pattern = {value}\n"
with fopen(config, "w", encoding="utf-8") as config_file:
config_file.write(config_file_content)
mock_run = MagicMock(return_value=value)
Expand Down Expand Up @@ -383,7 +383,7 @@ def test_persist_value_with_spaces_already_configured(tmp_path):
"""
config = str(tmp_path / "existing_sysctl_with_spaces.conf")
value = "|/usr/share/kdump-tools/dump-core %p %s %t %e"
config_file_content = "kernel.core_pattern = {}\n".format(value)
config_file_content = f"kernel.core_pattern = {value}\n"
with fopen(config, "w", encoding="utf-8") as config_file:
config_file.write(config_file_content)
mock_run = MagicMock(return_value="")
Expand Down Expand Up @@ -451,7 +451,7 @@ def test_persist_value_with_spaces_update_config(tmp_path):
assert os.path.isfile(config)
with fopen(config, encoding="utf-8") as config_file:
written = config_file.read()
assert written == "kernel.core_pattern = {}\n".format(value)
assert written == f"kernel.core_pattern = {value}\n"


def test_persist_value_with_spaces_new_file(tmp_path):
Expand Down
4 changes: 2 additions & 2 deletions tests/pytests/unit/modules/test_win_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def test_enable():
):
assert win_ip.enable("Ethernet")

mock_cmd.called_once_with(
mock_cmd.assert_called_once_with(
[
"netsh",
"interface",
Expand Down Expand Up @@ -180,7 +180,7 @@ def test_disable():
):
assert win_ip.disable("Ethernet")

mock_cmd.called_once_with(
mock_cmd.assert_called_once_with(
[
"netsh",
"interface",
Expand Down
2 changes: 1 addition & 1 deletion tests/pytests/unit/test_master.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_fileserver_duration():
end = time.time()
# Interval is equal to timeout so the _do_update method will be called
# one time.
update.called_once()
update.assert_called_once()
# Timeout is 1 second
duration = end - start
if duration > 2 and salt.utils.platform.spawning_platform():
Expand Down
4 changes: 3 additions & 1 deletion tests/pytests/unit/test_minion.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,9 @@ def compile_pillar(self):
with patch("salt.pillar.get_pillar", return_value=MockPillarCompiler()):
with patch("salt.loader.executors") as execmock:
minion.gen_modules()
assert execmock.called_with(minion.opts, minion.functions)
execmock.assert_called_with(
minion.opts, functions=minion.functions, proxy=minion.proxy, context={}
)
finally:
minion.destroy()

Expand Down
24 changes: 12 additions & 12 deletions tests/pytests/unit/utils/event/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def sock_dir(tmp_path):
def _assert_got_event(evt, data, msg=None, expected_failure=False):
assert evt is not None, msg
for key in data:
assert key in evt, "{}: Key {} missing".format(msg, key)
assert key in evt, f"{msg}: Key {key} missing"
assertMsg = "{0}: Key {1} value mismatch, {2} != {3}"
assertMsg = assertMsg.format(msg, key, data[key], evt[key])
if not expected_failure:
Expand All @@ -59,8 +59,8 @@ def test_minion_event(sock_dir):
:10
]
with salt.utils.event.MinionEvent(opts, listen=False) as me:
assert me.puburi == str(sock_dir / "minion_event_{}_pub.ipc".format(id_hash))
assert me.pulluri == str(sock_dir / "minion_event_{}_pull.ipc".format(id_hash))
assert me.puburi == str(sock_dir / f"minion_event_{id_hash}_pub.ipc")
assert me.pulluri == str(sock_dir / f"minion_event_{id_hash}_pull.ipc")


def test_minion_event_tcp_ipc_mode():
Expand All @@ -73,8 +73,8 @@ def test_minion_event_tcp_ipc_mode():
def test_minion_event_no_id(sock_dir):
with salt.utils.event.MinionEvent(dict(sock_dir=str(sock_dir)), listen=False) as me:
id_hash = hashlib.sha256(salt.utils.stringutils.to_bytes("")).hexdigest()[:10]
assert me.puburi == str(sock_dir / "minion_event_{}_pub.ipc".format(id_hash))
assert me.pulluri == str(sock_dir / "minion_event_{}_pull.ipc".format(id_hash))
assert me.puburi == str(sock_dir / f"minion_event_{id_hash}_pub.ipc")
assert me.pulluri == str(sock_dir / f"minion_event_{id_hash}_pull.ipc")


@pytest.mark.slow_test
Expand Down Expand Up @@ -256,9 +256,9 @@ def test_event_many(sock_dir):
with eventpublisher_process(str(sock_dir)):
with salt.utils.event.MasterEvent(str(sock_dir), listen=True) as me:
for i in range(500):
me.fire_event({"data": "{}".format(i)}, "testevents")
me.fire_event({"data": f"{i}"}, "testevents")
evt = me.get_event(tag="testevents")
_assert_got_event(evt, {"data": "{}".format(i)}, "Event {}".format(i))
_assert_got_event(evt, {"data": f"{i}"}, f"Event {i}")


@pytest.mark.slow_test
Expand All @@ -268,10 +268,10 @@ def test_event_many_backlog(sock_dir):
with salt.utils.event.MasterEvent(str(sock_dir), listen=True) as me:
# Must not exceed zmq HWM
for i in range(500):
me.fire_event({"data": "{}".format(i)}, "testevents")
me.fire_event({"data": f"{i}"}, "testevents")
for i in range(500):
evt = me.get_event(tag="testevents")
_assert_got_event(evt, {"data": "{}".format(i)}, "Event {}".format(i))
_assert_got_event(evt, {"data": f"{i}"}, f"Event {i}")


# Test the fire_master function. As it wraps the underlying fire_event,
Expand Down Expand Up @@ -300,7 +300,7 @@ def test_connect_pull_should_debug_log_on_StreamClosedError():
event = SaltEvent(node=None)
with patch.object(event, "pusher") as mock_pusher:
with patch.object(
salt.utils.event.log, "debug", auto_spec=True
salt.utils.event.log, "debug", autospec=True
) as mock_log_debug:
mock_pusher.connect.side_effect = tornado.iostream.StreamClosedError
event.connect_pull()
Expand All @@ -315,10 +315,10 @@ def test_connect_pull_should_error_log_on_other_errors(error):
event = SaltEvent(node=None)
with patch.object(event, "pusher") as mock_pusher:
with patch.object(
salt.utils.event.log, "debug", auto_spec=True
salt.utils.event.log, "debug", autospec=True
) as mock_log_debug:
with patch.object(
salt.utils.event.log, "error", auto_spec=True
salt.utils.event.log, "error", autospec=True
) as mock_log_error:
mock_pusher.connect.side_effect = error
event.connect_pull()
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/modules/test_nilrt_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_change_state_down_state(self):
"salt.modules.nilrt_ip._change_dhcp_config", return_value=True
) as change_dhcp_config_mock:
assert nilrt_ip._change_state("test_interface", "down")
assert change_dhcp_config_mock.called_with("test_interface", False)
change_dhcp_config_mock.assert_called_with("test_interface", False)

def test_change_state_up_state(self):
"""
Expand All @@ -42,7 +42,7 @@ def test_change_state_up_state(self):
"salt.modules.nilrt_ip._change_dhcp_config", return_value=True
) as change_dhcp_config_mock:
assert nilrt_ip._change_state("test_interface", "up")
assert change_dhcp_config_mock.called_with("test_interface")
change_dhcp_config_mock.assert_called_with("test_interface")

def test_set_static_all_with_dns(self):
"""
Expand Down
22 changes: 8 additions & 14 deletions tests/unit/netapi/rest_tornado/test_saltnado.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,6 @@ def completer():
with patch.object(
self.handler.application.event_listener,
"get_event",
autospec=True,
side_effect=fancy_get_event,
), patch.dict(
self.handler.application.opts,
Expand Down Expand Up @@ -699,7 +698,6 @@ def toggle_is_finished(*args, **kwargs):
with patch.object(
self.handler.application.event_listener,
"get_event",
autospec=True,
side_effect=fancy_get_event,
), patch.object(
self.handler,
Expand Down Expand Up @@ -730,8 +728,8 @@ def test_when_is_finished_then_all_collected_data_should_be_returned(self):
{
"tag": "fnord",
"data": {
"return": "return from fnord {}".format(i),
"id": "fnord {}".format(i),
"return": f"return from fnord {i}",
"id": f"fnord {i}",
},
}
)
Expand Down Expand Up @@ -761,7 +759,6 @@ def toggle_is_finished(*args, **kwargs):
with patch.object(
self.handler.application.event_listener,
"get_event",
autospec=True,
side_effect=fancy_get_event,
), patch.object(
self.handler,
Expand Down Expand Up @@ -795,8 +792,8 @@ def test_when_is_timed_out_then_all_collected_data_should_be_returned(self):
{
"tag": "fnord",
"data": {
"return": "return from fnord {}".format(i),
"id": "fnord {}".format(i),
"return": f"return from fnord {i}",
"id": f"fnord {i}",
},
}
)
Expand All @@ -821,7 +818,6 @@ def fancy_get_event(*args, **kwargs):
with patch.object(
self.handler.application.event_listener,
"get_event",
autospec=True,
side_effect=fancy_get_event,
), patch.dict(
self.handler.application.opts,
Expand All @@ -844,12 +840,12 @@ def test_when_minions_all_return_then_all_collected_data_should_be_returned(self
completed_events = [tornado.gen.Future() for _ in range(10)]
events_by_id = {}
for i, event in enumerate(completed_events):
id_ = "fnord {}".format(i)
id_ = f"fnord {i}"
events_by_id[id_] = event
event.set_result(
{
"tag": "fnord",
"data": {"return": "return from {}".format(id_), "id": id_},
"data": {"return": f"return from {id_}", "id": id_},
}
)
expected_result = {
Expand Down Expand Up @@ -879,7 +875,6 @@ def fancy_get_event(*args, **kwargs):
with patch.object(
self.handler.application.event_listener,
"get_event",
autospec=True,
side_effect=fancy_get_event,
), patch.dict(
self.handler.application.opts,
Expand All @@ -905,12 +900,12 @@ def test_when_min_wait_time_has_not_passed_then_disbatch_should_not_return_expec
events_by_id = {}
# Setup some real-enough looking return data
for i, event in enumerate(completed_events):
id_ = "fnord {}".format(i)
id_ = f"fnord {i}"
events_by_id[id_] = event
event.set_result(
{
"tag": "fnord",
"data": {"return": "return from {}".format(id_), "id": id_},
"data": {"return": f"return from {id_}", "id": id_},
}
)
# Hard coded instead of dynamic to avoid potentially writing a test
Expand Down Expand Up @@ -972,7 +967,6 @@ def fake_sleep(timer):
with patch.object(
self.handler.application.event_listener,
"get_event",
autospec=True,
side_effect=fancy_get_event,
), patch.object(
self.handler,
Expand Down

0 comments on commit 3506e7f

Please sign in to comment.