Skip to content

Commit fc49559

Browse files
committed
Add CONFIG to TestPathLookup
1 parent 8f7e5e6 commit fc49559

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

test/framework/src/main/java/org/elasticsearch/bootstrap/BootstrapForTesting.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.common.network.IfConfig;
1515
import org.elasticsearch.common.settings.Settings;
1616
import org.elasticsearch.core.Booleans;
17+
import org.elasticsearch.core.Nullable;
1718
import org.elasticsearch.core.PathUtils;
1819
import org.elasticsearch.entitlement.bootstrap.TestEntitlementBootstrap;
1920
import org.elasticsearch.jdk.JarHell;
@@ -75,12 +76,20 @@ public class BootstrapForTesting {
7576

7677
// Fire up entitlements
7778
try {
78-
TestEntitlementBootstrap.bootstrap(javaTmpDir);
79+
TestEntitlementBootstrap.bootstrap(javaTmpDir, maybePath(System.getProperty("tests.config")));
7980
} catch (IOException e) {
8081
throw new IllegalStateException(e.getClass().getSimpleName() + " while initializing entitlements for tests", e);
8182
}
8283
}
8384

85+
private static @Nullable Path maybePath(String str) {
86+
if (str == null) {
87+
return null;
88+
} else {
89+
return Path.of(str);
90+
}
91+
}
92+
8493
// does nothing, just easy way to make sure the class is loaded.
8594
public static void ensureInitialized() {}
8695
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.bootstrap.TestBuildInfo;
1313
import org.elasticsearch.bootstrap.TestBuildInfoParser;
1414
import org.elasticsearch.bootstrap.TestScopeResolver;
15+
import org.elasticsearch.core.Nullable;
1516
import org.elasticsearch.core.Strings;
1617
import org.elasticsearch.core.SuppressForbidden;
1718
import org.elasticsearch.entitlement.initialization.EntitlementInitialization;
@@ -38,6 +39,9 @@
3839
import java.util.TreeSet;
3940
import java.util.stream.Collectors;
4041

42+
import static org.elasticsearch.entitlement.runtime.policy.PathLookup.BaseDir.CONFIG;
43+
import static org.elasticsearch.entitlement.runtime.policy.PathLookup.BaseDir.TEMP;
44+
4145
public class TestEntitlementBootstrap {
4246

4347
private static final Logger logger = LogManager.getLogger(TestEntitlementBootstrap.class);
@@ -47,17 +51,25 @@ public class TestEntitlementBootstrap {
4751
/**
4852
* Activates entitlement checking in tests.
4953
*/
50-
public static void bootstrap(Path tempDir) throws IOException {
54+
public static void bootstrap(@Nullable Path tempDir, @Nullable Path configDir) throws IOException {
5155
if (isEnabledForTest() == false) {
5256
return;
5357
}
54-
TestPathLookup pathLookup = new TestPathLookup(List.of(tempDir));
58+
TestPathLookup pathLookup = new TestPathLookup(Map.of(TEMP, zeroOrOne(tempDir), CONFIG, zeroOrOne(configDir)));
5559
policyManager = createPolicyManager(pathLookup);
5660
EntitlementInitialization.initializeArgs = new EntitlementInitialization.InitializeArgs(pathLookup, Set.of(), policyManager);
5761
logger.debug("Loading entitlement agent");
5862
EntitlementBootstrap.loadAgent(EntitlementBootstrap.findAgentJar(), EntitlementInitialization.class.getName());
5963
}
6064

65+
private static <T> List<T> zeroOrOne(T item) {
66+
if (item == null) {
67+
return List.of();
68+
} else {
69+
return List.of(item);
70+
}
71+
}
72+
6173
public static boolean isEnabledForTest() {
6274
return Boolean.getBoolean("es.entitlement.enableForTests");
6375
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
package org.elasticsearch.entitlement.runtime.policy;
1111

1212
import java.nio.file.Path;
13+
import java.util.Collection;
1314
import java.util.List;
15+
import java.util.Map;
1416
import java.util.stream.Stream;
1517

1618
public class TestPathLookup implements PathLookup {
17-
final List<Path> tempDirPaths;
19+
final Map<BaseDir, Collection<Path>> tempDirPaths;
1820

19-
public TestPathLookup(List<Path> tempDirPaths) {
21+
public TestPathLookup(Map<BaseDir, Collection<Path>> tempDirPaths) {
2022
this.tempDirPaths = tempDirPaths;
2123
}
2224

@@ -27,10 +29,7 @@ public Path pidFile() {
2729

2830
@Override
2931
public Stream<Path> getBaseDirPaths(BaseDir baseDir) {
30-
return switch (baseDir) {
31-
case TEMP -> tempDirPaths.stream();
32-
default -> Stream.empty();
33-
};
32+
return tempDirPaths.getOrDefault(baseDir, List.of()).stream();
3433
}
3534

3635
@Override

test/framework/src/test/java/org/elasticsearch/entitlement/runtime/policy/TestPolicyManagerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void setupPolicyManager() {
3131
Map.of(),
3232
c -> new PolicyScope(PLUGIN, "example-plugin" + scopeCounter.incrementAndGet(), "org.example.module"),
3333
Map.of(),
34-
new TestPathLookup(List.of()),
34+
new TestPathLookup(Map.of()),
3535
List.of()
3636
);
3737
policyManager.setActive(true);

0 commit comments

Comments
 (0)