Search in sources :

Example 11 with BuildExecutionSession

use of org.jboss.pnc.spi.executor.BuildExecutionSession in project pnc by project-ncl.

the class BuildExecutionTest method shouldReceiveBuildResultViaHttpCallback.

@Test(timeout = 10000L)
public void shouldReceiveBuildResultViaHttpCallback() throws IOException, NoSuchAlgorithmException, URISyntaxException, InterruptedException, ExecutionException, TimeoutException {
    // given
    TaskStatusUpdateEvent.Builder updateEventBuilder = TaskStatusUpdateEvent.newBuilder();
    updateEventBuilder.taskId(UUID.randomUUID().toString()).newStatus(Status.COMPLETED).outputChecksum(Md5.digest("black"));
    Credentials user = Credentials.USER;
    List<Request.Header> headers = new ArrayList<>();
    headers.add(new Request.Header(Headers.CONTENT_TYPE_STRING, MediaType.APPLICATION_JSON));
    user.createAuthHeader((k, v) -> {
        headers.add(new Request.Header(k, v));
        return null;
    });
    String data = objectMapper.writeValueAsString(updateEventBuilder.build());
    BlockingQueue<TaskStatusUpdateEvent> events = new ArrayBlockingQueue<>(10);
    Consumer<TaskStatusUpdateEvent> statusChangeConsumer = (e) -> {
        events.add(e);
    };
    BuildExecutionSession session = createFakeExectionSession(statusChangeConsumer);
    // when
    String executionId = "11";
    ((BuildExecutorMock) buildExecutor).addRunningExecution(executionId, session);
    HttpClient httpClient = new HttpClient();
    Request request = new Request(Request.Method.POST, URI.create("http://localhost:8080/pnc-rest/v2/build-execution/" + executionId + "/completed"), headers);
    CompletableFuture<HttpClient.Response> responseFuture = httpClient.invoke(request, data);
    // then
    // event received
    TaskStatusUpdateEvent event = events.take();
    Assert.assertEquals(Status.COMPLETED, event.getNewStatus());
    Assert.assertEquals(200, responseFuture.get(5, TimeUnit.SECONDS).getCode());
}
Also used : Arquillian(org.jboss.arquillian.junit.Arquillian) Credentials(org.jboss.pnc.integration.setup.Credentials) URISyntaxException(java.net.URISyntaxException) RunWith(org.junit.runner.RunWith) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) ContainerTest(org.jboss.pnc.test.category.ContainerTest) Inject(javax.inject.Inject) MediaType(javax.ws.rs.core.MediaType) BuildExecutorMock(org.jboss.pnc.mock.executor.BuildExecutorMock) URI(java.net.URI) Deployments(org.jboss.pnc.integration.setup.Deployments) TaskStatusUpdateEvent(org.jboss.pnc.buildagent.api.TaskStatusUpdateEvent) EnterpriseArchive(org.jboss.shrinkwrap.api.spec.EnterpriseArchive) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) BuildExecutionSessionMock(org.jboss.pnc.mock.executor.BuildExecutionSessionMock) UUID(java.util.UUID) BuildExecutor(org.jboss.pnc.spi.executor.BuildExecutor) Category(org.junit.experimental.categories.Category) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) List(java.util.List) Deployment(org.jboss.arquillian.container.test.api.Deployment) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) Md5(org.jboss.pnc.common.security.Md5) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Headers(io.undertow.util.Headers) Request(org.jboss.pnc.api.dto.Request) Status(org.jboss.pnc.buildagent.api.Status) EXECUTOR_JAR(org.jboss.pnc.integration.setup.Deployments.EXECUTOR_JAR) HttpClient(org.jboss.pnc.buildagent.common.http.HttpClient) Assert(org.junit.Assert) BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) BuildExecutorMock(org.jboss.pnc.mock.executor.BuildExecutorMock) ArrayList(java.util.ArrayList) Request(org.jboss.pnc.api.dto.Request) TaskStatusUpdateEvent(org.jboss.pnc.buildagent.api.TaskStatusUpdateEvent) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) HttpClient(org.jboss.pnc.buildagent.common.http.HttpClient) Credentials(org.jboss.pnc.integration.setup.Credentials) ContainerTest(org.jboss.pnc.test.category.ContainerTest) Test(org.junit.Test)

