Skip to content

Commit

Permalink
Merge pull request #30063 from watermelonjam/main
Browse files Browse the repository at this point in the history
Add durable container log file destination
  • Loading branch information
geoand authored Jan 3, 2023
2 parents 6a31744 + 6a16241 commit 6014d34
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@
import static io.quarkus.test.common.LauncherUtil.waitForCapturedListeningData;
import static io.quarkus.test.common.LauncherUtil.waitForStartedFunction;
import static java.lang.ProcessBuilder.Redirect.DISCARD;
import static java.lang.ProcessBuilder.Redirect.PIPE;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.TeeInputStream;
import org.apache.commons.lang3.RandomStringUtils;

import io.quarkus.runtime.util.ContainerRuntimeUtil;
Expand All @@ -43,6 +51,8 @@ public class DefaultDockerContainerLauncher implements DockerContainerArtifactLa

private String containerRuntimeBinaryName;

private ExecutorService executorService = Executors.newSingleThreadExecutor();

@Override
public void init(DockerContainerArtifactLauncher.DockerInitContext initContext) {
this.httpPort = initContext.httpPort();
Expand Down Expand Up @@ -131,15 +141,20 @@ public void start() throws IOException {
Files.deleteIfExists(logFile);
Files.createDirectories(logFile.getParent());

Path containerLogFile = Paths.get("target", "container.log");
Files.createDirectories(containerLogFile.getParent());
FileOutputStream containerLogOutputStream = new FileOutputStream(containerLogFile.toFile(), true);

System.out.println("Executing \"" + String.join(" ", args) + "\"");

Function<IntegrationTestStartedNotifier.Context, IntegrationTestStartedNotifier.Result> startedFunction = createStartedFunction();

// the idea here is to obtain the logs of the application simply by redirecting all its output the a file
// this is done in contrast with the JarLauncher and NativeImageLauncher because in the case of the container
// the log itself is written inside the container
Process quarkusProcess = new ProcessBuilder(args).redirectError(logFile.toFile()).redirectOutput(logFile.toFile())
.start();
Process quarkusProcess = new ProcessBuilder(args).redirectError(PIPE).redirectOutput(PIPE).start();
InputStream tee = new TeeInputStream(quarkusProcess.getInputStream(), new FileOutputStream(logFile.toFile()));
executorService.submit(() -> IOUtils.copy(tee, containerLogOutputStream));

if (startedFunction != null) {
IntegrationTestStartedNotifier.Result result = waitForStartedFunction(startedFunction, quarkusProcess,
Expand Down Expand Up @@ -194,6 +209,7 @@ public void close() {
} catch (IOException | InterruptedException e) {
System.out.println("Unable to stop container '" + containerName + "'");
}
executorService.shutdown();
}

}

0 comments on commit 6014d34

Please sign in to comment.