Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP #7777

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft

WIP #7777

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/src/main/java/io/kestra/core/models/Label.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public record Label(@NotNull String key, @NotNull String value) {
public static final String RESTARTED = SYSTEM_PREFIX + "restarted";
public static final String REPLAY = SYSTEM_PREFIX + "replay";
public static final String REPLAYED = SYSTEM_PREFIX + "replayed";
public static final String TEST = SYSTEM_PREFIX + "test";

/**
* Static helper method for converting a list of labels to a nested map.
Expand Down
28 changes: 24 additions & 4 deletions core/src/main/java/io/kestra/core/models/executions/Execution.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import io.kestra.core.serializers.ListOrMapOfLabelDeserializer;
import io.kestra.core.serializers.ListOrMapOfLabelSerializer;
import io.kestra.core.services.LabelService;
import io.kestra.core.test.Fixture;
import io.kestra.core.utils.IdUtils;
import io.kestra.core.utils.ListUtils;
import io.kestra.core.utils.MapUtils;
import io.micronaut.core.annotation.Nullable;
import io.swagger.v3.oas.annotations.Hidden;
Expand Down Expand Up @@ -111,6 +113,10 @@ public class Execution implements DeletedInterface, TenantInterface {
@Setter
String traceParent;

@With
@Nullable
List<Fixture> fixtures;

/**
* Factory method for constructing a new {@link Execution} object for the given {@link Flow}.
*
Expand Down Expand Up @@ -205,7 +211,8 @@ public Execution withState(State.Type state) {
this.deleted,
this.metadata,
this.scheduleDate,
this.traceParent
this.traceParent,
this.fixtures
);
}

Expand All @@ -229,7 +236,8 @@ public Execution withLabels(List<Label> labels) {
this.deleted,
this.metadata,
this.scheduleDate,
this.traceParent
this.traceParent,
this.fixtures
);
}

Expand Down Expand Up @@ -266,7 +274,8 @@ public Execution withTaskRun(TaskRun taskRun) throws InternalException {
this.deleted,
this.metadata,
this.scheduleDate,
this.traceParent
this.traceParent,
this.fixtures
);
}

Expand All @@ -290,7 +299,8 @@ public Execution childExecution(String childExecutionId, List<TaskRun> taskRunLi
this.deleted,
this.metadata,
this.scheduleDate,
this.traceParent
this.traceParent,
this.fixtures
);
}

Expand Down Expand Up @@ -723,6 +733,16 @@ public FailedExecutionWithLog failedExecutionFromExecutor(Exception e) {
);
}

public Optional<Fixture> getFixtureForTaskRun(TaskRun taskRun) {
if (this.fixtures == null) {
return Optional.empty();
}

return this.fixtures.stream()
.filter(fixture -> Objects.equals(fixture.getId(), taskRun.getTaskId()) && Objects.equals(fixture.getValue(), taskRun.getValue()))
.findFirst();
}

/**
* Create a new attempt for failed worker execution
*
Expand Down
31 changes: 29 additions & 2 deletions core/src/main/java/io/kestra/core/runners/ExecutorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import io.kestra.core.queues.QueueFactoryInterface;
import io.kestra.core.queues.QueueInterface;
import io.kestra.core.services.*;
import io.kestra.core.test.Fixture;
import io.kestra.core.trace.propagation.RunContextTextMapSetter;
import io.kestra.core.utils.IdUtils;
import io.kestra.core.utils.ListUtils;
import io.kestra.core.utils.TruthUtils;
import io.kestra.plugin.core.flow.Pause;
Expand Down Expand Up @@ -759,7 +761,7 @@ private Executor handleWorkerTask(final Executor executor) throws InternalExcept
Map<Boolean, List<WorkerTask>> workerTasks = executor.getExecution()
.getTaskRunList()
.stream()
.filter(taskRun -> taskRun.getState().getCurrent().isCreated())
.filter(taskRun -> taskRun.getState().getCurrent().isCreated() && executor.getExecution().getFixtureForTaskRun(taskRun).isEmpty())
.map(throwFunction(taskRun -> {
Task task = executor.getFlow().findTaskByTaskId(taskRun.getTaskId());
RunContext runContext = runContextFactory.of(executor.getFlow(), task, executor.getExecution(), taskRun);
Expand Down Expand Up @@ -812,7 +814,32 @@ private Executor handleWorkerTask(final Executor executor) throws InternalExcept
)
.collect(Collectors.groupingBy(workerTask -> workerTask.getTaskRun().getState().isFailed() || workerTask.getTaskRun().getState().getCurrent() == State.Type.CANCELLED));

if (workerTasks.isEmpty()) {
// mock WorkerTaskResult for mocked execution
// submit TaskRun when receiving created, must be done after the state execution store
// TODO check if it cannot be done directly inside the Execution
boolean hasMockedWorkerTask = false;
record FixtureAndTaskRun(Fixture fixture, TaskRun taskRun) {} // FIXME temporal
if (executor.getExecution().getFixtures() != null) {
List<WorkerTaskResult> workerTaskResults = executor.getExecution()
.getTaskRunList()
.stream()
.filter(taskRun -> taskRun.getState().getCurrent().isCreated())
.flatMap(taskRun -> executor.getExecution().getFixtureForTaskRun(taskRun).stream().map(fixture -> new FixtureAndTaskRun(fixture, taskRun)))
.map(fixtureAndTaskRun -> WorkerTaskResult.builder()
.taskRun(fixtureAndTaskRun.taskRun()
.withState(Optional.ofNullable(fixtureAndTaskRun.fixture().getState()).orElse(State.Type.SUCCESS))
.withOutputs(fixtureAndTaskRun.fixture().getOutputs())
)
.build()
)
.toList();

hasMockedWorkerTask = !workerTaskResults.isEmpty();
this.addWorkerTaskResults(executor, executor.getFlow(), workerTaskResults);
}


if (workerTasks.isEmpty() || hasMockedWorkerTask) {
return executor;
}

Expand Down
23 changes: 23 additions & 0 deletions core/src/main/java/io/kestra/core/test/Fixture.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.kestra.core.test;

import io.kestra.core.models.flows.State;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;

import java.util.Map;

@Getter
@Builder
public class Fixture {
@NotNull
private String id;

private String value;

@Builder.Default
private State.Type state = State.Type.SUCCESS;

private Map<String, Object> outputs;
}
15 changes: 15 additions & 0 deletions core/src/main/java/io/kestra/core/test/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.kestra.core.test;

import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class Input {
@NotNull
private String id;

@NotNull
private String value;
}
24 changes: 24 additions & 0 deletions core/src/main/java/io/kestra/core/test/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.kestra.core.test;

import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;

import java.util.List;

@Getter
@Builder
public class Test {
@NotNull
private String id;

@NotNull
private String namespace;

@NotNull
private String flowId;

private List<Input> inputs;

private List<Fixture> fixtures;
}
21 changes: 21 additions & 0 deletions test1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
id: test1
namespace: company.team
flowId: kookabura_411641

# Define inputs
inputs:
- id: input
value: toto

# Define fixtures: a.k.a. mock of task runs and their outputs
fixtures:
# Don't run it and set its output
- id: output
outputs:
values:
name: fake
# Don't run it
- id: sleep
# Don't run it and set its state to WARNING
- id: fail
state: WARNING
13 changes: 13 additions & 0 deletions test2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
id: test2
namespace: company.team
flowId: dogfish_450884

# Define inputs
inputs:
- id: input
value: fake

# Define fixtures: a.k.a. mock of task runs and their outputs
fixtures:
# Don't run it
- id: start
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.kestra.core.storages.StorageContext;
import io.kestra.core.storages.StorageInterface;
import io.kestra.core.tenant.TenantService;
import io.kestra.core.test.Fixture;
import io.kestra.core.trace.propagation.ExecutionTextMapSetter;
import io.kestra.core.utils.Await;
import io.kestra.core.utils.ListUtils;
Expand Down Expand Up @@ -694,8 +695,8 @@ public static class ExecutionResponse extends Execution {
private final URI url;

// This is not nice, but we cannot use @AllArgsConstructor as it would open a bunch of necessary changes on the Execution class.
ExecutionResponse(String tenantId, String id, String namespace, String flowId, Integer flowRevision, List<TaskRun> taskRunList, Map<String, Object> inputs, Map<String, Object> outputs, List<Label> labels, Map<String, Object> variables, State state, String parentId, String originalId, ExecutionTrigger trigger, boolean deleted, ExecutionMetadata metadata, Instant scheduleDate, String traceParent, URI url) {
super(tenantId, id, namespace, flowId, flowRevision, taskRunList, inputs, outputs, labels, variables, state, parentId, originalId, trigger, deleted, metadata, scheduleDate, traceParent);
ExecutionResponse(String tenantId, String id, String namespace, String flowId, Integer flowRevision, List<TaskRun> taskRunList, Map<String, Object> inputs, Map<String, Object> outputs, List<Label> labels, Map<String, Object> variables, State state, String parentId, String originalId, ExecutionTrigger trigger, boolean deleted, ExecutionMetadata metadata, Instant scheduleDate, String traceParent, List<Fixture> fixtures, URI url) {
super(tenantId, id, namespace, flowId, flowRevision, taskRunList, inputs, outputs, labels, variables, state, parentId, originalId, trigger, deleted, metadata, scheduleDate, traceParent, fixtures);

this.url = url;
}
Expand All @@ -720,6 +721,7 @@ public static ExecutionResponse fromExecution(Execution execution, URI url) {
execution.getMetadata(),
execution.getScheduleDate(),
execution.getTraceParent(),
execution.getFixtures(),
url
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package io.kestra.webserver.controllers.api;

import io.kestra.core.events.CrudEvent;
import io.kestra.core.events.CrudEventType;
import io.kestra.core.models.Label;
import io.kestra.core.models.executions.Execution;
import io.kestra.core.models.executions.TaskRun;
import io.kestra.core.models.flows.Flow;
import io.kestra.core.models.flows.State;
import io.kestra.core.queues.QueueException;
import io.kestra.core.queues.QueueFactoryInterface;
import io.kestra.core.queues.QueueInterface;
import io.kestra.core.serializers.YamlParser;
import io.kestra.core.services.FlowService;
import io.kestra.core.tenant.TenantService;
import io.kestra.core.test.Test;
import io.kestra.core.utils.IdUtils;
import io.kestra.core.utils.ListUtils;
import io.micronaut.context.event.ApplicationEventPublisher;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Post;
import io.micronaut.scheduling.TaskExecutors;
import io.micronaut.scheduling.annotation.ExecuteOn;
import io.micronaut.validation.Validated;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static io.kestra.core.utils.Rethrow.throwFunction;

@Slf4j
@Validated
@Controller("/api/v1/tests")
public class TestController {
@Inject
private FlowService flowService;

@Inject
private TenantService tenantService;

@Inject
private YamlParser yamlParser;

@Inject
@Named(QueueFactoryInterface.EXECUTION_NAMED)
protected QueueInterface<Execution> executionQueue;

@Inject
private ApplicationEventPublisher<CrudEvent<Execution>> eventPublisher;

@ExecuteOn(TaskExecutors.IO)
@Post(consumes = MediaType.APPLICATION_YAML)
@Operation(tags = {"Flows"}, summary = "Test a flow")
public void test(@Parameter(description = "The flow namespace") @Body String testStr) throws QueueException, InterruptedException {
Test test = yamlParser.parse(testStr, Test.class);

Flow flow = flowService.getFlowIfExecutableOrThrow(tenantService.resolveTenant(), test.getNamespace(), test.getFlowId(), Optional.empty());
Map<String, Object> inputs = ListUtils.emptyOnNull(test.getInputs()).stream().collect(Collectors.toMap(
input -> input.getId(),
input -> input.getValue()
));
Execution current = Execution.newExecution(flow, (f, e) -> inputs, null, Optional.empty())
.withFixtures(test.getFixtures());


List<Label> existingLabels = ListUtils.emptyOnNull(current.getLabels());
existingLabels.add(new Label(Label.TEST, "true"));
current = current.withLabels(existingLabels);

// List<TaskRun> taskRuns = ListUtils.emptyOnNull(test.getFixtures()).stream()
// .map(throwFunction(fixture -> {
// Thread.sleep(1); // This is to have correct ordering based on task run start date
// return TaskRun.builder()
// .id(IdUtils.create())
// .state(new State().withState(Optional.ofNullable(fixture.getState()).orElse(State.Type.SUCCESS)))
// .taskId(fixture.getId())
// .tenantId(flow.getTenantId())
// .namespace(test.getNamespace())
// .flowId(test.getFlowId())
// .outputs(fixture.getOutputs())
// .build();
// }
// ))
// .toList();
// current = current.withTaskRunList(taskRuns);

executionQueue.emit(current);
eventPublisher.publishEvent(new CrudEvent<>(current, CrudEventType.CREATE));
}

}
Loading