diff options
author | 2015-11-19 17:35:57 +0000 | |
---|---|---|
committer | 2015-11-20 09:17:15 +0000 | |
commit | dd3ac7dfa1e1b581faf708413462292b2d6124f1 (patch) | |
tree | 4ecb27b6302aa84b27fa4e874ae256500da01fe6 /src/test/java/com/google/devtools | |
parent | d3a5c57a5e375d5f49b62a02ac7ab8a0a016b8e9 (diff) |
Open-source tests from lib/exec
--
MOS_MIGRATED_REVID=108255623
Diffstat (limited to 'src/test/java/com/google/devtools')
6 files changed, 456 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/BUILD b/src/test/java/com/google/devtools/build/lib/BUILD index 3f776a9eb7..0ce1d00fb1 100644 --- a/src/test/java/com/google/devtools/build/lib/BUILD +++ b/src/test/java/com/google/devtools/build/lib/BUILD @@ -912,6 +912,28 @@ java_test( ], ) +java_test( + name = "exec-tests", + srcs = glob(["exec/*.java"]), + args = ["com.google.devtools.build.lib.AllTests"], + tags = ["exec"], + deps = [ + ":actions_testutil", + ":analysis_testutil", + ":foundations_testutil", + ":test_runner", + ":testutil", + "//src/main/java/com/google/devtools/build/lib:analysis-exec-rules-skyframe", + "//src/main/java/com/google/devtools/build/lib:util", + "//src/main/java/com/google/devtools/build/lib:vfs", + "//src/main/java/com/google/devtools/build/lib/actions", + "//third_party:guava", + "//third_party:junit4", + "//third_party:protobuf", + "//third_party:truth", + ], +) + java_library( name = "worker-example-lib", srcs = glob(["worker/ExampleWorker*.java"]), @@ -945,6 +967,7 @@ TEST_SUITES = [ "server", "skyframe", "ideinfo", + "exec", ] [ diff --git a/src/test/java/com/google/devtools/build/lib/exec/AbstractDistributorDriverTest.java b/src/test/java/com/google/devtools/build/lib/exec/AbstractDistributorDriverTest.java new file mode 100644 index 0000000000..8aaf85d1f5 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/exec/AbstractDistributorDriverTest.java @@ -0,0 +1,57 @@ +// Copyright 2014 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.exec; + +import static org.junit.Assert.assertEquals; + +import com.google.common.collect.ImmutableMap; +import com.google.devtools.build.lib.testutil.Suite; +import com.google.devtools.build.lib.testutil.TestSpec; +import com.google.devtools.build.lib.util.UserUtils; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.util.Map; + +/** + * Unit tests for AbstractDistributorDriver. + */ +@RunWith(JUnit4.class) +@TestSpec(size = Suite.SMALL_TESTS) +public class AbstractDistributorDriverTest { + + private static final String USER = "real_user"; + private static final Map<String, String> CLIENT_ENV = ImmutableMap + .of("BLAZE_ORIGINATING_USER", USER); + public static final ImmutableMap<String, String> EMPTY_CLIENT_ENV = ImmutableMap.of(); + + + @Test + public void testGetOriginatingUserFromFlag() throws Exception { + assertEquals(USER, UserUtils.getOriginatingUser(USER, EMPTY_CLIENT_ENV)); + } + + @Test + public void testGetOriginatingUserFromClientEnv() throws Exception { + assertEquals(USER, UserUtils.getOriginatingUser("", CLIENT_ENV)); + } + + @Test + public void testGetOriginatingUserFromUserUtils() { + assertEquals(UserUtils.getUserName(), UserUtils.getOriginatingUser("", EMPTY_CLIENT_ENV)); + } +} diff --git a/src/test/java/com/google/devtools/build/lib/exec/DigestTest.java b/src/test/java/com/google/devtools/build/lib/exec/DigestTest.java new file mode 100644 index 0000000000..c99016655f --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/exec/DigestTest.java @@ -0,0 +1,116 @@ +// 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.exec; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import com.google.devtools.build.lib.actions.cache.VirtualActionInput; +import com.google.devtools.build.lib.testutil.Suite; +import com.google.devtools.build.lib.testutil.TestSpec; +import com.google.devtools.build.lib.util.Pair; +import com.google.protobuf.ByteString; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.ByteBuffer; + +/** + * Tests for Digest. + * + */ +@RunWith(JUnit4.class) +@TestSpec(size = Suite.SMALL_TESTS) +public class DigestTest { + + private static final String UGLY = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLM" + + "NOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; + private static final String UGLY_DIGEST = "e5df5a39f2b8cb71b24e1d8038f93131"; + + @Test + public void testFromString() { + assertEquals( + "d41d8cd98f00b204e9800998ecf8427e", Digest.fromContent("".getBytes(UTF_8)).toStringUtf8()); + assertEquals("d41d8cd98f00b204e9800998ecf8427e", + Digest.fromBuffer(ByteBuffer.wrap(new byte[]{})).toStringUtf8()); + + // Whitespace counts. + assertEquals( + "7215ee9c7d9dc229d2921a40e899ec5f", Digest.fromContent(" ".getBytes(UTF_8)).toStringUtf8()); + assertEquals( + "23b58def11b45727d3351702515f86af", + Digest.fromContent(" ".getBytes(UTF_8)).toStringUtf8()); + + assertEquals( + "8b1a9953c4611296a827abf8c47804d7", + Digest.fromContent("Hello".getBytes(UTF_8)).toStringUtf8()); + + assertEquals(UGLY_DIGEST, Digest.fromContent(UGLY.getBytes()).toStringUtf8()); + + // ByteBuffer digest not idempotent because ByteBuffer manages a "position" internally. + ByteBuffer buffer = ByteBuffer.wrap(UGLY.getBytes(UTF_8)); + assertEquals(UGLY_DIGEST, Digest.fromBuffer(buffer).toStringUtf8()); + assertEquals("d41d8cd98f00b204e9800998ecf8427e", + Digest.fromBuffer(buffer).toStringUtf8()); + buffer.rewind(); + assertEquals("e5df5a39f2b8cb71b24e1d8038f93131", + Digest.fromBuffer(buffer).toStringUtf8()); + } + + @Test + public void testEmpty() { + assertFalse(Digest.isEmpty(ByteString.EMPTY)); + assertTrue(Digest.isEmpty(ByteString.copyFromUtf8("d41d8cd98f00b204e9800998ecf8427e"))); + assertTrue(Digest.isEmpty(Digest.EMPTY_DIGEST)); + assertFalse(Digest.isEmpty(ByteString.copyFromUtf8("xyz"))); + assertFalse(Digest.isEmpty(Digest.fromContent(" ".getBytes(UTF_8)))); + assertEquals("d41d8cd98f00b204e9800998ecf8427e", Digest.EMPTY_DIGEST.toStringUtf8()); + } + + @Test + public void testIsDigest() { + assertFalse(Digest.isDigest(null)); + assertFalse(Digest.isDigest(ByteString.EMPTY)); + assertFalse(Digest.isDigest(ByteString.copyFromUtf8("a"))); + assertFalse(Digest.isDigest(ByteString.copyFromUtf8("xyz"))); + assertTrue(Digest.isDigest(ByteString.copyFromUtf8("8b1a9953c4611296a827abf8c47804d7"))); + } + + @Test + public void testFromVirtualInput() throws Exception{ + Pair<ByteString, Long> result = + Digest.fromVirtualActionInput( + new VirtualActionInput() { + @Override + public void writeTo(OutputStream out) throws IOException { + out.write(UGLY.getBytes(UTF_8)); + } + + @Override + public String getExecPathString() { + throw new UnsupportedOperationException(); + } + }); + assertEquals(UGLY_DIGEST, result.first.toStringUtf8()); + assertEquals(UGLY.length(), result.second.longValue()); + } +} + diff --git a/src/test/java/com/google/devtools/build/lib/exec/MiddlemanActionTest.java b/src/test/java/com/google/devtools/build/lib/exec/MiddlemanActionTest.java new file mode 100644 index 0000000000..22e65ed262 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/exec/MiddlemanActionTest.java @@ -0,0 +1,115 @@ +// 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.exec; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.devtools.build.lib.actions.util.ActionsTestUtil.NULL_ACTION_OWNER; + +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import com.google.devtools.build.lib.actions.Action; +import com.google.devtools.build.lib.actions.Artifact; +import com.google.devtools.build.lib.actions.MiddlemanAction; +import com.google.devtools.build.lib.actions.MiddlemanFactory; +import com.google.devtools.build.lib.analysis.util.AnalysisTestUtil; +import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; +import com.google.devtools.build.lib.testutil.Suite; +import com.google.devtools.build.lib.testutil.TestSpec; + +import java.util.ArrayList; +import java.util.Arrays; + +/** + * A test for {@link MiddlemanAction}. + */ +@TestSpec(size = Suite.SMALL_TESTS) +public class MiddlemanActionTest extends BuildViewTestCase { + + private AnalysisTestUtil.CollectingAnalysisEnvironment analysisEnvironment; + private MiddlemanFactory middlemanFactory; + private Artifact a, b, middle; + + @Override + public void setUp() throws Exception { + super.setUp(); + scratch.file("a/BUILD", + "testing_dummy_rule(name='a', outs=['a.out'])"); + scratch.file("b/BUILD", + "testing_dummy_rule(name='b', outs=['b.out'])"); + a = getFilesToBuild(getConfiguredTarget("//a")).iterator().next(); + b = getFilesToBuild(getConfiguredTarget("//b")).iterator().next(); + analysisEnvironment = + new AnalysisTestUtil.CollectingAnalysisEnvironment( + AnalysisTestUtil.STUB_ANALYSIS_ENVIRONMENT); + middlemanFactory = new MiddlemanFactory(view.getArtifactFactory(), analysisEnvironment); + middle = middlemanFactory.createAggregatingMiddleman( + NULL_ACTION_OWNER, "middleman_test", + Arrays.asList(a, b), + targetConfig.getMiddlemanDirectory()); + analysisEnvironment.registerWith(getMutableActionGraph()); + } + + public void testActionIsAMiddleman() { + Action middleman = getGeneratingAction(middle); + assertTrue("Encountered instance of " + middleman.getClass(), + middleman.getActionType().isMiddleman()); + } + + public void testAAndBAreInputsToMiddleman() { + MiddlemanAction middleman = (MiddlemanAction) getGeneratingAction(middle); + assertThat(middleman.getInputs()).containsExactly(a, b); + } + + public void testMiddleIsOutputOfMiddleman() { + MiddlemanAction middleman = (MiddlemanAction) getGeneratingAction(middle); + assertThat(middleman.getOutputs()).containsExactly(middle); + } + + public void testMiddlemanIsNullForEmptyInputs() throws Exception { + assertNull(middlemanFactory.createAggregatingMiddleman(NULL_ACTION_OWNER, + "middleman_test", new ArrayList<Artifact>(), targetConfig.getMiddlemanDirectory())); + } + + public void testMiddlemanIsIdentityForLonelyInput() throws Exception { + assertEquals(a, + middlemanFactory.createAggregatingMiddleman( + NULL_ACTION_OWNER, "middleman_test", + Lists.newArrayList(a), + targetConfig.getMiddlemanDirectory())); + } + + public void testDifferentExecutablesForRunfilesMiddleman() throws Exception { + scratch.file("c/BUILD", + "testing_dummy_rule(name='c', outs=['c.out', 'd.out', 'common.out'])"); + + Artifact c = getFilesToBuild(getConfiguredTarget("//c:c.out")).iterator().next(); + Artifact d = getFilesToBuild(getConfiguredTarget("//c:d.out")).iterator().next(); + Artifact common = getFilesToBuild(getConfiguredTarget("//c:common.out")).iterator().next(); + + analysisEnvironment.clear(); + Artifact middlemanForC = middlemanFactory.createRunfilesMiddleman( + NULL_ACTION_OWNER, c, Arrays.asList(c, common), targetConfig.getMiddlemanDirectory()); + Artifact middlemanForD = middlemanFactory.createRunfilesMiddleman( + NULL_ACTION_OWNER, d, Arrays.asList(d, common), targetConfig.getMiddlemanDirectory()); + analysisEnvironment.registerWith(getMutableActionGraph()); + + MiddlemanAction middlemanActionForC = (MiddlemanAction) getGeneratingAction(middlemanForC); + MiddlemanAction middlemanActionForD = (MiddlemanAction) getGeneratingAction(middlemanForD); + + assertThat(Sets.newHashSet(middlemanActionForD.getInputs())) + .isNotEqualTo(Sets.newHashSet(middlemanActionForC.getInputs())); + assertThat(Sets.newHashSet(middlemanActionForD.getOutputs())) + .isNotEqualTo(Sets.newHashSet(middlemanActionForC.getOutputs())); + } +} diff --git a/src/test/java/com/google/devtools/build/lib/exec/ParameterFileTest.java b/src/test/java/com/google/devtools/build/lib/exec/ParameterFileTest.java new file mode 100644 index 0000000000..7f07e5a9ad --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/exec/ParameterFileTest.java @@ -0,0 +1,34 @@ +// 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.exec; + +import com.google.devtools.build.lib.actions.ParameterFile; +import com.google.devtools.build.lib.testutil.FoundationTestCase; +import com.google.devtools.build.lib.testutil.Suite; +import com.google.devtools.build.lib.testutil.TestSpec; +import com.google.devtools.build.lib.vfs.PathFragment; + +/** + * Tests for {@link ParameterFile}. + */ +@TestSpec(size = Suite.SMALL_TESTS) +public class ParameterFileTest extends FoundationTestCase { + + public void testDerive() { + assertEquals(new PathFragment("a/b-2.params"), + ParameterFile.derivePath(new PathFragment("a/b"))); + assertEquals(new PathFragment("b-2.params"), ParameterFile.derivePath(new PathFragment("b"))); + } +} diff --git a/src/test/java/com/google/devtools/build/lib/exec/SingleBuildFileCacheTest.java b/src/test/java/com/google/devtools/build/lib/exec/SingleBuildFileCacheTest.java new file mode 100644 index 0000000000..852b8b5c35 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/exec/SingleBuildFileCacheTest.java @@ -0,0 +1,111 @@ +// 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.exec; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.fail; + +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.testutil.Suite; +import com.google.devtools.build.lib.testutil.TestSpec; +import com.google.devtools.build.lib.vfs.FileSystem; +import com.google.devtools.build.lib.vfs.Path; +import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; +import com.google.protobuf.ByteString; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +/** Tests SingleBuildFileCache. */ +@RunWith(JUnit4.class) +@TestSpec(size = Suite.SMALL_TESTS) +public class SingleBuildFileCacheTest { + private FileSystem fs; + private ActionInputFileCache cache; + private Map<String, Integer> calls; + private static final String EMPTY_MD5 = "d41d8cd98f00b204e9800998ecf8427e"; + + @Before + public void setUp() throws Exception { + calls = new HashMap<>(); + fs = new InMemoryFileSystem() { + @Override + protected InputStream getInputStream(Path path) throws IOException { + int c = calls.containsKey(path.toString()) + ? calls.get(path.toString()) : 0; + c++; + calls.put(path.toString(), c); + return super.getInputStream(path); + } + }; + cache = new SingleBuildFileCache("/", fs); + Path root = fs.getRootDirectory(); + Path file = root.getChild("empty"); + file.getOutputStream().close(); + } + + @Test + public void testExceptionsCached() throws Exception { + ActionInput empty = ActionInputHelper.fromPath("/noexist"); + IOException caught = null; + try { + cache.getDigest(empty); + fail("non existent file should raise exception"); + } catch (IOException expected) { + caught = expected; + } + try { + cache.getSizeInBytes(empty); + fail("non existent file should raise exception."); + } catch (IOException expected) { + assertSame(caught, expected); + } + } + + @Test + public void testCache() throws Exception { + ActionInput empty = ActionInputHelper.fromPath("/empty"); + cache.getDigest(empty); + assert(calls.containsKey("/empty")); + assertEquals(1, (int) calls.get("/empty")); + cache.getDigest(empty); + assertEquals(1, (int) calls.get("/empty")); + } + + @Test + public void testBasic() throws Exception { + ActionInput empty = ActionInputHelper.fromPath("/empty"); + assertEquals(0, cache.getSizeInBytes(empty)); + ByteString digest = cache.getDigest(empty); + + assertEquals(EMPTY_MD5, digest.toStringUtf8()); + assertEquals("/empty", cache.getInputFromDigest(digest).getExecPathString()); + assert(cache.contentsAvailableLocally(digest)); + + ByteString other = ByteString.copyFrom("f41d8cd98f00b204e9800998ecf8427e", "UTF-16"); + assert(!cache.contentsAvailableLocally(other)); + assert(calls.containsKey("/empty")); + } +} |