diff --git a/km3buu/config.py b/km3buu/config.py index ba4906b14d750e9137639b3e964702c0058ba386..8b986dca0ffdd67165cf479e21d1797963ed284d 100644 --- a/km3buu/config.py +++ b/km3buu/config.py @@ -12,7 +12,7 @@ from os.path import isfile, isdir, join, dirname, abspath from configparser import ConfigParser, Error, NoOptionError, NoSectionError from thepipe.logger import get_logger from . import IMAGE_NAME -from .core import build_image +from .environment import build_image __author__ = "Johannes Schumann" __copyright__ = "Copyright 2020, Johannes Schumann and the KM3NeT collaboration." diff --git a/km3buu/ctrl.py b/km3buu/ctrl.py index f37bc69fd2fbc7098e2a8a63b35f6189fb3a6798..d8af86bd1a7e5ff7cca34063d4b55777fb779faa 100644 --- a/km3buu/ctrl.py +++ b/km3buu/ctrl.py @@ -14,8 +14,6 @@ __status__ = "Development" import os from spython.main import Client -from spython.utils import get_singularity_version -from distutils.version import LooseVersion from os.path import join, abspath, basename, isdir, isfile from tempfile import NamedTemporaryFile, TemporaryDirectory from thepipe.logger import get_logger @@ -23,16 +21,14 @@ from thepipe.logger import get_logger from . import IMAGE_NAME from .config import Config from .jobcard import Jobcard +from .environment import is_singularity_version_greater, MIN_SINGULARITY_VERSION log = get_logger(basename(__file__)) -log.setLevel("INFO") -singularity_version = LooseVersion(get_singularity_version().split()[-1]) -if singularity_version < LooseVersion("3.3"): - log.error("Singularity version lower than 3.3 (found: %s)" % - singularity_version.vstring) - raise OSError("Singularity version below 3.3 (found: %s)" % - singularity_version.vstring) +if not is_singularity_version_greater( + MIN_SINGULARITY_VERSION): # pragma: no cover + log.error("Singularity version lower than %" % MIN_SINGULARITY_VERSION) + raise OSError("Singularity version below %s" % MIN_SINGULARITY_VERSION) GIBUU_SHELL = """ #!/bin/bash diff --git a/km3buu/core.py b/km3buu/environment.py similarity index 65% rename from km3buu/core.py rename to km3buu/environment.py index eb9e911307d2f936094e5ce8cffcc067d995b9f7..8a4c3f03f4b9b05b6263086e16d244a657719cdc 100644 --- a/km3buu/core.py +++ b/km3buu/environment.py @@ -1,4 +1,4 @@ -# Filename: core.py +# Filename: environment.py """ Core functions for the package environment @@ -14,13 +14,23 @@ __status__ = "Development" import os from spython.main import Client +from spython.utils import get_singularity_version from os.path import join, isdir, basename from thepipe.logger import get_logger +from distutils.version import LooseVersion from . import IMAGE_NAME, DOCKER_URL log = get_logger(basename(__file__)) +MIN_SINGULARITY_VERSION = "3.3" + + +def is_singularity_version_greater(min_version): # pragma: no cover + singularity_version = LooseVersion(get_singularity_version().split()[-1]) + retval = singularity_version > LooseVersion(MIN_SINGULARITY_VERSION) + return retval + def build_image(output_dir): if not isdir(output_dir): diff --git a/km3buu/tests/test_environment.py b/km3buu/tests/test_environment.py new file mode 100644 index 0000000000000000000000000000000000000000..247690b1e66adda9d5c3c93be01c24a19f5738d8 --- /dev/null +++ b/km3buu/tests/test_environment.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python +# coding=utf-8 +# Filename: test_environment.py + +__author__ = "Johannes Schumann" +__copyright__ = "Copyright 2020, Johannes Schumann and the KM3NeT collaboration." +__credits__ = [] +__license__ = "MIT" +__maintainer__ = "Johannes Schumann" +__email__ = "jschumann@km3net.de" +__status__ = "Development" + +import unittest +from unittest.mock import patch +from km3buu.environment import * +from os.path import dirname, join +from spython.main import Client +from km3buu import DOCKER_URL, IMAGE_NAME + + +class TestBuild(unittest.TestCase): + def test_wrong_dir_path(self): + wrong_path = "foobar" + try: + build_image(wrong_path) + assert False + except OSError as e: + assert str(e) == "Directory not found!" + + @patch.object(Client, 'build', return_value=123) + def test_build_cmd(self, function): + existing_path = dirname(__file__) + assert build_image(existing_path) == 123 + expected_image_path = join(existing_path, IMAGE_NAME) + function.assert_called_once_with(DOCKER_URL, + image=expected_image_path, + sudo=False, + ext="simg") diff --git a/km3buu/tests/test_output.py b/km3buu/tests/test_output.py index e62b063f48df6845a0b3afe74e1e019081b790ca..439ad7e7cab05ac13497feb54dd87604020e7ba6 100644 --- a/km3buu/tests/test_output.py +++ b/km3buu/tests/test_output.py @@ -30,7 +30,6 @@ class TestXSection(unittest.TestCase): self.assertAlmostEqual(xsection['Delta'], 0.61537) - class TestFinalEvents(unittest.TestCase): def setUp(self): self.filename = join(TESTDATA_DIR, EVENT_FILENAME) @@ -55,7 +54,13 @@ class TestFinalEvents(unittest.TestCase): def test_slicing(self): assert self.final_events[0:2] is not None + def test_length(self): + assert len(self.final_events) == 5 + class TestGiBUUOutput(unittest.TestCase): def setUp(self): - pass + self.output = GiBUUOutput(TESTDATA_DIR) + + def test_attr(self): + assert hasattr(self.output, "events")