robocar-base/vendor/github.com/testcontainers/testcontainers-go
2021-09-01 19:30:49 +02:00
..
wait Upgrade dependencies 2021-01-17 19:00:46 +01:00
.gitignore Upgrade dependencies 2021-01-17 19:00:46 +01:00
.travis.yml Upgrade dependencies 2021-01-17 19:00:46 +01:00
compose.go Upgrade github.com/testcontainers/testcontainers-go 2020-09-06 14:31:23 +02:00
container.go Upgrade dependencies 2021-01-17 19:00:46 +01:00
docker.go Upgrade dependencies 2021-01-17 19:00:46 +01:00
generic.go Upgrade github.com/testcontainers/testcontainers-go 2020-09-06 14:31:23 +02:00
LICENSE Fix dependencies 2020-03-01 17:06:34 +01:00
logconsumer.go Fix dependencies 2020-03-01 17:06:34 +01:00
mkdocs.yml Upgrade github.com/testcontainers/testcontainers-go 2020-09-06 14:31:23 +02:00
network.go Fix dependencies 2020-03-01 17:06:34 +01:00
Pipfile Upgrade github.com/testcontainers/testcontainers-go 2020-09-06 14:31:23 +02:00
Pipfile.lock Upgrade github.com/testcontainers/testcontainers-go 2020-09-06 14:31:23 +02:00
README.md Upgrade github.com/testcontainers/testcontainers-go 2020-09-06 14:31:23 +02:00
reaper.go Upgrade dependencies 2021-01-17 19:00:46 +01:00
requirements.txt Upgrade github.com/testcontainers/testcontainers-go 2020-09-06 14:31:23 +02:00
runtime.txt Upgrade github.com/testcontainers/testcontainers-go 2020-09-06 14:31:23 +02:00
testing.go Upgrade github.com/testcontainers/testcontainers-go 2020-09-06 14:31:23 +02:00

Build Status Go Report Card GoDoc Reference

When I was working on a Zipkin PR I discovered a nice Java library called Testcontainers.

It provides an easy and clean API over the go docker sdk to run, terminate and connect to containers in your tests.

I found myself comfortable programmatically writing the containers I need to run an integration/smoke tests. So I started porting this library in Go.

This is an example:

package main

import (
	"context"
	"fmt"
	"net/http"
	"testing"

	"github.com/testcontainers/testcontainers-go"
	"github.com/testcontainers/testcontainers-go/wait"
)

func TestNginxLatestReturn(t *testing.T) {
	ctx := context.Background()
	req := testcontainers.ContainerRequest{
		Image:        "nginx",
		ExposedPorts: []string{"80/tcp"},
		WaitingFor:   wait.ForHTTP("/"),
	}
	nginxC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
		ContainerRequest: req,
		Started:          true,
	})
	if err != nil {
		t.Error(err)
	}
	defer nginxC.Terminate(ctx)
	ip, err := nginxC.Host(ctx)
	if err != nil {
		t.Error(err)
	}
	port, err := nginxC.MappedPort(ctx, "80")
	if err != nil {
		t.Error(err)
	}
	resp, err := http.Get(fmt.Sprintf("http://%s:%s", ip, port.Port()))
	if resp.StatusCode != http.StatusOK {
		t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode)
	}
}

This is a simple example, you can create one container in my case using the nginx image. You can get its IP ip, err := nginxC.GetContainerIpAddress(ctx) and you can use it to make a GET: resp, err := http.Get(fmt.Sprintf("http://%s", ip))

To clean your environment you can defer the container termination defer nginxC.Terminate(ctx, t). t is *testing.T and it is used to notify is the defer failed marking the test as failed.

Documentation

The documentation lives in ./docs and it is rendered at golang.testcontainers.org.