|
| 1 | +// Copyright 2022 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.devtools.build.lib.testutil.TestUtils.tmpDirFile; |
| 17 | + |
| 18 | +import com.google.common.collect.ImmutableList; |
| 19 | +import com.google.devtools.build.lib.buildtool.util.BuildIntegrationTestCase; |
| 20 | +import com.google.devtools.build.lib.runtime.BlazeModule; |
| 21 | +import com.google.devtools.build.lib.runtime.BlazeRuntime; |
| 22 | +import com.google.devtools.build.lib.runtime.BlockWaitingModule; |
| 23 | +import com.google.devtools.build.lib.runtime.BuildSummaryStatsModule; |
| 24 | +import com.google.devtools.build.lib.standalone.StandaloneModule; |
| 25 | +import com.google.devtools.build.lib.vfs.PathFragment; |
| 26 | +import java.io.IOException; |
| 27 | +import org.junit.After; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.runner.RunWith; |
| 30 | +import org.junit.runners.JUnit4; |
| 31 | + |
| 32 | +@RunWith(JUnit4.class) |
| 33 | +public class DiskCacheIntegrationTest extends BuildIntegrationTestCase { |
| 34 | + |
| 35 | + private static PathFragment getDiskCacheDir() { |
| 36 | + PathFragment testTmpDir = PathFragment.create(tmpDirFile().getAbsolutePath()); |
| 37 | + return testTmpDir.getRelative("disk_cache"); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + protected void setupOptions() throws Exception { |
| 42 | + super.setupOptions(); |
| 43 | + |
| 44 | + addOptions("--disk_cache=" + getDiskCacheDir()); |
| 45 | + } |
| 46 | + |
| 47 | + @After |
| 48 | + public void tearDown() throws IOException { |
| 49 | + getWorkspace().getFileSystem().getPath(getDiskCacheDir()).deleteTree(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected ImmutableList<BlazeModule> getSpawnModules() { |
| 54 | + return ImmutableList.<BlazeModule>builder() |
| 55 | + .addAll(super.getSpawnModules()) |
| 56 | + .add(new StandaloneModule()) |
| 57 | + .build(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + protected BlazeRuntime.Builder getRuntimeBuilder() throws Exception { |
| 62 | + return super.getRuntimeBuilder() |
| 63 | + .addBlazeModule(new RemoteModule()) |
| 64 | + .addBlazeModule(new BuildSummaryStatsModule()) |
| 65 | + .addBlazeModule(new BlockWaitingModule()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void hitDiskCache() throws Exception { |
| 70 | + write( |
| 71 | + "BUILD", |
| 72 | + "genrule(", |
| 73 | + " name = 'foo',", |
| 74 | + " srcs = ['foo.in'],", |
| 75 | + " outs = ['foo.out'],", |
| 76 | + " cmd = 'cat $(SRCS) > $@',", |
| 77 | + ")", |
| 78 | + "genrule(", |
| 79 | + " name = 'foobar',", |
| 80 | + " srcs = [':foo.out', 'bar.in'],", |
| 81 | + " outs = ['foobar.out'],", |
| 82 | + " cmd = 'cat $(SRCS) > $@',", |
| 83 | + ")"); |
| 84 | + write("foo.in", "foo"); |
| 85 | + write("bar.in", "bar"); |
| 86 | + buildTarget("//:foobar"); |
| 87 | + cleanAndRestartServer(); |
| 88 | + |
| 89 | + buildTarget("//:foobar"); |
| 90 | + |
| 91 | + events.assertContainsInfo("2 disk cache hit"); |
| 92 | + } |
| 93 | + |
| 94 | + private void blobsReferencedInAAreMissingCasIgnoresAc(String... additionalOptions) |
| 95 | + throws Exception { |
| 96 | + // Arrange: Prepare the workspace and populate disk cache |
| 97 | + write( |
| 98 | + "BUILD", |
| 99 | + "genrule(", |
| 100 | + " name = 'foo',", |
| 101 | + " srcs = ['foo.in'],", |
| 102 | + " outs = ['foo.out'],", |
| 103 | + " cmd = 'cat $(SRCS) > $@',", |
| 104 | + ")", |
| 105 | + "genrule(", |
| 106 | + " name = 'foobar',", |
| 107 | + " srcs = [':foo.out', 'bar.in'],", |
| 108 | + " outs = ['foobar.out'],", |
| 109 | + " cmd = 'cat $(SRCS) > $@',", |
| 110 | + ")"); |
| 111 | + write("foo.in", "foo"); |
| 112 | + write("bar.in", "bar"); |
| 113 | + addOptions(additionalOptions); |
| 114 | + buildTarget("//:foobar"); |
| 115 | + cleanAndRestartServer(); |
| 116 | + |
| 117 | + // Act: Delete blobs in CAS from disk cache and do a clean build |
| 118 | + getWorkspace().getFileSystem().getPath(getDiskCacheDir().getRelative("cas")).deleteTree(); |
| 119 | + addOptions(additionalOptions); |
| 120 | + buildTarget("//:foobar"); |
| 121 | + |
| 122 | + // Assert: Should ignore the stale AC and rerun the generating action |
| 123 | + events.assertDoesNotContainEvent("disk cache hit"); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void blobsReferencedInAcAreMissingCas_ignoresAc() throws Exception { |
| 128 | + blobsReferencedInAAreMissingCasIgnoresAc(); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void bwob_blobsReferencedInAcAreMissingCas_ignoresAc() throws Exception { |
| 133 | + blobsReferencedInAAreMissingCasIgnoresAc("--remote_download_minimal"); |
| 134 | + } |
| 135 | + |
| 136 | + private void cleanAndRestartServer() throws Exception { |
| 137 | + getOutputBase().getRelative("action_cache").deleteTreesBelow(); |
| 138 | + // Simulates a server restart |
| 139 | + createRuntimeWrapper(); |
| 140 | + } |
| 141 | +} |
0 commit comments