Search in sources :

Example 1 with TestResultData

use of com.google.devtools.build.lib.view.test.TestStatus.TestResultData in project bazel by bazelbuild.

the class WorkerTestStrategy method execInWorker.

private TestResultData execInWorker(TestRunnerAction action, ActionExecutionContext actionExecutionContext, Map<String, String> environment, List<String> startupArgs, Path execRoot, int retriesLeft) throws ExecException, InterruptedException, IOException {
    Executor executor = actionExecutionContext.getExecutor();
    // TODO(kush): Remove once we're out of the experimental phase.
    executor.getEventHandler().handle(Event.warn("RUNNING TEST IN AN EXPERIMENTAL PERSISTENT WORKER. RESULTS MAY BE INACCURATE"));
    TestResultData.Builder builder = TestResultData.newBuilder();
    Path testLogPath = action.getTestLog().getPath();
    Worker worker = null;
    WorkerKey key = null;
    long startTime = executor.getClock().currentTimeMillis();
    try {
        HashCode workerFilesHash = WorkerFilesHash.getWorkerFilesHash(action.getTools(), actionExecutionContext);
        key = new WorkerKey(startupArgs, environment, execRoot, action.getMnemonic(), workerFilesHash, ImmutableMap.<PathFragment, Path>of(), ImmutableSet.<PathFragment>of(), /*mustBeSandboxed=*/
        false);
        worker = workerPool.borrowObject(key);
        WorkRequest request = WorkRequest.getDefaultInstance();
        request.writeDelimitedTo(worker.getOutputStream());
        worker.getOutputStream().flush();
        WorkResponse response = WorkResponse.parseDelimitedFrom(worker.getInputStream());
        actionExecutionContext.getFileOutErr().getErrorStream().write(response.getOutputBytes().toByteArray());
        long duration = executor.getClock().currentTimeMillis() - startTime;
        builder.addTestTimes(duration);
        builder.setRunDurationMillis(duration);
        if (response.getExitCode() == 0) {
            builder.setTestPassed(true).setStatus(BlazeTestStatus.PASSED).setCachable(true).setPassedLog(testLogPath.getPathString());
        } else {
            builder.setTestPassed(false).setStatus(BlazeTestStatus.FAILED).addFailedLogs(testLogPath.getPathString());
        }
        TestCase details = parseTestResult(action.resolve(actionExecutionContext.getExecutor().getExecRoot()).getXmlOutputPath());
        if (details != null) {
            builder.setTestCase(details);
        }
        return builder.build();
    } catch (IOException | InterruptedException e) {
        if (e instanceof InterruptedException) {
            // The user pressed Ctrl-C. Get out here quick.
            retriesLeft = 0;
        }
        if (worker != null) {
            workerPool.invalidateObject(key, worker);
            worker = null;
        }
        if (retriesLeft > 0) {
            // The worker process failed, but we still have some retries left. Let's retry with a fresh
            // worker.
            executor.getEventHandler().handle(Event.warn(key.getMnemonic() + " worker failed (" + e + "), invalidating and retrying with new worker..."));
            return execInWorker(action, actionExecutionContext, environment, startupArgs, execRoot, retriesLeft - 1);
        } else {
            throw new TestExecException(e.getMessage());
        }
    } finally {
        if (worker != null) {
            workerPool.returnObject(key, worker);
        }
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) TestResultData(com.google.devtools.build.lib.view.test.TestStatus.TestResultData) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) IOException(java.io.IOException) WorkRequest(com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest) Executor(com.google.devtools.build.lib.actions.Executor) HashCode(com.google.common.hash.HashCode) TestCase(com.google.devtools.build.lib.view.test.TestStatus.TestCase) WorkResponse(com.google.devtools.build.lib.worker.WorkerProtocol.WorkResponse) TestExecException(com.google.devtools.build.lib.actions.TestExecException)

Example 2 with TestResultData

