Browse Source

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)
Jake Landis 5 years ago
parent
commit
55aeefa500

+ 0 - 97
buildSrc/src/main/groovy/org/elasticsearch/gradle/test/RestIntegTestTask.groovy

@@ -1,97 +0,0 @@
-/*
- * Licensed to Elasticsearch under one or more contributor
- * license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright
- * ownership. Elasticsearch licenses this file to you under
- * the Apache License, Version 2.0 (the "License"); you may
- * not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.elasticsearch.gradle.test
-
-import org.elasticsearch.gradle.testclusters.ElasticsearchCluster
-import org.elasticsearch.gradle.testclusters.RestTestRunnerTask
-import org.gradle.api.DefaultTask
-import org.gradle.api.Task
-import org.gradle.api.tasks.testing.Test
-
-/**
- * A wrapper task around setting up a cluster and running rest tests.
- */
-class RestIntegTestTask extends DefaultTask {
-
-    protected Test runner
-
-    RestIntegTestTask() {
-        runner = project.tasks.create("${name}Runner", RestTestRunnerTask.class)
-        super.dependsOn(runner)
-
-        ElasticsearchCluster cluster = project.testClusters.create(name)
-        runner.useCluster cluster
-
-        runner.include('**/*IT.class')
-        runner.systemProperty('tests.rest.load_packaged', 'false')
-
-        if (System.getProperty("tests.rest.cluster") == null) {
-            if (System.getProperty("tests.cluster") != null || System.getProperty("tests.clustername") != null) {
-                throw new IllegalArgumentException("tests.rest.cluster, tests.cluster, and tests.clustername must all be null or non-null")
-            }
-
-            runner.nonInputProperties.systemProperty('tests.rest.cluster', "${-> cluster.allHttpSocketURI.join(",")}")
-            runner.nonInputProperties.systemProperty('tests.cluster', "${-> cluster.transportPortURI}")
-            runner.nonInputProperties.systemProperty('tests.clustername', "${-> cluster.getName()}")
-        } else {
-            if (System.getProperty("tests.cluster") == null || System.getProperty("tests.clustername") == null) {
-                throw new IllegalArgumentException("tests.rest.cluster, tests.cluster, and tests.clustername must all be null or non-null")
-            }
-            // an external cluster was specified and all responsibility for cluster configuration is taken by the user
-            runner.systemProperty('tests.rest.cluster', System.getProperty("tests.rest.cluster"))
-            runner.systemProperty('test.cluster', System.getProperty("tests.cluster"))
-            runner.systemProperty('test.clustername', System.getProperty("tests.clustername"))
-        }
-
-        // this must run after all projects have been configured, so we know any project
-        // references can be accessed as a fully configured
-        project.gradle.projectsEvaluated {
-            if (enabled == false) {
-                runner.enabled = false
-                return // no need to add cluster formation tasks if the task won't run!
-            }
-        }
-    }
-
-    @Override
-    public Task dependsOn(Object... dependencies) {
-        runner.dependsOn(dependencies)
-        for (Object dependency : dependencies) {
-            if (dependency instanceof Fixture) {
-                runner.finalizedBy(((Fixture) dependency).getStopTask())
-            }
-        }
-        return this
-    }
-
-    @Override
-    public void setDependsOn(Iterable<?> dependencies) {
-        runner.setDependsOn(dependencies)
-        for (Object dependency : dependencies) {
-            if (dependency instanceof Fixture) {
-                runner.finalizedBy(((Fixture) dependency).getStopTask())
-            }
-        }
-    }
-
-    public void runner(Closure configure) {
-        project.tasks.getByName("${name}Runner").configure(configure)
-    }
-
-}

+ 11 - 1
buildSrc/src/main/java/org/elasticsearch/gradle/SystemPropertyCommandLineArgumentProvider.java

@@ -5,11 +5,16 @@ import org.gradle.process.CommandLineArgumentProvider;
 
 import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.function.Supplier;
 import java.util.stream.Collectors;
 
 public class SystemPropertyCommandLineArgumentProvider implements CommandLineArgumentProvider {
     private final Map<String, Object> systemProperties = new LinkedHashMap<>();
 
+    public void systemProperty(String key, Supplier<String> value) {
+        systemProperties.put(key, value);
+    }
+
     public void systemProperty(String key, Object value) {
         systemProperties.put(key, value);
     }
@@ -18,7 +23,12 @@ public class SystemPropertyCommandLineArgumentProvider implements CommandLineArg
     public Iterable<String> asArguments() {
         return systemProperties.entrySet()
             .stream()
-            .map(entry -> "-D" + entry.getKey() + "=" + entry.getValue())
+            .map(
+                entry -> "-D"
+                    + entry.getKey()
+                    + "="
+                    + (entry.getValue() instanceof Supplier ? ((Supplier) entry.getValue()).get() : entry.getValue())
+            )
             .collect(Collectors.toList());
     }
 

+ 4 - 3
buildSrc/src/main/groovy/org/elasticsearch/gradle/test/Fixture.groovy → buildSrc/src/main/java/org/elasticsearch/gradle/test/Fixture.java

@@ -7,7 +7,7 @@
  * not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *    http://www.apache.org/licenses/LICENSE-2.0
+ *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
@@ -16,7 +16,8 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.elasticsearch.gradle.test
+
+package org.elasticsearch.gradle.test;
 
 /**
  * Any object that can produce an accompanying stop task, meant to tear down
@@ -25,6 +26,6 @@ package org.elasticsearch.gradle.test
 public interface Fixture {
 
     /** A task which will stop this fixture. This should be used as a finalizedBy for any tasks that use the fixture. */
-    public Object getStopTask()
+    Object getStopTask();
 
 }

