Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve MariaDB client test #485

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
*/
package mariadb;

import org.awaitility.Awaitility;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
Expand All @@ -22,6 +20,11 @@
import java.time.Duration;
import java.util.Properties;

import org.awaitility.Awaitility;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
Expand All @@ -35,35 +38,77 @@ public class MariaDbTests {

private static final String DATABASE = "test";

private static final String JDBC_URL = "jdbc:mariadb://localhost:3306/" + DATABASE;
private static final String DOCKER_IMAGE = "mariadb:11";

private static final File STD_OUT = new File("mariadb-stdout.txt");

private static final File STD_ERR = new File("mariadb-stderr.txt");

private static String jdbcUrl;

private static Process process;

private static Connection openConnection() throws SQLException {
Properties props = new Properties();
props.setProperty("user", USERNAME);
props.setProperty("password", PASSWORD);
return DriverManager.getConnection(JDBC_URL, props);
return DriverManager.getConnection(jdbcUrl, props);
}

@BeforeAll
static void beforeAll() throws IOException {
System.out.println("Starting MariaDB ...");
int hostPort = findAvailablePort();
jdbcUrl = "jdbc:mariadb://127.0.0.1:%d/%s".formatted(hostPort, DATABASE);

System.out.printf("Starting MariaDB on port %d ...%n", hostPort);
process = new ProcessBuilder(
"docker", "run", "--rm", "-p", "3306:3306", "-e", "MARIADB_DATABASE=" + DATABASE, "-e", "MARIADB_USER=" + USERNAME,
"-e", "MARIADB_PASSWORD=" + PASSWORD, "-e", "MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=true", "mariadb:10.8").redirectOutput(new File("mariadb-stdout.txt"))
.redirectError(new File("mariadb-stderr.txt")).start();
"docker", "run", "--rm", "-p", hostPort + ":3306", "-e", "MARIADB_DATABASE=" + DATABASE, "-e", "MARIADB_USER=" + USERNAME,
"-e", "MARIADB_PASSWORD=" + PASSWORD, "-e", "MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=true", DOCKER_IMAGE).redirectOutput(STD_OUT)
.redirectError(STD_ERR).start();

waitUntilContainerIsReady();

System.out.printf("MariaDB started on port %d, JDBC URL: '%s'%n", hostPort, jdbcUrl);
}

// Wait until connection can be established
private static void waitUntilContainerIsReady() {
Awaitility.await().atMost(Duration.ofMinutes(1)).ignoreExceptionsMatching(e ->
e instanceof SQLNonTransientConnectionException
).until(() -> {
if (!process.isAlive()) {
printFileContent("stdout", STD_OUT);
printFileContent("stderr", STD_ERR);
throw new IllegalStateException("Process has already exited with code %d".formatted(process.exitValue()));
}
openConnection().close();
return true;
});
System.out.println("MariaDB started");
}

private static void printFileContent(String name, File file) {
if (!file.exists()) {
System.out.println("<< " + file + " not found >>");
return;
}
try {
String content = Files.readString(file.toPath(), StandardCharsets.UTF_8);
System.out.println("Content of " + name + ":");
System.out.println(content);
System.out.println();
} catch (IOException e) {
System.out.println("<< Exception while reading " + file + " >>");
e.printStackTrace();
}
}

private static int findAvailablePort() throws IOException {
try (ServerSocket serverSocket = new ServerSocket()) {
serverSocket.bind(null);
return serverSocket.getLocalPort();
}
}


@AfterAll
static void tearDown() {
if (process != null && process.isAlive()) {
Expand Down