Skip to content

Commit

Permalink
Make SonataFlow images deps available offline, fix jbang setup and deps
Browse files Browse the repository at this point in the history
Signed-off-by: Ricardo Zanini <[email protected]>
  • Loading branch information
ricardozanini committed Dec 26, 2023
1 parent 5621502 commit 4513faf
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 141 deletions.
8 changes: 5 additions & 3 deletions modules/kogito-swf/common/scripts/added/create-app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 21 additions & 0 deletions tests/shell/README.md
Original file line number Diff line number Diff line change
@@ -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.
22 changes: 22 additions & 0 deletions tests/shell/kogito-swf-builder/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
18 changes: 18 additions & 0 deletions tests/shell/kogito-swf-builder/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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",
"#"
]
}
118 changes: 0 additions & 118 deletions tests/shell/kogito-swf-builder/RunTests.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/shell/kogito-swf-builder/resources/greet/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
121 changes: 121 additions & 0 deletions tests/shell/kogito-swf-builder/src/RunTests.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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");
}
}
22 changes: 22 additions & 0 deletions tests/shell/kogito-swf-devmode/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
18 changes: 18 additions & 0 deletions tests/shell/kogito-swf-devmode/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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",
"#"
]
}
Loading

0 comments on commit 4513faf

Please sign in to comment.