From eb6db65f01cf4703bad571f00a5f174e9766e92d Mon Sep 17 00:00:00 2001 From: Stefan Verhoeven Date: Wed, 3 Apr 2019 22:14:20 +0200 Subject: [PATCH] Fix error in staging of config file --- cpp/bmi-c | 2 +- grpc4bmi/utils.py | 4 ++-- test/test_utils.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 test/test_utils.py diff --git a/cpp/bmi-c b/cpp/bmi-c index 48d3bdb..8892f9c 160000 --- a/cpp/bmi-c +++ b/cpp/bmi-c @@ -1 +1 @@ -Subproject commit 48d3bdb63fbd51a43476c09d6ec3d7da15b62206 +Subproject commit 8892f9cd5c1da9f3bb571ea74d78466b8fba1a08 diff --git a/grpc4bmi/utils.py b/grpc4bmi/utils.py index cadc9bd..85f4d62 100644 --- a/grpc4bmi/utils.py +++ b/grpc4bmi/utils.py @@ -14,8 +14,8 @@ def stage_config_file(filename, input_dir, input_mount_point, home_mounted=False home_mounted (bool): True if home directory is mounted inside container """ fn = filename - is_filename_inside_input_dir = input_dir and abspath(input_dir) < abspath(filename) - is_filename_inside_home_dir = home_mounted and expanduser('~') < abspath(filename) + is_filename_inside_input_dir = input_dir and abspath(filename).startswith(abspath(input_dir)) + is_filename_inside_home_dir = home_mounted and abspath(filename).startswith(expanduser('~')) filename_exists = exists(filename) if is_filename_inside_input_dir: # Replace input dir outside container by input dir inside container diff --git a/test/test_utils.py b/test/test_utils.py new file mode 100644 index 0000000..6053c29 --- /dev/null +++ b/test/test_utils.py @@ -0,0 +1,44 @@ +import pytest + +from grpc4bmi.utils import stage_config_file + + +class TestStageConfigFile: + def test_inside_inputdir(self, tmpdir): + input_dir = tmpdir.mkdir('input') + c = input_dir.join('config.cfg') + c.write("something") + + result = stage_config_file(c, input_dir, input_mount_point='/data/input') + assert result == '/data/input/config.cfg' + + def test_outside_inputdir(self, tmpdir): + input_dir = tmpdir.mkdir('input') + c = tmpdir.join('config.cfg') + c.write("something") + + result = stage_config_file(c, input_dir, input_mount_point='/data/input') + assert result == '/data/input/config.cfg' + + def test_outside_inputdir_beforeconfig(self, tmpdir): + input_dir = tmpdir.mkdir('ainput') + c = tmpdir.join('config.cfg') + c.write("something") + + result = stage_config_file(c, input_dir, input_mount_point='/data/input') + assert result == '/data/input/config.cfg' + + def test_cfg_in_container(self, tmpdir): + input_dir = tmpdir.mkdir('input') + + c = '/somewhere/config.cfg' + result = stage_config_file(c, input_dir, input_mount_point='/data/input') + assert result == c + + def test_noinputdir(self, tmpdir): + c = tmpdir.join('config.cfg') + c.write("something") + + with pytest.raises(Exception) as exc_info: + stage_config_file(c, None, None) + assert str(exc_info.value).startswith('Unable to copy')