Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Philipp Wollermann | d93922b | 2015-09-08 17:11:08 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 14 | package com.google.devtools.build.lib.sandbox; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
| 17 | import static org.junit.Assert.fail; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.devtools.build.lib.actions.ActionExecutionContext; |
Rumou Duan | 33bab46 | 2016-04-25 17:55:12 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.actions.ActionExecutionMetadata; |
Philipp Wollermann | d93922b | 2015-09-08 17:11:08 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.actions.BaseSpawn; |
| 23 | import com.google.devtools.build.lib.actions.ResourceSet; |
| 24 | import com.google.devtools.build.lib.actions.Spawn; |
| 25 | import com.google.devtools.build.lib.actions.UserExecException; |
| 26 | import com.google.devtools.build.lib.actions.util.ActionsTestUtil; |
| 27 | import com.google.devtools.build.lib.exec.SingleBuildFileCache; |
| 28 | import com.google.devtools.build.lib.shell.BadExitStatusException; |
| 29 | import com.google.devtools.build.lib.testutil.TestSpec; |
Philipp Wollermann | d93922b | 2015-09-08 17:11:08 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.util.OS; |
| 31 | import com.google.devtools.build.lib.vfs.Path; |
Philipp Wollermann | 278814b | 2016-07-15 14:41:54 +0000 | [diff] [blame^] | 32 | import java.util.Arrays; |
| 33 | import java.util.Map; |
Philipp Wollermann | d93922b | 2015-09-08 17:11:08 +0000 | [diff] [blame] | 34 | import org.junit.Test; |
| 35 | import org.junit.runner.RunWith; |
| 36 | import org.junit.runners.JUnit4; |
| 37 | |
Philipp Wollermann | d93922b | 2015-09-08 17:11:08 +0000 | [diff] [blame] | 38 | /** |
Philipp Wollermann | 278814b | 2016-07-15 14:41:54 +0000 | [diff] [blame^] | 39 | * Tests for {@code LinuxSandboxedStrategy} that must run locally, because they need to actually run |
| 40 | * the linux-sandbox binary. |
Philipp Wollermann | d93922b | 2015-09-08 17:11:08 +0000 | [diff] [blame] | 41 | */ |
| 42 | @TestSpec(localOnly = true, supportedOs = OS.LINUX) |
| 43 | @RunWith(JUnit4.class) |
| 44 | public 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 Duan | 33bab46 | 2016-04-25 17:55:12 +0000 | [diff] [blame] | 48 | ActionExecutionMetadata action = new ActionsTestUtil.NullAction(); |
Philipp Wollermann | d93922b | 2015-09-08 17:11:08 +0000 | [diff] [blame] | 49 | 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 Wollermann | b504305 | 2015-10-06 11:35:52 +0000 | [diff] [blame] | 67 | getLinuxSandboxedStrategy().exec(spawn, createContext()); |
Philipp Wollermann | d93922b | 2015-09-08 17:11:08 +0000 | [diff] [blame] | 68 | 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 Wollermann | b504305 | 2015-10-06 11:35:52 +0000 | [diff] [blame] | 76 | getLinuxSandboxedStrategy().exec(spawn, createContext()); |
Philipp Wollermann | d93922b | 2015-09-08 17:11:08 +0000 | [diff] [blame] | 77 | fail(); |
| 78 | } catch (UserExecException e) { |
| 79 | assertThat(err()).isEqualTo("ERROR\n"); |
Philipp Wollermann | d93922b | 2015-09-08 17:11:08 +0000 | [diff] [blame] | 80 | assertThat(e.getCause()).isInstanceOf(BadExitStatusException.class); |
| 81 | } |
| 82 | } |
| 83 | } |