aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/remote/TreeNodeRepositoryTest.java
blob: 48953ce2f99f02c70e3fe8fc430a97c16fd58f62 (plain)
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
// Copyright 2015 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.remote;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableCollection;
import com.google.devtools.build.lib.actions.ActionInput;
import com.google.devtools.build.lib.actions.ActionInputFileCache;
import com.google.devtools.build.lib.actions.ActionInputHelper;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.ArtifactRoot;
import com.google.devtools.build.lib.clock.BlazeClock;
import com.google.devtools.build.lib.exec.SingleBuildFileCache;
import com.google.devtools.build.lib.remote.TreeNodeRepository.TreeNode;
import com.google.devtools.build.lib.testutil.Scratch;
import com.google.devtools.build.lib.vfs.FileSystem.HashFunction;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
import com.google.devtools.remoteexecution.v1test.Digest;
import com.google.devtools.remoteexecution.v1test.Directory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.SortedMap;
import java.util.TreeMap;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for {@link TreeNodeRepository}. */
@RunWith(JUnit4.class)
public class TreeNodeRepositoryTest {
  private Scratch scratch;
  private DigestUtil digestUtil;
  private Path execRoot;
  private ArtifactRoot rootDir;

  @Before
  public final void setRootDir() throws Exception {
    digestUtil = new DigestUtil(HashFunction.SHA256);
    scratch = new Scratch(new InMemoryFileSystem(BlazeClock.instance(), HashFunction.SHA256));
    execRoot = scratch.getFileSystem().getPath("/exec/root");
    rootDir = ArtifactRoot.asSourceRoot(scratch.dir("/exec/root"));
  }

  private TreeNodeRepository createTestTreeNodeRepository() {
    ActionInputFileCache inputFileCache =
        new SingleBuildFileCache(execRoot.getPathString(), scratch.getFileSystem());
    return new TreeNodeRepository(execRoot, inputFileCache, digestUtil);
  }

  private TreeNode buildFromActionInputs(TreeNodeRepository repo, ActionInput... inputs)
      throws IOException {
    TreeMap<PathFragment, ActionInput> sortedMap = new TreeMap<>();
    for (ActionInput input : inputs) {
      sortedMap.put(PathFragment.create(input.getExecPathString()), input);
    }
    return repo.buildFromActionInputs(sortedMap);
  }

  @Test
  @SuppressWarnings("ReferenceEquality")
  public void testSubtreeReusage() throws Exception {
    Artifact fooCc = new Artifact(scratch.file("/exec/root/a/foo.cc"), rootDir);
    Artifact fooH = new Artifact(scratch.file("/exec/root/a/foo.h"), rootDir);
    Artifact bar = new Artifact(scratch.file("/exec/root/b/bar.txt"), rootDir);
    Artifact baz = new Artifact(scratch.file("/exec/root/c/baz.txt"), rootDir);
    TreeNodeRepository repo = createTestTreeNodeRepository();
    TreeNode root1 = buildFromActionInputs(repo, fooCc, fooH, bar);
    TreeNode root2 = buildFromActionInputs(repo, fooCc, fooH, baz);
    // Reusing same node for the "a" subtree.
    assertThat(
            root1.getChildEntries().get(0).getChild() == root2.getChildEntries().get(0).getChild())
        .isTrue();
  }

  @Test
  public void testMerkleDigests() throws Exception {
    Artifact foo = new Artifact(scratch.file("/exec/root/a/foo", "1"), rootDir);
    Artifact bar = new Artifact(scratch.file("/exec/root/a/bar", "11"), rootDir);
    TreeNodeRepository repo = createTestTreeNodeRepository();
    TreeNode root = buildFromActionInputs(repo, foo, bar);
    TreeNode aNode = root.getChildEntries().get(0).getChild();
    TreeNode fooNode = aNode.getChildEntries().get(1).getChild(); // foo > bar in sort order!
    TreeNode barNode = aNode.getChildEntries().get(0).getChild();

    repo.computeMerkleDigests(root);
    ImmutableCollection<Digest> digests = repo.getAllDigests(root);
    Digest rootDigest = repo.getMerkleDigest(root);
    Digest aDigest = repo.getMerkleDigest(aNode);
    Digest fooDigest = repo.getMerkleDigest(fooNode); // The contents digest.
    Digest barDigest = repo.getMerkleDigest(barNode);
    assertThat(digests).containsExactly(rootDigest, aDigest, barDigest, fooDigest);

    ArrayList<Directory> directories = new ArrayList<>();
    ArrayList<ActionInput> actionInputs = new ArrayList<>();
    repo.getDataFromDigests(digests, actionInputs, directories);
    assertThat(actionInputs).containsExactly(bar, foo);
    assertThat(directories).hasSize(2);
    Directory rootDirectory = directories.get(0);
    assertThat(rootDirectory.getDirectories(0).getName()).isEqualTo("a");
    assertThat(rootDirectory.getDirectories(0).getDigest()).isEqualTo(aDigest);
    Directory aDirectory = directories.get(1);
    assertThat(aDirectory.getFiles(0).getName()).isEqualTo("bar");
    assertThat(aDirectory.getFiles(0).getDigest()).isEqualTo(barDigest);
    assertThat(aDirectory.getFiles(1).getName()).isEqualTo("foo");
    assertThat(aDirectory.getFiles(1).getDigest()).isEqualTo(fooDigest);
  }

