You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kyuubi.apache.org by ch...@apache.org on 2021/10/28 12:46:03 UTC

[incubator-kyuubi] branch master updated: [KYUUBI #1294] Refactor test suite about RESTful APIs to keep the unified Scala test style

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 0d76485  [KYUUBI #1294] Refactor test suite about RESTful APIs to keep the unified Scala test style
0d76485 is described below

commit 0d764857195177da7e8076f2624074b5b4d09b6f
Author: simon <zh...@cvte.com>
AuthorDate: Thu Oct 28 20:45:54 2021 +0800

    [KYUUBI #1294] Refactor test suite about RESTful APIs to keep the unified Scala test style
    
    ### _Why are the changes needed?_
    Move Junit to the scalatest, keep the same code style.
    
    ### _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
    
    - [ ] [Run test](https://kyuubi.readthedocs.io/en/latest/develop_tools/testing.html#running-tests) locally before make a pull request
    
    Closes #1298 from simon824/r.
    
    Closes #1294
    
    92521d9f [simon] fix failed to cancel future
    af7e59b9 [simon] fix codestyle
    18f4aa47 [simon] bugfix
    d3be6afc [simon] followup
    0e1fad1c [simon] bugfix
    0365e2da [simon] fix codestyle
    4916c9f5 [simon] fix codestyle
    0a627a67 [simon] bugfix
    df675898 [simon] bugfix
    4984b500 [simon] bugfix
    ca7161ca [simon] followup
    6e49da5e [simon] r
    
    Authored-by: simon <zh...@cvte.com>
    Signed-off-by: Cheng Pan <ch...@apache.org>
---
 .../kyuubi/server/RestFrontendServiceSuite.scala   | 45 ++++++++------
 .../server/api/v1/SessionsResourceSuite.scala      | 68 ++++++++++------------
 2 files changed, 58 insertions(+), 55 deletions(-)

diff --git a/kyuubi-server/src/test/scala/org/apache/kyuubi/server/RestFrontendServiceSuite.scala b/kyuubi-server/src/test/scala/org/apache/kyuubi/server/RestFrontendServiceSuite.scala
index 61348ce..2cb205c 100644
--- a/kyuubi-server/src/test/scala/org/apache/kyuubi/server/RestFrontendServiceSuite.scala
+++ b/kyuubi-server/src/test/scala/org/apache/kyuubi/server/RestFrontendServiceSuite.scala
@@ -18,25 +18,25 @@
 package org.apache.kyuubi.server
 
 import java.util.Locale
-import javax.ws.rs.core.Application
+import javax.ws.rs.client.WebTarget
+import javax.ws.rs.core.{Application, UriBuilder}
 
-import org.glassfish.jersey.client.ClientConfig
+import org.glassfish.jersey.client._
 import org.glassfish.jersey.server.ResourceConfig
-import org.glassfish.jersey.test.{JerseyTest, TestProperties}
+import org.glassfish.jersey.test.JerseyTest
 import org.glassfish.jersey.test.jetty.JettyTestContainerFactory
 import org.glassfish.jersey.test.spi.TestContainerFactory
-import org.junit.Test
 import org.scalatest.time.SpanSugar._
 import scala.io.Source
 
 import org.apache.kyuubi.KyuubiFunSuite
 import org.apache.kyuubi.config.KyuubiConf
-import org.apache.kyuubi.server.RestFrontendServiceSuite.{withKyuubiRestServer, TEST_SERVER_PORT}
+import org.apache.kyuubi.server.RestFrontendServiceSuite.withKyuubiRestServer
 import org.apache.kyuubi.server.api.KyuubiScalaObjectMapper
 import org.apache.kyuubi.service.NoopServer
 import org.apache.kyuubi.service.ServiceState._
 
-class RestFrontendServiceSuite extends KyuubiFunSuite{
+class RestFrontendServiceSuite extends KyuubiFunSuite {
 
   test("kyuubi rest frontend service basic") {
     val server = new RestFrontendServiceSuite.RestNoopServer()
@@ -70,7 +70,7 @@ class RestFrontendServiceSuite extends KyuubiFunSuite{
 
   test("kyuubi rest frontend service http basic") {
     RestFrontendServiceSuite.withKyuubiRestServer {
-      (_, host, port) =>
+      (_, host, port, _) =>
         eventually(timeout(10.seconds), interval(50.milliseconds)) {
           val html = Source.fromURL(s"http://$host:$port/api/v1/ping").mkString
           assert(html.toLowerCase(Locale.ROOT).equals("pong"))
@@ -86,20 +86,30 @@ object RestFrontendServiceSuite {
   }
 
   val TEST_SERVER_PORT = KyuubiConf().get(KyuubiConf.FRONTEND_REST_BIND_PORT)
+  val TEST_SERVER_HOST = "localhost"
+
+  def withKyuubiRestServer(f: (
+    RestFrontendService, String, Int, WebTarget) => Unit): Unit = {
 
-  def withKyuubiRestServer(f: (RestFrontendService, String, Int) => Unit): Unit = {
     val server = new RestNoopServer()
     server.stop()
     val conf = KyuubiConf()
-    conf.set(KyuubiConf.FRONTEND_REST_BIND_HOST, "localhost")
+    conf.set(KyuubiConf.FRONTEND_REST_BIND_HOST, TEST_SERVER_HOST)
 
     server.initialize(conf)
     server.start()
 
+    val restApiBaseSuite = new RestApiBaseSuite
+    restApiBaseSuite.setUp()
+    val baseUri = UriBuilder.fromUri(s"http://$TEST_SERVER_HOST/")
+      .port(TEST_SERVER_PORT).build(new Array[AnyRef](0))
+    val webTarget = restApiBaseSuite.client.target(baseUri)
+
     try {
       f(server.frontendServices.head, conf.get(KyuubiConf.FRONTEND_REST_BIND_HOST).get,
-        TEST_SERVER_PORT)
+        TEST_SERVER_PORT, webTarget)
     } finally {
+      restApiBaseSuite.tearDown()
       server.stop()
     }
   }
@@ -109,7 +119,6 @@ object RestFrontendServiceSuite {
 class RestApiBaseSuite extends JerseyTest {
 
   override def configure: Application = {
-    forceSet(TestProperties.CONTAINER_PORT, TEST_SERVER_PORT.toString)
     new ResourceConfig(getClass)
   }
 
@@ -122,24 +131,24 @@ class RestApiBaseSuite extends JerseyTest {
 
 }
 
-class RestErrorAndExceptionSuite extends RestApiBaseSuite {
+class RestErrorAndExceptionSuite extends KyuubiFunSuite {
 
-  @Test
-  def testErrorAndExceptionResponse: Unit = {
+  test("test error and exception response") {
     withKyuubiRestServer {
-      (_, _, _) =>
+      (_, _, _, webTarget) =>
+
         // send a not exists request
-        var response = target("api/v1/pong").request().get()
+        var response = webTarget.path("api/v1/pong").request().get()
         assert(404 == response.getStatus)
         assert(response.getStatusInfo.getReasonPhrase.equalsIgnoreCase("not found"))
 
         // send a exists request but wrong http method
-        response = target("api/v1/ping").request().post(null)
+        response = webTarget.path("api/v1/ping").request().post(null)
         assert(405 == response.getStatus)
         assert(response.getStatusInfo.getReasonPhrase.equalsIgnoreCase("method not allowed"))
 
         // send a request but throws a exception on the server side
-        response = target("api/v1/exception").request().get()
+        response = webTarget.path("api/v1/exception").request().get()
         assert(500 == response.getStatus)
         assert(response.getStatusInfo.getReasonPhrase.equalsIgnoreCase("server error"))
     }
diff --git a/kyuubi-server/src/test/scala/org/apache/kyuubi/server/api/v1/SessionsResourceSuite.scala b/kyuubi-server/src/test/scala/org/apache/kyuubi/server/api/v1/SessionsResourceSuite.scala
index 8ddb8b1..b0a7921 100644
--- a/kyuubi-server/src/test/scala/org/apache/kyuubi/server/api/v1/SessionsResourceSuite.scala
+++ b/kyuubi-server/src/test/scala/org/apache/kyuubi/server/api/v1/SessionsResourceSuite.scala
@@ -20,21 +20,19 @@ package org.apache.kyuubi.server.api.v1
 import javax.ws.rs.client.Entity
 import javax.ws.rs.core.{MediaType, Response}
 
-import org.junit.Test
-
-import org.apache.kyuubi.server.{RestApiBaseSuite, RestFrontendService, RestFrontendServiceSuite}
+import org.apache.kyuubi.KyuubiFunSuite
+import org.apache.kyuubi.server.{RestFrontendService, RestFrontendServiceSuite}
 import org.apache.kyuubi.session.SessionHandle
 
-class SessionsResourceSuite extends RestApiBaseSuite {
+class SessionsResourceSuite extends KyuubiFunSuite {
 
-  @Test
-  def testOpenAndCountSession: Unit = {
+  test("test open and count session") {
     val requestObj = SessionOpenRequest(
       1, "admin", "123456", "localhost", Map("testConfig" -> "testValue"))
 
     RestFrontendServiceSuite.withKyuubiRestServer {
-      (_, _, _) =>
-        var response = target(s"api/v1/sessions")
+      (_, _, _, webTarget) =>
+        var response = webTarget.path("api/v1/sessions")
           .request(MediaType.APPLICATION_JSON_TYPE)
           .post(Entity.entity(requestObj, MediaType.APPLICATION_JSON_TYPE))
 
@@ -46,20 +44,19 @@ class SessionsResourceSuite extends RestApiBaseSuite {
         assert(sessionHandle.identifier != null)
 
         // verify the open session count
-        response = target("api/v1/sessions/count").request().get()
+        response = webTarget.path("api/v1/sessions/count").request().get()
         val openedSessionCount = response.readEntity(classOf[SessionOpenCount])
         assert(openedSessionCount.openSessionCount == 1)
     }
   }
 
-  @Test
-  def testCloseAndCountSession: Unit = {
+  test("test close and count session") {
     val requestObj = SessionOpenRequest(
       1, "admin", "123456", "localhost", Map("testConfig" -> "testValue"))
 
     RestFrontendServiceSuite.withKyuubiRestServer {
-      (_, _, _) =>
-        var response = target(s"api/v1/sessions")
+      (_, _, _, webTarget) =>
+        var response = webTarget.path("api/v1/sessions")
           .request(MediaType.APPLICATION_JSON_TYPE)
           .post(Entity.entity(requestObj, MediaType.APPLICATION_JSON_TYPE))
 
@@ -73,20 +70,19 @@ class SessionsResourceSuite extends RestApiBaseSuite {
         // close a opened session
         val serializedSessionHandle = s"${sessionHandle.identifier.publicId}|" +
           s"${sessionHandle.identifier.secretId}|${sessionHandle.protocol.getValue}"
-        response = target(s"api/v1/sessions/$serializedSessionHandle").request().delete()
+        response = webTarget.path(s"api/v1/sessions/$serializedSessionHandle").request().delete()
         assert(200 == response.getStatus)
 
         // verify the open session count again
-        response = target("api/v1/sessions/count").request().get()
+        response = webTarget.path("api/v1/sessions/count").request().get()
         val openedSessionCount = response.readEntity(classOf[SessionOpenCount])
         assert(openedSessionCount.openSessionCount == 0)
     }
   }
 
-  @Test
-  def testExecPoolStatistic: Unit = {
+  test("test execPoolStatistic") {
     RestFrontendServiceSuite.withKyuubiRestServer {
-      (restFrontendService: RestFrontendService, _, _) =>
+      (restFrontendService: RestFrontendService, _, _, webTarget) =>
 
         val sessionManager = restFrontendService.be.sessionManager
         val future = sessionManager.submitBackgroundOperation(() => {
@@ -94,36 +90,36 @@ class SessionsResourceSuite extends RestApiBaseSuite {
         })
 
         // verify the exec pool statistic
-        var response = target("api/v1/sessions/execpool/statistic").request().get()
+        var response = webTarget.path("api/v1/sessions/execpool/statistic").request().get()
         val execPoolStatistic1 = response.readEntity(classOf[ExecPoolStatistic])
         assert(execPoolStatistic1.execPoolSize == 1 && execPoolStatistic1.execPoolActiveCount == 1)
 
-        future.cancel(true)
-        response = target("api/v1/sessions/execpool/statistic").request().get()
+        // if failed to cancel, need to wait the thread finish.
+        if (!future.cancel(true)) Thread.sleep(3000)
+        response = webTarget.path("api/v1/sessions/execpool/statistic").request().get()
         val execPoolStatistic2 = response.readEntity(classOf[ExecPoolStatistic])
         assert(execPoolStatistic2.execPoolSize == 1 && execPoolStatistic2.execPoolActiveCount == 0)
 
         sessionManager.stop()
-        response = target("api/v1/sessions/execpool/statistic").request().get()
+        response = webTarget.path("api/v1/sessions/execpool/statistic").request().get()
         val execPoolStatistic3 = response.readEntity(classOf[ExecPoolStatistic])
         assert(execPoolStatistic3.execPoolSize == 0 && execPoolStatistic3.execPoolActiveCount == 0)
 
     }
   }
 
-  @Test
-  def testGetSessionList: Unit = {
+  test("test getSessionList") {
     val requestObj = SessionOpenRequest(
       1, "admin", "123456", "localhost", Map("testConfig" -> "testValue"))
 
     RestFrontendServiceSuite.withKyuubiRestServer {
-      (_, _, _) =>
-        var response = target(s"api/v1/sessions")
+      (_, _, _, webTarget) =>
+        var response = webTarget.path("api/v1/sessions")
           .request(MediaType.APPLICATION_JSON_TYPE)
           .post(Entity.entity(requestObj, MediaType.APPLICATION_JSON_TYPE))
 
         // get session list
-        var response2 = target("api/v1/sessions").request().get()
+        var response2 = webTarget.path("api/v1/sessions").request().get()
         assert(200 == response2.getStatus)
         val sessions1 = response2.readEntity(classOf[SessionList])
         assert(sessions1.sessionList.nonEmpty)
@@ -132,25 +128,24 @@ class SessionsResourceSuite extends RestApiBaseSuite {
         val sessionHandle = response.readEntity(classOf[SessionHandle])
         val serializedSessionHandle = s"${sessionHandle.identifier.publicId}|" +
           s"${sessionHandle.identifier.secretId}|${sessionHandle.protocol.getValue}"
-        response = target(s"api/v1/sessions/$serializedSessionHandle").request().delete()
+        response = webTarget.path(s"api/v1/sessions/$serializedSessionHandle").request().delete()
         assert(200 == response.getStatus)
 
         // get session list again
-        response2 = target("api/v1/sessions").request().get()
+        response2 = webTarget.path("api/v1/sessions").request().get()
         assert(200 == response2.getStatus)
         val sessions2 = response2.readEntity(classOf[SessionList])
         assert(sessions2.sessionList.isEmpty)
     }
   }
 
-  @Test
-  def testGetSessionDetail: Unit = {
+  test("test getSessionDetail") {
     val requestObj = SessionOpenRequest(
       1, "admin", "123456", "localhost", Map("testConfig" -> "testValue"))
 
     RestFrontendServiceSuite.withKyuubiRestServer {
-      (_, _, _) =>
-        var response: Response = target(s"api/v1/sessions")
+      (_, _, _, webTarget) =>
+        var response: Response = webTarget.path("api/v1/sessions")
           .request(MediaType.APPLICATION_JSON_TYPE)
           .post(Entity.entity(requestObj, MediaType.APPLICATION_JSON_TYPE))
 
@@ -159,19 +154,18 @@ class SessionsResourceSuite extends RestApiBaseSuite {
           s"${sessionHandle.identifier.secretId}|${sessionHandle.protocol.getValue}"
 
         // get session detail
-        response = target(s"api/v1/sessions/$serializedSessionHandle").request().get()
+        response = webTarget.path(s"api/v1/sessions/$serializedSessionHandle").request().get()
         assert(200 == response.getStatus)
         var sessions = response.readEntity(classOf[SessionDetail])
         assert(sessions.configs.nonEmpty)
 
         // close a opened session
-        response = target(s"api/v1/sessions/$serializedSessionHandle").request().delete()
+        response = webTarget.path(s"api/v1/sessions/$serializedSessionHandle").request().delete()
         assert(200 == response.getStatus)
 
         // get session detail again
-        response = target(s"api/v1/sessions/$serializedSessionHandle").request().get()
+        response = webTarget.path(s"api/v1/sessions/$serializedSessionHandle").request().get()
         assert(404 == response.getStatus)
-
     }
   }
 }