Skip to content

Commit 093fdc0

Browse files
mark-vieiraywangd
andauthored
[Test] Allow configuring configDir for the Java test cluster (elastic#125094) (elastic#125707)
For creating and deleting projects in multi-project tests, we need create and delete settings and secrets files on the fly. This PR adds such feature to the Java test cluster with an option to specify the config directory. (cherry picked from commit a1b0ed1) Co-authored-by: Yang Wang <[email protected]>
1 parent 63be3d2 commit 093fdc0

File tree

6 files changed

+47
-13
lines changed

6 files changed

+47
-13
lines changed

test/test-clusters/src/main/java/org/elasticsearch/test/cluster/local/AbstractLocalClusterFactory.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import java.util.Locale;
5555
import java.util.Map;
5656
import java.util.Objects;
57+
import java.util.Optional;
5758
import java.util.Properties;
5859
import java.util.Set;
5960
import java.util.UUID;
@@ -146,7 +147,7 @@ public Node(
146147
this.repoDir = baseWorkingDir.resolve("repo");
147148
this.dataDir = workingDir.resolve("data");
148149
this.logsDir = workingDir.resolve("logs");
149-
this.configDir = workingDir.resolve("config");
150+
this.configDir = Optional.ofNullable(spec.getConfigDir()).orElse(workingDir.resolve("config"));
150151
this.tempDir = workingDir.resolve("tmp"); // elasticsearch temporary directory
151152
this.debugPort = DefaultLocalClusterHandle.NEXT_DEBUG_PORT.getAndIncrement();
152153
}
@@ -294,6 +295,10 @@ Path getWorkingDir() {
294295
return workingDir;
295296
}
296297

298+
Path getConfigDir() {
299+
return configDir;
300+
}
301+
297302
public void waitUntilReady() {
298303
try {
299304
Retry.retryUntilTrue(NODE_UP_TIMEOUT, Duration.ofMillis(500), () -> {
@@ -426,7 +431,7 @@ private void writeConfiguration() {
426431
try (Stream<Path> configFiles = Files.walk(distributionDir.resolve("config"))) {
427432
for (Path file : configFiles.toList()) {
428433
Path relativePath = distributionDir.resolve("config").relativize(file);
429-
Path dest = configDir.resolve(relativePath);
434+
Path dest = configDir.resolve(relativePath.toFile().getPath());
430435
if (Files.exists(dest) == false) {
431436
Files.createDirectories(dest.getParent());
432437
Files.copy(file, dest);
@@ -640,7 +645,7 @@ private void configureSecurity() {
640645
if (operators.isEmpty() == false) {
641646
// TODO: Support service accounts here
642647
final String operatorUsersFileName = "operator_users.yml";
643-
final Path destination = workingDir.resolve("config").resolve(operatorUsersFileName);
648+
final Path destination = configDir.resolve(operatorUsersFileName);
644649
if (Files.exists(destination)) {
645650
throw new IllegalStateException(
646651
"Operator users file ["
@@ -667,7 +672,7 @@ private void configureSecurity() {
667672
}
668673

669674
private void writeRolesFile() {
670-
Path destination = workingDir.resolve("config").resolve("roles.yml");
675+
Path destination = configDir.resolve("roles.yml");
671676
spec.getRolesFiles().forEach(rolesFile -> {
672677
try (
673678
Writer writer = Files.newBufferedWriter(destination, StandardOpenOption.APPEND);
@@ -857,7 +862,7 @@ private void startElasticsearch() {
857862

858863
private Map<String, String> getEnvironmentVariables() {
859864
Map<String, String> environment = new HashMap<>(spec.resolveEnvironment());
860-
environment.put("ES_PATH_CONF", workingDir.resolve("config").toString());
865+
environment.put("ES_PATH_CONF", configDir.toString());
861866
environment.put("ES_TMPDIR", workingDir.resolve("tmp").toString());
862867
// Windows requires this as it defaults to `c:\windows` despite ES_TMPDIR
863868
environment.put("TMP", workingDir.resolve("tmp").toString());

test/test-clusters/src/main/java/org/elasticsearch/test/cluster/local/AbstractLocalClusterSpecBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ private LocalNodeSpec build(LocalClusterSpec cluster, int nodeIndex) {
212212
getExtraConfigFiles(),
213213
getSystemPropertyProviders(),
214214
getSystemProperties(),
215-
getJvmArgs()
215+
getJvmArgs(),
216+
Optional.ofNullable(getConfigDirSupplier()).map(Supplier::get).orElse(null)
216217
);
217218
}
218219
}

test/test-clusters/src/main/java/org/elasticsearch/test/cluster/local/AbstractLocalSpecBuilder.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.elasticsearch.test.cluster.util.Version;
1818
import org.elasticsearch.test.cluster.util.resource.Resource;
1919

20+
import java.nio.file.Path;
2021
import java.util.ArrayList;
2122
import java.util.EnumSet;
2223
import java.util.HashMap;
@@ -47,6 +48,7 @@ public abstract class AbstractLocalSpecBuilder<T extends LocalSpecBuilder<?>> im
4748
private DistributionType distributionType;
4849
private Version version;
4950
private String keystorePassword;
51+
private Supplier<Path> configDirSupplier;
5052

5153
protected AbstractLocalSpecBuilder(AbstractLocalSpecBuilder<?> parent) {
5254
this.parent = parent;
@@ -270,6 +272,16 @@ public String getKeystorePassword() {
270272
return inherit(() -> parent.getKeystorePassword(), keystorePassword);
271273
}
272274

275+
@Override
276+
public T withConfigDir(Supplier<Path> configDirSupplier) {
277+
this.configDirSupplier = configDirSupplier;
278+
return cast(this);
279+
}
280+
281+
public Supplier<Path> getConfigDirSupplier() {
282+
return inherit(() -> parent.getConfigDirSupplier(), configDirSupplier);
283+
}
284+
273285
@Override
274286
public T version(Version version) {
275287
this.version = version;

test/test-clusters/src/main/java/org/elasticsearch/test/cluster/local/DefaultLocalClusterHandle.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,15 @@ private WaitForHttpResource configureWaitForReady() throws MalformedURLException
237237
private void configureWaitSecurity(WaitForHttpResource wait, Node node) {
238238
String caFile = node.getSpec().getSetting("xpack.security.http.ssl.certificate_authorities", null);
239239
if (caFile != null) {
240-
wait.setCertificateAuthorities(node.getWorkingDir().resolve("config").resolve(caFile).toFile());
240+
wait.setCertificateAuthorities(node.getConfigDir().resolve(caFile).toFile());
241241
}
242242
String sslCertFile = node.getSpec().getSetting("xpack.security.http.ssl.certificate", null);
243243
if (sslCertFile != null) {
244-
wait.setCertificateAuthorities(node.getWorkingDir().resolve("config").resolve(sslCertFile).toFile());
244+
wait.setCertificateAuthorities(node.getConfigDir().resolve(sslCertFile).toFile());
245245
}
246246
String sslKeystoreFile = node.getSpec().getSetting("xpack.security.http.ssl.keystore.path", null);
247247
if (sslKeystoreFile != null && caFile == null) { // Can not set both trust stores and CA
248-
wait.setTrustStoreFile(node.getWorkingDir().resolve("config").resolve(sslKeystoreFile).toFile());
248+
wait.setTrustStoreFile(node.getConfigDir().resolve(sslKeystoreFile).toFile());
249249
}
250250
String keystorePassword = node.getSpec().getSetting("xpack.security.http.ssl.keystore.secure_password", null);
251251
if (keystorePassword != null) {
@@ -254,7 +254,7 @@ private void configureWaitSecurity(WaitForHttpResource wait, Node node) {
254254
}
255255

256256
private boolean isSecurityAutoConfigured(Node node) {
257-
Path configFile = node.getWorkingDir().resolve("config").resolve("elasticsearch.yml");
257+
Path configFile = node.getConfigDir().resolve("elasticsearch.yml");
258258
try (Stream<String> lines = Files.lines(configFile)) {
259259
return lines.anyMatch(l -> l.contains("BEGIN SECURITY AUTO CONFIGURATION"));
260260
} catch (IOException e) {
@@ -273,7 +273,7 @@ private void writeUnicastHostsFile() {
273273
LOGGER.info("Skipping writing unicast hosts file for node {}", node.getName());
274274
return;
275275
}
276-
Path hostsFile = node.getWorkingDir().resolve("config").resolve("unicast_hosts.txt");
276+
Path hostsFile = node.getConfigDir().resolve("unicast_hosts.txt");
277277
LOGGER.info("Writing unicast hosts file {} for node {}", hostsFile, node.getName());
278278
Files.writeString(hostsFile, transportUris);
279279
} catch (IOException e) {

test/test-clusters/src/main/java/org/elasticsearch/test/cluster/local/LocalClusterSpec.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.elasticsearch.test.cluster.util.Version;
2020
import org.elasticsearch.test.cluster.util.resource.Resource;
2121

22+
import java.nio.file.Path;
2223
import java.util.HashMap;
2324
import java.util.List;
2425
import java.util.Map;
@@ -103,6 +104,7 @@ public static class LocalNodeSpec {
103104
private final List<SystemPropertyProvider> systemPropertyProviders;
104105
private final Map<String, String> systemProperties;
105106
private final List<String> jvmArgs;
107+
private final Path configDir;
106108
private Version version;
107109

108110
public LocalNodeSpec(
@@ -124,7 +126,8 @@ public LocalNodeSpec(
124126
Map<String, Resource> extraConfigFiles,
125127
List<SystemPropertyProvider> systemPropertyProviders,
126128
Map<String, String> systemProperties,
127-
List<String> jvmArgs
129+
List<String> jvmArgs,
130+
Path configDir
128131
) {
129132
this.cluster = cluster;
130133
this.name = name;
@@ -145,6 +148,7 @@ public LocalNodeSpec(
145148
this.systemPropertyProviders = systemPropertyProviders;
146149
this.systemProperties = systemProperties;
147150
this.jvmArgs = jvmArgs;
151+
this.configDir = configDir;
148152
}
149153

150154
void setVersion(Version version) {
@@ -203,6 +207,10 @@ public List<String> getJvmArgs() {
203207
return jvmArgs;
204208
}
205209

210+
public Path getConfigDir() {
211+
return configDir;
212+
}
213+
206214
public boolean isSecurityEnabled() {
207215
return Boolean.parseBoolean(getSetting("xpack.security.enabled", getVersion().onOrAfter("8.0.0") ? "true" : "false"));
208216
}
@@ -339,7 +347,8 @@ private LocalNodeSpec getFilteredSpec(SettingsProvider filteredProvider, Setting
339347
n.extraConfigFiles,
340348
n.systemPropertyProviders,
341349
n.systemProperties,
342-
n.jvmArgs
350+
n.jvmArgs,
351+
n.configDir
343352
)
344353
)
345354
.toList();

test/test-clusters/src/main/java/org/elasticsearch/test/cluster/local/LocalSpecBuilder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.elasticsearch.test.cluster.util.Version;
1919
import org.elasticsearch.test.cluster.util.resource.Resource;
2020

21+
import java.nio.file.Path;
2122
import java.util.function.Consumer;
2223
import java.util.function.Predicate;
2324
import java.util.function.Supplier;
@@ -160,4 +161,10 @@ interface LocalSpecBuilder<T extends LocalSpecBuilder<?>> {
160161
* Adds an additional command line argument to node JVM arguments.
161162
*/
162163
T jvmArg(String arg);
164+
165+
/**
166+
* Register a supplier to provide the config directory. The default config directory
167+
* is used when the supplier is null or the return value of the supplier is null.
168+
*/
169+
T withConfigDir(Supplier<Path> configDirSupplier);
163170
}

0 commit comments

Comments
 (0)