Post test result for cancelled runs

We create the equivalent of an empty FailedAttemptResult with a status
code of INCOMPLETE, and post that in the case of a cancelled concurrent
test action. We add a new method on TestRunnerSpawn to finalize a
cancelled test. Also add test coverage to check that a TestResult with
the correct status is posted for each action.

Why INCOMPLETE?

We have three options here: 1. use INCOMPLETE, 2. use NO_STATUS, or 3.
add a new status code to BlazeTestStatus. The other existing enum values
are all inappropriate:
PASSED, FLAKY, TIMEOUT, FAILED, REMOTE_FAILURE, FAILED_TO_BUILD, and
BLAZE_HALTED_BEFORE_TESTING

About option 2: NO_STATUS would not allow us to distinguish cancelled
tests from tests that were not run in the first place. INCOMPLETE is
currently unused and undocumented, so it's unclear what the originally
intended semantics are. Before open sourcing Bazel, Google had an
INTERRUPTED status, which got translated to INCOMPLETE in the process.

About option 3: This would require updating all the downstream systems
to support the new status code, including the BEP, which has the exact
same list of status codes as BlazeTestStatus.

CancelFuture handling?

It might be better to push down the cancelFuture handling to the
TestAttemptContinuation - that'd allow us to collect additional data
about the test attempt and simplify this code. Possibly, we could only
push down interrupt handling, but we probably need to know that the
interrupt is due to a cancelled run rather than a user interrupt.

PiperOrigin-RevId: 275246041
diff --git a/src/main/java/com/google/devtools/build/lib/exec/StandaloneTestStrategy.java b/src/main/java/com/google/devtools/build/lib/exec/StandaloneTestStrategy.java
index a6aa223..66c527c 100644
--- a/src/main/java/com/google/devtools/build/lib/exec/StandaloneTestStrategy.java
+++ b/src/main/java/com/google/devtools/build/lib/exec/StandaloneTestStrategy.java
@@ -41,6 +41,7 @@
 import com.google.devtools.build.lib.analysis.test.TestRunnerAction;
 import com.google.devtools.build.lib.analysis.test.TestRunnerAction.ResolvedPaths;
 import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos;
+import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.TestResult.ExecutionInfo;
 import com.google.devtools.build.lib.buildeventstream.TestFileNameConstants;
 import com.google.devtools.build.lib.events.Reporter;
 import com.google.devtools.build.lib.util.OS;
@@ -239,7 +240,6 @@
       testOutputs = renameOutputs(actionExecutionContext, action, testOutputs, attemptId);
     }
 
-    TestResultData.Builder dataBuilder = result.testResultDataBuilder();
     // Recover the test log path, which may have been renamed, and add it to the data builder.
     Path renamedTestLog = null;
     for (Pair<String, Path> pair : testOutputs) {
@@ -247,8 +247,13 @@
         renamedTestLog = pair.getSecond();
       }
     }
+
+    TestResultData.Builder dataBuilder = result.testResultDataBuilder();
     if (dataBuilder.getStatus() == BlazeTestStatus.PASSED) {
       dataBuilder.setPassedLog(renamedTestLog.toString());
+    } else if (dataBuilder.getStatus() == BlazeTestStatus.INCOMPLETE) {
+      // Incomplete (cancelled) test runs don't have a log.
+      Preconditions.checkState(renamedTestLog == null);
     } else {
       dataBuilder.addFailedLogs(renamedTestLog.toString());
     }
@@ -466,6 +471,19 @@
       StandaloneTestStrategy.this.finalizeTest(
           testAction, actionExecutionContext, (StandaloneTestResult) finalResult, failedAttempts);
     }
+
+    @Override
+    public void finalizeCancelledTest(List<FailedAttemptResult> failedAttempts) throws IOException {
+      TestResultData.Builder builder =
+          TestResultData.newBuilder().setTestPassed(false).setStatus(BlazeTestStatus.INCOMPLETE);
+      StandaloneTestResult standaloneTestResult =
+          StandaloneTestResult.builder()
+              .setSpawnResults(ImmutableList.of())
+              .setTestResultDataBuilder(builder)
+              .setExecutionInfo(ExecutionInfo.getDefaultInstance())
+              .build();
+      finalizeTest(standaloneTestResult, failedAttempts);
+    }
   }
 
   private final class BazelTestAttemptContinuation extends TestAttemptContinuation {