Completed
Pull Request — master (#145)
by
unknown
54s
created

TestPBS.test_str()   A

Complexity

Conditions 1

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
dl 0
loc 48
rs 9.125
c 1
b 1
f 0
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
# Prolog #
82
83
# Commands #
84
85
# Epilog #"""
86
        pbs = PBS(queue_name="qtest@mp2", walltime="01:00:00")
87
        assert_equal(str(pbs), expected)
88
89
        # Create a more complex PBS file
90
        commands = ["cd $HOME; echo 1 2 3 1>> cmd1.o 2>> cmd1.e",
91
                    "cd $HOME; echo 3 2 1 1>> cmd1.o 2>> cmd1.e"]
92
        modules = ["CUDA_Toolkit/6.0", "python2.7"]
93
94
        expected = """#!/bin/bash
95
#PBS -q qtest@mp2
96
#PBS -V
97
#PBS -A xyz-123-ab
98
#PBS -l walltime=01:00:00
99
#PBS -l nodes=2:ppn=3:gpus=1
100
101
# Modules #
102
module load CUDA_Toolkit/6.0
103
module load python2.7
104
105
# Prolog #
106
107
# Commands #
108
{command1}
109
{command2}
110
111
# Epilog #""".format(command1=commands[0], command2=commands[1])
112
113
        pbs = PBS(queue_name="qtest@mp2", walltime="01:00:00")
114
        pbs.add_resources(nodes="2:ppn=3:gpus=1")
115
        pbs.add_options(A="xyz-123-ab")
116
        pbs.add_modules_to_load(*modules)
117
        pbs.add_commands(*commands)
118
119
        assert_equal(str(pbs), expected)
120
121
    def test_save(self):
122
        pbs_filename = os.path.join(self.testing_dir, "pbs.sh")
123
        self.pbs.save(pbs_filename)
124
        assert_equal(str(self.pbs), open(pbs_filename).read())
125