Search in sources :

Example 21 with StageExecutorResult

use of pipelite.stage.executor.StageExecutorResult in project pipelite by enasequence.

the class SshCmdRunnerTest method echo.

@Test
public void echo() {
    SshCmdRunner cmdRunner = cmdRunner();
    StageExecutorResult result = cmdRunner.execute("echo test");
    assertThat(result.isError()).isFalse();
    assertThat(result.getAttribute(StageExecutorResultAttribute.EXIT_CODE)).isEqualTo("0");
    assertThat(result.getStageLog()).startsWith("test");
}
Also used : StageExecutorResult(pipelite.stage.executor.StageExecutorResult) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 22 with StageExecutorResult

use of pipelite.stage.executor.StageExecutorResult in project pipelite by enasequence.

the class SshCmdRunnerTest method unknownCommand.

@Test
public void unknownCommand() {
    SshCmdRunner cmdRunner = cmdRunner();
    StageExecutorResult result = cmdRunner.execute(PipeliteIdCreator.id());
    assertThat(result.isError()).isTrue();
    assertThat(result.getAttribute(StageExecutorResultAttribute.EXIT_CODE)).isNotEqualTo("0");
}
Also used : StageExecutorResult(pipelite.stage.executor.StageExecutorResult) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 23 with StageExecutorResult

use of pipelite.stage.executor.StageExecutorResult in project pipelite by enasequence.

the class DescribeJobsTest method error.

