Skip to content

Commit

Permalink
unit tests now cover both stdout and stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
a-dubs committed Jul 29, 2023
1 parent a865e0f commit 1a2981a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
2 changes: 0 additions & 2 deletions cloudinit/cmd/devel/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ def _write_command_output_to_file(cmd, filename, msg, verbosity):
try:
ensure_dir(os.path.dirname(filename))
output = subp(cmd)[0]
# with open(filename, "w") as f:
# f.write(output)
write_file(filename, output)
except ProcessExecutionError as e:
write_file(filename, str(e))
Expand Down
55 changes: 41 additions & 14 deletions tests/unittests/cmd/devel/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,37 +211,64 @@ def fake_subp(cmd):

def test_write_command_output_to_file(self, m_getuid, tmpdir):
# what does this do???
test_str_1 = "test #1"
test_str_2 = "test #2"
m_getuid.return_value = 100
test_str = "cloud-init? more like cloud-innit!"
output_file1 = tmpdir.join("test-output-file-1.txt")
output_file2 = tmpdir.join("test-output-file-2.txt")
# output_file3 = tmpdir.join("test-output-file-3.txt")
nonexistent_path = "/this-directory-does-not-exist"
expected_stderr = (
"Unexpected error while running command.\n"
+ f"Command: ['ls', '{nonexistent_path}']\n"
+ "Exit code: 2\n"
+ "Reason: -\n"
+ "Stdout: \n"
+ f"Stderr: ls: cannot access '{dir}': No such file or directory"
)
return_output1 = logs._write_command_output_to_file(
filename=output_file1,
cmd=["echo", test_str_1],
cmd=["echo", test_str],
msg="",
verbosity=1,
)
return_output2 = logs._write_command_output_to_file(
filename=output_file2,
cmd=["ls", nonexistent_path],
msg="",
verbosity=1,
)

assert test_str + "\n" == return_output1
assert test_str + "\n" == load_file(output_file1)
assert expected_stderr == return_output2
assert expected_stderr == load_file(output_file2)

def test_stream_command_output_to_file(self, m_getuid, tmpdir):
test_str = "cloud-init, shmoud-init"
expected_stderr = (
"ls: cannot access '/this-directory-does-not-exist':"
+ " No such file or directory"
)
m_getuid.return_value = 100
output_file1 = tmpdir.join("test-output-file-1.txt")
output_file2 = tmpdir.join("test-output-file-2.txt")
logs._stream_command_output_to_file(
filename=output_file1,
cmd=["echo", test_str],
msg="",
verbosity=1,
)
logs._stream_command_output_to_file(
filename=output_file2,
cmd=["echo", test_str_2],
cmd=["ls", "/this-directory-does-not-exist"],
msg="",
verbosity=1,
)
# return_output3 = logs._write_command_output_to_file(
# filename=output_file3,
# cmd=["ls", dir:="/this-directory-does-not-exist"],
# msg="",
# verbosity=1,
# )

assert test_str_1 + "\n" == return_output1
assert test_str_1 + "\n" == load_file(output_file1)
assert test_str + "\n" == load_file(output_file1)

# no output should have been returned so this value should be None
# assert None == return_output2
assert test_str_2 + "\n" == load_file(output_file2)
assert expected_stderr + "\n" == load_file(output_file2)

# expected_err_msg = "Unexpected error while running command.\n" + \
# f"Command: ['ls', '{dir}']\n" + \
Expand Down

0 comments on commit 1a2981a

Please sign in to comment.