Skip to content

Commit b2c17a3

Browse files
committed
Refactor detect_cluster tests
1 parent a20405b commit b2c17a3

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

smartdispatch/tests/test_utils.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def test_slugify():
5555
for arg, expected in testing_arguments:
5656
assert_equal(utils.slugify(arg), expected)
5757

58+
5859
command_output = """\
5960
Server Max Tot Que Run Hld Wat Trn Ext Com Status
6061
---------------- --- --- --- --- --- --- --- --- --- ----------
@@ -69,25 +70,28 @@ def test_slugify():
6970

7071

7172
class ClusterIdentificationTest(unittest.TestCase):
73+
server_names = ["hades", "m", "guil", "helios", "hades"]
74+
clusters = ["hades", "mammouth", "guillimin", "helios"]
75+
command_output = command_output
76+
77+
def __init__(self, *args, **kwargs):
78+
super(ClusterIdentificationTest, self).__init__(*args, **kwargs)
79+
self.detect_cluster = utils.detect_cluster
7280

7381
def test_detect_cluster(self):
74-
server_name = ["hades", "m", "guil", "helios", "hades"]
75-
clusters = ["hades", "mammouth", "guillimin", "helios"]
76-
77-
for name, cluster in zip(server_name, clusters):
78-
with patch('smartdispatch.utils.Popen') as mock_communicate:
79-
mock_communicate.return_value.communicate.return_value = (command_output.format(name),)
80-
self.assertEquals(utils.detect_cluster(), cluster)
81-
82-
# def test_detect_mila_cluster(self):
83-
# with patch('smartdispatch.utils.Popen') as mock_communicate:
84-
# mock_communicate.return_value.communicate.side_effect = OSError
85-
# self.assertIsNone(utils.detect_cluster())
86-
87-
def test_get_slurm_cluster_name(self):
88-
clusters = ["graham", "cedar", "mila"]
89-
90-
for cluster in clusters:
91-
with patch('smartdispatch.utils.Popen') as mock_communicate:
92-
mock_communicate.return_value.communicate.return_value = (slurm_command.format(cluster),)
93-
self.assertEquals(utils.get_slurm_cluster_name(), cluster)
82+
83+
with patch('smartdispatch.utils.Popen') as MockPopen:
84+
mock_process = MockPopen.return_value
85+
for name, cluster in zip(self.server_names, self.clusters):
86+
mock_process.communicate.return_value = (
87+
self.command_output.format(name),)
88+
self.assertEquals(self.detect_cluster(), cluster)
89+
90+
91+
class SlurmClusterIdentificationTest(ClusterIdentificationTest):
92+
server_names = clusters = ["graham", "cedar", "mila"]
93+
command_output = slurm_command
94+
95+
def __init__(self, *args, **kwargs):
96+
super(SlurmClusterIdentificationTest, self).__init__(*args, **kwargs)
97+
self.detect_cluster = utils.get_slurm_cluster_name

0 commit comments

Comments
 (0)