Skip to content

Commit 11fa052

Browse files
authored
[8.x] [TEST] Migrated REST tests extending JsonLogsIntegTestCase (#115188) (#116025)
* [TEST] Migrated REST tests extending JsonLogsIntegTestCase (#115188) REST tests extending JsonLogsIntegTestCase migrated to the new REST testing framework, using 'elasticsearch.internal-java-rest-test' Gradle plugin Explicit handling of test single-cluster config by checking if cluster is explicitly configured as single-node to avoid an incorrect configuration which may cause split brain. Replaced `ElasticsearchJavaPlugin` with `ElasticsearchJavaBasePlugin` in `RestTestBasePlugin` for better granularity. Additionally, updated the `DistributionDownloadPlugin` to set configurations as non-consumable and the `InternalDistributionArchiveSetupPlugin` to mark certain configurations as consumable. This ensures that configurations are correctly utilized during build setup and execution phases. (cherry picked from commit 130cc74) * Fixed DefaultSettingsProvider to use Java 17 (#116029) It is needed to be able to backport the change to the 8.x. Having same code for both versions can help us to avoid some merge conflicts in the future.
1 parent 2439869 commit 11fa052

File tree

27 files changed

+226
-212
lines changed

27 files changed

+226
-212
lines changed

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/InternalDistributionArchiveSetupPlugin.java

+2
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,14 @@ private void registerAndConfigureDistributionArchivesExtension(Project project)
7676
sub.getArtifacts().add(DEFAULT_CONFIGURATION_NAME, distributionArchive.getArchiveTask());
7777
var extractedConfiguration = sub.getConfigurations().create(EXTRACTED_CONFIGURATION_NAME);
7878
extractedConfiguration.setCanBeResolved(false);
79+
extractedConfiguration.setCanBeConsumed(true);
7980
extractedConfiguration.getAttributes()
8081
.attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.DIRECTORY_TYPE);
8182
sub.getArtifacts().add(EXTRACTED_CONFIGURATION_NAME, distributionArchive.getExpandedDistTask());
8283
// The "composite" configuration is specifically used for resolving transformed artifacts in an included build
8384
var compositeConfiguration = sub.getConfigurations().create(COMPOSITE_CONFIGURATION_NAME);
8485
compositeConfiguration.setCanBeResolved(false);
86+
compositeConfiguration.setCanBeConsumed(true);
8587
compositeConfiguration.getAttributes()
8688
.attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.DIRECTORY_TYPE);
8789
compositeConfiguration.getAttributes().attribute(Attribute.of("composite", Boolean.class), true);

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/RestrictedBuildApiService.java

