ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 1 | // Copyright 2017 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 | |
| 15 | package com.google.devtools.build.lib.exec; |
| 16 | |
| 17 | import static com.google.common.truth.Truth.assertThat; |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 18 | import static java.nio.charset.StandardCharsets.UTF_8; |
| 19 | import static org.junit.Assert.fail; |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 20 | import static org.mockito.Matchers.any; |
| 21 | import static org.mockito.Mockito.when; |
| 22 | |
ruperts | 7967f33 | 2017-11-21 16:37:13 -0800 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableList; |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 24 | import com.google.common.collect.ImmutableMap; |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 25 | import com.google.common.collect.Iterables; |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 26 | import com.google.common.collect.MoreCollectors; |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 27 | import com.google.devtools.build.lib.actions.ActionContext; |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 28 | import com.google.devtools.build.lib.actions.ActionExecutionContext; |
ulfjack | 645fe0f | 2019-02-28 05:58:36 -0800 | [diff] [blame] | 29 | import com.google.devtools.build.lib.actions.ActionExecutionException; |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 30 | import com.google.devtools.build.lib.actions.ActionInputPrefetcher; |
| 31 | import com.google.devtools.build.lib.actions.ActionKeyContext; |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 32 | import com.google.devtools.build.lib.actions.Artifact; |
ulfjack | 0858ae1 | 2018-07-27 02:37:53 -0700 | [diff] [blame] | 33 | import com.google.devtools.build.lib.actions.Spawn; |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 34 | import com.google.devtools.build.lib.actions.SpawnActionContext; |
ulfjack | 895c43d | 2019-03-25 01:58:08 -0700 | [diff] [blame] | 35 | import com.google.devtools.build.lib.actions.SpawnContinuation; |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 36 | import com.google.devtools.build.lib.actions.SpawnResult; |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 37 | import com.google.devtools.build.lib.actions.SpawnResult.Status; |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 38 | import com.google.devtools.build.lib.analysis.ConfiguredTarget; |
ulfjack | 645fe0f | 2019-02-28 05:58:36 -0800 | [diff] [blame] | 39 | import com.google.devtools.build.lib.analysis.test.TestActionContext; |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 40 | import com.google.devtools.build.lib.analysis.test.TestProvider; |
| 41 | import com.google.devtools.build.lib.analysis.test.TestResult; |
| 42 | import com.google.devtools.build.lib.analysis.test.TestRunnerAction; |
| 43 | import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; |
ulfjack | 81eda1d | 2018-06-07 08:38:21 -0700 | [diff] [blame] | 44 | import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.TestStatus; |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 45 | import com.google.devtools.build.lib.clock.BlazeClock; |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 46 | import com.google.devtools.build.lib.clock.Clock; |
| 47 | import com.google.devtools.build.lib.events.ExtendedEventHandler; |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 48 | import com.google.devtools.build.lib.events.StoredEventHandler; |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 49 | import com.google.devtools.build.lib.exec.TestStrategy.TestOutputFormat; |
ulfjack | 3d8e51c | 2018-11-29 06:54:29 -0800 | [diff] [blame] | 50 | import com.google.devtools.build.lib.util.OS; |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 51 | import com.google.devtools.build.lib.util.io.FileOutErr; |
| 52 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 53 | import com.google.devtools.build.lib.vfs.Path; |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 54 | import com.google.devtools.build.lib.view.test.TestStatus.BlazeTestStatus; |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 55 | import com.google.devtools.common.options.Options; |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 56 | import java.io.IOException; |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 57 | import java.io.OutputStream; |
ruperts | 02fb4bb | 2017-11-04 06:40:14 +0100 | [diff] [blame] | 58 | import java.time.Duration; |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 59 | import java.util.ArrayList; |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 60 | import java.util.List; |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 61 | import org.junit.Before; |
| 62 | import org.junit.Test; |
| 63 | import org.junit.runner.RunWith; |
| 64 | import org.junit.runners.JUnit4; |
| 65 | import org.mockito.Mock; |
| 66 | import org.mockito.MockitoAnnotations; |
| 67 | |
| 68 | /** Unit tests for {@link StandaloneTestStrategy}. */ |
| 69 | @RunWith(JUnit4.class) |
| 70 | public final class StandaloneTestStrategyTest extends BuildViewTestCase { |
| 71 | |
| 72 | private static class TestedStandaloneTestStrategy extends StandaloneTestStrategy { |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 73 | TestResult postedResult = null; |
| 74 | |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 75 | public TestedStandaloneTestStrategy( |
| 76 | ExecutionOptions executionOptions, BinTools binTools, Path tmpDirRoot) { |
| 77 | super(executionOptions, binTools, tmpDirRoot); |
| 78 | } |
| 79 | |
| 80 | @Override |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 81 | protected void postTestResult( |
| 82 | ActionExecutionContext actionExecutionContext, TestResult result) { |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 83 | postedResult = result; |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 87 | private class FakeActionExecutionContext extends ActionExecutionContext { |
| 88 | private final SpawnActionContext spawnActionContext; |
| 89 | |
| 90 | public FakeActionExecutionContext( |
| 91 | FileOutErr fileOutErr, SpawnActionContext spawnActionContext) { |
| 92 | super( |
| 93 | /* executor= */ null, |
| 94 | /* actionInputFileCache= */ null, |
| 95 | ActionInputPrefetcher.NONE, |
| 96 | new ActionKeyContext(), |
| 97 | /* metadataHandler= */ null, |
| 98 | fileOutErr, |
janakr | af5df29 | 2018-12-17 11:35:44 -0800 | [diff] [blame] | 99 | /*eventHandler=*/ null, |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 100 | /* clientEnv= */ ImmutableMap.of(), |
| 101 | /* topLevelFilesets= */ ImmutableMap.of(), |
| 102 | /* artifactExpander= */ null, |
| 103 | /* actionFileSystem= */ null, |
| 104 | /* skyframeDepsResult= */ null); |
| 105 | this.spawnActionContext = spawnActionContext; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public Clock getClock() { |
| 110 | return BlazeClock.instance(); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public <T extends ActionContext> T getContext(Class<? extends T> type) { |
| 115 | return SpawnActionContext.class.equals(type) ? type.cast(spawnActionContext) : null; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public ExtendedEventHandler getEventHandler() { |
| 120 | return storedEvents; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public Path getExecRoot() { |
| 125 | return outputBase.getRelative("execroot"); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public ActionExecutionContext withFileOutErr(FileOutErr fileOutErr) { |
| 130 | return new FakeActionExecutionContext(fileOutErr, spawnActionContext); |
| 131 | } |
| 132 | } |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 133 | |
| 134 | @Mock private SpawnActionContext spawnActionContext; |
| 135 | |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 136 | private StoredEventHandler storedEvents = new StoredEventHandler(); |
| 137 | |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 138 | @Before |
| 139 | public final void setUp() throws Exception { |
| 140 | MockitoAnnotations.initMocks(this); |
| 141 | } |
| 142 | |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 143 | private FileOutErr createTempOutErr(Path tmpDirRoot) { |
| 144 | Path outPath = tmpDirRoot.getRelative("test-out.txt"); |
| 145 | Path errPath = tmpDirRoot.getRelative("test-err.txt"); |
| 146 | return new FileOutErr(outPath, errPath); |
| 147 | } |
| 148 | |
| 149 | private TestRunnerAction getTestAction(String target) throws Exception { |
| 150 | ConfiguredTarget configuredTarget = getConfiguredTarget(target); |
| 151 | List<Artifact> testStatusArtifacts = |
| 152 | configuredTarget.getProvider(TestProvider.class).getTestParams().getTestStatusArtifacts(); |
| 153 | Artifact testStatusArtifact = Iterables.getOnlyElement(testStatusArtifacts); |
| 154 | TestRunnerAction action = (TestRunnerAction) getGeneratingAction(testStatusArtifact); |
| 155 | action.getTestLog().getPath().getParentDirectory().createDirectoryAndParents(); |
| 156 | return action; |
| 157 | } |
| 158 | |
ulfjack | 645fe0f | 2019-02-28 05:58:36 -0800 | [diff] [blame] | 159 | private List<SpawnResult> execute( |
| 160 | TestRunnerAction testRunnerAction, |
| 161 | ActionExecutionContext actionExecutionContext, |
| 162 | TestActionContext testActionContext) |
| 163 | throws ActionExecutionException, InterruptedException { |
| 164 | return testRunnerAction.execute(actionExecutionContext, testActionContext).spawnResults(); |
| 165 | } |
| 166 | |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 167 | @Test |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 168 | public void testRunTestOnce() throws Exception { |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 169 | ExecutionOptions executionOptions = ExecutionOptions.DEFAULTS; |
| 170 | Path tmpDirRoot = TestStrategy.getTmpRoot(rootDirectory, outputBase, executionOptions); |
ulfjack | 0588ba0 | 2018-02-15 10:40:26 -0800 | [diff] [blame] | 171 | BinTools binTools = BinTools.forUnitTesting(directories, analysisMock.getEmbeddedTools()); |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 172 | TestedStandaloneTestStrategy standaloneTestStrategy = |
| 173 | new TestedStandaloneTestStrategy(executionOptions, binTools, tmpDirRoot); |
| 174 | |
| 175 | // setup a test action |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 176 | scratch.file("standalone/simple_test.sh", "this does not get executed, it is mocked out"); |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 177 | scratch.file( |
| 178 | "standalone/BUILD", |
| 179 | "sh_test(", |
| 180 | " name = \"simple_test\",", |
| 181 | " size = \"small\",", |
| 182 | " srcs = [\"simple_test.sh\"],", |
| 183 | ")"); |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 184 | TestRunnerAction testRunnerAction = getTestAction("//standalone:simple_test"); |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 185 | |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 186 | SpawnResult expectedSpawnResult = |
ruperts | 02fb4bb | 2017-11-04 06:40:14 +0100 | [diff] [blame] | 187 | new SpawnResult.Builder() |
| 188 | .setStatus(Status.SUCCESS) |
| 189 | .setWallTime(Duration.ofMillis(10)) |
Googler | 4dd6f00 | 2018-03-27 08:15:39 -0700 | [diff] [blame] | 190 | .setRunnerName("test") |
ruperts | 02fb4bb | 2017-11-04 06:40:14 +0100 | [diff] [blame] | 191 | .build(); |
ulfjack | 895c43d | 2019-03-25 01:58:08 -0700 | [diff] [blame] | 192 | when(spawnActionContext.beginExecution(any(), any())) |
| 193 | .thenReturn(SpawnContinuation.immediate(expectedSpawnResult)); |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 194 | |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 195 | ActionExecutionContext actionExecutionContext = |
| 196 | new FakeActionExecutionContext(createTempOutErr(tmpDirRoot), spawnActionContext); |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 197 | |
| 198 | // actual StandaloneTestStrategy execution |
ruperts | 7967f33 | 2017-11-21 16:37:13 -0800 | [diff] [blame] | 199 | List<SpawnResult> spawnResults = |
ulfjack | 645fe0f | 2019-02-28 05:58:36 -0800 | [diff] [blame] | 200 | execute(testRunnerAction, actionExecutionContext, standaloneTestStrategy); |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 201 | |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 202 | assertThat(spawnResults).contains(expectedSpawnResult); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 203 | TestResult result = standaloneTestStrategy.postedResult; |
| 204 | assertThat(result).isNotNull(); |
| 205 | assertThat(result.isCached()).isFalse(); |
cpovirk | a4d3da6 | 2019-05-02 14:27:33 -0700 | [diff] [blame] | 206 | assertThat(result.getTestAction()).isSameInstanceAs(testRunnerAction); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 207 | assertThat(result.getData().getTestPassed()).isTrue(); |
| 208 | assertThat(result.getData().getRemotelyCached()).isFalse(); |
| 209 | assertThat(result.getData().getIsRemoteStrategy()).isFalse(); |
| 210 | assertThat(result.getData().getRunDurationMillis()).isEqualTo(10); |
| 211 | assertThat(result.getData().getTestTimesList()).containsExactly(10L); |
| 212 | TestAttempt attempt = |
| 213 | storedEvents |
| 214 | .getPosts() |
| 215 | .stream() |
| 216 | .filter(TestAttempt.class::isInstance) |
| 217 | .map(TestAttempt.class::cast) |
| 218 | .collect(MoreCollectors.onlyElement()); |
| 219 | assertThat(attempt.getExecutionInfo().getStrategy()).isEqualTo("test"); |
| 220 | assertThat(attempt.getExecutionInfo().getHostname()).isEqualTo(""); |
| 221 | } |
| 222 | |
| 223 | @Test |
| 224 | public void testRunFlakyTest() throws Exception { |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 225 | ExecutionOptions executionOptions = Options.getDefaults(ExecutionOptions.class); |
| 226 | // TODO(ulfjack): Update this test for split xml generation. |
| 227 | executionOptions.splitXmlGeneration = false; |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 228 | |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 229 | Path tmpDirRoot = TestStrategy.getTmpRoot(rootDirectory, outputBase, executionOptions); |
| 230 | BinTools binTools = BinTools.forUnitTesting(directories, analysisMock.getEmbeddedTools()); |
| 231 | TestedStandaloneTestStrategy standaloneTestStrategy = |
| 232 | new TestedStandaloneTestStrategy(executionOptions, binTools, tmpDirRoot); |
| 233 | |
| 234 | // setup a test action |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 235 | scratch.file("standalone/simple_test.sh", "this does not get executed, it is mocked out"); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 236 | scratch.file( |
| 237 | "standalone/BUILD", |
| 238 | "sh_test(", |
| 239 | " name = \"simple_test\",", |
| 240 | " size = \"small\",", |
| 241 | " srcs = [\"simple_test.sh\"],", |
| 242 | " flaky = True,", |
| 243 | ")"); |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 244 | TestRunnerAction testRunnerAction = getTestAction("//standalone:simple_test"); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 245 | |
| 246 | SpawnResult failSpawnResult = |
| 247 | new SpawnResult.Builder() |
| 248 | .setStatus(Status.NON_ZERO_EXIT) |
| 249 | .setExitCode(1) |
| 250 | .setWallTime(Duration.ofMillis(10)) |
| 251 | .setRunnerName("test") |
| 252 | .build(); |
| 253 | SpawnResult passSpawnResult = |
| 254 | new SpawnResult.Builder() |
| 255 | .setStatus(Status.SUCCESS) |
| 256 | .setWallTime(Duration.ofMillis(15)) |
| 257 | .setRunnerName("test") |
| 258 | .build(); |
ulfjack | 895c43d | 2019-03-25 01:58:08 -0700 | [diff] [blame] | 259 | when(spawnActionContext.beginExecution(any(), any())) |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 260 | .thenThrow(new SpawnExecException("test failed", failSpawnResult, false)) |
ulfjack | 895c43d | 2019-03-25 01:58:08 -0700 | [diff] [blame] | 261 | .thenReturn(SpawnContinuation.immediate(passSpawnResult)); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 262 | |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 263 | ActionExecutionContext actionExecutionContext = |
| 264 | new FakeActionExecutionContext(createTempOutErr(tmpDirRoot), spawnActionContext); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 265 | |
| 266 | // actual StandaloneTestStrategy execution |
| 267 | List<SpawnResult> spawnResults = |
ulfjack | 645fe0f | 2019-02-28 05:58:36 -0800 | [diff] [blame] | 268 | execute(testRunnerAction, actionExecutionContext, standaloneTestStrategy); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 269 | |
ulfjack | b70f0c1 | 2019-02-27 07:23:38 -0800 | [diff] [blame] | 270 | assertThat(spawnResults).containsExactly(failSpawnResult, passSpawnResult).inOrder(); |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 271 | |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 272 | TestResult result = standaloneTestStrategy.postedResult; |
| 273 | assertThat(result).isNotNull(); |
| 274 | assertThat(result.isCached()).isFalse(); |
cpovirk | a4d3da6 | 2019-05-02 14:27:33 -0700 | [diff] [blame] | 275 | assertThat(result.getTestAction()).isSameInstanceAs(testRunnerAction); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 276 | assertThat(result.getData().getStatus()).isEqualTo(BlazeTestStatus.FLAKY); |
| 277 | assertThat(result.getData().getTestPassed()).isTrue(); |
| 278 | assertThat(result.getData().getRemotelyCached()).isFalse(); |
| 279 | assertThat(result.getData().getIsRemoteStrategy()).isFalse(); |
| 280 | assertThat(result.getData().getRunDurationMillis()).isEqualTo(15L); |
| 281 | assertThat(result.getData().getTestTimesList()).containsExactly(10L, 15L); |
| 282 | List<TestAttempt> attempts = |
| 283 | storedEvents |
| 284 | .getPosts() |
| 285 | .stream() |
| 286 | .filter(TestAttempt.class::isInstance) |
| 287 | .map(TestAttempt.class::cast) |
| 288 | .collect(ImmutableList.toImmutableList()); |
| 289 | assertThat(attempts).hasSize(2); |
| 290 | TestAttempt failedAttempt = attempts.get(0); |
| 291 | assertThat(failedAttempt.getExecutionInfo().getStrategy()).isEqualTo("test"); |
| 292 | assertThat(failedAttempt.getExecutionInfo().getHostname()).isEqualTo(""); |
ulfjack | 81eda1d | 2018-06-07 08:38:21 -0700 | [diff] [blame] | 293 | assertThat(failedAttempt.getStatus()).isEqualTo(TestStatus.FAILED); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 294 | assertThat(failedAttempt.getExecutionInfo().getCachedRemotely()).isFalse(); |
| 295 | TestAttempt okAttempt = attempts.get(1); |
ulfjack | 81eda1d | 2018-06-07 08:38:21 -0700 | [diff] [blame] | 296 | assertThat(okAttempt.getStatus()).isEqualTo(TestStatus.PASSED); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 297 | assertThat(okAttempt.getExecutionInfo().getStrategy()).isEqualTo("test"); |
| 298 | assertThat(okAttempt.getExecutionInfo().getHostname()).isEqualTo(""); |
| 299 | } |
| 300 | |
| 301 | @Test |
| 302 | public void testRunTestRemotely() throws Exception { |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 303 | ExecutionOptions executionOptions = ExecutionOptions.DEFAULTS; |
| 304 | Path tmpDirRoot = TestStrategy.getTmpRoot(rootDirectory, outputBase, executionOptions); |
| 305 | BinTools binTools = BinTools.forUnitTesting(directories, analysisMock.getEmbeddedTools()); |
| 306 | TestedStandaloneTestStrategy standaloneTestStrategy = |
| 307 | new TestedStandaloneTestStrategy(executionOptions, binTools, tmpDirRoot); |
| 308 | |
| 309 | // setup a test action |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 310 | scratch.file("standalone/simple_test.sh", "this does not get executed, it is mocked out"); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 311 | scratch.file( |
| 312 | "standalone/BUILD", |
| 313 | "sh_test(", |
| 314 | " name = \"simple_test\",", |
| 315 | " size = \"small\",", |
| 316 | " srcs = [\"simple_test.sh\"],", |
| 317 | ")"); |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 318 | TestRunnerAction testRunnerAction = getTestAction("//standalone:simple_test"); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 319 | |
| 320 | SpawnResult expectedSpawnResult = |
| 321 | new SpawnResult.Builder() |
| 322 | .setStatus(Status.SUCCESS) |
| 323 | .setWallTime(Duration.ofMillis(10)) |
| 324 | .setRunnerName("remote") |
| 325 | .setExecutorHostname("a-remote-host") |
| 326 | .build(); |
ulfjack | 895c43d | 2019-03-25 01:58:08 -0700 | [diff] [blame] | 327 | when(spawnActionContext.beginExecution(any(), any())) |
| 328 | .thenReturn(SpawnContinuation.immediate(expectedSpawnResult)); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 329 | |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 330 | ActionExecutionContext actionExecutionContext = |
| 331 | new FakeActionExecutionContext(createTempOutErr(tmpDirRoot), spawnActionContext); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 332 | |
| 333 | // actual StandaloneTestStrategy execution |
| 334 | List<SpawnResult> spawnResults = |
ulfjack | 645fe0f | 2019-02-28 05:58:36 -0800 | [diff] [blame] | 335 | execute(testRunnerAction, actionExecutionContext, standaloneTestStrategy); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 336 | |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 337 | assertThat(spawnResults).contains(expectedSpawnResult); |
| 338 | |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 339 | TestResult result = standaloneTestStrategy.postedResult; |
| 340 | assertThat(result).isNotNull(); |
| 341 | assertThat(result.isCached()).isFalse(); |
cpovirk | a4d3da6 | 2019-05-02 14:27:33 -0700 | [diff] [blame] | 342 | assertThat(result.getTestAction()).isSameInstanceAs(testRunnerAction); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 343 | assertThat(result.getData().getTestPassed()).isTrue(); |
| 344 | assertThat(result.getData().getRemotelyCached()).isFalse(); |
| 345 | assertThat(result.getData().getIsRemoteStrategy()).isTrue(); |
| 346 | assertThat(result.getData().getRunDurationMillis()).isEqualTo(10); |
| 347 | assertThat(result.getData().getTestTimesList()).containsExactly(10L); |
| 348 | TestAttempt attempt = |
| 349 | storedEvents |
| 350 | .getPosts() |
| 351 | .stream() |
| 352 | .filter(TestAttempt.class::isInstance) |
| 353 | .map(TestAttempt.class::cast) |
| 354 | .collect(MoreCollectors.onlyElement()); |
ulfjack | 81eda1d | 2018-06-07 08:38:21 -0700 | [diff] [blame] | 355 | assertThat(attempt.getStatus()).isEqualTo(TestStatus.PASSED); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 356 | assertThat(attempt.getExecutionInfo().getStrategy()).isEqualTo("remote"); |
| 357 | assertThat(attempt.getExecutionInfo().getHostname()).isEqualTo("a-remote-host"); |
| 358 | } |
| 359 | |
| 360 | @Test |
| 361 | public void testRunRemotelyCachedTest() throws Exception { |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 362 | ExecutionOptions executionOptions = ExecutionOptions.DEFAULTS; |
| 363 | Path tmpDirRoot = TestStrategy.getTmpRoot(rootDirectory, outputBase, executionOptions); |
| 364 | BinTools binTools = BinTools.forUnitTesting(directories, analysisMock.getEmbeddedTools()); |
| 365 | TestedStandaloneTestStrategy standaloneTestStrategy = |
| 366 | new TestedStandaloneTestStrategy(executionOptions, binTools, tmpDirRoot); |
| 367 | |
| 368 | // setup a test action |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 369 | scratch.file("standalone/simple_test.sh", "this does not get executed, it is mocked out"); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 370 | scratch.file( |
| 371 | "standalone/BUILD", |
| 372 | "sh_test(", |
| 373 | " name = \"simple_test\",", |
| 374 | " size = \"small\",", |
| 375 | " srcs = [\"simple_test.sh\"],", |
| 376 | ")"); |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 377 | TestRunnerAction testRunnerAction = getTestAction("//standalone:simple_test"); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 378 | |
| 379 | SpawnResult expectedSpawnResult = |
| 380 | new SpawnResult.Builder() |
| 381 | .setStatus(Status.SUCCESS) |
| 382 | .setCacheHit(true) |
| 383 | .setWallTime(Duration.ofMillis(10)) |
| 384 | .setRunnerName("remote cache") |
| 385 | .build(); |
ulfjack | 895c43d | 2019-03-25 01:58:08 -0700 | [diff] [blame] | 386 | when(spawnActionContext.beginExecution(any(), any())) |
| 387 | .thenReturn(SpawnContinuation.immediate(expectedSpawnResult)); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 388 | |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 389 | ActionExecutionContext actionExecutionContext = |
| 390 | new FakeActionExecutionContext(createTempOutErr(tmpDirRoot), spawnActionContext); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 391 | |
| 392 | // actual StandaloneTestStrategy execution |
| 393 | List<SpawnResult> spawnResults = |
ulfjack | 645fe0f | 2019-02-28 05:58:36 -0800 | [diff] [blame] | 394 | execute(testRunnerAction, actionExecutionContext, standaloneTestStrategy); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 395 | |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 396 | // check that the rigged SpawnResult was returned |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 397 | assertThat(spawnResults).contains(expectedSpawnResult); |
| 398 | |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 399 | TestResult result = standaloneTestStrategy.postedResult; |
| 400 | assertThat(result).isNotNull(); |
| 401 | assertThat(result.isCached()).isFalse(); |
cpovirk | a4d3da6 | 2019-05-02 14:27:33 -0700 | [diff] [blame] | 402 | assertThat(result.getTestAction()).isSameInstanceAs(testRunnerAction); |
Benjamin Peterson | 1bbeadc | 2018-04-26 05:27:10 -0700 | [diff] [blame] | 403 | assertThat(result.getData().getTestPassed()).isTrue(); |
| 404 | assertThat(result.getData().getRemotelyCached()).isTrue(); |
| 405 | assertThat(result.getData().getIsRemoteStrategy()).isFalse(); |
| 406 | assertThat(result.getData().getRunDurationMillis()).isEqualTo(10); |
| 407 | assertThat(result.getData().getTestTimesList()).containsExactly(10L); |
| 408 | TestAttempt attempt = |
| 409 | storedEvents |
| 410 | .getPosts() |
| 411 | .stream() |
| 412 | .filter(TestAttempt.class::isInstance) |
| 413 | .map(TestAttempt.class::cast) |
| 414 | .collect(MoreCollectors.onlyElement()); |
| 415 | assertThat(attempt.getExecutionInfo().getStrategy()).isEqualTo("remote cache"); |
| 416 | assertThat(attempt.getExecutionInfo().getHostname()).isEqualTo(""); |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | @Test |
| 420 | public void testThatTestLogAndOutputAreReturned() throws Exception { |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 421 | ExecutionOptions executionOptions = Options.getDefaults(ExecutionOptions.class); |
| 422 | executionOptions.testOutput = TestOutputFormat.ERRORS; |
| 423 | Path tmpDirRoot = TestStrategy.getTmpRoot(rootDirectory, outputBase, executionOptions); |
ulfjack | 0588ba0 | 2018-02-15 10:40:26 -0800 | [diff] [blame] | 424 | BinTools binTools = BinTools.forUnitTesting(directories, analysisMock.getEmbeddedTools()); |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 425 | TestedStandaloneTestStrategy standaloneTestStrategy = |
| 426 | new TestedStandaloneTestStrategy(executionOptions, binTools, tmpDirRoot); |
| 427 | |
| 428 | // setup a test action |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 429 | scratch.file("standalone/failing_test.sh", "this does not get executed, it is mocked out"); |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 430 | scratch.file( |
| 431 | "standalone/BUILD", |
| 432 | "sh_test(", |
| 433 | " name = \"failing_test\",", |
| 434 | " size = \"small\",", |
| 435 | " srcs = [\"failing_test.sh\"],", |
| 436 | ")"); |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 437 | TestRunnerAction testRunnerAction = getTestAction("//standalone:failing_test"); |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 438 | |
| 439 | SpawnResult expectedSpawnResult = |
Googler | 4dd6f00 | 2018-03-27 08:15:39 -0700 | [diff] [blame] | 440 | new SpawnResult.Builder() |
| 441 | .setStatus(Status.NON_ZERO_EXIT) |
| 442 | .setExitCode(1) |
| 443 | .setRunnerName("test") |
| 444 | .build(); |
ulfjack | 895c43d | 2019-03-25 01:58:08 -0700 | [diff] [blame] | 445 | when(spawnActionContext.beginExecution(any(), any())) |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 446 | .thenAnswer( |
| 447 | (invocation) -> { |
Googler | f1bbbff | 2019-03-19 07:31:40 -0700 | [diff] [blame] | 448 | Spawn spawn = invocation.getArgument(0); |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 449 | if (spawn.getOutputFiles().size() != 1) { |
Googler | f1bbbff | 2019-03-19 07:31:40 -0700 | [diff] [blame] | 450 | ActionExecutionContext context = invocation.getArgument(1); |
| 451 | FileOutErr outErr = context.getFileOutErr(); |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 452 | try (OutputStream stream = outErr.getOutputStream()) { |
| 453 | stream.write("This will not appear in the test output: bla\n".getBytes(UTF_8)); |
| 454 | stream.write((TestLogHelper.HEADER_DELIMITER + "\n").getBytes(UTF_8)); |
| 455 | stream.write("This will appear in the test output: foo\n".getBytes(UTF_8)); |
| 456 | } |
| 457 | throw new SpawnExecException( |
| 458 | "Failure!!", |
| 459 | expectedSpawnResult, |
| 460 | /*forciblyRunRemotely=*/ false, |
| 461 | /*catastrophe=*/ false); |
| 462 | } else { |
ulfjack | 895c43d | 2019-03-25 01:58:08 -0700 | [diff] [blame] | 463 | return SpawnContinuation.immediate( |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 464 | new SpawnResult.Builder() |
| 465 | .setStatus(Status.SUCCESS) |
| 466 | .setRunnerName("test") |
| 467 | .build()); |
| 468 | } |
| 469 | }); |
| 470 | |
| 471 | FileOutErr outErr = createTempOutErr(tmpDirRoot); |
| 472 | ActionExecutionContext actionExecutionContext = |
| 473 | new FakeActionExecutionContext(outErr, spawnActionContext); |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 474 | |
| 475 | // actual StandaloneTestStrategy execution |
ruperts | 7967f33 | 2017-11-21 16:37:13 -0800 | [diff] [blame] | 476 | List<SpawnResult> spawnResults = |
ulfjack | 645fe0f | 2019-02-28 05:58:36 -0800 | [diff] [blame] | 477 | execute(testRunnerAction, actionExecutionContext, standaloneTestStrategy); |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 478 | |
| 479 | // check that the rigged SpawnResult was returned |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 480 | assertThat(spawnResults).contains(expectedSpawnResult); |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 481 | // check that the test log contains all the output |
| 482 | try { |
| 483 | String logData = FileSystemUtils.readContent(testRunnerAction.getTestLog().getPath(), UTF_8); |
| 484 | assertThat(logData).contains("bla"); |
| 485 | assertThat(logData).contains(TestLogHelper.HEADER_DELIMITER); |
| 486 | assertThat(logData).contains("foo"); |
| 487 | } catch (IOException e) { |
| 488 | fail("Test log missing: " + testRunnerAction.getTestLog().getPath()); |
| 489 | } |
| 490 | // check that the test stdout contains all the expected output |
| 491 | outErr.close(); // Create the output files. |
| 492 | try { |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 493 | String outData = FileSystemUtils.readContent(outErr.getOutputPath(), UTF_8); |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 494 | assertThat(outData) |
| 495 | .contains("==================== Test output for //standalone:failing_test:"); |
| 496 | assertThat(outData).doesNotContain("bla"); |
| 497 | assertThat(outData).doesNotContain(TestLogHelper.HEADER_DELIMITER); |
| 498 | assertThat(outData).contains("foo"); |
| 499 | assertThat(outData) |
| 500 | .contains( |
| 501 | "================================================================================"); |
| 502 | } catch (IOException e) { |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 503 | fail("Test stdout file missing: " + outErr.getOutputPath()); |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 504 | } |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 505 | assertThat(outErr.getErrorPath().exists()).isFalse(); |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | @Test |
ulfjack | 0858ae1 | 2018-07-27 02:37:53 -0700 | [diff] [blame] | 509 | public void testThatTestLogAndOutputAreReturnedWithSplitXmlGeneration() throws Exception { |
ulfjack | 0858ae1 | 2018-07-27 02:37:53 -0700 | [diff] [blame] | 510 | ExecutionOptions executionOptions = Options.getDefaults(ExecutionOptions.class); |
| 511 | executionOptions.testOutput = TestOutputFormat.ERRORS; |
| 512 | executionOptions.splitXmlGeneration = true; |
| 513 | Path tmpDirRoot = TestStrategy.getTmpRoot(rootDirectory, outputBase, executionOptions); |
| 514 | BinTools binTools = BinTools.forUnitTesting(directories, analysisMock.getEmbeddedTools()); |
| 515 | TestedStandaloneTestStrategy standaloneTestStrategy = |
| 516 | new TestedStandaloneTestStrategy(executionOptions, binTools, tmpDirRoot); |
| 517 | |
| 518 | // setup a test action |
ulfjack | 0858ae1 | 2018-07-27 02:37:53 -0700 | [diff] [blame] | 519 | scratch.file("standalone/failing_test.sh", "this does not get executed, it is mocked out"); |
ulfjack | 0858ae1 | 2018-07-27 02:37:53 -0700 | [diff] [blame] | 520 | scratch.file( |
| 521 | "standalone/BUILD", |
| 522 | "sh_test(", |
| 523 | " name = \"failing_test\",", |
| 524 | " size = \"small\",", |
| 525 | " srcs = [\"failing_test.sh\"],", |
Jakob Buchgraber | 803801d | 2019-03-21 09:22:58 -0700 | [diff] [blame] | 526 | " tags = [\"local\"],", |
ulfjack | 0858ae1 | 2018-07-27 02:37:53 -0700 | [diff] [blame] | 527 | ")"); |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 528 | TestRunnerAction testRunnerAction = getTestAction("//standalone:failing_test"); |
ulfjack | 0858ae1 | 2018-07-27 02:37:53 -0700 | [diff] [blame] | 529 | |
| 530 | SpawnResult testSpawnResult = |
| 531 | new SpawnResult.Builder() |
| 532 | .setStatus(Status.NON_ZERO_EXIT) |
| 533 | .setExitCode(1) |
| 534 | .setRunnerName("test") |
| 535 | .build(); |
ulfjack | 0858ae1 | 2018-07-27 02:37:53 -0700 | [diff] [blame] | 536 | SpawnResult xmlGeneratorSpawnResult = |
| 537 | new SpawnResult.Builder() |
| 538 | .setStatus(Status.SUCCESS) |
| 539 | .setRunnerName("test") |
| 540 | .build(); |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 541 | List<FileOutErr> called = new ArrayList<>(); |
ulfjack | 895c43d | 2019-03-25 01:58:08 -0700 | [diff] [blame] | 542 | when(spawnActionContext.beginExecution(any(), any())) |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 543 | .thenAnswer( |
| 544 | (invocation) -> { |
Googler | f1bbbff | 2019-03-19 07:31:40 -0700 | [diff] [blame] | 545 | Spawn spawn = invocation.getArgument(0); |
Jakob Buchgraber | 803801d | 2019-03-21 09:22:58 -0700 | [diff] [blame] | 546 | // Test that both spawns have the local tag attached as a execution info |
| 547 | assertThat(spawn.getExecutionInfo()).containsKey("local"); |
Googler | f1bbbff | 2019-03-19 07:31:40 -0700 | [diff] [blame] | 548 | ActionExecutionContext context = invocation.getArgument(1); |
| 549 | FileOutErr outErr = context.getFileOutErr(); |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 550 | called.add(outErr); |
| 551 | if (spawn.getOutputFiles().size() != 1) { |
| 552 | try (OutputStream stream = outErr.getOutputStream()) { |
| 553 | stream.write("This will not appear in the test output: bla\n".getBytes(UTF_8)); |
| 554 | stream.write((TestLogHelper.HEADER_DELIMITER + "\n").getBytes(UTF_8)); |
| 555 | stream.write("This will appear in the test output: foo\n".getBytes(UTF_8)); |
| 556 | } |
| 557 | throw new SpawnExecException( |
| 558 | "Failure!!", |
| 559 | testSpawnResult, |
| 560 | /*forciblyRunRemotely=*/ false, |
| 561 | /*catastrophe=*/ false); |
| 562 | } else { |
ulfjack | 3d8e51c | 2018-11-29 06:54:29 -0800 | [diff] [blame] | 563 | String testName = |
| 564 | OS.getCurrent() == OS.WINDOWS |
| 565 | ? "standalone/failing_test.exe" |
| 566 | : "standalone/failing_test"; |
| 567 | assertThat(spawn.getEnvironment()).containsEntry("TEST_BINARY", testName); |
ulfjack | 895c43d | 2019-03-25 01:58:08 -0700 | [diff] [blame] | 568 | return SpawnContinuation.immediate(xmlGeneratorSpawnResult); |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 569 | } |
| 570 | }); |
| 571 | |
| 572 | FileOutErr outErr = createTempOutErr(tmpDirRoot); |
| 573 | ActionExecutionContext actionExecutionContext = |
| 574 | new FakeActionExecutionContext(outErr, spawnActionContext); |
ulfjack | 0858ae1 | 2018-07-27 02:37:53 -0700 | [diff] [blame] | 575 | |
| 576 | // actual StandaloneTestStrategy execution |
| 577 | List<SpawnResult> spawnResults = |
ulfjack | 645fe0f | 2019-02-28 05:58:36 -0800 | [diff] [blame] | 578 | execute(testRunnerAction, actionExecutionContext, standaloneTestStrategy); |
ulfjack | 0858ae1 | 2018-07-27 02:37:53 -0700 | [diff] [blame] | 579 | |
| 580 | // check that the rigged SpawnResult was returned |
| 581 | assertThat(spawnResults).containsExactly(testSpawnResult, xmlGeneratorSpawnResult); |
| 582 | // check that the test log contains all the output |
| 583 | String logData = FileSystemUtils.readContent(testRunnerAction.getTestLog().getPath(), UTF_8); |
| 584 | assertThat(logData).contains("bla"); |
| 585 | assertThat(logData).contains(TestLogHelper.HEADER_DELIMITER); |
| 586 | assertThat(logData).contains("foo"); |
| 587 | // check that the test stdout contains all the expected output |
| 588 | outErr.close(); // Create the output files. |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 589 | String outData = FileSystemUtils.readContent(outErr.getOutputPath(), UTF_8); |
ulfjack | 0858ae1 | 2018-07-27 02:37:53 -0700 | [diff] [blame] | 590 | assertThat(outData) |
| 591 | .contains("==================== Test output for //standalone:failing_test:"); |
| 592 | assertThat(outData).doesNotContain("bla"); |
| 593 | assertThat(outData).doesNotContain(TestLogHelper.HEADER_DELIMITER); |
| 594 | assertThat(outData).contains("foo"); |
| 595 | assertThat(outData) |
| 596 | .contains( |
| 597 | "================================================================================"); |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 598 | assertThat(outErr.getErrorPath().exists()).isFalse(); |
| 599 | assertThat(called).hasSize(2); |
| 600 | assertThat(called).containsNoDuplicates(); |
ulfjack | 0858ae1 | 2018-07-27 02:37:53 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | @Test |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 604 | public void testEmptyOutputCreatesEmptyLogFile() throws Exception { |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 605 | ExecutionOptions executionOptions = Options.getDefaults(ExecutionOptions.class); |
| 606 | executionOptions.testOutput = TestOutputFormat.ALL; |
| 607 | Path tmpDirRoot = TestStrategy.getTmpRoot(rootDirectory, outputBase, executionOptions); |
ulfjack | 0588ba0 | 2018-02-15 10:40:26 -0800 | [diff] [blame] | 608 | BinTools binTools = BinTools.forUnitTesting(directories, analysisMock.getEmbeddedTools()); |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 609 | TestedStandaloneTestStrategy standaloneTestStrategy = |
| 610 | new TestedStandaloneTestStrategy(executionOptions, binTools, tmpDirRoot); |
| 611 | |
| 612 | // setup a test action |
| 613 | scratch.file("standalone/empty_test.sh", "this does not get executed, it is mocked out"); |
| 614 | scratch.file( |
| 615 | "standalone/BUILD", |
| 616 | "sh_test(", |
| 617 | " name = \"empty_test\",", |
| 618 | " size = \"small\",", |
| 619 | " srcs = [\"empty_test.sh\"],", |
| 620 | ")"); |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 621 | TestRunnerAction testRunnerAction = getTestAction("//standalone:empty_test"); |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 622 | |
Googler | 4dd6f00 | 2018-03-27 08:15:39 -0700 | [diff] [blame] | 623 | SpawnResult expectedSpawnResult = |
| 624 | new SpawnResult.Builder().setStatus(Status.SUCCESS).setRunnerName("test").build(); |
ulfjack | 895c43d | 2019-03-25 01:58:08 -0700 | [diff] [blame] | 625 | when(spawnActionContext.beginExecution(any(), any())) |
| 626 | .thenReturn(SpawnContinuation.immediate(expectedSpawnResult)); |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 627 | |
| 628 | FileOutErr outErr = createTempOutErr(tmpDirRoot); |
| 629 | ActionExecutionContext actionExecutionContext = |
| 630 | new FakeActionExecutionContext(outErr, spawnActionContext); |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 631 | |
| 632 | // actual StandaloneTestStrategy execution |
ruperts | 7967f33 | 2017-11-21 16:37:13 -0800 | [diff] [blame] | 633 | List<SpawnResult> spawnResults = |
ulfjack | 645fe0f | 2019-02-28 05:58:36 -0800 | [diff] [blame] | 634 | execute(testRunnerAction, actionExecutionContext, standaloneTestStrategy); |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 635 | |
| 636 | // check that the rigged SpawnResult was returned |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 637 | assertThat(spawnResults).contains(expectedSpawnResult); |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 638 | // check that the test log contains all the output |
| 639 | try { |
| 640 | String logData = FileSystemUtils.readContent(testRunnerAction.getTestLog().getPath(), UTF_8); |
| 641 | assertThat(logData).isEmpty(); |
| 642 | } catch (IOException e) { |
| 643 | fail("Test log missing: " + testRunnerAction.getTestLog().getPath()); |
| 644 | } |
| 645 | // check that the test stdout contains all the expected output |
| 646 | outErr.close(); // Create the output files. |
| 647 | try { |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 648 | String outData = FileSystemUtils.readContent(outErr.getOutputPath(), UTF_8); |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 649 | String emptyOutput = |
| 650 | "==================== Test output for //standalone:empty_test:(\\s)*" |
| 651 | + "================================================================================(\\s)*"; |
| 652 | assertThat(outData).matches(emptyOutput); |
| 653 | } catch (IOException e) { |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 654 | fail("Test stdout file missing: " + outErr.getOutputPath()); |
olaola | c35fe50 | 2017-10-17 04:09:07 +0200 | [diff] [blame] | 655 | } |
ulfjack | 14bb5cd | 2018-11-29 04:30:26 -0800 | [diff] [blame] | 656 | assertThat(outErr.getErrorPath().exists()).isFalse(); |
shahan | 18726b7 | 2018-03-15 14:18:46 -0700 | [diff] [blame] | 657 | } |
ulfjack | 0ff19c6 | 2019-05-21 08:50:23 -0700 | [diff] [blame^] | 658 | |
| 659 | @Test |
| 660 | public void testAppendStdErrDoesNotBusyLoop() throws Exception { |
| 661 | ExecutionOptions executionOptions = Options.getDefaults(ExecutionOptions.class); |
| 662 | executionOptions.testOutput = TestOutputFormat.ALL; |
| 663 | Path tmpDirRoot = TestStrategy.getTmpRoot(rootDirectory, outputBase, executionOptions); |
| 664 | BinTools binTools = BinTools.forUnitTesting(directories, analysisMock.getEmbeddedTools()); |
| 665 | TestedStandaloneTestStrategy standaloneTestStrategy = |
| 666 | new TestedStandaloneTestStrategy(executionOptions, binTools, tmpDirRoot); |
| 667 | |
| 668 | // setup a test action |
| 669 | scratch.file("standalone/empty_test.sh", "this does not get executed, it is mocked out"); |
| 670 | scratch.file( |
| 671 | "standalone/BUILD", |
| 672 | "sh_test(", |
| 673 | " name = \"empty_test\",", |
| 674 | " size = \"small\",", |
| 675 | " srcs = [\"empty_test.sh\"],", |
| 676 | ")"); |
| 677 | TestRunnerAction testRunnerAction = getTestAction("//standalone:empty_test"); |
| 678 | |
| 679 | SpawnResult expectedSpawnResult = |
| 680 | new SpawnResult.Builder().setStatus(Status.SUCCESS).setRunnerName("test").build(); |
| 681 | when(spawnActionContext.beginExecution(any(), any())) |
| 682 | .then( |
| 683 | (invocation) -> { |
| 684 | ((ActionExecutionContext) invocation.getArgument(1)).getFileOutErr().printErr("Foo"); |
| 685 | return SpawnContinuation.immediate(expectedSpawnResult); |
| 686 | }); |
| 687 | |
| 688 | FileOutErr outErr = createTempOutErr(tmpDirRoot); |
| 689 | ActionExecutionContext actionExecutionContext = |
| 690 | new FakeActionExecutionContext(outErr, spawnActionContext); |
| 691 | |
| 692 | // actual StandaloneTestStrategy execution |
| 693 | execute(testRunnerAction, actionExecutionContext, standaloneTestStrategy); |
| 694 | |
| 695 | // check that the test stdout contains all the expected output |
| 696 | try { |
| 697 | String outData = FileSystemUtils.readContent(outErr.getOutputPath(), UTF_8); |
| 698 | assertThat(outData).contains("Foo"); |
| 699 | } catch (IOException e) { |
| 700 | fail("Test stdout file missing: " + outErr.getOutputPath()); |
| 701 | } |
| 702 | } |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 703 | } |