|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | import unittest |
3 | | - |
4 | | -from smartdispatch import utils |
5 | | - |
| 3 | +try: |
| 4 | + from mock import patch |
| 5 | + import mock |
| 6 | +except ImportError: |
| 7 | + from unittest.mock import patch |
| 8 | + import unittest.mock |
6 | 9 | from nose.tools import assert_equal, assert_true |
7 | 10 | from numpy.testing import assert_array_equal |
| 11 | +import subprocess |
8 | 12 |
|
| 13 | +from smartdispatch import utils |
9 | 14 |
|
10 | 15 | class PrintBoxedTests(unittest.TestCase): |
11 | 16 |
|
@@ -49,3 +54,40 @@ def test_slugify(): |
49 | 54 |
|
50 | 55 | for arg, expected in testing_arguments: |
51 | 56 | assert_equal(utils.slugify(arg), expected) |
| 57 | + |
| 58 | +command_output = """\ |
| 59 | +Server Max Tot Que Run Hld Wat Trn Ext Com Status |
| 60 | +---------------- --- --- --- --- --- --- --- --- --- ---------- |
| 61 | +gpu-srv1.{} 0 1674 524 121 47 0 0 22 960 Idle |
| 62 | +""" |
| 63 | + |
| 64 | +slurm_command = """\ |
| 65 | + Cluster ControlHost ControlPort RPC Share GrpJobs GrpTRES GrpSubmit MaxJobs MaxTRES MaxSubmit MaxWall QOS Def QOS |
| 66 | +---------- --------------- ------------ ----- --------- ------- ------------- --------- ------- ------------- --------- ----------- -------------------- --------- |
| 67 | + {} 132.204.24.224 6817 7680 1 normal |
| 68 | +""" |
| 69 | + |
| 70 | + |
| 71 | +class ClusterIdentificationTest(unittest.TestCase): |
| 72 | + |
| 73 | + 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) |
0 commit comments