more improvements

This commit is contained in:
andreaTP 2021-12-07 09:19:06 +00:00 committed by Pedro Igor
parent e1a2e27e6c
commit 1f9b2be65a
4 changed files with 54 additions and 32 deletions

View file

@ -9,6 +9,7 @@ import org.testcontainers.containers.output.ToStringConsumer;
import org.testcontainers.images.builder.ImageFromDockerfile;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
@ -20,29 +21,46 @@ public final class DockerKeycloakDistribution implements KeycloakDistribution {
private boolean manualStop;
private int exitCode = -1;
private List<String> stdout = List.of();
private List<String> stderr = List.of();
private String stdout = "";
private String stderr = "";
private ToStringConsumer backupConsumer = new ToStringConsumer();
private File distributionFile = new File("../../../distribution/server-x-dist/target/keycloak.x-" + Version.VERSION_KEYCLOAK + ".tar.gz");
private File cachedDockerfile = createDockerCacheFile();
private boolean dockerfileFetched = false;
private GenericContainer keycloakContainer = null;
private GenericContainer runKeycloakContainer() {
private File createDockerCacheFile() {
try {
File tmp = File.createTempFile("Dockerfile", "keycloak.x");
tmp.deleteOnExit();
return tmp;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void fetchDockerfile() {
if (!dockerfileFetched) {
try {
FileUtils.copyURLToFile(new URL("https://raw.githubusercontent.com/keycloak/keycloak-containers/main/server-x/Dockerfile"), cachedDockerfile);
dockerfileFetched = true;
} catch (Exception cause) {
throw new RuntimeException("Cannot download upstream Dockerfile", cause);
}
}
}
private GenericContainer getKeycloakContainer() {
if (!distributionFile.exists()) {
throw new RuntimeException("Distribution archive " + distributionFile.getAbsolutePath() +" doesn't exists");
}
File dockerFile = null;
try {
dockerFile = File.createTempFile("keycloakx", "Dockerfile");
FileUtils.copyURLToFile(new URL("https://raw.githubusercontent.com/keycloak/keycloak-containers/main/server-x/Dockerfile"), dockerFile);
} catch (Exception cause) {
throw new RuntimeException("Cannot download upstream Dockerfile", cause);
}
fetchDockerfile();
return new GenericContainer(
new ImageFromDockerfile()
.withFileFromFile("keycloakx.tar.gz", distributionFile)
.withFileFromFile("Dockerfile", dockerFile)
.withFileFromFile("Dockerfile", cachedDockerfile)
.withBuildArg("KEYCLOAK_DIST", "keycloakx.tar.gz")
)
.withExposedPorts(8080);
@ -57,12 +75,12 @@ public final class DockerKeycloakDistribution implements KeycloakDistribution {
public void start(List<String> arguments) {
try {
this.exitCode = -1;
this.stdout = List.of();
this.stderr = List.of();
this.stdout = "";
this.stderr = "";
this.backupConsumer = new ToStringConsumer();
keycloakContainer = runKeycloakContainer();
keycloakContainer = getKeycloakContainer();
keycloakContainer
.withLogConsumer(backupConsumer)
@ -73,8 +91,8 @@ public final class DockerKeycloakDistribution implements KeycloakDistribution {
io.restassured.RestAssured.port = keycloakContainer.getMappedPort(8080);
} catch (Exception cause) {
this.exitCode = -1;
this.stdout = List.of(backupConsumer.toUtf8String());
this.stderr = List.of(backupConsumer.toUtf8String());
this.stdout = backupConsumer.toUtf8String();
this.stderr = backupConsumer.toUtf8String();
keycloakContainer = null;
LOGGER.warn("Failed to start Keycloak container", cause);
}
@ -84,8 +102,8 @@ public final class DockerKeycloakDistribution implements KeycloakDistribution {
public void stop() {
try {
if (keycloakContainer != null) {
this.stdout = getOutputStream();
this.stderr = getErrorStream();
this.stdout = fetchOutputStream();
this.stderr = fetchErrorStream();
keycloakContainer.stop();
this.exitCode = 0;
@ -97,29 +115,36 @@ public final class DockerKeycloakDistribution implements KeycloakDistribution {
keycloakContainer = null;
}
}
@Override
public List<String> getOutputStream() {
private String fetchOutputStream() {
if (keycloakContainer != null && keycloakContainer.isRunning()) {
return List.of(keycloakContainer.getLogs(OutputFrame.OutputType.STDOUT));
return keycloakContainer.getLogs(OutputFrame.OutputType.STDOUT);
} else if (this.stdout.isEmpty()) {
return List.of(backupConsumer.toUtf8String());
return backupConsumer.toUtf8String();
} else {
return this.stdout;
}
}
@Override
public List<String> getErrorStream() {
public List<String> getOutputStream() {
return List.of(fetchOutputStream().split("\n"));
}
public String fetchErrorStream() {
if (keycloakContainer != null && keycloakContainer.isRunning()) {
return List.of(keycloakContainer.getLogs(OutputFrame.OutputType.STDERR));
return keycloakContainer.getLogs(OutputFrame.OutputType.STDERR);
} else if (this.stderr.isEmpty()) {
return List.of(backupConsumer.toUtf8String());
return backupConsumer.toUtf8String();
} else {
return this.stderr;
}
}
@Override
public List<String> getErrorStream() {
return List.of(fetchErrorStream().split("\n"));
}
@Override
public int getExitCode() {
return this.exitCode;

View file

@ -51,8 +51,6 @@ class BuildCommandDistTest {
() -> "The Error Output:\n" + result.getErrorOutput() + "doesn't contains the expected string.");
assertTrue(result.getErrorOutput().contains("For more details run the same command passing the '--verbose' option. Also you can use '--help' to see the details about the usage of the particular command."),
() -> "The Error Output:\n" + result.getErrorOutput() + "doesn't contains the expected string.");
String errorString = "";
for (String s: result.getErrorStream()) { errorString += s + "\n"; }
assertEquals(4, errorString.split("\n").length);
assertEquals(4, result.getErrorStream().size());
}
}

View file

@ -35,8 +35,6 @@ public class StartCommandDistTest extends StartCommandTest {
void failIfAutoBuildUsingDevProfile(LaunchResult result) {
assertTrue(result.getErrorOutput().contains("ERROR: You can not 'start' the server using the 'dev' configuration profile. Please re-build the server first, using 'kc.sh build' for the default production profile, or using 'kc.sh build --profile=<profile>' with a profile more suitable for production."),
() -> "The Output:\n" + result.getErrorOutput() + "doesn't contains the expected string.");
String errorString = "";
for (String s: result.getErrorStream()) { errorString += s + "\n"; }
assertEquals(4, errorString.split("\n").length);
assertEquals(4, result.getErrorStream().size());
}
}

View file

@ -0,0 +1 @@
checks.disable=true