Tim
Tim

Reputation: 335

Testing docker image. Easy start with exec and get log

I create small app for my CI. This app very easy, it print to console property, which i set as option, and exit. I can execute it in my cmd(i use windows), like this:

docker run --rm myImage:latest say --param "hello world"

and i see in cmd "hello world". Now i wand write test on my image. I use Kotlin as main languge, Junit5 as test framework and com.github.dockerjava.api.DockerClient as Docker client. I want write simple test like this:

@Testcontainers
class Test{

  val dockerImageId = System.getProperty("dockerImageId ")
  var dockerClient = DockerClientFactory.instance().client()

  @Test
    fun init() {
        val log = dockerClient.stratContainerFromImageId(dockerImageId).withExec("say --param \"hello world\"").getLog()
        assertEqals("hello world", log)
    }
}

After bad night with DockerClient Documentation, i can start image like this:

dockerClient.startContainerCmd(dockerClient.createContainerCmd(dockerId).exec().id).exec()

But wen i open LogContainerCmd and start learning ResultCallback i wanna die!

I'm looking for EASY way, for my simple test. How now it?

Upvotes: 0

Views: 616

Answers (1)

Vitaly Chura
Vitaly Chura

Reputation: 870

Why don't you create a container as a test class field as suggested in docs? https://www.testcontainers.org/quickstart/junit_5_quickstart/ starting from section 2

Then you can run a command, and either do execResult.getStdout() or container.getLogs()

Upvotes: 1

Related Questions