Skip to content

Commit 5c73901

Browse files
committed
[TST] Addressed @mgermain's comment. Added unit test for the --onlyPending option
1 parent 8cbe5af commit 5c73901

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

tests/test_smart_dispatch.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33
import tempfile
44
import shutil
5+
from os.path import join as pjoin
56

67
from subprocess import call
78

@@ -15,13 +16,14 @@ def setUp(self):
1516
self.logs_dir = os.path.join(self.testing_dir, 'SMART_DISPATCH_LOGS')
1617

1718
base_command = 'smart_dispatch.py --pool 10 -C 42 -q test -t 5:00 -x {0}'
18-
self.launch_command = base_command.format('launch echo "1 2 3 4" "6 7 8" "9 0"')
19+
self.launch_command = base_command.format('launch echo "[1 2 3 4]" "[6 7 8]" "[9 0]"')
1920
self.resume_command = base_command.format('resume {0}')
2021

2122
self._cwd = os.getcwd()
2223
os.chdir(self.testing_dir)
2324

2425
def tearDown(self):
26+
print "Tear down"
2527
os.chdir(self._cwd)
2628
shutil.rmtree(self.testing_dir)
2729

@@ -39,10 +41,49 @@ def test_main_resume(self):
3941
call(self.launch_command, shell=True)
4042
batch_uid = os.listdir(self.logs_dir)[0]
4143

42-
# Actual test
44+
# Simulate that some commands are in the running state.
45+
path_job_commands = os.path.join(self.logs_dir, batch_uid, "commands")
46+
pending_commands_file = pjoin(path_job_commands, "commands.txt")
47+
running_commands_file = pjoin(path_job_commands, "running_commands.txt")
48+
commands = open(pending_commands_file).read().strip().split("\n")
49+
with open(running_commands_file, 'w') as running_commands:
50+
running_commands.write("\n".join(commands[::2]) + "\n")
51+
with open(pending_commands_file, 'w') as pending_commands:
52+
pending_commands.write("\n".join(commands[1::2]) + "\n")
53+
54+
# Actual test (should move running commands back to pending).
4355
exit_status = call(self.resume_command.format(batch_uid), shell=True)
4456

4557
# Test validation
4658
assert_equal(exit_status, 0)
4759
assert_true(os.path.isdir(self.logs_dir))
4860
assert_equal(len(os.listdir(self.logs_dir)), 1)
61+
assert_equal(len(open(running_commands_file).readlines()), 0)
62+
assert_equal(len(open(pending_commands_file).readlines()), len(commands))
63+
64+
def test_main_resume_only_pending(self):
65+
# SetUp
66+
call(self.launch_command, shell=True)
67+
batch_uid = os.listdir(self.logs_dir)[0]
68+
69+
# Simulate that some commands are in the running state.
70+
path_job_commands = os.path.join(self.logs_dir, batch_uid, "commands")
71+
pending_commands_file = pjoin(path_job_commands, "commands.txt")
72+
running_commands_file = pjoin(path_job_commands, "running_commands.txt")
73+
commands = open(pending_commands_file).read().strip().split("\n")
74+
with open(running_commands_file, 'w') as running_commands:
75+
running_commands.write("\n".join(commands[::2]) + "\n")
76+
with open(pending_commands_file, 'w') as pending_commands:
77+
pending_commands.write("\n".join(commands[1::2]) + "\n")
78+
79+
# Actual test (should NOT move running commands back to pending).
80+
command_line = self.resume_command.format(batch_uid)
81+
command_line += " --onlyPending"
82+
exit_status = call(command_line, shell=True)
83+
84+
# Test validation
85+
assert_equal(exit_status, 0)
86+
assert_true(os.path.isdir(self.logs_dir))
87+
assert_equal(len(os.listdir(self.logs_dir)), 1)
88+
assert_equal(len(open(running_commands_file).readlines()), len(commands[::2]))
89+
assert_equal(len(open(pending_commands_file).readlines()), len(commands[1::2]))

0 commit comments

Comments
 (0)