Skip to content

Commit f7a7f0b

Browse files
jdconradprdoyle
andauthored
Generate a test dependencies file to support unit tests in entitlements (#127486)
Generates a test file with the following information and format: { "component": "<component name>", "locations": [ { "representative_class": <class name with package>, "module": "<module name>" }, ... ] } For painless: { "component": "lang-painless", "locations": [ { "representative_class": "org/objectweb/asm/tree/analysis/Analyzer.class", "module": "org.objectweb.asm.tree.analysis" }, ... ] } Then it copies the following files into the jar for consumption by unit tests: * META-INF/plugin-test-build-info.json * META-INF/es-plugins/<plugin name>/plugin-descriptor.properties * META-INF/es-plugins/<plugin name>/entitlement-policy.yaml For server, the files in the jar become the following: * META-INF/server-test-build-info.json This should provide enough information for BootstrapForTesting to be able to build a mapping of caller class to policy file using the class file to look up the jar or directory within the class path and then associating that with it's specified module and finally using the specified module to look up the appropriate entitlement policy. caller class -> specified module -> entitlement policy --------- Co-authored-by: Patrick Doyle <[email protected]>
1 parent 85d7359 commit f7a7f0b

File tree

7 files changed

+518
-2
lines changed

7 files changed

+518
-2
lines changed

build-tools/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ gradlePlugin {
5151
id = 'elasticsearch.stable-esplugin'
5252
implementationClass = 'org.elasticsearch.gradle.plugin.StablePluginBuildPlugin'
5353
}
54+
testBuildInfo {
55+
id = 'elasticsearch.test-build-info'
56+
implementationClass = 'org.elasticsearch.gradle.test.TestBuildInfoPlugin'
57+
}
5458
javaRestTest {
5559
id = 'elasticsearch.java-rest-test'
5660
implementationClass = 'org.elasticsearch.gradle.test.JavaRestTestPlugin'
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.elasticsearch.gradle.test
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper
4+
5+
import org.elasticsearch.gradle.fixtures.AbstractGradleFuncTest
6+
import org.gradle.testkit.runner.TaskOutcome
7+
8+
class TestBuildInfoPluginFuncTest extends AbstractGradleFuncTest {
9+
def "works"() {
10+
given:
11+
file("src/main/java/com/example/Example.java") << """
12+
package com.example;
13+
14+
public class Example {
15+
}
16+
"""
17+
18+
file("src/main/java/module-info.java") << """
19+
module com.example {
20+
exports com.example;
21+
}
22+
"""
23+
24+
buildFile << """
25+
import org.elasticsearch.gradle.plugin.GenerateTestBuildInfoTask;
26+
27+
plugins {
28+
id 'java'
29+
id 'elasticsearch.test-build-info'
30+
}
31+
32+
repositories {
33+
mavenCentral()
34+
}
35+
36+
tasks.withType(GenerateTestBuildInfoTask.class) {
37+
componentName = 'example-component'
38+
outputFile = new File('build/generated-build-info/plugin-test-build-info.json')
39+
}
40+
"""
41+
42+
when:
43+
def result = gradleRunner('generateTestBuildInfo').build()
44+
def task = result.task(":generateTestBuildInfo")
45+
46+
47+
then:
48+
task.outcome == TaskOutcome.SUCCESS
49+
50+
def output = file("build/generated-build-info/plugin-test-build-info.json")
51+
output.exists() == true
52+
53+
def location = Map.of(
54+
"module", "com.example",
55+
"representative_class", "com/example/Example.class"
56+
)
57+
def expectedOutput = Map.of(
58+
"component", "example-component",
59+
"locations", List.of(location)
60+
)
61+
new ObjectMapper().readValue(output, Map.class) == expectedOutput
62+
}
63+
}

build-tools/src/main/java/org/elasticsearch/gradle/plugin/BasePluginBuildPlugin.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ private TaskProvider<Zip> createBundleTasks(final Project project, PluginPropert
121121
task.getIsLicensed().set(providerFactory.provider(extension::isLicensed));
122122

123123
var mainSourceSet = project.getExtensions().getByType(SourceSetContainer.class).getByName(SourceSet.MAIN_SOURCE_SET_NAME);
124-
FileCollection moduleInfoFile = mainSourceSet.getOutput().getAsFileTree().matching(p -> p.include("module-info.class"));
124+
FileCollection moduleInfoFile = mainSourceSet.getOutput()
125+
.getClassesDirs()
126+
.getAsFileTree()
127+
.matching(p -> p.include("module-info.class"));
125128
task.getModuleInfoFile().setFrom(moduleInfoFile);
126129

127130
});

0 commit comments

Comments
 (0)