Googler | 06e1781 | 2018-03-20 15:05:10 -0700 | [diff] [blame] | 1 | // Copyright 2018 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 | package com.google.devtools.build.lib.actions; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
Googler | 17e28df | 2019-09-10 12:07:05 -0700 | [diff] [blame] | 17 | import static com.google.common.truth.Truth8.assertThat; |
Googler | 06e1781 | 2018-03-20 15:05:10 -0700 | [diff] [blame] | 18 | |
Googler | 17e28df | 2019-09-10 12:07:05 -0700 | [diff] [blame] | 19 | import com.google.devtools.build.lib.actions.SpawnResult.MetadataLog; |
Jakob Buchgraber | 615ac4e | 2019-03-21 11:39:04 -0700 | [diff] [blame] | 20 | import com.google.devtools.build.lib.actions.SpawnResult.Status; |
janakr | 40f2a12 | 2020-10-06 08:53:15 -0700 | [diff] [blame^] | 21 | import com.google.devtools.build.lib.server.FailureDetails; |
| 22 | import com.google.devtools.build.lib.server.FailureDetails.FailureDetail; |
| 23 | import com.google.devtools.build.lib.server.FailureDetails.Spawn.Code; |
Googler | 17e28df | 2019-09-10 12:07:05 -0700 | [diff] [blame] | 24 | import com.google.devtools.build.lib.vfs.Path; |
| 25 | import com.google.devtools.build.lib.vfs.util.FileSystems; |
Jakob Buchgraber | 615ac4e | 2019-03-21 11:39:04 -0700 | [diff] [blame] | 26 | import com.google.protobuf.ByteString; |
Googler | 06e1781 | 2018-03-20 15:05:10 -0700 | [diff] [blame] | 27 | import java.time.Duration; |
| 28 | import org.junit.Test; |
| 29 | import org.junit.runner.RunWith; |
| 30 | import org.junit.runners.JUnit4; |
| 31 | |
| 32 | /** |
| 33 | * Testing common SpawnResult features |
| 34 | */ |
| 35 | @RunWith(JUnit4.class) |
| 36 | public final class SpawnResultTest { |
| 37 | |
| 38 | @Test |
| 39 | public void getTimeoutMessage() { |
| 40 | SpawnResult r = |
| 41 | new SpawnResult.Builder() |
| 42 | .setStatus(SpawnResult.Status.TIMEOUT) |
| 43 | .setWallTime(Duration.ofSeconds(5)) |
mschaller | 83c5bb0 | 2020-02-19 15:29:17 -0800 | [diff] [blame] | 44 | .setExitCode(SpawnResult.POSIX_TIMEOUT_EXIT_CODE) |
janakr | 40f2a12 | 2020-10-06 08:53:15 -0700 | [diff] [blame^] | 45 | .setFailureDetail( |
| 46 | FailureDetail.newBuilder() |
| 47 | .setSpawn(FailureDetails.Spawn.newBuilder().setCode(Code.TIMEOUT)) |
| 48 | .build()) |
Googler | 4dd6f00 | 2018-03-27 08:15:39 -0700 | [diff] [blame] | 49 | .setRunnerName("test") |
Googler | 06e1781 | 2018-03-20 15:05:10 -0700 | [diff] [blame] | 50 | .build(); |
janakr | 619dcf8 | 2020-09-22 14:08:59 -0700 | [diff] [blame] | 51 | assertThat(r.getDetailMessage("", "", false, false)) |
Googler | 06e1781 | 2018-03-20 15:05:10 -0700 | [diff] [blame] | 52 | .contains("(failed due to timeout after 5.00 seconds.)"); |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void getTimeoutMessageNoTime() { |
| 57 | SpawnResult r = |
Googler | 4dd6f00 | 2018-03-27 08:15:39 -0700 | [diff] [blame] | 58 | new SpawnResult.Builder() |
| 59 | .setStatus(SpawnResult.Status.TIMEOUT) |
mschaller | 83c5bb0 | 2020-02-19 15:29:17 -0800 | [diff] [blame] | 60 | .setExitCode(SpawnResult.POSIX_TIMEOUT_EXIT_CODE) |
janakr | 40f2a12 | 2020-10-06 08:53:15 -0700 | [diff] [blame^] | 61 | .setFailureDetail( |
| 62 | FailureDetail.newBuilder() |
| 63 | .setSpawn(FailureDetails.Spawn.newBuilder().setCode(Code.TIMEOUT)) |
| 64 | .build()) |
Googler | 4dd6f00 | 2018-03-27 08:15:39 -0700 | [diff] [blame] | 65 | .setRunnerName("test") |
| 66 | .build(); |
janakr | 619dcf8 | 2020-09-22 14:08:59 -0700 | [diff] [blame] | 67 | assertThat(r.getDetailMessage("", "", false, false)).contains("(failed due to timeout.)"); |
Googler | 06e1781 | 2018-03-20 15:05:10 -0700 | [diff] [blame] | 68 | } |
Jakob Buchgraber | 615ac4e | 2019-03-21 11:39:04 -0700 | [diff] [blame] | 69 | |
| 70 | @Test |
| 71 | public void inMemoryContents() throws Exception { |
| 72 | ActionInput output = ActionInputHelper.fromPath("/foo/bar"); |
| 73 | ByteString contents = ByteString.copyFromUtf8("hello world"); |
| 74 | |
| 75 | SpawnResult r = |
| 76 | new SpawnResult.Builder() |
| 77 | .setStatus(Status.SUCCESS) |
| 78 | .setExitCode(0) |
| 79 | .setRunnerName("test") |
| 80 | .setInMemoryOutput(output, contents) |
| 81 | .build(); |
| 82 | |
| 83 | assertThat(ByteString.readFrom(r.getInMemoryOutput(output))).isEqualTo(contents); |
| 84 | assertThat(r.getInMemoryOutput(null)).isEqualTo(null); |
| 85 | assertThat(r.getInMemoryOutput(ActionInputHelper.fromPath("/does/not/exist"))).isEqualTo(null); |
| 86 | } |
Googler | 17e28df | 2019-09-10 12:07:05 -0700 | [diff] [blame] | 87 | |
| 88 | @Test |
janakr | 40f2a12 | 2020-10-06 08:53:15 -0700 | [diff] [blame^] | 89 | public void getSpawnResultLogs() { |
Googler | 17e28df | 2019-09-10 12:07:05 -0700 | [diff] [blame] | 90 | SpawnResult.Builder builder = |
| 91 | new SpawnResult.Builder().setStatus(Status.SUCCESS).setExitCode(0).setRunnerName("test"); |
| 92 | |
| 93 | assertThat(builder.build().getActionMetadataLog()).isEmpty(); |
| 94 | |
| 95 | String logName = "/path/to/logs.txt"; |
| 96 | Path logPath = FileSystems.getJavaIoFileSystem().getPath(logName); |
| 97 | MetadataLog metadataLog = new MetadataLog("test_metadata_log", logPath); |
| 98 | SpawnResult withLogs = builder.setActionMetadataLog(metadataLog).build(); |
| 99 | |
| 100 | assertThat(withLogs.getActionMetadataLog()).hasValue(metadataLog); |
| 101 | assertThat(withLogs.getActionMetadataLog().get().getFilePath()).isEqualTo(logPath); |
| 102 | } |
Googler | 06e1781 | 2018-03-20 15:05:10 -0700 | [diff] [blame] | 103 | } |