9
9
10
10
package org .elasticsearch .bootstrap ;
11
11
12
+ import org .elasticsearch .bootstrap .agent .TestAPMAgent ;
12
13
import org .elasticsearch .entitlement .runtime .policy .PolicyManager .PolicyScope ;
13
14
import org .elasticsearch .plugins .PluginBundle ;
14
15
import org .elasticsearch .plugins .PluginDescriptor ;
@@ -39,45 +40,38 @@ public class ScopeResolverTests extends ESTestCase {
39
40
/**
40
41
* A test agent package name for use in tests.
41
42
*/
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 () ;
43
44
44
45
private record TestPluginLayer (PluginBundle pluginBundle , ClassLoader pluginClassLoader , ModuleLayer pluginModuleLayer )
45
46
implements
46
47
PluginsLoader .PluginLayer {}
47
48
48
- public void testBootLayer () throws ClassNotFoundException {
49
+ public void testBootLayer () {
49
50
ScopeResolver scopeResolver = ScopeResolver .create (Stream .empty (), TEST_AGENTS_PACKAGE_NAME );
50
51
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
+ );
58
64
}
59
65
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 );
74
68
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 ));
78
72
}
79
73
80
- public void testResolveMultipleModularPlugins () throws IOException , ClassNotFoundException {
74
+ public void testModularPlugins () throws IOException , ClassNotFoundException {
81
75
final Path home = createTempDir ();
82
76
83
77
Path jar1 = createModularPluginJar (home , "plugin1" , "module.one" , "p" , "A" );
@@ -128,7 +122,7 @@ public void testResolveReferencedModulesInModularPlugins() throws IOException, C
128
122
assertEquals (PolicyScope .plugin ("plugin2" , "module.two" ), scopeResolver .resolveClassToScope (loader .loadClass ("q.B" )));
129
123
}
130
124
131
- public void testResolveMultipleNonModularPlugins () throws IOException , ClassNotFoundException {
125
+ public void testNonModularPlugins () throws IOException , ClassNotFoundException {
132
126
final Path home = createTempDir ();
133
127
134
128
Path jar1 = createNonModularPluginJar (home , "plugin1" , "p" , "A" );
@@ -148,24 +142,6 @@ public void testResolveMultipleNonModularPlugins() throws IOException, ClassNotF
148
142
}
149
143
}
150
144
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
-
169
145
private static URLClassLoader createClassLoader (Path jar ) throws MalformedURLException {
170
146
return new URLClassLoader (new URL [] { jar .toUri ().toURL () });
171
147
}
0 commit comments