| 1 |
# ----------------------------------------------------------- |
| 2 |
# Copyright (C) 2009 StatPro Italia s.r.l. |
| 3 |
# |
| 4 |
# StatPro Italia |
| 5 |
# Via G. B. Vico 4 |
| 6 |
# I-20123 Milano |
| 7 |
# ITALY |
| 8 |
# |
| 9 |
# phone: +39 02 96875 1 |
| 10 |
# fax: +39 02 96875 605 |
| 11 |
# |
| 12 |
# email: [email protected] |
| 13 |
# |
| 14 |
# This program is distributed in the hope that it will be |
| 15 |
# useful, but WITHOUT ANY WARRANTY; without even the |
| 16 |
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 17 |
# PURPOSE. See the license for more details. |
| 18 |
# ----------------------------------------------------------- |
| 19 |
|
| 20 |
""" |
| 21 |
test module for the functional interface |
| 22 |
""" |
| 23 |
|
| 24 |
from __future__ import absolute_import, print_function, unicode_literals |
| 25 |
|
| 26 |
import sys |
| 27 |
import unittest |
| 28 |
from os import environ |
| 29 |
|
| 30 |
from nose.tools import eq_ |
| 31 |
|
| 32 |
from drmaa import Session, JobTemplate |
| 33 |
from drmaa.const import (JobControlAction, JobSubmissionState, PLACEHOLDER_HD, |
| 34 |
SUBMISSION_STATE_ACTIVE) |
| 35 |
|
| 36 |
|
| 37 |
# Python 3 compatability help |
| 38 |
if sys.version_info < (3, 0): |
| 39 |
bytes = str |
| 40 |
str = unicode |
| 41 |
|
| 42 |
|
| 43 |
def setup(): |
| 44 |
"initialize DRMAA library" |
| 45 |
Session.initialize() |
| 46 |
|
| 47 |
|
| 48 |
def teardown(): |
| 49 |
"finalize DRMAA session" |
| 50 |
Session.exit() |
| 51 |
|
| 52 |
|
| 53 |
def test_allocate(): |
| 54 |
"job template allocation" |
| 55 |
jt = Session.createJobTemplate() |
| 56 |
Session.deleteJobTemplate(jt) |
| 57 |
|
| 58 |
|
| 59 |
class SubmitBase(unittest.TestCase): |
| 60 |
|
| 61 |
def setUp(self): |
| 62 |
self.jt = jt = Session.createJobTemplate() |
| 63 |
jt.remoteCommand = 'python' |
| 64 |
jt.args = ['-c', "print('hello from python!')"] |
| 65 |
if hasattr(self, 'jt_tweaks'): |
| 66 |
self.jt_tweaks() |
| 67 |
self.jid = Session.runJob(jt) |
| 68 |
|
| 69 |
def tearDown(self): |
| 70 |
Session.deleteJobTemplate(self.jt) |
| 71 |
|
| 72 |
|
| 73 |
class EnvironmentTest(SubmitBase): |
| 74 |
|
| 75 |
def jt_tweaks(self): |
| 76 |
environ['PIPPO'] = 'aaaa' |
| 77 |
self.jt.args = (["-c", |
| 78 |
("from os import environ as env; assert ('PIPPO' in " + |
| 79 |
"env) and (env['PIPPO'] == 'aaaa')")]) |
| 80 |
self.jt.jobEnvironment = environ |
| 81 |
|
| 82 |
def test_environment(self): |
| 83 |
"""environment variables are correctly passed to submitted jobs""" |
| 84 |
jinfo = Session.wait(self.jid) |
| 85 |
eq_(jinfo.jobId, self.jid) |
| 86 |
assert hasattr(jinfo, 'hasExited') |
| 87 |
assert hasattr(jinfo, 'exitStatus') and jinfo.exitStatus == 0 |
| 88 |
|
| 89 |
|
| 90 |
class Submit(SubmitBase): |
| 91 |
|
| 92 |
def test_run_bulk(self): |
| 93 |
"""run bulk job""" |
| 94 |
jids = Session.runBulkJobs(self.jt, 1, 2, 1) |
| 95 |
|
| 96 |
def test_wait(self): |
| 97 |
"""waiting for job completion""" |
| 98 |
jinfo = Session.wait(self.jid) |
| 99 |
eq_(jinfo.jobId, self.jid) |
| 100 |
assert hasattr(jinfo, 'hasExited') |
| 101 |
assert hasattr(jinfo, 'hasExited') and type(jinfo.hasExited) is bool |
| 102 |
assert hasattr(jinfo, 'hasSignal') and type(jinfo.hasSignal) is bool |
| 103 |
assert hasattr(jinfo, 'terminatedSignal') and type(jinfo.terminatedSignal) is str |
| 104 |
assert hasattr(jinfo, 'hasCoreDump') and type(jinfo.hasCoreDump) is bool |
| 105 |
assert hasattr(jinfo, 'wasAborted') and type(jinfo.wasAborted) is bool |
| 106 |
assert hasattr(jinfo, 'exitStatus') and type(jinfo.exitStatus) is int |
| 107 |
assert hasattr(jinfo, 'resourceUsage') and type(jinfo.resourceUsage) is dict |
| 108 |
|
| 109 |
def test_sync(self): |
| 110 |
"""sync with a job""" |
| 111 |
Session.synchronize(self.jid) |
| 112 |
|
| 113 |
def test_control_terminate(self): |
| 114 |
"""control/terminate works""" |
| 115 |
Session.control(self.jid, JobControlAction.TERMINATE) |
| 116 |
Session.synchronize(self.jid, |
| 117 |
Session.TIMEOUT_WAIT_FOREVER, |
| 118 |
False) |
| 119 |
try: |
| 120 |
Session.wait(self.jid, Session.TIMEOUT_WAIT_FOREVER) |
| 121 |
except Exception as e: |
| 122 |
assert e.args[0].startswith('code 24') # no rusage |
| 123 |
|
| 124 |
|
| 125 |
class JobTemplateTests(unittest.TestCase): |
| 126 |
|
| 127 |
def setUp(self): |
| 128 |
self.jt = Session.createJobTemplate() |
| 129 |
|
| 130 |
def test_scalar_attributes(self): |
| 131 |
"""scalar attributes work""" |
| 132 |
for name, value in [("remoteCommand", 'cat'), |
| 133 |
("jobSubmissionState", SUBMISSION_STATE_ACTIVE), |
| 134 |
("workingDirectory", JobTemplate.HOME_DIRECTORY), |
| 135 |
("nativeSpecification", '-shell yes'), |
| 136 |
("blockEmail", False), |
| 137 |
("jobName", 'pippo'), |
| 138 |
("inputPath", ":%s" % PLACEHOLDER_HD), |
| 139 |
("outputPath", ":%s/pippo.out" % PLACEHOLDER_HD), |
| 140 |
("errorPath", ":%s/pippo.out" % PLACEHOLDER_HD), |
| 141 |
("joinFiles", True)]: |
| 142 |
setattr(self.jt, name, value) |
| 143 |
eq_(getattr(self.jt, name), value) |
| 144 |
|
| 145 |
# skipping this. the parameters above have to be tweaked a bit |
| 146 |
def xtest_tmp(self): |
| 147 |
self.test_scalar_attributes() |
| 148 |
self.jt.args = ['.colordb'] |
| 149 |
jid = Session.runJob(self.jt) |
| 150 |
jinfo = Session.wait(jid) |
| 151 |
print(jinfo) |
| 152 |
|
| 153 |
def test_vector_attributes(self): |
| 154 |
"""vector attributes work""" |
| 155 |
args = [10, 'de', 'arglebargle'] |
| 156 |
self.jt.args = args |
| 157 |
eq_(self.jt.args, ['10', 'de', 'arglebargle']) |
| 158 |
em = ['[email protected]', '[email protected]'] |
| 159 |
self.jt.email = em |
| 160 |
eq_(self.jt.email, em) |
| 161 |
|
| 162 |
def test_dict_attribute(self): |
| 163 |
"""dict attributes work""" |
| 164 |
from drmaa.const import ATTR_BUFFER |
| 165 |
self.jt.jobEnvironment = environ |
| 166 |
for x in environ: |
| 167 |
# attribute values could be truncated. For some reason, |
| 168 |
# GE returns the first 1014 chars available (!) |
| 169 |
eq_(environ[x][:ATTR_BUFFER - 10], |
| 170 |
self.jt.jobEnvironment[x][:ATTR_BUFFER - 10]) |
| 171 |
|
| 172 |
def test_attribute_names(self): |
| 173 |
"""attribute names work""" |
| 174 |
assert len(self.jt.attributeNames) > 0 |
| 175 |
|
| 176 |
def test_block_email(self): |
| 177 |
"""blockEmail works""" |
| 178 |
self.jt.blockEmail = True |
| 179 |
assert self.jt.blockEmail |
| 180 |
self.jt.blockEmail = False |
| 181 |
assert not self.jt.blockEmail |
| 182 |
|
| 183 |
def test_join_files(self): |
| 184 |
"""joinFiles works""" |
| 185 |
self.jt.joinFiles = True |
| 186 |
assert self.jt.joinFiles |
| 187 |
self.jt.joinFiles = False |
| 188 |
assert not self.jt.joinFiles |
| 189 |
|
| 190 |
def test_submission_state(self): |
| 191 |
"""submission state attributes work""" |
| 192 |
self.jt.jobSubmissionState = JobSubmissionState.HOLD_STATE |
| 193 |
eq_(self.jt.jobSubmissionState, JobSubmissionState.HOLD_STATE) |
| 194 |
self.jt.jobSubmissionState = JobSubmissionState.ACTIVE_STATE |
| 195 |
eq_(self.jt.jobSubmissionState, JobSubmissionState.ACTIVE_STATE) |
| 196 |
|
| 197 |
def tearDown(self): |
| 198 |
Session.deleteJobTemplate(self.jt) |