Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Integration testing with TestСontainers-Go

Integration testing with TestСontainers-Go

GopherCon Europe 2020 - Lightning talk

Nikolay Kuznetsov

June 18, 2020
Tweet

More Decks by Nikolay Kuznetsov

Other Decks in Programming

Transcript

  1. • Host port conflicts • Not ready сontainer / service

    • Resource leak (the container keeps running) • Stale data (if reusing the same container) What could go wrong?
  2. TestContainers-Go github.com/testcontainers/testcontainers-go • Docker Go client under the hood •

    Host port randomization • Containers clean up at the test shutdown • Readiness waiting strategies
  3. Host port randomization • API to get a host port:

    • Prevents port conflicts • Enables parallel builds port, err := pgContainer.MappedPort(ctx, "5432/tcp")
  4. Containers cleanup: Ryuk github.com/testcontainers/moby-ryuk • Ryuk kills containers (networks, volumes)

    by labels • TC assigns labels to started containers • TC keeps a connection to Ryuk open • Ryuk acts when the connection is terminated
  5. Host port waiting strategy • Impl checks both from outside

    and inside container • Default (customizable) timeout is 60 seconds tc.ContainerRequest{ Image: "postgres:12.1", ExposedPorts: []string{"5432/tcp"}, WaitingFor: wait.ForListeningPort("5432/tcp"), },
  6. Takeaways • testcontainers.org • Balance between flexibility, speed and features

    • Great for integration tests • Possible to use for end-to-end tests