|
| 1 | +package build.buildfarm.worker.util; |
| 2 | + |
| 3 | +import static build.buildfarm.worker.util.InputsIndexer.BAZEL_TOOL_INPUT_MARKER; |
| 4 | +import static com.google.common.truth.Truth.assertThat; |
| 5 | + |
| 6 | +import build.bazel.remote.execution.v2.*; |
| 7 | +import build.buildfarm.common.DigestUtil; |
| 8 | +import build.buildfarm.v1test.Tree; |
| 9 | +import com.google.common.collect.ImmutableMap; |
| 10 | +import com.google.devtools.build.lib.worker.WorkerProtocol.Input; |
| 11 | +import com.google.protobuf.ByteString; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.nio.file.Paths; |
| 14 | +import java.util.stream.Collectors; |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.runner.RunWith; |
| 17 | +import org.junit.runners.JUnit4; |
| 18 | + |
| 19 | +@RunWith(JUnit4.class) |
| 20 | +public class InputsIndexerTest { |
| 21 | + private final DigestUtil DIGEST_UTIL = new DigestUtil(DigestUtil.HashFunction.SHA256); |
| 22 | + |
| 23 | + @Test |
| 24 | + public void basicEmptyTree() { |
| 25 | + Tree emptyTree = Tree.newBuilder().build(); |
| 26 | + InputsIndexer indexer = new InputsIndexer(emptyTree); |
| 27 | + assertThat(indexer.tree).isEqualTo(emptyTree); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void canGetRootDir() { |
| 32 | + Tree.Builder treeBuilder = Tree.newBuilder(); |
| 33 | + |
| 34 | + Directory rootDir = Directory.getDefaultInstance(); |
| 35 | + Digest rootDirDigest = addDirToTree(treeBuilder, "my_root_dir", rootDir); |
| 36 | + treeBuilder.setRootDigest(rootDirDigest); |
| 37 | + |
| 38 | + InputsIndexer indexer = new InputsIndexer(treeBuilder.build()); |
| 39 | + assertThat(indexer.proxyDirs.get(rootDirDigest)).isEqualTo(rootDir); |
| 40 | + |
| 41 | + Path arbitraryOpRoot = Paths.get("."); |
| 42 | + assertThat(indexer.getAllInputs(arbitraryOpRoot).size()).isEqualTo(0); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void rootDirWithFiles() { |
| 47 | + Tree.Builder treeBuilder = Tree.newBuilder(); |
| 48 | + |
| 49 | + FileNode myfile = |
| 50 | + makeFileNode("my_file", "my file contents", NodeProperties.getDefaultInstance()); |
| 51 | + Directory rootDir = Directory.newBuilder().addFiles(myfile).build(); |
| 52 | + Digest rootDirDigest = addDirToTree(treeBuilder, "my_root_dir", rootDir); |
| 53 | + treeBuilder.setRootDigest(rootDirDigest); |
| 54 | + |
| 55 | + InputsIndexer indexer = new InputsIndexer(treeBuilder.build()); |
| 56 | + assertThat(indexer.proxyDirs.get(rootDirDigest)).isEqualTo(rootDir); |
| 57 | + |
| 58 | + Path arbitraryOpRoot = Paths.get("asdf"); |
| 59 | + Input myfileInput = makeInput(arbitraryOpRoot, myfile); |
| 60 | + |
| 61 | + ImmutableMap<Path, Input> expectedInputs = |
| 62 | + ImmutableMap.of(Paths.get(myfileInput.getPath()), myfileInput); |
| 63 | + |
| 64 | + assertThat(indexer.getAllInputs(arbitraryOpRoot)).isEqualTo(expectedInputs); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void canRecurseAndDistinguishToolInputs() { |
| 69 | + Tree.Builder treeBuilder = Tree.newBuilder(); |
| 70 | + |
| 71 | + FileNode myfile = |
| 72 | + makeFileNode("my_file", "my file contents", NodeProperties.getDefaultInstance()); |
| 73 | + FileNode subdirfile = |
| 74 | + makeFileNode("subdir_file", "my subdir file contents", NodeProperties.getDefaultInstance()); |
| 75 | + FileNode toolfile = |
| 76 | + makeFileNode( |
| 77 | + "tool_file", |
| 78 | + "my tool file contents", |
| 79 | + makeNodeProperties(ImmutableMap.of(BAZEL_TOOL_INPUT_MARKER, "value doesn't matter"))); |
| 80 | + |
| 81 | + Directory subDir = Directory.newBuilder().addFiles(subdirfile).build(); |
| 82 | + String subDirName = "my_sub_dir"; |
| 83 | + Digest subDirDigest = addDirToTree(treeBuilder, subDirName, subDir); |
| 84 | + |
| 85 | + Directory rootDir = |
| 86 | + Directory.newBuilder() |
| 87 | + .addFiles(myfile) |
| 88 | + .addFiles(toolfile) |
| 89 | + .addDirectories(makeDirNode(subDirName, subDirDigest)) |
| 90 | + .build(); |
| 91 | + |
| 92 | + Digest rootDirDigest = addDirToTree(treeBuilder, "my_root_dir", rootDir); |
| 93 | + treeBuilder.setRootDigest(rootDirDigest); |
| 94 | + |
| 95 | + InputsIndexer indexer = new InputsIndexer(treeBuilder.build()); |
| 96 | + assertThat(indexer.proxyDirs.get(rootDirDigest)).isEqualTo(rootDir); |
| 97 | + assertThat(indexer.proxyDirs.size()).isEqualTo(2); |
| 98 | + |
| 99 | + Path arbitraryOpRoot = Paths.get("asdf"); |
| 100 | + Input myfileInput = makeInput(arbitraryOpRoot, myfile); |
| 101 | + Input subdirfileInput = makeInput(arbitraryOpRoot.resolve(subDirName), subdirfile); |
| 102 | + Input toolfileInput = makeInput(arbitraryOpRoot, toolfile); |
| 103 | + |
| 104 | + ImmutableMap<Path, Input> nonToolInputs = |
| 105 | + ImmutableMap.of( |
| 106 | + Paths.get(myfileInput.getPath()), |
| 107 | + myfileInput, |
| 108 | + Paths.get(subdirfileInput.getPath()), |
| 109 | + subdirfileInput); |
| 110 | + ImmutableMap<Path, Input> toolInputs = |
| 111 | + ImmutableMap.of(Paths.get(toolfileInput.getPath()), toolfileInput); |
| 112 | + ImmutableMap<Path, Input> allInputs = |
| 113 | + ImmutableMap.<Path, Input>builder().putAll(nonToolInputs).putAll(toolInputs).build(); |
| 114 | + |
| 115 | + assertThat(indexer.getAllInputs(arbitraryOpRoot)).isEqualTo(allInputs); |
| 116 | + assertThat(indexer.getAllInputs(arbitraryOpRoot).size()).isEqualTo(3); |
| 117 | + assertThat(indexer.getToolInputs(arbitraryOpRoot)).isEqualTo(toolInputs); |
| 118 | + } |
| 119 | + |
| 120 | + Digest addDirToTree(Tree.Builder treeBuilder, String dirname, Directory dir) { |
| 121 | + ByteString dirnameBytes = ByteString.copyFromUtf8(dirname); |
| 122 | + Digest digest = DIGEST_UTIL.compute(dirnameBytes); |
| 123 | + String hash = digest.getHash(); |
| 124 | + treeBuilder.putDirectories(hash, dir); |
| 125 | + return digest; |
| 126 | + } |
| 127 | + |
| 128 | + FileNode makeFileNode(String filename, String content, NodeProperties nodeProperties) { |
| 129 | + return FileNode.newBuilder() |
| 130 | + .setName(filename) |
| 131 | + .setDigest(DIGEST_UTIL.compute(ByteString.copyFromUtf8(content))) |
| 132 | + .setIsExecutable(false) |
| 133 | + .setNodeProperties(nodeProperties) |
| 134 | + .build(); |
| 135 | + } |
| 136 | + |
| 137 | + DirectoryNode makeDirNode(String dirname, Digest dirDigest) { |
| 138 | + // Pretty sure we don't need the actual hash for our testing purposes |
| 139 | + return DirectoryNode.newBuilder().setName(dirname).setDigest(dirDigest).build(); |
| 140 | + } |
| 141 | + |
| 142 | + NodeProperties makeNodeProperties(ImmutableMap<String, String> props) { |
| 143 | + return NodeProperties.newBuilder() |
| 144 | + .addAllProperties( |
| 145 | + props.entrySet().stream() |
| 146 | + .map( |
| 147 | + kv -> |
| 148 | + NodeProperty.newBuilder() |
| 149 | + .setName(kv.getKey()) |
| 150 | + .setValue(kv.getValue()) |
| 151 | + .build()) |
| 152 | + .collect(Collectors.toList())) |
| 153 | + .build(); |
| 154 | + } |
| 155 | + |
| 156 | + Input makeInput(Path fileDir, FileNode file) { |
| 157 | + Path fileNodePath = fileDir.resolve(file.getName()); |
| 158 | + return Input.newBuilder() |
| 159 | + .setPath(fileNodePath.toString()) |
| 160 | + .setDigest(file.getDigest().getHashBytes()) |
| 161 | + .build(); |
| 162 | + } |
| 163 | +} |
0 commit comments