This is an automated email from the ASF dual-hosted git repository.

bowenliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kyuubi.git


The following commit(s) were added to refs/heads/master by this push:
     new cba1be973 [KYUUBI #4717] [AUTHZ] Check Authz plugin's spec json files 
in UT
cba1be973 is described below

commit cba1be97399c2f8370fd1120059916511591e2cf
Author: packyan <[email protected]>
AuthorDate: Thu Apr 20 09:41:08 2023 +0800

    [KYUUBI #4717] [AUTHZ] Check Authz plugin's spec json files in UT
    
    ### _Why are the changes needed?_
    
    to close #4715
    
    ### _How was this patch tested?_
    - [ ] Add some test cases that check the changes thoroughly including 
negative and positive cases if possible
    
    - [ ] Add screenshots for manual tests if appropriate
    
    - [x] [Run 
test](https://kyuubi.readthedocs.io/en/master/develop_tools/testing.html#running-tests)
 locally before make a pull request
    
    Closes #4717 from 
packyan/imporve_authz_spec_json_should_be_generated_in_each_build.
    
    Closes #4717
    
    88e70daa7 [Deng An] Update JsonSpecFileGenerator.scala
    d195a6db7 [Deng An] Merge branch 'master' into 
imporve_authz_spec_json_should_be_generated_in_each_build
    a078c8c53 [packyan] add ut for check or generate spec json files.
    
    Lead-authored-by: packyan <[email protected]>
    Co-authored-by: Deng An <[email protected]>
    Co-authored-by: Deng An <[email protected]>
    Signed-off-by: liangbowen <[email protected]>
---
 .../spark/authz/gen/JsonSpecFileGenerator.scala    | 55 ++++++++++++++++------
 1 file changed, 41 insertions(+), 14 deletions(-)

diff --git 
a/extensions/spark/kyuubi-spark-authz/src/test/scala/org/apache/kyuubi/plugin/spark/authz/gen/JsonSpecFileGenerator.scala
 
b/extensions/spark/kyuubi-spark-authz/src/test/scala/org/apache/kyuubi/plugin/spark/authz/gen/JsonSpecFileGenerator.scala
index 7c7ed138b..c95685f34 100644
--- 
a/extensions/spark/kyuubi-spark-authz/src/test/scala/org/apache/kyuubi/plugin/spark/authz/gen/JsonSpecFileGenerator.scala
+++ 
b/extensions/spark/kyuubi-spark-authz/src/test/scala/org/apache/kyuubi/plugin/spark/authz/gen/JsonSpecFileGenerator.scala
@@ -18,37 +18,64 @@
 package org.apache.kyuubi.plugin.spark.authz.gen
 
 import java.nio.charset.StandardCharsets
-import java.nio.file.{Files, Paths}
+import java.nio.file.{Files, Paths, StandardOpenOption}
+
+import org.apache.commons.io.FileUtils
+//scalastyle:off
+import org.scalatest.funsuite.AnyFunSuite
 
 import org.apache.kyuubi.plugin.spark.authz.serde.{mapper, CommandSpec}
 
 /**
  * Generates the default command specs to src/main/resources dir.
  *
- * Usage:
- * mvn scala:run -DmainClass=this class -pl :kyuubi-spark-authz_2.12
+ * To run the test suite:
+ * build/mvn clean test -Pgen-policy -pl :kyuubi-spark-authz_2.12 -Dtest=none
+ * 
-DwildcardSuites=org.apache.kyuubi.plugin.spark.authz.gen.JsonSpecFileGenerator
+ *
+ * To regenerate the ranger policy file:
+ * KYUUBI_UPDATE=1 build/mvn clean test -Pgen-policy -pl 
:kyuubi-spark-authz_2.12 -Dtest=none
+ * 
-DwildcardSuites=org.apache.kyuubi.plugin.spark.authz.gen.JsonSpecFileGenerator
  */
-object JsonSpecFileGenerator {
 
-  def main(args: Array[String]): Unit = {
+class JsonSpecFileGenerator extends AnyFunSuite {
+  // scalastyle:on
+  test("check spec json files") {
     writeCommandSpecJson("database", DatabaseCommands.data)
     writeCommandSpecJson("table", TableCommands.data ++ IcebergCommands.data)
     writeCommandSpecJson("function", FunctionCommands.data)
     writeCommandSpecJson("scan", Scans.data)
   }
 
-  def writeCommandSpecJson[T <: CommandSpec](commandType: String, specArr: 
Array[T]): Unit = {
+  def writeCommandSpecJson[T <: CommandSpec](
+      commandType: String,
+      specArr: Array[T]): Unit = {
     val pluginHome = 
getClass.getProtectionDomain.getCodeSource.getLocation.getPath
       .split("target").head
     val filename = s"${commandType}_command_spec.json"
-    val writer = {
-      val p = Paths.get(pluginHome, "src", "main", "resources", filename)
-      Files.newBufferedWriter(p, StandardCharsets.UTF_8)
+    val filePath = Paths.get(pluginHome, "src", "main", "resources", filename)
+
+    val generatedStr = mapper.writerWithDefaultPrettyPrinter()
+      .writeValueAsString(specArr.sortBy(_.classname))
+
+    if (sys.env.get("KYUUBI_UPDATE").contains("1")) {
+      // scalastyle:off println
+      println(s"writing ${specArr.length} specs to $filename")
+      // scalastyle:on println
+      Files.write(
+        filePath,
+        generatedStr.getBytes(StandardCharsets.UTF_8),
+        StandardOpenOption.CREATE,
+        StandardOpenOption.TRUNCATE_EXISTING)
+    } else {
+      val existedFileContent =
+        FileUtils.readFileToString(filePath.toFile, StandardCharsets.UTF_8)
+      withClue(s"Check $filename failed. Please regenerate the ranger policy 
file by running"
+        + "`KYUUBI_UPDATE=1 build/mvn clean test -Pgen-policy"
+        + " -pl :kyuubi-spark-authz_2.12 -Dtest=none"
+        + " 
-DwildcardSuites=org.apache.kyuubi.plugin.spark.authz.gen.JsonSpecFileGenerator`.")
 {
+        assert(generatedStr.equals(existedFileContent))
+      }
     }
-    // scalastyle:off println
-    println(s"writing ${specArr.length} specs to $filename")
-    // scalastyle:on println
-    mapper.writerWithDefaultPrettyPrinter().writeValue(writer, 
specArr.sortBy(_.classname))
-    writer.close()
   }
 }

Reply via email to