Skip to content

Commit

Permalink
#22706 Some tests using testcontainers.
Browse files Browse the repository at this point in the history
  • Loading branch information
dcolina committed Jul 10, 2023
1 parent d355b59 commit 74b0094
Show file tree
Hide file tree
Showing 6 changed files with 424 additions and 5 deletions.
57 changes: 52 additions & 5 deletions tools/dotcms-cli/api-data-model/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dotCMS-cli</artifactId>
<groupId>com.dotcms</groupId>
Expand All @@ -22,6 +22,9 @@
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
<quarkus.platform.version>2.9.1.Final</quarkus.platform.version>
<surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
<junit-jupiter.version>5.9.3</junit-jupiter.version>
<testcontainers.version>1.17.6</testcontainers.version>
<!-- <docker-java.version>3.3.0</docker-java.version>-->
</properties>
<dependencyManagement>
<dependencies>
Expand All @@ -32,6 +35,8 @@
<type>pom</type>
<scope>import</scope>
</dependency>


</dependencies>
</dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -75,6 +80,44 @@
<artifactId>jackson-datatype-guava</artifactId>
<version>2.13.3</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>net.java.dev.jna</groupId>-->
<!-- <artifactId>jna</artifactId>-->
<!-- <version>5.7.0</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.junit.jupiter</groupId>-->
<!-- <artifactId>junit-jupiter</artifactId>-->
<!-- <version>${junit-jupiter.version}</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>elasticsearch</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-openapi</artifactId>
Expand Down Expand Up @@ -128,7 +171,8 @@
<version>${surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<java.util.logging.manager>org.jboss.logmanager.LogManager
</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
Expand Down Expand Up @@ -157,8 +201,11 @@
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<native.image.path>
${project.build.directory}/${project.build.finalName}-runner
</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager
</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//package com.dotcms;
//
//import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
//import org.testcontainers.containers.DockerComposeContainer;
//import org.testcontainers.containers.wait.strategy.Wait;
//
//import java.io.File;
//import java.time.Duration;
//import java.util.Collections;
//import java.util.Map;
//
//public class DockerComposeResource implements QuarkusTestResourceLifecycleManager {
//
// private static final int POSTGRES_SERVICE_PORT = 5432;
// private static final int ELASTICSEARCH_SERVICE_PORT = 9200;
// private static final int DOTCMS_SERVICE_PORT = 8080;
// private static final int STARTUP_TIMEOUT = 120;
// private static final boolean USE_LOCAL_COMPOSE = false;
//
// private static final DockerComposeContainer<?> DOCKER_COMPOSE_CONTAINER =
// new DockerComposeContainer("dotcms-env", new File("src/test/resources/docker-compose.yaml"))
// .withExposedService("postgres", POSTGRES_SERVICE_PORT, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(STARTUP_TIMEOUT)))
// .withExposedService("elasticsearch", ELASTICSEARCH_SERVICE_PORT, Wait.forHttp("/").forPort(ELASTICSEARCH_SERVICE_PORT).forStatusCode(200))
// .withExposedService("dotcms", DOTCMS_SERVICE_PORT, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(STARTUP_TIMEOUT)))
// .withLocalCompose(USE_LOCAL_COMPOSE); // need to be false to run on macOS
// @Override
// public Map<String, String> start() {
// DOCKER_COMPOSE_CONTAINER.start();
// String dotcmsServiceHost = DOCKER_COMPOSE_CONTAINER.getServiceHost("dotcms", DOTCMS_SERVICE_PORT);
// String dotcmsServicePort = DOCKER_COMPOSE_CONTAINER.getServicePort("dotcms", DOTCMS_SERVICE_PORT).toString();
//
// return Collections.singletonMap("myapp.service.url", "http://" + dotcmsServiceHost + ":" + dotcmsServicePort);
// }
//
// @Override
// public void stop() {
// DOCKER_COMPOSE_CONTAINER.stop();
// }
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//package com.dotcms;
//
//import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
//import org.testcontainers.containers.DockerComposeContainer;
//import org.testcontainers.containers.wait.strategy.Wait;
//import org.testcontainers.lifecycle.Startables;
//
//import java.io.File;
//import java.time.Duration;
//import java.util.Collections;
//import java.util.Map;
//import java.util.stream.Stream;
//
//public class DockerComposeTestResource implements QuarkusTestResourceLifecycleManager {
//
// private static final DockerComposeContainer<?> composeContainer = new DockerComposeContainer<>(new File("src/test/resources/docker-compose.yaml"))
// .withLocalCompose(true)
//
// .withServices("postgres");
//
// static {
// Startables.deepStart(Stream.of(composeContainer)).join();
// }
// @Override
// public Map<String, String> start() {
// return Map.of(
// "quarkus.datasource.jdbc.url", composeContainer.getServiceHost("postgres", 5432)
// + ":" + composeContainer.getServicePort("postgres", 5432),
// "quarkus.datasource.username", "your_username",
// "quarkus.datasource.password", "your_password"
// );
// }
//
// @Override
// public void stop() {
// // No explicit stop needed as Testcontainers manages the lifecycle
// }
//
//
//// private static final int SERVICE_PORT = 5432;
//// private static final int STARTUP_TIMEOUT = 60;
//// private static final DockerComposeContainer<?> DOCKER_COMPOSE_CONTAINER =
//// new DockerComposeContainer<>(new File("src/test/resources/docker-compose.yaml"))
//// .withExposedService(
//// "dotcms-env",
//// SERVICE_PORT,
//// Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(STARTUP_TIMEOUT)))
//// .withLocalCompose(true);
////
//// @Override
//// public Map<String, String> start() {
//// DOCKER_COMPOSE_CONTAINER.start();
//// String dotcmsServiceHost = DOCKER_COMPOSE_CONTAINER.getServiceHost("dotcms-env", SERVICE_PORT);
//// String dotcmsServicePort = DOCKER_COMPOSE_CONTAINER.getServicePort("dotcms-env", SERVICE_PORT).toString();
////
//// return Collections.singletonMap("dotcms.service.url", "http://" + dotcmsServiceHost + ":" + dotcmsServicePort);
//// }
////
//// @Override
//// public void stop() {
//// DOCKER_COMPOSE_CONTAINER.stop();
//// }
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.dotcms;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.DockerComposeContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.io.File;
import java.time.Duration;

@Testcontainers
public class DummyTest {

private static final int POSTGRES_SERVICE_PORT = 5432;
private static final int ELASTICSEARCH_SERVICE_PORT = 9200;
private static final int DOTCMS_SERVICE_PORT = 8080;
private static final int STARTUP_TIMEOUT = 120;

private static final DockerComposeContainer<?> COMPOSE_CONTAINER =
new DockerComposeContainer("dotcms-env", new File("src/test/resources/docker-compose.yaml"))
.withExposedService("postgres", POSTGRES_SERVICE_PORT, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(STARTUP_TIMEOUT)))
.withExposedService("elasticsearch", ELASTICSEARCH_SERVICE_PORT, Wait.forHttp("/").forPort(ELASTICSEARCH_SERVICE_PORT).forStatusCode(200))
.withExposedService("dotcms", DOTCMS_SERVICE_PORT, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(STARTUP_TIMEOUT)))
.withLocalCompose(false); // need to be false to run on macOS

