blob: d63edc7bfcbe653f6dcf2d98601d7ee1fee78b76 [file] [log] [blame]
rupertsaf276952017-11-30 00:23:12 -08001// 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
15package com.google.devtools.build.lib.runtime;
16
17import static com.google.common.truth.Truth.assertThat;
rupertsaf276952017-11-30 00:23:12 -080018
rupertsbec2fe82017-12-05 21:53:50 -080019import com.google.common.collect.ImmutableList;
jmmv2bb3b732020-09-03 10:04:27 -070020import com.google.common.collect.ImmutableMap;
21import com.google.devtools.build.lib.actions.ExecutionRequirements;
janakr97c0bd12020-09-08 13:19:03 -070022import com.google.devtools.build.lib.vfs.DigestHashFunction;
Philipp Wollermann23e1c5d2018-03-23 07:39:27 -070023import com.google.devtools.build.lib.vfs.FileSystem;
24import com.google.devtools.build.lib.vfs.Path;
25import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
jmmv511cfd92020-05-11 07:35:04 -070026import java.io.IOException;
rupertsaf276952017-11-30 00:23:12 -080027import java.time.Duration;
rupertsaf276952017-11-30 00:23:12 -080028import java.util.List;
Philipp Wollermann23e1c5d2018-03-23 07:39:27 -070029import org.junit.Before;
rupertsaf276952017-11-30 00:23:12 -080030import org.junit.Test;
31import org.junit.runner.RunWith;
32import org.junit.runners.JUnit4;
33
jmmv511cfd92020-05-11 07:35:04 -070034/** Unit tests for {@link ProcessWrapper}. */
rupertsaf276952017-11-30 00:23:12 -080035@RunWith(JUnit4.class)
jmmv511cfd92020-05-11 07:35:04 -070036public final class ProcessWrapperTest {
37
Philipp Wollermann23e1c5d2018-03-23 07:39:27 -070038 private FileSystem testFS;
rupertsaf276952017-11-30 00:23:12 -080039
Philipp Wollermann23e1c5d2018-03-23 07:39:27 -070040 @Before
jmmv511cfd92020-05-11 07:35:04 -070041 public void setUp() {
janakr97c0bd12020-09-08 13:19:03 -070042 testFS = new InMemoryFileSystem(DigestHashFunction.SHA256);
Philipp Wollermann23e1c5d2018-03-23 07:39:27 -070043 }
rupertsaf276952017-11-30 00:23:12 -080044
jmmv59183b62020-05-12 11:05:09 -070045 private Path makeProcessWrapperBin(String path) throws IOException {
jmmv511cfd92020-05-11 07:35:04 -070046 Path processWrapperPath = testFS.getPath(path);
47 processWrapperPath.getParentDirectory().createDirectoryAndParents();
48 processWrapperPath.getOutputStream().close();
jmmv59183b62020-05-12 11:05:09 -070049 return processWrapperPath;
jmmv511cfd92020-05-11 07:35:04 -070050 }
rupertsaf276952017-11-30 00:23:12 -080051
jmmv511cfd92020-05-11 07:35:04 -070052 @Test
jmmvbfbd95f2020-08-31 13:12:38 -070053 public void testProcessWrapperCommandLineBuilder_buildsWithoutOptionalArguments()
jmmv511cfd92020-05-11 07:35:04 -070054 throws IOException {
rupertsbec2fe82017-12-05 21:53:50 -080055 ImmutableList<String> commandArguments = ImmutableList.of("echo", "hello, world");
rupertsaf276952017-11-30 00:23:12 -080056
rupertsbec2fe82017-12-05 21:53:50 -080057 ImmutableList<String> expectedCommandLine =
jmmv59183b62020-05-12 11:05:09 -070058 ImmutableList.<String>builder().add("/some/path").addAll(commandArguments).build();
rupertsaf276952017-11-30 00:23:12 -080059
jmmv59183b62020-05-12 11:05:09 -070060 ProcessWrapper processWrapper =
jmmv2bb3b732020-09-03 10:04:27 -070061 new ProcessWrapper(
62 makeProcessWrapperBin("/some/path"), /*killDelay=*/ null, /*gracefulSigterm=*/ false);
jmmv511cfd92020-05-11 07:35:04 -070063 List<String> commandLine = processWrapper.commandLineBuilder(commandArguments).build();
rupertsaf276952017-11-30 00:23:12 -080064
ruperts5274d8b2017-12-20 10:45:58 -080065 assertThat(commandLine).containsExactlyElementsIn(expectedCommandLine).inOrder();
rupertsaf276952017-11-30 00:23:12 -080066 }
67
68 @Test
jmmvbfbd95f2020-08-31 13:12:38 -070069 public void testProcessWrapperCommandLineBuilder_buildsWithOptionalArguments()
jmmv511cfd92020-05-11 07:35:04 -070070 throws IOException {
rupertsbec2fe82017-12-05 21:53:50 -080071 ImmutableList<String> commandArguments = ImmutableList.of("echo", "hello, world");
rupertsaf276952017-11-30 00:23:12 -080072
73 Duration timeout = Duration.ofSeconds(10);
74 Duration killDelay = Duration.ofSeconds(2);
Philipp Wollermann23e1c5d2018-03-23 07:39:27 -070075 Path stdoutPath = testFS.getPath("/stdout.txt");
76 Path stderrPath = testFS.getPath("/stderr.txt");
77 Path statisticsPath = testFS.getPath("/stats.out");
rupertsaf276952017-11-30 00:23:12 -080078
rupertsbec2fe82017-12-05 21:53:50 -080079 ImmutableList<String> expectedCommandLine =
80 ImmutableList.<String>builder()
jmmv59183b62020-05-12 11:05:09 -070081 .add("/path/process-wrapper")
rupertsbec2fe82017-12-05 21:53:50 -080082 .add("--timeout=" + timeout.getSeconds())
83 .add("--kill_delay=" + killDelay.getSeconds())
84 .add("--stdout=" + stdoutPath)
85 .add("--stderr=" + stderrPath)
86 .add("--stats=" + statisticsPath)
jmmv2bb3b732020-09-03 10:04:27 -070087 .add("--graceful_sigterm")
rupertsbec2fe82017-12-05 21:53:50 -080088 .addAll(commandArguments)
89 .build();
rupertsaf276952017-11-30 00:23:12 -080090
jmmv59183b62020-05-12 11:05:09 -070091 ProcessWrapper processWrapper =
jmmv2bb3b732020-09-03 10:04:27 -070092 new ProcessWrapper(
93 makeProcessWrapperBin("/path/process-wrapper"), killDelay, /*gracefulSigterm=*/ true);
jmmv59183b62020-05-12 11:05:09 -070094
rupertsaf276952017-11-30 00:23:12 -080095 List<String> commandLine =
jmmv511cfd92020-05-11 07:35:04 -070096 processWrapper
97 .commandLineBuilder(commandArguments)
rupertsaf276952017-11-30 00:23:12 -080098 .setTimeout(timeout)
rupertsaf276952017-11-30 00:23:12 -080099 .setStdoutPath(stdoutPath)
100 .setStderrPath(stderrPath)
rupertsbec2fe82017-12-05 21:53:50 -0800101 .setStatisticsPath(statisticsPath)
rupertsaf276952017-11-30 00:23:12 -0800102 .build();
103
ruperts5274d8b2017-12-20 10:45:58 -0800104 assertThat(commandLine).containsExactlyElementsIn(expectedCommandLine).inOrder();
rupertsaf276952017-11-30 00:23:12 -0800105 }
jmmv2bb3b732020-09-03 10:04:27 -0700106
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 }
rupertsaf276952017-11-30 00:23:12 -0800129}