Completed
Push — master ( 0b9edb...ab2b1a )
by Mathieu
10s
created

smartdispatch.tests.TestPBS   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 109
Duplicated Lines 0 %
Metric Value
dl 0
loc 109
rs 10
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A TestPBS.test_add_commands() 0 9 1
A TestPBS.setUp() 0 5 1
A TestPBS.test_add_resources() 0 10 1
A TestPBS.test_add_options() 0 11 1
A TestPBS.test_add_modules_to_load() 0 9 1
A TestPBS.test_constructor() 0 5 1
A TestPBS.tearDown() 0 2 1
A TestPBS.test_save() 0 4 1
B TestPBS.test_str() 0 44 1
1
from nose.tools import assert_true, assert_equal, assert_raises
2
from numpy.testing import assert_array_equal
3
4
5
from smartdispatch.pbs import PBS
6
import unittest
7
import tempfile
8
import shutil
9
import os
10
11
12
class TestPBS(unittest.TestCase):
13
14
    def setUp(self):
15
        self.testing_dir = tempfile.mkdtemp()
16
        self.queue_name = "qtest@cluster"
17
        self.walltime = "10:42"
18
        self.pbs = PBS(self.queue_name, self.walltime)
19
20
    def tearDown(self):
21
        shutil.rmtree(self.testing_dir)
22
23
    def test_constructor(self):
24
        assert_raises(ValueError, PBS, self.queue_name, None)
25
        assert_raises(ValueError, PBS, None, self.walltime)
26
        assert_raises(ValueError, PBS, "", self.walltime)
27
        assert_raises(ValueError, PBS, self.queue_name, "Wrong walltime format")
28
29
    def test_add_options(self):
30
        # Default options
31
        assert_equal(len(self.pbs.options), 2)
32
        assert_true('-V' in self.pbs.options.keys())
33
        assert_equal(self.pbs.options['-q'], self.queue_name)
34
35
        self.pbs.add_options(A="option1")
36
        assert_equal(self.pbs.options["-A"], "option1")
37
        self.pbs.add_options(A="option2", B="option3")
38
        assert_equal(self.pbs.options["-A"], "option2")
39
        assert_equal(self.pbs.options["-B"], "option3")
40
41
    def test_add_resources(self):
42
        assert_equal(len(self.pbs.resources), 1)
43
        assert_equal(self.pbs.resources["walltime"], self.walltime)
44
45
        self.pbs.add_resources(nodes="23:ppn=2")
46
        assert_equal(self.pbs.resources["nodes"], "23:ppn=2")
47
48
        self.pbs.add_resources(pmem=123, walltime="42:42")
49
        assert_equal(self.pbs.resources["pmem"], 123)
50
        assert_equal(self.pbs.resources["walltime"], "42:42")
51
52
    def test_add_modules_to_load(self):
53
        assert_equal(self.pbs.modules, [])
54
55
        modules = ["cuda", "gcc", "python/2.7"]
56
        self.pbs.add_modules_to_load(modules[0], modules[1])
57
        assert_array_equal(self.pbs.modules, modules[:2])
58
59
        self.pbs.add_modules_to_load(modules[-1])
60
        assert_array_equal(self.pbs.modules, modules)
61
62
    def test_add_commands(self):
63
        assert_equal(self.pbs.commands, [])
64
65
        commands = ["python my_script.py", "echo 1234", "git push origin master"]
66
        self.pbs.add_commands(commands[0])
67
        assert_array_equal(self.pbs.commands, commands[0])
68
69
        self.pbs.add_commands(*commands[1:])
70
        assert_array_equal(self.pbs.commands, commands)
71
72
    def test_str(self):
73
        # Create simple PBS file
74
        expected = """#!/bin/bash
75
#PBS -q qtest@mp2
76
#PBS -V
77
#PBS -l walltime=01:00:00
78
79
# Modules #
80
81
# Commands #
82
83
wait"""
84
        pbs = PBS(queue_name="qtest@mp2", walltime="01:00:00")
85
        assert_equal(str(pbs), expected)
86
87
        # Create a more complex PBS file
88
        commands = ["cd $HOME; echo 1 2 3 1>> cmd1.o 2>> cmd1.e",
89
                    "cd $HOME; echo 3 2 1 1>> cmd1.o 2>> cmd1.e"]
90
        modules = ["CUDA_Toolkit/6.0", "python2.7"]
91
92
        expected = """#!/bin/bash
93
#PBS -q qtest@mp2
94
#PBS -V
95
#PBS -A xyz-123-ab
96
#PBS -l walltime=01:00:00
97
#PBS -l nodes=2:ppn=3:gpus=1
98
99
# Modules #
100
module load CUDA_Toolkit/6.0
101
module load python2.7
102
103
# Commands #
104
{command1} &
105
{command2} &
106
107
wait""".format(command1=commands[0], command2=commands[1])
108
109
        pbs = PBS(queue_name="qtest@mp2", walltime="01:00:00")
110
        pbs.add_resources(nodes="2:ppn=3:gpus=1")
111
        pbs.add_options(A="xyz-123-ab")
112
        pbs.add_modules_to_load(*modules)
113
        pbs.add_commands(*commands)
114
115
        assert_equal(str(pbs), expected)
116
117
    def test_save(self):
118
        pbs_filename = os.path.join(self.testing_dir, "pbs.sh")
119
        self.pbs.save(pbs_filename)
120
        assert_equal(str(self.pbs), open(pbs_filename).read())
121