static {
COMPOSE_CONTAINER.start();
// Stream.of(GENERIC_CONTAINER, COMPOSE_CONTAINER).parallel().forEach(Startable::start);
// Startables.deepStart(Stream.of(GENERIC_CONTAINER, COMPOSE_CONTAINER)).join();
}

@BeforeAll
public static void beforeAll() {
System.out.println("beforeAll");

System.out.println("Postgres address: " + COMPOSE_CONTAINER.getServiceHost("postgres", POSTGRES_SERVICE_PORT));
System.out.println("Postgres port: " + COMPOSE_CONTAINER.getServicePort("postgres", POSTGRES_SERVICE_PORT));

System.out.println("Elasticsearch address: " + COMPOSE_CONTAINER.getServiceHost("elasticsearch", ELASTICSEARCH_SERVICE_PORT));
System.out.println("Elasticsearch port: " + COMPOSE_CONTAINER.getServicePort("elasticsearch", ELASTICSEARCH_SERVICE_PORT));

System.out.println("DotCMS address: " + COMPOSE_CONTAINER.getServiceHost("dotcms", DOTCMS_SERVICE_PORT));
System.out.println("DotCMS port: " + COMPOSE_CONTAINER.getServicePort("dotcms", DOTCMS_SERVICE_PORT));

}

@Test
public void myTest() {
// Your test logic here
System.out.println("Hello World");
}


// private static final Network NETWORK = Network.newNetwork();
//
// @Container
// private static final ElasticsearchContainer ELASTICSEARCH_CONTAINER =
// new ElasticsearchContainer(DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.10.2"))
// .withEnv("discovery.type", "single-node")
// .withEnv("cluster.name", "elastic-cluster")
// .withEnv("bootstrap.memory_lock", "true")
// .withEnv("ES_JAVA_OPTS", "-Xms512m -Xmx1G")
// .withExposedPorts(ELASTICSEARCH_SERVICE_PORT, 9600)
// .waitingFor(Wait.forHttp("/").forPort(ELASTICSEARCH_SERVICE_PORT).forStatusCode(200))
// .withNetwork(NETWORK)
// .withLogConsumer(new Slf4jLogConsumer(org.slf4j.LoggerFactory.getLogger("elasticsearch")))
// .withReuse(true);





