From 4513faf487b7887eb853c8046964f6a6634179f6 Mon Sep 17 00:00:00 2001 From: Ricardo Zanini Date: Tue, 26 Dec 2023 17:25:12 -0300 Subject: [PATCH] Make SonataFlow images deps available offline, fix jbang setup and deps Signed-off-by: Ricardo Zanini --- .../common/scripts/added/create-app.sh | 8 +- tests/shell/README.md | 21 +++ .../kogito-swf-builder/.vscode/launch.json | 22 ++++ .../kogito-swf-builder/.vscode/settings.json | 18 +++ tests/shell/kogito-swf-builder/RunTests.java | 118 ----------------- .../greet-with-inputschema/Dockerfile | 2 +- .../resources/greet/Dockerfile | 2 +- .../kogito-swf-builder/src/RunTests.java | 121 ++++++++++++++++++ .../kogito-swf-devmode/.vscode/launch.json | 22 ++++ .../kogito-swf-devmode/.vscode/settings.json | 18 +++ .../{ => src}/RunTests.java | 31 ++--- tests/shell/run.sh | 2 +- 12 files changed, 244 insertions(+), 141 deletions(-) create mode 100644 tests/shell/README.md create mode 100644 tests/shell/kogito-swf-builder/.vscode/launch.json create mode 100644 tests/shell/kogito-swf-builder/.vscode/settings.json delete mode 100644 tests/shell/kogito-swf-builder/RunTests.java create mode 100644 tests/shell/kogito-swf-builder/src/RunTests.java create mode 100644 tests/shell/kogito-swf-devmode/.vscode/launch.json create mode 100644 tests/shell/kogito-swf-devmode/.vscode/settings.json rename tests/shell/kogito-swf-devmode/{ => src}/RunTests.java (71%) diff --git a/modules/kogito-swf/common/scripts/added/create-app.sh b/modules/kogito-swf/common/scripts/added/create-app.sh index 6153a02d9..5a4ce738a 100755 --- a/modules/kogito-swf/common/scripts/added/create-app.sh +++ b/modules/kogito-swf/common/scripts/added/create-app.sh @@ -104,9 +104,11 @@ if [ "${SCRIPT_DEBUG^^}" = "TRUE" ]; then cat pom.xml fi +# we force the dependencies download beforehand, so we won't have problems when running or building our apps in offline mode +# see: +# https://quarkus.io/guides/maven-tooling#downloading-maven-artifact-dependencies-for-offline-development-and-testing +# https://maven.apache.org/plugins/maven-dependency-plugin/go-offline-mojo.html "${MAVEN_HOME}"/bin/mvn -B ${MAVEN_ARGS_APPEND} \ -nsu \ -s "${MAVEN_SETTINGS_PATH}" \ - -DskipTests \ - -Dquarkus.container-image.build=false \ - clean install + clean dependency:go-offline io.quarkus.platform:quarkus-maven-plugin:"${QUARKUS_PLATFORM_VERSION}":go-offline diff --git a/tests/shell/README.md b/tests/shell/README.md new file mode 100644 index 000000000..687dc4d2f --- /dev/null +++ b/tests/shell/README.md @@ -0,0 +1,21 @@ +# Tests in Shell + +## Running Tests With JBang + +- Install JBang +- Install VSCode Red Hat's Java plugin +- Install VSCode JBang plugin + +You can then edit the files in `kogito-swf-builder` and `kogito-swf-devmode` with intellisense. + +The `run.sh` should be used to run the tests since it must set a few env vars. To run from your terminal, try: + +```shell +tests/shell/run.sh kogito-swf-devmode quay.io/kiegroup/kogito-swf-devmode:999-SNAPSHOT +``` + +The first argument is the test case to run and the second, the image. + +Under the hood, it uses [Junit's Console Launcher](https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher) tool to run the tests from the command line. + +Update this file with new findings, and don't remove the `.vscode` folder. It's useful to run JBang from the IDE. diff --git a/tests/shell/kogito-swf-builder/.vscode/launch.json b/tests/shell/kogito-swf-builder/.vscode/launch.json new file mode 100644 index 000000000..f329b9513 --- /dev/null +++ b/tests/shell/kogito-swf-builder/.vscode/launch.json @@ -0,0 +1,22 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "RunTests-port-4004", + "request": "attach", + "hostName": "localhost", + "port": 4004 + }, + { + "type": "java", + "name": "Debug (Launch) - RunTests", + "request": "launch", + "mainClass": "RunTests", + "projectName": "RunTests" + } + ] +} diff --git a/tests/shell/kogito-swf-builder/.vscode/settings.json b/tests/shell/kogito-swf-builder/.vscode/settings.json new file mode 100644 index 000000000..ca7dd1980 --- /dev/null +++ b/tests/shell/kogito-swf-builder/.vscode/settings.json @@ -0,0 +1,18 @@ +{ + "java.import.gradle.enabled": false, + "java.import.maven.enabled": false, + "java.eclipse.downloadSources": true, + "files.exclude": { + "bin/": true, + ".eclipse/": true, + ".project": true, + ".classpath": true, + "build.gradle": true + }, + "java.completion.importOrder": [ + "", + "javax", + "java", + "#" + ] +} \ No newline at end of file diff --git a/tests/shell/kogito-swf-builder/RunTests.java b/tests/shell/kogito-swf-builder/RunTests.java deleted file mode 100644 index 8c5705028..000000000 --- a/tests/shell/kogito-swf-builder/RunTests.java +++ /dev/null @@ -1,118 +0,0 @@ -///usr/bin/env jbang "$0" "$@" ; exit $? - -//DEPS org.slf4j:slf4j-simple:2.0.6 - -// Junit console to start the test engine: -//DEPS org.junit.platform:junit-platform-console:1.8.2 - -// engine to run the tests (tests are written with Junit5): -//DEPS org.junit.jupiter:junit-jupiter-engine:5.8.2 - -// testcontainers -//DEPS org.testcontainers:testcontainers:1.17.6 -//DEPS org.testcontainers:junit-jupiter:1.17.6 - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.io.IOException; -import java.io.PrintWriter; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.net.http.HttpResponse.BodyHandlers; -import java.nio.file.Paths; -import java.time.Duration; -import java.util.Collections; - -import org.junit.jupiter.api.Test; -import org.junit.platform.console.options.CommandLineOptions; -import org.junit.platform.console.tasks.ConsoleTestExecutor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.containers.output.Slf4jLogConsumer; -import org.testcontainers.containers.wait.strategy.Wait; -import org.testcontainers.images.builder.ImageFromDockerfile; -import org.testcontainers.junit.jupiter.Container; -import org.testcontainers.junit.jupiter.Testcontainers; - -@Testcontainers -public class RunTests { - - private static Logger LOGGER = LoggerFactory.getLogger(RunTests.class); - - private Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(LOGGER); - - @Container - private GenericContainer greetBuiltImage = new GenericContainer( - new ImageFromDockerfile("dev.local/jbang-test/swf-test:" + Math.round(Math.random() * 1000000.00)) - .withDockerfile(Paths.get(getScriptDirPath(), "resources/greet", "Dockerfile")) - .withBuildArg("BUILDER_IMAGE_TAG", getTestImage())) - .withExposedPorts(8080) - .waitingFor(Wait.forHttp("/jsongreet")) - .withLogConsumer(logConsumer); - - @Test - public void testBuiltContainerAnswerCorrectly() throws URISyntaxException, IOException, InterruptedException { - greetBuiltImage.start(); - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("http://" + greetBuiltImage.getHost() + ":" + greetBuiltImage.getFirstMappedPort() + "/jsongreet")) - .header("Content-Type", "application/json") - .header("Accept", "application/json") - .timeout(Duration.ofSeconds(10)) - .POST(HttpRequest.BodyPublishers - .ofString("{\"workflowdata\" : {\"name\": \"John\", \"language\": \"English\"}}")) - .build(); - HttpResponse response = HttpClient.newHttpClient().send(request, BodyHandlers.ofString()); - assertEquals(201, response.statusCode()); - greetBuiltImage.stop(); - } - - @Container - private GenericContainer greetWithInputSchemaBuiltImage = new GenericContainer( - new ImageFromDockerfile("dev.local/jbang-test/swf-test:" + Math.round(Math.random() * 1000000.00)) - .withDockerfile(Paths.get(getScriptDirPath(), "resources/greet-with-inputschema", "Dockerfile")) - .withBuildArg("BUILDER_IMAGE_TAG", getTestImage())) - .withExposedPorts(8080) - .waitingFor(Wait.forHttp("/greeting")) - .withLogConsumer(logConsumer); - - @Test - public void testBuiltContainerWithInputSchemaAnswerCorrectly() throws URISyntaxException, IOException, InterruptedException { - greetWithInputSchemaBuiltImage.start(); - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("http://" + greetWithInputSchemaBuiltImage.getHost() + ":" + greetWithInputSchemaBuiltImage.getFirstMappedPort() + "/greeting")) - .header("Content-Type", "application/json") - .header("Accept", "application/json") - .timeout(Duration.ofSeconds(10)) - .POST(HttpRequest.BodyPublishers - .ofString("{\"name\": \"John\", \"language\": \"English\"}")) - .build(); - HttpResponse response = HttpClient.newHttpClient().send(request, BodyHandlers.ofString()); - assertEquals(201, response.statusCode()); - greetWithInputSchemaBuiltImage.stop(); - } - - public static void main(String... args) throws Exception { - // Log docker build. Source: https://github.com/testcontainers/testcontainers-java/issues/3093 - System.setProperty("org.slf4j.simpleLogger.log.com.github.dockerjava.api.command.BuildImageResultCallback", "debug"); - CommandLineOptions options = new CommandLineOptions(); - options.setSelectedClasses(Collections.singletonList(RunTests.class.getName())); - options.setReportsDir(Paths.get(getOutputDir())); - new ConsoleTestExecutor(options).execute(new PrintWriter(System.out)); - } - - static String getTestImage() { - return System.getenv("TEST_IMAGE"); - } - - static String getOutputDir() { - return System.getenv("OUTPUT_DIR"); - } - - static String getScriptDirPath() { - return System.getenv("TESTS_SCRIPT_DIR_PATH"); - } -} \ No newline at end of file diff --git a/tests/shell/kogito-swf-builder/resources/greet-with-inputschema/Dockerfile b/tests/shell/kogito-swf-builder/resources/greet-with-inputschema/Dockerfile index 3581d31c5..2172c7129 100644 --- a/tests/shell/kogito-swf-builder/resources/greet-with-inputschema/Dockerfile +++ b/tests/shell/kogito-swf-builder/resources/greet-with-inputschema/Dockerfile @@ -19,7 +19,7 @@ RUN "${KOGITO_HOME}"/launch/build-app.sh './resources' #============================= # Runtime Run #============================= -FROM registry.access.redhat.com/ubi8/openjdk-11-runtime:latest +FROM registry.access.redhat.com/ubi8/openjdk-17-runtime:latest ARG QUARKUS_LAUNCH_DEVMODE=false diff --git a/tests/shell/kogito-swf-builder/resources/greet/Dockerfile b/tests/shell/kogito-swf-builder/resources/greet/Dockerfile index 3581d31c5..2172c7129 100644 --- a/tests/shell/kogito-swf-builder/resources/greet/Dockerfile +++ b/tests/shell/kogito-swf-builder/resources/greet/Dockerfile @@ -19,7 +19,7 @@ RUN "${KOGITO_HOME}"/launch/build-app.sh './resources' #============================= # Runtime Run #============================= -FROM registry.access.redhat.com/ubi8/openjdk-11-runtime:latest +FROM registry.access.redhat.com/ubi8/openjdk-17-runtime:latest ARG QUARKUS_LAUNCH_DEVMODE=false diff --git a/tests/shell/kogito-swf-builder/src/RunTests.java b/tests/shell/kogito-swf-builder/src/RunTests.java new file mode 100644 index 000000000..d3bc70a9d --- /dev/null +++ b/tests/shell/kogito-swf-builder/src/RunTests.java @@ -0,0 +1,121 @@ +///usr/bin/env jbang "$0" "$@" ; exit $? + +//DEPS org.slf4j:slf4j-simple:2.0.9 + +// Junit console to start the test engine: +//DEPS org.junit.platform:junit-platform-console:1.10.1 + +// engine to run the tests (tests are written with Junit5): +//DEPS org.junit.jupiter:junit-jupiter-engine:5.10.1 + +// testcontainers +//DEPS org.testcontainers:testcontainers:1.19.3 +//DEPS org.testcontainers:junit-jupiter:1.19.3 + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.net.http.HttpResponse.BodyHandlers; +import java.nio.file.Paths; +import java.time.Duration; +import org.junit.jupiter.api.Test; +import org.junit.platform.console.ConsoleLauncher; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.output.Slf4jLogConsumer; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.images.builder.ImageFromDockerfile; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +@Testcontainers +public class RunTests { + + private static Logger LOGGER = LoggerFactory.getLogger(RunTests.class); + + private Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(LOGGER); + + @Container + private GenericContainer greetBuiltImage = new GenericContainer( + new ImageFromDockerfile( + "dev.local/jbang-test/swf-test:" + Math.round(Math.random() * 1000000.00)) + .withDockerfile(Paths.get(getScriptDirPath(), "resources/greet", "Dockerfile")) + .withBuildArg("BUILDER_IMAGE_TAG", getTestImage())) + .withExposedPorts(8080) + .waitingFor(Wait.forHttp("/jsongreet")) + .withLogConsumer(logConsumer); + + @Test + public void testBuiltContainerAnswerCorrectly() throws URISyntaxException, IOException, InterruptedException { + greetBuiltImage.start(); + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("http://" + greetBuiltImage.getHost() + ":" + + greetBuiltImage.getFirstMappedPort() + "/jsongreet")) + .header("Content-Type", "application/json") + .header("Accept", "application/json") + .timeout(Duration.ofSeconds(10)) + .POST(HttpRequest.BodyPublishers + .ofString("{\"workflowdata\" : {\"name\": \"John\", \"language\": \"English\"}}")) + .build(); + HttpResponse response = HttpClient.newHttpClient().send(request, BodyHandlers.ofString()); + assertEquals(201, response.statusCode()); + greetBuiltImage.stop(); + } + + @Container + private GenericContainer greetWithInputSchemaBuiltImage = new GenericContainer( + new ImageFromDockerfile( + "dev.local/jbang-test/swf-test:" + Math.round(Math.random() * 1000000.00)) + .withDockerfile(Paths.get(getScriptDirPath(), + "resources/greet-with-inputschema", "Dockerfile")) + .withBuildArg("BUILDER_IMAGE_TAG", getTestImage())) + .withExposedPorts(8080) + .waitingFor(Wait.forHttp("/greeting")) + .withLogConsumer(logConsumer); + + @Test + public void testBuiltContainerWithInputSchemaAnswerCorrectly() + throws URISyntaxException, IOException, InterruptedException { + greetWithInputSchemaBuiltImage.start(); + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("http://" + greetWithInputSchemaBuiltImage.getHost() + ":" + + greetWithInputSchemaBuiltImage.getFirstMappedPort() + "/greeting")) + .header("Content-Type", "application/json") + .header("Accept", "application/json") + .timeout(Duration.ofSeconds(10)) + .POST(HttpRequest.BodyPublishers + .ofString("{\"name\": \"John\", \"language\": \"English\"}")) + .build(); + HttpResponse response = HttpClient.newHttpClient().send(request, BodyHandlers.ofString()); + assertEquals(201, response.statusCode()); + greetWithInputSchemaBuiltImage.stop(); + } + + public static void main(String... args) throws Exception { + // Log docker build. Source: + // https://github.com/testcontainers/testcontainers-java/issues/3093 + System.setProperty( + "org.slf4j.simpleLogger.log.com.github.dockerjava.api.command.BuildImageResultCallback", + "debug"); + ConsoleLauncher.main("--select-class=" + RunTests.class.getName(), + "--reports-dir=" + Paths.get(getOutputDir()).toString()); + } + + static String getTestImage() { + return System.getenv("TEST_IMAGE"); + } + + static String getOutputDir() { + return System.getenv("OUTPUT_DIR"); + } + + static String getScriptDirPath() { + return System.getenv("TESTS_SCRIPT_DIR_PATH"); + } +} \ No newline at end of file diff --git a/tests/shell/kogito-swf-devmode/.vscode/launch.json b/tests/shell/kogito-swf-devmode/.vscode/launch.json new file mode 100644 index 000000000..f329b9513 --- /dev/null +++ b/tests/shell/kogito-swf-devmode/.vscode/launch.json @@ -0,0 +1,22 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "RunTests-port-4004", + "request": "attach", + "hostName": "localhost", + "port": 4004 + }, + { + "type": "java", + "name": "Debug (Launch) - RunTests", + "request": "launch", + "mainClass": "RunTests", + "projectName": "RunTests" + } + ] +} diff --git a/tests/shell/kogito-swf-devmode/.vscode/settings.json b/tests/shell/kogito-swf-devmode/.vscode/settings.json new file mode 100644 index 000000000..ca7dd1980 --- /dev/null +++ b/tests/shell/kogito-swf-devmode/.vscode/settings.json @@ -0,0 +1,18 @@ +{ + "java.import.gradle.enabled": false, + "java.import.maven.enabled": false, + "java.eclipse.downloadSources": true, + "files.exclude": { + "bin/": true, + ".eclipse/": true, + ".project": true, + ".classpath": true, + "build.gradle": true + }, + "java.completion.importOrder": [ + "", + "javax", + "java", + "#" + ] +} \ No newline at end of file diff --git a/tests/shell/kogito-swf-devmode/RunTests.java b/tests/shell/kogito-swf-devmode/src/RunTests.java similarity index 71% rename from tests/shell/kogito-swf-devmode/RunTests.java rename to tests/shell/kogito-swf-devmode/src/RunTests.java index c3cb89db3..9971e8229 100644 --- a/tests/shell/kogito-swf-devmode/RunTests.java +++ b/tests/shell/kogito-swf-devmode/src/RunTests.java @@ -1,21 +1,20 @@ ///usr/bin/env jbang "$0" "$@" ; exit $? -//DEPS org.slf4j:slf4j-simple:2.0.6 +//DEPS org.slf4j:slf4j-simple:2.0.9 // Junit console to start the test engine: -//DEPS org.junit.platform:junit-platform-console:1.8.2 +//DEPS org.junit.platform:junit-platform-console:1.10.1 // engine to run the tests (tests are written with Junit5): -//DEPS org.junit.jupiter:junit-jupiter-engine:5.8.2 +//DEPS org.junit.jupiter:junit-jupiter-engine:5.10.1 // testcontainers -//DEPS org.testcontainers:testcontainers:1.17.6 -//DEPS org.testcontainers:junit-jupiter:1.17.6 +//DEPS org.testcontainers:testcontainers:1.19.3 +//DEPS org.testcontainers:junit-jupiter:1.19.3 import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; -import java.io.PrintWriter; import java.net.URI; import java.net.URISyntaxException; import java.net.http.HttpClient; @@ -24,18 +23,14 @@ import java.net.http.HttpResponse.BodyHandlers; import java.nio.file.Paths; import java.time.Duration; -import java.util.Collections; - import org.junit.jupiter.api.Test; -import org.junit.platform.console.options.CommandLineOptions; -import org.junit.platform.console.tasks.ConsoleTestExecutor; +import org.junit.platform.console.ConsoleLauncher; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.testcontainers.containers.BindMode; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.output.Slf4jLogConsumer; import org.testcontainers.containers.wait.strategy.Wait; -import org.testcontainers.images.builder.ImageFromDockerfile; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; @@ -48,16 +43,20 @@ public class RunTests { @Container private GenericContainer devModeImage = new GenericContainer(getTestImage()) - .withFileSystemBind(getScriptDirPath() + "/resources", "/home/kogito/serverless-workflow-project/src/main/resources", BindMode.READ_ONLY) + .withEnv("MAVEN_ARGS_APPEND", "-Ddebug=false -Dquarkus.devservices.enabled=false") + .withFileSystemBind(getScriptDirPath() + "/resources", + "/home/kogito/serverless-workflow-project/src/main/resources", BindMode.READ_ONLY) .withExposedPorts(8080) .waitingFor(Wait.forHttp("/jsongreet")) + .withStartupTimeout(Duration.ofMinutes(2)) .withLogConsumer(logConsumer); @Test public void testBuiltContainerAnswerCorrectly() throws URISyntaxException, IOException, InterruptedException { devModeImage.start(); HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("http://" + devModeImage.getHost() + ":" + devModeImage.getFirstMappedPort() + "/jsongreet")) + .uri(new URI( + "http://" + devModeImage.getHost() + ":" + devModeImage.getFirstMappedPort() + "/jsongreet")) .header("Content-Type", "application/json") .header("Accept", "application/json") .timeout(Duration.ofSeconds(10)) @@ -69,10 +68,8 @@ public void testBuiltContainerAnswerCorrectly() throws URISyntaxException, IOExc } public static void main(String... args) throws Exception { - CommandLineOptions options = new CommandLineOptions(); - options.setSelectedClasses(Collections.singletonList(RunTests.class.getName())); - options.setReportsDir(Paths.get(getOutputDir())); - new ConsoleTestExecutor(options).execute(new PrintWriter(System.out)); + ConsoleLauncher.main("--select-class=" + RunTests.class.getName(), + "--reports-dir=" + Paths.get(getOutputDir()).toString()); } static String getTestImage() { diff --git a/tests/shell/run.sh b/tests/shell/run.sh index 890543958..f61b18e42 100755 --- a/tests/shell/run.sh +++ b/tests/shell/run.sh @@ -26,7 +26,7 @@ echo "OUTPUT_DIR=${OUTPUT_DIR}" echo "TESTS_SCRIPT_DIR_PATH=${TESTS_SCRIPT_DIR_PATH}" if [ -d "${script_dir_path}/${image_name}" ]; then - curl -Ls https://sh.jbang.dev | bash -s - "${TESTS_SCRIPT_DIR_PATH}/RunTests.java" + curl -Ls https://sh.jbang.dev | bash -s - "${TESTS_SCRIPT_DIR_PATH}/src/RunTests.java" else echo "No shell test to run for image ${image_name}" fi \ No newline at end of file