remote/repo: don't accept non-zero action result

Closes #10916.

PiperOrigin-RevId: 300750156
diff --git a/src/main/java/com/google/devtools/build/lib/remote/RemoteRepositoryRemoteExecutor.java b/src/main/java/com/google/devtools/build/lib/remote/RemoteRepositoryRemoteExecutor.java
index 71fa2f3..d4e9d54 100644
--- a/src/main/java/com/google/devtools/build/lib/remote/RemoteRepositoryRemoteExecutor.java
+++ b/src/main/java/com/google/devtools/build/lib/remote/RemoteRepositoryRemoteExecutor.java
@@ -112,7 +112,7 @@
       Digest actionDigest = digestUtil.compute(action);
       ActionKey actionKey = new ActionKey(actionDigest);
       ActionResult actionResult = remoteCache.downloadActionResult(actionKey);
-      if (actionResult == null) {
+      if (actionResult == null || actionResult.getExitCode() != 0) {
         Map<Digest, Message> additionalInputs = Maps.newHashMapWithExpectedSize(2);
         additionalInputs.put(actionDigest, action);
         additionalInputs.put(commandHash, command);
diff --git a/src/test/java/com/google/devtools/build/lib/remote/BUILD b/src/test/java/com/google/devtools/build/lib/remote/BUILD
index 59d3d7d..a0c907a 100644
--- a/src/test/java/com/google/devtools/build/lib/remote/BUILD
+++ b/src/test/java/com/google/devtools/build/lib/remote/BUILD
@@ -43,6 +43,7 @@
         "//src/main/java/com/google/devtools/build/lib:build-base",
         "//src/main/java/com/google/devtools/build/lib:events",
         "//src/main/java/com/google/devtools/build/lib:exitcode-external",
+        "//src/main/java/com/google/devtools/build/lib:runtime",
         "//src/main/java/com/google/devtools/build/lib:util",
         "//src/main/java/com/google/devtools/build/lib/actions",
         "//src/main/java/com/google/devtools/build/lib/actions:localhost_capacity",
diff --git a/src/test/java/com/google/devtools/build/lib/remote/RemoteRepositoryRemoteExecutorTest.java b/src/test/java/com/google/devtools/build/lib/remote/RemoteRepositoryRemoteExecutorTest.java
new file mode 100644
index 0000000..9eb4d40
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/remote/RemoteRepositoryRemoteExecutorTest.java
@@ -0,0 +1,116 @@
+// Copyright 2020 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package com.google.devtools.build.lib.remote;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import build.bazel.remote.execution.v2.ActionResult;
+import build.bazel.remote.execution.v2.ExecuteResponse;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.devtools.build.lib.remote.util.DigestUtil;
+import com.google.devtools.build.lib.runtime.RepositoryRemoteExecutor.ExecutionResult;
+import com.google.devtools.build.lib.vfs.DigestHashFunction;
+import io.grpc.Context;
+import java.io.IOException;
+import java.time.Duration;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+/** Tests for {@link com.google.devtools.build.lib.remote.RemoteRepositoryRemoteExecutor}. */
+@RunWith(JUnit4.class)
+public class RemoteRepositoryRemoteExecutorTest {
+
+  public static final DigestUtil DIGEST_UTIL = new DigestUtil(DigestHashFunction.SHA256);
+
+  @Mock public RemoteExecutionCache remoteCache;
+
+  @Mock public GrpcRemoteExecutor remoteExecutor;
+
+  private RemoteRepositoryRemoteExecutor repoExecutor;
+
+  @Before
+  public void setup() {
+    MockitoAnnotations.initMocks(this);
+    repoExecutor =
+        new RemoteRepositoryRemoteExecutor(
+            remoteCache,
+            remoteExecutor,
+            DIGEST_UTIL,
+            Context.current(),
+            /* remoteInstanceName= */ "foo",
+            /* acceptCached= */ true);
+  }
+
+  @Test
+  public void testZeroExitCodeFromCache() throws IOException, InterruptedException {
+    // Test that an ActionResult with exit code zero is accepted as cached.
+
+    // Arrange
+    ActionResult cachedResult = ActionResult.newBuilder().setExitCode(0).build();
+    when(remoteCache.downloadActionResult(any())).thenReturn(cachedResult);
+
+    // Act
+    ExecutionResult executionResult =
+        repoExecutor.execute(
+            ImmutableList.of("/bin/bash", "-c", "exit 0"),
+            /* executionProperties= */ ImmutableMap.of(),
+            /* environment= */ ImmutableMap.of(),
+            /* workingDirectory= */ null,
+            /* timeout= */ Duration.ZERO);
+
+    // Assert
+    verify(remoteCache).downloadActionResult(any());
+    // Don't fallback to execution
+    verify(remoteExecutor, never()).executeRemotely(any());
+
+    assertThat(executionResult.exitCode()).isEqualTo(0);
+  }
+
+  @Test
+  public void testNoneZeroExitCodeFromCache() throws IOException, InterruptedException {
+    // Test that an ActionResult with a none-zero exit code is not accepted as cached.
+
+    // Arrange
+    ActionResult cachedResult = ActionResult.newBuilder().setExitCode(1).build();
+    when(remoteCache.downloadActionResult(any())).thenReturn(cachedResult);
+
+    ExecuteResponse response = ExecuteResponse.newBuilder().setResult(cachedResult).build();
+    when(remoteExecutor.executeRemotely(any())).thenReturn(response);
+
+    // Act
+    ExecutionResult executionResult =
+        repoExecutor.execute(
+            ImmutableList.of("/bin/bash", "-c", "exit 1"),
+            /* executionProperties= */ ImmutableMap.of(),
+            /* environment= */ ImmutableMap.of(),
+            /* workingDirectory= */ null,
+            /* timeout= */ Duration.ZERO);
+
+    // Assert
+    verify(remoteCache).downloadActionResult(any());
+    // Fallback to execution
+    verify(remoteExecutor).executeRemotely(any());
+
+    assertThat(executionResult.exitCode()).isEqualTo(1);
+  }
+}