// @Container
// public static final DockerComposeContainer<?> dotCmsEnv = new DockerComposeContainer(new File("src/test/resources/docker-compose.yaml"))
// .withExposedService("dotcms", POSTGRES_SERVICE_PORT, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(STARTUP_TIMEOUT)))
// .withLocalCompose(true);

// @Container
// private static final DockerComposeContainer<?> dotCmsEnv = new DockerComposeContainer(new File("src/test/resources/docker-compose.yaml"))
// .withExposedService("dotcms", 8080, Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(60)))
// .withLocalCompose(true);

// @Container
// private static final PostgreSQLContainer<?> POSTGRES_CONTAINER =
// new PostgreSQLContainer<>(DockerImageName.parse("postgres:15"))
//// .withDatabaseName("dotcms")
//// .withUsername("dotcms")
//// .withPassword("dotcms")
// .withEnv("POSTGRES_USER", "dotcms")
// .withEnv("POSTGRES_PASSWORD", "dotcms")
// .withEnv("POSTGRES_DB", "dotcms")
// .withCommand("postgres -c 'max_connections=400' -c 'shared_buffers=128MB'")
// .withExposedPorts(POSTGRES_SERVICE_PORT)
// .waitingFor(Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(STARTUP_TIMEOUT)))
// .withNetwork(NETWORK)
// .withReuse(true);


// @BeforeAll
// public static void setup() {
//
//// System.out.println("Postgres address: " + POSTGRES_CONTAINER.getHost());
//// System.out.println("Postgres port: " + POSTGRES_CONTAINER.getFirstMappedPort());
////
//// System.out.println("Elasticsearch address: " + ELASTICSEARCH_CONTAINER.getHost());
//// System.out.println("Elasticsearch port: " + ELASTICSEARCH_CONTAINER.getFirstMappedPort());
////
//// GenericContainer<?> DOTCMS_CONTAINER = new GenericContainer<>(DockerImageName.parse("dotcms/dotcms:latest"))
//// .withEnv("CMS_JAVA_OPTS", "-Xmx1G")
//// .withEnv("TZ", "UTC")
//// .withEnv("DB_BASE_URL", "jdbc:postgresql://" + POSTGRES_CONTAINER.getHost() + ":5432/dotcms")
////// .withEnv("DB_BASE_URL", "jdbc:postgresql://" + POSTGRES_CONTAINER.getHost() + ":" + POSTGRES_CONTAINER.getFirstMappedPort() + "/dotcms")
//// .withEnv("DB_USERNAME", "dotcms")
//// .withEnv("DB_PASSWORD", "dotcms")
//// .withEnv("DOT_ES_AUTH_BASIC_PASSWORD", "admin")
//// .withEnv("DOT_INITIAL_ADMIN_PASSWORD", "admin")
//// .withEnv("DOT_ES_ENDPOINTS", "https://" + ELASTICSEARCH_CONTAINER.getHost() + ":9200")
////// .withEnv("DOT_ES_ENDPOINTS", "https://" + ELASTICSEARCH_CONTAINER.getHost() + ":" + ELASTICSEARCH_CONTAINER.getFirstMappedPort())
////// .withEnv("CUSTOM_STARTER_URL", "https://repo.dotcms.com/artifactory/libs-release-local/com/dotcms/starter/20211201/starter-20211201.zip")
//// .withExposedPorts(8080, 8443)
//// .waitingFor(Wait.forHttp("/dotAdmin").forPort(8080).forStatusCode(200))
//// .dependsOn(POSTGRES_CONTAINER, ELASTICSEARCH_CONTAINER)
//// .withNetwork(NETWORK)
//// .withLogConsumer(logger -> System.out.print(logger.getUtf8String()))
//// .withReuse(true);
////
//// DOTCMS_CONTAINER.start();
//
//// System.out.println("DotCMS address: " + DOTCMS_CONTAINER.getHost());
//// System.out.println("DotCMS port: " + DOTCMS_CONTAINER.getFirstMappedPort());
//
// System.out.println("DummyTest.setup");
//
// }

// @Test
// public void should_launch_containers_test() {
// System.out.println("DummyTest.test");
// }
}
Loading

0 comments on commit 74b0094

Please sign in to comment.