Belle II Software  release-08-01-10
test_clustercontroldrmaa.py
1 #!/usr/bin/env python3
2 
3 
10 
11 import unittest
12 import sys
13 import tempfile
14 import os
15 from enum import Enum
16 from unittest.mock import MagicMock
17 from clustercontroldrmaa import Cluster
18 from validationscript import Script
19 
20 
21 class TestClusterControlDrmaa(unittest.TestCase):
22  """
23  Test for for the DRMAA-backend of the clustercontrol
24  """
25 
26  class SessionMock:
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 
53  def createJobTemplate(self):
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.jobStatusReturnjobStatusReturn
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.SessionMockSessionMock
89  ) # MagicMock(return_value=session_mock)
90  drmaa_mock.JobState = self.SessionMockSessionMock.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.SessionMockSessionMock.jobStatusReturn = self.SessionMockSessionMock.JobState.DONE
112 
113  self.assertTrue(cc.is_job_finished(job)[0])
114 
115  cc.terminate(job)
116 
117 
118 if __name__ == "__main__":
119  unittest.main()