Skip to content

Commit

Permalink
[KYUUBI #1740] Use testcontainers scalatest in trino module
Browse files Browse the repository at this point in the history
<!--
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.
-->
Use testcontainers scalatest instead testcontainers

### _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

- [X] [Run test](https://kyuubi.readthedocs.io/en/latest/develop_tools/testing.html#running-tests) locally before make a pull request

Closes #1741 from hddong/use-testcontainers-scala-trino.

Closes #1740

3a12747 [hongdongdong] [KYUUBI #1740] Use testcontainers scalatest in trino module

Authored-by: hongdongdong <hongdongdong@cmss.chinamobile.com>
Signed-off-by: Cheng Pan <chengpan@apache.org>
  • Loading branch information
hddong authored and pan3793 committed Jan 13, 2022
1 parent 97f2e0e commit ab4184d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 55 deletions.
9 changes: 4 additions & 5 deletions externals/kyuubi-trino-engine/pom.xml
Expand Up @@ -65,15 +65,14 @@
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<groupId>com.dimafeng</groupId>
<artifactId>testcontainers-scala-scalatest_${scala.binary.version}</artifactId>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.testcontainers/trino -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>trino</artifactId>
<groupId>com.dimafeng</groupId>
<artifactId>testcontainers-scala-trino_${scala.binary.version}</artifactId>
<scope>test</scope>
</dependency>

Expand Down
Expand Up @@ -20,13 +20,13 @@ package org.apache.kyuubi.engine.trino
class TrinoContextSuite extends WithTrinoContainerServer {

test("set current schema") {
val trinoContext = TrinoContext(httpClient, session)
withTrinoContainer { trinoContext =>
val trinoStatement = TrinoStatement(trinoContext, kyuubiConf, "select 1")
assert("tiny" === trinoStatement.getCurrentDatabase)

val trinoStatement = TrinoStatement(trinoContext, kyuubiConf, "select 1")
assert("tiny" === trinoStatement.getCurrentDatabase)

trinoContext.setCurrentSchema("sf1")
val trinoStatement2 = TrinoStatement(trinoContext, kyuubiConf, "select 1")
assert("sf1" === trinoStatement2.getCurrentDatabase)
trinoContext.setCurrentSchema("sf1")
val trinoStatement2 = TrinoStatement(trinoContext, kyuubiConf, "select 1")
assert("sf1" === trinoStatement2.getCurrentDatabase)
}
}
}
Expand Up @@ -22,42 +22,47 @@ import org.apache.kyuubi.KyuubiSQLException
class TrinoStatementSuite extends WithTrinoContainerServer {

test("test query") {
val trinoStatement = TrinoStatement(TrinoContext(httpClient, session), kyuubiConf, "select 1")
val schema = trinoStatement.getColumns
val resultSet = trinoStatement.execute()
withTrinoContainer { trinoContext =>
val trinoStatement = TrinoStatement(trinoContext, kyuubiConf, "select 1")
val schema = trinoStatement.getColumns
val resultSet = trinoStatement.execute()

assert(schema.size === 1)
assert(schema(0).getName === "_col0")
assert(schema.size === 1)
assert(schema(0).getName === "_col0")

assert(resultSet.toIterator.hasNext)
assert(resultSet.toIterator.next() === List(1))
assert(resultSet.toIterator.hasNext)
assert(resultSet.toIterator.next() === List(1))

val trinoStatement2 =
TrinoStatement(TrinoContext(httpClient, session), kyuubiConf, "show schemas")
val schema2 = trinoStatement2.getColumns
val resultSet2 = trinoStatement2.execute()
val trinoStatement2 = TrinoStatement(trinoContext, kyuubiConf, "show schemas")
val schema2 = trinoStatement2.getColumns
val resultSet2 = trinoStatement2.execute()

assert(schema2.size === 1)
assert(resultSet2.toIterator.hasNext)
assert(schema2.size === 1)
assert(resultSet2.toIterator.hasNext)
}
}

test("test update session") {
val trinoStatement = TrinoStatement(TrinoContext(httpClient, session), kyuubiConf, "select 1")
val schema2 = trinoStatement.getColumns
withTrinoContainer { trinoContext =>
val trinoStatement = TrinoStatement(trinoContext, kyuubiConf, "select 1")
val schema2 = trinoStatement.getColumns

assert(schema2.size === 1)
assert(schema2(0).getName === "_col0")
assert(this.schema === trinoStatement.getCurrentDatabase)
assert(schema2.size === 1)
assert(schema2(0).getName === "_col0")
assert(this.schema === trinoStatement.getCurrentDatabase)

val trinoStatement2 = TrinoStatement(TrinoContext(httpClient, session), kyuubiConf, "use sf1")
trinoStatement2.execute()
val trinoStatement2 = TrinoStatement(trinoContext, kyuubiConf, "use sf1")
trinoStatement2.execute()

assert("sf1" === trinoStatement2.getCurrentDatabase)
assert("sf1" === trinoStatement2.getCurrentDatabase)
}
}

test("test exception") {
val trinoStatement = TrinoStatement(TrinoContext(httpClient, session), kyuubiConf, "use kyuubi")
val e1 = intercept[KyuubiSQLException](trinoStatement.execute())
assert(e1.getMessage.contains("Schema does not exist: tpch.kyuubi"))
withTrinoContainer { trinoContext =>
val trinoStatement = TrinoStatement(trinoContext, kyuubiConf, "use kyuubi")
val e1 = intercept[KyuubiSQLException](trinoStatement.execute())
assert(e1.getMessage.contains("Schema does not exist: tpch.kyuubi"))
}
}
}
Expand Up @@ -25,39 +25,38 @@ import java.util.concurrent.TimeUnit

import scala.collection.JavaConverters._

import com.dimafeng.testcontainers.TrinoContainer
import com.dimafeng.testcontainers.scalatest.TestContainerForAll
import io.airlift.units.Duration
import io.trino.client.ClientSelectedRole
import io.trino.client.ClientSession
import okhttp3.OkHttpClient
import org.testcontainers.containers.TrinoContainer
import org.testcontainers.utility.DockerImageName

import org.apache.kyuubi.KyuubiFunSuite
import org.apache.kyuubi.config.KyuubiConf

trait WithTrinoContainerServer extends KyuubiFunSuite {
trait WithTrinoContainerServer extends KyuubiFunSuite with TestContainerForAll {

final val IMAGE_VERSION = 363
final val DOCKER_IMAGE_NAME = s"trinodb/trino:${IMAGE_VERSION}"

val trino = new TrinoContainer(DOCKER_IMAGE_NAME)
override val containerDef = TrinoContainer.Def(DockerImageName.parse(DOCKER_IMAGE_NAME))

val kyuubiConf: KyuubiConf = KyuubiConf()

protected val catalog = "tpch"
protected val schema = "tiny"

override def beforeAll(): Unit = {
trino.start()
super.beforeAll()
}

override def afterAll(): Unit = {
trino.stop()
super.afterAll()
def withTrinoContainer(tc: TrinoContext => Unit): Unit = {
withContainers { trinoContainer =>
val connectionUrl = trinoContainer.jdbcUrl.replace("jdbc:trino", "http")
val trinoContext = TrinoContext(httpClient, session(connectionUrl))
tc(trinoContext)
}
}

lazy val connectionUrl = trino.getJdbcUrl.replace("jdbc:trino", "http")

lazy val session = new ClientSession(
protected def session(connectionUrl: String): ClientSession = new ClientSession(
URI.create(connectionUrl),
"kyuubi_test",
Optional.empty(),
Expand Down
10 changes: 5 additions & 5 deletions pom.xml
Expand Up @@ -137,7 +137,7 @@
<spark.archive.download.skip>false</spark.archive.download.skip>
<swagger.version>2.1.11</swagger.version>
<swagger-ui.version>4.1.3</swagger-ui.version>
<testcontainers.version>1.16.2</testcontainers.version>
<testcontainers.version>0.39.12</testcontainers.version>
<trino.client.version>363</trino.client.version>
<zookeeper.version>3.4.14</zookeeper.version>

Expand Down Expand Up @@ -455,14 +455,14 @@
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<groupId>com.dimafeng</groupId>
<artifactId>testcontainers-scala-scalatest_${scala.binary.version}</artifactId>
<version>${testcontainers.version}</version>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>trino</artifactId>
<groupId>com.dimafeng</groupId>
<artifactId>testcontainers-scala-trino_${scala.binary.version}</artifactId>
<version>${testcontainers.version}</version>
</dependency>

Expand Down

0 comments on commit ab4184d

Please sign in to comment.