Belle II Software development
test_clustercontroldrmaa.py
1#!/usr/bin/env python3
2
3
10
11import unittest
12import sys
13import tempfile
14import os
15from enum import Enum
16from unittest.mock import MagicMock
17from clustercontroldrmaa import Cluster
18from validationscript import Script
19
20
21class TestClusterControlDrmaa(unittest.TestCase):
22 """
23 Test for for the DRMAA-backend of the clustercontrol
24 """
25
27 """
28 Class to mock a DRMAA session
29 """
30
31 class JobState(Enum):
32 """
33 Possible DRMAA Job States
34 """
35
36
37 DONE = 1
38
39 FAILED = 2
40
41 RUNNING = 3
42
43
44 jobStatusReturn = JobState.RUNNING
45
46 def __enter__(self):
47 """ to support python with syntax"""
48 return self
49
50 def __exit__(self, exc, value, tb):
51 """ to support python with syntax"""
52
54 """fake creating job template"""
55 return MagicMock()
56
57 def runJob(self, jt):
58 """fake job running"""
59 return MagicMock()
60
61 def jobStatus(self, jt):
62 """fake job status terun"""
63 return self.jobStatusReturn
64
65 def deleteJobTemplate(self, jt):
66 """fake job deletion"""
67 return MagicMock()
68
69 def test_no_drmaa(self):
70 """
71 Overwrite drmaa module and see what happens ...
72 """
73 sys.modules["drmaa"] = None
74 self.assertFalse(Cluster.is_supported())
75
77 """
78 Test to submit and monitor a regular job
79 """
80
81 with tempfile.TemporaryDirectory() as td:
82 # switch to temp folder as the cluster controller will create
83 # some helper files
84 os.chdir(str(td))
85
86 drmaa_mock = MagicMock()
87 drmaa_mock.Session = (
88 self.SessionMock
89 ) # MagicMock(return_value=session_mock)
90 drmaa_mock.JobState = self.SessionMock.JobState
91
92 sys.modules["drmaa"] = drmaa_mock
93
94 # this just imports the drmaa module which should always work with our
95 # mock
96 self.assertTrue(Cluster.is_supported())
97
98 job = Script(path="myscript1.py", package="test_package", log=None)
99
100 cc = Cluster()
101 cc.execute(job)
102
103 # check if job id has been set
104 self.assertTrue(job.job_id)
105
106 # check on job, not finished yet
107 self.assertFalse(cc.is_job_finished(job)[0])
108
109 # check on job, finished !
110 # change behaviour of jobStatus
111 self.SessionMock.jobStatusReturn = self.SessionMock.JobState.DONE
112
113 self.assertTrue(cc.is_job_finished(job)[0])
114
115 cc.terminate(job)
116
117
118if __name__ == "__main__":
119 unittest.main()
JobState jobStatusReturn
the job state this session mock will return