From 03a2e0c172aa27fcd0bdc0e4c79d019c343a31f8 Mon Sep 17 00:00:00 2001 From: Fedor Dudinsky Date: Fri, 8 Mar 2024 17:35:44 +0100 Subject: [PATCH] Cover custom init containers https://issues.redhat.com/browse/QQE-507 --- README.md | 2 + sql-db/panache-flyway/pom.xml | 11 +++ .../sqldb/panacheflyway/init/CustomYaml.java | 24 ++++++ .../init/OpenShiftDefaultInitContainerIT.java | 74 ++++++++++++++++++ .../init/OpenShiftFlywayInitContainerIT.java | 77 +++++++++++++++++++ .../src/test/resources/Dockerfile | 16 ++++ 6 files changed, 204 insertions(+) create mode 100644 sql-db/panache-flyway/src/test/java/io/quarkus/ts/sqldb/panacheflyway/init/CustomYaml.java create mode 100644 sql-db/panache-flyway/src/test/java/io/quarkus/ts/sqldb/panacheflyway/init/OpenShiftDefaultInitContainerIT.java create mode 100644 sql-db/panache-flyway/src/test/java/io/quarkus/ts/sqldb/panacheflyway/init/OpenShiftFlywayInitContainerIT.java create mode 100755 sql-db/panache-flyway/src/test/resources/Dockerfile diff --git a/README.md b/README.md index e2cf2bd96..51a9bc033 100644 --- a/README.md +++ b/README.md @@ -582,6 +582,8 @@ PanacheRepository is a facade class. Facade class can override certain methods t PanacheRepositoryResource methods. - AgroalPoolTest, will cover how the db pool is managed in terms of IDLE-timeout, max connections and concurrency. +- InitContainer tests, which cover, whether we can use custom init containers + - Dockerfile for custom init container can be found inside resources folder ### `sql-db/reactive-rest-data-panache` diff --git a/sql-db/panache-flyway/pom.xml b/sql-db/panache-flyway/pom.xml index 2f993ff58..64597d7a9 100644 --- a/sql-db/panache-flyway/pom.xml +++ b/sql-db/panache-flyway/pom.xml @@ -50,4 +50,15 @@ test + + + deploy-to-openshift-using-extension + + + io.quarkus + quarkus-openshift + + + + diff --git a/sql-db/panache-flyway/src/test/java/io/quarkus/ts/sqldb/panacheflyway/init/CustomYaml.java b/sql-db/panache-flyway/src/test/java/io/quarkus/ts/sqldb/panacheflyway/init/CustomYaml.java new file mode 100644 index 000000000..d8d53ee86 --- /dev/null +++ b/sql-db/panache-flyway/src/test/java/io/quarkus/ts/sqldb/panacheflyway/init/CustomYaml.java @@ -0,0 +1,24 @@ +package io.quarkus.ts.sqldb.panacheflyway.init; + +import java.util.ArrayList; +import java.util.Map; + +class CustomYaml { + private final Map content; + + CustomYaml(Object content) { + this.content = (Map) content; + } + + CustomYaml get(String name) { + return new CustomYaml(this.content.get(name)); + } + + String getValue(String name) { + return (String) this.content.get(name); + } + + public CustomYaml getFromArray(String name, int i) { + return new CustomYaml(((ArrayList) this.content.get(name)).get(i)); + } +} diff --git a/sql-db/panache-flyway/src/test/java/io/quarkus/ts/sqldb/panacheflyway/init/OpenShiftDefaultInitContainerIT.java b/sql-db/panache-flyway/src/test/java/io/quarkus/ts/sqldb/panacheflyway/init/OpenShiftDefaultInitContainerIT.java new file mode 100644 index 000000000..5b060ecd4 --- /dev/null +++ b/sql-db/panache-flyway/src/test/java/io/quarkus/ts/sqldb/panacheflyway/init/OpenShiftDefaultInitContainerIT.java @@ -0,0 +1,74 @@ +package io.quarkus.ts.sqldb.panacheflyway.init; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.apache.http.HttpStatus; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.yaml.snakeyaml.Yaml; + +import io.quarkus.test.bootstrap.MySqlService; +import io.quarkus.test.bootstrap.RestService; +import io.quarkus.test.scenarios.OpenShiftDeploymentStrategy; +import io.quarkus.test.scenarios.OpenShiftScenario; +import io.quarkus.test.services.Container; +import io.quarkus.test.services.QuarkusApplication; + +@OpenShiftScenario(deployment = OpenShiftDeploymentStrategy.UsingOpenShiftExtension) +public class OpenShiftDefaultInitContainerIT { + + private final Path openShiftYaml = Paths.get("target/", this.getClass().getSimpleName(), + "app/target/kubernetes/openshift.yml"); + private static final String CUSTOM_IMAGE = "quay.io/quarkusqeteam/wait:0.0.2"; + + @Container(image = "${mysql.80.image}", port = 3306, expectedLog = "Only MySQL server logs after this point") + static MySqlService database = new MySqlService(); + + @QuarkusApplication + static RestService app = new RestService() + .withProperty("quarkus.datasource.username", database.getUser()) + .withProperty("quarkus.datasource.password", database.getPassword()) + .withProperty("quarkus.datasource.jdbc.url", database::getJdbcUrl) + .withProperty("quarkus.openshift.init-task-defaults.wait-for-container.image", CUSTOM_IMAGE) + .withProperty("quarkus.flyway.schemas", database.getDatabase()); + + @Test + @Disabled + void migrated() { + String userList = app.given() + .accept("application/hal+json") + .when().get("/users/all?sort=name") + .then() + .statusCode(HttpStatus.SC_OK) + .extract().response().jsonPath().getString("_embedded.user_list.name"); + assertEquals("[Alaba, Balaba]", userList); + } + + @Test + void yaml() { + try (InputStream content = Files.newInputStream(openShiftYaml)) { + Iterable objects = new Yaml().loadAll(content); + String image = null; + for (Object object : objects) { + CustomYaml source = new CustomYaml(object); + if ("Deployment".equals(source.getValue("kind"))) { + image = source.get("spec") + .get("template") + .get("spec") + .getFromArray("initContainers", 0) + .getValue("image"); + } + } + Assertions.assertEquals(CUSTOM_IMAGE, image); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/sql-db/panache-flyway/src/test/java/io/quarkus/ts/sqldb/panacheflyway/init/OpenShiftFlywayInitContainerIT.java b/sql-db/panache-flyway/src/test/java/io/quarkus/ts/sqldb/panacheflyway/init/OpenShiftFlywayInitContainerIT.java new file mode 100644 index 000000000..2fa7907dd --- /dev/null +++ b/sql-db/panache-flyway/src/test/java/io/quarkus/ts/sqldb/panacheflyway/init/OpenShiftFlywayInitContainerIT.java @@ -0,0 +1,77 @@ +package io.quarkus.ts.sqldb.panacheflyway.init; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.apache.http.HttpStatus; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.yaml.snakeyaml.Yaml; + +import io.quarkus.test.bootstrap.MySqlService; +import io.quarkus.test.bootstrap.RestService; +import io.quarkus.test.scenarios.OpenShiftDeploymentStrategy; +import io.quarkus.test.scenarios.OpenShiftScenario; +import io.quarkus.test.services.Container; +import io.quarkus.test.services.QuarkusApplication; + +@OpenShiftScenario(deployment = OpenShiftDeploymentStrategy.UsingOpenShiftExtension) +@Disabled("https://github.com/quarkusio/quarkus/issues/39230") +public class OpenShiftFlywayInitContainerIT { + + private final Path openShiftYaml = Paths.get("target/", this.getClass().getSimpleName(), + "app/target/kubernetes/openshift.yml"); + private static final String CUSTOM_IMAGE = "quay.io/quarkusqeteam/wait"; + + @Container(image = "${mysql.80.image}", port = 3306, expectedLog = "Only MySQL server logs after this point") + static MySqlService database = new MySqlService(); + + @QuarkusApplication + static RestService app = new RestService() + .withProperty("quarkus.datasource.username", database.getUser()) + .withProperty("quarkus.datasource.password", database.getPassword()) + .withProperty("quarkus.datasource.jdbc.url", database::getJdbcUrl) + .withProperty("quarkus.openshift.init-containers.wait-for-flyway.image", CUSTOM_IMAGE) + .withProperty("quarkus.openshift.init-containers.wait-for-flyway.image-pull-policy", "Always") + .withProperty("quarkus.flyway.schemas", database.getDatabase()); + + @Test + @Disabled + void migrated() { + String userList = app.given() + .accept("application/hal+json") + .when().get("/users/all?sort=name") + .then() + .statusCode(HttpStatus.SC_OK) + .extract().response().jsonPath().getString("_embedded.user_list.name"); + assertEquals("[Alaba, Balaba]", userList); + } + + @Test + void yaml() { + try (InputStream content = Files.newInputStream(openShiftYaml)) { + Iterable objects = new Yaml().loadAll(content); + CustomYaml initContainer = null; + for (Object object : objects) { + CustomYaml source = new CustomYaml(object); + if ("Deployment".equals(source.getValue("kind"))) { + initContainer = source.get("spec") + .get("template") + .get("spec") + .getFromArray("initContainers", 0); + } + } + Assertions.assertNotNull(initContainer); + Assertions.assertEquals(CUSTOM_IMAGE, initContainer.getValue("image")); + Assertions.assertEquals("Always", initContainer.getValue("imagePullPolicy")); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/sql-db/panache-flyway/src/test/resources/Dockerfile b/sql-db/panache-flyway/src/test/resources/Dockerfile new file mode 100755 index 000000000..81bcd2076 --- /dev/null +++ b/sql-db/panache-flyway/src/test/resources/Dockerfile @@ -0,0 +1,16 @@ +#!/usr/bin/env -S podman build . --tag quay.io/quarkusqeteam/wait:0.0.2 --file +FROM ubi8:latest + +COPY <