Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [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.skyframe; |
| 15 | |
Googler | db4c550 | 2024-08-08 11:03:51 -0700 | [diff] [blame^] | 16 | import com.google.devtools.build.lib.actions.ActionExecutionException; |
| 17 | import com.google.devtools.build.lib.actions.ActionLookupData; |
| 18 | import com.google.devtools.build.lib.actions.ActionLookupValue; |
janakr | a4634cf | 2017-08-24 09:23:39 +0200 | [diff] [blame] | 19 | import com.google.devtools.build.lib.actions.Artifact; |
| 20 | import com.google.devtools.build.lib.analysis.ConfiguredTarget; |
janakr | 876deaa | 2021-02-17 07:49:48 -0800 | [diff] [blame] | 21 | import com.google.devtools.build.lib.analysis.ConfiguredTargetValue; |
Googler | bfd4e24 | 2016-07-15 22:23:37 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.analysis.TopLevelArtifactContext; |
Googler | db4c550 | 2024-08-08 11:03:51 -0700 | [diff] [blame^] | 23 | import com.google.devtools.build.lib.analysis.test.TestAttempt; |
janakr | a4634cf | 2017-08-24 09:23:39 +0200 | [diff] [blame] | 24 | import com.google.devtools.build.lib.analysis.test.TestProvider; |
Googler | db4c550 | 2024-08-08 11:03:51 -0700 | [diff] [blame^] | 25 | import com.google.devtools.build.lib.analysis.test.TestResult; |
| 26 | import com.google.devtools.build.lib.analysis.test.TestRunnerAction; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.cmdline.Label; |
Googler | db4c550 | 2024-08-08 11:03:51 -0700 | [diff] [blame^] | 28 | import com.google.devtools.build.lib.server.FailureDetails.Execution.Code; |
| 29 | import com.google.devtools.build.lib.util.DetailedExitCode; |
| 30 | import com.google.devtools.build.lib.util.ExitCode; |
| 31 | import com.google.devtools.build.lib.view.test.TestStatus.BlazeTestStatus; |
| 32 | import com.google.devtools.build.lib.view.test.TestStatus.TestResultData; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 33 | import com.google.devtools.build.skyframe.SkyFunction; |
| 34 | import com.google.devtools.build.skyframe.SkyKey; |
| 35 | import com.google.devtools.build.skyframe.SkyValue; |
Googler | db4c550 | 2024-08-08 11:03:51 -0700 | [diff] [blame^] | 36 | import com.google.devtools.build.skyframe.SkyframeLookupResult; |
| 37 | import java.util.List; |
Googler | 5cc598c | 2022-07-06 03:29:45 -0700 | [diff] [blame] | 38 | import javax.annotation.Nullable; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 39 | |
| 40 | /** |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 41 | * TestCompletionFunction builds all relevant test artifacts of a {@link |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 42 | * com.google.devtools.build.lib.analysis.ConfiguredTarget}. This includes test shards and repeated |
| 43 | * runs. |
| 44 | */ |
| 45 | public final class TestCompletionFunction implements SkyFunction { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 46 | @Override |
Googler | 5cc598c | 2022-07-06 03:29:45 -0700 | [diff] [blame] | 47 | @Nullable |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 48 | public SkyValue compute(SkyKey skyKey, Environment env) throws InterruptedException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 49 | TestCompletionValue.TestCompletionKey key = |
| 50 | (TestCompletionValue.TestCompletionKey) skyKey.argument(); |
janakr | 573807d | 2018-01-11 14:02:35 -0800 | [diff] [blame] | 51 | ConfiguredTargetKey ctKey = key.configuredTargetKey(); |
Googler | bfd4e24 | 2016-07-15 22:23:37 +0000 | [diff] [blame] | 52 | TopLevelArtifactContext ctx = key.topLevelArtifactContext(); |
Googler | db4c550 | 2024-08-08 11:03:51 -0700 | [diff] [blame^] | 53 | if (env.getValue(TargetCompletionValue.key(ctKey, ctx, /* willTest= */ true)) == null) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 54 | return null; |
| 55 | } |
janakr | a4634cf | 2017-08-24 09:23:39 +0200 | [diff] [blame] | 56 | |
Googler | 3c7d571 | 2023-07-14 06:33:31 -0700 | [diff] [blame] | 57 | ConfiguredTargetValue ctValue = (ConfiguredTargetValue) env.getValue(ctKey); |
janakr | a4634cf | 2017-08-24 09:23:39 +0200 | [diff] [blame] | 58 | if (ctValue == null) { |
| 59 | return null; |
| 60 | } |
| 61 | |
| 62 | ConfiguredTarget ct = ctValue.getConfiguredTarget(); |
| 63 | if (key.exclusiveTesting()) { |
janakr | b9d8d58 | 2018-06-13 21:57:19 -0700 | [diff] [blame] | 64 | // Request test execution iteratively if testing exclusively. |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 65 | for (Artifact.DerivedArtifact testArtifact : TestProvider.getTestStatusArtifacts(ct)) { |
| 66 | env.getValue(testArtifact.getGeneratingActionKey()); |
janakr | b9d8d58 | 2018-06-13 21:57:19 -0700 | [diff] [blame] | 67 | if (env.valuesMissing()) { |
janakr | a4634cf | 2017-08-24 09:23:39 +0200 | [diff] [blame] | 68 | return null; |
| 69 | } |
| 70 | } |
| 71 | } else { |
Googler | db4c550 | 2024-08-08 11:03:51 -0700 | [diff] [blame^] | 72 | List<SkyKey> skyKeys = Artifact.keys(TestProvider.getTestStatusArtifacts(ct)); |
| 73 | SkyframeLookupResult result = env.getValuesAndExceptions(skyKeys); |
| 74 | if (env.valuesMissing()) { |
janakr | a4634cf | 2017-08-24 09:23:39 +0200 | [diff] [blame] | 75 | return null; |
| 76 | } |
Googler | db4c550 | 2024-08-08 11:03:51 -0700 | [diff] [blame^] | 77 | for (SkyKey actionKey : skyKeys) { |
| 78 | try { |
| 79 | if (result.getOrThrow(actionKey, ActionExecutionException.class) == null) { |
| 80 | return null; |
| 81 | } |
| 82 | } catch (ActionExecutionException e) { |
| 83 | DetailedExitCode detailedExitCode = e.getDetailedExitCode(); |
| 84 | if (detailedExitCode.getExitCode().equals(ExitCode.BUILD_FAILURE) |
| 85 | && ctValue instanceof ActionLookupValue actionLookupValue) { |
| 86 | postTestResultEventsForUnbuildableTestInputs( |
| 87 | env, (ActionLookupData) actionKey, actionLookupValue, detailedExitCode); |
| 88 | } else { |
| 89 | return null; |
| 90 | } |
| 91 | } |
| 92 | } |
janakr | a4634cf | 2017-08-24 09:23:39 +0200 | [diff] [blame] | 93 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 94 | return TestCompletionValue.TEST_COMPLETION_MARKER; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public String extractTag(SkyKey skyKey) { |
janakr | ac2cd35 | 2017-12-20 13:37:13 -0800 | [diff] [blame] | 99 | return Label.print(((ConfiguredTargetKey) skyKey.argument()).getLabel()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 100 | } |
Googler | db4c550 | 2024-08-08 11:03:51 -0700 | [diff] [blame^] | 101 | |
| 102 | /** |
| 103 | * Posts events for test actions that could not run because one or more exec-configuration inputs |
| 104 | * common to all tests failed to build. |
| 105 | * |
| 106 | * <p>When we run this SkyFunction we will have already built the test executable and its inputs, |
| 107 | * but we might fail to build the exec-configuration attributes providing inputs to the {@link |
| 108 | * TestRunnerAction} such as {@code $test_runtime}, {@code $test_wrapper}, {@code |
| 109 | * test_setup_script} and others (see {@link |
| 110 | * com.google.devtools.build.lib.analysis.BaseRuleClasses.TestBaseRule#build(com.google.devtools.build.lib.packages.RuleClass.Builder, |
| 111 | * com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment)} where all Test-type rules |
| 112 | * have additional attributes added). |
| 113 | * |
| 114 | * <p>When these exec-configuration inputs cannot be built, we do not get to use any {@code |
| 115 | * TestStrategy} that is responsible for posting {@link TestAttempt} and {@link TestResult} |
| 116 | * events. We need to handle this case here and post minimal events indicating the test {@link |
| 117 | * BlazeTestStatus.FAILED_TO_BUILD FAILED_TO_BUILD}. |
| 118 | */ |
| 119 | private static void postTestResultEventsForUnbuildableTestInputs( |
| 120 | Environment env, |
| 121 | ActionLookupData actionKey, |
| 122 | ActionLookupValue actionLookupValue, |
| 123 | DetailedExitCode detailedExitCode) { |
| 124 | BlazeTestStatus status = BlazeTestStatus.FAILED_TO_BUILD; |
| 125 | if (detailedExitCode |
| 126 | .getFailureDetail() |
| 127 | .getExecution() |
| 128 | .getCode() |
| 129 | .equals(Code.ACTION_NOT_UP_TO_DATE)) { |
| 130 | status = BlazeTestStatus.NO_STATUS; |
| 131 | } |
| 132 | TestRunnerAction testRunnerAction = |
| 133 | (TestRunnerAction) actionLookupValue.getAction(actionKey.getActionIndex()); |
| 134 | TestResultData testData = TestResultData.newBuilder().setStatus(status).build(); |
| 135 | env.getListener().post(TestAttempt.forUnstartableTestResult(testRunnerAction, testData)); |
| 136 | env.getListener() |
| 137 | .post(new TestResult(testRunnerAction, testData, /* cached= */ false, detailedExitCode)); |
| 138 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 139 | } |