Browse Source

Test fixtures improovements (#36037)

* Upgrae plugin to latest and expose udp
* Explicit check for windows
* Rename the properties for the port numbers
* Tasks for pre and pos container actions
Alpar Torok 6 years ago
parent
commit
c00d0fc814

+ 1 - 1
buildSrc/build.gradle

@@ -124,7 +124,7 @@ dependencies {
   compile "org.elasticsearch:jna:4.5.1"
   compile 'com.github.jengelman.gradle.plugins:shadow:2.0.4'
   compile 'de.thetaphi:forbiddenapis:2.6'
-  compile 'com.avast.gradle:docker-compose-gradle-plugin:0.4.5'
+  compile 'com.avast.gradle:gradle-docker-compose-plugin:0.8.12'
   testCompile "junit:junit:${props.getProperty('junit')}"
 }
 

+ 66 - 17
buildSrc/src/main/java/org/elasticsearch/gradle/testfixtures/TestFixturesPlugin.java

@@ -27,12 +27,15 @@ import org.gradle.api.Plugin;
 import org.gradle.api.Project;
 import org.gradle.api.Task;
 import org.gradle.api.plugins.BasePlugin;
+import org.gradle.api.plugins.ExtraPropertiesExtension;
 import org.gradle.api.tasks.Input;
 import org.gradle.api.tasks.TaskContainer;
+import org.gradle.internal.os.OperatingSystem;
 
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.Collections;
+import java.util.function.BiConsumer;
 
 public class TestFixturesPlugin implements Plugin<Project> {
 
@@ -54,7 +57,16 @@ public class TestFixturesPlugin implements Plugin<Project> {
             disableTaskByType(tasks, ThirdPartyAuditTask.class);
             disableTaskByType(tasks, JarHellTask.class);
 
+            Task buildFixture = project.getTasks().create("buildFixture");
+            Task preProcessFixture = project.getTasks().create("preProcessFixture");
+            buildFixture.dependsOn(preProcessFixture);
+            Task postProcessFixture = project.getTasks().create("postProcessFixture");
+            buildFixture.dependsOn(postProcessFixture);
+
             if (dockerComposeSupported(project) == false) {
+                preProcessFixture.setEnabled(false);
+                postProcessFixture.setEnabled(false);
+                buildFixture.setEnabled(false);
                 return;
             }
 
@@ -68,8 +80,18 @@ public class TestFixturesPlugin implements Plugin<Project> {
                     "/usr/local/bin/docker-compose" : "/usr/bin/docker-compose"
             );
 
-            project.getTasks().getByName("clean").dependsOn("composeDown");
+            buildFixture.dependsOn(tasks.getByName("composeUp"));
+            tasks.getByName("composeUp").mustRunAfter(preProcessFixture);
+            postProcessFixture.dependsOn("composeUp");
+
+            configureServiceInfoForTask(
+                postProcessFixture,
+                project,
+                (name, port) -> postProcessFixture.getExtensions()
+                    .getByType(ExtraPropertiesExtension.class).set(name, port)
+            );
         } else {
+            extension.fixtures.all(fixtureProject -> project.evaluationDependsOn(fixtureProject.getPath()));
             if (dockerComposeSupported(project) == false) {
                 project.getLogger().warn(
                     "Tests for {} require docker-compose at /usr/local/bin/docker-compose or /usr/bin/docker-compose " +
@@ -82,27 +104,54 @@ public class TestFixturesPlugin implements Plugin<Project> {
             }
             tasks.withType(getTaskClass("com.carrotsearch.gradle.junit4.RandomizedTestingTask"), task ->
                 extension.fixtures.all(fixtureProject -> {
-                    task.dependsOn(fixtureProject.getTasks().getByName("composeUp"));
-                    task.finalizedBy(fixtureProject.getTasks().getByName("composeDown"));
-                    // Configure ports for the tests as system properties.
-                    // We only know these at execution time so we need to do it in doFirst
-                    task.doFirst(it ->
-                        fixtureProject.getExtensions().getByType(ComposeExtension.class).getServicesInfos()
-                            .forEach((service, infos) ->
-                                infos.getPorts()
-                                    .forEach((container, host) -> setSystemProperty(
-                                        it,
-                                        "test.fixtures." + fixtureProject.getName() + "." + service + "." + container,
-                                        host
-                                    ))
-                            ));
-                }));
+                    fixtureProject.getTasks().matching(it->it.getName().equals("buildFixture")).all(buildFixture ->
+                        task.dependsOn(buildFixture)
+                    );
+                    fixtureProject.getTasks().matching(it->it.getName().equals("composeDown")).all(composeDown ->
+                        task.finalizedBy(composeDown)
+                    );
+                    configureServiceInfoForTask(
+                        task,
+                        fixtureProject,
+                        (name, port) -> setSystemProperty(task, name, port)
+                    );
+                })
+            );
         }
     }
 
+    private void configureServiceInfoForTask(Task task, Project fixtureProject, BiConsumer<String, Integer> consumer) {
+        // Configure ports for the tests as system properties.
+        // We only know these at execution time so we need to do it in doFirst
+        task.doFirst(theTask ->
+            fixtureProject.getExtensions().getByType(ComposeExtension.class).getServicesInfos()
+                .forEach((service, infos) -> {
+                    theTask.getLogger().info(
+                        "Port maps for {}\nTCP:{}\nUDP:{}\nexposed to {}",
+                        fixtureProject.getPath(),
+                        infos.getTcpPorts(),
+                        infos.getUdpPorts(),
+                        theTask.getPath()
+                    );
+                    infos.getTcpPorts()
+                        .forEach((container, host) -> consumer.accept(
+                            "test.fixtures." + service + ".tcp." + container,
+                            host
+                        ));
+                    infos.getUdpPorts()
+                        .forEach((container, host) -> consumer.accept(
+                            "test.fixtures." + service + ".udp." + container,
+                            host
+                        ));
+                })
+        );
+    }
+
     @Input
     public boolean dockerComposeSupported(Project project) {
-        // Don't look for docker-compose on the PATH yet that would pick up on Windows as well
+        if (OperatingSystem.current().isWindows()) {
+            return false;
+        }
         final boolean hasDockerCompose = project.file("/usr/local/bin/docker-compose").exists() ||
             project.file("/usr/bin/docker-compose").exists();
         return hasDockerCompose && Boolean.parseBoolean(System.getProperty("tests.fixture.enabled", "true"));

+ 0 - 0
test/fixtures/hdfs-fixture/docker-compose.yml


+ 2 - 2
x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/AbstractActiveDirectoryTestCase.java

@@ -154,9 +154,9 @@ public abstract class AbstractActiveDirectoryTestCase extends ESTestCase {
     }
 
     private static String getFromProperty(String port) {
-        String key = "test.fixtures.smb-fixture.fixture." + port;
+        String key = "test.fixtures.smb-fixture.tcp." + port;
         final String value = System.getProperty(key);
-        assertNotNull("Expected the actual value for " + port + " to be in system property " + key, value);
+        assertNotNull("Expected the actual value for port " + port + " to be in system property " + key, value);
         return value;
     }
 }

+ 1 - 1
x-pack/test/smb-fixture/docker-compose.yml

@@ -1,6 +1,6 @@
 version: '3'
 services:
-  fixture:
+  smb-fixture:
     build: 
       context: .
       dockerfile: Dockerfile