Search in sources :

Example 6 with BuildDriverException

use of org.jboss.pnc.spi.builddriver.exception.BuildDriverException in project pnc by project-ncl.

the class BlockedBuildDriverMock method complete.

protected void complete(BuildExecutionSession buildExecutionSession, final RunningEnvironment runningEnvironment, Consumer<CompletedBuild> onComplete) throws InterruptedException {
    log.info("Running blocked build ...");
    semaphore.acquire();
    setBuildStatus(TestProjectConfigurationBuilder.CANCEL);
    log.info("Blocked build canceled.");
    onComplete.accept(new CompletedBuild() {

        @Override
        public BuildDriverResult getBuildResult() throws BuildDriverException {
            return getBuildResultMock(runningEnvironment);
        }

        @Override
        public RunningEnvironment getRunningEnvironment() {
            return runningEnvironment;
        }
    });
}
Also used : CompletedBuild(org.jboss.pnc.spi.builddriver.CompletedBuild) BuildDriverResult(org.jboss.pnc.spi.builddriver.BuildDriverResult) BuildDriverException(org.jboss.pnc.spi.builddriver.exception.BuildDriverException) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment)

Example 7 with BuildDriverException

use of org.jboss.pnc.spi.builddriver.exception.BuildDriverException in project pnc by project-ncl.

the class BuildDriverMock method complete.

protected void complete(BuildExecutionSession buildExecutionSession, final RunningEnvironment runningEnvironment, Consumer<CompletedBuild> onComplete) throws InterruptedException {
    Thread.sleep(RandomUtils.randInt(100, 300));
    setBuildStatus(buildExecutionSession.getBuildExecutionConfiguration().getBuildScript());
    onComplete.accept(new CompletedBuild() {

        @Override
        public BuildDriverResult getBuildResult() throws BuildDriverException {
            return getBuildResultMock(runningEnvironment);
        }

        @Override
        public RunningEnvironment getRunningEnvironment() {
            return runningEnvironment;
        }
    });
}
Also used : CompletedBuild(org.jboss.pnc.spi.builddriver.CompletedBuild) BuildDriverResult(org.jboss.pnc.spi.builddriver.BuildDriverResult) BuildDriverException(org.jboss.pnc.spi.builddriver.exception.BuildDriverException) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment)

Example 8 with BuildDriverException

use of org.jboss.pnc.spi.builddriver.exception.BuildDriverException in project pnc by project-ncl.

the class DebugInContainerTest method shouldEnableSshWhenBuildFails.

