3434import java .io .File ;
3535import java .util .List ;
3636import java .util .Map ;
37+ import java .util .Set ;
3738import java .util .stream .Stream ;
3839
3940import javax .inject .Inject ;
@@ -50,6 +51,8 @@ public abstract class ElasticsearchTestBasePlugin implements Plugin<Project> {
5051
5152 public static final String DUMP_OUTPUT_ON_FAILURE_PROP_NAME = "dumpOutputOnFailure" ;
5253
54+ public static final Set <String > TEST_TASKS_WITH_ENTITLEMENTS = Set .of ("test" , "internalClusterTest" );
55+
5356 @ Inject
5457 protected abstract ProviderFactory getProviderFactory ();
5558
@@ -178,10 +181,17 @@ public void execute(Task t) {
178181 SourceSetContainer sourceSets = project .getExtensions ().getByType (SourceSetContainer .class );
179182 SourceSet mainSourceSet = sourceSets .findByName (SourceSet .MAIN_SOURCE_SET_NAME );
180183 SourceSet testSourceSet = sourceSets .findByName (SourceSet .TEST_SOURCE_SET_NAME );
181- if ("test" .equals (test .getName ()) && mainSourceSet != null && testSourceSet != null ) {
184+ SourceSet internalClusterTestSourceSet = sourceSets .findByName ("internalClusterTest" );
185+
186+ if (TEST_TASKS_WITH_ENTITLEMENTS .contains (test .getName ()) && mainSourceSet != null && testSourceSet != null ) {
182187 FileCollection mainRuntime = mainSourceSet .getRuntimeClasspath ();
183188 FileCollection testRuntime = testSourceSet .getRuntimeClasspath ();
184- FileCollection testOnlyFiles = testRuntime .minus (mainRuntime );
189+ FileCollection internalClusterTestRuntime = "internalClusterTest" .equals (test .getName ())
190+ && internalClusterTestSourceSet == null
191+ ? project .files () // empty file collection
192+ : internalClusterTestSourceSet .getRuntimeClasspath ();
193+ FileCollection testOnlyFiles = testRuntime .plus (internalClusterTestRuntime ).minus (mainRuntime );
194+
185195 test .doFirst (task -> test .environment ("es.entitlement.testOnlyPath" , testOnlyFiles .getAsPath ()));
186196 }
187197
@@ -241,16 +251,18 @@ public void execute(Task t) {
241251 * Computes and sets the {@code --patch-module=java.base} and {@code --add-opens=java.base} JVM command line options.
242252 */
243253 private void configureJavaBaseModuleOptions (Project project ) {
244- project .getTasks ().withType (Test .class ).matching (task -> task .getName ().equals ("test" )).configureEach (test -> {
245- FileCollection patchedImmutableCollections = patchedImmutableCollections (project );
254+ project .getTasks ().withType (Test .class ).configureEach (test -> {
255+ // patch immutable collections only for "test" task
256+ FileCollection patchedImmutableCollections = test .getName ().equals ("test" ) ? patchedImmutableCollections (project ) : null ;
246257 if (patchedImmutableCollections != null ) {
247258 test .getInputs ().files (patchedImmutableCollections );
248259 test .systemProperty ("tests.hackImmutableCollections" , "true" );
249260 }
250261
251- FileCollection entitlementBridge = entitlementBridge (project );
262+ FileCollection entitlementBridge = TEST_TASKS_WITH_ENTITLEMENTS . contains ( test . getName ()) ? entitlementBridge (project ) : null ;
252263 if (entitlementBridge != null ) {
253264 test .getInputs ().files (entitlementBridge );
265+ test .systemProperty ("es.entitlement.enableForTests" , "true" );
254266 }
255267
256268 test .getJvmArgumentProviders ().add (() -> {
@@ -312,7 +324,9 @@ private static void configureEntitlements(Project project) {
312324 }
313325 FileCollection bridgeFiles = bridgeConfig ;
314326
315- project .getTasks ().withType (Test .class ).configureEach (test -> {
327+ project .getTasks ().withType (Test .class )
328+ .matching (test -> TEST_TASKS_WITH_ENTITLEMENTS .contains (test .getName ()))
329+ .configureEach (test -> {
316330 // See also SystemJvmOptions.maybeAttachEntitlementAgent.
317331
318332 // Agent
0 commit comments