Skip to content

Commit e1587ee

Browse files
committed
[KYUUBI #1631] Migrating existing rest fe tests to real cases
<!-- Thanks for sending a pull request! Here are some tips for you: 1. If this is your first time, please read our contributor guidelines: https://kyuubi.readthedocs.io/en/latest/community/contributions.html 2. If the PR is related to an issue in https://github.com/apache/incubator-kyuubi/issues, add '[KYUUBI #XXXX]' in your PR title, e.g., '[KYUUBI #XXXX] Your PR title ...'. 3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][KYUUBI #XXXX] Your PR title ...'. --> ### _Why are the changes needed?_ <!-- Please clarify why the changes are needed. For instance, 1. If you add a feature, you can talk about the use case of it. 2. If you fix a bug, you can clarify why it is a bug. --> In this PR, we target the existing UTs from a noop server to a real shared Kyuubi server. ### _How was this patch tested?_ - [x] 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 #1631 from yaooqinn/resttest. Closes #1631 2726ab4 [Kent Yao] address comments c0da809 [Kent Yao] ci 5d12f70 [Kent Yao] address comments 80e1404 [Kent Yao] Migrating existing rest fe test to real cases a31ab50 [Kent Yao] Migrating existing rest fe test to real cases 0c1feb4 [Kent Yao] Migrating existing rest fe test to real cases fbd9769 [Kent Yao] Migrating existing rest fe test to real cases 75cca5f [Kent Yao] Migrating existing rest fe test to real cases 119712e [Kent Yao] Migrating existing rest fe test to real cases 8196e4d [Kent Yao] Migrating existing rest fe test to real cases Authored-by: Kent Yao <yao@apache.org> Signed-off-by: Kent Yao <yao@apache.org>
1 parent 2af105a commit e1587ee

File tree

8 files changed

+314
-462
lines changed

8 files changed

+314
-462
lines changed

kyuubi-server/src/main/scala/org/apache/kyuubi/server/KyuubiRestFrontendService.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class KyuubiRestFrontendService(override val serverable: Serverable)
7676

7777
override def connectionUrl: String = {
7878
checkInitialized()
79-
s"${serverAddr.getCanonicalHostName}:$portNum"
79+
s"${serverAddr.getCanonicalHostName}:${connector.getLocalPort}"
8080
}
8181

8282
override def start(): Unit = {

kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/v1/SessionsResource.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,15 @@ private[v1] class SessionsResource extends ApiRequestContext {
211211
@PathParam("sessionHandle") sessionHandleStr: String,
212212
request: GetSchemasRequest): OperationHandle = {
213213
try {
214-
backendService.getSchemas(
215-
parseSessionHandle(sessionHandleStr),
214+
val sessionHandle = parseSessionHandle(sessionHandleStr)
215+
val operationHandle = backendService.getSchemas(
216+
sessionHandle,
216217
request.catalogName,
217218
request.schemaName)
219+
operationHandle
218220
} catch {
219-
case NonFatal(_) =>
220-
throw new NotFoundException(s"Error getting schemas")
221+
case NonFatal(e) =>
222+
throw new NotFoundException(s"Error getting schemas", e)
221223
}
222224
}
223225

kyuubi-server/src/test/scala/org/apache/kyuubi/RestFrontendTestHelper.scala

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.kyuubi
1919

20+
import java.net.URI
2021
import javax.ws.rs.client.WebTarget
2122
import javax.ws.rs.core.{Application, UriBuilder}
2223

@@ -28,9 +29,10 @@ import org.glassfish.jersey.test.spi.TestContainerFactory
2829

2930
import org.apache.kyuubi.RestFrontendTestHelper.RestApiBaseSuite
3031
import org.apache.kyuubi.config.KyuubiConf
31-
import org.apache.kyuubi.server.KyuubiRestFrontendService
32+
import org.apache.kyuubi.config.KyuubiConf.FrontendProtocols
33+
import org.apache.kyuubi.config.KyuubiConf.FrontendProtocols.FrontendProtocol
3234
import org.apache.kyuubi.server.api.KyuubiScalaObjectMapper
33-
import org.apache.kyuubi.service.NoopRestFrontendServer
35+
import org.apache.kyuubi.service.AbstractFrontendService
3436

3537
object RestFrontendTestHelper {
3638

@@ -46,40 +48,28 @@ object RestFrontendTestHelper {
4648
}
4749
}
4850

49-
trait RestFrontendTestHelper {
51+
trait RestFrontendTestHelper extends WithKyuubiServer {
5052

51-
val restFrontendHost: String = "localhost"
52-
val restFrontendPort: Int = KyuubiConf().get(KyuubiConf.FRONTEND_REST_BIND_PORT)
53+
override protected val conf: KyuubiConf = KyuubiConf()
5354

54-
def withKyuubiRestServer(
55-
f: (KyuubiRestFrontendService, String, Int, WebTarget) => Unit): Unit = {
55+
override protected val frontendProtocols: Seq[FrontendProtocol] =
56+
FrontendProtocols.REST :: Nil
5657

57-
val server = new NoopRestFrontendServer()
58-
server.stop()
59-
val conf = KyuubiConf()
60-
conf.set(KyuubiConf.FRONTEND_REST_BIND_HOST, Some(restFrontendHost))
58+
private val restApiBaseSuite = new RestApiBaseSuite
6159

62-
server.initialize(conf)
63-
server.start()
64-
65-
val restApiBaseSuite = new RestApiBaseSuite
60+
override def beforeAll(): Unit = {
61+
super.beforeAll()
6662
restApiBaseSuite.setUp()
67-
// noinspection HttpUrlsUsage
68-
val baseUri = UriBuilder
69-
.fromUri(s"http://$restFrontendHost/")
70-
.port(restFrontendPort)
71-
.build()
72-
val webTarget = restApiBaseSuite.client.target(baseUri)
73-
74-
try {
75-
f(
76-
server.frontendServices.head,
77-
conf.get(KyuubiConf.FRONTEND_REST_BIND_HOST).get,
78-
restFrontendPort,
79-
webTarget)
80-
} finally {
81-
restApiBaseSuite.tearDown()
82-
server.stop()
83-
}
8463
}
64+
65+
override def afterAll(): Unit = {
66+
restApiBaseSuite.tearDown()
67+
super.afterAll()
68+
}
69+
70+
protected lazy val fe: AbstractFrontendService = server.frontendServices.head
71+
72+
protected lazy val baseUri: URI = UriBuilder.fromUri(s"http://${fe.connectionUrl}/").build()
73+
74+
protected lazy val webTarget: WebTarget = restApiBaseSuite.client.target(baseUri)
8575
}

kyuubi-server/src/test/scala/org/apache/kyuubi/WithKyuubiServer.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package org.apache.kyuubi
1919

2020
import org.apache.kyuubi.config.KyuubiConf
2121
import org.apache.kyuubi.config.KyuubiConf._
22+
import org.apache.kyuubi.config.KyuubiConf.FrontendProtocols.FrontendProtocol
2223
import org.apache.kyuubi.ha.HighAvailabilityConf.{HA_ZK_AUTH_TYPE, HA_ZK_QUORUM}
2324
import org.apache.kyuubi.ha.client.ZooKeeperAuthTypes
2425
import org.apache.kyuubi.server.KyuubiServer
@@ -28,7 +29,7 @@ trait WithKyuubiServer extends KyuubiFunSuite {
2829

2930
protected val conf: KyuubiConf
3031

31-
protected val frontendProtocols: Seq[FrontendProtocols.Value] =
32+
protected val frontendProtocols: Seq[FrontendProtocol] =
3233
FrontendProtocols.THRIFT_BINARY :: Nil
3334

3435
private var zkServer: EmbeddedZookeeper = _

kyuubi-server/src/test/scala/org/apache/kyuubi/server/KyuubiRestFrontendServiceSuite.scala

Lines changed: 22 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -17,80 +17,36 @@
1717

1818
package org.apache.kyuubi.server
1919

20-
import java.util.Locale
20+
import org.apache.kyuubi.RestFrontendTestHelper
2121

22-
import scala.io.Source
22+
class KyuubiRestFrontendServiceSuite extends RestFrontendTestHelper {
2323

24-
import org.scalatest.time.SpanSugar._
25-
26-
import org.apache.kyuubi.{KyuubiFunSuite, RestFrontendTestHelper}
27-
import org.apache.kyuubi.config.KyuubiConf
28-
import org.apache.kyuubi.service.NoopRestFrontendServer
29-
import org.apache.kyuubi.service.ServiceState._
30-
31-
class KyuubiRestFrontendServiceSuite extends KyuubiFunSuite with RestFrontendTestHelper {
32-
33-
test("kyuubi REST frontend service basic") {
34-
val server = new NoopRestFrontendServer()
35-
server.stop()
36-
val conf = KyuubiConf()
37-
assert(server.getServices.isEmpty)
38-
assert(server.getServiceState === LATENT)
39-
val e = intercept[IllegalStateException](server.frontendServices.head.connectionUrl)
40-
assert(e.getMessage startsWith "Illegal Service State: LATENT")
41-
assert(server.getConf === null)
24+
test("kyuubi REST frontend service http basic") {
25+
val resp = webTarget.path("/api/v1/ping").request().get()
26+
assert(resp.readEntity(classOf[String]) === "pong")
27+
}
4228

43-
server.initialize(conf)
44-
assert(server.getServiceState === INITIALIZED)
45-
val frontendService = server.frontendServices.head
46-
assert(frontendService.getServiceState == INITIALIZED)
47-
assert(server.frontendServices.head.connectionUrl.split(":").length === 2)
48-
assert(server.getConf === conf)
49-
assert(server.getStartTime === 0)
50-
server.stop()
29+
test("error and exception response") {
30+
var response = webTarget.path("api/v1/pong").request().get()
31+
assert(404 == response.getStatus)
32+
assert(response.getStatusInfo.getReasonPhrase.equalsIgnoreCase("not found"))
5133

52-
server.start()
53-
assert(server.getServiceState === STARTED)
54-
assert(frontendService.getServiceState == STARTED)
55-
assert(server.getStartTime !== 0)
34+
response = webTarget.path("api/v1/ping").request().post(null)
35+
assert(405 == response.getStatus)
36+
assert(response.getStatusInfo.getReasonPhrase.equalsIgnoreCase("method not allowed"))
5637

57-
server.stop()
58-
assert(server.getServiceState === STOPPED)
59-
assert(frontendService.getServiceState == STOPPED)
60-
server.stop()
38+
response = webTarget.path("api/v1/exception").request().get()
39+
assert(500 == response.getStatus)
40+
assert(response.getStatusInfo.getReasonPhrase.equalsIgnoreCase("server error"))
6141
}
6242

63-
test("kyuubi REST frontend service http basic") {
64-
withKyuubiRestServer { (_, host, port, _) =>
65-
eventually(timeout(10.seconds), interval(50.milliseconds)) {
66-
val html = {
67-
// noinspection HttpUrlsUsage
68-
val s = Source.fromURL(s"http://$host:$port/api/v1/ping")
69-
val str = s.mkString
70-
s.close()
71-
str
72-
}
73-
assert(html.toLowerCase(Locale.ROOT).equals("pong"))
74-
}
75-
}
43+
test("swagger ui") {
44+
val resp = webTarget.path("/api/v1/swagger-ui").request().get()
45+
assert(resp.getStatus === 200)
7646
}
7747

78-
test("test error and exception response") {
79-
withKyuubiRestServer { (_, _, _, webTarget) =>
80-
// send a not exists request
81-
var response = webTarget.path("api/v1/pong").request().get()
82-
assert(404 == response.getStatus)
83-
assert(response.getStatusInfo.getReasonPhrase.equalsIgnoreCase("not found"))
84-
85-
// send a exists request but wrong http method
86-
response = webTarget.path("api/v1/ping").request().post(null)
87-
assert(405 == response.getStatus)
88-
assert(response.getStatusInfo.getReasonPhrase.equalsIgnoreCase("method not allowed"))
89-
90-
// send a request but throws a exception on the server side
91-
response = webTarget.path("api/v1/exception").request().get()
92-
assert(500 == response.getStatus)
93-
assert(response.getStatusInfo.getReasonPhrase.equalsIgnoreCase("server error"))
94-
}
48+
test("swagger ui json data") {
49+
val resp = webTarget.path("/openapi.json").request().get()
50+
assert(resp.getStatus === 200)
9551
}
9652
}

0 commit comments

Comments
 (0)