Search in sources :

Example 6 with RunningBuild

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

the class TermdBuildDriverTest method shouldFetchFromGitAndBuild.

@Test(timeout = 15_000)
public void shouldFetchFromGitAndBuild() throws Throwable {
    // given
    Path tmpRepo = Files.createTempDirectory("tmpRepo");
    String repoPath = "file://" + tmpRepo.toAbsolutePath().toString() + "/test-repo";
    ZipUtils.unzipToDir(tmpRepo, "/repo.zip");
    String dirName = "test-repo-cloned";
    TermdBuildDriver driver = new TermdBuildDriver(systemConfig, buildDriverModuleConfig, clientFactory);
    BuildExecutionSession buildExecution = mock(BuildExecutionSession.class);
    BuildExecutionConfiguration buildExecutionConfiguration = mock(BuildExecutionConfiguration.class);
    doReturn(repoPath).when(buildExecutionConfiguration).getScmRepoURL();
    doReturn("master").when(buildExecutionConfiguration).getScmRevision();
    doReturn("mvn validate").when(buildExecutionConfiguration).getBuildScript();
    doReturn(dirName).when(buildExecutionConfiguration).getName();
    doReturn(buildExecutionConfiguration).when(buildExecution).getBuildExecutionConfiguration();
    doReturn(mock(RunningEnvironment.class)).when(buildExecution).getRunningEnvironment();
    AtomicReference<CompletedBuild> buildResult = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    Consumer<CompletedBuild> onComplete = (completedBuild) -> {
        logger.info("Build completed.");
        buildResult.set(completedBuild);
        latch.countDown();
    };
    Consumer<Throwable> onError = (throwable) -> {
        logger.error("Error received: ", throwable);
        fail(throwable.getMessage());
    };
    // when
    RunningBuild runningBuild = driver.startProjectBuild(buildExecution, localEnvironmentPointer, onComplete, // TODO set monitor
    onError);
    // before the build
    // starts
    logger.info("Waiting for build to complete...");
    latch.await();
    // then
    assertThat(buildResult.get().getBuildResult()).isNotNull();
    assertThat(buildResult.get().getBuildResult().getBuildLog()).isNotEmpty();
    assertThat(Files.exists(localEnvironmentPointer.getWorkingDirectory())).isTrue();
    assertThat(Files.exists(localEnvironmentPointer.getWorkingDirectory().resolve(dirName))).isTrue();
}
Also used : Path(java.nio.file.Path) 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) RunningEnvironment(org.jboss.pnc.spi.environment.RunningEnvironment) Test(org.junit.Test)

Example 7 with RunningBuild

use of org.jboss.pnc.spi.builddriver.RunningBuild 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

Consumer (java.util.function.Consumer)7 SystemConfig (org.jboss.pnc.common.json.moduleconfig.SystemConfig)7 CompletedBuild (org.jboss.pnc.spi.builddriver.CompletedBuild)7 RunningBuild (org.jboss.pnc.spi.builddriver.RunningBuild)7 RunningEnvironment (org.jboss.pnc.spi.environment.RunningEnvironment)7 BuildExecutionSession (org.jboss.pnc.spi.executor.BuildExecutionSession)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 BuildDriverException (org.jboss.pnc.spi.builddriver.exception.BuildDriverException)6 BuildExecutionConfiguration (org.jboss.pnc.spi.executor.BuildExecutionConfiguration)6 Status (org.jboss.pnc.buildagent.api.Status)5 Test (org.junit.Test)5 Mockito.doReturn (org.mockito.Mockito.doReturn)5 Mockito.mock (org.mockito.Mockito.mock)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 ConfigurationParseException (org.jboss.pnc.common.json.ConfigurationParseException)4 DebugData (org.jboss.pnc.spi.builddriver.DebugData)4 IOException (java.io.IOException)3 MethodHandles (java.lang.invoke.MethodHandles)3 Files (java.nio.file.Files)3