ulfjack | 9274cba | 2017-08-11 23:19:48 +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 | package com.google.devtools.build.lib.remote; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
| 17 | import static org.mockito.Matchers.any; |
| 18 | import static org.mockito.Matchers.eq; |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 19 | import static org.mockito.Mockito.doThrow; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 20 | import static org.mockito.Mockito.never; |
| 21 | import static org.mockito.Mockito.verify; |
| 22 | import static org.mockito.Mockito.when; |
| 23 | |
olaola | f0aa55d | 2018-08-16 08:51:06 -0700 | [diff] [blame] | 24 | import build.bazel.remote.execution.v2.Action; |
| 25 | import build.bazel.remote.execution.v2.ActionResult; |
| 26 | import build.bazel.remote.execution.v2.Command; |
| 27 | import build.bazel.remote.execution.v2.RequestMetadata; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 28 | import com.google.common.collect.ImmutableList; |
| 29 | import com.google.common.collect.ImmutableMap; |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 30 | import com.google.common.eventbus.EventBus; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 31 | import com.google.devtools.build.lib.actions.ActionInput; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 32 | import com.google.devtools.build.lib.actions.ActionInputHelper; |
| 33 | import com.google.devtools.build.lib.actions.Artifact; |
| 34 | import com.google.devtools.build.lib.actions.Artifact.ArtifactExpander; |
olaola | a22d0e9 | 2017-12-11 07:53:15 -0800 | [diff] [blame] | 35 | import com.google.devtools.build.lib.actions.ExecutionRequirements; |
shahan | 499503b | 2018-06-07 18:57:07 -0700 | [diff] [blame] | 36 | import com.google.devtools.build.lib.actions.MetadataProvider; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 37 | import com.google.devtools.build.lib.actions.ResourceSet; |
| 38 | import com.google.devtools.build.lib.actions.SimpleSpawn; |
ruperts | da40fbf | 2017-09-22 05:59:42 +0200 | [diff] [blame] | 39 | import com.google.devtools.build.lib.actions.SpawnResult; |
| 40 | import com.google.devtools.build.lib.actions.SpawnResult.Status; |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 41 | import com.google.devtools.build.lib.clock.JavaClock; |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 42 | import com.google.devtools.build.lib.events.Event; |
| 43 | import com.google.devtools.build.lib.events.EventKind; |
| 44 | import com.google.devtools.build.lib.events.Reporter; |
| 45 | import com.google.devtools.build.lib.events.StoredEventHandler; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 46 | import com.google.devtools.build.lib.exec.SpawnCache.CacheHandle; |
| 47 | import com.google.devtools.build.lib.exec.SpawnInputExpander; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 48 | import com.google.devtools.build.lib.exec.SpawnRunner.ProgressStatus; |
tomlu | 29e306d | 2018-04-19 05:41:44 -0700 | [diff] [blame] | 49 | import com.google.devtools.build.lib.exec.SpawnRunner.SpawnExecutionContext; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 50 | import com.google.devtools.build.lib.exec.util.FakeOwner; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 51 | import com.google.devtools.build.lib.remote.TreeNodeRepository.TreeNode; |
Googler | 922d1e6 | 2018-03-05 14:49:00 -0800 | [diff] [blame] | 52 | import com.google.devtools.build.lib.remote.util.DigestUtil; |
| 53 | import com.google.devtools.build.lib.remote.util.DigestUtil.ActionKey; |
| 54 | import com.google.devtools.build.lib.remote.util.TracingMetadataUtils; |
Benjamin Peterson | 7e1c7bc | 2018-05-03 04:30:19 -0700 | [diff] [blame] | 55 | import com.google.devtools.build.lib.util.Pair; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 56 | import com.google.devtools.build.lib.util.io.FileOutErr; |
ccalvarin | bda12a1 | 2018-06-21 18:57:26 -0700 | [diff] [blame] | 57 | import com.google.devtools.build.lib.vfs.DigestHashFunction; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 58 | import com.google.devtools.build.lib.vfs.FileSystem; |
| 59 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
| 60 | import com.google.devtools.build.lib.vfs.Path; |
| 61 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 62 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
| 63 | import com.google.devtools.common.options.Options; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 64 | import java.io.IOException; |
| 65 | import java.time.Duration; |
Benjamin Peterson | 7e1c7bc | 2018-05-03 04:30:19 -0700 | [diff] [blame] | 66 | import java.util.ArrayList; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 67 | import java.util.Collection; |
Benjamin Peterson | 7e1c7bc | 2018-05-03 04:30:19 -0700 | [diff] [blame] | 68 | import java.util.List; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 69 | import java.util.SortedMap; |
| 70 | import org.junit.Before; |
| 71 | import org.junit.Test; |
| 72 | import org.junit.runner.RunWith; |
| 73 | import org.junit.runners.JUnit4; |
| 74 | import org.mockito.Mock; |
olaola | ba8b0b3 | 2017-10-20 09:48:56 +0200 | [diff] [blame] | 75 | import org.mockito.Mockito; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 76 | import org.mockito.MockitoAnnotations; |
olaola | ba8b0b3 | 2017-10-20 09:48:56 +0200 | [diff] [blame] | 77 | import org.mockito.invocation.InvocationOnMock; |
| 78 | import org.mockito.stubbing.Answer; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 79 | |
| 80 | /** Tests for {@link RemoteSpawnCache}. */ |
| 81 | @RunWith(JUnit4.class) |
| 82 | public class RemoteSpawnCacheTest { |
| 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 | }; |
| 90 | |
| 91 | private FileSystem fs; |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 92 | private DigestUtil digestUtil; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 93 | private Path execRoot; |
| 94 | private SimpleSpawn simpleSpawn; |
| 95 | private FakeActionInputFileCache fakeFileCache; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 96 | @Mock private AbstractRemoteActionCache remoteCache; |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 97 | private RemoteSpawnCache cache; |
| 98 | private FileOutErr outErr; |
Benjamin Peterson | 7e1c7bc | 2018-05-03 04:30:19 -0700 | [diff] [blame] | 99 | private final List<Pair<ProgressStatus, String>> progressUpdates = new ArrayList(); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 100 | |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 101 | private StoredEventHandler eventHandler = new StoredEventHandler(); |
| 102 | |
tomlu | 29e306d | 2018-04-19 05:41:44 -0700 | [diff] [blame] | 103 | private final SpawnExecutionContext simplePolicy = |
| 104 | new SpawnExecutionContext() { |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 105 | @Override |
| 106 | public int getId() { |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void prefetchInputs() { |
| 112 | // CachedLocalSpawnRunner should never prefetch itself, though the nested SpawnRunner may. |
| 113 | throw new UnsupportedOperationException(); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void lockOutputFiles() throws InterruptedException { |
| 118 | throw new UnsupportedOperationException(); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public boolean speculating() { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | @Override |
shahan | 499503b | 2018-06-07 18:57:07 -0700 | [diff] [blame] | 127 | public MetadataProvider getMetadataProvider() { |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 128 | return fakeFileCache; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public ArtifactExpander getArtifactExpander() { |
| 133 | throw new UnsupportedOperationException(); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public Duration getTimeout() { |
| 138 | return Duration.ZERO; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public FileOutErr getFileOutErr() { |
| 143 | return outErr; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public SortedMap<PathFragment, ActionInput> getInputMapping() throws IOException { |
tomlu | 1a19b62 | 2018-01-11 15:17:28 -0800 | [diff] [blame] | 148 | return new SpawnInputExpander(execRoot, /*strict*/ false) |
kush | 2ce45a2 | 2018-05-02 14:15:37 -0700 | [diff] [blame] | 149 | .getInputMapping(simpleSpawn, SIMPLE_ARTIFACT_EXPANDER, fakeFileCache); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void report(ProgressStatus state, String name) { |
Benjamin Peterson | 7e1c7bc | 2018-05-03 04:30:19 -0700 | [diff] [blame] | 154 | progressUpdates.add(Pair.of(state, name)); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 155 | } |
| 156 | }; |
| 157 | |
| 158 | @Before |
| 159 | public final void setUp() throws Exception { |
| 160 | MockitoAnnotations.initMocks(this); |
ccalvarin | bda12a1 | 2018-06-21 18:57:26 -0700 | [diff] [blame] | 161 | fs = new InMemoryFileSystem(new JavaClock(), DigestHashFunction.SHA256); |
| 162 | digestUtil = new DigestUtil(DigestHashFunction.SHA256); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 163 | execRoot = fs.getPath("/exec/root"); |
| 164 | FileSystemUtils.createDirectoryAndParents(execRoot); |
| 165 | fakeFileCache = new FakeActionInputFileCache(execRoot); |
| 166 | simpleSpawn = |
| 167 | new SimpleSpawn( |
| 168 | new FakeOwner("Mnemonic", "Progress Message"), |
| 169 | ImmutableList.of("/bin/echo", "Hi!"), |
| 170 | ImmutableMap.of("VARIABLE", "value"), |
| 171 | /*executionInfo=*/ ImmutableMap.<String, String>of(), |
| 172 | /*inputs=*/ ImmutableList.of(ActionInputHelper.fromPath("input")), |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 173 | /*outputs=*/ ImmutableList.of(ActionInputHelper.fromPath("/random/file")), |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 174 | ResourceSet.ZERO); |
| 175 | |
| 176 | Path stdout = fs.getPath("/tmp/stdout"); |
| 177 | Path stderr = fs.getPath("/tmp/stderr"); |
| 178 | FileSystemUtils.createDirectoryAndParents(stdout.getParentDirectory()); |
| 179 | FileSystemUtils.createDirectoryAndParents(stderr.getParentDirectory()); |
| 180 | outErr = new FileOutErr(stdout, stderr); |
| 181 | RemoteOptions options = Options.getDefaults(RemoteOptions.class); |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 182 | Reporter reporter = new Reporter(new EventBus()); |
| 183 | eventHandler = new StoredEventHandler(); |
| 184 | reporter.addHandler(eventHandler); |
olaola | 6f32d5a | 2017-09-20 17:12:19 +0200 | [diff] [blame] | 185 | cache = |
| 186 | new RemoteSpawnCache( |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 187 | execRoot, |
| 188 | options, |
| 189 | remoteCache, |
| 190 | "build-req-id", |
| 191 | "command-id", |
buchgr | 559a07d | 2017-11-30 11:09:35 -0800 | [diff] [blame] | 192 | reporter, |
| 193 | digestUtil); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 194 | fakeFileCache.createScratchInput(simpleSpawn.getInputFiles().get(0), "xyz"); |
| 195 | } |
| 196 | |
| 197 | @SuppressWarnings("unchecked") |
| 198 | @Test |
| 199 | public void cacheHit() throws Exception { |
| 200 | ActionResult actionResult = ActionResult.getDefaultInstance(); |
olaola | ba8b0b3 | 2017-10-20 09:48:56 +0200 | [diff] [blame] | 201 | when(remoteCache.getCachedActionResult(any(ActionKey.class))) |
| 202 | .thenAnswer( |
| 203 | new Answer<ActionResult>() { |
| 204 | @Override |
| 205 | public ActionResult answer(InvocationOnMock invocation) { |
| 206 | RequestMetadata meta = TracingMetadataUtils.fromCurrentContext(); |
| 207 | assertThat(meta.getCorrelatedInvocationsId()).isEqualTo("build-req-id"); |
| 208 | assertThat(meta.getToolInvocationId()).isEqualTo("command-id"); |
| 209 | return actionResult; |
| 210 | } |
| 211 | }); |
| 212 | Mockito.doAnswer( |
| 213 | new Answer<Void>() { |
| 214 | @Override |
| 215 | public Void answer(InvocationOnMock invocation) { |
| 216 | RequestMetadata meta = TracingMetadataUtils.fromCurrentContext(); |
| 217 | assertThat(meta.getCorrelatedInvocationsId()).isEqualTo("build-req-id"); |
| 218 | assertThat(meta.getToolInvocationId()).isEqualTo("command-id"); |
| 219 | return null; |
| 220 | } |
| 221 | }) |
| 222 | .when(remoteCache) |
| 223 | .download(actionResult, execRoot, outErr); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 224 | |
| 225 | CacheHandle entry = cache.lookup(simpleSpawn, simplePolicy); |
| 226 | assertThat(entry.hasResult()).isTrue(); |
| 227 | SpawnResult result = entry.getResult(); |
| 228 | // All other methods on RemoteActionCache have side effects, so we verify all of them. |
| 229 | verify(remoteCache).download(actionResult, execRoot, outErr); |
| 230 | verify(remoteCache, never()) |
| 231 | .ensureInputsPresent( |
| 232 | any(TreeNodeRepository.class), |
| 233 | any(Path.class), |
| 234 | any(TreeNode.class), |
olaola | f0aa55d | 2018-08-16 08:51:06 -0700 | [diff] [blame] | 235 | any(Action.class), |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 236 | any(Command.class)); |
| 237 | verify(remoteCache, never()) |
| 238 | .upload( |
olaola | 7744b86 | 2017-09-18 23:04:33 +0200 | [diff] [blame] | 239 | any(ActionKey.class), |
olaola | f0aa55d | 2018-08-16 08:51:06 -0700 | [diff] [blame] | 240 | any(Action.class), |
| 241 | any(Command.class), |
olaola | 7744b86 | 2017-09-18 23:04:33 +0200 | [diff] [blame] | 242 | any(Path.class), |
| 243 | any(Collection.class), |
| 244 | any(FileOutErr.class), |
| 245 | any(Boolean.class)); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 246 | assertThat(result.setupSuccess()).isTrue(); |
| 247 | assertThat(result.exitCode()).isEqualTo(0); |
olaola | e5c9bdf | 2018-02-20 05:29:19 -0800 | [diff] [blame] | 248 | assertThat(result.isCacheHit()).isTrue(); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 249 | // We expect the CachedLocalSpawnRunner to _not_ write to outErr at all. |
| 250 | assertThat(outErr.hasRecordedOutput()).isFalse(); |
| 251 | assertThat(outErr.hasRecordedStderr()).isFalse(); |
Benjamin Peterson | 7e1c7bc | 2018-05-03 04:30:19 -0700 | [diff] [blame] | 252 | assertThat(progressUpdates) |
| 253 | .containsExactly(Pair.of(ProgressStatus.CHECKING_CACHE, "remote-cache")); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | @Test |
| 257 | public void cacheMiss() throws Exception { |
| 258 | CacheHandle entry = cache.lookup(simpleSpawn, simplePolicy); |
| 259 | assertThat(entry.hasResult()).isFalse(); |
Googler | 4dd6f00 | 2018-03-27 08:15:39 -0700 | [diff] [blame] | 260 | SpawnResult result = |
| 261 | new SpawnResult.Builder() |
| 262 | .setExitCode(0) |
| 263 | .setStatus(Status.SUCCESS) |
| 264 | .setRunnerName("test") |
| 265 | .build(); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 266 | ImmutableList<Path> outputFiles = ImmutableList.of(fs.getPath("/random/file")); |
olaola | ba8b0b3 | 2017-10-20 09:48:56 +0200 | [diff] [blame] | 267 | Mockito.doAnswer( |
| 268 | new Answer<Void>() { |
| 269 | @Override |
| 270 | public Void answer(InvocationOnMock invocation) { |
| 271 | RequestMetadata meta = TracingMetadataUtils.fromCurrentContext(); |
| 272 | assertThat(meta.getCorrelatedInvocationsId()).isEqualTo("build-req-id"); |
| 273 | assertThat(meta.getToolInvocationId()).isEqualTo("command-id"); |
| 274 | return null; |
| 275 | } |
| 276 | }) |
| 277 | .when(remoteCache) |
olaola | f0aa55d | 2018-08-16 08:51:06 -0700 | [diff] [blame] | 278 | .upload( |
| 279 | any(ActionKey.class), |
| 280 | any(Action.class), |
| 281 | any(Command.class), |
| 282 | any(Path.class), |
| 283 | eq(outputFiles), |
| 284 | eq(outErr), |
| 285 | eq(true)); |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 286 | entry.store(result); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 287 | verify(remoteCache) |
olaola | f0aa55d | 2018-08-16 08:51:06 -0700 | [diff] [blame] | 288 | .upload( |
| 289 | any(ActionKey.class), |
| 290 | any(Action.class), |
| 291 | any(Command.class), |
| 292 | any(Path.class), |
| 293 | eq(outputFiles), |
| 294 | eq(outErr), |
| 295 | eq(true)); |
Benjamin Peterson | 7e1c7bc | 2018-05-03 04:30:19 -0700 | [diff] [blame] | 296 | assertThat(progressUpdates) |
| 297 | .containsExactly(Pair.of(ProgressStatus.CHECKING_CACHE, "remote-cache")); |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 298 | } |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 299 | |
| 300 | @Test |
olaola | a22d0e9 | 2017-12-11 07:53:15 -0800 | [diff] [blame] | 301 | public void noCacheSpawns() throws Exception { |
| 302 | // Checks that spawns that have mayBeCached false are not looked up in the remote cache, |
| 303 | // and also that their result is not uploaded to the remote cache. The artifacts, however, |
| 304 | // are uploaded. |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 305 | SimpleSpawn uncacheableSpawn = |
| 306 | new SimpleSpawn( |
| 307 | new FakeOwner("foo", "bar"), |
| 308 | /*arguments=*/ ImmutableList.of(), |
| 309 | /*environment=*/ ImmutableMap.of(), |
| 310 | ImmutableMap.of(ExecutionRequirements.NO_CACHE, ""), |
| 311 | /*inputs=*/ ImmutableList.of(), |
| 312 | /*outputs=*/ ImmutableList.of(ActionInputHelper.fromPath("/random/file")), |
| 313 | ResourceSet.ZERO); |
olaola | a22d0e9 | 2017-12-11 07:53:15 -0800 | [diff] [blame] | 314 | CacheHandle entry = cache.lookup(uncacheableSpawn, simplePolicy); |
| 315 | verify(remoteCache, never()) |
| 316 | .getCachedActionResult(any(ActionKey.class)); |
| 317 | assertThat(entry.hasResult()).isFalse(); |
Googler | 4dd6f00 | 2018-03-27 08:15:39 -0700 | [diff] [blame] | 318 | SpawnResult result = |
| 319 | new SpawnResult.Builder() |
| 320 | .setExitCode(0) |
| 321 | .setStatus(Status.SUCCESS) |
| 322 | .setRunnerName("test") |
| 323 | .build(); |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 324 | entry.store(result); |
olaola | a22d0e9 | 2017-12-11 07:53:15 -0800 | [diff] [blame] | 325 | ImmutableList<Path> outputFiles = ImmutableList.of(fs.getPath("/random/file")); |
olaola | a22d0e9 | 2017-12-11 07:53:15 -0800 | [diff] [blame] | 326 | verify(remoteCache) |
olaola | f0aa55d | 2018-08-16 08:51:06 -0700 | [diff] [blame] | 327 | .upload( |
| 328 | any(ActionKey.class), |
| 329 | any(Action.class), |
| 330 | any(Command.class), |
| 331 | any(Path.class), |
| 332 | eq(outputFiles), |
| 333 | eq(outErr), |
| 334 | eq(false)); |
Benjamin Peterson | 7e1c7bc | 2018-05-03 04:30:19 -0700 | [diff] [blame] | 335 | assertThat(progressUpdates).containsExactly(); |
olaola | a22d0e9 | 2017-12-11 07:53:15 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | @Test |
| 339 | public void noCacheSpawnsNoResultStore() throws Exception { |
| 340 | // Only successful action results are uploaded to the remote cache. The artifacts, however, |
| 341 | // are uploaded regardless. |
| 342 | CacheHandle entry = cache.lookup(simpleSpawn, simplePolicy); |
| 343 | verify(remoteCache).getCachedActionResult(any(ActionKey.class)); |
| 344 | assertThat(entry.hasResult()).isFalse(); |
| 345 | SpawnResult result = |
Googler | 4dd6f00 | 2018-03-27 08:15:39 -0700 | [diff] [blame] | 346 | new SpawnResult.Builder() |
| 347 | .setExitCode(1) |
| 348 | .setStatus(Status.NON_ZERO_EXIT) |
| 349 | .setRunnerName("test") |
| 350 | .build(); |
olaola | a22d0e9 | 2017-12-11 07:53:15 -0800 | [diff] [blame] | 351 | ImmutableList<Path> outputFiles = ImmutableList.of(fs.getPath("/random/file")); |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 352 | entry.store(result); |
olaola | a22d0e9 | 2017-12-11 07:53:15 -0800 | [diff] [blame] | 353 | verify(remoteCache) |
olaola | f0aa55d | 2018-08-16 08:51:06 -0700 | [diff] [blame] | 354 | .upload( |
| 355 | any(ActionKey.class), |
| 356 | any(Action.class), |
| 357 | any(Command.class), |
| 358 | any(Path.class), |
| 359 | eq(outputFiles), |
| 360 | eq(outErr), |
| 361 | eq(false)); |
Benjamin Peterson | 7e1c7bc | 2018-05-03 04:30:19 -0700 | [diff] [blame] | 362 | assertThat(progressUpdates) |
| 363 | .containsExactly(Pair.of(ProgressStatus.CHECKING_CACHE, "remote-cache")); |
olaola | a22d0e9 | 2017-12-11 07:53:15 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | @Test |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 367 | public void printWarningIfUploadFails() throws Exception { |
| 368 | CacheHandle entry = cache.lookup(simpleSpawn, simplePolicy); |
| 369 | assertThat(entry.hasResult()).isFalse(); |
Googler | 4dd6f00 | 2018-03-27 08:15:39 -0700 | [diff] [blame] | 370 | SpawnResult result = |
| 371 | new SpawnResult.Builder() |
| 372 | .setExitCode(0) |
| 373 | .setStatus(Status.SUCCESS) |
| 374 | .setRunnerName("test") |
| 375 | .build(); |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 376 | ImmutableList<Path> outputFiles = ImmutableList.of(fs.getPath("/random/file")); |
| 377 | |
olaola | 7744b86 | 2017-09-18 23:04:33 +0200 | [diff] [blame] | 378 | doThrow(new IOException("cache down")) |
| 379 | .when(remoteCache) |
olaola | f0aa55d | 2018-08-16 08:51:06 -0700 | [diff] [blame] | 380 | .upload( |
| 381 | any(ActionKey.class), |
| 382 | any(Action.class), |
| 383 | any(Command.class), |
| 384 | any(Path.class), |
| 385 | eq(outputFiles), |
| 386 | eq(outErr), |
| 387 | eq(true)); |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 388 | |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 389 | entry.store(result); |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 390 | verify(remoteCache) |
olaola | f0aa55d | 2018-08-16 08:51:06 -0700 | [diff] [blame] | 391 | .upload( |
| 392 | any(ActionKey.class), |
| 393 | any(Action.class), |
| 394 | any(Command.class), |
| 395 | any(Path.class), |
| 396 | eq(outputFiles), |
| 397 | eq(outErr), |
| 398 | eq(true)); |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 399 | |
| 400 | assertThat(eventHandler.getEvents()).hasSize(1); |
| 401 | Event evt = eventHandler.getEvents().get(0); |
| 402 | assertThat(evt.getKind()).isEqualTo(EventKind.WARNING); |
Jakob Buchgraber | 321138f | 2018-05-07 05:24:53 -0700 | [diff] [blame] | 403 | assertThat(evt.getMessage()).contains("Error"); |
| 404 | assertThat(evt.getMessage()).contains("writing"); |
| 405 | assertThat(evt.getMessage()).contains("cache down"); |
Benjamin Peterson | 7e1c7bc | 2018-05-03 04:30:19 -0700 | [diff] [blame] | 406 | assertThat(progressUpdates) |
| 407 | .containsExactly(Pair.of(ProgressStatus.CHECKING_CACHE, "remote-cache")); |
Benjamin Peterson | 3ff87f7 | 2017-08-21 18:41:45 +0200 | [diff] [blame] | 408 | } |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 409 | } |