|
| 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