Skip to content

Commit

Permalink
Test validation of module names
Browse files Browse the repository at this point in the history
  • Loading branch information
mimischi committed Feb 28, 2018
1 parent f1d4d6f commit 2d7fe4b
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 3 deletions.
36 changes: 33 additions & 3 deletions mdbenchmark/tests/test_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


@pytest.mark.parametrize('tpr_file', ('protein.tpr', 'protein'))
def test_generate(cli_runner, tmpdir, tpr_file):
def test_generate(cli_runner, monkeypatch, tmpdir, tpr_file):
"""Run an integration test on the generate function.
Make sure that we accept both `protein` and `protein.tpr` as input files.
Expand All @@ -39,6 +39,28 @@ def test_generate(cli_runner, tmpdir, tpr_file):
'Creating a total of 4 benchmarks, with a run time of 15' \
' minutes each.\nFinished generating all benchmarks.\nYou can' \
' now submit the jobs with mdbenchmark submit.\n'

# Test that we get a warning, if no module name validation is performed.
result = cli_runner.invoke(cli.cli, [
'generate', '--module=gromacs/2016', '--host=draco',
'--max-nodes=4', '--gpu', '--name={}'.format(tpr_file)
])
assert result.exit_code == 0
assert result.output == 'WARNING Not performing module name validation.\n' + output

# monkeypatch the output of the available modules
monkeypatch.setattr('mdbenchmark.generate.get_available_modules',
lambda: {'gromacs': ['2016']})

# Test that we can skip module name validation, even if it actually works.
result = cli_runner.invoke(cli.cli, [
'generate', '--module=gromacs/2016', '--host=draco',
'--max-nodes=4', '--gpu', '--name={}'.format(tpr_file),
'--skip-validation'
])
assert result.exit_code == 0
assert result.output == 'WARNING Not performing module name validation.\n' + output

result = cli_runner.invoke(cli.cli, [
'generate', '--module=gromacs/2016', '--host=draco',
'--max-nodes=4', '--gpu', '--name={}'.format(tpr_file)
Expand Down Expand Up @@ -73,9 +95,13 @@ def test_generate(cli_runner, tmpdir, tpr_file):
'draco_gromacs/2016_gpu/{}/bench.job'.format(i))


def test_generate_console_messages(cli_runner, tmpdir):
def test_generate_console_messages(cli_runner, monkeypatch, tmpdir):
"""Test that the CLI for generate prints all error messages as expected."""
with tmpdir.as_cwd():
# monkeypatch the output of the available modules
monkeypatch.setattr('mdbenchmark.generate.get_available_modules',
lambda: {'gromacs': ['2016']})

# Test that we get an error when not supplying a file name
result = cli_runner.invoke(
cli.cli, ['generate', '--module=gromacs/2016', '--host=draco'])
Expand Down Expand Up @@ -125,12 +151,16 @@ def test_generate_console_messages(cli_runner, tmpdir):
assert result.output == output


def test_generate_namd_experimental_warning(cli_runner, tmpdir):
def test_generate_namd_experimental_warning(cli_runner, monkeypatch, tmpdir):
"""Test that we print the NAMD experimental warning."""
with tmpdir.as_cwd():
for f in ['md.namd', 'md.psf', 'md.pdb']:
open(f, 'a').close()

# monkeypatch the output of the available modules
monkeypatch.setattr('mdbenchmark.generate.get_available_modules',
lambda: {'namd': ['123']})

result = cli_runner.invoke(cli.cli, [
'generate', '--module=namd/123', '--host=draco', '--name=md'
])
Expand Down
103 changes: 103 additions & 0 deletions mdbenchmark/tests/test_validate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*-
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 fileencoding=utf-8
#
# MDBenchmark
# Copyright (c) 2017 Max Linke & Michael Gecht and contributors
# (see the file AUTHORS for the full list of names)
#
# MDBenchmark 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.
#
# MDBenchmark 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 MDBenchmark. If not, see <http://www.gnu.org/licenses/>.
import os

import click
import pytest

from mdbenchmark import validate
from mdbenchmark.ext.click_test import cli_runner

DIR_STRUCTURE = {
'applications': {
'gromacs': ['2016.4', '5.1.4-plumed2.3', '2018.1', '.12345-version'],
'namd': ['123', '456'],
'amber': ['13', '14', '15']
},
'visualization': {
'vmd': ['1.9.3', '1.9.4']
}
}

# @pytest.fixture
# def dir_structure(request):
# class File(object):
# def __enter__(self, path, structure):
# self.path = path

# for k, v in dir_structure.items():
# for k2, v2 in v.items():
# os.makedirs(os.path.join(k, k2))
# for v3 in v2:
# open(os.path.join(k, k2, v3), 'a').close()

# def __exit__(self):
# os.remove(self.path)

# obj = File()
# request.addfinalizer(obj.__exit__)
# return obj


def test_validation(monkeypatch, tmpdir, cli_runner):
"""Test that we retrieve the correct module versions from a given path and can
validate module names and versions.
"""

@click.group()
def test_cli():
pass

@test_cli.command()
def test():
validate.validate_module_name('wrong-format')

with tmpdir.as_cwd():
for k, v in DIR_STRUCTURE.items():
for k2, v2 in v.items():
os.makedirs(os.path.join(k, k2))
for v3 in v2:
open(os.path.join(k, k2, v3), 'a').close()

# Make sure we return None when we cannot find the environment variable
assert validate.get_available_modules() == None
# Test the same thing for the `validate_module_name` function
assert validate.validate_module_name('gromacs/123') == None

# Prepare path variable that we are going to monkeypatch for
# `validate.get_available_modules`
dirs = ':'.join(
[os.path.join(os.getcwd(), x) for x in os.listdir(os.getcwd())])
monkeypatch.setenv('MODULEPATH', dirs)
modules = validate.get_available_modules()

# Assert that the correct modules and their versions are returned.
assert set(modules['gromacs']) == set(
['2016.4', '5.1.4-plumed2.3', '2018.1'])
assert set(modules['namd']) == set(['123', '456'])

# Make sure we return a boolean if the module is available or not.
assert not validate.validate_module_name('gromacs/123')
assert validate.validate_module_name('gromacs/2018.1')

output = 'ERROR We were not able to determine the module name.\n'
result = cli_runner.invoke(test_cli, ['test'])
assert result.exit_code == 1
assert result.output == output

0 comments on commit 2d7fe4b

Please sign in to comment.