Skip to content

Commit 55aeefa

Browse files
authored
Convert RestIntegTestTask to Java (#54528)
This commit migrates the RestIntegTestTask from groovy to Java. No changes to logic should be included, however the following changes are needed: * Move Fixture interface to Java (Java can not depend on Groovy classes) * Support lazy evaluation of non-input System parameters (can not use Groovy strings) * Use constants for system property names * Remove dead System property pass through code (the build plugin does this already)
1 parent 97a890a commit 55aeefa

File tree

4 files changed

+119
-101
lines changed

4 files changed

+119
-101
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/test/RestIntegTestTask.groovy

Lines changed: 0 additions & 97 deletions
This file was deleted.

buildSrc/src/main/java/org/elasticsearch/gradle/SystemPropertyCommandLineArgumentProvider.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55

66
import java.util.LinkedHashMap;
77
import java.util.Map;
8+
import java.util.function.Supplier;
89
import java.util.stream.Collectors;
910

1011
public class SystemPropertyCommandLineArgumentProvider implements CommandLineArgumentProvider {
1112
private final Map<String, Object> systemProperties = new LinkedHashMap<>();
1213

14+
public void systemProperty(String key, Supplier<String> value) {
15+
systemProperties.put(key, value);
16+
}
17+
1318
public void systemProperty(String key, Object value) {
1419
systemProperties.put(key, value);
1520
}
@@ -18,7 +23,12 @@ public void systemProperty(String key, Object value) {
1823
public Iterable<String> asArguments() {
1924
return systemProperties.entrySet()
2025
.stream()
21-
.map(entry -> "-D" + entry.getKey() + "=" + entry.getValue())
26+
.map(
27+
entry -> "-D"
28+
+ entry.getKey()
29+
+ "="
30+
+ (entry.getValue() instanceof Supplier ? ((Supplier) entry.getValue()).get() : entry.getValue())
31+
)
2232
.collect(Collectors.toList());
2333
}
2434

buildSrc/src/main/groovy/org/elasticsearch/gradle/test/Fixture.groovy renamed to buildSrc/src/main/java/org/elasticsearch/gradle/test/Fixture.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* not use this file except in compliance with the License.
88
* You may obtain a copy of the License at
99
*
10-
* http://www.apache.org/licenses/LICENSE-2.0
10+
* http://www.apache.org/licenses/LICENSE-2.0
1111
*
1212
* Unless required by applicable law or agreed to in writing,
1313
* software distributed under the License is distributed on an
@@ -16,7 +16,8 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.elasticsearch.gradle.test
19+
20+
package org.elasticsearch.gradle.test;
2021

2122
/**
2223
* Any object that can produce an accompanying stop task, meant to tear down
@@ -25,6 +26,6 @@
2526
public interface Fixture {
2627

2728
/** A task which will stop this fixture. This should be used as a finalizedBy for any tasks that use the fixture. */
28-
public Object getStopTask()
29+
Object getStopTask();
2930

3031
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.gradle.test;
21+
22+
import org.elasticsearch.gradle.SystemPropertyCommandLineArgumentProvider;
23+
import org.elasticsearch.gradle.testclusters.ElasticsearchCluster;
24+
import org.elasticsearch.gradle.testclusters.RestTestRunnerTask;
25+
import org.elasticsearch.gradle.testclusters.TestClustersPlugin;
26+
import org.gradle.api.Action;
27+
import org.gradle.api.DefaultTask;
28+
import org.gradle.api.NamedDomainObjectContainer;
29+
import org.gradle.api.Project;
30+
import org.gradle.api.Task;
31+
32+
public class RestIntegTestTask extends DefaultTask {
33+
34+
protected RestTestRunnerTask runner;
35+
private static final String TESTS_REST_CLUSTER = "tests.rest.cluster";
36+
private static final String TESTS_CLUSTER = "tests.cluster";
37+
private static final String TESTS_CLUSTER_NAME = "tests.clustername";
38+
39+
public RestIntegTestTask() {
40+
Project project = getProject();
41+
String name = getName();
42+
runner = project.getTasks().create(name + "Runner", RestTestRunnerTask.class);
43+
super.dependsOn(runner);
44+
@SuppressWarnings("unchecked")
45+
NamedDomainObjectContainer<ElasticsearchCluster> testClusters = (NamedDomainObjectContainer<ElasticsearchCluster>) project
46+
.getExtensions()
47+
.getByName(TestClustersPlugin.EXTENSION_NAME);
48+
ElasticsearchCluster cluster = testClusters.create(name);
49+
runner.useCluster(cluster);
50+
runner.include("**/*IT.class");
51+
runner.systemProperty("tests.rest.load_packaged", Boolean.FALSE.toString());
52+
if (System.getProperty(TESTS_REST_CLUSTER) == null) {
53+
if (System.getProperty(TESTS_CLUSTER) != null || System.getProperty(TESTS_CLUSTER_NAME) != null) {
54+
throw new IllegalArgumentException(
55+
String.format("%s, %s, and %s must all be null or non-null", TESTS_REST_CLUSTER, TESTS_CLUSTER, TESTS_CLUSTER_NAME)
56+
);
57+
}
58+
SystemPropertyCommandLineArgumentProvider runnerNonInputProperties = (SystemPropertyCommandLineArgumentProvider) runner
59+
.getExtensions()
60+
.getByName("nonInputProperties");
61+
runnerNonInputProperties.systemProperty(TESTS_REST_CLUSTER, () -> String.join(",", cluster.getAllHttpSocketURI()));
62+
runnerNonInputProperties.systemProperty(TESTS_CLUSTER, () -> String.join(",", cluster.getAllTransportPortURI()));
63+
runnerNonInputProperties.systemProperty(TESTS_CLUSTER_NAME, cluster::getName);
64+
} else {
65+
if (System.getProperty(TESTS_CLUSTER) == null || System.getProperty(TESTS_CLUSTER_NAME) == null) {
66+
throw new IllegalArgumentException(
67+
String.format("%s, %s, and %s must all be null or non-null", TESTS_REST_CLUSTER, TESTS_CLUSTER, TESTS_CLUSTER_NAME)
68+
);
69+
}
70+
}
71+
// this must run after all projects have been configured, so we know any project
72+
// references can be accessed as a fully configured
73+
project.getGradle().projectsEvaluated(x -> {
74+
if (isEnabled() == false) {
75+
runner.setEnabled(false);
76+
}
77+
});
78+
}
79+
80+
@Override
81+
public Task dependsOn(Object... dependencies) {
82+
runner.dependsOn(dependencies);
83+
for (Object dependency : dependencies) {
84+
if (dependency instanceof Fixture) {
85+
runner.finalizedBy(((Fixture) dependency).getStopTask());
86+
}
87+
}
88+
return this;
89+
}
90+
91+
@Override
92+
public void setDependsOn(Iterable<?> dependencies) {
93+
runner.setDependsOn(dependencies);
94+
for (Object dependency : dependencies) {
95+
if (dependency instanceof Fixture) {
96+
runner.finalizedBy(((Fixture) dependency).getStopTask());
97+
}
98+
}
99+
}
100+
101+
public void runner(Action<? super RestTestRunnerTask> configure) {
102+
configure.execute(runner);
103+
}
104+
}

0 commit comments

Comments
 (0)