Skip to content

Commit

Permalink
Add slurm runner unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JayjeetAtGithub committed Mar 22, 2020
1 parent a22b29e commit a8b9094
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
4 changes: 1 addition & 3 deletions cli/popper/runner_slurm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ def __exit__(self, exc_type, exc, traceback):
SlurmRunner.spawned_processes = set()

def exec_srun_cmd(self, cmd):
step_env = SlurmRunner.prepare_environment(self.step, os.environ)
try:
cmd.insert(0, 'srun')
with Popen(cmd, stdout=PIPE, stderr=STDOUT,
universal_newlines=True, preexec_fn=os.setsid,
env=step_env, cwd=self.config.workspace_dir) as p:
cwd=self.config.workspace_dir) as p:
SlurmRunner.spawned_processes.add(p)

log.debug('Reading process output')
Expand Down Expand Up @@ -65,7 +64,6 @@ def __init__(self, config):

def run(self, step):
"""Execute the given step in docker."""
self.step = step
cid = pu.sanitized_name(step['name'], self.config.wid)

build, img, dockerfile = HostDockerRunner.get_build_info(
Expand Down
1 change: 1 addition & 0 deletions cli/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
include_package_data=True,
install_requires=[
'dotmap',
'testfixtures',
'python-vagrant',
'GitPython',
'spython',
Expand Down
50 changes: 50 additions & 0 deletions cli/test/test_runner_slurm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os
import unittest

import utils as testutils

from popper.parser import YMLWorkflow
from popper.runner import WorkflowRunner
from popper.runner_slurm import SlurmRunner, DockerRunner
from popper.cli import log as log

from dotmap import DotMap

from testfixtures.mock import call
from testfixtures import Replacer, ShouldRaise, compare
from testfixtures.popen import MockPopen, PopenBehaviour


class TestSlurmSlurmRunner(unittest.TestCase):
def setUp(self):
log.setLevel('CRITICAL')
self.Popen = MockPopen()
replacer = Replacer()
replacer.replace('popper.runner_slurm.Popen', self.Popen)
self.addCleanup(replacer.restore)

def test_exec_run(self):
self.Popen.set_command('srun ls -l', returncode=0)
config = DotMap()
config.workspace_dir = os.environ["HOME"]
slurm_runner = SlurmRunner(config)
self.assertEqual(slurm_runner.exec_srun_cmd(['ls', '-l']), 0)
self.assertEqual(len(SlurmRunner.spawned_processes), 0)


class TestSlurmDockerRunner(unittest.TestCase):
def setUp(self):
log.setLevel('CRITICAL')
self.Popen = MockPopen()
replacer = Replacer()
replacer.replace('popper.runner_slurm.Popen', self.Popen)
self.addCleanup(replacer.restore)

def test_start_container(self):
self.Popen.set_command('srun docker start --attach', returncode=0)
config = DotMap()
config.workspace_dir = os.environ["HOME"]
docker_runner = DockerRunner(config)
self.assertEqual(docker_runner.exec_srun_cmd(
['docker', 'start', '--attach']), 0)
self.assertEqual(len(SlurmRunner.spawned_processes), 0)
4 changes: 3 additions & 1 deletion cli/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ def test_load_config_file(self):
pu.write_file('settings.yml', conf_content)
config = pu.load_config_file('settings.yml')
self.assertTrue(config.get('engine'), True)
self.assertDictEqual(config['engine']['options'], {'runtime': 'nvidia'})
self.assertDictEqual(
config['engine']['options'], {
'runtime': 'nvidia'})
os.remove('settings.yml')

def test_assert_executable_exists(self):
Expand Down

0 comments on commit a8b9094

Please sign in to comment.