Skip to content

Commit

Permalink
8294402: Add diagnostic logging to VMProps.checkDockerSupport
Browse files Browse the repository at this point in the history
Reviewed-by: dholmes, lmesnik
  • Loading branch information
Mikhailo Seledtsov committed Feb 17, 2023
1 parent a263f28 commit 03d613b
Showing 1 changed file with 89 additions and 2 deletions.
91 changes: 89 additions & 2 deletions test/jtreg-ext/requires/VMProps.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -27,10 +27,13 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -87,6 +90,7 @@ public void put(String key, Supplier<String> s) {
*/
@Override
public Map<String, String> call() {
log("Entering call()");
SafeMap map = new SafeMap();
map.put("vm.flavor", this::vmFlavor);
map.put("vm.compMode", this::vmCompMode);
Expand Down Expand Up @@ -127,6 +131,7 @@ public Map<String, String> call() {
vmOptFinalFlags(map);

dump(map.map);
log("Leaving call()");
return map.map;
}

Expand Down Expand Up @@ -473,6 +478,8 @@ protected String isCompiler2Enabled() {
* @return true if docker is supported in a given environment
*/
protected String dockerSupport() {
log("Entering dockerSupport()");

boolean isSupported = false;
if (Platform.isLinux()) {
// currently docker testing is only supported for Linux,
Expand All @@ -491,6 +498,8 @@ protected String dockerSupport() {
}
}

log("dockerSupport(): platform check: isSupported = " + isSupported);

if (isSupported) {
try {
isSupported = checkDockerSupport();
Expand All @@ -499,15 +508,59 @@ protected String dockerSupport() {
}
}

log("dockerSupport(): returning isSupported = " + isSupported);
return "" + isSupported;
}

// Configures process builder to redirect process stdout and stderr to a file.
// Returns file names for stdout and stderr.
private Map<String, String> redirectOutputToLogFile(String msg, ProcessBuilder pb, String fileNameBase) {
Map<String, String> result = new HashMap<>();
String timeStamp = Instant.now().toString().replace(":", "-").replace(".", "-");

String stdoutFileName = String.format("./%s-stdout--%s.log", fileNameBase, timeStamp);
pb.redirectOutput(new File(stdoutFileName));
log(msg + ": child process stdout redirected to " + stdoutFileName);
result.put("stdout", stdoutFileName);

String stderrFileName = String.format("./%s-stderr--%s.log", fileNameBase, timeStamp);
pb.redirectError(new File(stderrFileName));
log(msg + ": child process stderr redirected to " + stderrFileName);
result.put("stderr", stderrFileName);

return result;
}

private void printLogfileContent(Map<String, String> logFileNames) {
logFileNames.entrySet().stream()
.forEach(entry ->
{
log("------------- " + entry.getKey());
try {
Files.lines(Path.of(entry.getValue()))
.forEach(line -> log(line));
} catch (IOException ie) {
log("Exception while reading file: " + ie);
}
log("-------------");
});
}

private boolean checkDockerSupport() throws IOException, InterruptedException {
log("checkDockerSupport(): entering");
ProcessBuilder pb = new ProcessBuilder(Container.ENGINE_COMMAND, "ps");
Map<String, String> logFileNames = redirectOutputToLogFile("checkDockerSupport(): <container> ps",
pb, "container-ps");
Process p = pb.start();
p.waitFor(10, TimeUnit.SECONDS);
int exitValue = p.exitValue();

log(String.format("checkDockerSupport(): exitValue = %s, pid = %s", exitValue, p.pid()));
if (exitValue != 0) {
printLogfileContent(logFileNames);
}

return (p.exitValue() == 0);
return (exitValue == 0);
}

/**
Expand Down Expand Up @@ -622,6 +675,40 @@ protected static void dump(Map<String, String> map) {
}
}

/**
* Log diagnostic message.
*
* @param msg
*/
protected static void log(String msg) {
// Always log to a file.
logToFile(msg);

// Also log to stderr; guarded by property to avoid excessive verbosity.
// By jtreg design stderr produced here will be visible
// in the output of a parent process. Note: stdout should not be used
// for logging as jtreg parses that output directly and only echoes it
// in the event of a failure.
if (Boolean.getBoolean("jtreg.log.vmprops")) {
System.err.println("VMProps: " + msg);
}
}

/**
* Log diagnostic message to a file.
*
* @param msg
*/
protected static void logToFile(String msg) {
String fileName = "./vmprops.log";
try {
Files.writeString(Paths.get(fileName), msg + "\n", Charset.forName("ISO-8859-1"),
StandardOpenOption.APPEND, StandardOpenOption.CREATE);
} catch (IOException e) {
throw new RuntimeException("Failed to log into '" + fileName + "'", e);
}
}

/**
* This method is for the testing purpose only.
*
Expand Down

1 comment on commit 03d613b

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.