aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/shell/integration/sandboxfs_test.sh
blob: a436640739b1b859986b091a16f75e98bb120978 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
# Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${CURRENT_DIR}/../integration_test_setup.sh" \
  || { echo "integration_test_setup.sh not found!" >&2; exit 1; }

# Bazel build arguments to disable the use of the sandbox.  We have tests below
# that configure a fake sandboxfs and they would fail if they were to use it.
DISABLE_SANDBOX_ARGS=(
  --genrule_strategy=local
  --spawn_strategy=local
)

function create_fake_sandboxfs() {
  local path="${1}"; shift

  cat >"${path}" <<EOF
#! /bin/sh
echo "ARGS: \${*}" 1>&2

while read line; do
  echo "Received: \${line}" 1>&2
  if [ -z "\${line}" ]; then
    echo "Done"
  fi
done
EOF
  chmod +x "${path}"
}

function create_hello_package() {
  mkdir -p hello

  cat >hello/BUILD <<EOF
cc_binary(name = "hello", srcs = ["hello.cc"])
EOF

  cat >hello/hello.cc <<EOF
#include <stdio.h>
int main(void) { printf("Hello, world!\n"); return 0; }
EOF
}

function test_default_sandboxfs_from_path() {
  mkdir -p fake-tools
  create_fake_sandboxfs fake-tools/sandboxfs
  PATH="$(pwd)/fake-tools:${PATH}"; export PATH

  create_hello_package

  local output_base="$(bazel info output_base)"
  local sandbox_base="${output_base}/sandbox"

  # This test relies on a PATH change that is only recognized when the server
  # first starts up, so ensure there are no Bazel servers left behind.
  #
  # TODO(philwo): This is awful.  The testing infrastructure should ensure
  # that tests cannot pollute each other's state, but at the moment that's not
  # the case.
  bazel shutdown

  bazel build \
    "${DISABLE_SANDBOX_ARGS[@]}" \
    --experimental_use_sandboxfs \
    //hello >"${TEST_log}" 2>&1 || fail "Build should have succeeded"

  expect_log "Mounting sandboxfs instance"
}

function test_explicit_sandboxfs_not_found() {
  create_hello_package

  bazel build \
    --experimental_use_sandboxfs \
    --experimental_sandboxfs_path="/non-existent/sandboxfs" \
    //hello >"${TEST_log}" 2>&1 && fail "Build succeeded but should have failed"

  expect_log "Failed to initialize sandbox: .*Cannot run .*/non-existent/"
}

function test_mount_unmount() {
  create_fake_sandboxfs fake-sandboxfs.sh
  create_hello_package

  local output_base="$(bazel info output_base)"
  local sandbox_base="${output_base}/sandbox"

  bazel build \
    "${DISABLE_SANDBOX_ARGS[@]}" \
    --experimental_use_sandboxfs \
    --experimental_sandboxfs_path="$(pwd)/fake-sandboxfs.sh" \
    --sandbox_debug \
    //hello >"${TEST_log}" 2>&1 || fail "Build should have succeeded"

  expect_log "Mounting sandboxfs instance"
  expect_log "unmounting sandboxfs"

  # Dump fake sandboxfs' log for debugging.
  sed -e 's,^,SANDBOXFS: ,' "${sandbox_base}/sandboxfs.log" >>"${TEST_log}"

  grep -q "ARGS: .*${sandbox_base}/sandboxfs" "${sandbox_base}/sandboxfs.log" \
    || fail "Cannot find expected mount point in sandboxfs mount call"
}

run_suite "sandboxfs-based sandboxing tests"