blob: bd3e830859ded0577080cafe50abb788eca4980e [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2015 The Bazel Authors. All rights reserved.
Philipp Wollermannd93922b2015-09-08 17:11:08 +00002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.lib.sandbox;
15
16import static com.google.common.truth.Truth.assertThat;
17import static org.junit.Assert.fail;
18
19import com.google.common.collect.ImmutableMap;
20import com.google.devtools.build.lib.actions.ActionExecutionContext;
Rumou Duan33bab462016-04-25 17:55:12 +000021import com.google.devtools.build.lib.actions.ActionExecutionMetadata;
Philipp Wollermannd93922b2015-09-08 17:11:08 +000022import com.google.devtools.build.lib.actions.BaseSpawn;
23import com.google.devtools.build.lib.actions.ResourceSet;
24import com.google.devtools.build.lib.actions.Spawn;
25import com.google.devtools.build.lib.actions.UserExecException;
26import com.google.devtools.build.lib.actions.util.ActionsTestUtil;
27import com.google.devtools.build.lib.exec.SingleBuildFileCache;
28import com.google.devtools.build.lib.shell.BadExitStatusException;
29import com.google.devtools.build.lib.testutil.TestSpec;
Philipp Wollermannd93922b2015-09-08 17:11:08 +000030import com.google.devtools.build.lib.util.OS;
31import com.google.devtools.build.lib.vfs.Path;
Philipp Wollermann278814b2016-07-15 14:41:54 +000032import java.util.Arrays;
33import java.util.Map;
Philipp Wollermannd93922b2015-09-08 17:11:08 +000034import org.junit.Test;
35import org.junit.runner.RunWith;
36import org.junit.runners.JUnit4;
37
Philipp Wollermannd93922b2015-09-08 17:11:08 +000038/**
Philipp Wollermann278814b2016-07-15 14:41:54 +000039 * Tests for {@code LinuxSandboxedStrategy} that must run locally, because they need to actually run
40 * the linux-sandbox binary.
Philipp Wollermannd93922b2015-09-08 17:11:08 +000041 */
42@TestSpec(localOnly = true, supportedOs = OS.LINUX)
43@RunWith(JUnit4.class)
44public class LocalLinuxSandboxedStrategyTest extends LinuxSandboxedStrategyTestCase {
45 protected Spawn createSpawn(String... arguments) {
46 Map<String, String> environment = ImmutableMap.<String, String>of();
47 Map<String, String> executionInfo = ImmutableMap.<String, String>of();
Rumou Duan33bab462016-04-25 17:55:12 +000048 ActionExecutionMetadata action = new ActionsTestUtil.NullAction();
Philipp Wollermannd93922b2015-09-08 17:11:08 +000049 ResourceSet localResources = ResourceSet.ZERO;
50 return new BaseSpawn(
51 Arrays.asList(arguments), environment, executionInfo, action, localResources);
52 }
53
54 protected ActionExecutionContext createContext() {
55 Path execRoot = executor.getExecRoot();
56 return new ActionExecutionContext(
57 executor,
58 new SingleBuildFileCache(execRoot.getPathString(), execRoot.getFileSystem()),
59 null,
60 outErr,
61 null);
62 }
63
64 @Test
65 public void testExecutionSuccess() throws Exception {
66 Spawn spawn = createSpawn("/bin/sh", "-c", "echo Hello, world.; touch dummy");
Philipp Wollermannb5043052015-10-06 11:35:52 +000067 getLinuxSandboxedStrategy().exec(spawn, createContext());
Philipp Wollermannd93922b2015-09-08 17:11:08 +000068 assertThat(out()).isEqualTo("Hello, world.\n");
69 assertThat(err()).isEmpty();
70 }
71
72 @Test
73 public void testExecutionFailurePrintsCorrectMessage() throws Exception {
74 Spawn spawn = createSpawn("/bin/sh", "-c", "echo ERROR >&2; exit 1");
75 try {
Philipp Wollermannb5043052015-10-06 11:35:52 +000076 getLinuxSandboxedStrategy().exec(spawn, createContext());
Philipp Wollermannd93922b2015-09-08 17:11:08 +000077 fail();
78 } catch (UserExecException e) {
79 assertThat(err()).isEqualTo("ERROR\n");
Philipp Wollermannd93922b2015-09-08 17:11:08 +000080 assertThat(e.getCause()).isInstanceOf(BadExitStatusException.class);
81 }
82 }
83}