From f5aa66b2b3db013205ac9a0bd394405a73983bbc Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Wed, 4 Aug 2021 08:22:18 +0300 Subject: [PATCH] Add integration test for Mongo Service Binding --- .../mongodb-rest-data-panache/pom.xml | 69 +++++++++++++++ .../panache/MongoDbRestDataPanacheTest.java | 2 + .../rest/data/panache/MongoTestResource.java | 84 +++++++++++++++++++ .../src/test/resources/k8s-sb/mongo/database | 1 + .../src/test/resources/k8s-sb/mongo/host | 1 + .../src/test/resources/k8s-sb/mongo/port | 1 + .../src/test/resources/k8s-sb/mongo/provider | 1 + .../src/test/resources/k8s-sb/mongo/type | 1 + 8 files changed, 160 insertions(+) create mode 100644 integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoTestResource.java create mode 100644 integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/database create mode 100644 integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/host create mode 100644 integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/port create mode 100644 integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/provider create mode 100644 integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/type diff --git a/integration-tests/mongodb-rest-data-panache/pom.xml b/integration-tests/mongodb-rest-data-panache/pom.xml index 5db7bba03d433..089a5ffe772bd 100644 --- a/integration-tests/mongodb-rest-data-panache/pom.xml +++ b/integration-tests/mongodb-rest-data-panache/pom.xml @@ -21,6 +21,11 @@ io.quarkus quarkus-resteasy-jackson + + + io.quarkus + quarkus-kubernetes-service-binding + io.quarkus quarkus-junit5 @@ -69,6 +74,19 @@ + + io.quarkus + quarkus-kubernetes-service-binding-deployment + ${project.version} + pom + test + + + * + * + + + @@ -84,6 +102,57 @@ + + maven-surefire-plugin + + + + ${project.basedir}/src/test/resources/k8s-sb + + + + + + + + native + + + native + + + + + + maven-failsafe-plugin + + + + integration-test + verify + + + + + ${project.build.directory}/${project.build.finalName}-runner + + + + ${project.basedir}/src/test/resources/k8s-sb + + + + + + + + + + + native + + + diff --git a/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoDbRestDataPanacheTest.java b/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoDbRestDataPanacheTest.java index 05417efbf3b55..8fcbb28da9270 100644 --- a/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoDbRestDataPanacheTest.java +++ b/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoDbRestDataPanacheTest.java @@ -18,11 +18,13 @@ import org.junit.jupiter.api.condition.DisabledOnOs; import org.junit.jupiter.api.condition.OS; +import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.junit.QuarkusTest; import io.restassured.response.Response; @QuarkusTest @DisabledOnOs(OS.WINDOWS) +@QuarkusTestResource(MongoTestResource.class) class MongoDbRestDataPanacheTest { private Author dostoevsky; diff --git a/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoTestResource.java b/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoTestResource.java new file mode 100644 index 0000000000000..f086d7a74df25 --- /dev/null +++ b/integration-tests/mongodb-rest-data-panache/src/test/java/io/quarkus/it/mongodb/rest/data/panache/MongoTestResource.java @@ -0,0 +1,84 @@ +package io.quarkus.it.mongodb.rest.data.panache; + +import java.util.Collections; +import java.util.Map; + +import org.jboss.logging.Logger; + +import de.flapdoodle.embed.mongo.MongodExecutable; +import de.flapdoodle.embed.mongo.MongodStarter; +import de.flapdoodle.embed.mongo.config.IMongodConfig; +import de.flapdoodle.embed.mongo.config.MongodConfigBuilder; +import de.flapdoodle.embed.mongo.config.Net; +import de.flapdoodle.embed.mongo.distribution.Version; +import de.flapdoodle.embed.process.runtime.Network; +import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; + +public class MongoTestResource implements QuarkusTestResourceLifecycleManager { + + private static final Logger LOGGER = Logger.getLogger(MongoTestResource.class); + private static MongodExecutable MONGO; + + @Override + public Map start() { + try { + //JDK bug workaround + //https://github.com/quarkusio/quarkus/issues/14424 + //force class init to prevent possible deadlock when done by mongo threads + Class.forName("sun.net.ext.ExtendedSocketOptions", true, ClassLoader.getSystemClassLoader()); + } catch (ClassNotFoundException e) { + } + try { + Version.Main version = Version.Main.V4_0; + int port = 37017; + LOGGER.infof("Starting Mongo %s on port %s", version, port); + IMongodConfig config = new MongodConfigBuilder() + .version(version) + .net(new Net(port, Network.localhostIsIPv6())) + .build(); + MONGO = getMongodExecutable(config); + try { + MONGO.start(); + } catch (Exception e) { + //every so often mongo fails to start on CI runs + //see if this helps + Thread.sleep(1000); + MONGO.start(); + } + + // don't return any properties, since we want Service Binding to configure Quarkus + return Collections.emptyMap(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private MongodExecutable getMongodExecutable(IMongodConfig config) { + try { + return doGetExecutable(config); + } catch (Exception e) { + // sometimes the download process can timeout so just sleep and try again + try { + Thread.sleep(1000); + } catch (InterruptedException ignored) { + + } + return doGetExecutable(config); + } + } + + private MongodExecutable doGetExecutable(IMongodConfig config) { + return MongodStarter.getDefaultInstance().prepare(config); + } + + private void initData() { + + } + + @Override + public void stop() { + if (MONGO != null) { + MONGO.stop(); + } + } +} diff --git a/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/database b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/database new file mode 100644 index 0000000000000..9daeafb9864cf --- /dev/null +++ b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/database @@ -0,0 +1 @@ +test diff --git a/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/host b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/host new file mode 100644 index 0000000000000..2fbb50c4a8dc7 --- /dev/null +++ b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/host @@ -0,0 +1 @@ +localhost diff --git a/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/port b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/port new file mode 100644 index 0000000000000..72ae75d500d06 --- /dev/null +++ b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/port @@ -0,0 +1 @@ +37017 diff --git a/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/provider b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/provider new file mode 100644 index 0000000000000..98c8f47051d4b --- /dev/null +++ b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/provider @@ -0,0 +1 @@ +atlas \ No newline at end of file diff --git a/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/type b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/type new file mode 100644 index 0000000000000..e73baecce656b --- /dev/null +++ b/integration-tests/mongodb-rest-data-panache/src/test/resources/k8s-sb/mongo/type @@ -0,0 +1 @@ +mongodb \ No newline at end of file