This is an automated email from the ASF dual-hosted git repository. tvb pushed a commit to branch aevri/enable_spawn_ci_7 in repository https://gitbox.apache.org/repos/asf/buildstream.git
commit 8becbcf111a0a97f78645ad94ac0ffc9edd9e582 Author: Angelos Evripiotis <[email protected]> AuthorDate: Tue Oct 29 17:14:39 2019 +0000 tests: refactor, extract symlink_host_tools_to_dir Extract a helper function to simplify the repeated pattern of creating a temporary dir of symlinks for isolating PATH to. Use it in more places. --- tests/integration/cachedfail.py | 11 +++++----- tests/sandboxes/missing_dependencies.py | 22 +++++++------------ tests/sandboxes/selection.py | 11 +++++----- tests/testutils/__init__.py | 1 + tests/testutils/host_tools.py | 39 +++++++++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 25 deletions(-) diff --git a/tests/integration/cachedfail.py b/tests/integration/cachedfail.py index 63ad2d4..f624c48 100644 --- a/tests/integration/cachedfail.py +++ b/tests/integration/cachedfail.py @@ -20,12 +20,12 @@ import os import pytest -from buildstream import utils, _yaml +from buildstream import _yaml from buildstream._exceptions import ErrorDomain from buildstream.testing import cli_integration as cli # pylint: disable=unused-import from buildstream.testing._utils.site import HAVE_SANDBOX -from tests.testutils import create_artifact_share +from tests.testutils import create_artifact_share, symlink_host_tools_to_dir pytestmark = pytest.mark.integration @@ -229,9 +229,8 @@ def test_push_failed_missing_shell(cli, tmpdir, datafiles, on_error): @pytest.mark.datafiles(DATA_DIR) def test_host_tools_errors_are_not_cached(cli, datafiles, tmp_path): # Create symlink to buildbox-casd to work with custom PATH - buildbox_casd = tmp_path.joinpath('bin/buildbox-casd') - buildbox_casd.parent.mkdir() - os.symlink(utils.get_host_tool('buildbox-casd'), str(buildbox_casd)) + bin_dir = str(tmp_path / 'bin') + symlink_host_tools_to_dir(['buildbox-casd'], bin_dir) project = str(datafiles) element_path = os.path.join(project, 'elements', 'element.bst') @@ -257,7 +256,7 @@ def test_host_tools_errors_are_not_cached(cli, datafiles, tmp_path): result1 = cli.run( project=project, args=['build', 'element.bst'], - env={'PATH': str(tmp_path.joinpath('bin')), + env={'PATH': bin_dir, 'BST_FORCE_SANDBOX': None}) result1.assert_task_error(ErrorDomain.SANDBOX, 'unavailable-local-sandbox') assert cli.get_element_state(project, 'element.bst') == 'buildable' diff --git a/tests/sandboxes/missing_dependencies.py b/tests/sandboxes/missing_dependencies.py index a5bf31e..8935dd8 100644 --- a/tests/sandboxes/missing_dependencies.py +++ b/tests/sandboxes/missing_dependencies.py @@ -5,11 +5,12 @@ import os import pytest -from buildstream import utils, _yaml +from buildstream import _yaml from buildstream._exceptions import ErrorDomain from buildstream.testing._utils.site import IS_LINUX from buildstream.testing import cli # pylint: disable=unused-import +from tests.testutils import symlink_host_tools_to_dir # Project directory DATA_DIR = os.path.join( @@ -18,19 +19,12 @@ DATA_DIR = os.path.join( ) -def _symlink_host_tools_to_dir(host_tools, dir_): - dir_.mkdir(exist_ok=True) - for tool in host_tools: - target_path = dir_ / tool - os.symlink(utils.get_host_tool(tool), str(target_path)) - - @pytest.mark.skipif(not IS_LINUX, reason='Only available on Linux') @pytest.mark.datafiles(DATA_DIR) def test_missing_bwrap_has_nice_error_message(cli, datafiles, tmp_path): # Create symlink to buildbox-casd and git to work with custom PATH - bin_dir = tmp_path / "bin" - _symlink_host_tools_to_dir(['buildbox-casd', 'git'], bin_dir) + bin_dir = str(tmp_path / "bin") + symlink_host_tools_to_dir(['buildbox-casd', 'git'], bin_dir) project = str(datafiles) element_path = os.path.join(project, 'elements', 'element.bst') @@ -56,7 +50,7 @@ def test_missing_bwrap_has_nice_error_message(cli, datafiles, tmp_path): result = cli.run( project=project, args=['build', 'element.bst'], - env={'PATH': str(bin_dir), + env={'PATH': bin_dir, 'BST_FORCE_SANDBOX': None}) result.assert_task_error(ErrorDomain.SANDBOX, 'unavailable-local-sandbox') assert "not found" in result.stderr @@ -76,8 +70,8 @@ def test_old_brwap_has_nice_error_message(cli, datafiles, tmp_path): bwrap.chmod(0o755) # Create symlink to buildbox-casd and git to work with custom PATH - bin_dir = tmp_path / "bin" - _symlink_host_tools_to_dir(['buildbox-casd', 'git'], bin_dir) + bin_dir = str(tmp_path / "bin") + symlink_host_tools_to_dir(['buildbox-casd', 'git'], bin_dir) project = str(datafiles) element_path = os.path.join(project, 'elements', 'element3.bst') @@ -103,7 +97,7 @@ def test_old_brwap_has_nice_error_message(cli, datafiles, tmp_path): result = cli.run( project=project, args=['--debug', '--verbose', 'build', 'element3.bst'], - env={'PATH': str(bin_dir), + env={'PATH': bin_dir, 'BST_FORCE_SANDBOX': None}) result.assert_task_error(ErrorDomain.SANDBOX, 'unavailable-local-sandbox') assert "too old" in result.stderr diff --git a/tests/sandboxes/selection.py b/tests/sandboxes/selection.py index b4bbb1b..db937d1 100644 --- a/tests/sandboxes/selection.py +++ b/tests/sandboxes/selection.py @@ -19,10 +19,12 @@ import os import pytest -from buildstream import utils, _yaml +from buildstream import _yaml from buildstream._exceptions import ErrorDomain from buildstream.testing import cli # pylint: disable=unused-import +from tests.testutils import symlink_host_tools_to_dir + pytestmark = pytest.mark.integration @@ -66,9 +68,8 @@ def test_force_sandbox(cli, datafiles): @pytest.mark.datafiles(DATA_DIR) def test_dummy_sandbox_fallback(cli, datafiles, tmp_path): # Create symlink to buildbox-casd to work with custom PATH - buildbox_casd = tmp_path.joinpath('bin/buildbox-casd') - buildbox_casd.parent.mkdir() - os.symlink(utils.get_host_tool('buildbox-casd'), str(buildbox_casd)) + bin_dir = str(tmp_path / 'bin') + symlink_host_tools_to_dir(['buildbox-casd'], bin_dir) project = str(datafiles) element_path = os.path.join(project, 'elements', 'element.bst') @@ -94,7 +95,7 @@ def test_dummy_sandbox_fallback(cli, datafiles, tmp_path): result = cli.run( project=project, args=['build', 'element.bst'], - env={'PATH': str(tmp_path.joinpath('bin')), + env={'PATH': bin_dir, 'BST_FORCE_SANDBOX': None}) # But if we dont spesify a sandbox then we fall back to dummy, we still # fail early but only once we know we need a facny sandbox and that diff --git a/tests/testutils/__init__.py b/tests/testutils/__init__.py index 9642ddf..1105803 100644 --- a/tests/testutils/__init__.py +++ b/tests/testutils/__init__.py @@ -27,6 +27,7 @@ from .artifactshare import create_artifact_share, create_split_share, assert_sha from .context import dummy_context from .element_generators import create_element_size, update_element_size from .junction import generate_junction +from .host_tools import symlink_host_tools_to_dir from .runner_integration import wait_for_cache_granularity from .python_repo import setup_pypi_repo from .platform import override_platform_uname diff --git a/tests/testutils/host_tools.py b/tests/testutils/host_tools.py new file mode 100644 index 0000000..d6c1794 --- /dev/null +++ b/tests/testutils/host_tools.py @@ -0,0 +1,39 @@ +# +# Copyright (C) 2019 Bloomberg Finance LP +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library. If not, see <http://www.gnu.org/licenses/>. + +import os + +from buildstream import utils + + +# symlink_host_tools_to_dir() +# +# Ensure the specified tools are symlinked into the supplied directory. +# +# Create # the directory if it doesn't exist. +# +# This is useful for isolating tests, such that only the specified tools are +# available. +# +# Args: +# host_tools (List[str]): The string names of the tools, e.g. ['git', 'bzr']. +# dir_ (path-like): The path to put the symlinks into. +# +def symlink_host_tools_to_dir(host_tools, dir_): + os.makedirs(dir_, exist_ok=True) + for tool in host_tools: + target_path = os.path.join(dir_, tool) + os.symlink(utils.get_host_tool(tool), str(target_path))