-4
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ private static ListMultimap<Class<?>, String> createLegacyRestTestBasePluginUsag
5353
map.put(LegacyRestTestBasePlugin.class, ":plugins:repository-hdfs");
5454
map.put(LegacyRestTestBasePlugin.class, ":plugins:store-smb");
5555
map.put(LegacyRestTestBasePlugin.class, ":qa:ccs-rolling-upgrade-remote-cluster");
56-
map.put(LegacyRestTestBasePlugin.class, ":qa:ccs-unavailable-clusters");
57-
map.put(LegacyRestTestBasePlugin.class, ":qa:logging-config");
5856
map.put(LegacyRestTestBasePlugin.class, ":qa:mixed-cluster");
5957
map.put(LegacyRestTestBasePlugin.class, ":qa:multi-cluster-search");
6058
map.put(LegacyRestTestBasePlugin.class, ":qa:remote-clusters");
@@ -65,15 +63,13 @@ private static ListMultimap<Class<?>, String> createLegacyRestTestBasePluginUsag
6563
map.put(LegacyRestTestBasePlugin.class, ":qa:smoke-test-ingest-with-all-dependencies");
6664
map.put(LegacyRestTestBasePlugin.class, ":qa:smoke-test-plugins");
6765
map.put(LegacyRestTestBasePlugin.class, ":qa:system-indices");
68-
map.put(LegacyRestTestBasePlugin.class, ":qa:unconfigured-node-name");
6966
map.put(LegacyRestTestBasePlugin.class, ":qa:verify-version-constants");
7067
map.put(LegacyRestTestBasePlugin.class, ":test:external-modules:test-apm-integration");
7168
map.put(LegacyRestTestBasePlugin.class, ":test:external-modules:test-delayed-aggs");
7269
map.put(LegacyRestTestBasePlugin.class, ":test:external-modules:test-die-with-dignity");
7370
map.put(LegacyRestTestBasePlugin.class, ":test:external-modules:test-error-query");
7471
map.put(LegacyRestTestBasePlugin.class, ":test:external-modules:test-latency-simulating-directory");
7572
map.put(LegacyRestTestBasePlugin.class, ":test:yaml-rest-runner");
76-
map.put(LegacyRestTestBasePlugin.class, ":distribution:archives:integ-test-zip");
7773
map.put(LegacyRestTestBasePlugin.class, ":x-pack:plugin:core");
7874
map.put(LegacyRestTestBasePlugin.class, ":x-pack:plugin:ent-search");
7975
map.put(LegacyRestTestBasePlugin.class, ":x-pack:plugin:fleet");

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +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.ElasticsearchJavaPlugin;
21+
import org.elasticsearch.gradle.internal.ElasticsearchJavaBasePlugin;
2222
import org.elasticsearch.gradle.internal.InternalDistributionDownloadPlugin;
2323
import org.elasticsearch.gradle.internal.info.BuildParams;
2424
import org.elasticsearch.gradle.internal.test.ErrorReportingTestListener;
@@ -89,7 +89,7 @@ public RestTestBasePlugin(ProviderFactory providerFactory) {
8989

9090
@Override
9191
public void apply(Project project) {
92-
project.getPluginManager().apply(ElasticsearchJavaPlugin.class);
92+
project.getPluginManager().apply(ElasticsearchJavaBasePlugin.class);
9393
project.getPluginManager().apply(InternalDistributionDownloadPlugin.class);
9494

9595
// Register integ-test and default distributions

build-tools/src/main/java/org/elasticsearch/gradle/DistributionDownloadPlugin.java

+2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ public void apply(Project project) {
8989
private void setupDistributionContainer(Project project) {
9090
distributionsContainer = project.container(ElasticsearchDistribution.class, name -> {
9191
var fileConfiguration = project.getConfigurations().create(DISTRO_CONFIG_PREFIX + name);
92+
fileConfiguration.setCanBeConsumed(false);
9293
var extractedConfiguration = project.getConfigurations().create(DISTRO_EXTRACTED_CONFIG_PREFIX + name);
94+
extractedConfiguration.setCanBeConsumed(false);
9395
extractedConfiguration.getAttributes()
9496
.attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.DIRECTORY_TYPE);
9597
var distribution = new ElasticsearchDistribution(

distribution/archives/integ-test-zip/build.gradle

+1-17
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import org.apache.tools.ant.filters.ReplaceTokens
1111

12-
apply plugin: 'elasticsearch.legacy-java-rest-test'
12+
apply plugin: 'elasticsearch.internal-java-rest-test'
1313

1414
// The integ-test-distribution is published to maven
1515
apply plugin: 'elasticsearch.publish'
@@ -35,19 +35,3 @@ publishing {
3535
}
3636
}
3737
}
38-
39-
tasks.named("javaRestTest").configure {
40-
dependsOn "assemble"
41-
/*
42-
* There are two unique things going on here:
43-
* 1. These tests can be run against an external cluster with
44-
* -Dtests.rest.cluster=whatever and -Dtest.cluster=whatever
45-
* 2. *One* of these tests is incompatible with that and should be skipped
46-
* when running against an external cluster.
47-
*/
48-
if (project.providers.systemProperty("tests.rest.cluster").isPresent()) {
49-
nonInputProperties.systemProperty 'tests.logfile', testClusters.named('javaRestTest').map(c -> c.singleNode().serverLog)
50-
} else {
51-
systemProperty 'tests.logfile', '--external--'
52-
}
53-
}

distribution/archives/integ-test-zip/src/javaRestTest/java/org/elasticsearch/test/rest/CreatedLocationHeaderIT.java

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import org.elasticsearch.client.Request;
1313
import org.elasticsearch.client.Response;
14+
import org.elasticsearch.test.cluster.ElasticsearchCluster;
15+
import org.junit.ClassRule;
1416

1517
import java.io.IOException;
1618

@@ -23,6 +25,14 @@
2325
*/
2426
public class CreatedLocationHeaderIT extends ESRestTestCase {
2527

28+
@ClassRule
29+
public static ElasticsearchCluster cluster = ElasticsearchCluster.local().build();
30+
31+
@Override
32+
protected String getTestRestCluster() {
33+
return cluster.getHttpAddresses();
34+
}
35+
2636
public void testCreate() throws IOException {
2737
locationTestCase("PUT", "test/_doc/1");
2838
}

distribution/archives/integ-test-zip/src/javaRestTest/java/org/elasticsearch/test/rest/JsonLogsFormatAndParseIT.java

+20-20
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,36 @@
1010
package org.elasticsearch.test.rest;
1111

1212
import org.elasticsearch.common.logging.JsonLogsIntegTestCase;
13+
import org.elasticsearch.test.cluster.ElasticsearchCluster;
14+
import org.elasticsearch.test.cluster.LogType;
1315
import org.hamcrest.Matcher;
16+
import org.junit.ClassRule;
1417

15-
import java.io.BufferedReader;
16-
import java.io.IOException;
17-
import java.nio.charset.StandardCharsets;
18-
import java.nio.file.Files;
19-
import java.nio.file.Path;
20-
import java.security.AccessController;
21-
import java.security.PrivilegedAction;
18+
import java.io.InputStream;
2219

2320
import static org.hamcrest.Matchers.is;
2421

2522
public class JsonLogsFormatAndParseIT extends JsonLogsIntegTestCase {
23+
24+
private static final String NODE_NAME = "test-node-0";
25+
26+
@ClassRule
27+
public static ElasticsearchCluster cluster = ElasticsearchCluster.local()
28+
.withNode(localNodeSpecBuilder -> localNodeSpecBuilder.name(NODE_NAME))
29+
.build();
30+
31+
@Override
32+
protected String getTestRestCluster() {
33+
return cluster.getHttpAddresses();
34+
}
35+
2636
@Override
2737
protected Matcher<String> nodeNameMatcher() {
28-
return is("integTest-0");
38+
return is(NODE_NAME);
2939
}
3040

3141
@Override
32-
protected BufferedReader openReader(Path logFile) {
33-
assumeFalse(
34-
"Skipping test because it is being run against an external cluster.",
35-
logFile.getFileName().toString().equals("--external--")
36-
);
37-
return AccessController.doPrivileged((PrivilegedAction<BufferedReader>) () -> {
38-
try {
39-
return Files.newBufferedReader(logFile, StandardCharsets.UTF_8);
40-
} catch (IOException e) {
41-
throw new RuntimeException(e);
42-
}
43-
});
42+
protected InputStream openLogsStream() {
43+
return cluster.getNodeLog(0, LogType.SERVER_JSON);
4444
}
4545
}

distribution/archives/integ-test-zip/src/javaRestTest/java/org/elasticsearch/test/rest/NodeRestUsageIT.java

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import org.elasticsearch.common.Strings;
1616
import org.elasticsearch.search.aggregations.AggregationBuilders;
1717
import org.elasticsearch.search.builder.SearchSourceBuilder;
18+
import org.elasticsearch.test.cluster.ElasticsearchCluster;
19+
import org.junit.ClassRule;
1820

1921
import java.io.IOException;
2022
import java.util.Collections;
@@ -28,6 +30,14 @@
2830

2931
public class NodeRestUsageIT extends ESRestTestCase {
3032

33+
@ClassRule
34+
public static ElasticsearchCluster cluster = ElasticsearchCluster.local().build();
35+
36+
@Override
37+
protected String getTestRestCluster() {
38+
return cluster.getHttpAddresses();
39+
}
40+
3141
@SuppressWarnings("unchecked")
3242
public void testWithRestUsage() throws IOException {
3343
// First get the current usage figures

distribution/archives/integ-test-zip/src/javaRestTest/java/org/elasticsearch/test/rest/RequestsWithoutContentIT.java

+10
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,23 @@
1111

1212
import org.elasticsearch.client.Request;
1313
import org.elasticsearch.client.ResponseException;
14+
import org.elasticsearch.test.cluster.ElasticsearchCluster;
15+
import org.junit.ClassRule;
1416

1517
import java.io.IOException;
1618

1719
import static org.hamcrest.CoreMatchers.containsString;
1820

1921
public class RequestsWithoutContentIT extends ESRestTestCase {
2022

23+
@ClassRule
24+
public static ElasticsearchCluster cluster = ElasticsearchCluster.local().build();
25+
26+
@Override
27+
protected String getTestRestCluster() {
28+
return cluster.getHttpAddresses();
29+
}
30+
2131
public void testIndexMissingBody() throws IOException {
2232
ResponseException responseException = expectThrows(
2333
ResponseException.class,

distribution/archives/integ-test-zip/src/javaRestTest/java/org/elasticsearch/test/rest/WaitForRefreshAndCloseIT.java

+11
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
import org.elasticsearch.client.Response;
1818
import org.elasticsearch.client.ResponseException;
1919
import org.elasticsearch.client.ResponseListener;
20+
import org.elasticsearch.test.cluster.ElasticsearchCluster;
2021
import org.junit.After;
2122
import org.junit.Before;
23+
import org.junit.ClassRule;
2224

2325
import java.io.IOException;
2426
import java.util.Map;
@@ -32,6 +34,15 @@
3234
* Tests that wait for refresh is fired if the index is closed.
3335
*/
3436
public class WaitForRefreshAndCloseIT extends ESRestTestCase {
37+
38+
@ClassRule
39+
public static ElasticsearchCluster cluster = ElasticsearchCluster.local().build();
40+
41+
@Override
42+
protected String getTestRestCluster() {
43+
return cluster.getHttpAddresses();
44+
}
45+
3546
@Before
3647
public void setupIndex() throws IOException {
3748
Request request = new Request("PUT", "/test");

qa/logging-config/build.gradle

+1-21
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,14 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99
apply plugin: 'elasticsearch.build'
10-
apply plugin: 'elasticsearch.legacy-java-rest-test'
10+
apply plugin: 'elasticsearch.internal-java-rest-test'
1111

1212
dependencies {
1313
testImplementation project(":libs:x-content")
1414
testImplementation project(":test:framework")
1515

1616
}
1717

18-
testClusters.all {
19-
setting 'xpack.security.enabled', 'false'
20-
}
21-
22-
testClusters.matching { it.name == "javaRestTest" }.configureEach {
23-
/**
24-
* Provide a custom log4j configuration where layout is an old style pattern and confirm that Elasticsearch
25-
* can successfully startup.
26-
*/
27-
extraConfigFile 'log4j2.properties', file('es-v7-log4j2.properties')
28-
}
29-
30-
tasks.named("javaRestTest").configure {
31-
nonInputProperties.systemProperty 'tests.logfile',
32-
"${-> testClusters.javaRestTest.singleNode().getServerLog().absolutePath.replaceAll("_server.json", ".log")}"
33-
34-
nonInputProperties.systemProperty 'tests.jsonLogfile',
35-
"${-> testClusters.javaRestTest.singleNode().getServerLog()}"
36-
}
37-
3818
tasks.named("test").configure {
3919
systemProperty 'tests.security.manager', 'false'
4020
}

qa/logging-config/src/javaRestTest/java/org/elasticsearch/common/logging/CustomLoggingConfigIT.java

+30-38
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@
88
*/
99
package org.elasticsearch.common.logging;
1010

11-
import org.elasticsearch.core.SuppressForbidden;
11+
import org.elasticsearch.test.cluster.ElasticsearchCluster;
12+
import org.elasticsearch.test.cluster.LogType;
13+
import org.elasticsearch.test.cluster.util.resource.Resource;
1214
import org.elasticsearch.test.rest.ESRestTestCase;
1315
import org.hamcrest.Matchers;
16+
import org.junit.ClassRule;
1417

18+
import java.io.BufferedReader;
1519
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.io.InputStreamReader;
1622
import java.io.UncheckedIOException;
1723
import java.nio.charset.StandardCharsets;
18-
import java.nio.file.Files;
19-
import java.nio.file.Path;
20-
import java.nio.file.Paths;
21-
import java.security.AccessController;
22-
import java.security.PrivilegedAction;
2324
import java.util.List;
2425

2526
import static org.hamcrest.Matchers.matchesRegex;
@@ -34,53 +35,44 @@ public class CustomLoggingConfigIT extends ESRestTestCase {
3435
// [2020-03-20T14:51:59,989][INFO ][o.e.g.GatewayService ] [integTest-0] recovered [0] indices into cluster_state
3536
private static final String NODE_STARTED = ".*recovered.*cluster_state.*";
3637

38+
@ClassRule
39+
public static ElasticsearchCluster cluster = ElasticsearchCluster.local()
40+
.setting("xpack.security.enabled", "false")
41+
.configFile("log4j2.properties", Resource.fromClasspath("es-v7-log4j2.properties"))
42+
.build();
43+
44+
@Override
45+
protected String getTestRestCluster() {
46+
return cluster.getHttpAddresses();
47+
}
48+
3749
public void testSuccessfulStartupWithCustomConfig() throws Exception {
3850
assertBusy(() -> {
39-
List<String> lines = readAllLines(getPlaintextLogFile());
51+
List<String> lines = getPlaintextLog();
4052
assertThat(lines, Matchers.hasItem(matchesRegex(NODE_STARTED)));
4153
});
4254
}
4355

4456
public void testParseAllV7JsonLines() throws Exception {
4557
assertBusy(() -> {
46-
List<String> lines = readAllLines(getJSONLogFile());
58+
List<String> lines = getJSONLog();
4759
assertThat(lines, Matchers.hasItem(matchesRegex(NODE_STARTED)));
4860
});
4961
}
5062

51-
private List<String> readAllLines(Path logFile) {
52-
return AccessController.doPrivileged((PrivilegedAction<List<String>>) () -> {
53-
try {
54-
return Files.readAllLines(logFile, StandardCharsets.UTF_8);
55-
} catch (IOException e) {
56-
throw new UncheckedIOException(e);
57-
}
58-
});
59-
}
60-
61-
@SuppressForbidden(reason = "PathUtils doesn't have permission to read this file")
62-
private Path getJSONLogFile() {
63-
String logFileString = System.getProperty("tests.logfile");
64-
if (logFileString == null) {
65-
fail(
66-
"tests.logfile must be set to run this test. It is automatically "
67-
+ "set by gradle. If you must set it yourself then it should be the absolute path to the "
68-
+ "log file."
69-
);
63+
private List<String> getJSONLog() {
64+
try (InputStream nodeLog = cluster.getNodeLog(0, LogType.SERVER_JSON)) {
65+
return new BufferedReader(new InputStreamReader(nodeLog, StandardCharsets.UTF_8)).lines().toList();
66+
} catch (IOException e) {
67+
throw new UncheckedIOException(e);
7068
}
71-
return Paths.get(logFileString);
7269
}
7370

74-
@SuppressForbidden(reason = "PathUtils doesn't have permission to read this file")
75-
private Path getPlaintextLogFile() {
76-
String logFileString = System.getProperty("tests.logfile");
77-
if (logFileString == null) {
78-
fail(
79-
"tests.logfile must be set to run this test. It is automatically "
80-
+ "set by gradle. If you must set it yourself then it should be the absolute path to the "
81-
+ "log file."
82-
);
71+
private List<String> getPlaintextLog() {
72+
try (InputStream nodeLog = cluster.getNodeLog(0, LogType.SERVER)) {
73+
return new BufferedReader(new InputStreamReader(nodeLog, StandardCharsets.UTF_8)).lines().toList();
74+
} catch (IOException e) {
75+
throw new UncheckedIOException(e);
8376
}
84-
return Paths.get(logFileString);
8577
}
8678
}

0 commit comments

Comments
 (0)