diff options
author | Tiago Gomes <tiago.gomes@codethink.co.uk> | 2018-06-28 11:31:09 +0100 |
---|---|---|
committer | Tristan Van Berkom <tristan.van.berkom@gmail.com> | 2018-07-02 14:45:04 +0000 |
commit | 9067e269a9f2866e659ef33a69aad72b01cb6633 (patch) | |
tree | 214930ffc29bb3e7c10de81babb834e4119f8c77 | |
parent | a85aaee6630c2e58c20f538a235179ec65b6b32e (diff) | |
download | buildstream-9067e269a9f2866e659ef33a69aad72b01cb6633.tar.gz |
Provide better error message on missing commands
Before running a command in the sandbox, check its existence and fail
early if it does not.
This fixes issue #289.
-rw-r--r-- | buildstream/sandbox/_sandboxbwrap.py | 6 | ||||
-rw-r--r-- | buildstream/sandbox/_sandboxchroot.py | 5 | ||||
-rw-r--r-- | buildstream/sandbox/sandbox.py | 22 | ||||
-rw-r--r-- | tests/sandboxes/missing-command.py | 19 | ||||
-rw-r--r-- | tests/sandboxes/missing-command/no-runtime.bst | 1 | ||||
-rw-r--r-- | tests/sandboxes/missing-command/project.conf | 1 |
6 files changed, 54 insertions, 0 deletions
diff --git a/buildstream/sandbox/_sandboxbwrap.py b/buildstream/sandbox/_sandboxbwrap.py index 3a0645aae..9ed677620 100644 --- a/buildstream/sandbox/_sandboxbwrap.py +++ b/buildstream/sandbox/_sandboxbwrap.py @@ -28,6 +28,7 @@ from contextlib import ExitStack import psutil +from .._exceptions import SandboxError from .. import utils, _signals from ._mount import MountMap from . import Sandbox, SandboxFlags @@ -66,6 +67,11 @@ class SandboxBwrap(Sandbox): if env is None: env = self._get_environment() + if not self._has_command(command[0], env): + raise SandboxError("Staged artifacts do not provide command " + "'{}'".format(command[0]), + reason='missing-command') + # We want command args as a list of strings if isinstance(command, str): command = [command] diff --git a/buildstream/sandbox/_sandboxchroot.py b/buildstream/sandbox/_sandboxchroot.py index 22e69d549..8788c3031 100644 --- a/buildstream/sandbox/_sandboxchroot.py +++ b/buildstream/sandbox/_sandboxchroot.py @@ -58,6 +58,11 @@ class SandboxChroot(Sandbox): if env is None: env = self._get_environment() + if not self._has_command(command[0], env): + raise SandboxError("Staged artifacts do not provide command " + "'{}'".format(command[0]), + reason='missing-command') + # Command must be a list if isinstance(command, str): command = [command] diff --git a/buildstream/sandbox/sandbox.py b/buildstream/sandbox/sandbox.py index 595b9e778..7e1e32b65 100644 --- a/buildstream/sandbox/sandbox.py +++ b/buildstream/sandbox/sandbox.py @@ -279,3 +279,25 @@ class Sandbox(): # data passed in during construction. def _get_config(self): return self.__config + + # _has_command() + # + # Tests whether a command exists inside the sandbox + # + # Args: + # command (list): The command to test. + # env (dict): A dictionary of string key, value pairs to set as environment + # variables inside the sandbox environment. + # Returns: + # (bool): Whether a command exists inside the sandbox. + def _has_command(self, command, env=None): + if os.path.isabs(command): + return os.path.exists(os.path.join( + self.get_directory(), command.lstrip(os.sep))) + + for path in env.get('PATH').split(':'): + if os.path.exists(os.path.join( + self.get_directory(), path.lstrip(os.sep), command)): + return True + + return False diff --git a/tests/sandboxes/missing-command.py b/tests/sandboxes/missing-command.py new file mode 100644 index 000000000..8f210bcec --- /dev/null +++ b/tests/sandboxes/missing-command.py @@ -0,0 +1,19 @@ +import os +import pytest + +from buildstream._exceptions import ErrorDomain + +from tests.testutils import cli + + +DATA_DIR = os.path.join( + os.path.dirname(os.path.realpath(__file__)), + "missing-command" +) + + +@pytest.mark.datafiles(DATA_DIR) +def test_missing_command(cli, tmpdir, datafiles): + project = os.path.join(datafiles.dirname, datafiles.basename) + result = cli.run(project=project, args=['build', 'no-runtime.bst']) + result.assert_task_error(ErrorDomain.SANDBOX, 'missing-command') diff --git a/tests/sandboxes/missing-command/no-runtime.bst b/tests/sandboxes/missing-command/no-runtime.bst new file mode 100644 index 000000000..4d7f70266 --- /dev/null +++ b/tests/sandboxes/missing-command/no-runtime.bst @@ -0,0 +1 @@ +kind: manual diff --git a/tests/sandboxes/missing-command/project.conf b/tests/sandboxes/missing-command/project.conf new file mode 100644 index 000000000..b32753625 --- /dev/null +++ b/tests/sandboxes/missing-command/project.conf @@ -0,0 +1 @@ +name: test |