summaryrefslogtreecommitdiff
path: root/tests/integration/sandbox-bwrap.py
blob: d08076f5a361db05774952a742d7c3b071bfae8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Pylint doesn't play well with fixtures and dependency injection from pytest
# pylint: disable=redefined-outer-name

import os
import pytest

from buildstream._exceptions import ErrorDomain

from buildstream.testing import cli_integration as cli  # pylint: disable=unused-import
from buildstream.testing._utils.site import HAVE_BWRAP, HAVE_BWRAP_JSON_STATUS


pytestmark = pytest.mark.integration


DATA_DIR = os.path.join(
    os.path.dirname(os.path.realpath(__file__)),
    "project"
)


# Bubblewrap sandbox doesn't remove the dirs it created during its execution,
# so BuildStream tries to remove them to do good. BuildStream should be extra
# careful when those folders already exist and should not touch them, though.
@pytest.mark.skipif(not HAVE_BWRAP, reason='Only available with bubblewrap')
@pytest.mark.datafiles(DATA_DIR)
def test_sandbox_bwrap_cleanup_build(cli, datafiles):
    project = str(datafiles)
    # This element depends on a base image with non-empty `/tmp` folder.
    element_name = 'sandbox-bwrap/test-cleanup.bst'

    # Here, BuildStream should not attempt any rmdir etc.
    result = cli.run(project=project, args=['build', element_name])
    assert result.exit_code == 0


@pytest.mark.skipif(not HAVE_BWRAP, reason='Only available with bubblewrap')
@pytest.mark.skipif(not HAVE_BWRAP_JSON_STATUS, reason='Only available with bubblewrap supporting --json-status-fd')
@pytest.mark.datafiles(DATA_DIR)
def test_sandbox_bwrap_distinguish_setup_error(cli, datafiles):
    project = str(datafiles)
    element_name = 'sandbox-bwrap/non-executable-shell.bst'

    result = cli.run(project=project, args=['build', element_name])
    result.assert_task_error(error_domain=ErrorDomain.SANDBOX, error_reason="bwrap-sandbox-fail")


@pytest.mark.skipif(not HAVE_BWRAP, reason='Only available with bubblewrap')
@pytest.mark.datafiles(DATA_DIR)
def test_sandbox_bwrap_return_subprocess(cli, datafiles):
    project = str(datafiles)
    element_name = 'sandbox-bwrap/command-exit-42.bst'

    cli.configure({
        "logging": {
            "message-format": "%{element}|%{message}",
        },
    })

    result = cli.run(project=project, args=['build', element_name])
    result.assert_task_error(error_domain=ErrorDomain.SANDBOX, error_reason="command-failed")
    assert "sandbox-bwrap/command-exit-42.bst|Command failed with exitcode 42" in result.stderr