Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 2, 2023 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/1320885b0b0207e9de7d63f786800ad0 to your computer and use it in GitHub Desktop.
Save dacr/1320885b0b0207e9de7d63f786800ad0 to your computer and use it in GitHub Desktop.
scala with docker testing using docker-testkit library. / published by https://github.com/dacr/code-examples-manager #84841154-519a-447a-a4b6-ff8bd5a053a4/a7f54f6d59eb9d2674ddd2f0ce3fba8e323e4cb1
// summary : scala with docker testing using docker-testkit library.
// keywords : scala, scalatest, docker, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 84841154-519a-447a-a4b6-ff8bd5a053a4
// created-on : 2020-08-14T14:37:10Z
// managed-by : https://github.com/dacr/code-examples-manager
// execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc'
import $ivy.`org.scalatest::scalatest:3.2.6`
import $ivy.`com.whisk::docker-testkit-impl-spotify:0.9.9`
import $ivy.`com.softwaremill.sttp.client::core:2.2.6`
import $ivy.`com.softwaremill.sttp.client::json4s:2.2.6`
import $ivy.`org.slf4j:slf4j-nop:1.7.30` // to avoid missing logging impl warning at startup
import $ivy.`org.json4s::json4s-jackson:3.6.11`
import $ivy.`javax.activation:activation:1.1.1` // workaround for startup exception messages with java>=11
import org.scalatest._, flatspec._, matchers._, OptionValues._
import com.whisk.docker._
import sttp.client.quick._, sttp.client.json4s._
import org.json4s.JValue, org.json4s.jackson.Serialization
// ==================================================================================================
// Provided DockerTestKit allow to have control over the used scalatest release
trait DockerTestKit extends BeforeAndAfterAll with org.scalatest.concurrent.ScalaFutures with DockerKit {
self: Suite =>
import org.scalatest.time.{Span, Seconds, Millis}
def dockerInitPatienceInterval =
PatienceConfig(scaled(Span(20, Seconds)), scaled(Span(10, Millis)))
def dockerPullImagesPatienceInterval =
PatienceConfig(scaled(Span(1200, Seconds)), scaled(Span(250, Millis)))
override def beforeAll(): Unit = {
super.beforeAll();
startAllOrFail()
}
override def afterAll(): Unit = {
stopAllQuietly();
super.afterAll()
}
}
// ==================================================================================================
trait DockerHttpBinService extends com.whisk.docker.impl.spotify.DockerKitSpotify {
val httpBinPort = 8080
lazy val httpbinContainer = {
DockerContainer("kennethreitz/httpbin:latest")
.withPortMapping(80 -> DockerPortMapping(Some(httpBinPort), "127.0.0.1"))
.withReadyChecker(DockerReadyChecker.LogLineContains("Booting worker with pid"))
}
override def dockerContainers: List[DockerContainer] = httpbinContainer :: Nil //:: super.dockerContainers
}
// ==================================================================================================
object ThatSpec extends AnyFlatSpec with should.Matchers with DockerTestKit with DockerHttpBinService {
override def suiteName = "ThatSpec"
implicit val serialization = org.json4s.jackson.Serialization
implicit val formats = org.json4s.DefaultFormats
"httpbin" should "be ready" in {
note("CONTAINERS ARE INITIALIZED LAZILY TO BE AVAILABLE FOR THE FIRST TEST TO BE EXECUTED")
note("SO THEY ARE NOT AVAILABLE WHEN THE TEST OBJECT IS INITIALIZED... OR THE TEST CLASS INSTANCIATED ")
val result = quickRequest.get(uri"http://127.0.0.1:$httpBinPort/json").response(asJson[JValue]).send()
val author = result.body.toOption.map(_ \ "slideshow" \ "author").flatMap(_.extractOpt[String])
author.value shouldBe "Yours Truly"
}
}
ThatSpec.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment