Search in sources :

Example 1 with TestParams

use of com.google.devtools.build.lib.rules.test.TestProvider.TestParams in project bazel by bazelbuild.

the class RuleConfiguredTargetBuilder method initializeTestProvider.

private TestProvider initializeTestProvider(FilesToRunProvider filesToRunProvider) {
    int explicitShardCount = ruleContext.attributes().get("shard_count", Type.INTEGER);
    if (explicitShardCount < 0 && ruleContext.getRule().isAttributeValueExplicitlySpecified("shard_count")) {
        ruleContext.attributeError("shard_count", "Must not be negative.");
    }
    if (explicitShardCount > 50) {
        ruleContext.attributeError("shard_count", "Having more than 50 shards is indicative of poor test organization. " + "Please reduce the number of shards.");
    }
    TestActionBuilder testActionBuilder = new TestActionBuilder(ruleContext).setInstrumentedFiles(providersBuilder.getProvider(InstrumentedFilesProvider.class));
    TestEnvironmentProvider environmentProvider = (TestEnvironmentProvider) skylarkDeclaredProviders.build().get(TestEnvironmentProvider.SKYLARK_CONSTRUCTOR.getKey());
    if (environmentProvider != null) {
        testActionBuilder.addExtraEnv(environmentProvider.getEnvironment());
    }
    TestParams testParams = testActionBuilder.setFilesToRunProvider(filesToRunProvider).setExecutionRequirements((ExecutionInfoProvider) skylarkDeclaredProviders.build().get(ExecutionInfoProvider.SKYLARK_CONSTRUCTOR.getKey())).setShardCount(explicitShardCount).build();
    ImmutableList<String> testTags = ImmutableList.copyOf(ruleContext.getRule().getRuleTags());
    return new TestProvider(testParams, testTags);
}
Also used : TestProvider(com.google.devtools.build.lib.rules.test.TestProvider) TestActionBuilder(com.google.devtools.build.lib.rules.test.TestActionBuilder) InstrumentedFilesProvider(com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider) TestParams(com.google.devtools.build.lib.rules.test.TestProvider.TestParams) TestEnvironmentProvider(com.google.devtools.build.lib.rules.test.TestEnvironmentProvider)

Example 2 with TestParams

use of com.google.devtools.build.lib.rules.test.TestProvider.TestParams in project bazel by bazelbuild.

the class TestActionBuilder method createTestAction.

/**
   * Creates a test action and artifacts for the given rule. The test action will
   * use the specified executable and runfiles.
   *
   * @return ordered list of test artifacts, one per action. These are used to drive
   *    execution in Skyframe, and by AggregatingTestListener and
   *    TestResultAnalyzer to keep track of completed and pending test runs.
   */
private TestParams createTestAction(int shards) {
    PathFragment targetName = new PathFragment(ruleContext.getLabel().getName());
    BuildConfiguration config = ruleContext.getConfiguration();
    AnalysisEnvironment env = ruleContext.getAnalysisEnvironment();
    Root root = config.getTestLogsDirectory(ruleContext.getRule().getRepository());
    NestedSetBuilder<Artifact> inputsBuilder = NestedSetBuilder.stableOrder();
    inputsBuilder.addTransitive(NestedSetBuilder.create(Order.STABLE_ORDER, runfilesSupport.getRunfilesMiddleman()));
    NestedSet<Artifact> testRuntime = PrerequisiteArtifacts.nestedSet(ruleContext, "$test_runtime", Mode.HOST);
    inputsBuilder.addTransitive(testRuntime);
    TestTargetProperties testProperties = new TestTargetProperties(ruleContext, executionRequirements);
    // If the test rule does not provide InstrumentedFilesProvider, there's not much that we can do.
    final boolean collectCodeCoverage = config.isCodeCoverageEnabled() && instrumentedFiles != null;
    TreeMap<String, String> testEnv = new TreeMap<>();
    TestTargetExecutionSettings executionSettings;
    if (collectCodeCoverage) {
        inputsBuilder.addTransitive(instrumentedFiles.getCoverageSupportFiles());
        // Add instrumented file manifest artifact to the list of inputs. This file will contain
        // exec paths of all source files that should be included into the code coverage output.
        NestedSet<Artifact> metadataFiles = instrumentedFiles.getInstrumentationMetadataFiles();
        inputsBuilder.addTransitive(metadataFiles);
        inputsBuilder.addTransitive(PrerequisiteArtifacts.nestedSet(ruleContext, "$coverage_support", Mode.DONT_CHECK));
        // We don't add this attribute to non-supported test target
        if (ruleContext.isAttrDefined("$lcov_merger", LABEL)) {
            Artifact lcovMerger = ruleContext.getPrerequisiteArtifact("$lcov_merger", Mode.TARGET);
            if (lcovMerger != null) {
                inputsBuilder.addTransitive(PrerequisiteArtifacts.nestedSet(ruleContext, "$lcov_merger", Mode.TARGET));
                // Pass this LcovMerger_deploy.jar path to collect_coverage.sh
                testEnv.put("LCOV_MERGER", lcovMerger.getExecPathString());
            }
        }
        Artifact instrumentedFileManifest = InstrumentedFileManifestAction.getInstrumentedFileManifest(ruleContext, instrumentedFiles.getInstrumentedFiles(), metadataFiles);
        executionSettings = new TestTargetExecutionSettings(ruleContext, runfilesSupport, executable, instrumentedFileManifest, shards);
        inputsBuilder.add(instrumentedFileManifest);
        for (Pair<String, String> coverageEnvEntry : instrumentedFiles.getCoverageEnvironment()) {
            testEnv.put(coverageEnvEntry.getFirst(), coverageEnvEntry.getSecond());
        }
    } else {
        executionSettings = new TestTargetExecutionSettings(ruleContext, runfilesSupport, executable, null, shards);
    }
    testEnv.putAll(extraEnv);
    if (config.getRunUnder() != null) {
        Artifact runUnderExecutable = executionSettings.getRunUnderExecutable();
        if (runUnderExecutable != null) {
            inputsBuilder.add(runUnderExecutable);
        }
    }
    int runsPerTest = config.getRunsPerTestForLabel(ruleContext.getLabel());
    Iterable<Artifact> inputs = inputsBuilder.build();
    int shardRuns = (shards > 0 ? shards : 1);
    List<Artifact> results = Lists.newArrayListWithCapacity(runsPerTest * shardRuns);
    ImmutableList.Builder<Artifact> coverageArtifacts = ImmutableList.builder();
    boolean useExperimentalTestRunner = false;
    if (ruleContext.attributes().has("use_testrunner", Type.BOOLEAN)) {
        useExperimentalTestRunner = ruleContext.attributes().get("use_testrunner", Type.BOOLEAN) && ruleContext.attributes().get("tags", Type.STRING_LIST).contains("experimental_testrunner");
    }
    for (int run = 0; run < runsPerTest; run++) {
        // Use a 1-based index for user friendliness.
        String testRunDir = runsPerTest > 1 ? String.format("run_%d_of_%d", run + 1, runsPerTest) : "";
        for (int shard = 0; shard < shardRuns; shard++) {
            String shardRunDir = (shardRuns > 1 ? String.format("shard_%d_of_%d", shard + 1, shards) : "");
            if (testRunDir.isEmpty()) {
                shardRunDir = shardRunDir.isEmpty() ? "" : shardRunDir + PathFragment.SEPARATOR_CHAR;
            } else {
                testRunDir += PathFragment.SEPARATOR_CHAR;
                shardRunDir = shardRunDir.isEmpty() ? testRunDir : shardRunDir + "_" + testRunDir;
            }
            Artifact testLog = ruleContext.getPackageRelativeArtifact(targetName.getRelative(shardRunDir + "test.log"), root);
            Artifact cacheStatus = ruleContext.getPackageRelativeArtifact(targetName.getRelative(shardRunDir + "test.cache_status"), root);
            Artifact coverageArtifact = null;
            if (collectCodeCoverage) {
                coverageArtifact = ruleContext.getPackageRelativeArtifact(targetName.getRelative(shardRunDir + "coverage.dat"), root);
                coverageArtifacts.add(coverageArtifact);
            }
            Artifact microCoverageArtifact = null;
            if (collectCodeCoverage && config.isMicroCoverageEnabled()) {
                microCoverageArtifact = ruleContext.getPackageRelativeArtifact(targetName.getRelative(shardRunDir + "coverage.micro.dat"), root);
            }
            env.registerAction(new TestRunnerAction(ruleContext.getActionOwner(), inputs, testRuntime, testLog, cacheStatus, coverageArtifact, microCoverageArtifact, testProperties, testEnv, executionSettings, shard, run, config, ruleContext.getWorkspaceName(), useExperimentalTestRunner));
            results.add(cacheStatus);
        }
    }
    // TODO(bazel-team): Passing the reportGenerator to every TestParams is a bit strange.
    Artifact reportGenerator = null;
    if (config.isCodeCoverageEnabled()) {
        // It's not enough to add this if the rule has coverage enabled because the command line may
        // contain rules with baseline coverage but no test rules that have coverage enabled, and in
        // that case, we still need the report generator.
        reportGenerator = ruleContext.getPrerequisiteArtifact("$coverage_report_generator", Mode.HOST);
    }
    return new TestParams(runsPerTest, shards, TestTimeout.getTestTimeout(ruleContext.getRule()), ruleContext.getRule().getRuleClass(), ImmutableList.copyOf(results), coverageArtifacts.build(), reportGenerator);
}
Also used : Root(com.google.devtools.build.lib.actions.Root) ImmutableList(com.google.common.collect.ImmutableList) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) TreeMap(java.util.TreeMap) Artifact(com.google.devtools.build.lib.actions.Artifact) BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) AnalysisEnvironment(com.google.devtools.build.lib.analysis.AnalysisEnvironment) TestParams(com.google.devtools.build.lib.rules.test.TestProvider.TestParams)

Example 3 with TestParams

use of com.google.devtools.build.lib.rules.test.TestProvider.TestParams in project bazel by bazelbuild.

the class TestResultAnalyzerTest method makeTestSummaryBuilder.

private TestSummary.Builder makeTestSummaryBuilder() {
    // a lot of mocks to mock out fetching the TestTimeout configuration needed by
    //  {@link TestResultAnalyzer#shouldEmitTestSizeWarningInSummary(...)
    TestParams mockParams = mock(TestParams.class);
    when(mockParams.getTimeout()).thenReturn(TestTimeout.LONG);
    TestProvider testProvider = new TestProvider(mockParams, ImmutableList.<String>of());
    ConfiguredTarget mockTarget = mock(ConfiguredTarget.class);
    when(mockTarget.getProvider(TestProvider.class)).thenReturn(testProvider);
    return TestSummary.newBuilder().setStatus(BlazeTestStatus.PASSED).setTarget(mockTarget);
}
Also used : TestProvider(com.google.devtools.build.lib.rules.test.TestProvider) TestParams(com.google.devtools.build.lib.rules.test.TestProvider.TestParams) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget)

Aggregations

TestParams (com.google.devtools.build.lib.rules.test.TestProvider.TestParams)3 TestProvider (com.google.devtools.build.lib.rules.test.TestProvider)2 ImmutableList (com.google.common.collect.ImmutableList)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 Root (com.google.devtools.build.lib.actions.Root)1 AnalysisEnvironment (com.google.devtools.build.lib.analysis.AnalysisEnvironment)1 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)1 InstrumentedFilesProvider (com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider)1 TestActionBuilder (com.google.devtools.build.lib.rules.test.TestActionBuilder)1 TestEnvironmentProvider (com.google.devtools.build.lib.rules.test.TestEnvironmentProvider)1 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)1 TreeMap (java.util.TreeMap)1