Skip to content

Commit 67d6bf3

Browse files
committed
[TST] output and error logs produced by smart_worker.py.
1 parent ea96413 commit 67d6bf3

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

scripts/smart_worker.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,17 @@ def main():
4545
stdout_filename = os.path.join(args.logs_dir, uid + ".out")
4646
stderr_filename = os.path.join(args.logs_dir, uid + ".err")
4747

48+
stdout_already_exists = os.path.isfile(stdout_filename)
49+
stderr_already_exists = os.path.isfile(stderr_filename)
50+
4851
with open(stdout_filename, 'a') as stdout_file:
4952
with open(stderr_filename, 'a') as stderr_file:
53+
if stdout_already_exists:
54+
stdout_file.write('\n')
55+
56+
if stderr_already_exists:
57+
stderr_file.write('\n')
58+
5059
stdout_file.write(t.strftime("## %Y-%m-%d %H:%M:%S ##\n"))
5160
stdout_file.write("# " + command + '\n')
5261
stdout_file.flush()

tests/test_smart_worker.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ def tearDown(self):
3131
def test_main(self):
3232
command = ["smart_worker.py", self.command_manager._commands_filename, self.logs_dir]
3333
assert_equal(call(command), 0)
34+
# Simulate a resume, i.e. re-run the command, the output/error should be concatenated.
35+
self.command_manager.set_commands_to_run(self.commands)
36+
assert_equal(call(command), 0)
3437

3538
# Check output logs
3639
filenames = os.listdir(self.logs_dir)
@@ -41,13 +44,22 @@ def test_main(self):
4144
uid = os.path.splitext(os.path.basename(log_filename))[0]
4245
executed_command = self.commands[self.commands_uid.index(uid)]
4346

44-
# First line is the executed command in comment
45-
header = logfile.readline().strip()
46-
assert_equal(header, "# " + executed_command)
47+
# Since the command was run twice.
48+
for _ in range(2):
49+
# First line is the datetime of the executed command in comment.
50+
line = logfile.readline().strip()
51+
assert_true(time.strftime("## %Y-%m-%d %H:%M:") in line) # Don't check seconds.
52+
53+
# Second line is the executed command in comment.
54+
line = logfile.readline().strip()
55+
assert_equal(line, "# " + executed_command)
4756

48-
# Next should be the command's output
49-
output = logfile.readline().strip()
50-
assert_equal(output, executed_command[-1]) # We know those are 'echo' of a digit
57+
# Next should be the command's output
58+
line = logfile.readline().strip()
59+
assert_equal(line, executed_command[-1]) # We know those are 'echo' of a digit
60+
61+
# Empty line
62+
assert_equal(logfile.readline().strip(), "")
5163

5264
# Log should be empty now
5365
assert_equal("", logfile.read())
@@ -60,9 +72,18 @@ def test_main(self):
6072
uid = os.path.splitext(os.path.basename(log_filename))[0]
6173
executed_command = self.commands[self.commands_uid.index(uid)]
6274

63-
# First line is the executed command in comment
64-
header = logfile.readline().strip()
65-
assert_equal(header, "# " + executed_command)
75+
# Since the command was run twice.
76+
for _ in range(2):
77+
# First line is the datetime of the executed command in comment.
78+
line = logfile.readline().strip()
79+
assert_true(time.strftime("## %Y-%m-%d %H:%M:") in line) # Don't check seconds.
80+
81+
# Second line is the executed command in comment.
82+
line = logfile.readline().strip()
83+
assert_equal(line, "# " + executed_command)
84+
85+
# Empty line
86+
assert_equal(logfile.readline().strip(), "")
6687

6788
# Log should be empty now
6889
assert_equal("", logfile.read())

0 commit comments

Comments
 (0)