dockerenv


License

License

Categories

Categories

Docker Container Virtualization Tools
GroupId

GroupId

com.github.aaronp
ArtifactId

ArtifactId

dockerenv_2.12
Last Version

Last Version

0.6.0
Release Date

Release Date

Type

Type

jar
Description

Description

dockerenv
dockerenv
Project URL

Project URL

https://github.com/aaronp/dockerenv
Project Organization

Project Organization

com.github.aaronp
Source Code Management

Source Code Management

https://github.com/aaronp/dockerenv

Download dockerenv_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.aaronp/dockerenv_2.12/ -->
<dependency>
    <groupId>com.github.aaronp</groupId>
    <artifactId>dockerenv_2.12</artifactId>
    <version>0.6.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.aaronp/dockerenv_2.12/
implementation 'com.github.aaronp:dockerenv_2.12:0.6.0'
// https://jarcasting.com/artifacts/com.github.aaronp/dockerenv_2.12/
implementation ("com.github.aaronp:dockerenv_2.12:0.6.0")
'com.github.aaronp:dockerenv_2.12:jar:0.6.0'
<dependency org="com.github.aaronp" name="dockerenv_2.12" rev="0.6.0">
  <artifact name="dockerenv_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.aaronp', module='dockerenv_2.12', version='0.6.0')
)
libraryDependencies += "com.github.aaronp" % "dockerenv_2.12" % "0.6.0"
[com.github.aaronp/dockerenv_2.12 "0.6.0"]

Dependencies

compile (1)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.12

test (5)

Group / Artifact Type Version
org.scalactic : scalactic_2.12 jar 3.2.2
org.scalatest : scalatest_2.12 jar 3.2.2
org.pegdown : pegdown jar 1.6.0
com.vladsch.flexmark : flexmark-all jar 0.35.10
junit : junit jar 4.13

Project Modules

There are no modules declared in this project.

dockerenv

Build Status Maven Central Coverage Status Scaladoc

Dockerenv provides a handle on useful docker containers, typically for running tests against real services as opposed to having to mock.

See docs here

The gist is that we can easily spin up real services (databases, kafka clusters, etc) which we can then connect against.

e.g. Import docker env in your build like this:

libraryDependencies += "com.github.aaronp" %% "dockerenv" % "latest version" % "test" 

Then use it like this:

  println(step("Let's do some ETL! Starting Mysql...", 0))
  dockerenv.mysql().bracket {
    println(step("Mysql Started, Starting Kafka", 1))
    dockerenv.kafka().bracket {
      println(step("Kafka Started, Starting Mongo", 2))
      dockerenv.mongo().bracket {
        println(step("All up! Killing Mongo...", 3))
      }
      println(step("Mongo Down, Killing Kafka", 2))
    }
    println(step("Kafka Down, Killing Mysql", 1))
  }
  println(step("All done!", 0))

  // pretty-prints the message with the current 'docker ps' status
  private def step(msg: String, indent: Int): String = {
    val status      = dockerPS
    ...
  }

  def dockerPS: String = "docker ps".!!

The output for which is:

_____________________________________________________________________________________________________________________________
Let's do some ETL! Starting Mysql...
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

    ________________________________________________________________________________________________________________________________________________________________
    Mysql Started, Starting Kafka
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                  PORTS                               NAMES
    39d16d02b95c        mysql:8.0.18        "docker-entrypoint.s…"   1 second ago        Up Less than a second   33060/tcp, 0.0.0.0:7777->3306/tcp   dockerenv-mysql
    
        __________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
        Kafka Started, Starting Mongo
        CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS                  PORTS                                                                                                                      NAMES
        a6e9bea0393c        dockerenv-kafka:test   "supervisord -n"         1 second ago        Up Less than a second   0.0.0.0:2181->2181/tcp, 0.0.0.0:8083->8083/tcp, 0.0.0.0:9080->9080/tcp, 0.0.0.0:9092->9092/tcp, 0.0.0.0:29092->29092/tcp   test-kafka
        39d16d02b95c        mysql:8.0.18           "docker-entrypoint.s…"   2 seconds ago       Up 1 second             33060/tcp, 0.0.0.0:7777->3306/tcp                                                                                          dockerenv-mysql
        
            __________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
            All up! Killing Mongo...
            CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS                  PORTS                                                                                                                      NAMES
            fef2d8c0662a        mongo:4.0              "docker-entrypoint.s…"   1 second ago        Up Less than a second   0.0.0.0:9010->27017/tcp                                                                                                    dockerenv-mongo
            a6e9bea0393c        dockerenv-kafka:test   "supervisord -n"         2 seconds ago       Up 1 second             0.0.0.0:2181->2181/tcp, 0.0.0.0:8083->8083/tcp, 0.0.0.0:9080->9080/tcp, 0.0.0.0:9092->9092/tcp, 0.0.0.0:29092->29092/tcp   test-kafka
            39d16d02b95c        mysql:8.0.18           "docker-entrypoint.s…"   3 seconds ago       Up 2 seconds            33060/tcp, 0.0.0.0:7777->3306/tcp                                                                                          dockerenv-mysql
            
        ______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
        Mongo Down, Killing Kafka
        CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                                                                                                                      NAMES
        a6e9bea0393c        dockerenv-kafka:test   "supervisord -n"         3 seconds ago       Up 1 second         0.0.0.0:2181->2181/tcp, 0.0.0.0:8083->8083/tcp, 0.0.0.0:9080->9080/tcp, 0.0.0.0:9092->9092/tcp, 0.0.0.0:29092->29092/tcp   test-kafka
        39d16d02b95c        mysql:8.0.18           "docker-entrypoint.s…"   4 seconds ago       Up 3 seconds        33060/tcp, 0.0.0.0:7777->3306/tcp                                                                                          dockerenv-mysql
        
    ____________________________________________________________________________________________________________________________________________________________
    Kafka Down, Killing Postgres
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
    39d16d02b95c        mysql:8.0.18        "docker-entrypoint.s…"   6 seconds ago       Up 4 seconds        33060/tcp, 0.0.0.0:7777->3306/tcp   dockerenv-mysql
    
_____________________________________________________________________________________________________________________________
All done!
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

You can also depend on the test artifact to extend common base test classes:

libraryDependencies += "com.github.aaronp" %% "dockerenv" % "latest version" % "test" classifier "tests"

which gives you:

// the BaseXYZSpec classes ensure XYZ is started/stopped for your tests, and expose a 'dockerHandle' should you need to 
// stop the services for run more complex scenarios, like testing failover/retries  
class MyKafkaTest extends BaseKafkaSpec {

  "Kafka" should {
    "connect to a running kafka instance" in {

      isDockerRunning() shouldBe true
 
      val topic = "testTopic"

      // insert your code here which needs to do something with kafka (e.g. publish/consume some data)
      // here we just execute a script within the kafka container to demonstrate it's running by invoking 
      // 'kafka-topics.sh' script within the container via our listTopics.sh wrapper 
      val Success((0, listOutput)) = dockerHandle.runInScriptDir("listTopics.sh")
      listOutput should not be(empty)
    }
  }
}

Versions

Version
0.6.0
0.5.4
0.5.0
0.4.5
0.4.3
0.4.0
0.3.4
0.3.0
0.0.4
0.0.3
0.0.2
0.0.1