blob: fee03048b18d0f9c42ed613f644fa34beb8226f0 [file] [log] [blame]
Googler06e17812018-03-20 15:05:10 -07001// 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.
14package com.google.devtools.build.lib.actions;
15
16import static com.google.common.truth.Truth.assertThat;
Googler17e28df2019-09-10 12:07:05 -070017import static com.google.common.truth.Truth8.assertThat;
Googler06e17812018-03-20 15:05:10 -070018
Googler17e28df2019-09-10 12:07:05 -070019import com.google.devtools.build.lib.actions.SpawnResult.MetadataLog;
Jakob Buchgraber615ac4e2019-03-21 11:39:04 -070020import com.google.devtools.build.lib.actions.SpawnResult.Status;
janakr40f2a122020-10-06 08:53:15 -070021import com.google.devtools.build.lib.server.FailureDetails;
22import com.google.devtools.build.lib.server.FailureDetails.FailureDetail;
23import com.google.devtools.build.lib.server.FailureDetails.Spawn.Code;
Googler17e28df2019-09-10 12:07:05 -070024import com.google.devtools.build.lib.vfs.Path;
25import com.google.devtools.build.lib.vfs.util.FileSystems;
Jakob Buchgraber615ac4e2019-03-21 11:39:04 -070026import com.google.protobuf.ByteString;
Googler06e17812018-03-20 15:05:10 -070027import java.time.Duration;
28import org.junit.Test;
29import org.junit.runner.RunWith;
30import org.junit.runners.JUnit4;
31
32/**
33 * Testing common SpawnResult features
34 */
35@RunWith(JUnit4.class)
36public 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))
mschaller83c5bb02020-02-19 15:29:17 -080044 .setExitCode(SpawnResult.POSIX_TIMEOUT_EXIT_CODE)
janakr40f2a122020-10-06 08:53:15 -070045 .setFailureDetail(
46 FailureDetail.newBuilder()
47 .setSpawn(FailureDetails.Spawn.newBuilder().setCode(Code.TIMEOUT))
48 .build())
Googler4dd6f002018-03-27 08:15:39 -070049 .setRunnerName("test")
Googler06e17812018-03-20 15:05:10 -070050 .build();
janakr619dcf82020-09-22 14:08:59 -070051 assertThat(r.getDetailMessage("", "", false, false))
Googler06e17812018-03-20 15:05:10 -070052 .contains("(failed due to timeout after 5.00 seconds.)");
53 }
54
55 @Test
56 public void getTimeoutMessageNoTime() {
57 SpawnResult r =
Googler4dd6f002018-03-27 08:15:39 -070058 new SpawnResult.Builder()
59 .setStatus(SpawnResult.Status.TIMEOUT)
mschaller83c5bb02020-02-19 15:29:17 -080060 .setExitCode(SpawnResult.POSIX_TIMEOUT_EXIT_CODE)
janakr40f2a122020-10-06 08:53:15 -070061 .setFailureDetail(
62 FailureDetail.newBuilder()
63 .setSpawn(FailureDetails.Spawn.newBuilder().setCode(Code.TIMEOUT))
64 .build())
Googler4dd6f002018-03-27 08:15:39 -070065 .setRunnerName("test")
66 .build();
janakr619dcf82020-09-22 14:08:59 -070067 assertThat(r.getDetailMessage("", "", false, false)).contains("(failed due to timeout.)");
Googler06e17812018-03-20 15:05:10 -070068 }
Jakob Buchgraber615ac4e2019-03-21 11:39:04 -070069
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 }
Googler17e28df2019-09-10 12:07:05 -070087
88 @Test
janakr40f2a122020-10-06 08:53:15 -070089 public void getSpawnResultLogs() {
Googler17e28df2019-09-10 12:07:05 -070090 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 }
Googler06e17812018-03-20 15:05:10 -0700103}