© 2018 InfluxData. All rights reserved.
33 @gianarb -
[email protected]
TestContainers
I maintain the Golang
version of the library
testcontainers/testcontainer
s-go
package main
import (
"context"
"fmt"
"net/http"
"testing"
testcontainers "github.com/testcontainers/testcontainers-go"
)
func TestNginxLatestReturn(t *testing.T) {
ctx := context.Background()
req := testcontainers.ContainerRequest{
Image: "nginx",
ExposedPorts: []string{"80/tcp"},
}
nginxC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
defer nginxC.Terminate(ctx)
ip, err := nginxC.Host(ctx)
port, err := nginxC.MappedPort(ctx, "80")
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)
}
}