From 8c9f87aa187cfb7893e04fddb76e82f6bbb48a89 Mon Sep 17 00:00:00 2001 From: ruperts Date: Wed, 20 Dec 2017 12:02:09 -0800 Subject: Add CommandUsingLinuxSandboxtTest, to test execution statistics for Commands that use the linux-sandbox tool, irrespective of which SpawnRunner they use. RELNOTES: None. PiperOrigin-RevId: 179716067 --- src/test/java/com/google/devtools/build/lib/BUILD | 1 + .../lib/shell/CommandUsingLinuxSandboxTest.java | 136 +++++++++++++++++++++ .../lib/shell/CommandUsingProcessWrapperTest.java | 46 ++----- .../lib/shell/ExecutionStatisticsTestUtil.java | 66 ++++++++++ 4 files changed, 216 insertions(+), 33 deletions(-) create mode 100644 src/test/java/com/google/devtools/build/lib/shell/CommandUsingLinuxSandboxTest.java create mode 100644 src/test/java/com/google/devtools/build/lib/shell/ExecutionStatisticsTestUtil.java (limited to 'src/test/java/com/google/devtools/build') diff --git a/src/test/java/com/google/devtools/build/lib/BUILD b/src/test/java/com/google/devtools/build/lib/BUILD index 0dc8f7b412..d9b1af55bc 100644 --- a/src/test/java/com/google/devtools/build/lib/BUILD +++ b/src/test/java/com/google/devtools/build/lib/BUILD @@ -754,6 +754,7 @@ java_test( ":test_runner", ":testutil", "//src/main/java/com/google/devtools/build/lib:bazel-main", + "//src/main/java/com/google/devtools/build/lib:os_util", "//src/main/java/com/google/devtools/build/lib:runtime", "//src/main/java/com/google/devtools/build/lib:util", "//src/main/java/com/google/devtools/build/lib/collect", diff --git a/src/test/java/com/google/devtools/build/lib/shell/CommandUsingLinuxSandboxTest.java b/src/test/java/com/google/devtools/build/lib/shell/CommandUsingLinuxSandboxTest.java new file mode 100644 index 0000000000..67a0981a7b --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/shell/CommandUsingLinuxSandboxTest.java @@ -0,0 +1,136 @@ +// Copyright 2017 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.shell; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assume.assumeTrue; + +import com.google.common.collect.ImmutableList; +import com.google.devtools.build.lib.runtime.LinuxSandboxUtil; +import com.google.devtools.build.lib.testutil.BlazeTestUtils; +import com.google.devtools.build.lib.testutil.TestConstants; +import com.google.devtools.build.lib.testutil.TestUtils; +import com.google.devtools.build.lib.util.OS; +import java.io.File; +import java.io.IOException; +import java.time.Duration; +import java.util.List; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link Command}s that are run using the {@code linux-sandbox}. */ +@RunWith(JUnit4.class) +public final class CommandUsingLinuxSandboxTest { + private String getLinuxSandboxPath() { + return BlazeTestUtils.runfilesDir() + "/" + TestConstants.LINUX_SANDBOX_PATH; + } + + private String getCpuTimeSpenderPath() { + return BlazeTestUtils.runfilesDir() + "/" + TestConstants.CPU_TIME_SPENDER_PATH; + } + + @Test + public void testCommand_Echo() throws Exception { + ImmutableList commandArguments = ImmutableList.of("echo", "colorless green ideas"); + + Command command = new Command(commandArguments.toArray(new String[0])); + CommandResult commandResult = command.execute(); + + assertThat(commandResult.getTerminationStatus().success()).isTrue(); + assertThat(commandResult.getStdoutStream().toString()).contains("colorless green ideas"); + } + + @Test + public void testLinuxSandboxedCommand_Echo() throws Exception { + // TODO(b/62588075) Currently no linux-sandbox tool support in Windows. + assumeTrue(OS.getCurrent() != OS.WINDOWS); + // TODO(b/62588075) Currently no linux-sandbox tool support in MacOS. + assumeTrue(OS.getCurrent() != OS.DARWIN); + + ImmutableList commandArguments = ImmutableList.of("echo", "sleep furiously"); + + List fullCommandLine = + LinuxSandboxUtil.commandLineBuilder(getLinuxSandboxPath(), commandArguments).build(); + + Command command = new Command(fullCommandLine.toArray(new String[0])); + CommandResult commandResult = command.execute(); + + assertThat(commandResult.getTerminationStatus().success()).isTrue(); + assertThat(commandResult.getStdoutStream().toString()).contains("sleep furiously"); + } + + private void checkLinuxSandboxStatistics(Duration userTimeToSpend, Duration systemTimeToSpend) + throws IOException, CommandException { + ImmutableList commandArguments = + ImmutableList.of( + getCpuTimeSpenderPath(), + Long.toString(userTimeToSpend.getSeconds()), + Long.toString(systemTimeToSpend.getSeconds())); + + File outputDir = TestUtils.makeTempDir(); + String statisticsFilePath = outputDir.getAbsolutePath() + "/" + "stats.out"; + + List fullCommandLine = + LinuxSandboxUtil.commandLineBuilder(getLinuxSandboxPath(), commandArguments) + .setStatisticsPath(statisticsFilePath) + .build(); + + ExecutionStatisticsTestUtil.executeCommandAndCheckStatisticsAboutCpuTimeSpent( + userTimeToSpend, systemTimeToSpend, fullCommandLine, statisticsFilePath); + } + + @Test + public void testLinuxSandboxedCommand_WithStatistics_SpendUserTime() + throws CommandException, IOException { + // TODO(b/62588075) Currently no linux-sandbox tool support in Windows. + assumeTrue(OS.getCurrent() != OS.WINDOWS); + // TODO(b/62588075) Currently no linux-sandbox tool support in MacOS. + assumeTrue(OS.getCurrent() != OS.DARWIN); + + Duration userTimeToSpend = Duration.ofSeconds(10); + Duration systemTimeToSpend = Duration.ZERO; + + checkLinuxSandboxStatistics(userTimeToSpend, systemTimeToSpend); + } + + @Test + public void testLinuxSandboxedCommand_WithStatistics_SpendSystemTime() + throws CommandException, IOException { + // TODO(b/62588075) Currently no linux-sandbox tool support in Windows. + assumeTrue(OS.getCurrent() != OS.WINDOWS); + // TODO(b/62588075) Currently no linux-sandbox tool support in MacOS. + assumeTrue(OS.getCurrent() != OS.DARWIN); + + Duration userTimeToSpend = Duration.ZERO; + Duration systemTimeToSpend = Duration.ofSeconds(10); + + checkLinuxSandboxStatistics(userTimeToSpend, systemTimeToSpend); + } + + @Test + public void testLinuxSandboxedCommand_WithStatistics_SpendUserAndSystemTime() + throws CommandException, IOException { + // TODO(b/62588075) Currently no linux-sandbox tool support in Windows. + assumeTrue(OS.getCurrent() != OS.WINDOWS); + // TODO(b/62588075) Currently no linux-sandbox tool support in MacOS. + assumeTrue(OS.getCurrent() != OS.DARWIN); + + Duration userTimeToSpend = Duration.ofSeconds(10); + Duration systemTimeToSpend = Duration.ofSeconds(10); + + checkLinuxSandboxStatistics(userTimeToSpend, systemTimeToSpend); + } +} diff --git a/src/test/java/com/google/devtools/build/lib/shell/CommandUsingProcessWrapperTest.java b/src/test/java/com/google/devtools/build/lib/shell/CommandUsingProcessWrapperTest.java index 32de79ac49..5028138479 100644 --- a/src/test/java/com/google/devtools/build/lib/shell/CommandUsingProcessWrapperTest.java +++ b/src/test/java/com/google/devtools/build/lib/shell/CommandUsingProcessWrapperTest.java @@ -15,7 +15,6 @@ package com.google.devtools.build.lib.shell; import static com.google.common.truth.Truth.assertThat; -import static com.google.common.truth.Truth8.assertThat; import com.google.common.collect.ImmutableList; import com.google.devtools.build.lib.runtime.ProcessWrapperUtil; @@ -26,7 +25,6 @@ import java.io.File; import java.io.IOException; import java.time.Duration; import java.util.List; -import java.util.Optional; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -57,57 +55,39 @@ public final class CommandUsingProcessWrapperTest { public void testProcessWrappedCommand_Echo() throws Exception { ImmutableList commandArguments = ImmutableList.of("echo", "even drones can fly away"); - List fullProcessWrapperCommandLine = + List fullCommandLine = ProcessWrapperUtil.commandLineBuilder() .setProcessWrapperPath(getProcessWrapperPath()) .setCommandArguments(commandArguments) .build(); - Command command = new Command(fullProcessWrapperCommandLine.toArray(new String[0])); + Command command = new Command(fullCommandLine.toArray(new String[0])); CommandResult commandResult = command.execute(); assertThat(commandResult.getTerminationStatus().success()).isTrue(); assertThat(commandResult.getStdoutStream().toString()).contains("even drones can fly away"); } - private void checkStatisticsAboutCpuTimeSpent( - Duration userTimeToSpend, Duration systemTimeToSpend) throws CommandException, IOException { - Duration userTimeLowerBound = userTimeToSpend; - Duration userTimeUpperBound = userTimeToSpend.plusSeconds(2); - Duration systemTimeLowerBound = systemTimeToSpend; - Duration systemTimeUpperBound = systemTimeToSpend.plusSeconds(2); - - File outputDir = TestUtils.makeTempDir(); - String statisticsFilePath = outputDir.getAbsolutePath() + "/" + "stats.out"; - + private void checkProcessWrapperStatistics(Duration userTimeToSpend, Duration systemTimeToSpend) + throws IOException, CommandException { ImmutableList commandArguments = ImmutableList.of( getCpuTimeSpenderPath(), Long.toString(userTimeToSpend.getSeconds()), Long.toString(systemTimeToSpend.getSeconds())); - List fullProcessWrapperCommandLine = + File outputDir = TestUtils.makeTempDir(); + String statisticsFilePath = outputDir.getAbsolutePath() + "/" + "stats.out"; + + List fullCommandLine = ProcessWrapperUtil.commandLineBuilder() .setProcessWrapperPath(getProcessWrapperPath()) .setCommandArguments(commandArguments) .setStatisticsPath(statisticsFilePath) .build(); - Command command = new Command(fullProcessWrapperCommandLine.toArray(new String[0])); - CommandResult commandResult = command.execute(); - assertThat(commandResult.getTerminationStatus().success()).isTrue(); - - Optional resourceUsage = - ExecutionStatistics.getResourceUsage(statisticsFilePath); - assertThat(resourceUsage).isPresent(); - - Duration userTime = resourceUsage.get().getUserExecutionTime(); - assertThat(userTime).isAtLeast(userTimeLowerBound); - assertThat(userTime).isAtMost(userTimeUpperBound); - - Duration systemTime = resourceUsage.get().getSystemExecutionTime(); - assertThat(systemTime).isAtLeast(systemTimeLowerBound); - assertThat(systemTime).isAtMost(systemTimeUpperBound); + ExecutionStatisticsTestUtil.executeCommandAndCheckStatisticsAboutCpuTimeSpent( + userTimeToSpend, systemTimeToSpend, fullCommandLine, statisticsFilePath); } @Test @@ -116,7 +96,7 @@ public final class CommandUsingProcessWrapperTest { Duration userTimeToSpend = Duration.ofSeconds(10); Duration systemTimeToSpend = Duration.ZERO; - checkStatisticsAboutCpuTimeSpent(userTimeToSpend, systemTimeToSpend); + checkProcessWrapperStatistics(userTimeToSpend, systemTimeToSpend); } @Test @@ -125,7 +105,7 @@ public final class CommandUsingProcessWrapperTest { Duration userTimeToSpend = Duration.ZERO; Duration systemTimeToSpend = Duration.ofSeconds(10); - checkStatisticsAboutCpuTimeSpent(userTimeToSpend, systemTimeToSpend); + checkProcessWrapperStatistics(userTimeToSpend, systemTimeToSpend); } @Test @@ -134,6 +114,6 @@ public final class CommandUsingProcessWrapperTest { Duration userTimeToSpend = Duration.ofSeconds(10); Duration systemTimeToSpend = Duration.ofSeconds(10); - checkStatisticsAboutCpuTimeSpent(userTimeToSpend, systemTimeToSpend); + checkProcessWrapperStatistics(userTimeToSpend, systemTimeToSpend); } } diff --git a/src/test/java/com/google/devtools/build/lib/shell/ExecutionStatisticsTestUtil.java b/src/test/java/com/google/devtools/build/lib/shell/ExecutionStatisticsTestUtil.java new file mode 100644 index 0000000000..e2aafd5252 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/shell/ExecutionStatisticsTestUtil.java @@ -0,0 +1,66 @@ +// Copyright 2017 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.shell; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth8.assertThat; + +import java.io.IOException; +import java.time.Duration; +import java.util.List; +import java.util.Optional; + +/** + * Utilities to assist with testing execution statistics generated via the {@code process-wrapper} + * and {@code linux-sandbox} tools. + */ +public class ExecutionStatisticsTestUtil { + /** + * Executes a command and checks that the execution statistics timing info for that command + * satisfy certain constraints. + * + * @param userTimeToSpend a lower bound for how much CPU user execution time was expected + * @param systemTimeToSpend a lower bound for how much CPU system execution time was expected + * @param fullCommandLine the command to execute, including any wrappers used (like linux-sandbox) + * @param statisticsFilePath where the execution statistics file will be generated (to be read) + */ + public static void executeCommandAndCheckStatisticsAboutCpuTimeSpent( + Duration userTimeToSpend, + Duration systemTimeToSpend, + List fullCommandLine, + String statisticsFilePath) + throws CommandException, IOException { + Duration userTimeLowerBound = userTimeToSpend; + Duration userTimeUpperBound = userTimeToSpend.plusSeconds(2); + Duration systemTimeLowerBound = systemTimeToSpend; + Duration systemTimeUpperBound = systemTimeToSpend.plusSeconds(2); + + Command command = new Command(fullCommandLine.toArray(new String[0])); + CommandResult commandResult = command.execute(); + assertThat(commandResult.getTerminationStatus().success()).isTrue(); + + Optional resourceUsage = + ExecutionStatistics.getResourceUsage(statisticsFilePath); + assertThat(resourceUsage).isPresent(); + + Duration userTime = resourceUsage.get().getUserExecutionTime(); + assertThat(userTime).isAtLeast(userTimeLowerBound); + assertThat(userTime).isAtMost(userTimeUpperBound); + + Duration systemTime = resourceUsage.get().getSystemExecutionTime(); + assertThat(systemTime).isAtLeast(systemTimeLowerBound); + assertThat(systemTime).isAtMost(systemTimeUpperBound); + } +} -- cgit v1.2.3