1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
// Copyright 2016 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.skyframe;
import static com.google.common.truth.Truth.assertThat;
import static com.google.devtools.build.lib.actions.ActionInputHelper.asTreeFileArtifacts;
import static org.junit.Assert.fail;
import com.google.common.base.Predicate;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.Action;
import com.google.devtools.build.lib.actions.ActionInputHelper;
import com.google.devtools.build.lib.actions.ActionLookupData;
import com.google.devtools.build.lib.actions.ActionLookupValue;
import com.google.devtools.build.lib.actions.Actions;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact;
import com.google.devtools.build.lib.actions.Artifact.SpecialArtifactType;
import com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact;
import com.google.devtools.build.lib.actions.ArtifactRoot;
import com.google.devtools.build.lib.actions.BasicActionLookupValue;
import com.google.devtools.build.lib.actions.MissingInputFileException;
import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException;
import com.google.devtools.build.lib.actions.cache.DigestUtils;
import com.google.devtools.build.lib.actions.cache.Metadata;
import com.google.devtools.build.lib.actions.util.TestAction.DummyAction;
import com.google.devtools.build.lib.events.NullEventHandler;
import com.google.devtools.build.lib.vfs.FileStatus;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.skyframe.EvaluationResult;
import com.google.devtools.build.skyframe.MemoizingEvaluator;
import com.google.devtools.build.skyframe.SkyFunction;
import com.google.devtools.build.skyframe.SkyFunctionException;
import com.google.devtools.build.skyframe.SkyFunctionException.Transience;
import com.google.devtools.build.skyframe.SkyKey;
import com.google.devtools.build.skyframe.SkyValue;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/**
* Test the behavior of ActionMetadataHandler and ArtifactFunction
* with respect to TreeArtifacts.
*/
@RunWith(JUnit4.class)
public class TreeArtifactMetadataTest extends ArtifactFunctionTestCase {
// A list of subpaths for the SetArtifact created by our custom ActionExecutionFunction.
private List<PathFragment> testTreeArtifactContents;
@Before
public final void setUp() throws Exception {
delegateActionExecutionFunction = new TreeArtifactExecutionFunction();
}
private TreeArtifactValue evaluateTreeArtifact(Artifact treeArtifact,
Iterable<PathFragment> children)
throws Exception {
testTreeArtifactContents = ImmutableList.copyOf(children);
for (PathFragment child : children) {
file(treeArtifact.getPath().getRelative(child), child.toString());
}
return (TreeArtifactValue) evaluateArtifactValue(treeArtifact, /*mandatory=*/ true);
}
private TreeArtifactValue doTestTreeArtifacts(Iterable<PathFragment> children) throws Exception {
SpecialArtifact output = createTreeArtifact("output");
return doTestTreeArtifacts(output, children);
}
private TreeArtifactValue doTestTreeArtifacts(
SpecialArtifact tree, Iterable<PathFragment> children) throws Exception {
TreeArtifactValue value = evaluateTreeArtifact(tree, children);
assertThat(value.getChildPaths()).containsExactlyElementsIn(ImmutableSet.copyOf(children));
assertThat(value.getChildren()).containsExactlyElementsIn(
asTreeFileArtifacts(tree, children));
// Assertions about digest. As of this writing this logic is essentially the same
// as that in TreeArtifact, but it's good practice to unit test anyway to guard against
// breaking changes.
Map<String, Metadata> digestBuilder = new HashMap<>();
for (PathFragment child : children) {
Metadata subdigest = FileArtifactValue.create(tree.getPath().getRelative(child));
digestBuilder.put(child.getPathString(), subdigest);
}
assertThat(DigestUtils.fromMetadata(digestBuilder).getDigestBytesUnsafe())
.isEqualTo(value.getDigest());
return value;
}
@Test
public void testEmptyTreeArtifacts() throws Exception {
TreeArtifactValue value = doTestTreeArtifacts(ImmutableList.<PathFragment>of());
// Additional test, only for this test method: we expect the Metadata is equal to
// the digest [0, 0, ...]
assertThat(value.getMetadata().getDigest()).isEqualTo(value.getDigest());
// Java zero-fills arrays.
assertThat(value.getDigest()).isEqualTo(new byte[16]);
}
@Test
public void testEqualTreeArtifacts() throws Exception {
Artifact treeArtifact = createTreeArtifact("out");
ImmutableList<PathFragment> children =
ImmutableList.of(PathFragment.create("one"), PathFragment.create("two"));
TreeArtifactValue valueOne = evaluateTreeArtifact(treeArtifact, children);
MemoizingEvaluator evaluator = driver.getGraphForTesting();
evaluator.delete(new Predicate<SkyKey>() {
@Override
public boolean apply(SkyKey key) {
// Delete action execution node to force our artifacts to be re-evaluated.
return actions.contains(key.argument());
}
});
TreeArtifactValue valueTwo = evaluateTreeArtifact(treeArtifact, children);
assertThat(valueOne.getDigest()).isNotSameAs(valueTwo.getDigest());
assertThat(valueOne).isEqualTo(valueTwo);
}
@Test
public void testTreeArtifactsWithDigests() throws Exception {
fastDigest = true;
doTestTreeArtifacts(ImmutableList.of(PathFragment.create("one")));
}
@Test
public void testTreeArtifactsWithoutDigests() throws Exception {
fastDigest = false;
doTestTreeArtifacts(ImmutableList.of(PathFragment.create("one")));
}
@Test
public void testTreeArtifactMultipleDigests() throws Exception {
doTestTreeArtifacts(ImmutableList.of(PathFragment.create("one"), PathFragment.create("two")));
}
@Test
public void testIdenticalTreeArtifactsProduceTheSameDigests() throws Exception {
// Make sure different root dirs for set artifacts don't produce different digests.
Artifact one = createTreeArtifact("outOne");
Artifact two = createTreeArtifact("outTwo");
ImmutableList<PathFragment> children =
ImmutableList.of(PathFragment.create("one"), PathFragment.create("two"));
TreeArtifactValue valueOne = evaluateTreeArtifact(one, children);
TreeArtifactValue valueTwo = evaluateTreeArtifact(two, children);
assertThat(valueOne.getDigest()).isEqualTo(valueTwo.getDigest());
}
/**
* Tests that ArtifactFunction rethrows transitive {@link IOException}s as
* {@link MissingInputFileException}s.
*/
@Test
public void testIOExceptionEndToEnd() throws Throwable {
final IOException exception = new IOException("boop");
setupRoot(
new CustomInMemoryFs() {
@Override
public FileStatus stat(Path path, boolean followSymlinks) throws IOException {
if (path.getBaseName().equals("one")) {
throw exception;
}
return super.stat(path, followSymlinks);
}
});
try {
Artifact artifact = createTreeArtifact("outOne");
TreeArtifactValue value = evaluateTreeArtifact(artifact,
ImmutableList.of(PathFragment.create("one")));
fail("MissingInputFileException expected, got " + value);
} catch (Exception e) {
assertThat(Throwables.getRootCause(e)).hasMessageThat().contains(exception.getMessage());
}
}
private void file(Path path, String contents) throws Exception {
FileSystemUtils.createDirectoryAndParents(path.getParentDirectory());
writeFile(path, contents);
}
private SpecialArtifact createTreeArtifact(String path) throws IOException {
PathFragment execPath = PathFragment.create("out").getRelative(path);
Path fullPath = root.getRelative(execPath);
SpecialArtifact output =
new SpecialArtifact(
ArtifactRoot.asDerivedRoot(root, root.getRelative("out")),
execPath,
ALL_OWNER,
SpecialArtifactType.TREE);
actions.add(new DummyAction(ImmutableList.<Artifact>of(), output));
FileSystemUtils.createDirectoryAndParents(fullPath);
return output;
}
private SkyValue evaluateArtifactValue(Artifact artifact, boolean mandatory) throws Exception {
SkyKey key = ArtifactSkyKey.key(artifact, mandatory);
EvaluationResult<SkyValue> result = evaluate(key);
if (result.hasError()) {
throw result.getError().getException();
}
return result.get(key);
}
private void setGeneratingActions() throws InterruptedException, ActionConflictException {
if (evaluator.getExistingValue(ALL_OWNER) == null) {
differencer.inject(
ImmutableMap.of(
ALL_OWNER,
new BasicActionLookupValue(
Actions.filterSharedActionsAndThrowActionConflict(
actionKeyContext, ImmutableList.copyOf(actions)),
false)));
}
}
private <E extends SkyValue> EvaluationResult<E> evaluate(SkyKey... keys)
throws InterruptedException, ActionConflictException {
setGeneratingActions();
return driver.evaluate(
Arrays.asList(keys), /*keepGoing=*/
false,
SkyframeExecutor.DEFAULT_THREAD_COUNT,
NullEventHandler.INSTANCE);
}
private class TreeArtifactExecutionFunction implements SkyFunction {
@Override
public SkyValue compute(SkyKey skyKey, Environment env)
throws SkyFunctionException, InterruptedException {
Map<Artifact, FileValue> fileData = new HashMap<>();
Map<TreeFileArtifact, FileArtifactValue> treeArtifactData = new HashMap<>();
ActionLookupData actionLookupData = (ActionLookupData) skyKey.argument();
ActionLookupValue actionLookupValue =
(ActionLookupValue) env.getValue(actionLookupData.getActionLookupKey());
Action action = actionLookupValue.getAction(actionLookupData.getActionIndex());
SpecialArtifact output = (SpecialArtifact) Iterables.getOnlyElement(action.getOutputs());
for (PathFragment subpath : testTreeArtifactContents) {
try {
TreeFileArtifact suboutput = ActionInputHelper.treeFileArtifact(output, subpath);
FileValue fileValue = ActionMetadataHandler.fileValueFromArtifact(
suboutput, null, null);
fileData.put(suboutput, fileValue);
treeArtifactData.put(suboutput, FileArtifactValue.create(suboutput, fileValue));
} catch (IOException e) {
throw new SkyFunctionException(e, Transience.TRANSIENT) {};
}
}
TreeArtifactValue treeArtifactValue = TreeArtifactValue.create(treeArtifactData);
return new ActionExecutionValue(
fileData,
ImmutableMap.of(output, treeArtifactValue),
ImmutableMap.<Artifact, FileArtifactValue>of());
}
@Override
public String extractTag(SkyKey skyKey) {
return null;
}
}
}
|