@Test
public void error() {
    int requestLimit = 10;
    int requestCnt = 100;
    AtomicInteger actualDescribeJobsCallCnt = new AtomicInteger();
    AtomicInteger actualRequestCnt = new AtomicInteger();
    DescribeJobsCallback<Integer, String> describeJobsCallback = (requestList, context) -> {
        actualDescribeJobsCallCnt.incrementAndGet();
        actualRequestCnt.addAndGet(requestList.size());
        return requestList.stream().collect(Collectors.toMap(i -> i, i -> StageExecutorResult.error()));
    };
    String executorContext = "test";
    final DescribeJobs<Integer, String> describeJobs = new DescribeJobs(mock(ServiceConfiguration.class), mock(InternalErrorService.class), requestLimit, executorContext, describeJobsCallback);
    IntStream.range(0, requestCnt).forEach(i -> describeJobs.addRequest(i));
    assertThat(describeJobs.getActiveRequests().size()).isEqualTo(requestCnt);
    describeJobs.makeRequests();
    assertThat(actualDescribeJobsCallCnt.get()).isEqualTo(requestCnt / requestLimit);
    assertThat(actualRequestCnt.get()).isEqualTo(requestCnt);
    // Check that there are no active requests.
    assertThat(describeJobs.getActiveRequests().size()).isEqualTo(0);
    // Check that the results are correct.
    IntStream.range(0, requestCnt).forEach(i -> {
        StageExecutorResult result = describeJobs.getResult(i, null);
        assertThat(result.isError()).isTrue();
    });
    // Check that the requests have been removed.
    IntStream.range(0, requestCnt).forEach(i -> assertThat(describeJobs.isRequest(i)).isFalse());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntStream(java.util.stream.IntStream) Test(org.junit.jupiter.api.Test) StageExecutorResult(pipelite.stage.executor.StageExecutorResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServiceConfiguration(pipelite.configuration.ServiceConfiguration) AssertionsForClassTypes.assertThat(org.assertj.core.api.AssertionsForClassTypes.assertThat) Collectors(java.util.stream.Collectors) Mockito.mock(org.mockito.Mockito.mock) InternalErrorService(pipelite.service.InternalErrorService) StageExecutorResult(pipelite.stage.executor.StageExecutorResult) ServiceConfiguration(pipelite.configuration.ServiceConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InternalErrorService(pipelite.service.InternalErrorService) Test(org.junit.jupiter.api.Test)

Example 24 with StageExecutorResult

use of pipelite.stage.executor.StageExecutorResult in project pipelite by enasequence.

the class KubernetesExecutor method describeJobs.

public static Map<String, StageExecutorResult> describeJobs(List<String> requests, KubernetesDescribeJobsCache.ExecutorContext executorContext) {
    log.atFine().log("Describing Kubernetes job results");
    Map<String, StageExecutorResult> results = new HashMap<>();
    Set<String> jobIds = new HashSet();
    String namespace = executorContext.getNamespace();
    try {
        KubernetesClient client = executorContext.getKubernetesClient();
        JobList jobList = RetryTask.DEFAULT.execute(r -> client.batch().v1().jobs().inNamespace(namespace).list());
        for (Job job : jobList.getItems()) {
            String jobId = job.getMetadata().getName();
            jobIds.add(jobId);
            results.put(jobId, describeJobsResult(namespace, jobId, client, job.getStatus()));
        }
        for (String jobId : requests) {
            if (!jobIds.contains(jobId)) {
                // Consider jobs that can't be found as failed.
                results.put(jobId, StageExecutorResult.error());
            }
        }
    } catch (KubernetesClientException e) {
        throw new PipeliteException("Kubernetes error", e);
    }
    return results;
}
Also used : StageExecutorResult(pipelite.stage.executor.StageExecutorResult) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) PipeliteException(pipelite.exception.PipeliteException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 25 with StageExecutorResult

use of pipelite.stage.executor.StageExecutorResult in project pipelite by enasequence.

the class KubernetesExecutor method describeJobsResult.

static StageExecutorResult describeJobsResult(String namespace, String jobId, KubernetesClient client, JobStatus jobStatus) {
    // Only one pod per job.
    StageExecutorResult result = describeJobsResultFromStatus(jobStatus);
    if (result.isActive()) {
        return result;
    }
    // Get exit code.
    Integer exitCode;
    try {
        List<Pod> pods = client.pods().inNamespace(namespace).withLabel("job-name", jobId).list().getItems();
        Pod pod = lastPodToStart(pods);
        if (pod == null || pod.getStatus() == null) {
            throw new PipeliteException("Could not get pod status for completed Kubernetes job: " + jobId);
        }
        List<ContainerStatus> containerStatuses = pod.getStatus().getContainerStatuses();
        ContainerStatus containerStatus = lastContainerToFinish(containerStatuses);
        if (containerStatus == null || containerStatus.getState() == null || containerStatus.getState().getTerminated() == null || containerStatus.getState().getTerminated().getExitCode() == null) {
            throw new PipeliteException("Could not get container status for completed Kubernetes job: " + jobId);
        }
        exitCode = containerStatus.getState().getTerminated().getExitCode();
    } catch (KubernetesClientException e) {
        throw new PipeliteException("Kubernetes error", e);
    }
    result.addAttribute(StageExecutorResultAttribute.EXIT_CODE, exitCode != null ? String.valueOf(exitCode) : "");
    result.addAttribute(StageExecutorResultAttribute.JOB_ID, jobId);
    return result;
}
Also used : StageExecutorResult(pipelite.stage.executor.StageExecutorResult) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus) Pod(io.fabric8.kubernetes.api.model.Pod) PipeliteException(pipelite.exception.PipeliteException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Aggregations

StageExecutorResult (pipelite.stage.executor.StageExecutorResult)33 Test (org.junit.jupiter.api.Test)14 PipeliteException (pipelite.exception.PipeliteException)5 Stage (pipelite.stage.Stage)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)3 Collectors (java.util.stream.Collectors)3 IntStream (java.util.stream.IntStream)3 AssertionsForClassTypes.assertThat (org.assertj.core.api.AssertionsForClassTypes.assertThat)3 Mockito.mock (org.mockito.Mockito.mock)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 ServiceConfiguration (pipelite.configuration.ServiceConfiguration)3 InternalErrorService (pipelite.service.InternalErrorService)3 Pod (io.fabric8.kubernetes.api.model.Pod)2 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)2 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)2 ZonedDateTime (java.time.ZonedDateTime)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 StageEntity (pipelite.entity.StageEntity)2 ContainerStatus (io.fabric8.kubernetes.api.model.ContainerStatus)1