1
|
|
|
import unittest |
2
|
|
|
from smartdispatch import smartdispatch_script |
3
|
|
|
import subprocess |
4
|
|
|
from mock import patch |
5
|
|
|
import tempfile as tmp |
6
|
|
|
import shutil |
7
|
|
|
import traceback |
8
|
|
|
|
9
|
|
|
class TestSmartScript(unittest.TestCase): |
10
|
|
|
|
11
|
|
|
def setUp(self): |
12
|
|
|
self._base_dir = tmp.mkdtemp() |
13
|
|
|
smartdispatch_script.LOGS_FOLDERNAME = self._base_dir |
14
|
|
|
|
15
|
|
|
def tearDown(self): |
16
|
|
|
shutil.rmtree(self._base_dir) |
17
|
|
|
|
18
|
|
|
def test_gpu_check(self): |
19
|
|
|
|
20
|
|
|
argv = ['-x', '-g', '2', '-G', '1', '-C', '1', '-q', 'random', '-t', '00:00:10' ,'launch', 'echo', 'testing123'] |
21
|
|
|
|
22
|
|
|
# Test if the check fail |
23
|
|
|
with self.assertRaises(SystemExit) as context: |
24
|
|
|
smartdispatch_script.main(argv=argv) |
25
|
|
|
|
26
|
|
|
self.assertTrue(context.exception.code, 2) |
27
|
|
|
|
28
|
|
|
# Test if the test pass |
29
|
|
|
argv[2] = '0' |
30
|
|
|
|
31
|
|
|
try: |
32
|
|
|
smartdispatch_script.main(argv=argv) |
33
|
|
|
except SystemExit as e: |
34
|
|
|
self.fail("The command failed the check, but it was supposed to pass.") |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
def test_cpu_check(self): |
38
|
|
|
|
39
|
|
|
argv = ['-x', '-c', '2', '-C', '1', '-G', '1', '-t', '00:00:10', '-q', 'random', 'launch', 'echo', 'testing123'] |
40
|
|
|
|
41
|
|
|
# Test if the check fail |
42
|
|
|
with self.assertRaises(SystemExit) as context: |
43
|
|
|
smartdispatch_script.main(argv=argv) |
44
|
|
|
|
45
|
|
|
self.assertTrue(context.exception.code, 2) |
46
|
|
|
|
47
|
|
|
# Test if the test pass |
48
|
|
|
argv[2] = '1' |
49
|
|
|
|
50
|
|
|
try: |
51
|
|
|
smartdispatch_script.main(argv=argv) |
52
|
|
|
except SystemExit as e: |
53
|
|
|
self.fail("The command failed the check, but it was supposed to pass.") |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
|
57
|
|
View Code Duplication |
@patch('subprocess.check_output') |
|
|
|
|
58
|
|
|
def test_launch_job_check(self, mock_check_output): |
59
|
|
|
|
60
|
|
|
mock_check_output.side_effect = subprocess.CalledProcessError(1, 1, "A wild error appeared!") |
61
|
|
|
argv = ['-t', '0:0:1', '-G', '1', '-C', '1', '-q', 'random', 'launch', 'echo', 'testing123'] |
62
|
|
|
|
63
|
|
|
# Test if the test fail. |
64
|
|
|
try: |
65
|
|
|
with self.assertRaises(SystemExit) as context: |
66
|
|
|
smartdispatch_script.main(argv=argv) |
67
|
|
|
|
68
|
|
|
self.assertTrue(context.exception.code, 2) |
69
|
|
|
|
70
|
|
|
except subprocess.CalledProcessError: |
71
|
|
|
self.fail("smartdispatch_script.main() raised CalledProcessError unexpectedly:\n {}".format(traceback.format_exc())) |
72
|
|
|
|
73
|
|
|
# Test if the test pass (i.e the script run normaly) |
74
|
|
|
mock_check_output.side_effect = None |
75
|
|
|
mock_check_output.return_value = "" |
76
|
|
|
|
77
|
|
|
try: |
78
|
|
|
smartdispatch_script.main(argv=argv) |
79
|
|
|
except SystemExit as e: |
80
|
|
|
self.fail("The launcher had no problem, but the script failed nonetheless.") |
81
|
|
|
|