0

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?

Tim
  • 155
  • 1
  • 3
  • 13

1 Answers1

1

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()

Vitaly Chura
  • 704
  • 8
  • 13
  • It was close! i create `@Container var myImage = GenericContainer(DockerImageName.parse("myName")).apply { withCreateContainerCmdModifier { it.withCmd("say --param \"hello world\"") } }` and my test `@Test fun log(){ println("containerId: ${myImage.containerId}") println("logs: ${myImage.logs}") println("containerId: ${myImage.containerId}") }` but console is `containerId: cc5e818... logs: containerId: cc5e818...` – Tim Nov 18 '20 at 20:10
  • log in test is empty! but i see logs in my docker dashbord. – Tim Nov 18 '20 at 20:18
  • it's my fault. Container need time to execute command. i insert Thread.sleep(1000) and i see log!!!! – Tim Nov 18 '20 at 20:31