Skip to content

Commit 16a4e3d

Browse files
committed
Make InternalYamlRestTestPlugin test suites compatible
1 parent 8cfe22b commit 16a4e3d

File tree

5 files changed

+188
-94
lines changed

5 files changed

+188
-94
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.elasticsearch.gradle.internal.test.rest;
2+
3+
import org.gradle.api.artifacts.ConfigurationContainer;
4+
import org.gradle.api.internal.tasks.TaskDependencyFactory;
5+
import org.gradle.api.plugins.jvm.internal.DefaultJvmTestSuite;
6+
import org.gradle.api.tasks.SourceSetContainer;
7+
8+
public abstract class DefaultYamlRestTestSuite extends DefaultJvmTestSuite implements YamlRestTestSuite {
9+
public DefaultYamlRestTestSuite(
10+
String name,
11+
SourceSetContainer sourceSets,
12+
ConfigurationContainer configurations,
13+
TaskDependencyFactory taskDependencyFactory
14+
) {
15+
super(name, sourceSets, configurations, taskDependencyFactory);
16+
}
17+
}

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/InternalYamlRestTestPlugin.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99

1010
package org.elasticsearch.gradle.internal.test.rest;
1111

12-
import org.elasticsearch.gradle.internal.test.RestIntegTestTask;
1312
import org.elasticsearch.gradle.util.GradleUtils;
13+
import org.gradle.api.Action;
1414
import org.gradle.api.Plugin;
1515
import org.gradle.api.Project;
1616
import org.gradle.api.plugins.JavaBasePlugin;
17+
import org.gradle.api.plugins.JvmTestSuitePlugin;
18+
import org.gradle.api.plugins.jvm.JvmTestSuite;
19+
import org.gradle.api.plugins.jvm.JvmTestSuiteTarget;
1720
import org.gradle.api.tasks.SourceSet;
18-
import org.gradle.api.tasks.SourceSetContainer;
19-
import org.gradle.api.tasks.TaskProvider;
21+
import org.gradle.testing.base.TestingExtension;
2022

21-
import static org.elasticsearch.gradle.internal.test.rest.RestTestUtil.registerTestTask;
2223
import static org.elasticsearch.gradle.internal.test.rest.RestTestUtil.setupYamlRestTestDependenciesDefaults;
2324