Example 12 with BuildExecutionSession

use of org.jboss.pnc.spi.executor.BuildExecutionSession 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 13 with BuildExecutionSession

use of org.jboss.pnc.spi.executor.BuildExecutionSession 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 14 with BuildExecutionSession

use of org.jboss.pnc.spi.executor.BuildExecutionSession 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 15 with BuildExecutionSession

use of org.jboss.pnc.spi.executor.BuildExecutionSession in project pnc by project-ncl.

the class BuildExecutorTriggerer method executeBuild.

public BuildExecutionSession executeBuild(BuildExecutionConfiguration buildExecutionConfig, String callbackUrl, String accessToken) throws CoreException, ExecutorException {
    Consumer<BuildExecutionStatusChangedEvent> onExecutionStatusChange = (statusChangedEvent) -> {
        log.debug("Received BuildExecutionStatusChangedEvent: " + statusChangedEvent);
        if (statusChangedEvent.isFinal() && callbackUrl != null && !callbackUrl.isEmpty()) {
            statusChangedEvent.getBuildResult().ifPresent((buildResult) -> bpmNotifier.sendBuildExecutionCompleted(callbackUrl, buildResult, accessToken));
        }
    };
    BuildExecutionSession buildExecutionSession = buildExecutor.startBuilding(buildExecutionConfig, onExecutionStatusChange, accessToken);
    return buildExecutionSession;
}
Also used : Logger(org.slf4j.Logger) TaskStatusUpdateEvent(org.jboss.pnc.buildagent.api.TaskStatusUpdateEvent) BuildTaskContext(org.jboss.pnc.common.logging.BuildTaskContext) ExpiresDate(org.jboss.pnc.common.Date.ExpiresDate) BpmNotifier(org.jboss.pnc.bpm.notification.BpmNotifier) LoggerFactory(org.slf4j.LoggerFactory) BuildExecutor(org.jboss.pnc.spi.executor.BuildExecutor) Inject(javax.inject.Inject) Consumer(java.util.function.Consumer) CoreException(org.jboss.pnc.spi.exception.CoreException) Optional(java.util.Optional) BuildExecutionStatusChangedEvent(org.jboss.pnc.spi.events.BuildExecutionStatusChangedEvent) ApplicationScoped(javax.enterprise.context.ApplicationScoped) BuildExecutionConfiguration(org.jboss.pnc.spi.executor.BuildExecutionConfiguration) ExecutorException(org.jboss.pnc.spi.executor.exceptions.ExecutorException) SystemConfig(org.jboss.pnc.common.json.moduleconfig.SystemConfig) BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) BuildExecutionSession(org.jboss.pnc.spi.executor.BuildExecutionSession) BuildExecutionStatusChangedEvent(org.jboss.pnc.spi.events.BuildExecutionStatusChangedEvent)

Aggregations

BuildExecutionSession (org.jboss.pnc.spi.executor.BuildExecutionSession)15 Consumer (java.util.function.Consumer)11 BuildExecutionConfiguration (org.jboss.pnc.spi.executor.BuildExecutionConfiguration)10 Logger (org.slf4j.Logger)10 LoggerFactory (org.slf4j.LoggerFactory)10 SystemConfig (org.jboss.pnc.common.json.moduleconfig.SystemConfig)9 CompletedBuild (org.jboss.pnc.spi.builddriver.CompletedBuild)8 RunningBuild (org.jboss.pnc.spi.builddriver.RunningBuild)8 RunningEnvironment (org.jboss.pnc.spi.environment.RunningEnvironment)8 Status (org.jboss.pnc.buildagent.api.Status)6 BuildDriverException (org.jboss.pnc.spi.builddriver.exception.BuildDriverException)6 Inject (javax.inject.Inject)5 TaskStatusUpdateEvent (org.jboss.pnc.buildagent.api.TaskStatusUpdateEvent)5 ConfigurationParseException (org.jboss.pnc.common.json.ConfigurationParseException)5 BuildExecutor (org.jboss.pnc.spi.executor.BuildExecutor)5 CompletableFuture (java.util.concurrent.CompletableFuture)4 ExecutorService (java.util.concurrent.ExecutorService)4 TimeUnit (java.util.concurrent.TimeUnit)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 ApplicationScoped (javax.enterprise.context.ApplicationScoped)4