Skip to content

Commit 8ac267c

Browse files
committed
review comments
1 parent d36b6a8 commit 8ac267c

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
*/
4242
public class EntitlementMetaTests extends ESTestCase {
4343
public void testSelfTestPasses() {
44-
assumeTrue("Not yet working in serverless", TestEntitlementsRule.isEnabledForTest());
44+
assumeTrue("Not yet working in serverless", TestEntitlementsRule.isEnabledForTests());
4545
Elasticsearch.entitlementSelfTest();
4646
}
4747

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public class WithEntitlementsOnTestCodeMetaTests extends ESTestCase {
3030
* is called from server code. The self-test should pass as usual.
3131
*/
3232
public void testSelfTestPasses() {
33-
assumeTrue("Not yet working in serverless", TestEntitlementsRule.isEnabledForTest());
33+
assumeTrue("Not yet working in serverless", TestEntitlementsRule.isEnabledForTests());
3434
Elasticsearch.entitlementSelfTest();
3535
}
3636

3737
@SuppressForbidden(reason = "Testing that a forbidden API is disallowed")
3838
public void testForbiddenActionDenied() {
39-
assumeTrue("Not yet working in serverless", TestEntitlementsRule.isEnabledForTest());
39+
assumeTrue("Not yet working in serverless", TestEntitlementsRule.isEnabledForTests());
4040
assertThrows(NotEntitledException.class, () -> Path.of(".").toRealPath());
4141
}
4242
}

test/framework/src/main/java/org/elasticsearch/entitlement/bootstrap/TestEntitlementsRule.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public class TestEntitlementsRule implements TestRule {
7474
static {
7575
PathLookup pathLookup = new TestPathLookup(BASE_DIR_PATHS);
7676
try {
77-
if (isEnabledForTest()) {
77+
if (isEnabledForTests()) {
7878
POLICY_MANAGER = createPolicyManager(pathLookup);
7979
loadAgent(POLICY_MANAGER, pathLookup);
8080
} else {
@@ -107,6 +107,7 @@ public Statement apply(Statement base, Description description) {
107107
public void evaluate() throws Throwable {
108108
if (active.compareAndSet(false, true)) {
109109
try {
110+
BASE_DIR_PATHS.keySet().retainAll(List.of(TEMP));
110111
POLICY_MANAGER.setActive(false == withoutEntitlements);
111112
POLICY_MANAGER.setTriviallyAllowingTestCode(false == withEntitlementsOnTestCode);
112113
if (entitledPackages != null) {
@@ -115,16 +116,12 @@ public void evaluate() throws Throwable {
115116
} else {
116117
POLICY_MANAGER.setEntitledTestPackages();
117118
}
118-
BASE_DIR_PATHS.keySet().retainAll(List.of(TEMP));
119119
POLICY_MANAGER.clearModuleEntitlementsCache();
120120
// evaluate the suite
121121
base.evaluate();
122122
} finally {
123-
POLICY_MANAGER.setActive(false);
124-
POLICY_MANAGER.setTriviallyAllowingTestCode(true);
125-
POLICY_MANAGER.setEntitledTestPackages();
126123
BASE_DIR_PATHS.keySet().retainAll(List.of(TEMP));
127-
POLICY_MANAGER.clearModuleEntitlementsCache();
124+
POLICY_MANAGER.resetAfterTest();
128125
active.set(false);
129126
}
130127
} else {
@@ -144,6 +141,7 @@ public void evaluate() throws Throwable {
144141
/**
145142
* Temporarily adds node paths based entitlements based on a node's {@code settings} and {@code configPath}
146143
* until the returned handle is closed.
144+
* @see PathLookup
147145
*/
148146
public Closeable addEntitledNodePaths(Settings settings, Path configPath) {
149147
if (POLICY_MANAGER == null) {
@@ -250,7 +248,7 @@ private static BiFunction<BaseDir, Collection<Path>, Collection<Path>> baseDirMo
250248
};
251249
}
252250

253-
public static boolean isEnabledForTest() {
251+
public static boolean isEnabledForTests() {
254252
return Booleans.parseBoolean(System.getProperty("es.entitlement.enableForTests", "false"));
255253
}
256254

@@ -272,7 +270,7 @@ private static TestPolicyManager createPolicyManager(PathLookup pathLookup) thro
272270

273271
String separator = System.getProperty("path.separator");
274272

275-
// In productions, plugins would have access to their respective bundle directories,
273+
// In production, plugins would have access to their respective bundle directories,
276274
// and so they'd be able to read from their jars. In testing, we approximate this
277275
// by considering the entire classpath to be "source paths" of all plugins. This
278276
// also has the effect of granting read access to everything on the test-only classpath,

test/framework/src/main/java/org/elasticsearch/entitlement/runtime/policy/TestPolicyManager.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ public TestPolicyManager(
5555
this.testOnlyClasspath = testOnlyClasspath;
5656
}
5757

58+
public final void resetAfterTest() {
59+
isActive = false;
60+
isTriviallyAllowingTestCode = true;
61+
entitledTestPackages = TEST_FRAMEWORK_PACKAGE_PREFIXES;
62+
clearModuleEntitlementsCache();
63+
}
64+
5865
public void setActive(boolean newValue) {
5966
this.isActive = newValue;
6067
}

0 commit comments

Comments
 (0)