2425
/**
@@ -32,15 +33,23 @@ public class InternalYamlRestTestPlugin implements Plugin<Project> {
3233
public void apply(Project project) {
3334
project.getPluginManager().apply(RestTestBasePlugin.class);
3435
project.getPluginManager().apply(RestResourcesPlugin.class);
36+
TestingExtension testing = project.getExtensions().getByType(TestingExtension.class);
37+
testing.getSuites().registerBinding(YamlRestTestSuite.class, DefaultYamlRestTestSuite.class);
38+
testing.getSuites().register(SOURCE_SET_NAME, YamlRestTestSuite.class, suite -> {
39+
suite.useJUnit();
40+
configureYamlSourceSet(project, suite.getSources());
41+
suite.getDependencies().getImplementation().add(suite.getDependencies().project());
42+
suite.getTargets()
43+
.all(
44+
(Action<JvmTestSuiteTarget>) jvmTestSuiteTarget -> jvmTestSuiteTarget.getTestTask()
45+
.configure(
46+
test -> project.getTasks().named(JavaBasePlugin.CHECK_TASK_NAME).configure(check -> check.dependsOn(test))
47+
)
48+
);
49+
});
50+
}
3551

36-
// create source set
37-
SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
38-
SourceSet yamlTestSourceSet = sourceSets.create(SOURCE_SET_NAME);
39-
40-
TaskProvider<RestIntegTestTask> testTask = registerTestTask(project, yamlTestSourceSet, SOURCE_SET_NAME, RestIntegTestTask.class);
41-
42-
project.getTasks().named(JavaBasePlugin.CHECK_TASK_NAME).configure(check -> check.dependsOn(testTask));
43-
52+
private void configureYamlSourceSet(Project project, SourceSet yamlTestSourceSet) {
4453
// setup the dependencies
4554
setupYamlRestTestDependenciesDefaults(project, yamlTestSourceSet, true);
4655

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/RestTestBasePlugin.java

Lines changed: 140 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.elasticsearch.gradle.Version;
1919
import org.elasticsearch.gradle.VersionProperties;
2020
import org.elasticsearch.gradle.distribution.ElasticsearchDistributionTypes;
21+
import org.elasticsearch.gradle.internal.BwcVersions;
2122
import org.elasticsearch.gradle.internal.ElasticsearchJavaBasePlugin;
2223
import org.elasticsearch.gradle.internal.InternalDistributionDownloadPlugin;
2324
import org.elasticsearch.gradle.internal.test.ClusterFeaturesMetadataPlugin;
@@ -44,10 +45,15 @@
4445
import org.gradle.api.file.FileCollection;
4546
import org.gradle.api.file.FileTree;
4647
import org.gradle.api.internal.artifacts.dependencies.ProjectDependencyInternal;
48+
import org.gradle.api.plugins.JavaBasePlugin;
49+
import org.gradle.api.plugins.JvmTestSuitePlugin;
50+
import org.gradle.api.plugins.jvm.JvmTestSuiteTarget;
4751
import org.gradle.api.provider.ProviderFactory;
4852
import org.gradle.api.tasks.ClasspathNormalizer;
4953
import org.gradle.api.tasks.PathSensitivity;
54+
import org.gradle.api.tasks.testing.Test;
5055
import org.gradle.api.tasks.util.PatternFilterable;
56+
import org.gradle.testing.base.TestingExtension;
5157

5258
import java.util.Collection;
5359
import java.util.Iterator;
@@ -94,6 +100,8 @@ public RestTestBasePlugin(ProviderFactory providerFactory) {
94100
public void apply(Project project) {
95101
project.getPluginManager().apply(ElasticsearchJavaBasePlugin.class);
96102
project.getPluginManager().apply(InternalDistributionDownloadPlugin.class);
103+
project.getPluginManager().apply(JvmTestSuitePlugin.class);
104+
97105
var bwcVersions = loadBuildParams(project).get().getBwcVersions();
98106

99107
// Register integ-test and default distributions
@@ -153,104 +161,154 @@ public void apply(Project project) {
153161

154162
});
155163

156-
project.getTasks().withType(StandaloneRestIntegTestTask.class).configureEach(task -> {
157-
SystemPropertyCommandLineArgumentProvider nonInputSystemProperties = task.getExtensions()
158-
.getByType(SystemPropertyCommandLineArgumentProvider.class);
159-
160-
task.dependsOn(integTestDistro, modulesConfiguration);
161-
registerDistributionInputs(task, integTestDistro);
162-
163-
// Pass feature metadata on to tests
164-
task.getInputs().files(featureMetadataConfig).withPathSensitivity(PathSensitivity.NONE);
165-
nonInputSystemProperties.systemProperty(TESTS_FEATURES_METADATA_PATH, () -> featureMetadataConfig.getAsPath());
166-
167-
// Enable parallel execution for these tests since each test gets its own cluster
168-
task.setMaxParallelForks(task.getProject().getGradle().getStartParameter().getMaxWorkerCount() / 2);
169-
nonInputSystemProperties.systemProperty(TESTS_MAX_PARALLEL_FORKS_SYSPROP, () -> String.valueOf(task.getMaxParallelForks()));
170-
171-
// Disable test failure reporting since this stuff is now captured in build scans
172-
task.getExtensions().getByType(ErrorReportingTestListener.class).setDumpOutputOnFailure(false);
173-
174-
// Disable the security manager and syscall filter since the test framework needs to fork processes
175-
task.systemProperty("tests.security.manager", "false");
176-
task.systemProperty("tests.system_call_filter", "false");
164+
TestingExtension testing = project.getExtensions().getByType(TestingExtension.class);
165+
testing.getSuites()
166+
.withType(RestTestSuite.class)
167+
.configureEach(
168+
restTestSuite -> restTestSuite.getTargets()
169+
.configureEach((Action<JvmTestSuiteTarget>) jvmTestSuiteTarget -> jvmTestSuiteTarget.getTestTask().configure(test -> {
170+
configureRegisteredRestTask(
171+
project,
172+
test,
173+
integTestDistro,
174+
modulesConfiguration,
175+
featureMetadataConfig,
176+
bwcVersions,
177+
pluginsConfiguration,
178+
extractedPluginsConfiguration,
179+
defaultDistro,
180+
defaultDistroFeatureMetadataConfig
181+
);
182+
test.setGroup(JavaBasePlugin.VERIFICATION_GROUP);
183+
test.setDescription("Runs the REST tests against an external cluster");
177184

178-
// Pass minimum wire compatible version which is used by upgrade tests
179-
task.systemProperty(MINIMUM_WIRE_COMPATIBLE_VERSION_SYSPROP, bwcVersions.getMinimumWireCompatibleVersion());
185+
}))
186+
);
180187

181-
// Register plugins and modules as task inputs and pass paths as system properties to tests
182-
var modulePath = project.getObjects().fileCollection().from(modulesConfiguration);
183-
nonInputSystemProperties.systemProperty(TESTS_CLUSTER_MODULES_PATH_SYSPROP, modulePath::getAsPath);
184-
registerConfigurationInputs(task, modulesConfiguration.getName(), modulePath);
185-
var pluginPath = project.getObjects().fileCollection().from(pluginsConfiguration);
186-
nonInputSystemProperties.systemProperty(TESTS_CLUSTER_PLUGINS_PATH_SYSPROP, pluginPath::getAsPath);
187-
registerConfigurationInputs(
188+
project.getTasks().withType(StandaloneRestIntegTestTask.class).configureEach(task -> {
189+
configureRegisteredRestTask(
190+
project,
188191
task,
189-
extractedPluginsConfiguration.getName(),
190-
project.getObjects().fileCollection().from(extractedPluginsConfiguration)
192+
integTestDistro,
193+
modulesConfiguration,
194+
featureMetadataConfig,
195+
bwcVersions,
196+
pluginsConfiguration,
197+
extractedPluginsConfiguration,
198+
defaultDistro,
199+
defaultDistroFeatureMetadataConfig
191200
);
201+
});
202+
}
192203

193-
// Wire up integ-test distribution by default for all test tasks
194-
FileCollection extracted = integTestDistro.getExtracted();
195-
nonInputSystemProperties.systemProperty(INTEG_TEST_DISTRIBUTION_SYSPROP, () -> extracted.getSingleFile().getPath());
196-
197-
// Add `usesDefaultDistribution()` extension method to test tasks to indicate they require the default distro
198-
task.getExtensions().getExtraProperties().set("usesDefaultDistribution", new Closure<Void>(task) {
199-
@Override
200-
public Void call(Object... args) {
201-
if (reasonForUsageProvided(args) == false) {
202-
throw new IllegalArgumentException(
203-
"Reason for using `usesDefaultDistribution` required.\nUse usesDefaultDistribution(\"reason why default distro is required here\")."
204-
);
205-
}
206-
task.dependsOn(defaultDistro);
207-
registerDistributionInputs(task, defaultDistro);
204+
private void configureRegisteredRestTask(
205+
Project project,
206+
Test task,
207+
ElasticsearchDistribution integTestDistro,
208+
Configuration modulesConfiguration,
209+
FileCollection featureMetadataConfig,
210+
BwcVersions bwcVersions,
211+
Configuration pluginsConfiguration,
212+
Configuration extractedPluginsConfiguration,
213+
ElasticsearchDistribution defaultDistro,
214+
FileCollection defaultDistroFeatureMetadataConfig
215+
) {
216+
SystemPropertyCommandLineArgumentProvider nonInputSystemProperties = task.getExtensions()
217+
.getByType(SystemPropertyCommandLineArgumentProvider.class);
218+
219+
task.dependsOn(integTestDistro, modulesConfiguration);
220+
registerDistributionInputs(task, integTestDistro);
221+
222+
// Pass feature metadata on to tests
223+
task.getInputs().files(featureMetadataConfig).withPathSensitivity(PathSensitivity.NONE);
224+
nonInputSystemProperties.systemProperty(TESTS_FEATURES_METADATA_PATH, () -> featureMetadataConfig.getAsPath());
225+
226+
// Enable parallel execution for these tests since each test gets its own cluster
227+
task.setMaxParallelForks(task.getProject().getGradle().getStartParameter().getMaxWorkerCount() / 2);
228+
nonInputSystemProperties.systemProperty(TESTS_MAX_PARALLEL_FORKS_SYSPROP, () -> String.valueOf(task.getMaxParallelForks()));
229+
230+
// Disable test failure reporting since this stuff is now captured in build scans
231+
task.getExtensions().getByType(ErrorReportingTestListener.class).setDumpOutputOnFailure(false);
232+
233+
// Disable the security manager and syscall filter since the test framework needs to fork processes
234+
task.systemProperty("tests.security.manager", "false");
235+
task.systemProperty("tests.system_call_filter", "false");
236+
237+
// Pass minimum wire compatible version which is used by upgrade tests
238+
task.systemProperty(MINIMUM_WIRE_COMPATIBLE_VERSION_SYSPROP, bwcVersions.getMinimumWireCompatibleVersion());
239+
240+
// Register plugins and modules as task inputs and pass paths as system properties to tests
241+
var modulePath = project.getObjects().fileCollection().from(modulesConfiguration);
242+
nonInputSystemProperties.systemProperty(TESTS_CLUSTER_MODULES_PATH_SYSPROP, modulePath::getAsPath);
243+
registerConfigurationInputs(task, modulesConfiguration.getName(), modulePath);
244+
var pluginPath = project.getObjects().fileCollection().from(pluginsConfiguration);
245+
nonInputSystemProperties.systemProperty(TESTS_CLUSTER_PLUGINS_PATH_SYSPROP, pluginPath::getAsPath);
246+
registerConfigurationInputs(
247+
task,
248+
extractedPluginsConfiguration.getName(),
249+
project.getObjects().fileCollection().from(extractedPluginsConfiguration)
250+
);
208251

209-
nonInputSystemProperties.systemProperty(
210-
DEFAULT_DISTRIBUTION_SYSPROP,
211-
providerFactory.provider(() -> defaultDistro.getExtracted().getSingleFile().getPath())
252+
// Wire up integ-test distribution by default for all test tasks
253+
FileCollection extracted = integTestDistro.getExtracted();
254+
nonInputSystemProperties.systemProperty(INTEG_TEST_DISTRIBUTION_SYSPROP, () -> extracted.getSingleFile().getPath());
255+
256+
// Add `usesDefaultDistribution()` extension method to test tasks to indicate they require the default distro
257+
task.getExtensions().getExtraProperties().set("usesDefaultDistribution", new Closure<Void>(task) {
258+
@Override
259+
public Void call(Object... args) {
260+
if (reasonForUsageProvided(args) == false) {
261+
throw new IllegalArgumentException(
262+
"Reason for using `usesDefaultDistribution` required.\nUse usesDefaultDistribution(\"reason why default distro is required here\")."
212263
);
264+
}
265+
task.dependsOn(defaultDistro);
266+
registerDistributionInputs(task, defaultDistro);
213267

214-
// If we are using the default distribution we need to register all module feature metadata
215-
task.getInputs().files(defaultDistroFeatureMetadataConfig).withPathSensitivity(PathSensitivity.NONE);
216-
nonInputSystemProperties.systemProperty(TESTS_FEATURES_METADATA_PATH, defaultDistroFeatureMetadataConfig::getAsPath);
268+
nonInputSystemProperties.systemProperty(
269+
DEFAULT_DISTRIBUTION_SYSPROP,
270+
providerFactory.provider(() -> defaultDistro.getExtracted().getSingleFile().getPath())
271+
);
217272

218-
return null;
219-
}
273+
// If we are using the default distribution we need to register all module feature metadata
274+
task.getInputs().files(defaultDistroFeatureMetadataConfig).withPathSensitivity(PathSensitivity.NONE);
275+
nonInputSystemProperties.systemProperty(TESTS_FEATURES_METADATA_PATH, defaultDistroFeatureMetadataConfig::getAsPath);
220276

221-
private static boolean reasonForUsageProvided(Object[] args) {
222-
return args.length == 1 && args[0] instanceof String && ((String) args[0]).isBlank() == false;
223-
}
224-
});
277+
return null;
278+
}
225279

226-
// Add `usesBwcDistribution(version)` extension method to test tasks to indicate they require a BWC distribution
227-
task.getExtensions().getExtraProperties().set("usesBwcDistribution", new Closure<Void>(task) {
228-
@Override
229-
public Void call(Object... args) {
230-
if (args.length != 1 || args[0] instanceof Version == false) {
231-
throw new IllegalArgumentException("Expected exactly one argument of type org.elasticsearch.gradle.Version");
232-
}
280+
private static boolean reasonForUsageProvided(Object[] args) {
281+
return args.length == 1 && args[0] instanceof String && ((String) args[0]).isBlank() == false;
282+
}
283+
});
233284

234-
Version version = (Version) args[0];
235-
boolean isReleased = bwcVersions.unreleasedInfo(version) == null && version.toString().equals("0.0.0") == false;
236-
String versionString = version.toString();
237-
ElasticsearchDistribution bwcDistro = createDistribution(project, "bwc_" + versionString, versionString);
285+
// Add `usesBwcDistribution(version)` extension method to test tasks to indicate they require a BWC distribution
286+
task.getExtensions().getExtraProperties().set("usesBwcDistribution", new Closure<Void>(task) {
287+
@Override
288+
public Void call(Object... args) {
289+
if (args.length != 1 || args[0] instanceof Version == false) {
290+
throw new IllegalArgumentException("Expected exactly one argument of type org.elasticsearch.gradle.Version");
291+
}
238292

239-
task.dependsOn(bwcDistro);
240-
registerDistributionInputs(task, bwcDistro);
293+
Version version = (Version) args[0];
294+
boolean isReleased = bwcVersions.unreleasedInfo(version) == null && version.toString().equals("0.0.0") == false;
295+
String versionString = version.toString();
296+
ElasticsearchDistribution bwcDistro = createDistribution(project, "bwc_" + versionString, versionString);
241297

242-
nonInputSystemProperties.systemProperty(
243-
(isReleased ? BWC_RELEASED_DISTRIBUTION_SYSPROP_PREFIX : BWC_SNAPSHOT_DISTRIBUTION_SYSPROP_PREFIX) + versionString,
244-
providerFactory.provider(() -> bwcDistro.getExtracted().getSingleFile().getPath())
245-
);
298+
task.dependsOn(bwcDistro);
299+
registerDistributionInputs(task, bwcDistro);
246300

247-
if (version.getMajor() > 0 && version.before(bwcVersions.getMinimumWireCompatibleVersion())) {
248-
// If we are upgrade testing older versions we also need to upgrade to 7.last
249-
this.call(bwcVersions.getMinimumWireCompatibleVersion());
250-
}
251-
return null;
301+
nonInputSystemProperties.systemProperty(
302+
(isReleased ? BWC_RELEASED_DISTRIBUTION_SYSPROP_PREFIX : BWC_SNAPSHOT_DISTRIBUTION_SYSPROP_PREFIX) + versionString,
303+
providerFactory.provider(() -> bwcDistro.getExtracted().getSingleFile().getPath())
304+
);
305+
306+
if (version.getMajor() > 0 && version.before(bwcVersions.getMinimumWireCompatibleVersion())) {
307+
// If we are upgrade testing older versions we also need to upgrade to 7.last
308+
this.call(bwcVersions.getMinimumWireCompatibleVersion());
252309
}
253-
});
310+
return null;
311+
}
254312
});
255313
}
256314

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.elasticsearch.gradle.internal.test.rest;
2+
3+
import org.gradle.api.plugins.jvm.JvmTestSuite;
4+
5+
public interface RestTestSuite extends JvmTestSuite {
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.elasticsearch.gradle.internal.test.rest;
2+
3+
public interface YamlRestTestSuite extends RestTestSuite {
4+
}

0 commit comments

Comments
 (0)