Skip to content

Commit 5a0d5c5

Browse files
Custom Gradle plugin to leverage java agent (#17900)
Signed-off-by: Prudhvi Godithi <[email protected]>
1 parent b37fad8 commit 5a0d5c5

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.gradle.agent;
10+
11+
import org.gradle.api.Plugin;
12+
import org.gradle.api.Project;
13+
import org.gradle.api.artifacts.Configuration;
14+
import org.gradle.api.tasks.Copy;
15+
import org.gradle.api.tasks.TaskProvider;
16+
import org.gradle.api.tasks.testing.Test;
17+
18+
import java.io.File;
19+
import java.util.Objects;
20+
21+
/**
22+
* Gradle plugin to automatically configure the OpenSearch Java agent
23+
* for test tasks in OpenSearch plugin projects.
24+
*/
25+
public class JavaAgent implements Plugin<Project> {
26+
27+
/**
28+
* Plugin implementation that sets up java agent configuration and applies it to test tasks.
29+
*/
30+
@Override
31+
public void apply(Project project) {
32+
Configuration agentConfiguration = project.getConfigurations().findByName("agent");
33+
if (agentConfiguration == null) {
34+
agentConfiguration = project.getConfigurations().create("agent");
35+
}
36+
37+
project.afterEvaluate(p -> {
38+
String opensearchVersion = getOpensearchVersion(p);
39+
p.getDependencies().add("agent", "org.opensearch:opensearch-agent-bootstrap:" + opensearchVersion);
40+
p.getDependencies().add("agent", "org.opensearch:opensearch-agent:" + opensearchVersion);
41+
});
42+
43+
Configuration finalAgentConfiguration = agentConfiguration;
44+
TaskProvider<Copy> prepareJavaAgent = project.getTasks().register("prepareJavaAgent", Copy.class, task -> {
45+
task.from(finalAgentConfiguration);
46+
task.into(new File(project.getBuildDir(), "agent"));
47+
});
48+
49+
project.getTasks().withType(Test.class).configureEach(testTask -> {
50+
testTask.dependsOn(prepareJavaAgent);
51+
52+
final String opensearchVersion = getOpensearchVersion(project);
53+
54+
testTask.doFirst(task -> {
55+
File agentJar = new File(project.getBuildDir(), "agent/opensearch-agent-" + opensearchVersion + ".jar");
56+
57+
testTask.jvmArgs("-javaagent:" + agentJar.getAbsolutePath());
58+
});
59+
});
60+
}
61+
62+
/**
63+
* Gets the OpenSearch version from project properties, with a fallback default.
64+
*
65+
* @param project The Gradle project
66+
* @return The OpenSearch version to use
67+
*/
68+
private String getOpensearchVersion(Project project) {
69+
return Objects.requireNonNull(project.property("opensearch_version")).toString();
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
# The OpenSearch Contributors require contributions made to
5+
# this file be licensed under the Apache-2.0 license or a
6+
# compatible open source license.
7+
#
8+
9+
implementation-class=org.opensearch.gradle.agent.JavaAgent
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.gradle.agent;
10+
11+
import org.opensearch.gradle.test.GradleUnitTestCase;
12+
import org.gradle.api.Project;
13+
import org.gradle.api.artifacts.Configuration;
14+
import org.gradle.api.tasks.Copy;
15+
import org.gradle.testfixtures.ProjectBuilder;
16+
import org.junit.After;
17+
import org.junit.Before;
18+
import org.junit.Test;
19+
import org.junit.rules.TemporaryFolder;
20+
21+
import java.io.File;
22+
import java.io.IOException;
23+
24+
public class JavaAgentTests extends GradleUnitTestCase {
25+
private TemporaryFolder projectDir;
26+
private final String PREPARE_JAVA_AGENT_TASK = "prepareJavaAgent";
27+
28+
@Before
29+
public void setUp() throws IOException {
30+
projectDir = new TemporaryFolder();
31+
projectDir.create();
32+
}
33+
34+
@After
35+
public void tearDown() {
36+
projectDir.delete();
37+
}
38+
39+
/**
40+
* This test is used to verify that adding the 'opensearch.java-agent' to the project
41+
* creates the necessary agent configuration and tasks. This is basically
42+
* a behavioral test of the {@link JavaAgent#apply(Project)} method.
43+
*/
44+
@Test
45+
public void applyJavaAgentPlugin() {
46+
// Create an empty project and apply the JavaAgent plugin
47+
Project project = ProjectBuilder.builder().build();
48+
project.getPluginManager().apply(JavaAgent.class);
49+
50+
// Verify the agent configuration was created
51+
Configuration agentConfig = project.getConfigurations().findByName("agent");
52+
assertNotNull("Agent configuration should be created", agentConfig);
53+
54+
// Verify the prepareJavaAgent task was created and is of the right type
55+
assertNotNull("prepareJavaAgent task should be created", project.getTasks().findByName(PREPARE_JAVA_AGENT_TASK));
56+
assertTrue("prepareJavaAgent task should be of type Copy", project.getTasks().findByName(PREPARE_JAVA_AGENT_TASK) instanceof Copy);
57+
58+
// Verify the destination directory of the Copy task
59+
Copy prepareTask = (Copy) project.getTasks().findByName(PREPARE_JAVA_AGENT_TASK);
60+
assertEquals(
61+
"Destination directory should be build/agent",
62+
new File(project.getBuildDir(), "agent"),
63+
prepareTask.getDestinationDir()
64+
);
65+
}
66+
}

0 commit comments

Comments
 (0)