generated from kestra-io/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(script): no Docker options when not using Docker
- Loading branch information
Showing
6 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
.../java/io/kestra/plugin/scripts/exec/scripts/validations/AbstractExecScriptValidation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.kestra.plugin.scripts.exec.scripts.validations; | ||
|
||
import io.kestra.plugin.scripts.exec.scripts.validations.validators.AbstractExecScriptValidator; | ||
|
||
import javax.validation.Constraint; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Constraint(validatedBy = AbstractExecScriptValidator.class) | ||
@Inherited | ||
public @interface AbstractExecScriptValidation { | ||
String message() default "invalid script ({validatedValue})"; | ||
} |
36 changes: 36 additions & 0 deletions
36
...estra/plugin/scripts/exec/scripts/validations/validators/AbstractExecScriptValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.kestra.plugin.scripts.exec.scripts.validations.validators; | ||
|
||
import io.kestra.plugin.scripts.exec.scripts.models.DockerOptions; | ||
import io.kestra.plugin.scripts.exec.scripts.models.RunnerType; | ||
import io.kestra.plugin.scripts.exec.scripts.validations.AbstractExecScriptValidation; | ||
import io.micronaut.core.annotation.AnnotationValue; | ||
import io.micronaut.core.annotation.Introspected; | ||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.core.annotation.Nullable; | ||
import io.micronaut.validation.validator.constraints.ConstraintValidator; | ||
import io.micronaut.validation.validator.constraints.ConstraintValidatorContext; | ||
import jakarta.inject.Singleton; | ||
|
||
@Singleton | ||
@Introspected | ||
public class AbstractExecScriptValidator implements ConstraintValidator<AbstractExecScriptValidation, io.kestra.plugin.scripts.exec.AbstractExecScript> { | ||
@Override | ||
public boolean isValid( | ||
@Nullable io.kestra.plugin.scripts.exec.AbstractExecScript value, | ||
@NonNull AnnotationValue<AbstractExecScriptValidation> annotationMetadata, | ||
@NonNull ConstraintValidatorContext context) { | ||
if (value == null) { | ||
return true; | ||
} | ||
|
||
final DockerOptions defaultOptions = DockerOptions.builder().build(); | ||
|
||
if (value.getRunner() != RunnerType.DOCKER && value.getDocker() != null && !value.getDocker().equals(defaultOptions)) { | ||
context.messageTemplate("invalid script: custom Docker options require the Docker runner"); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
plugin-script/src/test/java/io/kestra/plugin/scripts/exec/AbstractExecScriptTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package io.kestra.plugin.scripts.exec; | ||
|
||
import io.kestra.core.models.validations.ModelValidator; | ||
import io.kestra.core.runners.RunContext; | ||
import io.kestra.plugin.scripts.exec.scripts.models.DockerOptions; | ||
import io.kestra.plugin.scripts.exec.scripts.models.RunnerType; | ||
import io.kestra.plugin.scripts.exec.scripts.models.ScriptOutput; | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import jakarta.inject.Inject; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.experimental.SuperBuilder; | ||
import org.apache.commons.lang3.NotImplementedException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
@MicronautTest | ||
class AbstractExecScriptTest { | ||
@Inject | ||
private ModelValidator modelValidator; | ||
|
||
@SuperBuilder | ||
@Getter | ||
public static class AbstractExecScriptCls extends AbstractExecScript { | ||
@Builder.Default | ||
protected DockerOptions docker = DockerOptions.builder().build(); | ||
|
||
@Override | ||
public ScriptOutput run(RunContext runContext) throws Exception { | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
|
||
@Test | ||
void validation() { | ||
final AbstractExecScriptCls defaults = AbstractExecScriptCls.builder() | ||
.id("unit-test") | ||
.type(AbstractExecScriptCls.class.getName()) | ||
.build(); | ||
|
||
assertThat(modelValidator.isValid(defaults).isEmpty(), is(true)); | ||
|
||
final AbstractExecScriptCls validDocker = AbstractExecScriptCls.builder() | ||
.id("unit-test") | ||
.type(AbstractExecScriptCls.class.getName()) | ||
.docker(DockerOptions.builder() | ||
.pullPolicy(DockerOptions.PullPolicy.IF_NOT_PRESENT) | ||
.build() | ||
) | ||
.runner(RunnerType.DOCKER) | ||
.build(); | ||
|
||
assertThat(modelValidator.isValid(validDocker).isEmpty(), is(true)); | ||
|
||
final AbstractExecScriptCls validProcess = AbstractExecScriptCls.builder() | ||
.id("unit-test") | ||
.type(AbstractExecScriptCls.class.getName()) | ||
.runner(RunnerType.PROCESS) | ||
.build(); | ||
|
||
assertThat(modelValidator.isValid(validProcess).isEmpty(), is(true)); | ||
|
||
final AbstractExecScriptCls dockerPolicyWithProcessRunner = AbstractExecScriptCls.builder() | ||
.id("unit-test") | ||
.type(AbstractExecScriptCls.class.getName()) | ||
.docker(DockerOptions.builder() | ||
.pullPolicy(DockerOptions.PullPolicy.IF_NOT_PRESENT) | ||
.build() | ||
) | ||
.runner(RunnerType.PROCESS) | ||
.build(); | ||
|
||
assertThat(modelValidator.isValid(dockerPolicyWithProcessRunner).isPresent(), is(true)); | ||
assertThat( | ||
modelValidator.isValid(dockerPolicyWithProcessRunner).get().getMessage(), | ||
containsString(": invalid script: custom Docker options require the Docker runner") | ||
); | ||
} | ||
} |