Skip to content

Commit 438c1ef

Browse files
committed
Added unit tests for --commandsFile option.
1 parent 8b4b232 commit 438c1ef

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

scripts/smart_dispatch.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ def parse_arguments():
150150
parser.add_argument('-c', '--coresPerCommand', type=int, required=False, help='How many cores a command needs.', default=1)
151151
parser.add_argument('-g', '--gpusPerCommand', type=int, required=False, help='How many gpus a command needs.', default=1)
152152
# parser.add_argument('-m', '--memPerCommand', type=float, required=False, help='How much memory a command needs (in Gb).')
153-
parser.add_argument('-f', '--commandsFile', type=str, required=False, help='File containing commands to launch. Each command must be on a seperate line. (Replaces commandAndOptions)')
153+
parser.add_argument('-f', '--commandsFile', type=argparse.FileType('r'), required=False,
154+
help='File containing commands to launch. Each command must be on a seperate line. (Replaces commandAndOptions)')
154155

155156
parser.add_argument('-l', '--modules', type=str, required=False, help='List of additional modules to load.', nargs='+')
156157
parser.add_argument('-x', '--doNotLaunch', action='store_true', help='Creates the QSUB files without launching them.')
@@ -174,9 +175,6 @@ def parse_arguments():
174175
if args.queueName not in AVAILABLE_QUEUES and ((args.coresPerNode is None and args.gpusPerNode is None) or args.walltime is None):
175176
parser.error("Unknown queue, --coresPerNode/--gpusPerNode and --walltime must be set.")
176177

177-
if args.commandsFile is not None and not os.path.isfile(args.commandsFile):
178-
parser.error("Command file does not exist: '{0}'.".format(args.commandsFile))
179-
180178
return args
181179

182180

tests/test_smart_dispatch.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@ def setUp(self):
1515
self.testing_dir = tempfile.mkdtemp()
1616
self.logs_dir = os.path.join(self.testing_dir, 'SMART_DISPATCH_LOGS')
1717

18-
self.commands = 'echo "[1 2 3 4]" "[6 7 8]" "[9 0]"'
19-
self.nb_commands = 4*3*2
18+
self.folded_commands = 'echo "[1 2 3 4]" "[6 7 8]" "[9 0]"'
19+
self.commands = ["echo 1 6 9", "echo 1 6 0", "echo 1 7 9", "echo 1 7 0", "echo 1 8 9", "echo 1 8 0",
20+
"echo 2 6 9", "echo 2 6 0", "echo 2 7 9", "echo 2 7 0", "echo 2 8 9", "echo 2 8 0",
21+
"echo 3 6 9", "echo 3 6 0", "echo 3 7 9", "echo 3 7 0", "echo 3 8 9", "echo 3 8 0",
22+
"echo 4 6 9", "echo 4 6 0", "echo 4 7 9", "echo 4 7 0", "echo 4 8 9", "echo 4 8 0"]
23+
self.nb_commands = len(self.commands)
2024

21-
smart_dispatch_command = 'smart_dispatch.py -C 1 -q test -t 5:00 -x {0}'
22-
self.launch_command = smart_dispatch_command.format('launch ' + self.commands)
23-
self.resume_command = smart_dispatch_command.format('resume {0}')
25+
self.smart_dispatch_command = 'smart_dispatch.py -C 1 -q test -t 5:00 -x'
26+
self.launch_command = self.smart_dispatch_command + " launch " + self.folded_commands
27+
self.resume_command = self.smart_dispatch_command + " resume {0}"
2428

2529
smart_dispatch_command_with_pool = 'smart_dispatch.py --pool 10 -C 1 -q test -t 5:00 -x {0}'
26-
self.launch_command_with_pool = smart_dispatch_command_with_pool.format('launch ' + self.commands)
30+
self.launch_command_with_pool = smart_dispatch_command_with_pool.format('launch ' + self.folded_commands)
2731
self.nb_workers = 10
2832

2933
self._cwd = os.getcwd()
@@ -46,6 +50,24 @@ def test_main_launch(self):
4650
path_job_commands = os.path.join(self.logs_dir, batch_uid, "commands")
4751
assert_equal(len(os.listdir(path_job_commands)), self.nb_commands + 1)
4852

53+
def test_launch_using_commands_file(self):
54+
# Actual test
55+
commands_filename = "commands_to_run.txt"
56+
open(commands_filename, 'w').write("\n".join(self.commands))
57+
58+
launch_command = self.smart_dispatch_command + " -f {0} launch".format(commands_filename)
59+
exit_status = call(launch_command, shell=True)
60+
61+
# Test validation
62+
assert_equal(exit_status, 0)
63+
assert_true(os.path.isdir(self.logs_dir))
64+
assert_equal(len(os.listdir(self.logs_dir)), 1)
65+
66+
batch_uid = os.listdir(self.logs_dir)[0]
67+
path_job_commands = os.path.join(self.logs_dir, batch_uid, "commands")
68+
assert_equal(len(os.listdir(path_job_commands)), self.nb_commands + 1)
69+
assert_equal(open(pjoin(path_job_commands, 'commands.txt')).read(), "\n".join(self.commands) + "\n")
70+
4971
def test_main_launch_with_pool_of_workers(self):
5072
# Actual test
5173
exit_status = call(self.launch_command_with_pool, shell=True)

0 commit comments

Comments
 (0)