Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 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 | package com.google.devtools.build.lib.shell; |
| 15 | |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertWithMessage; |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 18 | import static com.google.devtools.build.lib.shell.TestUtil.assertArrayEquals; |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 19 | import static org.junit.Assert.fail; |
| 20 | |
Dmitry Lomov | 8efc3ef | 2016-02-17 09:50:03 +0000 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableMap; |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.testutil.BlazeTestUtils; |
| 23 | import com.google.devtools.build.lib.testutil.TestConstants; |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 24 | import java.io.ByteArrayInputStream; |
| 25 | import java.io.ByteArrayOutputStream; |
| 26 | import java.io.File; |
| 27 | import java.io.IOException; |
| 28 | import java.io.InputStream; |
| 29 | import java.io.OutputStream; |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 30 | import java.time.Duration; |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 31 | import java.util.Collections; |
| 32 | import java.util.Map; |
| 33 | import java.util.logging.Level; |
| 34 | import java.util.logging.Logger; |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | import org.junit.runners.JUnit4; |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 39 | |
| 40 | /** |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 41 | * Unit tests for {@link Command}. This test will only succeed on Linux, currently, because of its |
| 42 | * non-portable nature. |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 43 | */ |
| 44 | @RunWith(JUnit4.class) |
| 45 | public class CommandTest { |
| 46 | |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 47 | // Platform-independent tests ---------------------------------------------- |
| 48 | |
| 49 | @Before |
Florian Weikert | 2c71a82 | 2015-12-02 17:22:38 +0000 | [diff] [blame] | 50 | public final void configureLogger() throws Exception { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 51 | // Enable all log statements to ensure there are no problems with logging code. |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 52 | Logger.getLogger("com.google.devtools.build.lib.shell.Command").setLevel(Level.FINEST); |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void testIllegalArgs() throws Exception { |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 57 | try { |
| 58 | new Command((String[]) null); |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 59 | fail("Should have thrown NullPointerException"); |
| 60 | } catch (NullPointerException iae) { |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 61 | // good |
| 62 | } |
| 63 | |
| 64 | try { |
| 65 | new Command(new String[] {"/bin/true", null}).execute(); |
| 66 | fail("Should have thrown NullPointerException"); |
| 67 | } catch (NullPointerException npe) { |
| 68 | // good |
| 69 | } |
| 70 | |
| 71 | try { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 72 | Command r = new Command(new String[] {"foo"}); |
| 73 | r.executeAsync((InputStream) null, Command.KILL_SUBPROCESS_ON_INTERRUPT).get(); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 74 | fail("Should have thrown NullPointerException"); |
| 75 | } catch (NullPointerException npe) { |
| 76 | // good |
| 77 | } |
| 78 | |
| 79 | } |
| 80 | |
| 81 | @Test |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 82 | public void testGetters() { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 83 | File workingDir = new File("."); |
| 84 | Map<String, String> env = Collections.singletonMap("foo", "bar"); |
| 85 | String[] commandArgs = new String[] { "command" }; |
| 86 | Command command = new Command(commandArgs, env, workingDir); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 87 | assertArrayEquals(commandArgs, command.getCommandLineElements()); |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 88 | for (String key : env.keySet()) { |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 89 | assertThat(command.getEnvironmentVariables()).containsEntry(key, env.get(key)); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 90 | } |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 91 | assertThat(command.getWorkingDirectory()).isEqualTo(workingDir); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // Platform-dependent tests ------------------------------------------------ |
| 95 | |
| 96 | @Test |
| 97 | public void testSimpleCommand() throws Exception { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 98 | Command command = new Command(new String[] {"ls"}); |
| 99 | CommandResult result = command.execute(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 100 | assertThat(result.getTerminationStatus().success()).isTrue(); |
| 101 | assertThat(result.getStderr()).isEmpty(); |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 102 | assertThat(result.getStdout().length).isGreaterThan(0); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Test |
| 106 | public void testArguments() throws Exception { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 107 | Command command = new Command(new String[] {"echo", "foo"}); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 108 | checkSuccess(command.execute(), "foo\n"); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void testEnvironment() throws Exception { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 113 | Map<String, String> env = Collections.singletonMap("FOO", "BAR"); |
| 114 | Command command = new Command(new String[] {"/bin/sh", "-c", "echo $FOO"}, env, null); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 115 | checkSuccess(command.execute(), "BAR\n"); |
| 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void testWorkingDir() throws Exception { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 120 | Command command = new Command(new String[] {"pwd"}, null, new File("/")); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 121 | checkSuccess(command.execute(), "/\n"); |
| 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void testStdin() throws Exception { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 126 | Command command = new Command(new String[] {"grep", "bar"}); |
| 127 | InputStream in = new ByteArrayInputStream("foobarbaz".getBytes()); |
| 128 | checkSuccess( |
| 129 | command.executeAsync(in, Command.KILL_SUBPROCESS_ON_INTERRUPT).get(), |
| 130 | "foobarbaz\n"); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Test |
| 134 | public void testRawCommand() throws Exception { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 135 | Command command = new Command(new String[] { "perl", "-e", "print 'a'x100000" }); |
| 136 | CommandResult result = command.execute(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 137 | assertThat(result.getTerminationStatus().success()).isTrue(); |
| 138 | assertThat(result.getStderr()).isEmpty(); |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 139 | assertThat(result.getStdout().length).isGreaterThan(0); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Test |
| 143 | public void testRawCommandWithDir() throws Exception { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 144 | Command command = new Command(new String[] { "pwd" }, null, new File("/")); |
| 145 | CommandResult result = command.execute(); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 146 | checkSuccess(result, "/\n"); |
| 147 | } |
| 148 | |
| 149 | @Test |
| 150 | public void testHugeOutput() throws Exception { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 151 | Command command = new Command(new String[] {"perl", "-e", "print 'a'x100000"}); |
| 152 | CommandResult result = command.execute(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 153 | assertThat(result.getTerminationStatus().success()).isTrue(); |
| 154 | assertThat(result.getStderr()).isEmpty(); |
| 155 | assertThat(result.getStdout()).hasLength(100000); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Test |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 159 | public void testNoStreamingInputForCat() throws Exception { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 160 | Command command = new Command(new String[]{"/bin/cat"}); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 161 | ByteArrayInputStream emptyInput = new ByteArrayInputStream(new byte[0]); |
| 162 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 163 | ByteArrayOutputStream err = new ByteArrayOutputStream(); |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 164 | CommandResult result = command |
| 165 | .executeAsync(emptyInput, out, err, Command.KILL_SUBPROCESS_ON_INTERRUPT) |
| 166 | .get(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 167 | assertThat(result.getTerminationStatus().success()).isTrue(); |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 168 | assertThat(out.toString("UTF-8")).isEmpty(); |
| 169 | assertThat(err.toString("UTF-8")).isEmpty(); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Test |
| 173 | public void testNoInputForCat() throws Exception { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 174 | Command command = new Command(new String[]{"/bin/cat"}); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 175 | CommandResult result = command.execute(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 176 | assertThat(result.getTerminationStatus().success()).isTrue(); |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 177 | assertThat(new String(result.getStdout(), "UTF-8")).isEmpty(); |
| 178 | assertThat(new String(result.getStderr(), "UTF-8")).isEmpty(); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void testProvidedOutputStreamCapturesHelloWorld() throws Exception { |
| 183 | String helloWorld = "Hello, world."; |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 184 | Command command = new Command(new String[]{"/bin/echo", helloWorld}); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 185 | ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); |
| 186 | ByteArrayOutputStream stdErr = new ByteArrayOutputStream(); |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 187 | command.execute(stdOut, stdErr); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 188 | assertThat(stdOut.toString("UTF-8")).isEqualTo(helloWorld + "\n"); |
| 189 | assertThat(stdErr.toByteArray()).isEmpty(); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | @Test |
| 193 | public void testAsynchronous() throws Exception { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 194 | File tempFile = File.createTempFile("googlecron-test", "tmp"); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 195 | tempFile.delete(); |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 196 | Command command = new Command(new String[] {"touch", tempFile.getAbsolutePath()}); |
| 197 | FutureCommandResult result = command.executeAsync(); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 198 | result.get(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 199 | assertThat(tempFile.exists()).isTrue(); |
| 200 | assertThat(result.isDone()).isTrue(); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 201 | tempFile.delete(); |
| 202 | } |
| 203 | |
| 204 | @Test |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 205 | public void testAsynchronousWithOutputStreams() throws Exception { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 206 | String helloWorld = "Hello, world."; |
| 207 | Command command = new Command(new String[]{"/bin/echo", helloWorld}); |
| 208 | ByteArrayInputStream emptyInput = new ByteArrayInputStream(new byte[0]); |
| 209 | ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); |
| 210 | ByteArrayOutputStream stdErr = new ByteArrayOutputStream(); |
cushon | 03e7018 | 2017-09-15 09:33:27 +0200 | [diff] [blame] | 211 | FutureCommandResult result = |
| 212 | command.executeAsync(emptyInput, stdOut, stdErr, /* killSubprocessOnInterrupt= */ false); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 213 | result.get(); // Make sure the process actually finished |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 214 | assertThat(stdOut.toString("UTF-8")).isEqualTo(helloWorld + "\n"); |
| 215 | assertThat(stdErr.toByteArray()).isEmpty(); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | @Test |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 219 | public void testTimeout() throws Exception { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 220 | // Sleep for 3 seconds, but timeout after 1 second. |
| 221 | Command command = new Command(new String[] {"sleep", "3"}, null, null, Duration.ofSeconds(1)); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 222 | try { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 223 | command.execute(); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 224 | fail("Should have thrown AbnormalTerminationException"); |
| 225 | } catch (AbnormalTerminationException ate) { |
| 226 | // good |
| 227 | checkCommandElements(ate, "sleep", "3"); |
| 228 | checkATE(ate); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | @Test |
| 233 | public void testTimeoutDoesntFire() throws Exception { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 234 | Command command = new Command(new String[] {"cat"}, null, null, Duration.ofSeconds(2)); |
| 235 | InputStream in = new ByteArrayInputStream(new byte[]{'H', 'i', '!'}); |
| 236 | command.executeAsync(in, Command.KILL_SUBPROCESS_ON_INTERRUPT).get(); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | @Test |
| 240 | public void testCommandDoesNotExist() throws Exception { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 241 | Command command = new Command(new String[]{"thisisnotreal"}); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 242 | try { |
| 243 | command.execute(); |
| 244 | fail(); |
| 245 | } catch (ExecFailedException e){ |
| 246 | // Good. |
| 247 | checkCommandElements(e, "thisisnotreal"); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | @Test |
| 252 | public void testNoSuchCommand() throws Exception { |
| 253 | final Command command = new Command(new String[] {"thisisnotreal"}); |
| 254 | try { |
| 255 | command.execute(); |
| 256 | fail("Should have thrown ExecFailedException"); |
| 257 | } catch (ExecFailedException expected) { |
| 258 | // good |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | @Test |
| 263 | public void testExitCodes() throws Exception { |
| 264 | // 0 => success |
| 265 | { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 266 | String[] args = { "/bin/sh", "-c", "exit 0" }; |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 267 | CommandResult result = new Command(args).execute(); |
| 268 | TerminationStatus status = result.getTerminationStatus(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 269 | assertThat(status.success()).isTrue(); |
| 270 | assertThat(status.exited()).isTrue(); |
| 271 | assertThat(status.getExitCode()).isEqualTo(0); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // Every exit value in range [1-255] is reported as such (except [129-191], |
| 275 | // which map to signals). |
| 276 | for (int exit : new int[] { 1, 2, 3, 127, 128, 192, 255 }) { |
| 277 | try { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 278 | String[] args = { "/bin/sh", "-c", "exit " + exit }; |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 279 | new Command(args).execute(); |
| 280 | fail("Should have exited with status " + exit); |
| 281 | } catch (BadExitStatusException e) { |
ulfjack | 0ec7180 | 2017-08-10 10:28:50 +0200 | [diff] [blame] | 282 | assertThat(e).hasMessageThat().isEqualTo("Process exited with status " + exit); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 283 | checkCommandElements(e, "/bin/sh", "-c", "exit " + exit); |
| 284 | TerminationStatus status = e.getResult().getTerminationStatus(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 285 | assertThat(status.success()).isFalse(); |
| 286 | assertThat(status.exited()).isTrue(); |
| 287 | assertThat(status.getExitCode()).isEqualTo(exit); |
| 288 | assertThat(status.toShortString()).isEqualTo("Exit " + exit); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | // negative exit values are modulo 256: |
| 293 | for (int exit : new int[] { -1, -2, -3 }) { |
| 294 | int expected = 256 + exit; |
| 295 | try { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 296 | String[] args = { "/bin/bash", "-c", "exit " + exit }; |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 297 | new Command(args).execute(); |
| 298 | fail("Should have exited with status " + expected); |
| 299 | } catch (BadExitStatusException e) { |
ulfjack | 0ec7180 | 2017-08-10 10:28:50 +0200 | [diff] [blame] | 300 | assertThat(e).hasMessageThat().isEqualTo("Process exited with status " + expected); |
Kristina Chodorow | 31ccc99 | 2015-04-10 19:10:59 +0000 | [diff] [blame] | 301 | checkCommandElements(e, "/bin/bash", "-c", "exit " + exit); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 302 | TerminationStatus status = e.getResult().getTerminationStatus(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 303 | assertThat(status.success()).isFalse(); |
| 304 | assertThat(status.exited()).isTrue(); |
| 305 | assertThat(status.getExitCode()).isEqualTo(expected); |
| 306 | assertThat(status.toShortString()).isEqualTo("Exit " + expected); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | @Test |
| 312 | public void testFailedWithSignal() throws Exception { |
| 313 | // SIGHUP, SIGINT, SIGKILL, SIGTERM |
| 314 | for (int signal : new int[] { 1, 2, 9, 15 }) { |
| 315 | // Invoke a C++ program (killmyself.cc) that will die |
| 316 | // with the specified signal. |
| 317 | String killmyself = BlazeTestUtils.runfilesDir() + "/" |
| 318 | + TestConstants.JAVATESTS_ROOT |
| 319 | + "/com/google/devtools/build/lib/shell/killmyself"; |
| 320 | try { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 321 | String[] args = { killmyself, "" + signal }; |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 322 | new Command(args).execute(); |
| 323 | fail("Expected signal " + signal); |
| 324 | } catch (AbnormalTerminationException e) { |
ulfjack | 0ec7180 | 2017-08-10 10:28:50 +0200 | [diff] [blame] | 325 | assertThat(e).hasMessageThat().isEqualTo("Process terminated by signal " + signal); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 326 | checkCommandElements(e, killmyself, "" + signal); |
| 327 | TerminationStatus status = e.getResult().getTerminationStatus(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 328 | assertThat(status.success()).isFalse(); |
| 329 | assertThat(status.exited()).isFalse(); |
| 330 | assertThat(status.getTerminatingSignal()).isEqualTo(signal); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 331 | |
| 332 | switch (signal) { |
lberki | e355e77 | 2017-05-31 14:34:53 +0200 | [diff] [blame] | 333 | case 1: assertThat(status.toShortString()).isEqualTo("Hangup"); break; |
| 334 | case 2: assertThat(status.toShortString()).isEqualTo("Interrupt"); break; |
| 335 | case 9: assertThat(status.toShortString()).isEqualTo("Killed"); break; |
| 336 | case 15: assertThat(status.toShortString()).isEqualTo("Terminated"); break; |
Googler | feb9c87 | 2018-07-30 05:06:31 -0700 | [diff] [blame] | 337 | default: // fall out |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | @Test |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 344 | public void testOnlyReadsPartialInput() throws Exception { |
Han-Wen Nienhuys | 924daad | 2015-02-16 15:01:06 +0000 | [diff] [blame] | 345 | // -c == --bytes, but -c also works on Darwin. |
| 346 | Command command = new Command(new String[] {"head", "-c", "500"}); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 347 | OutputStream out = new ByteArrayOutputStream(); |
| 348 | InputStream in = new InputStream() { |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 349 | @Override |
| 350 | public int read() { |
| 351 | return 0; // write an unbounded amount |
| 352 | } |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 353 | }; |
| 354 | |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 355 | CommandResult result = |
| 356 | command.executeAsync(in, out, out, Command.KILL_SUBPROCESS_ON_INTERRUPT).get(); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 357 | TerminationStatus status = result.getTerminationStatus(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 358 | assertThat(status.success()).isTrue(); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | @Test |
| 362 | public void testFlushing() throws Exception { |
| 363 | final Command command = new Command( |
Han-Wen Nienhuys | 8dff429 | 2015-02-16 14:38:06 +0000 | [diff] [blame] | 364 | // On darwin, /bin/sh does not support -n for the echo builtin. |
| 365 | new String[] {"/bin/bash", "-c", "echo -n Foo; sleep 0.1; echo Bar"}); |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 366 | // We run this command, passing in a special output stream that records when each flush() |
| 367 | // occurs. We test that a flush occurs after writing "Foo" and that another flush occurs after |
| 368 | // writing "Bar\n". |
| 369 | boolean[] flushed = new boolean[8]; |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 370 | OutputStream out = new OutputStream() { |
| 371 | private int count = 0; |
| 372 | @Override |
| 373 | public void write(int b) throws IOException { |
| 374 | count++; |
| 375 | } |
| 376 | @Override |
| 377 | public void flush() throws IOException { |
| 378 | flushed[count] = true; |
| 379 | } |
| 380 | }; |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 381 | command.execute(out, System.err); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 382 | assertThat(flushed[0]).isFalse(); |
| 383 | assertThat(flushed[1]).isFalse(); // 'F' |
| 384 | assertThat(flushed[2]).isFalse(); // 'o' |
| 385 | assertThat(flushed[3]).isTrue(); // 'o' <- expect flush here. |
| 386 | assertThat(flushed[4]).isFalse(); // 'B' |
| 387 | assertThat(flushed[5]).isFalse(); // 'a' |
| 388 | assertThat(flushed[6]).isFalse(); // 'r' |
| 389 | assertThat(flushed[7]).isTrue(); // '\n' |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 392 | @Test |
| 393 | public void testOutputStreamThrowsException() throws Exception { |
| 394 | OutputStream out = new OutputStream () { |
| 395 | @Override |
| 396 | public void write(int b) throws IOException { |
| 397 | throw new IOException(); |
| 398 | } |
| 399 | }; |
| 400 | Command command = new Command(new String[] {"/bin/echo", "foo"}); |
| 401 | try { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 402 | command.execute(out, out); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 403 | fail(); |
| 404 | } catch (AbnormalTerminationException e) { |
| 405 | // Good. |
| 406 | checkCommandElements(e, "/bin/echo", "foo"); |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 407 | assertThat(e).hasMessageThat().isEqualTo("java.io.IOException"); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
| 411 | @Test |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 412 | public void testOutputStreamThrowsExceptionAndCommandFails() throws Exception { |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 413 | OutputStream out = new OutputStream () { |
| 414 | @Override |
| 415 | public void write(int b) throws IOException { |
| 416 | throw new IOException(); |
| 417 | } |
| 418 | }; |
| 419 | Command command = new Command(new String[] {"cat", "/dev/thisisnotreal"}); |
| 420 | try { |
ulfjack | f2d4595 | 2017-08-09 15:27:49 +0200 | [diff] [blame] | 421 | command.execute(out, out); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 422 | fail(); |
| 423 | } catch (AbnormalTerminationException e) { |
| 424 | checkCommandElements(e, "cat", "/dev/thisisnotreal"); |
| 425 | TerminationStatus status = e.getResult().getTerminationStatus(); |
| 426 | // Subprocess either gets a SIGPIPE trying to write to our output stream, |
| 427 | // or it exits with failure. Both are observed, nondetermistically. |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 428 | assertThat(status.exited() ? status.getExitCode() == 1 : status.getTerminatingSignal() == 13) |
| 429 | .isTrue(); |
| 430 | assertWithMessage(e.getMessage()) |
| 431 | .that( |
| 432 | e.getMessage() |
| 433 | .endsWith("also encountered an error while attempting " + "to retrieve output")) |
| 434 | .isTrue(); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 438 | private static void checkCommandElements(CommandException e, |
| 439 | String... expected) { |
| 440 | assertArrayEquals(expected, e.getCommand().getCommandLineElements()); |
| 441 | } |
| 442 | |
| 443 | private static void checkATE(final AbnormalTerminationException ate) { |
| 444 | final CommandResult result = ate.getResult(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 445 | assertThat(result.getTerminationStatus().success()).isFalse(); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | private static void checkSuccess(final CommandResult result, |
| 449 | final String expectedOutput) { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 450 | assertThat(result.getTerminationStatus().success()).isTrue(); |
| 451 | assertThat(result.getStderr()).isEmpty(); |
| 452 | assertThat(new String(result.getStdout())).isEqualTo(expectedOutput); |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 453 | } |
Dmitry Lomov | 8efc3ef | 2016-02-17 09:50:03 +0000 | [diff] [blame] | 454 | |
| 455 | @Test |
| 456 | public void testRelativePath() throws Exception { |
| 457 | Command command = new Command(new String[]{"relative/path/to/binary"}, |
| 458 | ImmutableMap.<String, String>of(), |
| 459 | new File("/working/directory")); |
| 460 | assertThat(command.getCommandLineElements()[0]) |
| 461 | .isEqualTo("/working/directory/relative/path/to/binary"); |
| 462 | } |
Han-Wen Nienhuys | b0e387b | 2015-02-12 13:27:06 +0000 | [diff] [blame] | 463 | } |