+ 104 - 0
buildSrc/src/main/java/org/elasticsearch/gradle/test/RestIntegTestTask.java

@@ -0,0 +1,104 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.elasticsearch.gradle.test;
+
+import org.elasticsearch.gradle.SystemPropertyCommandLineArgumentProvider;
+import org.elasticsearch.gradle.testclusters.ElasticsearchCluster;
+import org.elasticsearch.gradle.testclusters.RestTestRunnerTask;
+import org.elasticsearch.gradle.testclusters.TestClustersPlugin;
+import org.gradle.api.Action;
+import org.gradle.api.DefaultTask;
+import org.gradle.api.NamedDomainObjectContainer;
+import org.gradle.api.Project;
+import org.gradle.api.Task;
+
+public class RestIntegTestTask extends DefaultTask {
+
+    protected RestTestRunnerTask runner;
+    private static final String TESTS_REST_CLUSTER = "tests.rest.cluster";
+    private static final String TESTS_CLUSTER = "tests.cluster";
+    private static final String TESTS_CLUSTER_NAME = "tests.clustername";
+
+    public RestIntegTestTask() {
+        Project project = getProject();
+        String name = getName();
+        runner = project.getTasks().create(name + "Runner", RestTestRunnerTask.class);
+        super.dependsOn(runner);
+        @SuppressWarnings("unchecked")
+        NamedDomainObjectContainer<ElasticsearchCluster> testClusters = (NamedDomainObjectContainer<ElasticsearchCluster>) project
+            .getExtensions()
+            .getByName(TestClustersPlugin.EXTENSION_NAME);
+        ElasticsearchCluster cluster = testClusters.create(name);
+        runner.useCluster(cluster);
+        runner.include("**/*IT.class");
+        runner.systemProperty("tests.rest.load_packaged", Boolean.FALSE.toString());
+        if (System.getProperty(TESTS_REST_CLUSTER) == null) {
+            if (System.getProperty(TESTS_CLUSTER) != null || System.getProperty(TESTS_CLUSTER_NAME) != null) {
+                throw new IllegalArgumentException(
+                    String.format("%s, %s, and %s must all be null or non-null", TESTS_REST_CLUSTER, TESTS_CLUSTER, TESTS_CLUSTER_NAME)
+                );
+            }
+            SystemPropertyCommandLineArgumentProvider runnerNonInputProperties = (SystemPropertyCommandLineArgumentProvider) runner
+                .getExtensions()
+                .getByName("nonInputProperties");
+            runnerNonInputProperties.systemProperty(TESTS_REST_CLUSTER, () -> String.join(",", cluster.getAllHttpSocketURI()));
+            runnerNonInputProperties.systemProperty(TESTS_CLUSTER, () -> String.join(",", cluster.getAllTransportPortURI()));
+            runnerNonInputProperties.systemProperty(TESTS_CLUSTER_NAME, cluster::getName);
+        } else {
+            if (System.getProperty(TESTS_CLUSTER) == null || System.getProperty(TESTS_CLUSTER_NAME) == null) {
+                throw new IllegalArgumentException(
+                    String.format("%s, %s, and %s must all be null or non-null", TESTS_REST_CLUSTER, TESTS_CLUSTER, TESTS_CLUSTER_NAME)
+                );
+            }
+        }
+        // this must run after all projects have been configured, so we know any project
+        // references can be accessed as a fully configured
+        project.getGradle().projectsEvaluated(x -> {
+            if (isEnabled() == false) {
+                runner.setEnabled(false);
+            }
+        });
+    }
+
+    @Override
+    public Task dependsOn(Object... dependencies) {
+        runner.dependsOn(dependencies);
+        for (Object dependency : dependencies) {
+            if (dependency instanceof Fixture) {
+                runner.finalizedBy(((Fixture) dependency).getStopTask());
+            }
+        }
+        return this;
+    }
+
+    @Override
+    public void setDependsOn(Iterable<?> dependencies) {
+        runner.setDependsOn(dependencies);
+        for (Object dependency : dependencies) {
+            if (dependency instanceof Fixture) {
+                runner.finalizedBy(((Fixture) dependency).getStopTask());
+            }
+        }
+    }
+
+    public void runner(Action<? super RestTestRunnerTask> configure) {
+        configure.execute(runner);
+    }
+}