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

8294402: Add diagnostic logging to VMProps.checkDockerSupport #1965

Closed
wants to merge 1 commit into from
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
91 changes: 89 additions & 2 deletions test/jtreg-ext/requires/VMProps.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2021, 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 @@ -125,6 +129,7 @@ public Map<String, String> call() {
vmOptFinalFlags(map);

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

Expand Down Expand Up @@ -432,6 +437,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 @@ -450,6 +457,8 @@ protected String dockerSupport() {
}
}

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

if (isSupported) {
try {
isSupported = checkDockerSupport();
Expand All @@ -458,15 +467,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 @@ -581,6 +634,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