ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 1 | // Copyright 2017 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.runtime; |
| 16 | |
| 17 | import static com.google.common.truth.Truth.assertThat; |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 18 | |
ruperts | bec2fe8 | 2017-12-05 21:53:50 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
jmmv | 2bb3b73 | 2020-09-03 10:04:27 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableMap; |
| 21 | import com.google.devtools.build.lib.actions.ExecutionRequirements; |
janakr | 97c0bd1 | 2020-09-08 13:19:03 -0700 | [diff] [blame] | 22 | import com.google.devtools.build.lib.vfs.DigestHashFunction; |
Philipp Wollermann | 23e1c5d | 2018-03-23 07:39:27 -0700 | [diff] [blame] | 23 | import com.google.devtools.build.lib.vfs.FileSystem; |
| 24 | import com.google.devtools.build.lib.vfs.Path; |
| 25 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
jmmv | 511cfd9 | 2020-05-11 07:35:04 -0700 | [diff] [blame] | 26 | import java.io.IOException; |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 27 | import java.time.Duration; |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 28 | import java.util.List; |
Philipp Wollermann | 23e1c5d | 2018-03-23 07:39:27 -0700 | [diff] [blame] | 29 | import org.junit.Before; |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 30 | import org.junit.Test; |
| 31 | import org.junit.runner.RunWith; |
| 32 | import org.junit.runners.JUnit4; |
| 33 | |
jmmv | 511cfd9 | 2020-05-11 07:35:04 -0700 | [diff] [blame] | 34 | /** Unit tests for {@link ProcessWrapper}. */ |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 35 | @RunWith(JUnit4.class) |
jmmv | 511cfd9 | 2020-05-11 07:35:04 -0700 | [diff] [blame] | 36 | public final class ProcessWrapperTest { |
| 37 | |
Philipp Wollermann | 23e1c5d | 2018-03-23 07:39:27 -0700 | [diff] [blame] | 38 | private FileSystem testFS; |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 39 | |
Philipp Wollermann | 23e1c5d | 2018-03-23 07:39:27 -0700 | [diff] [blame] | 40 | @Before |
jmmv | 511cfd9 | 2020-05-11 07:35:04 -0700 | [diff] [blame] | 41 | public void setUp() { |
janakr | 97c0bd1 | 2020-09-08 13:19:03 -0700 | [diff] [blame] | 42 | testFS = new InMemoryFileSystem(DigestHashFunction.SHA256); |
Philipp Wollermann | 23e1c5d | 2018-03-23 07:39:27 -0700 | [diff] [blame] | 43 | } |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 44 | |
jmmv | 59183b6 | 2020-05-12 11:05:09 -0700 | [diff] [blame] | 45 | private Path makeProcessWrapperBin(String path) throws IOException { |
jmmv | 511cfd9 | 2020-05-11 07:35:04 -0700 | [diff] [blame] | 46 | Path processWrapperPath = testFS.getPath(path); |
| 47 | processWrapperPath.getParentDirectory().createDirectoryAndParents(); |
| 48 | processWrapperPath.getOutputStream().close(); |
jmmv | 59183b6 | 2020-05-12 11:05:09 -0700 | [diff] [blame] | 49 | return processWrapperPath; |
jmmv | 511cfd9 | 2020-05-11 07:35:04 -0700 | [diff] [blame] | 50 | } |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 51 | |
jmmv | 511cfd9 | 2020-05-11 07:35:04 -0700 | [diff] [blame] | 52 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 53 | public void testProcessWrapperCommandLineBuilder_buildsWithoutOptionalArguments() |
jmmv | 511cfd9 | 2020-05-11 07:35:04 -0700 | [diff] [blame] | 54 | throws IOException { |
ruperts | bec2fe8 | 2017-12-05 21:53:50 -0800 | [diff] [blame] | 55 | ImmutableList<String> commandArguments = ImmutableList.of("echo", "hello, world"); |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 56 | |
ruperts | bec2fe8 | 2017-12-05 21:53:50 -0800 | [diff] [blame] | 57 | ImmutableList<String> expectedCommandLine = |
jmmv | 59183b6 | 2020-05-12 11:05:09 -0700 | [diff] [blame] | 58 | ImmutableList.<String>builder().add("/some/path").addAll(commandArguments).build(); |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 59 | |
jmmv | 59183b6 | 2020-05-12 11:05:09 -0700 | [diff] [blame] | 60 | ProcessWrapper processWrapper = |
jmmv | 2bb3b73 | 2020-09-03 10:04:27 -0700 | [diff] [blame] | 61 | new ProcessWrapper( |
| 62 | makeProcessWrapperBin("/some/path"), /*killDelay=*/ null, /*gracefulSigterm=*/ false); |
jmmv | 511cfd9 | 2020-05-11 07:35:04 -0700 | [diff] [blame] | 63 | List<String> commandLine = processWrapper.commandLineBuilder(commandArguments).build(); |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 64 | |
ruperts | 5274d8b | 2017-12-20 10:45:58 -0800 | [diff] [blame] | 65 | assertThat(commandLine).containsExactlyElementsIn(expectedCommandLine).inOrder(); |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 69 | public void testProcessWrapperCommandLineBuilder_buildsWithOptionalArguments() |
jmmv | 511cfd9 | 2020-05-11 07:35:04 -0700 | [diff] [blame] | 70 | throws IOException { |
ruperts | bec2fe8 | 2017-12-05 21:53:50 -0800 | [diff] [blame] | 71 | ImmutableList<String> commandArguments = ImmutableList.of("echo", "hello, world"); |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 72 | |
| 73 | Duration timeout = Duration.ofSeconds(10); |
| 74 | Duration killDelay = Duration.ofSeconds(2); |
Philipp Wollermann | 23e1c5d | 2018-03-23 07:39:27 -0700 | [diff] [blame] | 75 | Path stdoutPath = testFS.getPath("/stdout.txt"); |
| 76 | Path stderrPath = testFS.getPath("/stderr.txt"); |
| 77 | Path statisticsPath = testFS.getPath("/stats.out"); |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 78 | |
ruperts | bec2fe8 | 2017-12-05 21:53:50 -0800 | [diff] [blame] | 79 | ImmutableList<String> expectedCommandLine = |
| 80 | ImmutableList.<String>builder() |
jmmv | 59183b6 | 2020-05-12 11:05:09 -0700 | [diff] [blame] | 81 | .add("/path/process-wrapper") |
ruperts | bec2fe8 | 2017-12-05 21:53:50 -0800 | [diff] [blame] | 82 | .add("--timeout=" + timeout.getSeconds()) |
| 83 | .add("--kill_delay=" + killDelay.getSeconds()) |
| 84 | .add("--stdout=" + stdoutPath) |
| 85 | .add("--stderr=" + stderrPath) |
| 86 | .add("--stats=" + statisticsPath) |
jmmv | 2bb3b73 | 2020-09-03 10:04:27 -0700 | [diff] [blame] | 87 | .add("--graceful_sigterm") |
ruperts | bec2fe8 | 2017-12-05 21:53:50 -0800 | [diff] [blame] | 88 | .addAll(commandArguments) |
| 89 | .build(); |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 90 | |
jmmv | 59183b6 | 2020-05-12 11:05:09 -0700 | [diff] [blame] | 91 | ProcessWrapper processWrapper = |
jmmv | 2bb3b73 | 2020-09-03 10:04:27 -0700 | [diff] [blame] | 92 | new ProcessWrapper( |
| 93 | makeProcessWrapperBin("/path/process-wrapper"), killDelay, /*gracefulSigterm=*/ true); |
jmmv | 59183b6 | 2020-05-12 11:05:09 -0700 | [diff] [blame] | 94 | |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 95 | List<String> commandLine = |
jmmv | 511cfd9 | 2020-05-11 07:35:04 -0700 | [diff] [blame] | 96 | processWrapper |
| 97 | .commandLineBuilder(commandArguments) |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 98 | .setTimeout(timeout) |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 99 | .setStdoutPath(stdoutPath) |
| 100 | .setStderrPath(stderrPath) |
ruperts | bec2fe8 | 2017-12-05 21:53:50 -0800 | [diff] [blame] | 101 | .setStatisticsPath(statisticsPath) |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 102 | .build(); |
| 103 | |
ruperts | 5274d8b | 2017-12-20 10:45:58 -0800 | [diff] [blame] | 104 | assertThat(commandLine).containsExactlyElementsIn(expectedCommandLine).inOrder(); |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 105 | } |
jmmv | 2bb3b73 | 2020-09-03 10:04:27 -0700 | [diff] [blame] | 106 | |
| 107 | @Test |
| 108 | public void testProcessWrapperCommandLineBuilder_withExecutionInfo() throws IOException { |
| 109 | ImmutableList<String> commandArguments = ImmutableList.of("echo", "hello, world"); |
| 110 | |
| 111 | ProcessWrapper processWrapper = |
| 112 | new ProcessWrapper( |
| 113 | makeProcessWrapperBin("/some/path"), /*killDelay=*/ null, /*gracefulSigterm=*/ false); |
| 114 | ProcessWrapper.CommandLineBuilder builder = processWrapper.commandLineBuilder(commandArguments); |
| 115 | |
| 116 | ImmutableList<String> expectedWithoutExecutionInfo = |
| 117 | ImmutableList.<String>builder().add("/some/path").addAll(commandArguments).build(); |
| 118 | assertThat(builder.build()).containsExactlyElementsIn(expectedWithoutExecutionInfo).inOrder(); |
| 119 | |
| 120 | ImmutableList<String> expectedWithExecutionInfo = |
| 121 | ImmutableList.<String>builder() |
| 122 | .add("/some/path") |
| 123 | .add("--graceful_sigterm") |
| 124 | .addAll(commandArguments) |
| 125 | .build(); |
| 126 | builder.addExecutionInfo(ImmutableMap.of(ExecutionRequirements.GRACEFUL_TERMINATION, "1")); |
| 127 | assertThat(builder.build()).containsExactlyElementsIn(expectedWithExecutionInfo).inOrder(); |
| 128 | } |
ruperts | af27695 | 2017-11-30 00:23:12 -0800 | [diff] [blame] | 129 | } |