Skip to content

Instantly share code, notes, and snippets.

View dnatic09's full-sized avatar

Daniel Natic dnatic09

View GitHub Profile
@dnatic09
dnatic09 / Dockerfile-MultiStage-Scala
Created August 4, 2020 01:43
Example Dockerfile showing how to use multiple stages to build a Scala project
# First stage, SBT, identifer named `builder`
FROM mozilla/sbt:8u232_1.3.13 AS builder
# ADD all source
ADD . /
# Run SBT assembly, if tests fail, the docker image will not build
RUN sbt assembly
# Second stage, OpenJDK for JVM runtime
FROM adoptopenjdk/openjdk11:jdk-11.0.7_10-debian-slim
#############################################################################
# Assemble uber-jar using Mozilla's SBT Image
#############################################################################
FROM mozilla/sbt:8u232_1.3.8 as builder
ENV SBT_OPTS="-Xss2M -Xms512M -Xmx2G"
COPY . /
@dnatic09
dnatic09 / build.sbt
Created May 22, 2020 02:02
Dependency snippet to import TestContainers-Scala
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.0.8" % Test,
"com.dimafeng" %% "testcontainers-scala-scalatest" % "0.37.0" % Test
)
@dnatic09
dnatic09 / TestContainersExample2.scala
Created May 22, 2020 01:58
Example deep integration tests using TestContainers-Scala
class TestContainersExample2 extends TestContainersApiTestHarness {
override def beforeAll(): Unit = {
super.beforeAll()
importFile("/etc/data1.txt")
}
"TestContainersEx2" should "return NONE when the data does not exist" in {
val result = getDataId("keyThatIsBad")
result shouldBe Right(None)
}
@dnatic09
dnatic09 / TestContainersApiTestHarness.scala
Created May 22, 2020 01:53
Example test harness using TestContainers-Scala
trait TestContainersApiTestHarness extends FlatSpec with Matchers with BeforeAndAfterAll {
private val REDIS_PORT = 6379
private val container = GenericContainer("redis:5.0.8-alpine",
exposedPorts = Seq(REDIS_PORT),
waitStrategy = Wait.forListeningPort()
)
container.start()
private val mappedRedisExternalPort = container.mappedPort(REDIS_PORT)
private val redisClient = new RedisClient("localhost", mappedRedisExternalPort)
private val dao = new RedisTestContainersDao(redisClient)
@dnatic09
dnatic09 / TestContainersExample1.scala
Created May 22, 2020 01:21
Empty test that starts a Redis container
class TestContainersExample1 extends FlatSpec with Matchers with BeforeAndAfterAll {
private val container = GenericContainer("redis:5.0.8-alpine",
waitStrategy = Wait.forListeningPort()
)
container.start()
override def afterAll(): Unit = {
super.afterAll()
container.stop()
}