use of com.google.devtools.build.lib.view.test.TestStatus.TestResultData in project bazel by bazelbuild.

the class StandaloneTestStrategy method executeTestAttempt.

private TestResultData executeTestAttempt(TestRunnerAction action, Spawn spawn, ActionExecutionContext actionExecutionContext, Path execRoot, Path coverageDir, Path tmpDir, Path workingDirectory) throws IOException, ExecException, InterruptedException {
    prepareFileSystem(action, tmpDir, coverageDir, workingDirectory);
    try (FileOutErr fileOutErr = new FileOutErr(action.getTestLog().getPath(), action.resolve(execRoot).getTestStderr())) {
        TestResultData data = executeTest(action, spawn, actionExecutionContext.withFileOutErr(fileOutErr));
        appendStderr(fileOutErr.getOutputPath(), fileOutErr.getErrorPath());
        return data;
    }
}
Also used : FileOutErr(com.google.devtools.build.lib.util.io.FileOutErr) TestResultData(com.google.devtools.build.lib.view.test.TestStatus.TestResultData)

Example 3 with TestResultData

use of com.google.devtools.build.lib.view.test.TestStatus.TestResultData in project bazel by bazelbuild.

the class StandaloneTestStrategy method exec.

@Override
public void exec(TestRunnerAction action, ActionExecutionContext actionExecutionContext) throws ExecException, InterruptedException {
    Path execRoot = actionExecutionContext.getExecutor().getExecRoot();
    Path coverageDir = execRoot.getRelative(action.getCoverageDirectory());
    Path runfilesDir = getLocalRunfilesDirectory(action, actionExecutionContext, binTools, action.getLocalShellEnvironment(), action.isEnableRunfiles());
    Path tmpDir = tmpDirRoot.getChild(getTmpDirName(action.getExecutionSettings().getExecutable().getExecPath()));
    Map<String, String> env = setupEnvironment(action, execRoot, runfilesDir, tmpDir);
    Path workingDirectory = runfilesDir.getRelative(action.getRunfilesPrefix());
    ResolvedPaths resolvedPaths = action.resolve(execRoot);
    Map<String, String> info = new HashMap<>();
    // This key is only understood by StandaloneSpawnStrategy.
    info.put("timeout", "" + getTimeout(action));
    info.putAll(action.getTestProperties().getExecutionInfo());
    Spawn spawn = new SimpleSpawn(action, getArgs(COLLECT_COVERAGE, action), ImmutableMap.copyOf(env), ImmutableMap.copyOf(info), new RunfilesSupplierImpl(runfilesDir.asFragment(), action.getExecutionSettings().getRunfiles()), /*inputs=*/
    ImmutableList.copyOf(action.getInputs()), /*tools=*/
    ImmutableList.<Artifact>of(), /*filesetManifests=*/
    ImmutableList.<Artifact>of(), ImmutableList.copyOf(action.getSpawnOutputs()), action.getTestProperties().getLocalResourceUsage(executionOptions.usingLocalTestJobs()));
    Executor executor = actionExecutionContext.getExecutor();
    TestResultData.Builder dataBuilder = TestResultData.newBuilder();
    try {
        int maxAttempts = getTestAttempts(action);
        TestResultData data = executeTestAttempt(action, spawn, actionExecutionContext, execRoot, coverageDir, tmpDir, workingDirectory);
        int attempt;
        for (attempt = 1; data.getStatus() != BlazeTestStatus.PASSED && attempt < maxAttempts; attempt++) {
            processFailedTestAttempt(attempt, executor, action, dataBuilder, data, actionExecutionContext.getFileOutErr());
            data = executeTestAttempt(action, spawn, actionExecutionContext, execRoot, coverageDir, tmpDir, workingDirectory);
        }
        processLastTestAttempt(attempt, dataBuilder, data);
        ImmutableList.Builder<Pair<String, Path>> testOutputsBuilder = new ImmutableList.Builder<>();
        if (action.getTestLog().getPath().exists()) {
            testOutputsBuilder.add(Pair.of("test.log", action.getTestLog().getPath()));
        }
        if (resolvedPaths.getXmlOutputPath().exists()) {
            testOutputsBuilder.add(Pair.of("test.xml", resolvedPaths.getXmlOutputPath()));
        }
        executor.getEventBus().post(new TestAttempt(action, attempt, data.getTestPassed(), data.getRunDurationMillis(), testOutputsBuilder.build(), true));
        finalizeTest(actionExecutionContext, action, dataBuilder.build());
    } catch (IOException e) {
        executor.getEventHandler().handle(Event.error("Caught I/O exception: " + e));
        throw new EnvironmentalExecException("unexpected I/O exception", e);
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) TestResultData(com.google.devtools.build.lib.view.test.TestStatus.TestResultData) HashMap(java.util.HashMap) SimpleSpawn(com.google.devtools.build.lib.actions.SimpleSpawn) ImmutableList(com.google.common.collect.ImmutableList) Builder(com.google.devtools.build.lib.view.test.TestStatus.TestResultData.Builder) IOException(java.io.IOException) EnvironmentalExecException(com.google.devtools.build.lib.actions.EnvironmentalExecException) RunfilesSupplierImpl(com.google.devtools.build.lib.analysis.RunfilesSupplierImpl) ResolvedPaths(com.google.devtools.build.lib.rules.test.TestRunnerAction.ResolvedPaths) Executor(com.google.devtools.build.lib.actions.Executor) Builder(com.google.devtools.build.lib.view.test.TestStatus.TestResultData.Builder) TestAttempt(com.google.devtools.build.lib.rules.test.TestAttempt) SimpleSpawn(com.google.devtools.build.lib.actions.SimpleSpawn) Spawn(com.google.devtools.build.lib.actions.Spawn) Pair(com.google.devtools.build.lib.util.Pair)

Example 4 with TestResultData

use of com.google.devtools.build.lib.view.test.TestStatus.TestResultData in project bazel by bazelbuild.

the class TestResultAnalyzerTest method testIncrementalAnalyzeKeepsActionRanTrueWhenAlreadyTrueAndNewCachedResults.

@Test
public void testIncrementalAnalyzeKeepsActionRanTrueWhenAlreadyTrueAndNewCachedResults() {
    TestSummary.Builder summaryBuilder = makeTestSummaryBuilder().setActionRan(true);
    TestResultData testResultData = TestResultData.newBuilder().setRemotelyCached(true).build();
    TestResult result = new TestResult(mock(TestRunnerAction.class), testResultData, /*cached=*/
    true);
    TestSummary.Builder newSummaryBuilder = underTest.incrementalAnalyze(summaryBuilder, result);
    assertTrue(newSummaryBuilder.peek().actionRan());
}
Also used : TestResultData(com.google.devtools.build.lib.view.test.TestStatus.TestResultData) TestResult(com.google.devtools.build.lib.rules.test.TestResult) TestRunnerAction(com.google.devtools.build.lib.rules.test.TestRunnerAction) Test(org.junit.Test)

Example 5 with TestResultData

use of com.google.devtools.build.lib.view.test.TestStatus.TestResultData in project bazel by bazelbuild.

the class StandaloneTestStrategy method executeTest.

protected TestResultData executeTest(TestRunnerAction action, Spawn spawn, ActionExecutionContext actionExecutionContext) throws ExecException, InterruptedException, IOException {
    Executor executor = actionExecutionContext.getExecutor();
    Closeable streamed = null;
    Path testLogPath = action.getTestLog().getPath();
    TestResultData.Builder builder = TestResultData.newBuilder();
    long startTime = executor.getClock().currentTimeMillis();
    SpawnActionContext spawnActionContext = executor.getSpawnActionContext(action.getMnemonic());
    try {
        try {
            if (executionOptions.testOutput.equals(TestOutputFormat.STREAMED)) {
                streamed = new StreamedTestOutput(Reporter.outErrForReporter(actionExecutionContext.getExecutor().getEventHandler()), testLogPath);
            }
            spawnActionContext.exec(spawn, actionExecutionContext);
            builder.setTestPassed(true).setStatus(BlazeTestStatus.PASSED).setCachable(true).setPassedLog(testLogPath.getPathString());
        } catch (ExecException e) {
            // Execution failed, which we consider a test failure.
            // TODO(bazel-team): set cachable==true for relevant statuses (failure, but not for
            // timeout, etc.)
            builder.setTestPassed(false).setStatus(e.hasTimedOut() ? BlazeTestStatus.TIMEOUT : BlazeTestStatus.FAILED).addFailedLogs(testLogPath.getPathString());
            if (spawnActionContext.shouldPropagateExecException()) {
                throw e;
            }
        } finally {
            long duration = executor.getClock().currentTimeMillis() - startTime;
            builder.addTestTimes(duration);
            builder.addTestProcessTimes(duration);
            builder.setRunDurationMillis(duration);
            if (streamed != null) {
                streamed.close();
            }
        }
        TestCase details = parseTestResult(action.resolve(actionExecutionContext.getExecutor().getExecRoot()).getXmlOutputPath());
        if (details != null) {
            builder.setTestCase(details);
        }
        if (action.isCoverageMode()) {
            builder.setHasCoverage(true);
        }
        return builder.build();
    } catch (IOException e) {
        throw new TestExecException(e.getMessage());
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) Executor(com.google.devtools.build.lib.actions.Executor) TestResultData(com.google.devtools.build.lib.view.test.TestStatus.TestResultData) TestCase(com.google.devtools.build.lib.view.test.TestStatus.TestCase) Closeable(java.io.Closeable) EnvironmentalExecException(com.google.devtools.build.lib.actions.EnvironmentalExecException) TestExecException(com.google.devtools.build.lib.actions.TestExecException) ExecException(com.google.devtools.build.lib.actions.ExecException) Builder(com.google.devtools.build.lib.view.test.TestStatus.TestResultData.Builder) IOException(java.io.IOException) SpawnActionContext(com.google.devtools.build.lib.actions.SpawnActionContext) TestExecException(com.google.devtools.build.lib.actions.TestExecException)

Aggregations

TestResultData (com.google.devtools.build.lib.view.test.TestStatus.TestResultData)8 TestResult (com.google.devtools.build.lib.rules.test.TestResult)4 TestRunnerAction (com.google.devtools.build.lib.rules.test.TestRunnerAction)4 Test (org.junit.Test)4 Executor (com.google.devtools.build.lib.actions.Executor)3 Path (com.google.devtools.build.lib.vfs.Path)3 IOException (java.io.IOException)3 EnvironmentalExecException (com.google.devtools.build.lib.actions.EnvironmentalExecException)2 TestExecException (com.google.devtools.build.lib.actions.TestExecException)2 TestCase (com.google.devtools.build.lib.view.test.TestStatus.TestCase)2 Builder (com.google.devtools.build.lib.view.test.TestStatus.TestResultData.Builder)2 ImmutableList (com.google.common.collect.ImmutableList)1 HashCode (com.google.common.hash.HashCode)1 ExecException (com.google.devtools.build.lib.actions.ExecException)1 SimpleSpawn (com.google.devtools.build.lib.actions.SimpleSpawn)1 Spawn (com.google.devtools.build.lib.actions.Spawn)1 SpawnActionContext (com.google.devtools.build.lib.actions.SpawnActionContext)1 RunfilesSupplierImpl (com.google.devtools.build.lib.analysis.RunfilesSupplierImpl)1 TestAttempt (com.google.devtools.build.lib.rules.test.TestAttempt)1 ResolvedPaths (com.google.devtools.build.lib.rules.test.TestRunnerAction.ResolvedPaths)1