-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
284 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env python | ||
|
||
"""This module is a mock of schedd.py, purely for testing | ||
""" | ||
|
||
|
||
class ClassAd(object): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/env python | ||
|
||
"""This module is a mock of htcondor.py, purely for testing | ||
""" | ||
|
||
|
||
class Schedd(object): | ||
"""Mock of the `htcondor.Schedd` object | ||
""" | ||
|
||
_jobs = [] | ||
|
||
def query(self, constraints, attr_list=[], **kwargs): | ||
x = {} | ||
for con in constraints.split(' && '): | ||
try: | ||
a, b = con.split(' == ') | ||
except ValueError: | ||
break | ||
else: | ||
x[a] = eval(b) | ||
match = [] | ||
def match(job): | ||
print(job, x) | ||
for key in x: | ||
if x[key] != job[key]: | ||
return False | ||
return True | ||
return iter(filter(match, self._jobs)) | ||
|
||
def history(self, *args, **kwargs): | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (C) Duncan Macleod (2017) | ||
# | ||
# This file is part of PyOmicron. | ||
# | ||
# PyOmicron is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# PyOmicron is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with PyOmicron. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Tests for omicron.io | ||
""" | ||
|
||
# mock up condor modules for testing purposes | ||
# (so we don't need to have htcondor installed) | ||
import sys | ||
import mock_htcondor | ||
import mock_classad | ||
sys.modules['htcondor'] = mock_htcondor | ||
sys.modules['classad'] = mock_classad | ||
|
||
import utils | ||
from compat import (unittest, mock) | ||
|
||
from omicron import condor | ||
|
||
|
||
# -- mock utilities ----------------------------------------------------------- | ||
|
||
def mock_which(exe): | ||
return '/path/to/executable' | ||
|
||
|
||
def mock_shell_factory(output): | ||
def shell(*args, **kwargs): | ||
return output | ||
return shell | ||
|
||
|
||
def mock_schedd_factory(jobs): | ||
Schedd = mock_htcondor.Schedd | ||
Schedd._jobs = jobs | ||
return Schedd | ||
|
||
|
||
# -- tests -------------------------------------------------------------------- | ||
|
||
class CondorTests(unittest.TestCase): | ||
|
||
def test_submit_dag(self): | ||
shell_ = mock_shell_factory('1 job(s) submitted to cluster 12345') | ||
with mock.patch('omicron.condor.which', mock_which): | ||
with mock.patch('omicron.condor.shell', shell_): | ||
dagid = condor.submit_dag('test.dag') | ||
with utils.capture(condor.submit_dag, 'test.dag', '-append', | ||
'+OmicronDAGMan="GW"') as output: | ||
cmd = output.split('\n')[0] | ||
self.assertEqual(cmd, '$ /path/to/executable -append ' | ||
'+OmicronDAGMan="GW" test.dag') | ||
self.assertEqual(dagid, 12345) | ||
shell_ = mock_shell_factory('Error') | ||
with mock.patch('omicron.condor.which', mock_which): | ||
with mock.patch('omicron.condor.shell', shell_): | ||
self.assertRaises(AttributeError, condor.submit_dag, 'test.dag') | ||
|
||
def test_find_jobs(self): | ||
schedd_ = mock_schedd_factory([{'ClusterId': 1}, {'ClusterId': 2}]) | ||
with mock.patch('htcondor.Schedd', schedd_): | ||
jobs = condor.find_jobs() | ||
self.assertListEqual(jobs, [{'ClusterId': 1}, {'ClusterId': 2}]) | ||
jobs = condor.find_jobs(ClusterId=1) | ||
self.assertListEqual(jobs, [{'ClusterId': 1}]) | ||
jobs = condor.find_jobs(ClusterId=3) | ||
self.assertListEqual(jobs, []) | ||
|
||
def test_find_job(self): | ||
schedd_ = mock_schedd_factory([{'ClusterId': 1}, {'ClusterId': 2}]) | ||
with mock.patch('htcondor.Schedd', schedd_): | ||
job = condor.find_job(ClusterId=1) | ||
self.assertDictEqual(job, {'ClusterId': 1}) | ||
# check 0 jobs returned throws the right error | ||
with self.assertRaises(RuntimeError) as e: | ||
condor.find_job(ClusterId=3) | ||
self.assertTrue(str(e.exception).startswith('No jobs found')) | ||
# check multiple jobs returned throws the right error | ||
with self.assertRaises(RuntimeError) as e: | ||
condor.find_job() | ||
self.assertTrue(str(e.exception).startswith('Multiple jobs found')) | ||
|
||
def test_get_job_status(self): | ||
schedd_ = mock_schedd_factory([{'ClusterId': 1, 'JobStatus': 4}]) | ||
with mock.patch('htcondor.Schedd', schedd_): | ||
status = condor.get_job_status(1) | ||
self.assertEqual(status, 4) | ||
|
||
def test_dag_is_running(self): | ||
self.assertFalse(condor.dag_is_running('test.dag')) | ||
with mock.patch('os.path.isfile') as isfile: | ||
isfile.return_value = True | ||
self.assertTrue(condor.dag_is_running('test.dag')) | ||
schedd_ = mock_schedd_factory( | ||
[{'UserLog': 'test.dag.dagman.log'}]) | ||
with mock.patch('htcondor.Schedd', schedd_): | ||
self.assertTrue(condor.dag_is_running('test.dag')) | ||
self.assertFalse(condor.dag_is_running('test2.dag')) |
Oops, something went wrong.