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