Skip to content

Commit

Permalink
Add basic jlink smoke test (#4067)
Browse files Browse the repository at this point in the history
Basically runs `jlink --add-modules java.base --output <output>
--verbose`. This is to ensure that signing of builds don't break the
jlink functionality. Especially when linking from the run-time image
(JEP 493 in JDK 24+).
  • Loading branch information
jerboaa authored Dec 11, 2024
1 parent 71311f8 commit bf70b81
Showing 1 changed file with 76 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ********************************************************************************
* Copyright (c) 2021 Contributors to the Eclipse Foundation
* Copyright (c) 2021, 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) with this work for additional
* information regarding copyright ownership.
Expand All @@ -15,20 +15,27 @@

package net.adoptium.test;

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import static net.adoptium.test.JdkPlatform.Architecture;
import static net.adoptium.test.JdkPlatform.OperatingSystem;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import net.adoptium.test.JdkPlatform.Architecture;
import net.adoptium.test.JdkPlatform.OperatingSystem;

/**
* Tests the availability of various features like garbage collectors, flight recorder, that need to be enabled via
Expand Down Expand Up @@ -104,6 +111,68 @@ public void testLinkableRuntimeJDK24Plus() {
}
}

/**
* Tests whether basic jlink works using the default module path. The only
* included module in the output image is {@code java.base}.
*/
@Test
public void testJlinkJdk11AndBetter() throws IOException {
// Only JDK 11 (JDK 9, really) and better have jlink
if (jdkVersion.isNewerOrEqual(11)) {
Path output = Paths.get("java.base-image");
ensureOutputDirectoryDeleted(output);
List<String> command = new ArrayList<>();
command.add(String.format("%s/bin/jlink", testJdkHome));
command.add("--add-modules");
command.add("java.base");
command.add("--output");
command.add(output.toString());
command.add("--verbose");

try {
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.inheritIO();
Process process = processBuilder.start();

if (process.waitFor() != 0) {
throw new AssertionError("Basic jlink smoke test failed! " + command);
}
LOGGER.info("Basic jlink smoke test passed. Command was: " + command);
} catch (InterruptedException | IOException e) {
throw new RuntimeException("Failed to launch JVM", e);
} finally {
ensureOutputDirectoryDeleted(output);
}
}
}

private static void ensureOutputDirectoryDeleted(final Path path) throws IOException {
if (Files.exists(path)) {
deleteDirRecursively(path);
}
}

private static void deleteDirRecursively(final Path path) throws IOException {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

@Override
public FileVisitResult postVisitDirectory(final Path dir, final IOException e) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
if (Files.isDirectory(file)) {
// deleted in post-visit
return FileVisitResult.CONTINUE;
}
Files.delete(file);
return FileVisitResult.CONTINUE;
}
});
}

private boolean isVendorAdoptium() {
return System.getProperty("java.vendor", "").toLowerCase(Locale.US).contains("adoptium");
}
Expand Down

0 comments on commit bf70b81

Please sign in to comment.