@Test
public void shouldEnableSshWhenBuildFails() throws InterruptedException, BuildDriverException {
    TermdBuildDriverModuleConfig buildDriverModuleConfig = mock(TermdBuildDriverModuleConfig.class);
    doReturn(1000L).when(buildDriverModuleConfig).getLivenessProbeFrequencyMillis();
    doReturn(5000L).when(buildDriverModuleConfig).getLivenessFailTimeoutMillis();
    doReturn(5000).when(buildDriverModuleConfig).getFileTransferReadTimeout();
    ClientMockFactory buildAgentClientFactory = new ClientMockFactory();
    TermdBuildDriver driver = new TermdBuildDriver(systemConfig, buildDriverModuleConfig, buildAgentClientFactory);
    BuildExecutionSession buildExecution = mock(BuildExecutionSession.class);
    BuildExecutionConfiguration buildExecutionConfiguration = mock(BuildExecutionConfiguration.class);
    doReturn(buildExecutionConfiguration).when(buildExecution).getBuildExecutionConfiguration();
    RunningEnvironment runningEnvironment = mock(RunningEnvironment.class);
    doReturn(Paths.get("")).when(runningEnvironment).getWorkingDirectory();
    doReturn(new DebugData(true)).when(runningEnvironment).getDebugData();
    doReturn("http://localhost/").when(runningEnvironment).getInternalBuildAgentUrl();
    doReturn(runningEnvironment).when(buildExecution).getRunningEnvironment();
    BlockingQueue<CompletedBuild> result = new ArrayBlockingQueue(1);
    Consumer<CompletedBuild> onComplete = (completedBuild) -> {
        try {
            result.put(completedBuild);
        } catch (InterruptedException e) {
            Assert.fail("Unable to consume build result.");
        }
    };
    Consumer<Throwable> onError = (throwable) -> Assert.fail("Build should fail without system error.");
    // when
    RunningBuild runningBuild = driver.startProjectBuild(buildExecution, runningEnvironment, onComplete, onError);
    // wait to start waiting for completion and start liveness probe
    Thread.sleep(500);
    buildAgentClientFactory.getOnStatusUpdate().accept(TaskStatusUpdateEvent.newBuilder().newStatus(Status.FAILED).build());
    // then
    CompletedBuild completedBuild = result.poll(3, TimeUnit.SECONDS);
    Assert.assertNotNull("Missing build result.", completedBuild);
    Assert.assertEquals("The build should fail.", BuildStatus.FAILED, completedBuild.getBuildResult().getBuildStatus());
    List<Object> executedCommands = buildAgentClientFactory.getBuildAgentClient().getExecutedCommands();
    logger.info("Executed commands {}.", executedCommands);
    Assert.assertEquals(2, executedCommands.size());
    Assertions.assertThat(executedCommands).anySatisfy(c -> ((String) c).contains("startSshd.sh"));
}
Also used : BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) CompletedBuild(org.jboss.pnc.spi.builddriver.CompletedBuild) BuildDriverException(org.jboss.pnc.spi.builddriver.exception.BuildDriverException) RunningBuild(org.jboss.pnc.spi.builddriver.RunningBuild) LoggerFactory(org.slf4j.LoggerFactory) Assertions(org.assertj.core.api.Assertions) BuildExecutionConfiguration(org.jboss.pnc.spi.executor.BuildExecutionConfiguration) Mockito.doReturn(org.mockito.Mockito.doReturn) Logger(org.slf4j.Logger) TaskStatusUpdateEvent(org.jboss.pnc.buildagent.api.TaskStatusUpdateEvent) CompletedBuild(org.jboss.pnc.spi.builddriver.CompletedBuild) Test(org.junit.Test) BlockingQueue(java.util.concurrent.BlockingQueue) BuildStatus(org.jboss.pnc.enums.BuildStatus) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) List(java.util.List) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) Paths(java.nio.file.Paths) DebugData(org.jboss.pnc.spi.builddriver.DebugData) Status(org.jboss.pnc.buildagent.api.Status) TermdBuildDriverModuleConfig(org.jboss.pnc.common.json.moduleconfig.TermdBuildDriverModuleConfig) SystemConfig(org.jboss.pnc.common.json.moduleconfig.SystemConfig) Assert(org.junit.Assert) BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) Mockito.mock(org.mockito.Mockito.mock) BuildExecutionConfiguration(org.jboss.pnc.spi.executor.BuildExecutionConfiguration) DebugData(org.jboss.pnc.spi.builddriver.DebugData) RunningBuild(org.jboss.pnc.spi.builddriver.RunningBuild) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) TermdBuildDriverModuleConfig(org.jboss.pnc.common.json.moduleconfig.TermdBuildDriverModuleConfig) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Test(org.junit.Test)

Example 9 with BuildDriverException

use of org.jboss.pnc.spi.builddriver.exception.BuildDriverException in project pnc by project-ncl.

the class TermdBuildDriverTest method shouldStartAndCancelWhileExecutingCommand.