  @Test
  public void testGetAllDigests() throws Exception {
    Artifact foo1 = new Artifact(scratch.file("/exec/root/a/foo", "1"), rootDir);
    Artifact foo2 = new Artifact(scratch.file("/exec/root/b/foo", "1"), rootDir);
    Artifact foo3 = new Artifact(scratch.file("/exec/root/c/foo", "1"), rootDir);
    TreeNodeRepository repo = createTestTreeNodeRepository();
    TreeNode root = buildFromActionInputs(repo, foo1, foo2, foo3);
    repo.computeMerkleDigests(root);
    // Reusing same node for the "foo" subtree: only need the root, root child, and foo contents:
    assertThat(repo.getAllDigests(root)).hasSize(3);
  }

  @Test
  public void testEmptyTree() throws Exception {
    SortedMap<PathFragment, ActionInput> inputs = new TreeMap<>();
    TreeNodeRepository repo = createTestTreeNodeRepository();
    TreeNode root = repo.buildFromActionInputs(inputs);
    repo.computeMerkleDigests(root);

    assertThat(root.getChildEntries()).isEmpty();
  }

  @Test
  public void testDirectoryInput() throws Exception {
    Artifact foo = new Artifact(scratch.dir("/exec/root/a/foo"), rootDir);
    scratch.file("/exec/root/a/foo/foo.h", "1");
    ActionInput fooH = ActionInputHelper.fromPath("/exec/root/a/foo/foo.h");
    scratch.file("/exec/root/a/foo/foo.cc", "2");
    ActionInput fooCc = ActionInputHelper.fromPath("/exec/root/a/foo/foo.cc");
    Artifact bar = new Artifact(scratch.file("/exec/root/a/bar.txt"), rootDir);
    TreeNodeRepository repo = createTestTreeNodeRepository();

    TreeNode root = buildFromActionInputs(repo, foo, bar);
    TreeNode aNode = root.getChildEntries().get(0).getChild();
    TreeNode fooNode = aNode.getChildEntries().get(1).getChild(); // foo > bar in sort order!
    TreeNode barNode = aNode.getChildEntries().get(0).getChild();

    TreeNode fooHNode =
        fooNode.getChildEntries().get(1).getChild(); // foo.h > foo.cc in sort order!
    TreeNode fooCcNode = fooNode.getChildEntries().get(0).getChild();

    repo.computeMerkleDigests(root);
    ImmutableCollection<Digest> digests = repo.getAllDigests(root);
    Digest rootDigest = repo.getMerkleDigest(root);
    Digest aDigest = repo.getMerkleDigest(aNode);
    Digest fooDigest = repo.getMerkleDigest(fooNode);
    Digest fooHDigest = repo.getMerkleDigest(fooHNode);
    Digest fooCcDigest = repo.getMerkleDigest(fooCcNode);
    Digest barDigest = repo.getMerkleDigest(barNode);
    assertThat(digests)
        .containsExactly(rootDigest, aDigest, barDigest, fooDigest, fooHDigest, fooCcDigest);

    ArrayList<Directory> directories = new ArrayList<>();
    ArrayList<ActionInput> actionInputs = new ArrayList<>();
    repo.getDataFromDigests(digests, actionInputs, directories);
    assertThat(actionInputs).containsExactly(bar, fooH, fooCc);
    assertThat(directories).hasSize(3); // root, root/a and root/a/foo
    Directory rootDirectory = directories.get(0);
    assertThat(rootDirectory.getDirectories(0).getName()).isEqualTo("a");
    assertThat(rootDirectory.getDirectories(0).getDigest()).isEqualTo(aDigest);
    Directory aDirectory = directories.get(1);
    assertThat(aDirectory.getFiles(0).getName()).isEqualTo("bar.txt");
    assertThat(aDirectory.getFiles(0).getDigest()).isEqualTo(barDigest);
    assertThat(aDirectory.getDirectories(0).getName()).isEqualTo("foo");
    assertThat(aDirectory.getDirectories(0).getDigest()).isEqualTo(fooDigest);
    Directory fooDirectory = directories.get(2);
    assertThat(fooDirectory.getFiles(0).getName()).isEqualTo("foo.cc");
    assertThat(fooDirectory.getFiles(0).getDigest()).isEqualTo(fooCcDigest);
    assertThat(fooDirectory.getFiles(1).getName()).isEqualTo("foo.h");
    assertThat(fooDirectory.getFiles(1).getDigest()).isEqualTo(fooHDigest);
  }
}