You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@kyuubi.apache.org by "bowenliang123 (via GitHub)" <gi...@apache.org> on 2023/04/19 14:49:02 UTC

[GitHub] [kyuubi] bowenliang123 commented on a diff in pull request #4717: [AUTHZ] Check Authz plugin's spec json files in UT

bowenliang123 commented on code in PR #4717:
URL: https://github.com/apache/kyuubi/pull/4717#discussion_r1171457642


##########
extensions/spark/kyuubi-spark-authz/src/test/scala/org/apache/kyuubi/plugin/spark/authz/gen/JsonSpecFileGenerator.scala:
##########
@@ -18,37 +18,67 @@
 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)
+      // scalastyle:off println
+      println(s"checking ${specArr.length} specs in $filename")

Review Comment:
   Remove this unnecessary print.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@kyuubi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@kyuubi.apache.org
For additional commands, e-mail: notifications-help@kyuubi.apache.org