-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Generate a test dependencies file to support unit tests in entitlements #127486
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
Merged
Merged
Changes from 25 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
ee7acf3
add task skeleton
jdconrad 619fd63
generate file for test dependencies
jdconrad 1de3d1d
fix policy file
jdconrad 9841168
update
jdconrad a69872e
convert to json file isntead of properties file
jdconrad 73af578
fix dependency link
jdconrad 40cc22f
start of jar entry lookup
jdconrad 06522bd
Merge remote-tracking branch 'upstream/main' into eut2
jdconrad 65c7d72
generate file with class to module info
jdconrad a9984fe
add module info to build info file
jdconrad 445d3bf
added files to resources
jdconrad 2c1a15f
protect task for plugins with no source
jdconrad 73e6379
separate into separate plugin
jdconrad 036fed8
add output for server
jdconrad fdf526e
remove todo
jdconrad 7d7f8cd
Merge branch 'main' into eut2
jdconrad 5f4c870
clean up
jdconrad 729559b
add module info for directory
jdconrad 9867a39
add module inference when plugin is not modular
jdconrad c4764b9
Merge branch 'main' into eut2
jdconrad ff80fda
Merge branch 'main' into eut2
jdconrad f6585fc
[CI] Auto commit changes from spotless
927f9bf
Merge branch 'main' into eut2
jdconrad 32cadde
Merge branch 'main' into eut2
jdconrad 887e600
add comments
jdconrad 9949f91
Merge remote-tracking branch 'upstream/main' into eut2
prdoyle e57cd37
First pass addressing PR comments
prdoyle 444da3d
WIP non-working TestBuildInfoPluginFuncTest
prdoyle dd56d50
TestBuildInfoPluginFuncTest
prdoyle 854df10
Tweaks
prdoyle 931deb9
TestBuildInfoPluginFuncTest check the file's contents
prdoyle 461af45
Merge branch 'main' into eut2
jdconrad 33ce238
Use Files.walkFileTree
prdoyle 460ea40
some response to pr comments
jdconrad 8833e6e
Merge remote-tracking branch 'origin/eut2' into eut2
jdconrad 1266847
update gradle values to use callable
jdconrad a38969c
Split out extractModuleNameFromJar into smaller steps
prdoyle 0df276a
fix test
jdconrad 69742cf
Merge remote-tracking branch 'origin/eut2' into eut2
jdconrad 1d1c27d
Merge branch 'main' into eut2
jdconrad 75496f4
remove extraneous callable
jdconrad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
294 changes: 294 additions & 0 deletions
294
build-tools/src/main/java/org/elasticsearch/gradle/plugin/GenerateTestBuildInfoTask.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,294 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.gradle.plugin; | ||
|
|
||
| import org.gradle.api.DefaultTask; | ||
| import org.gradle.api.file.DirectoryProperty; | ||
| import org.gradle.api.file.FileCollection; | ||
| import org.gradle.api.provider.Property; | ||
| import org.gradle.api.tasks.Input; | ||
| import org.gradle.api.tasks.InputFiles; | ||
| import org.gradle.api.tasks.Optional; | ||
| import org.gradle.api.tasks.OutputDirectory; | ||
| import org.gradle.api.tasks.TaskAction; | ||
| import org.objectweb.asm.ClassReader; | ||
| import org.objectweb.asm.ClassVisitor; | ||
| import org.objectweb.asm.ModuleVisitor; | ||
| import org.objectweb.asm.Opcodes; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileInputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.jar.JarEntry; | ||
| import java.util.jar.JarFile; | ||
| import java.util.jar.Manifest; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| import java.util.zip.ZipEntry; | ||
|
|
||
| /** | ||
| * This task generates a file with a class to module mapping | ||
| * used to imitate modular behavior during unit tests so | ||
| * entitlements can lookup correct policies. | ||
| */ | ||
| public abstract class GenerateTestBuildInfoTask extends DefaultTask { | ||
jdconrad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public static final String DESCRIPTION = "generates plugin test dependencies file"; | ||
|
|
||
| public GenerateTestBuildInfoTask() { | ||
| setDescription(DESCRIPTION); | ||
| } | ||
|
|
||
| @Input | ||
| @Optional | ||
| public abstract Property<String> getModuleName(); | ||
|
|
||
| @Input | ||
| public abstract Property<String> getComponentName(); | ||
|
|
||
| @InputFiles | ||
jdconrad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public abstract Property<FileCollection> getCodeLocations(); | ||
|
|
||
| @Input | ||
| public abstract Property<String> getOutputFileName(); | ||
|
|
||
| @OutputDirectory | ||
| public abstract DirectoryProperty getOutputDirectory(); | ||
jdconrad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @TaskAction | ||
| public void generatePropertiesFile() throws IOException { | ||
| Map<String, String> classesToModules = buildClassesToModules(); | ||
|
|
||
| Path outputDirectory = getOutputDirectory().get().getAsFile().toPath(); | ||
| Files.createDirectories(outputDirectory); | ||
| Path outputFile = outputDirectory.resolve(getOutputFileName().get()); | ||
|
|
||
| try (var writer = Files.newBufferedWriter(outputFile, StandardCharsets.UTF_8)) { | ||
| writer.write("{\n"); | ||
jdconrad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| writer.write(" \"name\": \""); | ||
| writer.write(getComponentName().get()); | ||
| writer.write("\",\n"); | ||
|
|
||
| writer.write(" \"locations\": [\n"); | ||
| if (classesToModules.isEmpty() == false) { | ||
| StringBuilder sb = new StringBuilder(); | ||
| for (Map.Entry<String, String> entry : classesToModules.entrySet()) { | ||
| sb.append(" {\n"); | ||
| sb.append(" \"class\": \""); | ||
jdconrad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| sb.append(entry.getKey()); | ||
| sb.append("\",\n \"module\": \""); | ||
| sb.append(entry.getValue()); | ||
| sb.append("\"\n },\n"); | ||
| } | ||
| writer.write(sb.substring(0, sb.length() - 2)); | ||
| } | ||
| writer.write("\n ]\n}\n"); | ||
| } | ||
| } | ||
|
|
||
| // build the association for a class to a module; | ||
| // there are different methods for finding these depending on if the | ||
| // classpath entry is a jar or a directory | ||
jdconrad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private Map<String, String> buildClassesToModules() throws IOException { | ||
| Map<String, String> classesToModules = new HashMap<>(); | ||
| for (File file : getCodeLocations().get().getFiles()) { | ||
| if (file.exists()) { | ||
| if (file.getName().endsWith(".jar")) { | ||
| extractFromJar(file, classesToModules); | ||
| } else if (file.isDirectory()) { | ||
| extractFromDirectory(file, classesToModules); | ||
| } else { | ||
| throw new IllegalArgumentException("unrecognized classpath entry: " + file); | ||
| } | ||
| } | ||
| } | ||
| return classesToModules; | ||
| } | ||
|
|
||
| // find the first class and module when the class path entry is a jar | ||
| private void extractFromJar(File file, Map<String, String> classesToModules) throws IOException { | ||
| try (JarFile jarFile = new JarFile(file)) { | ||
| String className = extractClassNameFromJar(jarFile); | ||
| String moduleName = extractModuleNameFromJar(file, jarFile); | ||
|
|
||
| if (className != null) { | ||
jdconrad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| classesToModules.put(className, moduleName); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // look through the jar to find the first unique class that isn't | ||
| // in META-INF (those may not be unique) and isn't module-info.class | ||
| // (which is also not unique) and avoid anonymous classes | ||
| private String extractClassNameFromJar(JarFile jarFile) { | ||
| return jarFile.stream() | ||
| .filter( | ||
| je -> je.getName().startsWith("META-INF") == false | ||
| && je.getName().equals("module-info.class") == false | ||
| && je.getName().contains("$") == false | ||
| && je.getName().endsWith(".class") | ||
| ) | ||
| .findFirst() | ||
| .map(ZipEntry::getName) | ||
| .orElse(null); | ||
| } | ||
|
|
||
| // look through the jar for the module name with | ||
| // each step commented inline | ||
| private String extractModuleNameFromJar(File file, JarFile jarFile) throws IOException { | ||
jdconrad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| String moduleName = null; | ||
|
|
||
| // if the jar is multi-release, there will be a set versions | ||
| // under the path META-INF/versions/<version number>; | ||
| // each version will have its own module-info.class if this is a modular jar; | ||
| // look for the module name in the module-info from the latest version | ||
| // fewer than or equal to the current JVM version | ||
| if (jarFile.isMultiRelease()) { | ||
jdconrad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| List<Integer> versions = jarFile.stream() | ||
| .filter(je -> je.getName().startsWith("META-INF/versions/") && je.getName().endsWith("/module-info.class")) | ||
| .map(je -> Integer.parseInt(je.getName().substring(18, je.getName().length() - 18))) | ||
jdconrad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .toList(); | ||
| versions = new ArrayList<>(versions); | ||
| versions.sort(Integer::compareTo); | ||
| versions = versions.reversed(); | ||
| int major = Runtime.version().version().get(0); | ||
| StringBuilder path = new StringBuilder("META-INF/versions/"); | ||
| for (int version : versions) { | ||
| if (version <= major) { | ||
| path.append(version); | ||
| break; | ||
| } | ||
| } | ||
| if (path.length() > 18) { | ||
jdconrad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| path.append("/module-info.class"); | ||
| JarEntry moduleEntry = jarFile.getJarEntry(path.toString()); | ||
| if (moduleEntry != null) { | ||
| try (InputStream inputStream = jarFile.getInputStream(moduleEntry)) { | ||
| moduleName = extractModuleNameFromModuleInfo(inputStream); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // if the jar is *not* multi-release then first look in | ||
| // module-info.class from the top-level if it exists | ||
| if (moduleName == null) { | ||
| JarEntry moduleEntry = jarFile.getJarEntry("module-info.class"); | ||
| if (moduleEntry != null) { | ||
| try (InputStream inputStream = jarFile.getInputStream(moduleEntry)) { | ||
| moduleName = extractModuleNameFromModuleInfo(inputStream); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // if the jar does *not* contain module-info.class | ||
| // check the manifest file for the module name | ||
| if (moduleName == null) { | ||
| JarEntry manifestEntry = jarFile.getJarEntry("META-INF/MANIFEST.MF"); | ||
| if (manifestEntry != null) { | ||
| try (InputStream inputStream = jarFile.getInputStream(manifestEntry)) { | ||
| Manifest manifest = new Manifest(inputStream); | ||
| String amn = manifest.getMainAttributes().getValue("Automatic-Module-Name"); | ||
| if (amn != null) { | ||
| moduleName = amn; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // if the jar does not have module-info.class and no module name in the manifest | ||
| // default to the jar name without .jar and no versioning | ||
| if (moduleName == null) { | ||
| String jn = file.getName().substring(0, file.getName().length() - 4); | ||
jdconrad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Matcher matcher = Pattern.compile("-(\\d+(\\.|$))").matcher(jn); | ||
| if (matcher.find()) { | ||
| jn = jn.substring(0, matcher.start()); | ||
| } | ||
| jn = jn.replaceAll("[^A-Za-z0-9]", "."); | ||
| moduleName = jn; | ||
| } | ||
|
|
||
| return moduleName; | ||
| } | ||
|
|
||
| // find the first class and module when the class path entry is a directory | ||
| private void extractFromDirectory(File file, Map<String, String> classesToModules) throws IOException { | ||
| String className = extractClassNameFromDirectory(file); | ||
| String moduleName = extractModuleNameFromDirectory(file); | ||
|
|
||
| if (className != null && moduleName != null) { | ||
| classesToModules.put(className, moduleName); | ||
| } | ||
| } | ||
|
|
||
| // look through the directory to find the first unique class that isn't | ||
| // module-info.class (which may not be unique) and avoid anonymous classes | ||
| private String extractClassNameFromDirectory(File file) { | ||
| List<File> files = new ArrayList<>(List.of(file)); | ||
prdoyle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| while (files.isEmpty() == false) { | ||
| File find = files.removeFirst(); | ||
| if (find.exists()) { | ||
| if (find.getName().endsWith(".class") | ||
| && find.getName().equals("module-info.class") == false | ||
| && find.getName().contains("$") == false) { | ||
| return find.getAbsolutePath().substring(file.getAbsolutePath().length() + 1); | ||
| } else if (find.isDirectory()) { | ||
| files.addAll(Arrays.asList(find.listFiles())); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| return null; | ||
| } | ||
|
|
||
| // look through the directory to find the module name in either module-info.class | ||
| // if it exists or the preset one derived from the jar task | ||
| private String extractModuleNameFromDirectory(File file) throws IOException { | ||
| List<File> files = new ArrayList<>(List.of(file)); | ||
| while (files.isEmpty() == false) { | ||
| File find = files.removeFirst(); | ||
| if (find.exists()) { | ||
| if (find.getName().equals("module-info.class")) { | ||
| try (InputStream inputStream = new FileInputStream(find)) { | ||
| return extractModuleNameFromModuleInfo(inputStream); | ||
| } | ||
| } else if (find.isDirectory()) { | ||
| files.addAll(Arrays.asList(find.listFiles())); | ||
| } | ||
| } | ||
| } | ||
| return getModuleName().isPresent() ? getModuleName().get() : null; | ||
| } | ||
|
|
||
| // a helper method to extract the module name from module-info.class | ||
| // using an ASM ClassVisitor | ||
| private String extractModuleNameFromModuleInfo(InputStream inputStream) throws IOException { | ||
| String[] moduleName = new String[1]; | ||
| ClassReader cr = new ClassReader(inputStream); | ||
| cr.accept(new ClassVisitor(Opcodes.ASM9) { | ||
| @Override | ||
| public ModuleVisitor visitModule(String name, int access, String version) { | ||
| moduleName[0] = name; | ||
| return super.visitModule(name, access, version); | ||
| } | ||
| }, Opcodes.ASM9); | ||
| return moduleName[0]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.