소스 검색

Add cliSetup command to test clusters configuration (#50414)

This commit adds a cliSetup command that can be used to run arbitrary
bin scripts to setup a test cluster. This is the same as what was
previously called setupCommands in cluster formation tasks.

closes #50382
Ryan Ernst 5 년 전
부모
커밋
e32f4aa778

+ 5 - 0
buildSrc/src/main/java/org/elasticsearch/gradle/testclusters/ElasticsearchCluster.java

@@ -179,6 +179,11 @@ public class ElasticsearchCluster implements TestClusterConfiguration, Named {
         nodes.all(each -> each.keystore(key, valueSupplier));
     }
 
+    @Override
+    public void cliSetup(String binTool, CharSequence... args) {
+        nodes.all(each -> each.cliSetup(binTool, args));
+    }
+
     @Override
     public void setting(String key, String value) {
         nodes.all(each -> each.setting(key, value));

+ 44 - 5
buildSrc/src/main/java/org/elasticsearch/gradle/testclusters/ElasticsearchNode.java

@@ -124,6 +124,7 @@ public class ElasticsearchNode implements TestClusterConfiguration {
     private final LazyPropertyMap<String, CharSequence> settings = new LazyPropertyMap<>("Settings", this);
     private final LazyPropertyMap<String, CharSequence> keystoreSettings = new LazyPropertyMap<>("Keystore", this);
     private final LazyPropertyMap<String, File> keystoreFiles = new LazyPropertyMap<>("Keystore files", this, FileEntry::new);
+    private final LazyPropertyList<CliEntry> cliSetup = new LazyPropertyList<>("CLI setup commands", this);
     private final LazyPropertyMap<String, CharSequence> systemProperties = new LazyPropertyMap<>("System properties", this);
     private final LazyPropertyMap<String, CharSequence> environment = new LazyPropertyMap<>("Environment", this);
     private final LazyPropertyList<CharSequence> jvmArgs = new LazyPropertyList<>("JVM arguments", this);
@@ -301,6 +302,11 @@ public class ElasticsearchNode implements TestClusterConfiguration {
         keystoreFiles.put(key, valueSupplier);
     }
 
+    @Override
+    public void cliSetup(String binTool, CharSequence... args) {
+        cliSetup.add(new CliEntry(binTool, args));
+    }
+
     @Override
     public void setting(String key, String value) {
         settings.put(key, value);
@@ -475,6 +481,14 @@ public class ElasticsearchNode implements TestClusterConfiguration {
             ));
         }
 
+        if (cliSetup.isEmpty() == false) {
+            logToProcessStdout("Running " + cliSetup.size() + " setup commands");
+
+            for (CliEntry entry : cliSetup) {
+                runElasticsearchBinScript(entry.executable, entry.args);
+            }
+        }
+
         logToProcessStdout("Starting Elasticsearch process");
         startElasticsearchProcess();
     }
@@ -623,7 +637,7 @@ public class ElasticsearchNode implements TestClusterConfiguration {
         credentials.add(cred);
     }
 
-    private void runElasticsearchBinScriptWithInput(String input, String tool, String... args) {
+    private void runElasticsearchBinScriptWithInput(String input, String tool, CharSequence... args) {
         if (
             Files.exists(getDistroDir().resolve("bin").resolve(tool)) == false &&
                 Files.exists(getDistroDir().resolve("bin").resolve(tool + ".bat")) == false
@@ -642,12 +656,12 @@ public class ElasticsearchNode implements TestClusterConfiguration {
                         .supply()
                 );
                 spec.args(
-                    OS.<List<String>>conditional()
+                    OS.<List<CharSequence>>conditional()
                         .onWindows(() -> {
-                            ArrayList<String> result = new ArrayList<>();
+                            ArrayList<CharSequence> result = new ArrayList<>();
                             result.add("/c");
                             result.add("bin\\" + tool + ".bat");
-                            for (String arg : args) {
+                            for (CharSequence arg : args) {
                                 result.add(arg);
                             }
                             return result;
@@ -663,7 +677,7 @@ public class ElasticsearchNode implements TestClusterConfiguration {
         }
     }
 
-    private void runElasticsearchBinScript(String tool, String... args) {
+    private void runElasticsearchBinScript(String tool, CharSequence... args) {
         runElasticsearchBinScriptWithInput("", tool, args);
     }
 
@@ -1205,6 +1219,11 @@ public class ElasticsearchNode implements TestClusterConfiguration {
         return keystoreFiles.getNormalizedCollection();
     }
 
+    @Nested
+    public List<?> getCliSetup() {
+        return cliSetup.getNormalizedCollection();
+    }
+
     @Nested
     public List<?> getSettings() {
         return settings.getNormalizedCollection();
@@ -1376,4 +1395,24 @@ public class ElasticsearchNode implements TestClusterConfiguration {
             return file;
         }
     }
+
+    private static class CliEntry {
+        private String executable;
+        private CharSequence[] args;
+
+        CliEntry(String executable, CharSequence[] args) {
+            this.executable = executable;
+            this.args = args;
+        }
+
+        @Input
+        public String getExecutable() {
+            return executable;
+        }
+
+        @Input
+        public CharSequence[] getArgs() {
+            return args;
+        }
+    }
 }

+ 2 - 0
buildSrc/src/main/java/org/elasticsearch/gradle/testclusters/TestClusterConfiguration.java

@@ -58,6 +58,8 @@ public interface TestClusterConfiguration {
 
     void keystore(String key, FileSupplier valueSupplier);
 
+    void cliSetup(String binTool, CharSequence... args);
+
     void setting(String key, String value);
 
     void setting(String key, String value, PropertyNormalization normalization);