ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 1 | // Copyright 2018 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.skyframe; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
michajlo | 660d17f | 2020-03-27 09:01:57 -0700 | [diff] [blame] | 17 | import static org.junit.Assert.assertThrows; |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 18 | import static org.junit.Assert.fail; |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 19 | |
| 20 | import com.google.common.collect.ImmutableList; |
Jakob Buchgraber | 1407c04 | 2019-03-11 09:35:26 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableMap; |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableSet; |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 23 | import com.google.common.collect.Sets; |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 24 | import com.google.devtools.build.lib.actions.ActionInput; |
| 25 | import com.google.devtools.build.lib.actions.ActionInputHelper; |
| 26 | import com.google.devtools.build.lib.actions.ActionInputMap; |
| 27 | import com.google.devtools.build.lib.actions.Artifact; |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 28 | import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact; |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 29 | import com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact; |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 30 | import com.google.devtools.build.lib.actions.ArtifactPathResolver; |
| 31 | import com.google.devtools.build.lib.actions.ArtifactRoot; |
| 32 | import com.google.devtools.build.lib.actions.FileArtifactValue; |
Jakob Buchgraber | 1407c04 | 2019-03-11 09:35:26 -0700 | [diff] [blame] | 33 | import com.google.devtools.build.lib.actions.FileArtifactValue.RemoteFileArtifactValue; |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 34 | import com.google.devtools.build.lib.actions.FilesetOutputSymlink; |
| 35 | import com.google.devtools.build.lib.actions.HasDigest; |
| 36 | import com.google.devtools.build.lib.actions.HasDigest.ByteStringDigest; |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 37 | import com.google.devtools.build.lib.actions.util.ActionsTestUtil; |
Googler | e9186eb | 2020-07-09 17:13:15 -0700 | [diff] [blame] | 38 | import com.google.devtools.build.lib.testutil.ManualClock; |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 39 | import com.google.devtools.build.lib.testutil.Scratch; |
Googler | e9186eb | 2020-07-09 17:13:15 -0700 | [diff] [blame] | 40 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 41 | import com.google.devtools.build.lib.vfs.Path; |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 42 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 43 | import com.google.devtools.build.lib.vfs.Root; |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 44 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 45 | import java.io.FileNotFoundException; |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 46 | import java.io.IOException; |
Jakob Buchgraber | 1407c04 | 2019-03-11 09:35:26 -0700 | [diff] [blame] | 47 | import java.util.Map; |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 48 | import java.util.Set; |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 49 | import org.junit.Before; |
| 50 | import org.junit.Test; |
| 51 | import org.junit.runner.RunWith; |
| 52 | import org.junit.runners.JUnit4; |
| 53 | |
| 54 | /** Tests for {@link ActionMetadataHandler}. */ |
| 55 | @RunWith(JUnit4.class) |
Googler | e9186eb | 2020-07-09 17:13:15 -0700 | [diff] [blame] | 56 | public final class ActionMetadataHandlerTest { |
| 57 | |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 58 | private final Set<Path> chmodCalls = Sets.newConcurrentHashSet(); |
| 59 | |
| 60 | private final Scratch scratch = |
| 61 | new Scratch( |
| 62 | new InMemoryFileSystem() { |
| 63 | @Override |
| 64 | public void chmod(Path path, int mode) throws IOException { |
| 65 | assertThat(mode).isEqualTo(0555); // Read only and executable. |
| 66 | if (!chmodCalls.add(path)) { |
| 67 | fail("chmod called on " + path + " twice"); |
| 68 | } |
| 69 | super.chmod(path, mode); |
| 70 | } |
| 71 | }); |
| 72 | |
Googler | e9186eb | 2020-07-09 17:13:15 -0700 | [diff] [blame] | 73 | private final TimestampGranularityMonitor tsgm = |
| 74 | new TimestampGranularityMonitor(new ManualClock()); |
| 75 | |
| 76 | private final ArtifactRoot sourceRoot = |
| 77 | ArtifactRoot.asSourceRoot(Root.fromPath(scratch.resolve("/workspace"))); |
| 78 | private final ArtifactRoot outputRoot = |
| 79 | ArtifactRoot.asDerivedRoot(scratch.resolve("/output"), "bin"); |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 80 | private final Path execRoot = outputRoot.getRoot().asPath(); |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 81 | |
| 82 | @Before |
Googler | e9186eb | 2020-07-09 17:13:15 -0700 | [diff] [blame] | 83 | public void createRootDirs() throws Exception { |
| 84 | sourceRoot.getRoot().asPath().createDirectoryAndParents(); |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 85 | execRoot.createDirectoryAndParents(); |
| 86 | } |
| 87 | |
| 88 | private ActionMetadataHandler createHandler( |
| 89 | ActionInputMap inputMap, boolean forInputDiscovery, ImmutableSet<Artifact> outputs) { |
| 90 | return ActionMetadataHandler.create( |
| 91 | inputMap, |
| 92 | forInputDiscovery, |
| 93 | outputs, |
| 94 | tsgm, |
| 95 | ArtifactPathResolver.IDENTITY, |
jhorvitz | c69e268 | 2020-07-16 07:16:28 -0700 | [diff] [blame] | 96 | execRoot.asFragment(), |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 97 | /*expandedFilesets=*/ ImmutableMap.of()); |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void withNonArtifactInput() throws Exception { |
| 102 | ActionInput input = ActionInputHelper.fromPath("foo/bar"); |
janakr | 39969b1 | 2019-03-03 16:25:48 -0800 | [diff] [blame] | 103 | FileArtifactValue metadata = |
lberki | 812e6fe | 2019-07-25 07:50:55 -0700 | [diff] [blame] | 104 | FileArtifactValue.createForNormalFile( |
janakr | 39969b1 | 2019-03-03 16:25:48 -0800 | [diff] [blame] | 105 | new byte[] {1, 2, 3}, /*proxy=*/ null, 10L, /*isShareable=*/ true); |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 106 | ActionInputMap map = new ActionInputMap(1); |
mschaller | 4c7ca32 | 2018-11-20 13:22:05 -0800 | [diff] [blame] | 107 | map.putWithNoDepOwner(input, metadata); |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 108 | assertThat(map.getMetadata(input)).isEqualTo(metadata); |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 109 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 110 | createHandler(map, /*forInputDiscovery=*/ false, /*outputs=*/ ImmutableSet.of()); |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 111 | assertThat(handler.getMetadata(input)).isNull(); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 112 | assertThat(chmodCalls).isEmpty(); |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void withArtifactInput() throws Exception { |
| 117 | PathFragment path = PathFragment.create("src/a"); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 118 | Artifact artifact = ActionsTestUtil.createArtifactWithRootRelativePath(sourceRoot, path); |
janakr | 39969b1 | 2019-03-03 16:25:48 -0800 | [diff] [blame] | 119 | FileArtifactValue metadata = |
lberki | 812e6fe | 2019-07-25 07:50:55 -0700 | [diff] [blame] | 120 | FileArtifactValue.createForNormalFile( |
janakr | 39969b1 | 2019-03-03 16:25:48 -0800 | [diff] [blame] | 121 | new byte[] {1, 2, 3}, /*proxy=*/ null, 10L, /*isShareable=*/ true); |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 122 | ActionInputMap map = new ActionInputMap(1); |
mschaller | 4c7ca32 | 2018-11-20 13:22:05 -0800 | [diff] [blame] | 123 | map.putWithNoDepOwner(artifact, metadata); |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 124 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 125 | createHandler(map, /*forInputDiscovery=*/ false, /*outputs=*/ ImmutableSet.of()); |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 126 | assertThat(handler.getMetadata(artifact)).isEqualTo(metadata); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 127 | assertThat(chmodCalls).isEmpty(); |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Test |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 131 | public void unknownSourceArtifactDisallowedOutsideOfInputDiscovery() { |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 132 | PathFragment path = PathFragment.create("src/a"); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 133 | Artifact artifact = ActionsTestUtil.createArtifactWithRootRelativePath(sourceRoot, path); |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 134 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 135 | createHandler( |
| 136 | new ActionInputMap(0), /*forInputDiscovery=*/ false, /*outputs=*/ ImmutableSet.of()); |
Googler | e9186eb | 2020-07-09 17:13:15 -0700 | [diff] [blame] | 137 | Exception e = assertThrows(IllegalStateException.class, () -> handler.getMetadata(artifact)); |
| 138 | assertThat(e).hasMessageThat().contains(artifact + " is not present in declared outputs"); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 139 | assertThat(chmodCalls).isEmpty(); |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Test |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 143 | public void unknownSourceArtifactPermittedDuringInputDiscovery() throws Exception { |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 144 | PathFragment path = PathFragment.create("src/a"); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 145 | Artifact artifact = ActionsTestUtil.createArtifactWithRootRelativePath(sourceRoot, path); |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 146 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 147 | createHandler( |
| 148 | new ActionInputMap(0), /*forInputDiscovery=*/ true, /*outputs=*/ ImmutableSet.of()); |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 149 | assertThat(handler.getMetadata(artifact)).isNull(); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 150 | assertThat(chmodCalls).isEmpty(); |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 151 | } |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 152 | |
| 153 | @Test |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 154 | public void unknownArtifactPermittedDuringInputDiscovery() throws Exception { |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 155 | PathFragment path = PathFragment.create("foo/bar"); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 156 | Artifact artifact = ActionsTestUtil.createArtifactWithRootRelativePath(outputRoot, path); |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 157 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 158 | createHandler( |
| 159 | new ActionInputMap(0), /*forInputDiscovery=*/ true, /*outputs=*/ ImmutableSet.of()); |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 160 | assertThat(handler.getMetadata(artifact)).isNull(); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 161 | assertThat(chmodCalls).isEmpty(); |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Test |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 165 | public void withKnownOutputArtifactStatsFile() throws Exception { |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 166 | scratch.file("/output/bin/foo/bar", "not empty"); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 167 | Artifact artifact = ActionsTestUtil.createArtifact(outputRoot, "foo/bar"); |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 168 | assertThat(artifact.getPath().exists()).isTrue(); |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 169 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 170 | createHandler( |
| 171 | new ActionInputMap(0), |
| 172 | /*forInputDiscovery=*/ false, |
| 173 | /*outputs=*/ ImmutableSet.of(artifact)); |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 174 | assertThat(handler.getMetadata(artifact)).isNotNull(); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 175 | assertThat(chmodCalls).isEmpty(); |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @Test |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 179 | public void withMissingOutputArtifactStatsFileFailsWithException() { |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 180 | Artifact artifact = ActionsTestUtil.createArtifact(outputRoot, "foo/bar"); |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 181 | assertThat(artifact.getPath().exists()).isFalse(); |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 182 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 183 | createHandler( |
| 184 | new ActionInputMap(0), |
| 185 | /*forInputDiscovery=*/ false, |
| 186 | /*outputs=*/ ImmutableSet.of(artifact)); |
jcater | 83130f4 | 2019-04-30 14:29:28 -0700 | [diff] [blame] | 187 | assertThrows(FileNotFoundException.class, () -> handler.getMetadata(artifact)); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 188 | assertThat(chmodCalls).isEmpty(); |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Test |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 192 | public void unknownArtifactDisallowedOutsideOfInputDiscovery() { |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 193 | PathFragment path = PathFragment.create("foo/bar"); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 194 | Artifact artifact = ActionsTestUtil.createArtifactWithRootRelativePath(outputRoot, path); |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 195 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 196 | createHandler( |
| 197 | new ActionInputMap(0), /*forInputDiscovery=*/ false, /*outputs=*/ ImmutableSet.of()); |
jcater | 83130f4 | 2019-04-30 14:29:28 -0700 | [diff] [blame] | 198 | assertThrows(IllegalStateException.class, () -> handler.getMetadata(artifact)); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 199 | assertThat(chmodCalls).isEmpty(); |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Test |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 203 | public void unknownTreeArtifactPermittedDuringInputDiscovery() throws Exception { |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 204 | PathFragment path = PathFragment.create("bin/foo/bar"); |
janakr | 3290e22 | 2019-05-29 16:34:22 -0700 | [diff] [blame] | 205 | SpecialArtifact treeArtifact = |
Googler | 1d8d138 | 2020-05-18 12:10:49 -0700 | [diff] [blame] | 206 | ActionsTestUtil.createTreeArtifactWithGeneratingAction(outputRoot, path); |
| 207 | Artifact artifact = TreeFileArtifact.createTreeOutput(treeArtifact, "baz"); |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 208 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 209 | createHandler( |
| 210 | new ActionInputMap(0), /*forInputDiscovery=*/ true, /*outputs=*/ ImmutableSet.of()); |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 211 | assertThat(handler.getMetadata(artifact)).isNull(); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 212 | assertThat(chmodCalls).isEmpty(); |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Test |
| 216 | public void withUnknownOutputArtifactStatsFileTreeArtifact() throws Exception { |
| 217 | scratch.file("/output/bin/foo/bar/baz", "not empty"); |
| 218 | PathFragment path = PathFragment.create("bin/foo/bar"); |
janakr | 3290e22 | 2019-05-29 16:34:22 -0700 | [diff] [blame] | 219 | SpecialArtifact treeArtifact = |
Googler | 1d8d138 | 2020-05-18 12:10:49 -0700 | [diff] [blame] | 220 | ActionsTestUtil.createTreeArtifactWithGeneratingAction(outputRoot, path); |
| 221 | Artifact artifact = TreeFileArtifact.createTreeOutput(treeArtifact, "baz"); |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 222 | assertThat(artifact.getPath().exists()).isTrue(); |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 223 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 224 | createHandler( |
| 225 | new ActionInputMap(0), |
| 226 | /*forInputDiscovery=*/ false, |
| 227 | /*outputs=*/ ImmutableSet.of(treeArtifact)); |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 228 | assertThat(handler.getMetadata(artifact)).isNotNull(); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 229 | assertThat(chmodCalls).isEmpty(); |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | @Test |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 233 | public void unknownTreeArtifactDisallowedOutsideOfInputDiscovery() { |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 234 | PathFragment path = PathFragment.create("bin/foo/bar"); |
janakr | 3290e22 | 2019-05-29 16:34:22 -0700 | [diff] [blame] | 235 | SpecialArtifact treeArtifact = |
Googler | 1d8d138 | 2020-05-18 12:10:49 -0700 | [diff] [blame] | 236 | ActionsTestUtil.createTreeArtifactWithGeneratingAction(outputRoot, path); |
| 237 | Artifact artifact = TreeFileArtifact.createTreeOutput(treeArtifact, "baz"); |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 238 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 239 | createHandler( |
| 240 | new ActionInputMap(0), /*forInputDiscovery=*/ false, /*outputs=*/ ImmutableSet.of()); |
jcater | 83130f4 | 2019-04-30 14:29:28 -0700 | [diff] [blame] | 241 | assertThrows(IllegalStateException.class, () -> handler.getMetadata(artifact)); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 242 | assertThat(chmodCalls).isEmpty(); |
ulfjack | 9de2ea5 | 2018-10-17 09:23:23 -0700 | [diff] [blame] | 243 | } |
djasper | efdecce | 2019-02-21 08:41:22 -0800 | [diff] [blame] | 244 | |
| 245 | @Test |
Googler | 974879d | 2020-05-27 13:25:52 -0700 | [diff] [blame] | 246 | public void createsTreeArtifactValueFromFilesystem() throws Exception { |
| 247 | scratch.file("/output/bin/foo/bar/child1", "child1"); |
| 248 | scratch.file("/output/bin/foo/bar/child2", "child2"); |
| 249 | SpecialArtifact treeArtifact = |
| 250 | ActionsTestUtil.createTreeArtifactWithGeneratingAction( |
| 251 | outputRoot, PathFragment.create("bin/foo/bar")); |
| 252 | TreeFileArtifact child1 = TreeFileArtifact.createTreeOutput(treeArtifact, "child1"); |
| 253 | TreeFileArtifact child2 = TreeFileArtifact.createTreeOutput(treeArtifact, "child2"); |
| 254 | assertThat(child1.getPath().exists()).isTrue(); |
| 255 | assertThat(child2.getPath().exists()).isTrue(); |
| 256 | |
Googler | 974879d | 2020-05-27 13:25:52 -0700 | [diff] [blame] | 257 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 258 | createHandler( |
| 259 | new ActionInputMap(0), |
| 260 | /*forInputDiscovery=*/ false, |
| 261 | /*outputs=*/ ImmutableSet.of(treeArtifact)); |
Googler | 974879d | 2020-05-27 13:25:52 -0700 | [diff] [blame] | 262 | |
| 263 | FileArtifactValue treeMetadata = handler.getMetadata(treeArtifact); |
| 264 | FileArtifactValue child1Metadata = handler.getMetadata(child1); |
| 265 | FileArtifactValue child2Metadata = handler.getMetadata(child2); |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 266 | TreeArtifactValue tree = handler.getOutputStore().getTreeArtifactData(treeArtifact); |
Googler | 974879d | 2020-05-27 13:25:52 -0700 | [diff] [blame] | 267 | |
| 268 | assertThat(tree.getMetadata()).isEqualTo(treeMetadata); |
| 269 | assertThat(tree.getChildValues()) |
| 270 | .containsExactly(child1, child1Metadata, child2, child2Metadata); |
jhorvitz | bda125a | 2020-07-16 12:12:31 -0700 | [diff] [blame] | 271 | assertThat(handler.getTreeArtifactChildren(treeArtifact)).isEqualTo(tree.getChildren()); |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 272 | assertThat(handler.getOutputStore().getAllArtifactData()).isEmpty(); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 273 | assertThat(chmodCalls).isEmpty(); |
Googler | 974879d | 2020-05-27 13:25:52 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | @Test |
djasper | efdecce | 2019-02-21 08:41:22 -0800 | [diff] [blame] | 277 | public void resettingOutputs() throws Exception { |
djasper | efdecce | 2019-02-21 08:41:22 -0800 | [diff] [blame] | 278 | PathFragment path = PathFragment.create("foo/bar"); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 279 | Artifact artifact = ActionsTestUtil.createArtifactWithRootRelativePath(outputRoot, path); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 280 | Path outputPath = scratch.file(artifact.getPath().getPathString(), "not empty"); |
djasper | efdecce | 2019-02-21 08:41:22 -0800 | [diff] [blame] | 281 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 282 | createHandler( |
| 283 | new ActionInputMap(0), |
| 284 | /*forInputDiscovery=*/ true, |
| 285 | /*outputs=*/ ImmutableSet.of(artifact)); |
jhorvitz | bda125a | 2020-07-16 12:12:31 -0700 | [diff] [blame] | 286 | handler.prepareForActionExecution(); |
djasper | efdecce | 2019-02-21 08:41:22 -0800 | [diff] [blame] | 287 | |
| 288 | // The handler doesn't have any info. It'll stat the file and discover that it's 10 bytes long. |
| 289 | assertThat(handler.getMetadata(artifact).getSize()).isEqualTo(10); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 290 | assertThat(chmodCalls).containsExactly(outputPath); |
djasper | efdecce | 2019-02-21 08:41:22 -0800 | [diff] [blame] | 291 | |
| 292 | // Inject a remote file of size 42. |
Googler | ef55446 | 2020-06-04 17:40:55 -0700 | [diff] [blame] | 293 | handler.injectFile( |
Googler | b67aaba | 2020-05-13 09:49:37 -0700 | [diff] [blame] | 294 | artifact, new RemoteFileArtifactValue(new byte[] {1, 2, 3}, 42, 0, "ultimate-answer")); |
djasper | efdecce | 2019-02-21 08:41:22 -0800 | [diff] [blame] | 295 | assertThat(handler.getMetadata(artifact).getSize()).isEqualTo(42); |
| 296 | |
| 297 | // Reset this output, which will make the handler stat the file again. |
| 298 | handler.resetOutputs(ImmutableList.of(artifact)); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 299 | chmodCalls.clear(); // Permit a second chmod call for the artifact. |
djasper | efdecce | 2019-02-21 08:41:22 -0800 | [diff] [blame] | 300 | assertThat(handler.getMetadata(artifact).getSize()).isEqualTo(10); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 301 | assertThat(chmodCalls).containsExactly(outputPath); |
djasper | efdecce | 2019-02-21 08:41:22 -0800 | [diff] [blame] | 302 | } |
Jakob Buchgraber | 1407c04 | 2019-03-11 09:35:26 -0700 | [diff] [blame] | 303 | |
| 304 | @Test |
| 305 | public void injectRemoteArtifactMetadata() throws Exception { |
| 306 | PathFragment path = PathFragment.create("foo/bar"); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 307 | Artifact artifact = ActionsTestUtil.createArtifactWithRootRelativePath(outputRoot, path); |
Jakob Buchgraber | 1407c04 | 2019-03-11 09:35:26 -0700 | [diff] [blame] | 308 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 309 | createHandler( |
| 310 | new ActionInputMap(0), |
| 311 | /*forInputDiscovery=*/ true, |
| 312 | /*outputs=*/ ImmutableSet.of(artifact)); |
jhorvitz | bda125a | 2020-07-16 12:12:31 -0700 | [diff] [blame] | 313 | handler.prepareForActionExecution(); |
Jakob Buchgraber | 1407c04 | 2019-03-11 09:35:26 -0700 | [diff] [blame] | 314 | |
| 315 | byte[] digest = new byte[] {1, 2, 3}; |
| 316 | int size = 10; |
Googler | ef55446 | 2020-06-04 17:40:55 -0700 | [diff] [blame] | 317 | handler.injectFile( |
Googler | b67aaba | 2020-05-13 09:49:37 -0700 | [diff] [blame] | 318 | artifact, new RemoteFileArtifactValue(digest, size, /*locationIndex=*/ 1, "action-id")); |
Jakob Buchgraber | 1407c04 | 2019-03-11 09:35:26 -0700 | [diff] [blame] | 319 | |
| 320 | FileArtifactValue v = handler.getMetadata(artifact); |
| 321 | assertThat(v).isNotNull(); |
| 322 | assertThat(v.getDigest()).isEqualTo(digest); |
| 323 | assertThat(v.getSize()).isEqualTo(size); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 324 | assertThat(chmodCalls).isEmpty(); |
Jakob Buchgraber | 1407c04 | 2019-03-11 09:35:26 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | @Test |
Googler | 848c4ee | 2020-07-06 13:29:45 -0700 | [diff] [blame] | 328 | public void cannotInjectTreeArtifactChildIndividually() { |
Googler | 481bad3 | 2020-05-27 14:39:48 -0700 | [diff] [blame] | 329 | SpecialArtifact treeArtifact = |
| 330 | ActionsTestUtil.createTreeArtifactWithGeneratingAction( |
| 331 | outputRoot, PathFragment.create("bin/foo/bar")); |
Googler | 848c4ee | 2020-07-06 13:29:45 -0700 | [diff] [blame] | 332 | TreeFileArtifact child = TreeFileArtifact.createTreeOutput(treeArtifact, "child"); |
Googler | 481bad3 | 2020-05-27 14:39:48 -0700 | [diff] [blame] | 333 | |
Googler | 481bad3 | 2020-05-27 14:39:48 -0700 | [diff] [blame] | 334 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 335 | createHandler( |
| 336 | new ActionInputMap(0), |
| 337 | /*forInputDiscovery=*/ false, |
| 338 | /*outputs=*/ ImmutableSet.of(treeArtifact)); |
jhorvitz | bda125a | 2020-07-16 12:12:31 -0700 | [diff] [blame] | 339 | handler.prepareForActionExecution(); |
Googler | 481bad3 | 2020-05-27 14:39:48 -0700 | [diff] [blame] | 340 | |
Googler | 848c4ee | 2020-07-06 13:29:45 -0700 | [diff] [blame] | 341 | RemoteFileArtifactValue childValue = new RemoteFileArtifactValue(new byte[] {1, 2, 3}, 5, 1); |
Googler | 481bad3 | 2020-05-27 14:39:48 -0700 | [diff] [blame] | 342 | |
Googler | 848c4ee | 2020-07-06 13:29:45 -0700 | [diff] [blame] | 343 | assertThrows(IllegalArgumentException.class, () -> handler.injectFile(child, childValue)); |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 344 | assertThat(handler.getOutputStore().getAllArtifactData()).isEmpty(); |
| 345 | assertThat(handler.getOutputStore().getAllTreeArtifactData()).isEmpty(); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 346 | assertThat(chmodCalls).isEmpty(); |
Googler | 848c4ee | 2020-07-06 13:29:45 -0700 | [diff] [blame] | 347 | } |
Googler | 481bad3 | 2020-05-27 14:39:48 -0700 | [diff] [blame] | 348 | |
Googler | 848c4ee | 2020-07-06 13:29:45 -0700 | [diff] [blame] | 349 | @Test |
| 350 | public void canInjectTemplateExpansionOutput() { |
| 351 | SpecialArtifact treeArtifact = |
| 352 | ActionsTestUtil.createTreeArtifactWithGeneratingAction( |
| 353 | outputRoot, PathFragment.create("bin/foo/bar")); |
| 354 | TreeFileArtifact output = |
| 355 | TreeFileArtifact.createTemplateExpansionOutput( |
| 356 | treeArtifact, "output", ActionsTestUtil.NULL_TEMPLATE_EXPANSION_ARTIFACT_OWNER); |
Googler | 481bad3 | 2020-05-27 14:39:48 -0700 | [diff] [blame] | 357 | |
Googler | 848c4ee | 2020-07-06 13:29:45 -0700 | [diff] [blame] | 358 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 359 | createHandler( |
| 360 | new ActionInputMap(0), |
| 361 | /*forInputDiscovery=*/ false, |
| 362 | /*outputs=*/ ImmutableSet.of(treeArtifact)); |
jhorvitz | bda125a | 2020-07-16 12:12:31 -0700 | [diff] [blame] | 363 | handler.prepareForActionExecution(); |
Googler | 848c4ee | 2020-07-06 13:29:45 -0700 | [diff] [blame] | 364 | |
| 365 | RemoteFileArtifactValue value = new RemoteFileArtifactValue(new byte[] {1, 2, 3}, 5, 1); |
| 366 | handler.injectFile(output, value); |
| 367 | |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 368 | assertThat(handler.getOutputStore().getAllArtifactData()).containsExactly(output, value); |
| 369 | assertThat(handler.getOutputStore().getAllTreeArtifactData()).isEmpty(); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 370 | assertThat(chmodCalls).isEmpty(); |
Googler | 481bad3 | 2020-05-27 14:39:48 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | @Test |
Jakob Buchgraber | 1407c04 | 2019-03-11 09:35:26 -0700 | [diff] [blame] | 374 | public void injectRemoteTreeArtifactMetadata() throws Exception { |
| 375 | PathFragment path = PathFragment.create("bin/dir"); |
janakr | 3290e22 | 2019-05-29 16:34:22 -0700 | [diff] [blame] | 376 | SpecialArtifact treeArtifact = |
Googler | 1d8d138 | 2020-05-18 12:10:49 -0700 | [diff] [blame] | 377 | ActionsTestUtil.createTreeArtifactWithGeneratingAction(outputRoot, path); |
Jakob Buchgraber | 1407c04 | 2019-03-11 09:35:26 -0700 | [diff] [blame] | 378 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 379 | createHandler( |
| 380 | new ActionInputMap(0), |
| 381 | /*forInputDiscovery=*/ false, |
| 382 | /*outputs=*/ ImmutableSet.of(treeArtifact)); |
jhorvitz | bda125a | 2020-07-16 12:12:31 -0700 | [diff] [blame] | 383 | handler.prepareForActionExecution(); |
Jakob Buchgraber | 1407c04 | 2019-03-11 09:35:26 -0700 | [diff] [blame] | 384 | |
George Gensure | 3ef8fb9 | 2020-05-06 09:49:48 -0700 | [diff] [blame] | 385 | RemoteFileArtifactValue fooValue = |
| 386 | new RemoteFileArtifactValue(new byte[] {1, 2, 3}, 5, 1, "foo"); |
| 387 | RemoteFileArtifactValue barValue = |
| 388 | new RemoteFileArtifactValue(new byte[] {4, 5, 6}, 10, 1, "bar"); |
Googler | ef55446 | 2020-06-04 17:40:55 -0700 | [diff] [blame] | 389 | Map<TreeFileArtifact, FileArtifactValue> children = |
Googler | 04c546f | 2020-05-12 18:22:18 -0700 | [diff] [blame] | 390 | ImmutableMap.of( |
Googler | 1d8d138 | 2020-05-18 12:10:49 -0700 | [diff] [blame] | 391 | TreeFileArtifact.createTreeOutput(treeArtifact, "foo"), fooValue, |
| 392 | TreeFileArtifact.createTreeOutput(treeArtifact, "bar"), barValue); |
Jakob Buchgraber | 1407c04 | 2019-03-11 09:35:26 -0700 | [diff] [blame] | 393 | |
Googler | ef55446 | 2020-06-04 17:40:55 -0700 | [diff] [blame] | 394 | handler.injectDirectory(treeArtifact, children); |
Jakob Buchgraber | 1407c04 | 2019-03-11 09:35:26 -0700 | [diff] [blame] | 395 | |
| 396 | FileArtifactValue value = handler.getMetadata(treeArtifact); |
| 397 | assertThat(value).isNotNull(); |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 398 | TreeArtifactValue treeValue = handler.getOutputStore().getTreeArtifactData(treeArtifact); |
Jakob Buchgraber | 1407c04 | 2019-03-11 09:35:26 -0700 | [diff] [blame] | 399 | assertThat(treeValue).isNotNull(); |
| 400 | assertThat(treeValue.getDigest()).isEqualTo(value.getDigest()); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 401 | assertThat(chmodCalls).isEmpty(); |
Jakob Buchgraber | 1407c04 | 2019-03-11 09:35:26 -0700 | [diff] [blame] | 402 | |
| 403 | assertThat(treeValue.getChildPaths()) |
| 404 | .containsExactly(PathFragment.create("foo"), PathFragment.create("bar")); |
| 405 | assertThat(treeValue.getChildValues().values()).containsExactly(fooValue, barValue); |
jhorvitz | bda125a | 2020-07-16 12:12:31 -0700 | [diff] [blame] | 406 | assertThat(handler.getTreeArtifactChildren(treeArtifact)).isEqualTo(treeValue.getChildren()); |
Googler | aed4160 | 2020-06-02 12:22:53 -0700 | [diff] [blame] | 407 | |
| 408 | // Make sure that all children are transferred properly into the ActionExecutionValue. If any |
| 409 | // child is missing, getExistingFileArtifactValue will throw. |
janakr | 8444123 | 2020-03-03 07:32:28 -0800 | [diff] [blame] | 410 | ActionExecutionValue actionExecutionValue = |
| 411 | ActionExecutionValue.createFromOutputStore(handler.getOutputStore(), null, null, false); |
Googler | aed4160 | 2020-06-02 12:22:53 -0700 | [diff] [blame] | 412 | treeValue.getChildren().forEach(actionExecutionValue::getExistingFileArtifactValue); |
Jakob Buchgraber | 1407c04 | 2019-03-11 09:35:26 -0700 | [diff] [blame] | 413 | } |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 414 | |
| 415 | @Test |
| 416 | public void getMetadataFromFilesetMapping() throws Exception { |
| 417 | FileArtifactValue directoryFav = FileArtifactValue.createForDirectoryWithMtime(10L); |
| 418 | FileArtifactValue regularFav = |
| 419 | FileArtifactValue.createForVirtualActionInput(new byte[] {1, 2, 3, 4}, 10L); |
| 420 | HasDigest.ByteStringDigest byteStringDigest = new ByteStringDigest(new byte[] {2, 3, 4}); |
| 421 | |
Googler | e9186eb | 2020-07-09 17:13:15 -0700 | [diff] [blame] | 422 | ImmutableList<FilesetOutputSymlink> symlinks = |
| 423 | ImmutableList.of( |
| 424 | createFilesetOutputSymlink(directoryFav, "dir"), |
| 425 | createFilesetOutputSymlink(regularFav, "file"), |
| 426 | createFilesetOutputSymlink(byteStringDigest, "bytes")); |
| 427 | |
| 428 | Artifact artifact = |
| 429 | ActionsTestUtil.createArtifactWithRootRelativePath( |
| 430 | outputRoot, PathFragment.create("foo/bar")); |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 431 | ImmutableMap<Artifact, ImmutableList<FilesetOutputSymlink>> expandedFilesets = |
Googler | e9186eb | 2020-07-09 17:13:15 -0700 | [diff] [blame] | 432 | ImmutableMap.of(artifact, symlinks); |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 433 | |
| 434 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 435 | ActionMetadataHandler.create( |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 436 | new ActionInputMap(0), |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 437 | /*forInputDiscovery=*/ false, |
Googler | e9186eb | 2020-07-09 17:13:15 -0700 | [diff] [blame] | 438 | /*outputs=*/ ImmutableSet.of(), |
| 439 | tsgm, |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 440 | ArtifactPathResolver.IDENTITY, |
jhorvitz | c69e268 | 2020-07-16 07:16:28 -0700 | [diff] [blame] | 441 | execRoot.asFragment(), |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 442 | expandedFilesets); |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 443 | |
Googler | e9186eb | 2020-07-09 17:13:15 -0700 | [diff] [blame] | 444 | // Only the regular FileArtifactValue should have its metadata stored. |
| 445 | assertThat(handler.getMetadata(createInput("dir"))).isNull(); |
| 446 | assertThat(handler.getMetadata(createInput("file"))).isEqualTo(regularFav); |
| 447 | assertThat(handler.getMetadata(createInput("bytes"))).isNull(); |
| 448 | assertThat(handler.getMetadata(createInput("does_not_exist"))).isNull(); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 449 | assertThat(chmodCalls).isEmpty(); |
Googler | e9186eb | 2020-07-09 17:13:15 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | private FilesetOutputSymlink createFilesetOutputSymlink(HasDigest digest, String identifier) { |
| 453 | return FilesetOutputSymlink.create( |
| 454 | PathFragment.create(identifier + "_symlink"), |
| 455 | PathFragment.create(identifier), |
| 456 | digest, |
| 457 | /*isGeneratedTarget=*/ true, |
| 458 | outputRoot.getExecPath()); |
| 459 | } |
| 460 | |
| 461 | private ActionInput createInput(String identifier) { |
| 462 | return ActionInputHelper.fromPath(outputRoot.getRoot().getRelative(identifier).getPathString()); |
Googler | 5284e6c | 2019-10-30 07:43:54 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 465 | @Test |
| 466 | public void omitRegularArtifact() { |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 467 | Artifact omitted = |
| 468 | ActionsTestUtil.createArtifactWithRootRelativePath( |
| 469 | outputRoot, PathFragment.create("omitted")); |
| 470 | Artifact consumed = |
| 471 | ActionsTestUtil.createArtifactWithRootRelativePath( |
| 472 | outputRoot, PathFragment.create("consumed")); |
| 473 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 474 | createHandler( |
| 475 | new ActionInputMap(0), |
| 476 | /*forInputDiscovery=*/ false, |
| 477 | /*outputs=*/ ImmutableSet.of(omitted, consumed)); |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 478 | |
jhorvitz | bda125a | 2020-07-16 12:12:31 -0700 | [diff] [blame] | 479 | handler.prepareForActionExecution(); |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 480 | handler.markOmitted(omitted); |
| 481 | |
| 482 | assertThat(handler.artifactOmitted(omitted)).isTrue(); |
| 483 | assertThat(handler.artifactOmitted(consumed)).isFalse(); |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 484 | assertThat(handler.getOutputStore().getAllArtifactData()) |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 485 | .containsExactly(omitted, FileArtifactValue.OMITTED_FILE_MARKER); |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 486 | assertThat(handler.getOutputStore().getAllTreeArtifactData()).isEmpty(); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 487 | assertThat(chmodCalls).isEmpty(); |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | @Test |
| 491 | public void omitTreeArtifact() { |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 492 | SpecialArtifact omittedTree = |
| 493 | ActionsTestUtil.createTreeArtifactWithGeneratingAction( |
| 494 | outputRoot, PathFragment.create("omitted")); |
| 495 | SpecialArtifact consumedTree = |
| 496 | ActionsTestUtil.createTreeArtifactWithGeneratingAction( |
| 497 | outputRoot, PathFragment.create("consumed")); |
| 498 | ActionMetadataHandler handler = |
jhorvitz | c69e268 | 2020-07-16 07:16:28 -0700 | [diff] [blame] | 499 | createHandler( |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 500 | new ActionInputMap(0), |
| 501 | /*forInputDiscovery=*/ false, |
jhorvitz | c69e268 | 2020-07-16 07:16:28 -0700 | [diff] [blame] | 502 | /*outputs=*/ ImmutableSet.of(omittedTree, consumedTree)); |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 503 | |
jhorvitz | bda125a | 2020-07-16 12:12:31 -0700 | [diff] [blame] | 504 | handler.prepareForActionExecution(); |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 505 | handler.markOmitted(omittedTree); |
| 506 | handler.markOmitted(omittedTree); // Marking a tree artifact as omitted twice is tolerated. |
| 507 | |
| 508 | assertThat(handler.artifactOmitted(omittedTree)).isTrue(); |
| 509 | assertThat(handler.artifactOmitted(consumedTree)).isFalse(); |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 510 | assertThat(handler.getOutputStore().getAllTreeArtifactData()) |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 511 | .containsExactly(omittedTree, TreeArtifactValue.OMITTED_TREE_MARKER); |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 512 | assertThat(handler.getOutputStore().getAllArtifactData()).isEmpty(); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 513 | assertThat(chmodCalls).isEmpty(); |
| 514 | } |
| 515 | |
| 516 | @Test |
| 517 | public void outputArtifactNotPreviouslyInjectedInExecutionMode() throws Exception { |
| 518 | Artifact output = |
| 519 | ActionsTestUtil.createArtifactWithRootRelativePath( |
| 520 | outputRoot, PathFragment.create("dir/file.out")); |
| 521 | Path outputPath = scratch.file(output.getPath().getPathString(), "contents"); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 522 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 523 | createHandler( |
| 524 | new ActionInputMap(0), |
| 525 | /*forInputDiscovery=*/ false, |
| 526 | /*outputs=*/ ImmutableSet.of(output)); |
jhorvitz | bda125a | 2020-07-16 12:12:31 -0700 | [diff] [blame] | 527 | handler.prepareForActionExecution(); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 528 | |
| 529 | FileArtifactValue metadata = handler.getMetadata(output); |
| 530 | |
| 531 | assertThat(metadata.getDigest()).isEqualTo(outputPath.getDigest()); |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 532 | assertThat(handler.getOutputStore().getAllArtifactData()).containsExactly(output, metadata); |
| 533 | assertThat(handler.getOutputStore().getAllTreeArtifactData()).isEmpty(); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 534 | assertThat(chmodCalls).containsExactly(outputPath); |
| 535 | } |
| 536 | |
| 537 | @Test |
| 538 | public void outputTreeArtifactNotPreviouslyInjectedInExecutionMode() throws Exception { |
| 539 | SpecialArtifact treeArtifact = |
| 540 | ActionsTestUtil.createTreeArtifactWithGeneratingAction( |
| 541 | outputRoot, PathFragment.create("bin/foo/bar")); |
| 542 | TreeFileArtifact child1 = TreeFileArtifact.createTreeOutput(treeArtifact, "child1"); |
| 543 | TreeFileArtifact child2 = TreeFileArtifact.createTreeOutput(treeArtifact, "subdir/child2"); |
| 544 | Path child1Path = scratch.file(child1.getPath().getPathString(), "contents1"); |
| 545 | Path child2Path = scratch.file(child2.getPath().getPathString(), "contents2"); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 546 | ActionMetadataHandler handler = |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 547 | createHandler( |
| 548 | new ActionInputMap(0), |
| 549 | /*forInputDiscovery=*/ false, |
| 550 | /*outputs=*/ ImmutableSet.of(treeArtifact)); |
jhorvitz | bda125a | 2020-07-16 12:12:31 -0700 | [diff] [blame] | 551 | handler.prepareForActionExecution(); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 552 | |
| 553 | FileArtifactValue treeMetadata = handler.getMetadata(treeArtifact); |
| 554 | FileArtifactValue child1Metadata = handler.getMetadata(child1); |
| 555 | FileArtifactValue child2Metadata = handler.getMetadata(child2); |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 556 | TreeArtifactValue tree = handler.getOutputStore().getTreeArtifactData(treeArtifact); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 557 | |
| 558 | assertThat(tree.getMetadata()).isEqualTo(treeMetadata); |
| 559 | assertThat(tree.getChildValues()) |
| 560 | .containsExactly(child1, child1Metadata, child2, child2Metadata); |
jhorvitz | bda125a | 2020-07-16 12:12:31 -0700 | [diff] [blame] | 561 | assertThat(handler.getTreeArtifactChildren(treeArtifact)).isEqualTo(tree.getChildren()); |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 562 | assertThat(handler.getOutputStore().getAllArtifactData()).isEmpty(); |
jhorvitz | a68bdbb | 2020-07-13 16:49:20 -0700 | [diff] [blame] | 563 | assertThat(chmodCalls) |
| 564 | .containsExactly( |
| 565 | treeArtifact.getPath(), child1Path, child2Path, child2Path.getParentDirectory()); |
Googler | 61a9f57 | 2020-06-02 10:28:24 -0700 | [diff] [blame] | 566 | } |
jhorvitz | 6c8f7b9 | 2020-07-15 15:02:29 -0700 | [diff] [blame] | 567 | |
| 568 | @Test |
| 569 | public void transformAfterInputDiscovery() throws Exception { |
| 570 | Artifact known = |
| 571 | ActionsTestUtil.createArtifactWithRootRelativePath( |
| 572 | outputRoot, PathFragment.create("known")); |
| 573 | Artifact unknown = |
| 574 | ActionsTestUtil.createArtifactWithRootRelativePath( |
| 575 | outputRoot, PathFragment.create("unknown")); |
| 576 | ActionMetadataHandler handler = |
| 577 | createHandler( |
| 578 | new ActionInputMap(0), |
| 579 | /*forInputDiscovery=*/ true, |
| 580 | /*outputs=*/ ImmutableSet.of(known)); |
| 581 | |
| 582 | // Unknown artifact returns null during input discovery. |
| 583 | assertThat(handler.getMetadata(unknown)).isNull(); |
| 584 | |
| 585 | OutputStore newStore = new OutputStore(); |
| 586 | FileArtifactValue knownMetadata = new RemoteFileArtifactValue(new byte[] {1, 2, 3}, 5, 1); |
| 587 | newStore.putArtifactData(known, knownMetadata); |
| 588 | ActionMetadataHandler newHandler = handler.transformAfterInputDiscovery(newStore); |
| 589 | assertThat(newHandler.getOutputStore()).isNotEqualTo(handler.getOutputStore()); |
| 590 | assertThat(newHandler.getOutputStore()).isEqualTo(newStore); |
| 591 | |
| 592 | assertThat(newHandler.getMetadata(known)).isEqualTo(knownMetadata); |
| 593 | // Unknown artifact throws outside of input discovery. |
| 594 | assertThrows(IllegalStateException.class, () -> newHandler.getMetadata(unknown)); |
| 595 | // We can transform it again. |
| 596 | assertThat(newHandler.transformAfterInputDiscovery(new OutputStore())).isNotNull(); |
| 597 | assertThat(chmodCalls).isEmpty(); |
| 598 | } |
jhorvitz | bda125a | 2020-07-16 12:12:31 -0700 | [diff] [blame] | 599 | |
| 600 | @Test |
| 601 | public void getTreeArtifactChildren_noData_returnsEmptySet() { |
| 602 | SpecialArtifact treeArtifact = |
| 603 | ActionsTestUtil.createTreeArtifactWithGeneratingAction( |
| 604 | outputRoot, PathFragment.create("bin/tree")); |
| 605 | ActionMetadataHandler handler = |
| 606 | createHandler( |
| 607 | new ActionInputMap(0), |
| 608 | /*forInputDiscovery=*/ false, |
| 609 | /*outputs=*/ ImmutableSet.of(treeArtifact)); |
| 610 | assertThat(handler.getTreeArtifactChildren(treeArtifact)).isEmpty(); |
| 611 | } |
| 612 | |
| 613 | @Test |
| 614 | public void enteringExecutionModeClearsCachedOutputs() throws Exception { |
| 615 | Artifact artifact = |
| 616 | ActionsTestUtil.createArtifactWithRootRelativePath( |
| 617 | outputRoot, PathFragment.create("bin/output")); |
| 618 | SpecialArtifact treeArtifact = |
| 619 | ActionsTestUtil.createTreeArtifactWithGeneratingAction( |
| 620 | outputRoot, PathFragment.create("bin/tree")); |
| 621 | TreeFileArtifact child = TreeFileArtifact.createTreeOutput(treeArtifact, "child"); |
| 622 | scratch.file(artifact.getPath().getPathString(), "1"); |
| 623 | scratch.file(child.getPath().getPathString(), "1"); |
| 624 | ActionMetadataHandler handler = |
| 625 | createHandler( |
| 626 | new ActionInputMap(0), |
| 627 | /*forInputDiscovery=*/ false, |
| 628 | /*outputs=*/ ImmutableSet.of(artifact, treeArtifact)); |
| 629 | OutputStore store = handler.getOutputStore(); |
| 630 | |
| 631 | FileArtifactValue artifactMetadata1 = handler.getMetadata(artifact); |
| 632 | FileArtifactValue treeArtifactMetadata1 = handler.getMetadata(treeArtifact); |
| 633 | assertThat(artifactMetadata1).isNotNull(); |
| 634 | assertThat(artifactMetadata1).isNotNull(); |
| 635 | assertThat(store.getAllArtifactData().keySet()).containsExactly(artifact); |
| 636 | assertThat(store.getAllTreeArtifactData().keySet()).containsExactly(treeArtifact); |
| 637 | |
| 638 | // Entering execution mode should clear the cached outputs. |
| 639 | handler.prepareForActionExecution(); |
| 640 | assertThat(store.getAllArtifactData()).isEmpty(); |
| 641 | assertThat(store.getAllTreeArtifactData()).isEmpty(); |
| 642 | |
| 643 | // Updated metadata should be read from the filesystem. |
| 644 | scratch.overwriteFile(artifact.getPath().getPathString(), "2"); |
| 645 | scratch.overwriteFile(child.getPath().getPathString(), "2"); |
| 646 | FileArtifactValue artifactMetadata2 = handler.getMetadata(artifact); |
| 647 | FileArtifactValue treeArtifactMetadata2 = handler.getMetadata(treeArtifact); |
| 648 | assertThat(artifactMetadata2).isNotNull(); |
| 649 | assertThat(treeArtifactMetadata2).isNotNull(); |
| 650 | assertThat(artifactMetadata2).isNotEqualTo(artifactMetadata1); |
| 651 | assertThat(treeArtifactMetadata2).isNotEqualTo(treeArtifactMetadata1); |
| 652 | } |
| 653 | |
| 654 | @Test |
| 655 | public void cannotEnterExecutionModeTwice() { |
| 656 | ActionMetadataHandler handler = |
| 657 | createHandler( |
| 658 | new ActionInputMap(0), /*forInputDiscovery=*/ false, /*outputs=*/ ImmutableSet.of()); |
| 659 | handler.prepareForActionExecution(); |
| 660 | assertThrows(IllegalStateException.class, handler::prepareForActionExecution); |
| 661 | } |
| 662 | |
| 663 | @Test |
| 664 | public void fileArtifactValueFromArtifactCompatibleWithGetMetadata_changed() throws Exception { |
| 665 | Artifact artifact = |
| 666 | ActionsTestUtil.createArtifactWithRootRelativePath( |
| 667 | outputRoot, PathFragment.create("bin/output")); |
| 668 | scratch.file(artifact.getPath().getPathString(), "1"); |
| 669 | ActionMetadataHandler handler = |
| 670 | createHandler( |
| 671 | new ActionInputMap(0), |
| 672 | /*forInputDiscovery=*/ false, |
| 673 | /*outputs=*/ ImmutableSet.of(artifact)); |
| 674 | |
| 675 | FileArtifactValue getMetadataResult = handler.getMetadata(artifact); |
| 676 | assertThat(getMetadataResult).isNotNull(); |
| 677 | |
| 678 | scratch.overwriteFile(artifact.getPath().getPathString(), "2"); |
| 679 | FileArtifactValue fileArtifactValueFromArtifactResult = |
| 680 | ActionMetadataHandler.fileArtifactValueFromArtifact( |
| 681 | artifact, /*statNoFollow=*/ null, /*tsgm=*/ null); |
| 682 | assertThat(fileArtifactValueFromArtifactResult).isNotNull(); |
| 683 | |
| 684 | assertThat(fileArtifactValueFromArtifactResult.couldBeModifiedSince(getMetadataResult)) |
| 685 | .isTrue(); |
| 686 | } |
| 687 | |
| 688 | @Test |
| 689 | public void fileArtifactValueFromArtifactCompatibleWithGetMetadata_notChanged() throws Exception { |
| 690 | Artifact artifact = |
| 691 | ActionsTestUtil.createArtifactWithRootRelativePath( |
| 692 | outputRoot, PathFragment.create("bin/output")); |
| 693 | scratch.file(artifact.getPath().getPathString(), "contents"); |
| 694 | ActionMetadataHandler handler = |
| 695 | createHandler( |
| 696 | new ActionInputMap(0), |
| 697 | /*forInputDiscovery=*/ false, |
| 698 | /*outputs=*/ ImmutableSet.of(artifact)); |
| 699 | |
| 700 | FileArtifactValue getMetadataResult = handler.getMetadata(artifact); |
| 701 | assertThat(getMetadataResult).isNotNull(); |
| 702 | |
| 703 | FileArtifactValue fileArtifactValueFromArtifactResult = |
| 704 | ActionMetadataHandler.fileArtifactValueFromArtifact( |
| 705 | artifact, /*statNoFollow=*/ null, /*tsgm=*/ null); |
| 706 | assertThat(fileArtifactValueFromArtifactResult).isNotNull(); |
| 707 | |
| 708 | assertThat(fileArtifactValueFromArtifactResult.couldBeModifiedSince(getMetadataResult)) |
| 709 | .isFalse(); |
| 710 | } |
ulfjack | f2b260c | 2018-09-28 05:09:32 -0700 | [diff] [blame] | 711 | } |