Completed
Push — master ( b3ff01...2a4f2c )
by Mathieu
59s
created

test_main_resume_only_pending()   B

Complexity

Conditions 3

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 26
rs 8.8571
1
import os
2
import unittest
3
import tempfile
4
import shutil
5
from os.path import join as pjoin
6
7
from subprocess import call
8
9
from nose.tools import assert_true, assert_equal
10
11
12
class TestSmartdispatcher(unittest.TestCase):
13
14
    def setUp(self):
15
        self.testing_dir = tempfile.mkdtemp()
16
        self.logs_dir = os.path.join(self.testing_dir, 'SMART_DISPATCH_LOGS')
17
18
        base_command = 'smart_dispatch.py --pool 10 -C 42 -q test -t 5:00 -x {0}'
19
        self.launch_command = base_command.format('launch echo "[1 2 3 4]" "[6 7 8]" "[9 0]"')
20
        self.resume_command = base_command.format('resume {0}')
21
22
        self._cwd = os.getcwd()
23
        os.chdir(self.testing_dir)
24
25
    def tearDown(self):
26
        print "Tear down"
27
        os.chdir(self._cwd)
28
        shutil.rmtree(self.testing_dir)
29
30
    def test_main_launch(self):
31
        # Actual test
32
        exit_status = call(self.launch_command, shell=True)
33
34
        # Test validation
35
        assert_equal(exit_status, 0)
36
        assert_true(os.path.isdir(self.logs_dir))
37
        assert_equal(len(os.listdir(self.logs_dir)), 1)
38
39
    def test_main_resume(self):
40
        # SetUp
41
        call(self.launch_command, shell=True)
42
        batch_uid = os.listdir(self.logs_dir)[0]
43
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).
55
        exit_status = call(self.resume_command.format(batch_uid), shell=True)
56
57
        # Test validation
58
        assert_equal(exit_status, 0)
59
        assert_true(os.path.isdir(self.logs_dir))
60
        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]))
90