Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
| 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.standalone; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertWithMessage; |
michajlo | 660d17f | 2020-03-27 09:01:57 -0700 | [diff] [blame] | 18 | import static org.junit.Assert.assertThrows; |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 19 | |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.common.collect.ImmutableMap; |
ulfjack | e4cca14 | 2020-01-08 04:44:40 -0800 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableSet; |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 23 | import com.google.common.collect.Sets; |
| 24 | import com.google.common.eventbus.EventBus; |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.actions.ActionExecutionContext; |
mschaller | b4c83ec | 2019-12-26 08:27:08 -0800 | [diff] [blame] | 26 | import com.google.devtools.build.lib.actions.ActionExecutionContext.LostInputsCheck; |
Yun Peng | 7e4c9d5 | 2020-01-15 07:28:07 -0800 | [diff] [blame] | 27 | import com.google.devtools.build.lib.actions.ActionExecutionException; |
ulfjack | 5b99e50 | 2017-07-20 22:22:36 +0200 | [diff] [blame] | 28 | import com.google.devtools.build.lib.actions.ActionInputPrefetcher; |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 29 | import com.google.devtools.build.lib.actions.ActionKeyContext; |
ulfjack | acd291a | 2017-06-16 15:25:42 +0200 | [diff] [blame] | 30 | import com.google.devtools.build.lib.actions.Artifact; |
| 31 | import com.google.devtools.build.lib.actions.Artifact.ArtifactExpander; |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 32 | import com.google.devtools.build.lib.actions.ExecException; |
Ulf Adams | cddaaa6 | 2017-03-02 17:32:28 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.actions.ResourceManager; |
| 34 | import com.google.devtools.build.lib.actions.ResourceSet; |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 35 | import com.google.devtools.build.lib.actions.SimpleSpawn; |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.lib.actions.Spawn; |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 37 | import com.google.devtools.build.lib.actions.SpawnResult; |
janakr | 13b737a | 2021-07-02 14:24:25 -0700 | [diff] [blame^] | 38 | import com.google.devtools.build.lib.actions.ThreadStateReceiver; |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 39 | import com.google.devtools.build.lib.actions.util.ActionsTestUtil; |
janakr | 15ee837 | 2020-10-15 07:44:24 -0700 | [diff] [blame] | 40 | import com.google.devtools.build.lib.actions.util.ActionsTestUtil.NullAction; |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 41 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 42 | import com.google.devtools.build.lib.analysis.ServerDirectories; |
ulfjack | e4cca14 | 2020-01-08 04:44:40 -0800 | [diff] [blame] | 43 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
ajurkowski | a1d57ea | 2020-03-05 19:27:38 -0800 | [diff] [blame] | 44 | import com.google.devtools.build.lib.collect.nestedset.NestedSetExpander; |
ulfjack | e4cca14 | 2020-01-08 04:44:40 -0800 | [diff] [blame] | 45 | import com.google.devtools.build.lib.collect.nestedset.Order; |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 46 | import com.google.devtools.build.lib.events.PrintingEventHandler; |
| 47 | import com.google.devtools.build.lib.events.Reporter; |
felly | 472320c | 2018-10-29 14:27:00 -0700 | [diff] [blame] | 48 | import com.google.devtools.build.lib.exec.BinTools; |
Ulf Adams | dba3c83 | 2016-12-21 16:50:02 +0000 | [diff] [blame] | 49 | import com.google.devtools.build.lib.exec.BlazeExecutor; |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 50 | import com.google.devtools.build.lib.exec.ExecutionOptions; |
buchgr | 580038e | 2019-09-02 02:16:19 -0700 | [diff] [blame] | 51 | import com.google.devtools.build.lib.exec.RunfilesTreeUpdater; |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 52 | import com.google.devtools.build.lib.exec.SingleBuildFileCache; |
jcater | 9c3e678 | 2020-04-16 10:35:43 -0700 | [diff] [blame] | 53 | import com.google.devtools.build.lib.exec.SpawnStrategyResolver; |
ulfjack | acd291a | 2017-06-16 15:25:42 +0200 | [diff] [blame] | 54 | import com.google.devtools.build.lib.exec.local.LocalExecutionOptions; |
ulfjack | 19befaf | 2017-07-24 11:09:40 +0200 | [diff] [blame] | 55 | import com.google.devtools.build.lib.exec.local.LocalSpawnRunner; |
schmitt | 771fe38 | 2020-01-27 07:58:50 -0800 | [diff] [blame] | 56 | import com.google.devtools.build.lib.exec.util.TestExecutorBuilder; |
Ulf Adams | 6447ad9 | 2016-11-18 09:55:47 +0000 | [diff] [blame] | 57 | import com.google.devtools.build.lib.integration.util.IntegrationMock; |
kchodorow | 85ae190 | 2017-04-22 15:07:22 -0400 | [diff] [blame] | 58 | import com.google.devtools.build.lib.testutil.TestConstants; |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 59 | import com.google.devtools.build.lib.testutil.TestUtils; |
Dmitry Lomov | 9d74eba | 2015-11-26 11:04:01 +0000 | [diff] [blame] | 60 | import com.google.devtools.build.lib.util.OS; |
ulfjack | acd291a | 2017-06-16 15:25:42 +0200 | [diff] [blame] | 61 | import com.google.devtools.build.lib.util.io.FileOutErr; |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 62 | import com.google.devtools.build.lib.vfs.FileSystem; |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 63 | import com.google.devtools.build.lib.vfs.Path; |
twerth | d05f92e | 2021-05-11 05:45:32 -0700 | [diff] [blame] | 64 | import com.google.devtools.build.lib.vfs.UnixGlob; |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 65 | import com.google.devtools.build.lib.vfs.util.FileSystems; |
ulfjack | acd291a | 2017-06-16 15:25:42 +0200 | [diff] [blame] | 66 | import com.google.devtools.common.options.Options; |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 67 | import com.google.devtools.common.options.OptionsParser; |
Klaus Aehlig | d2fcd9d | 2016-08-26 08:16:25 +0000 | [diff] [blame] | 68 | import java.io.IOException; |
ulfjack | acd291a | 2017-06-16 15:25:42 +0200 | [diff] [blame] | 69 | import java.util.Collection; |
Yun Peng | 7e4c9d5 | 2020-01-15 07:28:07 -0800 | [diff] [blame] | 70 | import java.util.HashSet; |
ruperts | 7967f33 | 2017-11-21 16:37:13 -0800 | [diff] [blame] | 71 | import java.util.List; |
Florian Weikert | c4975fa | 2015-12-03 10:27:10 +0000 | [diff] [blame] | 72 | import org.junit.Before; |
| 73 | import org.junit.Test; |
| 74 | import org.junit.runner.RunWith; |
| 75 | import org.junit.runners.JUnit4; |
buchgr | 580038e | 2019-09-02 02:16:19 -0700 | [diff] [blame] | 76 | import org.mockito.Mockito; |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 77 | |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 78 | /** |
| 79 | * Test StandaloneSpawnStrategy. |
| 80 | */ |
Florian Weikert | c4975fa | 2015-12-03 10:27:10 +0000 | [diff] [blame] | 81 | @RunWith(JUnit4.class) |
| 82 | public class StandaloneSpawnStrategyTest { |
ulfjack | acd291a | 2017-06-16 15:25:42 +0200 | [diff] [blame] | 83 | private static final ArtifactExpander SIMPLE_ARTIFACT_EXPANDER = |
| 84 | new ArtifactExpander() { |
| 85 | @Override |
| 86 | public void expand(Artifact artifact, Collection<? super Artifact> output) { |
| 87 | output.add(artifact); |
| 88 | } |
| 89 | }; |
Yun Peng | 7e4c9d5 | 2020-01-15 07:28:07 -0800 | [diff] [blame] | 90 | private static final String WINDOWS_SYSTEM_DRIVE = "C:"; |
| 91 | private static final String CMD_EXE = getWinSystemBinary("cmd.exe"); |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 92 | |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 93 | private Reporter reporter = |
| 94 | new Reporter(new EventBus(), PrintingEventHandler.ERRORS_AND_WARNINGS_TO_STDERR); |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 95 | private BlazeExecutor executor; |
| 96 | private FileSystem fileSystem; |
ulfjack | acd291a | 2017-06-16 15:25:42 +0200 | [diff] [blame] | 97 | private FileOutErr outErr; |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 98 | |
| 99 | private Path createTestRoot() throws IOException { |
Yun Peng | 22eb332 | 2016-06-21 14:38:12 +0000 | [diff] [blame] | 100 | fileSystem = FileSystems.getNativeFileSystem(); |
jmmv | a2a2f9d | 2020-10-07 06:40:44 -0700 | [diff] [blame] | 101 | Path testRoot = fileSystem.getPath(TestUtils.tmpDir()).getRelative("test"); |
| 102 | testRoot.createDirectoryAndParents(); |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 103 | try { |
jmmv | 5cc1f65 | 2019-03-20 09:34:08 -0700 | [diff] [blame] | 104 | testRoot.deleteTreesBelow(); |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 105 | } catch (IOException e) { |
| 106 | System.err.println("Failed to remove directory " + testRoot + ": " + e.getMessage()); |
| 107 | throw e; |
| 108 | } |
| 109 | return testRoot; |
| 110 | } |
| 111 | |
Yun Peng | 7e4c9d5 | 2020-01-15 07:28:07 -0800 | [diff] [blame] | 112 | /** |
| 113 | * We assume Windows is installed on C: and all system binaries exist under C:\Windows\System32\ |
| 114 | */ |
| 115 | private static String getWinSystemBinary(String binary) { |
| 116 | return WINDOWS_SYSTEM_DRIVE + "\\Windows\\System32\\" + binary; |
| 117 | } |
| 118 | |
Florian Weikert | c4975fa | 2015-12-03 10:27:10 +0000 | [diff] [blame] | 119 | @Before |
| 120 | public final void setUp() throws Exception { |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 121 | Path testRoot = createTestRoot(); |
Ulf Adams | 015aad9 | 2016-07-13 16:49:40 +0000 | [diff] [blame] | 122 | Path workspaceDir = testRoot.getRelative("workspace-name"); |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 123 | workspaceDir.createDirectory(); |
ulfjack | acd291a | 2017-06-16 15:25:42 +0200 | [diff] [blame] | 124 | outErr = new FileOutErr(testRoot.getRelative("stdout"), testRoot.getRelative("stderr")); |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 125 | |
| 126 | // setup output base & directories |
| 127 | Path outputBase = testRoot.getRelative("outputBase"); |
| 128 | outputBase.createDirectory(); |
| 129 | |
Ulf Adams | 84e1032 | 2016-07-13 17:23:33 +0000 | [diff] [blame] | 130 | BlazeDirectories directories = |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 131 | new BlazeDirectories( |
Klaus Aehlig | c2499c4 | 2018-02-27 05:47:21 -0800 | [diff] [blame] | 132 | new ServerDirectories(outputBase, outputBase, outputBase), |
| 133 | workspaceDir, |
cushon | 849df36 | 2018-05-14 01:51:45 -0700 | [diff] [blame] | 134 | /* defaultSystemJavabase= */ null, |
Klaus Aehlig | c2499c4 | 2018-02-27 05:47:21 -0800 | [diff] [blame] | 135 | "mock-product-name"); |
Ulf Adams | 6447ad9 | 2016-11-18 09:55:47 +0000 | [diff] [blame] | 136 | // This call implicitly symlinks the integration bin tools into the exec root. |
felly | 472320c | 2018-10-29 14:27:00 -0700 | [diff] [blame] | 137 | IntegrationMock.get().getIntegrationBinTools(fileSystem, directories); |
jcater | 06d624a | 2019-06-18 09:07:58 -0700 | [diff] [blame] | 138 | OptionsParser optionsParser = |
| 139 | OptionsParser.builder().optionsClasses(ExecutionOptions.class).build(); |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 140 | optionsParser.parse("--verbose_failures"); |
ulfjack | acd291a | 2017-06-16 15:25:42 +0200 | [diff] [blame] | 141 | LocalExecutionOptions localExecutionOptions = Options.getDefaults(LocalExecutionOptions.class); |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 142 | |
Ulf Adams | cddaaa6 | 2017-03-02 17:32:28 +0000 | [diff] [blame] | 143 | ResourceManager resourceManager = ResourceManager.instanceForTestingOnly(); |
| 144 | resourceManager.setAvailableResources( |
ulfjack | 4f18986 | 2018-10-26 09:59:33 -0700 | [diff] [blame] | 145 | ResourceSet.create(/*memoryMb=*/1, /*cpuUsage=*/1, /*localTestCount=*/1)); |
kchodorow | 85ae190 | 2017-04-22 15:07:22 -0400 | [diff] [blame] | 146 | Path execRoot = directories.getExecRoot(TestConstants.WORKSPACE_NAME); |
schmitt | 771fe38 | 2020-01-27 07:58:50 -0800 | [diff] [blame] | 147 | BinTools binTools = BinTools.forIntegrationTesting(directories, ImmutableList.of()); |
| 148 | StandaloneSpawnStrategy strategy = |
| 149 | new StandaloneSpawnStrategy( |
kchodorow | 85ae190 | 2017-04-22 15:07:22 -0400 | [diff] [blame] | 150 | execRoot, |
schmitt | 771fe38 | 2020-01-27 07:58:50 -0800 | [diff] [blame] | 151 | new LocalSpawnRunner( |
| 152 | execRoot, |
| 153 | localExecutionOptions, |
| 154 | resourceManager, |
| 155 | (env, binTools1, fallbackTmpDir) -> ImmutableMap.copyOf(env), |
| 156 | binTools, |
jmmv | 511cfd9 | 2020-05-11 07:35:04 -0700 | [diff] [blame] | 157 | /*processWrapper=*/ null, |
janakr | 619dcf8 | 2020-09-22 14:08:59 -0700 | [diff] [blame] | 158 | Mockito.mock(RunfilesTreeUpdater.class)), |
| 159 | /*verboseFailures=*/ false); |
schmitt | 771fe38 | 2020-01-27 07:58:50 -0800 | [diff] [blame] | 160 | this.executor = |
| 161 | new TestExecutorBuilder(fileSystem, directories, binTools) |
jcater | 18e7850 | 2020-03-26 13:14:56 -0700 | [diff] [blame] | 162 | .addStrategy(strategy, "standalone") |
| 163 | .setDefaultStrategies("standalone") |
schmitt | 771fe38 | 2020-01-27 07:58:50 -0800 | [diff] [blame] | 164 | .build(); |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 165 | |
ulfjack | 9f6995a | 2018-02-09 04:34:48 -0800 | [diff] [blame] | 166 | executor.getExecRoot().createDirectoryAndParents(); |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | private Spawn createSpawn(String... arguments) { |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 170 | return new SimpleSpawn( |
Ulf Adams | cddaaa6 | 2017-03-02 17:32:28 +0000 | [diff] [blame] | 171 | new ActionsTestUtil.NullAction(), |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 172 | ImmutableList.copyOf(arguments), |
| 173 | /*environment=*/ ImmutableMap.of(), |
| 174 | /*executionInfo=*/ ImmutableMap.of(), |
ulfjack | e4cca14 | 2020-01-08 04:44:40 -0800 | [diff] [blame] | 175 | /*inputs=*/ NestedSetBuilder.emptySet(Order.STABLE_ORDER), |
| 176 | /*outputs=*/ ImmutableSet.of(), |
Ulf Adams | cddaaa6 | 2017-03-02 17:32:28 +0000 | [diff] [blame] | 177 | ResourceSet.ZERO); |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 180 | private String out() { |
| 181 | return outErr.outAsLatin1(); |
| 182 | } |
| 183 | private String err() { |
| 184 | return outErr.errAsLatin1(); |
| 185 | } |
| 186 | |
Florian Weikert | c4975fa | 2015-12-03 10:27:10 +0000 | [diff] [blame] | 187 | @Test |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 188 | public void testBinTrueExecutesFine() throws Exception { |
Dmitry Lomov | 9d74eba | 2015-11-26 11:04:01 +0000 | [diff] [blame] | 189 | Spawn spawn = createSpawn(getTrueCommand()); |
jcater | 9c3e678 | 2020-04-16 10:35:43 -0700 | [diff] [blame] | 190 | executor.getContext(SpawnStrategyResolver.class).exec(spawn, createContext()); |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 191 | |
Yun Peng | 7e4c9d5 | 2020-01-15 07:28:07 -0800 | [diff] [blame] | 192 | if (OS.getCurrent() != OS.WINDOWS) { |
| 193 | assertThat(out()).isEmpty(); |
| 194 | } |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 195 | assertThat(err()).isEmpty(); |
| 196 | } |
| 197 | |
ruperts | 7967f33 | 2017-11-21 16:37:13 -0800 | [diff] [blame] | 198 | private List<SpawnResult> run(Spawn spawn) throws Exception { |
jcater | 9c3e678 | 2020-04-16 10:35:43 -0700 | [diff] [blame] | 199 | return executor.getContext(SpawnStrategyResolver.class).exec(spawn, createContext()); |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 200 | } |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 201 | |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 202 | private ActionExecutionContext createContext() { |
| 203 | Path execRoot = executor.getExecRoot(); |
| 204 | return new ActionExecutionContext( |
| 205 | executor, |
| 206 | new SingleBuildFileCache(execRoot.getPathString(), execRoot.getFileSystem()), |
ulfjack | 7599a4d | 2017-07-21 13:58:33 +0200 | [diff] [blame] | 207 | ActionInputPrefetcher.NONE, |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 208 | new ActionKeyContext(), |
ulfjack | 256be11 | 2019-04-05 05:42:01 -0700 | [diff] [blame] | 209 | /*metadataHandler=*/ null, |
mschaller | b20f512 | 2020-04-23 21:36:25 -0700 | [diff] [blame] | 210 | /*rewindingEnabled=*/ false, |
mschaller | b4c83ec | 2019-12-26 08:27:08 -0800 | [diff] [blame] | 211 | LostInputsCheck.NONE, |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 212 | outErr, |
ulfjack | 256be11 | 2019-04-05 05:42:01 -0700 | [diff] [blame] | 213 | reporter, |
| 214 | /*clientEnv=*/ ImmutableMap.of(), |
| 215 | /*topLevelFilesets=*/ ImmutableMap.of(), |
shahan | b1dd4e3 | 2018-05-09 08:23:31 -0700 | [diff] [blame] | 216 | SIMPLE_ARTIFACT_EXPANDER, |
felly | 2b3befd | 2018-08-10 10:37:56 -0700 | [diff] [blame] | 217 | /*actionFileSystem=*/ null, |
ajurkowski | a1d57ea | 2020-03-05 19:27:38 -0800 | [diff] [blame] | 218 | /*skyframeDepsResult=*/ null, |
twerth | d05f92e | 2021-05-11 05:45:32 -0700 | [diff] [blame] | 219 | NestedSetExpander.DEFAULT, |
janakr | 13b737a | 2021-07-02 14:24:25 -0700 | [diff] [blame^] | 220 | UnixGlob.DEFAULT_SYSCALLS, |
| 221 | ThreadStateReceiver.NULL_INSTANCE); |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Florian Weikert | c4975fa | 2015-12-03 10:27:10 +0000 | [diff] [blame] | 224 | @Test |
Yun Peng | 7e4c9d5 | 2020-01-15 07:28:07 -0800 | [diff] [blame] | 225 | public void testBinFalseYieldsException() { |
jcater | 7472b37 | 2019-04-30 07:40:50 -0700 | [diff] [blame] | 226 | ExecException e = assertThrows(ExecException.class, () -> run(createSpawn(getFalseCommand()))); |
| 227 | assertWithMessage("got: " + e.getMessage()) |
Yun Peng | 7e4c9d5 | 2020-01-15 07:28:07 -0800 | [diff] [blame] | 228 | .that(e.getMessage().contains("failed: error executing command")) |
jcater | 7472b37 | 2019-04-30 07:40:50 -0700 | [diff] [blame] | 229 | .isTrue(); |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Dmitry Lomov | 9d74eba | 2015-11-26 11:04:01 +0000 | [diff] [blame] | 232 | private static String getFalseCommand() { |
Yun Peng | 7e4c9d5 | 2020-01-15 07:28:07 -0800 | [diff] [blame] | 233 | if (OS.getCurrent() == OS.WINDOWS) { |
| 234 | // No false command on Windows, we use help.exe as an alternative, |
| 235 | // the caveat is that the command will have some output to stdout. |
| 236 | // Default exit code of help is 1 |
| 237 | return getWinSystemBinary("help.exe"); |
| 238 | } |
Dmitry Lomov | 9d74eba | 2015-11-26 11:04:01 +0000 | [diff] [blame] | 239 | return OS.getCurrent() == OS.DARWIN ? "/usr/bin/false" : "/bin/false"; |
| 240 | } |
| 241 | |
| 242 | private static String getTrueCommand() { |
Yun Peng | 7e4c9d5 | 2020-01-15 07:28:07 -0800 | [diff] [blame] | 243 | if (OS.getCurrent() == OS.WINDOWS) { |
| 244 | // No true command on Windows, we use whoami.exe as an alternative, |
| 245 | // the caveat is that the command will have some output to stdout. |
| 246 | // Default exit code of help is 0 |
| 247 | return getWinSystemBinary("whoami.exe"); |
| 248 | } |
Dmitry Lomov | 9d74eba | 2015-11-26 11:04:01 +0000 | [diff] [blame] | 249 | return OS.getCurrent() == OS.DARWIN ? "/usr/bin/true" : "/bin/true"; |
| 250 | } |
| 251 | |
Florian Weikert | c4975fa | 2015-12-03 10:27:10 +0000 | [diff] [blame] | 252 | @Test |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 253 | public void testBinEchoPrintsArguments() throws Exception { |
Yun Peng | 7e4c9d5 | 2020-01-15 07:28:07 -0800 | [diff] [blame] | 254 | Spawn spawn; |
| 255 | if (OS.getCurrent() == OS.WINDOWS) { |
| 256 | spawn = createSpawn(CMD_EXE, "/c", "echo", "Hello,", "world."); |
| 257 | } else { |
| 258 | spawn = createSpawn("/bin/echo", "Hello,", "world."); |
| 259 | } |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 260 | run(spawn); |
Yun Peng | 7e4c9d5 | 2020-01-15 07:28:07 -0800 | [diff] [blame] | 261 | assertThat(out()).isEqualTo("Hello, world." + System.lineSeparator()); |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 262 | assertThat(err()).isEmpty(); |
| 263 | } |
| 264 | |
Florian Weikert | c4975fa | 2015-12-03 10:27:10 +0000 | [diff] [blame] | 265 | @Test |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 266 | public void testCommandRunsInWorkingDir() throws Exception { |
Yun Peng | 7e4c9d5 | 2020-01-15 07:28:07 -0800 | [diff] [blame] | 267 | Spawn spawn; |
| 268 | if (OS.getCurrent() == OS.WINDOWS) { |
| 269 | spawn = createSpawn(CMD_EXE, "/c", "cd"); |
| 270 | } else { |
| 271 | spawn = createSpawn("/bin/pwd"); |
| 272 | } |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 273 | run(spawn); |
Yun Peng | 7e4c9d5 | 2020-01-15 07:28:07 -0800 | [diff] [blame] | 274 | assertThat(out().replace('\\', '/')).isEqualTo(executor.getExecRoot() + System.lineSeparator()); |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Florian Weikert | c4975fa | 2015-12-03 10:27:10 +0000 | [diff] [blame] | 277 | @Test |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 278 | public void testCommandHonorsEnvironment() throws Exception { |
ulfjack | e4cca14 | 2020-01-08 04:44:40 -0800 | [diff] [blame] | 279 | Spawn spawn = |
| 280 | new SimpleSpawn( |
| 281 | new ActionsTestUtil.NullAction(), |
Yun Peng | 7e4c9d5 | 2020-01-15 07:28:07 -0800 | [diff] [blame] | 282 | OS.getCurrent() == OS.WINDOWS |
| 283 | ? ImmutableList.of(CMD_EXE, "/c", "set") |
| 284 | : ImmutableList.of("/usr/bin/env"), |
ulfjack | e4cca14 | 2020-01-08 04:44:40 -0800 | [diff] [blame] | 285 | /*environment=*/ ImmutableMap.of("foo", "bar", "baz", "boo"), |
| 286 | /*executionInfo=*/ ImmutableMap.of(), |
| 287 | /*inputs=*/ NestedSetBuilder.emptySet(Order.STABLE_ORDER), |
| 288 | /*outputs=*/ ImmutableSet.of(), |
| 289 | ResourceSet.ZERO); |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 290 | run(spawn); |
Yun Peng | 7e4c9d5 | 2020-01-15 07:28:07 -0800 | [diff] [blame] | 291 | HashSet<String> environment = Sets.newHashSet(out().split(System.lineSeparator())); |
| 292 | if (OS.getCurrent() == OS.WINDOWS || OS.getCurrent() == OS.DARWIN) { |
| 293 | // On Windows and macOS, we may have some other env vars |
| 294 | // (eg. SystemRoot or __CF_USER_TEXT_ENCODING). |
| 295 | assertThat(environment).contains("foo=bar"); |
| 296 | assertThat(environment).contains("baz=boo"); |
| 297 | } else { |
| 298 | assertThat(environment).isEqualTo(Sets.newHashSet("foo=bar", "baz=boo")); |
| 299 | } |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Florian Weikert | c4975fa | 2015-12-03 10:27:10 +0000 | [diff] [blame] | 302 | @Test |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 303 | public void testStandardError() throws Exception { |
Yun Peng | 7e4c9d5 | 2020-01-15 07:28:07 -0800 | [diff] [blame] | 304 | Spawn spawn; |
| 305 | if (OS.getCurrent() == OS.WINDOWS) { |
| 306 | spawn = createSpawn(CMD_EXE, "/c", "echo Oops!>&2"); |
| 307 | } else { |
| 308 | spawn = createSpawn("/bin/sh", "-c", "echo Oops! >&2"); |
| 309 | } |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 310 | run(spawn); |
Yun Peng | 7e4c9d5 | 2020-01-15 07:28:07 -0800 | [diff] [blame] | 311 | assertThat(err()).isEqualTo("Oops!" + System.lineSeparator()); |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 312 | assertThat(out()).isEmpty(); |
| 313 | } |
Yun Peng | 7e4c9d5 | 2020-01-15 07:28:07 -0800 | [diff] [blame] | 314 | |
| 315 | /** |
| 316 | * Regression test for https://github.com/bazelbuild/bazel/issues/10572 Make sure we do have the |
| 317 | * command line executed in the error message of ActionExecutionException when --verbose_failures |
| 318 | * is enabled. |
| 319 | */ |
| 320 | @Test |
| 321 | public void testVerboseFailures() { |
| 322 | ExecException e = assertThrows(ExecException.class, () -> run(createSpawn(getFalseCommand()))); |
janakr | df70135 | 2020-10-13 08:20:35 -0700 | [diff] [blame] | 323 | ActionExecutionException actionExecutionException = |
janakr | 15ee837 | 2020-10-15 07:44:24 -0700 | [diff] [blame] | 324 | e.toActionExecutionException(new NullAction()); |
Yun Peng | 7e4c9d5 | 2020-01-15 07:28:07 -0800 | [diff] [blame] | 325 | assertWithMessage("got: " + actionExecutionException.getMessage()) |
| 326 | .that(actionExecutionException.getMessage().contains("failed: error executing command")) |
| 327 | .isTrue(); |
| 328 | } |
Philipp Wollermann | 1ee9441 | 2015-11-25 13:52:17 +0000 | [diff] [blame] | 329 | } |