Skip to content

Commit

Permalink
Add DockerClickhouseService to facilitate clickhouse integration in d…
Browse files Browse the repository at this point in the history
…ocker (#152)
  • Loading branch information
XunGuo1992 authored Feb 14, 2024
1 parent 306cdbb commit 6ad2022
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export DOCKER_HOST=unix:///var/run/docker.sock
- [Mongodb](https://github.com/whisklabs/docker-it-scala/blob/master/samples/src/main/scala/com/whisk/docker/testkit/DockerMongodbService.scala)
- [MySQL](https://github.com/whisklabs/docker-it-scala/blob/master/samples/src/main/scala/com/whisk/docker/testkit/DockerMysqlService.scala)
- [Postgres](https://github.com/whisklabs/docker-it-scala/blob/master/samples/src/main/scala/com/whisk/docker/testkit/DockerPostgresService.scala)
- [Clickhouse](https://github.com/whisklabs/docker-it-scala/blob/master/samples/src/main/scala/com/whisk/docker/testkit/DockerClickhouseService.scala)
- [Multi container test](https://github.com/whisklabs/docker-it-scala/blob/master/tests/src/test/scala/com/whisk/docker/testkit/test/MultiContainerTest.scala)

# Defining Containers

There are two ways to define a docker container.
Expand Down Expand Up @@ -76,7 +76,7 @@ You can check [usage example](https://github.com/whisklabs/docker-it-scala/blob/
- Mongodb => `docker.mongo`
- Neo4j => `docker.mysql`
- Postgres => `docker.postgres`

- Clickhouse => `docker.clickhouse`
### Fields

- `image-name` required (String)
Expand Down
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ lazy val tests =
name := "docker-testkit-tests",
libraryDependencies ++= Seq(
"org.postgresql" % "postgresql" % "42.1.4" % "test",
"mysql" % "mysql-connector-java" % "5.1.44" % "test"
"mysql" % "mysql-connector-java" % "5.1.44" % "test",
"com.clickhouse" % "clickhouse-jdbc" % "0.5.0" % "test"
)
)
.dependsOn(core, scalatest, samples % "test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ object DockerReadyChecker {
"mysql"
} else if (driverLower.contains("postgres")) {
"postgresql"
} else if (driverLower.contains("clickhouse")) {
"clickhouse"
} else {
throw new IllegalArgumentException("unsupported database for ready check")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.whisk.docker.testkit

import com.spotify.docker.client.messages.PortBinding
import com.whisk.docker.testkit.scalatest.DockerTestKitForAll
import org.scalatest.Suite

import scala.concurrent.duration._

trait DockerClickhouseService extends DockerTestKitForAll { self: Suite =>
override val dockerTestTimeouts: DockerTestTimeouts = DockerTestTimeouts(pull = 10.minutes, init = 10.minutes, stop = 1.minutes)

def ClickhouseAdvertisedPort = 8123
def ClickhouseExposedPort = 8123

val ClickhouseUser = "default"
val ClickhousePassword = ""

val clickhouseContainer = ContainerSpec("clickhouse/clickhouse-server:23.6")
.withEnv(s"CLICKHOUSE_USER=$ClickhouseUser", s"CLICKHOUSE_PASSWORD=$ClickhousePassword")
.withPortBindings((ClickhouseAdvertisedPort, PortBinding.of("0.0.0.0", ClickhouseExposedPort)))
.withReadyChecker(
DockerReadyChecker
.Jdbc(
driverClass = "com.clickhouse.jdbc.ClickHouseDriver",
user = ClickhouseUser,
password = Some(ClickhousePassword)
)
.looped(15, 1.second)
)
.toContainer

override val managedContainers: ManagedContainers = clickhouseContainer.toManagedContainer
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.whisk.docker.testkit.test

import com.whisk.docker.testkit.{ContainerState, DockerClickhouseService}
import org.scalatest.funsuite.AnyFunSuite

class ClickhouseServiceTest extends AnyFunSuite with DockerClickhouseService {
test("test container started") {
assert(clickhouseContainer.state().isInstanceOf[ContainerState.Ready], "clickhouse is ready")
assert(clickhouseContainer.mappedPortOpt(ClickhouseAdvertisedPort) === Some(ClickhouseExposedPort),
"clickhouse port exposed")
}
}

0 comments on commit 6ad2022

Please sign in to comment.