@Test(timeout = 5_000)
public void shouldStartAndCancelWhileExecutingCommand() throws ConfigurationParseException, BuildDriverException, InterruptedException {
    // given
    String dirName = "test-workdir";
    String logStart = "Running the command...";
    String logEnd = "Command completed.";
    CountDownLatch latchCompleted = new CountDownLatch(1);
    ClientMockFactory mockFactory = new ClientMockFactory();
    TermdBuildDriver driver = new TermdBuildDriver(systemConfig, buildDriverModuleConfig, mockFactory);
    BuildExecutionSession buildExecution = mock(BuildExecutionSession.class);
    BuildExecutionConfiguration buildExecutionConfiguration = mock(BuildExecutionConfiguration.class);
    doReturn("echo \"" + logStart + "\"; mvn validate; echo \"" + logEnd + "\";").when(buildExecutionConfiguration).getBuildScript();
    doReturn(dirName).when(buildExecutionConfiguration).getName();
    doReturn(buildExecutionConfiguration).when(buildExecution).getBuildExecutionConfiguration();
    AtomicReference<CompletedBuild> buildResult = new AtomicReference<>();
    // when
    Consumer<CompletedBuild> onComplete = (completedBuild) -> {
        buildResult.set(completedBuild);
        latchCompleted.countDown();
    };
    Consumer<Throwable> onError = (throwable) -> {
        logger.error("Error received: ", throwable);
        fail(throwable.getMessage());
    };
    RunningBuild runningBuild = driver.startProjectBuild(buildExecution, localEnvironmentPointer, onComplete, onError);
    runningBuild.cancel();
    // simulate update for "CTRL+C" on a command, which results in the command failing
    mockFactory.getOnStatusUpdate().accept(TaskStatusUpdateEvent.newBuilder().newStatus(Status.FAILED).build());
    latchCompleted.await();
    // then
    assertThat(buildResult.get().getBuildResult()).isNotNull();
    assertThat(buildResult.get().getBuildResult().getBuildStatus()).isEqualTo(CANCELLED);
}
Also used : BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) CompletedBuild(org.jboss.pnc.spi.builddriver.CompletedBuild) CANCELLED(org.jboss.pnc.enums.BuildStatus.CANCELLED) BuildDriverException(org.jboss.pnc.spi.builddriver.exception.BuildDriverException) RunningBuild(org.jboss.pnc.spi.builddriver.RunningBuild) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConfigurationParseException(org.jboss.pnc.common.json.ConfigurationParseException) Assert.fail(org.junit.Assert.fail) BuildExecutionConfiguration(org.jboss.pnc.spi.executor.BuildExecutionConfiguration) Path(java.nio.file.Path) Mockito.doReturn(org.mockito.Mockito.doReturn) Logger(org.slf4j.Logger) TaskStatusUpdateEvent(org.jboss.pnc.buildagent.api.TaskStatusUpdateEvent) Files(java.nio.file.Files) MethodHandles(java.lang.invoke.MethodHandles) CompletedBuild(org.jboss.pnc.spi.builddriver.CompletedBuild) Test(org.junit.Test) IOException(java.io.IOException) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) Status(org.jboss.pnc.buildagent.api.Status) SystemConfig(org.jboss.pnc.common.json.moduleconfig.SystemConfig) BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) Mockito.mock(org.mockito.Mockito.mock) BuildExecutionConfiguration(org.jboss.pnc.spi.executor.BuildExecutionConfiguration) RunningBuild(org.jboss.pnc.spi.builddriver.RunningBuild) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 10 with BuildDriverException

use of org.jboss.pnc.spi.builddriver.exception.BuildDriverException in project pnc by project-ncl.

the class TermdBuildDriver method monitorBuildLiveness.

private CompletionStage<RemoteInvocationCompletion> monitorBuildLiveness(RemoteInvocation remoteInvocation) {
    CompletableFuture completableFuture = new CompletableFuture();
    AtomicReference<Long> lastSuccess = new AtomicReference<>();
    lastSuccess.set(System.currentTimeMillis());
    Runnable isAlive = () -> {
        if (remoteInvocation.isAlive()) {
            lastSuccess.set(System.currentTimeMillis());
        } else {
            Long last = lastSuccess.get();
            if (System.currentTimeMillis() - last > livenessFailTimeout) {
                logger.warn("Liveness probe failed.");
                RemoteInvocationCompletion completion = new RemoteInvocationCompletion(new BuildDriverException("Build Agent has gone away."));
                completableFuture.complete(completion);
            }
        }
    };
    ScheduledFuture<?> livenessMonitor = scheduledExecutorService.scheduleWithFixedDelay(isAlive, livenessProbeFrequency, livenessProbeFrequency, TimeUnit.MILLISECONDS);
    remoteInvocation.addPreClose(() -> livenessMonitor.cancel(false));
    return completableFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) BuildDriverException(org.jboss.pnc.spi.builddriver.exception.BuildDriverException) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Aggregations

BuildDriverException (org.jboss.pnc.spi.builddriver.exception.BuildDriverException)11 CompletedBuild (org.jboss.pnc.spi.builddriver.CompletedBuild)8 Consumer (java.util.function.Consumer)7 RunningEnvironment (org.jboss.pnc.spi.environment.RunningEnvironment)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 Test (org.junit.Test)6 SystemConfig (org.jboss.pnc.common.json.moduleconfig.SystemConfig)5 RunningBuild (org.jboss.pnc.spi.builddriver.RunningBuild)5 BuildExecutionSession (org.jboss.pnc.spi.executor.BuildExecutionSession)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Status (org.jboss.pnc.buildagent.api.Status)4 ConfigurationParseException (org.jboss.pnc.common.json.ConfigurationParseException)4 BuildExecutionConfiguration (org.jboss.pnc.spi.executor.BuildExecutionConfiguration)4 Mockito.doReturn (org.mockito.Mockito.doReturn)4 Mockito.mock (org.mockito.Mockito.mock)4 Paths (java.nio.file.Paths)3 TimeUnit (java.util.concurrent.TimeUnit)3 Inject (javax.inject.Inject)3 TermdBuildDriverModuleConfig (org.jboss.pnc.common.json.moduleconfig.TermdBuildDriverModuleConfig)3