Skip to content

Commit

Permalink
Rework StandaloneRestTestPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
breskeby committed Jan 10, 2025
1 parent 80126cf commit 0402394
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 198 deletions.
2 changes: 1 addition & 1 deletion build-tools-internal/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ gradlePlugin {
}
standaloneRestTest {
id = 'elasticsearch.standalone-rest-test'
implementationClass = 'org.elasticsearch.gradle.internal.test.StandaloneRestTestPlugin'
implementationClass = 'org.elasticsearch.gradle.internal.test.LegacyStandaloneRestTestPlugin'
}
standaloneTest {
id = 'elasticsearch.standalone-test'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.gradle.internal.test;

import org.elasticsearch.gradle.internal.ExportElasticsearchBuildResourcesTask;
import org.elasticsearch.gradle.internal.info.GlobalBuildInfoPlugin;
import org.elasticsearch.gradle.internal.precommit.InternalPrecommitTasks;
import org.elasticsearch.gradle.internal.test.rest.LegacyJavaRestTestPlugin;
import org.elasticsearch.gradle.internal.test.rest.LegacyYamlRestTestPlugin;
import org.elasticsearch.gradle.internal.test.rest.RestTestUtil;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.testing.Test;
import org.gradle.plugins.ide.eclipse.model.EclipseModel;
import org.gradle.plugins.ide.idea.model.IdeaModel;

import java.util.Arrays;
import java.util.Map;

/**
* Configures the build to compile tests against Elasticsearch's test framework
* and run REST tests. Use BuildPlugin if you want to build main code as well
* as tests.
*
* @deprecated use {@link InternalClusterTestPlugin}, {@link LegacyJavaRestTestPlugin} or
* {@link LegacyYamlRestTestPlugin} instead.
*/
@Deprecated
public class LegacyStandaloneRestTestPlugin implements Plugin<Project> {
@Override
public void apply(final Project project) {
if (project.getPluginManager().hasPlugin("elasticsearch.build")) {
throw new InvalidUserDataException(
"elasticsearch.standalone-test, elasticsearch.standalone-rest-test, " + "and elasticsearch.build are mutually exclusive"
);
}

project.getRootProject().getPluginManager().apply(GlobalBuildInfoPlugin.class);
project.getPluginManager().apply(LegacyRestTestBasePlugin.class);

project.getTasks().register("buildResources", ExportElasticsearchBuildResourcesTask.class);

// only setup tests to build
SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
final SourceSet testSourceSet = sourceSets.create("test");

project.getTasks().withType(Test.class).configureEach(test -> {
test.setTestClassesDirs(testSourceSet.getOutput().getClassesDirs());
test.setClasspath(testSourceSet.getRuntimeClasspath());
});

// create a compileOnly configuration as others might expect it
project.getConfigurations().create("compileOnly");
RestTestUtil.setupJavaRestTestDependenciesDefaults(project, testSourceSet);

EclipseModel eclipse = project.getExtensions().getByType(EclipseModel.class);
eclipse.getClasspath().setSourceSets(Arrays.asList(testSourceSet));
eclipse.getClasspath()
.setPlusConfigurations(
Arrays.asList(project.getConfigurations().getByName(JavaPlugin.TEST_RUNTIME_CLASSPATH_CONFIGURATION_NAME))
);

IdeaModel idea = project.getExtensions().getByType(IdeaModel.class);
idea.getModule().getTestSources().from(testSourceSet.getJava().getSrcDirs());
idea.getModule()
.getScopes()
.put(
"TEST",
Map.of("plus", Arrays.asList(project.getConfigurations().getByName(JavaPlugin.TEST_RUNTIME_CLASSPATH_CONFIGURATION_NAME)))
);
InternalPrecommitTasks.create(project, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.gradle.internal.precommit.InternalPrecommitTasks;
import org.elasticsearch.gradle.internal.test.rest.LegacyJavaRestTestPlugin;
import org.elasticsearch.gradle.internal.test.rest.LegacyYamlRestTestPlugin;
import org.elasticsearch.gradle.internal.test.rest.RestTestBasePlugin;
import org.elasticsearch.gradle.internal.test.rest.RestTestUtil;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.Plugin;
Expand All @@ -33,10 +34,7 @@
* and run REST tests. Use BuildPlugin if you want to build main code as well
* as tests.
*
* @deprecated use {@link InternalClusterTestPlugin}, {@link LegacyJavaRestTestPlugin} or
* {@link LegacyYamlRestTestPlugin} instead.
*/
@Deprecated
public class StandaloneRestTestPlugin implements Plugin<Project> {
@Override
public void apply(final Project project) {
Expand All @@ -46,9 +44,7 @@ public void apply(final Project project) {
);
}

project.getRootProject().getPluginManager().apply(GlobalBuildInfoPlugin.class);
project.getPluginManager().apply(LegacyRestTestBasePlugin.class);

project.getPluginManager().apply(RestTestBasePlugin.class);
project.getTasks().register("buildResources", ExportElasticsearchBuildResourcesTask.class);

// only setup tests to build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void apply(Project project) {
.configureEach(
filePermissionPlugin -> project.getTasks().named(FILEPERMISSIONS_TASK_NAME).configure(t -> t.dependsOn(exportKeyStore))
);
project.getPlugins().withType(StandaloneRestTestPlugin.class).configureEach(restTestPlugin -> {
project.getPlugins().withType(LegacyStandaloneRestTestPlugin.class).configureEach(restTestPlugin -> {
SourceSet testSourceSet = Util.getJavaTestSourceSet(project).get();
testSourceSet.getResources().srcDir(new File(keyStoreDir, "test/ssl"));
project.getTasks().named(testSourceSet.getProcessResourcesTaskName()).configure(t -> t.dependsOn(exportKeyStore));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public OldElasticsearchTestContainer() {
super(new RemoteDockerImage(DOCKER_IMAGE));
withNetwork(Network.newNetwork());
addExposedPorts(PORT);
canBeReused();
}

public OldElasticsearchTestContainer withSetting(String key, String value) {
Expand Down
200 changes: 10 additions & 190 deletions x-pack/qa/repository-old-versions/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,19 @@
* 2.0.
*/

import org.elasticsearch.gradle.Architecture
import org.elasticsearch.gradle.OS
import org.elasticsearch.gradle.Version
import org.elasticsearch.gradle.internal.BwcVersions
import org.elasticsearch.gradle.internal.test.AntFixture
import org.elasticsearch.gradle.testclusters.StandaloneRestIntegTestTask
import org.elasticsearch.gradle.transform.UnzipTransform
import static org.elasticsearch.gradle.PropertyNormalization.IGNORE_VALUE
import org.elasticsearch.gradle.internal.test.rest.CopyRestApiTask
import org.elasticsearch.gradle.internal.test.rest.CopyRestTestsTask
import org.elasticsearch.gradle.internal.test.rest.RestResourcesPlugin

//apply plugin: 'elasticsearch.jdk-download'
//apply plugin: 'elasticsearch.internal-testclusters'
//apply plugin: 'elasticsearch.standalone-rest-test'
apply plugin: 'elasticsearch.internal-java-rest-test'
apply plugin: org.elasticsearch.gradle.internal.test.StandaloneRestTestPlugin
apply plugin: 'elasticsearch.rest-resources'

configurations {
oldesFixture
}

dependencies {
oldesFixture project(':test:fixtures:old-elasticsearch')
javaRestTestImplementation project(':test:fixtures:old-elasticsearch')
}

jdks {
legacy {
vendor = 'adoptium'
version = '8u302+b08'
platform = OS.current().name().toLowerCase()
architecture = Architecture.current().name().toLowerCase()
}
testImplementation project(':test:test-clusters')
testImplementation project(':test:fixtures:old-elasticsearch')
}

restResources {
Expand All @@ -50,175 +29,16 @@ restResources {
}
}


// Register rest resources with source set
sourceSets.javaRestTest.getOutput()
.dir(
project.getTasks()
.withType(CopyRestApiTask.class)
.named(RestResourcesPlugin.COPY_REST_API_SPECS_TASK)
.flatMap(CopyRestApiTask::getOutputResourceDir)
);

sourceSets.javaRestTest.getOutput()
.dir(
project.getTasks()
.withType(CopyRestTestsTask.class)
.named(RestResourcesPlugin.COPY_YAML_TESTS_TASK)
.flatMap(CopyRestTestsTask::getOutputResourceDir)
);


if (OS.current() == OS.MAC && Architecture.current() == Architecture.AARCH64) {
jdks.legacy.vendor = 'zulu'
jdks.legacy.distributionVersion = '8.56.0.23'
}

interface Injected {
@Inject
FileSystemOperations getFs()
}

if (OS.current() == OS.WINDOWS) {
logger.warn("Disabling repository-old-versions tests because we can't get the pid file on windows")
} else {
/* Register a gradle artifact transformation to unpack resolved elasticsearch distributions. We only resolve
* zip files here. Using artifact transforms allow a better caching of the downloaded distros as the
* transformed (unpacked) distro will be cached by gradle resulting in less unpacking
*
* To avoid testing against too many old versions, always pick first and last version per major
*/
project.getDependencies().registerTransform(UnzipTransform.class, transformSpec -> {
transformSpec.getFrom().attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.ZIP_TYPE);
transformSpec.getTo().attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.DIRECTORY_TYPE);
});

int currentMajorVersion = org.elasticsearch.gradle.VersionProperties.elasticsearchVersion.major
assert (currentMajorVersion - 2) == 7 : "add archive BWC tests for major version " + (currentMajorVersion - 2)
int currentMajorVersion = org.elasticsearch.gradle.VersionProperties.elasticsearchVersion.major
assert (currentMajorVersion - 2) == 7: "add archive BWC tests for major version " + (currentMajorVersion - 2)
// for (String versionString : ['5.0.0', '5.6.16', '6.0.0', '6.8.20']) {
// Version version = Version.fromString(versionString)
// String packageName = 'org.elasticsearch.distribution.zip'
// String artifact = "${packageName}:elasticsearch:${version}@zip"
// String versionNoDots = version.toString().replace('.', '_')
// String configName = "es${versionNoDots}"
//
// def config = configurations.create(configName)
// config.getAttributes().attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.DIRECTORY_TYPE);
// dependencies.add(configName, artifact)
//
// String repoLocation = "${layout.buildDirectory.asFile.get()}/cluster/shared/repo/${versionNoDots}"
// String clusterName = versionNoDots
//
// def testClusterProvider = testClusters.register(clusterName) {
// testDistribution = 'DEFAULT'
// numberOfNodes = 2
// versions = [project.version, project.version] // to test full cluster restart
//
// setting 'path.repo', repoLocation, IGNORE_VALUE
// setting 'xpack.license.self_generated.type', 'trial'
//
// setting 'xpack.security.enabled', 'true'
// user username: 'admin', password: 'admin-password', role: 'superuser'
//
// setting 'xpack.searchable.snapshot.shared_cache.size', '16MB'
// setting 'xpack.searchable.snapshot.shared_cache.region_size', '256KB'
// }
//
// def oldesFixtureConfiguration = project.configurations.oldesFixture
//
// TaskProvider<AntFixture> fixture = tasks.register("oldES${versionNoDots}Fixture", AntFixture) {
// dependsOn project.configurations.oldesFixture, jdks.legacy, config
// executable = "${buildParams.runtimeJavaHome.get()}/bin/java"
// env 'CLASSPATH', "${-> oldesFixtureConfiguration.asPath}"
// // old versions of Elasticsearch need JAVA_HOME
// env 'JAVA_HOME', jdks.legacy.javaHomePath
// // If we are running on certain arm systems we need to explicitly set the stack size to overcome JDK page size bug
// if (Architecture.current() == Architecture.AARCH64) {
// env 'ES_JAVA_OPTS', '-Xss512k'
// }
// def dataPath = "${baseDir}/data"
// args 'oldes.OldElasticsearch',
// baseDir,
// "${ -> config.getSingleFile().toPath()}",
// false,
// "path.repo: ${repoLocation}",
// "path.data: ${dataPath}"
// if ((version.onOrAfter('6.8.0') && Architecture.current() == Architecture.AARCH64) || (version.onOrAfter("6.4.0") && BwcVersions.isMlCompatible(version) == false)) {
// // We need to explicitly disable ML when running old ES versions on ARM or on systems with newer GLIBC
// args 'xpack.ml.enabled: false'
// }
// def injected = project.objects.newInstance(Injected)
//
// doFirst {
// injected.getFs().delete { d ->
// d.delete(dataPath)
// }
// new File(dataPath).mkdirs()
// }
// maxWaitInSeconds = 60
// waitCondition = { fixture, ant ->
// // the fixture writes the ports file when Elasticsearch's HTTP service
// // is ready, so we can just wait for the file to exist
// return fixture.portsFile.exists()
// }
// }
//
// tasks.register("javaRestTestBeforeRestart#${versionNoDots}", StandaloneRestIntegTestTask) {
// useCluster testClusterProvider
// dependsOn fixture
// def injected = project.objects.newInstance(Injected)
//
// doFirst {
// injected.getFs().delete { d ->
// d.delete(repoLocation)
// }
// new File(repoLocation).mkdirs()
// }
// systemProperty 'tests.after_restart', 'false'
// }
//
// tasks.register("javaRestTestAfterRestart#${versionNoDots}", StandaloneRestIntegTestTask) {
// useCluster testClusterProvider
// dependsOn fixture
// dependsOn "javaRestTestBeforeRestart#${versionNoDots}"
// systemProperty 'tests.after_restart', 'true'
//
// doFirst {
// testClusterProvider.get().goToNextVersion()
// }
// }
//
// tasks.matching { it.name.startsWith("javaRestTest") && it.name.endsWith(versionNoDots) }.configureEach {
// it.nonInputProperties.systemProperty "tests.repo.location", repoLocation
// it.systemProperty "tests.es.version", version.toString()
//
// /* Use a closure on the string to delay evaluation until right before we
// * run the integration tests so that we can be sure that the file is
// * ready. */
// it.nonInputProperties.systemProperty "tests.es.port", "${-> fixture.get().addressAndPort}"
// it.nonInputProperties.systemProperty('tests.rest.cluster', "${-> testClusterProvider.get().allHttpSocketURI.join(",")}")
// it.nonInputProperties.systemProperty('tests.clustername', "${-> testClusterProvider.get().getName()}")
// }
//
// tasks.named("check").configure {
// dependsOn "javaRestTestAfterRestart#${versionNoDots}"
// }
// }
for (String versionString : ['6.8.20']) {
Version version = Version.fromString(versionString)
String versionNoDots = version.toString().replace('.', '_')


tasks.named("javaRestTest").configure {
tasks.register("javaRestTest${versionNoDots}", StandaloneRestIntegTestTask).configure {
usesDefaultDistribution()
systemProperty "tests.es.version", version.toString()
maxParallelForks = 1
}

//
// tasks.register("javaRestTestAfterRestart", RestIntegTestTask).configure {
// dependsOn('javaRestTest')
// usesDefaultDistribution()
//// dependsOn jdks.legacy
//// usesBwcDistribution(Version.fromString("7.17.20"))
//// usesBwcDistribution(Version.fromString("6.0.0"))
//// environment 'JAVA_HOME', jdks.legacy.javaHomePath
// }
}

0 comments on commit 0402394

Please sign in to comment.