Search in sources :

Example 1 with CANCELLED

use of org.jboss.pnc.enums.BuildStatus.CANCELLED in project pnc by project-ncl.

the class TermdBuildDriverTest method shouldStartAndCancelTheExecutionImmediately.

@Test(timeout = 5_000)
public void shouldStartAndCancelTheExecutionImmediately() throws ConfigurationParseException, BuildDriverException, InterruptedException, IOException {
    // given
    String dirName = "test-workdir";
    String logStart = "Running the command...";
    String logEnd = "Command completed.";
    TermdBuildDriver driver = new TermdBuildDriver(systemConfig, buildDriverModuleConfig, clientFactory);
    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
    CountDownLatch latch = new CountDownLatch(1);
    Consumer<CompletedBuild> onComplete = (completedBuild) -> {
        buildResult.set(completedBuild);
        latch.countDown();
    };
    Consumer<Throwable> onError = (throwable) -> {
        logger.error("Error received: ", throwable);
        fail(throwable.getMessage());
    };
    RunningBuild runningBuild = driver.startProjectBuild(buildExecution, localEnvironmentPointer, onComplete, onError);
    runningBuild.cancel();
    latch.await();
    // then
    assertThat(buildResult.get().getBuildResult().getBuildLog()).doesNotContain(logEnd);
    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 2 with CANCELLED

use of org.jboss.pnc.enums.BuildStatus.CANCELLED 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)

Aggregations

IOException (java.io.IOException)2 MethodHandles (java.lang.invoke.MethodHandles)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Consumer (java.util.function.Consumer)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Status (org.jboss.pnc.buildagent.api.Status)2 TaskStatusUpdateEvent (org.jboss.pnc.buildagent.api.TaskStatusUpdateEvent)2 ConfigurationParseException (org.jboss.pnc.common.json.ConfigurationParseException)2 SystemConfig (org.jboss.pnc.common.json.moduleconfig.SystemConfig)2 CANCELLED (org.jboss.pnc.enums.BuildStatus.CANCELLED)2 CompletedBuild (org.jboss.pnc.spi.builddriver.CompletedBuild)2 RunningBuild (org.jboss.pnc.spi.builddriver.RunningBuild)2 BuildDriverException (org.jboss.pnc.spi.builddriver.exception.BuildDriverException)2 RunningEnvironment (org.jboss.pnc.spi.environment.RunningEnvironment)2 BuildExecutionConfiguration (org.jboss.pnc.spi.executor.BuildExecutionConfiguration)2 BuildExecutionSession (org.jboss.pnc.spi.executor.BuildExecutionSession)2 Assert.fail (org.junit.Assert.fail)2