Skip to content

Commit b19289e

Browse files
committed
Clean and simplify ScopeResolverTests
1 parent eb7339a commit b19289e

File tree

2 files changed

+37
-46
lines changed

2 files changed

+37
-46
lines changed

server/src/test/java/org/elasticsearch/bootstrap/ScopeResolverTests.java

+22-46
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.bootstrap;
1111

12+
import org.elasticsearch.bootstrap.agent.TestAPMAgent;
1213
import org.elasticsearch.entitlement.runtime.policy.PolicyManager.PolicyScope;
1314
import org.elasticsearch.plugins.PluginBundle;
1415
import org.elasticsearch.plugins.PluginDescriptor;
@@ -39,45 +40,38 @@ public class ScopeResolverTests extends ESTestCase {
3940
/**
4041
* A test agent package name for use in tests.
4142
*/
42-
private static final String TEST_AGENTS_PACKAGE_NAME = "org.elasticsearch.entitlement.runtime.policy.agent";
43+
private static final String TEST_AGENTS_PACKAGE_NAME = TestAPMAgent.class.getPackage().getName();
4344

4445
private record TestPluginLayer(PluginBundle pluginBundle, ClassLoader pluginClassLoader, ModuleLayer pluginModuleLayer)
4546
implements
4647
PluginsLoader.PluginLayer {}
4748

48-
public void testBootLayer() throws ClassNotFoundException {
49+
public void testBootLayer() {
4950
ScopeResolver scopeResolver = ScopeResolver.create(Stream.empty(), TEST_AGENTS_PACKAGE_NAME);
5051

51-
// Tests do not run modular, so we cannot use a server class.
52-
// But we know that in production code the server module and its classes are in the boot layer.
53-
// So we use an arbitrary module in the boot layer, and an arbitrary class from that module (not java.base -- it is
54-
// loaded too early) to mimic a class that would be in the server module.
55-
var mockServerClass = ModuleLayer.boot().findLoader("jdk.httpserver").loadClass("com.sun.net.httpserver.HttpServer");
56-
57-
assertEquals(PolicyScope.server("jdk.httpserver"), scopeResolver.resolveClassToScope(mockServerClass));
52+
// Note that String is not actually a server class, but a JDK class;
53+
// however, that distinction is made by PolicyManager, not by ScopeResolver.
54+
assertEquals(
55+
"Named module in boot layer is a server module",
56+
PolicyScope.server("java.base"),
57+
scopeResolver.resolveClassToScope(String.class)
58+
);
59+
assertEquals(
60+
"Unnamed module in boot layer is unknown",
61+
PolicyScope.unknown(ALL_UNNAMED),
62+
scopeResolver.resolveClassToScope(ScopeResolver.class)
63+
);
5864
}
5965

60-
public void testResolveModularPlugin() throws IOException, ClassNotFoundException {
61-
String moduleName = "modular.plugin";
62-
String pluginName = "modular-plugin";
63-
64-
final Path home = createTempDir();
65-
66-
Path jar = createModularPluginJar(home, pluginName, moduleName, "p", "A");
67-
68-
var layer = createModuleLayer(moduleName, jar);
69-
var loader = layer.findLoader(moduleName);
70-
71-
PluginBundle bundle = createMockBundle(pluginName, moduleName, "p.A");
72-
Stream<PluginsLoader.PluginLayer> pluginLayers = Stream.of(new TestPluginLayer(bundle, loader, layer));
73-
ScopeResolver scopeResolver = ScopeResolver.create(pluginLayers, TEST_AGENTS_PACKAGE_NAME);
66+
public void testAPMAgent() {
67+
ScopeResolver scopeResolver = ScopeResolver.create(Stream.empty(), TEST_AGENTS_PACKAGE_NAME);
7468

75-
assertEquals(PolicyScope.plugin(pluginName, moduleName), scopeResolver.resolveClassToScope(loader.loadClass("p.A")));
76-
assertEquals(PolicyScope.unknown(ALL_UNNAMED), scopeResolver.resolveClassToScope(ScopeResolver.class));
77-
assertEquals(PolicyScope.server("java.base"), scopeResolver.resolveClassToScope(String.class));
69+
// Note that java agents are always non-modular.
70+
// See https://bugs.openjdk.org/browse/JDK-6932391
71+
assertEquals(PolicyScope.apmAgent(ALL_UNNAMED), scopeResolver.resolveClassToScope(TestAPMAgent.class));
7872
}
7973

80-
public void testResolveMultipleModularPlugins() throws IOException, ClassNotFoundException {
74+
public void testModularPlugins() throws IOException, ClassNotFoundException {
8175
final Path home = createTempDir();
8276

8377
Path jar1 = createModularPluginJar(home, "plugin1", "module.one", "p", "A");
@@ -128,7 +122,7 @@ public void testResolveReferencedModulesInModularPlugins() throws IOException, C
128122
assertEquals(PolicyScope.plugin("plugin2", "module.two"), scopeResolver.resolveClassToScope(loader.loadClass("q.B")));
129123
}
130124

131-
public void testResolveMultipleNonModularPlugins() throws IOException, ClassNotFoundException {
125+
public void testNonModularPlugins() throws IOException, ClassNotFoundException {
132126
final Path home = createTempDir();
133127

134128
Path jar1 = createNonModularPluginJar(home, "plugin1", "p", "A");
@@ -148,24 +142,6 @@ public void testResolveMultipleNonModularPlugins() throws IOException, ClassNotF
148142
}
149143
}
150144

151-
public void testResolveNonModularPlugin() throws IOException, ClassNotFoundException {
152-
String pluginName = "non-modular-plugin";
153-
154-
final Path home = createTempDir();
155-
156-
Path jar = createNonModularPluginJar(home, pluginName, "p", "A");
157-
158-
try (var loader = createClassLoader(jar)) {
159-
PluginBundle bundle = createMockBundle(pluginName, null, "p.A");
160-
Stream<PluginsLoader.PluginLayer> pluginLayers = Stream.of(new TestPluginLayer(bundle, loader, ModuleLayer.boot()));
161-
ScopeResolver scopeResolver = ScopeResolver.create(pluginLayers, TEST_AGENTS_PACKAGE_NAME);
162-
163-
assertEquals(PolicyScope.plugin(pluginName, ALL_UNNAMED), scopeResolver.resolveClassToScope(loader.loadClass("p.A")));
164-
assertEquals(PolicyScope.unknown(ALL_UNNAMED), scopeResolver.resolveClassToScope(ScopeResolver.class));
165-
assertEquals(PolicyScope.server("java.base"), scopeResolver.resolveClassToScope(String.class));
166-
}
167-
}
168-
169145
private static URLClassLoader createClassLoader(Path jar) throws MalformedURLException {
170146
return new URLClassLoader(new URL[] { jar.toUri().toURL() });
171147
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.bootstrap.agent;
11+
12+
/**
13+
* A test double for the APM agent
14+
*/
15+
public class TestAPMAgent {}

0 commit comments

Comments
 (0)