瀏覽代碼

Make reuse of sql test code explicit (#45884)

The sql project uses a common set of security tests, which are run in
subprojects. Currently these are shared through a shared directory, but
this is not setup correctly to ensure it is built before tests run. This
commit changes the test classes to be an artifact of the sql/qa/security
project and makes the test runner use the built artifact (a directory of
classes) for tests.

closes #45866
Ryan Ernst 6 年之前
父節點
當前提交
e2977dceba

+ 12 - 2
x-pack/plugin/sql/qa/build.gradle

@@ -47,7 +47,16 @@ forbiddenApisMain {
 thirdPartyAudit.enabled = false
 
 subprojects {
-  apply plugin: 'elasticsearch.standalone-rest-test'
+  if (subprojects.isEmpty()) {
+    // leaf project
+    apply plugin: 'elasticsearch.standalone-rest-test'
+  } else {
+    apply plugin: 'elasticsearch.build'
+  }
+
+  configurations.testRuntimeClasspath {
+    resolutionStrategy.force "org.slf4j:slf4j-api:1.7.25"
+  }
   dependencies {
     
     /* Since we're a standalone rest test we actually get transitive
@@ -65,7 +74,8 @@ subprojects {
 
     // H2GIS testing dependencies
     testRuntime ("org.orbisgis:h2gis:${h2gisVersion}") {
-        exclude group: "org.locationtech.jts"
+      exclude group: "org.locationtech.jts"
+      exclude group: "com.fasterxml.jackson.core"
     }
   
     testRuntime project(path: xpackModule('sql:jdbc'), configuration: 'nodeps')

+ 27 - 14
x-pack/plugin/sql/qa/security/build.gradle

@@ -1,3 +1,4 @@
+
 dependencies {
   testCompile project(':x-pack:plugin:core')
 }
@@ -6,27 +7,27 @@ Project mainProject = project
 
 group = "${group}.x-pack.qa.sql.security"
 
+configurations.create('testArtifacts')
+
+TaskProvider testJar = tasks.register("testJar", Jar) {
+  appendix 'test'
+  from sourceSets.test.output
+}
+
+artifacts {
+  testArtifacts testJar
+}
+
 // Tests are pushed down to subprojects and will be checked there.
 testingConventions.enabled = false
 
 subprojects {
-  // Use resources from the parent project in subprojects
-  sourceSets {
-    test {
-      mainProject.sourceSets.test.output.classesDirs.each { dir ->
-        output.addClassesDir { dir }
-        output.builtBy(mainProject.tasks.testClasses)
-      }
-      runtimeClasspath += mainProject.sourceSets.test.output
-    }
-  }
-
-  processTestResources {
-    from mainProject.file('src/test/resources')
-  }
+  // Use tests from the root security qa project in subprojects
+  configurations.create('testArtifacts')
 
   dependencies {
     testCompile project(":x-pack:plugin:core")
+    testArtifacts project(path: mainProject.path, configuration: 'testArtifacts')
   }
 
   testClusters.integTest {
@@ -42,10 +43,22 @@ subprojects {
     user username: "test_admin", password: "x-pack-test-password"
   }
 
+  File testArtifactsDir = project.file("$buildDir/testArtifacts")
+  TaskProvider copyTestClasses = tasks.register("copyTestClasses", Copy) {
+    dependsOn configurations.testArtifacts
+    from { zipTree(configurations.testArtifacts.singleFile) }
+    into testArtifactsDir
+  }
+
   integTest.runner {
+    dependsOn copyTestClasses
+    testClassesDirs += project.files(testArtifactsDir)
+    classpath += configurations.testArtifacts
     nonInputProperties.systemProperty 'tests.audit.logfile',
         "${ -> testClusters.integTest.singleNode().getAuditLog()}"
     nonInputProperties.systemProperty 'tests.audit.yesterday.logfile',
         "${ -> testClusters.integTest.singleNode().getAuditLog().getParentFile()}/integTest_audit-${new Date().format('yyyy-MM-dd')}.json"
   }
+
+  testingConventions.enabled = false
 }

+ 3 - 1
x-pack/plugin/sql/qa/security/src/test/java/org/elasticsearch/xpack/sql/qa/security/SqlSecurityTestCase.java

@@ -641,7 +641,9 @@ public abstract class SqlSecurityTestCase extends ESRestTestCase {
                                 assertThat(log.containsKey("user.name"), is(true));
                                 List<String> indices = new ArrayList<>();
                                 if (log.containsKey("indices")) {
-                                    indices = (ArrayList<String>) log.get("indices");
+                                    @SuppressWarnings("unchecked")
+                                    List<String> castIndices = (ArrayList<String>) log.get("indices");
+                                    indices = castIndices;
                                     if ("test_admin".equals(log.get("user.name"))) {
                                         /*
                                          * Sometimes we accidentally sneak access to the security tables. This is fine,

+ 1 - 1
x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/JdbcTestUtils.java

@@ -194,7 +194,7 @@ final class JdbcTestUtils {
                 }
             }
             // normal file access
-            else {
+            else if (Files.isDirectory(path)) {
                 Files.walkFileTree(path, EnumSet.allOf(FileVisitOption.class), 1, new SimpleFileVisitor<Path>() {
                     @Override
                     public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {