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