Skip to content

Commit

Permalink
fix: added mock to stop leaking journalctl that slows down unit test (#…
Browse files Browse the repository at this point in the history
…4556)

In test_logs.py, test_collect_logs_includes_optional_userdata() would leak the
journalctl subprocess call because it did not mock subprocess.call() like
test_collect_logs_creates_tarfile() does. This would cause the unit test to
take a long time to run because it was calling the actual journalctl command.

Fixes GH-4536
  • Loading branch information
a-dubs authored and holmanb committed Nov 14, 2023
1 parent 550cc92 commit 0f3f530
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tests/unittests/cmd/devel/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,22 @@ def fake_subp(cmd):
subp(cmd) # Pass through tar cmd so we can check output
return SubpResult(expected_subp[cmd_tuple], "")

def fake_subprocess_call(cmd, stdout=None, stderr=None):
cmd_tuple = tuple(cmd)
if cmd_tuple not in expected_subp:
raise AssertionError(
"Unexpected command provided to subprocess: {0}".format(
cmd
)
)
stdout.write(expected_subp[cmd_tuple])

fake_stderr = mock.MagicMock()

mocker.patch(M_PATH + "subp", side_effect=fake_subp)
mocker.patch(
M_PATH + "subprocess.call", side_effect=fake_subprocess_call
)
mocker.patch(M_PATH + "sys.stderr", fake_stderr)
mocker.patch(M_PATH + "CLOUDINIT_LOGS", [log1, log2])
mocker.patch(M_PATH + "CLOUDINIT_RUN_DIR", run_dir)
Expand Down

0 comments on commit 0f3f530

Please sign in to comment.