Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
stianst committed Jul 15, 2024
1 parent ac7c2eb commit 3f6b918
Show file tree
Hide file tree
Showing 17 changed files with 162 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import org.keycloak.test.framework.KeycloakTestDatabase;
import org.keycloak.test.framework.injection.InstanceWrapper;
import org.keycloak.test.framework.injection.LifeCycle;
import org.keycloak.test.framework.injection.Registry;
import org.keycloak.test.framework.injection.Supplier;

public abstract class DatabaseSupplier implements Supplier<TestDatabase, KeycloakTestDatabase> {

protected static TestDatabase testDatabase;
public abstract class AbstractDatabaseSupplier implements Supplier<TestDatabase, KeycloakTestDatabase> {

@Override
public Class<KeycloakTestDatabase> getAnnotationClass() {
Expand All @@ -21,18 +20,16 @@ public Class<TestDatabase> getValueType() {

@Override
public InstanceWrapper<TestDatabase, KeycloakTestDatabase> getValue(Registry registry, KeycloakTestDatabase annotation) {
InstanceWrapper<TestDatabase, KeycloakTestDatabase> wrapper = new InstanceWrapper<>(this, annotation);
testDatabase = registry.getDependency(TestDatabase.class, wrapper);
TestDatabase testDatabase = getTestDatabase();
testDatabase.start();
return wrapper;
return new InstanceWrapper<>(this, annotation, testDatabase, LifeCycle.GLOBAL);
}

@Override
public boolean compatible(InstanceWrapper<TestDatabase, KeycloakTestDatabase> a, InstanceWrapper<TestDatabase, KeycloakTestDatabase> b) {
return true;
}

public TestDatabase getTestDatabase() {
return testDatabase;
}
abstract TestDatabase getTestDatabase();

}
Original file line number Diff line number Diff line change
@@ -1,31 +1,76 @@
package org.keycloak.test.framework.database;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public interface DatabaseConfig {
public class DatabaseConfig {

default String vendor() {
return "";
private String vendor;
private String containerImage;
private String urlHost;
private String username;
private String password;

public String getVendor() {
return vendor;
}

public DatabaseConfig vendor(String vendor) {
this.vendor = vendor;
return this;
}

public String getContainerImage() {
return containerImage;
}

public DatabaseConfig containerImage(String containerImage) {
this.containerImage = containerImage;
return this;
}

public String getUrlHost() {
return urlHost;
}

default String containerImage() {
return "";
public DatabaseConfig urlHost(String urlHost) {
this.urlHost = urlHost;
return this;
}

default String urlHost() {
return "";
public String getUsername() {
return username;
}

default String username() {
return "";
public DatabaseConfig username(String username) {
this.username = username;
return this;
}

default String password() {
return "";
public String getPassword() {
return password;
}

default boolean isExternal() {
return false;
public DatabaseConfig password(String password) {
this.password = password;
return this;
}

public Map<String, String> toConfig() {
Map<String, String> config = new HashMap<>();
if (vendor != null) {
config.put("db", vendor);
}
if (urlHost != null) {
config.put("db-url-host", urlHost);
}
if (username != null) {
config.put("db-username", username);
}
if (password != null) {
config.put("db-password", password);
}
return config;
}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.keycloak.test.framework.database;

public class DevFileDatabaseSupplier extends AbstractDatabaseSupplier {

@Override
TestDatabase getTestDatabase() {
DatabaseConfig databaseConfig = new DatabaseConfig().vendor("dev-file");
return new TestDatabase(databaseConfig);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.keycloak.test.framework.database;

public class DevMemDatabaseSupplier extends AbstractDatabaseSupplier {

@Override
TestDatabase getTestDatabase() {
DatabaseConfig databaseConfig = new DatabaseConfig().vendor("dev-mem");
return new TestDatabase(databaseConfig);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.keycloak.test.framework.database;

public class PostgresDatabaseSupplier extends AbstractDatabaseSupplier {

@Override
TestDatabase getTestDatabase() {
DatabaseConfig databaseConfig = new DatabaseConfig()
.vendor("postgres")
.username("keycloak")
.password("keycloak")
.containerImage("the-postgres-container:the-version");
return new TestDatabase(databaseConfig);
}

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
package org.keycloak.test.framework.database;

import java.util.Map;

public class TestDatabase {

private DatabaseConfig databaseConfig;

public TestDatabase(DatabaseConfig config) {
databaseConfig = config;
public TestDatabase(DatabaseConfig databaseConfig) {
this.databaseConfig = databaseConfig;
}

public void start() {

if (databaseConfig.getContainerImage() != null) {
// TODO Start container
}
}

public void stop() {

if (databaseConfig.getContainerImage() != null) {
// TODO Stop container
} else if (databaseConfig.getVendor().equals("dev-mem")) {
// TODO Stop in-mem H2 database
}
}

public DatabaseConfig getDatabaseConfig() {
return databaseConfig;
public Map<String, String> getServerConfig() {
return databaseConfig.toConfig();
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package org.keycloak.test.framework.server;

import org.keycloak.test.framework.KeycloakIntegrationTest;
import org.keycloak.test.framework.KeycloakTestDatabase;
import org.keycloak.test.framework.database.TestDatabase;
import org.keycloak.test.framework.injection.InstanceWrapper;
import org.keycloak.test.framework.injection.LifeCycle;
import org.keycloak.test.framework.injection.Registry;
import org.keycloak.test.framework.injection.Supplier;
import org.keycloak.test.framework.injection.SupplierHelpers;

import java.util.Collections;
import java.util.Map;

public abstract class AbstractKeycloakTestServerSupplier implements Supplier<KeycloakTestServer, KeycloakIntegrationTest> {

@Override
Expand All @@ -25,11 +27,18 @@ public Class<KeycloakIntegrationTest> getAnnotationClass() {
public InstanceWrapper<KeycloakTestServer, KeycloakIntegrationTest> getValue(Registry registry, KeycloakIntegrationTest annotation) {
KeycloakTestServerConfig serverConfig = SupplierHelpers.getInstance(annotation.config());
InstanceWrapper<KeycloakTestServer, KeycloakIntegrationTest> wrapper = new InstanceWrapper<>(this, annotation);
TestDatabase testDatabase = registry.getDependency(TestDatabase.class, wrapper);

Map<String, String> databaseConfig;
if (requiresDatabase()) {
TestDatabase testDatabase = registry.getDependency(TestDatabase.class, wrapper);
databaseConfig = testDatabase.getServerConfig();
} else {
databaseConfig = Collections.emptyMap();
}

KeycloakTestServer keycloakTestServer = getServer();
keycloakTestServer.start(serverConfig, databaseConfig);

keycloakTestServer.start(serverConfig, testDatabase.getDatabaseConfig());
wrapper.setValue(keycloakTestServer, LifeCycle.GLOBAL);

return wrapper;
Expand All @@ -47,4 +56,6 @@ public void close(KeycloakTestServer keycloakTestServer) {

public abstract KeycloakTestServer getServer();

public abstract boolean requiresDatabase();

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class DistributionKeycloakTestServer implements KeycloakTestServer {

Expand All @@ -16,7 +17,7 @@ public class DistributionKeycloakTestServer implements KeycloakTestServer {
private RawKeycloakDistribution keycloak;

@Override
public void start(KeycloakTestServerConfig serverConfig) {
public void start(KeycloakTestServerConfig serverConfig, Map<String, String> databaseConfig) {
keycloak = new RawKeycloakDistribution(debug, manualStop, enableTls, reCreate, removeBuildOptionsAfterBuild, requestPort);

// Set environment variables user and password for Keycloak Admin used by Keycloak instance.
Expand All @@ -35,6 +36,8 @@ public void start(KeycloakTestServerConfig serverConfig) {

serverConfig.options().forEach((key, value) -> rawOptions.add("--" + key + "=" + value));

databaseConfig.forEach((key, value) -> rawOptions.add("--" + key + "=" + value));

keycloak.run(rawOptions).assertStartedDevMode();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ public KeycloakTestServer getServer() {
return new DistributionKeycloakTestServer();
}

@Override
public boolean requiresDatabase() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

import org.keycloak.Keycloak;
import org.keycloak.common.Version;
import org.keycloak.test.framework.database.DatabaseConfig;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class EmbeddedKeycloakTestServer implements KeycloakTestServer {

private Keycloak keycloak;

@Override
public void start(KeycloakTestServerConfig serverConfig, DatabaseConfig databaseConfig) {

public void start(KeycloakTestServerConfig serverConfig, Map<String, String> databaseConfig) {
List<String> rawOptions = new LinkedList<>();
rawOptions.add("start-dev");
rawOptions.add("--cache=local");
Expand All @@ -23,18 +22,11 @@ public void start(KeycloakTestServerConfig serverConfig, DatabaseConfig database
rawOptions.add("--features=" + String.join(",", serverConfig.features()));
}

serverConfig.adminUserName().ifPresent(username -> rawOptions.add("--bootstrap-admin-username=" + username));
serverConfig.adminUserPassword().ifPresent(password -> rawOptions.add("--bootstrap-admin-password=" + password));

//rawOptions.add("--db=" + databaseConfig.vendor()); // TODO With dev-mem there's an issue as the H2 DB isn't stopped when restarting embedded server
if (databaseConfig.isExternal()) {
rawOptions.add("--db-url-host=" + databaseConfig.urlHost());
rawOptions.add("--db-username=" + databaseConfig.username());
rawOptions.add("--db-password=" + databaseConfig.password());
}

serverConfig.adminUserName().ifPresent(username -> System.setProperty("keycloakAdmin", username));
serverConfig.adminUserPassword().ifPresent(password -> System.setProperty("keycloakAdminPassword", password));

serverConfig.options().forEach((key, value) -> rawOptions.add("--" + key + "=" + value));
databaseConfig.forEach((key, value) -> rawOptions.add("--" + key + "=" + value));

keycloak = Keycloak.builder()
.setVersion(Version.VERSION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ public KeycloakTestServer getServer() {
return new EmbeddedKeycloakTestServer();
}

@Override
public boolean requiresDatabase() {
return true;
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.keycloak.test.framework.server;

import org.keycloak.test.framework.database.DatabaseConfig;
import org.keycloak.test.framework.database.TestDatabase;
import java.util.Map;

public interface KeycloakTestServer {

void start(KeycloakTestServerConfig serverConfig, DatabaseConfig databaseConfig);
void start(KeycloakTestServerConfig serverConfig, Map<String, String> databaseConfig);

void stop();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package org.keycloak.test.framework.server;

import org.keycloak.test.framework.database.DatabaseConfig;
import org.keycloak.test.framework.database.TestDatabase;
import java.util.Map;

public class RemoteKeycloakTestServer implements KeycloakTestServer {

@Override
public void start(KeycloakTestServerConfig serverConfig, DatabaseConfig databaseConfig) {

public void start(KeycloakTestServerConfig serverConfig, Map<String, String> databaseConfig) {
}

@Override
public void stop() {

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ public KeycloakTestServer getServer() {
return new RemoteKeycloakTestServer();
}

@Override
public boolean requiresDatabase() {
return false;
}

}
Loading

0 comments on commit 3f6b918

Please sign in to comment.