Skip to content

Commit

Permalink
adds PopperTest class and popper versoin test (#803)
Browse files Browse the repository at this point in the history
Adds a PopperTest class that other unit tests can derive from in order
to make use of generic testing functionality. Currently this contains a
`mk_repo()` function that takes care of initializing a git repo with a
`README` file in it.

In addition, a test for the `popper version` command has been added
that makes use of Click's unit testing features that allow to test CLI
functionality. This will eventually replace the pipeline in the `ci/` folder.
  • Loading branch information
vipulchhabra99 committed Apr 18, 2020
1 parent 61c514c commit d91d11d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 33 deletions.
17 changes: 17 additions & 0 deletions cli/test/test_cmdversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from click.testing import CliRunner
import popper.commands.cmd_version as version
import unittest
from test_common import PopperTest
from popper import __version__ as version_value


class TestCommandVersion(PopperTest):

def test_version(self):

with self.assertLogs('popper') as test:
runner = CliRunner()
result = runner.invoke(version.cli)
self.assertEqual(result.exit_code, 0)

self.assertTrue(version_value in test.output[0])
41 changes: 41 additions & 0 deletions cli/test/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os
import sys
import shutil
import contextlib
import subprocess
import tempfile
import git
import unittest

FIXDIR = f'{os.path.dirname(os.path.realpath(__file__))}/fixtures'


class PopperTest(unittest.TestCase):

def mk_repo(self):
"""creates a test repo in a random temp file. Equivalent to:
REPODIR=/tmp/<random>
mkdir $REPODIR
cd $REPODIR
git init
touch README.md
git add .
git commit -m 'first commit'
"""
tempdir = tempfile.mkdtemp()
repo = git.Repo.init(tempdir)
readme = os.path.join(tempdir, 'README.md')
open(readme, 'w').close()
repo.index.add([readme])
repo.index.commit('first commit')
return repo


class PopperCommonTest(PopperTest):

def test_mkrepo(self):

repo = self.mk_repo()
self.assertTrue(os.path.isdir(repo.working_tree_dir))
self.assertTrue(os.path.isfile(
os.path.join(repo.working_tree_dir, 'README.md')))
2 changes: 1 addition & 1 deletion cli/test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

from popper.config import PopperConfig
from popper.cli import log
from test_common import PopperTest


FIXDIR = f'{os.path.dirname(os.path.realpath(__file__))}/fixtures'


class TestPopperConfig(unittest.TestCase):
default_args = {
'skip_clone': False,
Expand Down
15 changes: 10 additions & 5 deletions cli/test/test_runner_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import docker

import utils as testutils
import popper.utils as pu

from testfixtures import LogCapture
Expand All @@ -15,15 +14,20 @@
from popper.parser import YMLWorkflow
from popper.runner import WorkflowRunner
from popper.runner_host import HostRunner, DockerRunner


from test_common import PopperTest

from popper.cli import log as log


class TestHostHostRunner(unittest.TestCase):
class TestHostHostRunner(PopperTest):
def setUp(self):
log.setLevel('CRITICAL')

def test_run(self):
repo = testutils.mk_repo()

repo = self.mk_repo()
conf = PopperConfig(workspace_dir=repo.working_dir)

with WorkflowRunner(conf) as r:
Expand Down Expand Up @@ -97,7 +101,7 @@ def test_stop_running_tasks(self):
self.assertRaises(ProcessLookupError, os.kill, pid, 0)


class TestHostDockerRunner(unittest.TestCase):
class TestHostDockerRunner(PopperTest):
def setUp(self):
log.setLevel('CRITICAL')

Expand Down Expand Up @@ -212,7 +216,8 @@ def test_get_build_info(self):

@unittest.skipIf(os.environ['ENGINE'] != 'docker', 'ENGINE != docker')
def test_docker_basic_run(self):
repo = testutils.mk_repo()

repo = self.mk_repo()
conf = PopperConfig(workspace_dir=repo.working_dir)

with WorkflowRunner(conf) as r:
Expand Down
6 changes: 3 additions & 3 deletions cli/test/test_runner_slurm.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import os
import unittest
import tempfile
import utils as testutils

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

from test_common import PopperTest
from testfixtures import Replacer, replace, compare
from testfixtures.popen import MockPopen
from testfixtures.mock import call
Expand All @@ -18,7 +18,7 @@ def mock_kill(pid, sig):
return 0


class TestSlurmSlurmRunner(unittest.TestCase):
class TestSlurmSlurmRunner(PopperTest):
def setUp(self):
log.setLevel('CRITICAL')
self.Popen = MockPopen()
Expand Down Expand Up @@ -152,7 +152,7 @@ def test_submit_job_failure(self, mock_kill):
self.assertEqual(call_sbatch in self.Popen.all_calls, True)

def test_dry_run(self):
repo = testutils.mk_repo()
repo = self.mk_repo()
config = PopperConfig(
engine_name='docker',
resman_name='slurm',
Expand Down
24 changes: 0 additions & 24 deletions cli/test/utils.py

This file was deleted.

0 comments on commit d91d11d

Please sign in to comment.