diff --git a/README.md b/README.md index bdc1e3b114..b00fc282bc 100644 --- a/README.md +++ b/README.md @@ -61,49 +61,3 @@ 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. - -You can build more complex flow using env var to configure the containers. Let's -suppose you are testing an application that requires redis: - -```go -func TestRedisPing(t *testing.T) { - ctx := context.Background() - req := testcontainers.ContainerRequest{ - Image: "redis", - ExposedPorts: []string{"6379/tcp"}, - } - redisC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ - ContainerRequest: req, - Started: true, - }) - if err != nil { - t.Error(err) - } - defer redisC.Terminate(ctx) - ip, err := redisC.Host(ctx) - if err != nil { - t.Error(err) - } - redisPort, err := redisC.MappedPort(ctx, "6479/tcp") - if err != nil { - t.Error(err) - } - - appReq := testcontainers.ContainerRequest{ - ExposedPorts: []string{"8081/tcp"}, - Env: map[string]string{ - "REDIS_HOST": fmt.Sprintf("http://%s:%s", ip, redisPort.Port()), - }, - } - appC, err := testcontainers.RunContainer(ctx, "your/app", testcontainers.GenericContainerRequest{ - ContainerRequest: appReq, - Started: true, - }) - if err != nil { - t.Error(err) - } - defer appC.Terminate(ctx) - - // your assertions -} -``` diff --git a/docker_test.go b/docker_test.go index abffeb818e..8245937f70 100644 --- a/docker_test.go +++ b/docker_test.go @@ -446,61 +446,6 @@ func TestContainerRespondsWithHttp200ForIndex(t *testing.T) { } } -func TestContainerRespondsWithHttp404ForNonExistingPage(t *testing.T) { - t.Skip("Wait needs to be fixed") - ctx := context.Background() - - nginxPort := "80/tcp" - // delayed-nginx will wait 2s before opening port - nginxC, err := GenericContainer(ctx, GenericContainerRequest{ - ContainerRequest: ContainerRequest{ - Image: "nginx", - ExposedPorts: []string{ - nginxPort, - }, - WaitingFor: wait.ForHTTP("/nonExistingPage").WithStatusCodeMatcher(func(status int) bool { - return status == http.StatusNotFound - }), - }, - Started: true, - }) - if err != nil { - t.Fatal(err) - } - rC, err := RunContainer(ctx, "nginx", RequestContainer{ - ExportedPort: []string{ - nginxPort, - }, - WaitingFor: wait.ForHTTP("/nonExistingPage").WithStatusCodeMatcher(func(status int) bool { - return status == http.StatusNotFound - }), - }) - if rC != nil { - t.Fatal(rC) - } - if err != nil { - t.Fatal(err) - } - defer func() { - err := nginxC.Terminate(ctx) - if err != nil { - t.Fatal(err) - } - }() - - origin, err := nginxC.PortEndpoint(ctx, nat.Port(nginxPort), "http") - if err != nil { - t.Fatal(err) - } - resp, err := http.Get(origin + "/nonExistingPage") - if err != nil { - t.Error(err) - } - if resp.StatusCode != http.StatusNotFound { - t.Errorf("Expected status code %d. Got %d.", http.StatusNotFound, resp.StatusCode) - } -} - func TestContainerCreationTimesOutWithHttp(t *testing.T) { t.Skip("Wait needs to be fixed") ctx := context.Background() diff --git a/legacy.go b/legacy.go deleted file mode 100644 index 52690034f5..0000000000 --- a/legacy.go +++ /dev/null @@ -1,83 +0,0 @@ -package testcontainers - -import ( - "context" - - "github.com/docker/go-connections/nat" - "github.com/pkg/errors" - "github.com/testcontainers/testcontainers-go/wait" -) - -var _ DeprecatedContainer = (*DockerContainer)(nil) - -// GetHostEndpoint is deprecated and kept for backwards compat -// Deprecated: Use Endpoint() -func (c *DockerContainer) GetHostEndpoint(ctx context.Context, port string) (string, string, error) { - outerPort, err := c.MappedPort(ctx, nat.Port(port)) - if err != nil { - return "", "", err - } - - ip, err := c.Host(ctx) - if err != nil { - return "", "", err - } - - return ip, outerPort.Port(), nil -} - -// GetIPAddress is deprecated and kept for backwards compat -// Deprecated: Use IPAddress() -func (c *DockerContainer) GetIPAddress(ctx context.Context) (string, error) { - return c.Host(ctx) -} - -// LivenessCheckPorts is deprecated and kept for backwards compat -// Deprecated: Use Ports() -func (c *DockerContainer) LivenessCheckPorts(ctx context.Context) (nat.PortSet, error) { - ports, err := c.Ports(ctx) - var portSet nat.PortSet - for port := range ports { - portSet[port] = struct{}{} - } - return portSet, err -} - -// RequestContainer supplies input parameters for a container -// Deprecated: Use ContainerRequest with provider pattern -type RequestContainer struct { - Env map[string]string - ExportedPort []string - Cmd string - RegistryCred string - WaitingFor wait.Strategy -} - -// RunContainer takes a RequestContainer as input and it runs a container via the docker sdk -// Deprecated: Use GenericContainer() -func RunContainer(ctx context.Context, containerImage string, input RequestContainer) (DeprecatedContainer, error) { - req := ContainerRequest{ - Image: containerImage, - Env: input.Env, - ExposedPorts: input.ExportedPort, - Cmd: input.Cmd, - RegistryCred: input.RegistryCred, - WaitingFor: input.WaitingFor, - } - - container, err := GenericContainer(ctx, GenericContainerRequest{ - ContainerRequest: req, - ProviderType: ProviderDocker, - Started: true, - }) - if err != nil { - return nil, errors.Wrap(err, "failed to launch generic container") - } - - legacyContainer, ok := container.(*DockerContainer) - if !ok { - return nil, errors.New("failed to get docker container from provider") - } - - return legacyContainer, nil -} diff --git a/legacy_test.go b/legacy_test.go deleted file mode 100644 index 2e45107133..0000000000 --- a/legacy_test.go +++ /dev/null @@ -1,244 +0,0 @@ -package testcontainers - -import ( - "context" - "fmt" - "net/http" - "testing" - "time" - - "github.com/docker/go-connections/nat" - "github.com/testcontainers/testcontainers-go/wait" -) - -func TestLegacyTwoContainersExposingTheSamePort(t *testing.T) { - ctx := context.Background() - nginxA, err := RunContainer(ctx, "nginx", RequestContainer{ - ExportedPort: []string{ - "80/tcp", - }, - }) - if err != nil { - t.Fatal(err) - } - defer func() { - err := nginxA.Terminate(ctx) - if err != nil { - t.Fatal(err) - } - }() - - nginxB, err := RunContainer(ctx, "nginx", RequestContainer{ - ExportedPort: []string{ - "80/tcp", - }, - }) - if err != nil { - t.Fatal(err) - } - defer func() { - err := nginxB.Terminate(ctx) - if err != nil { - t.Fatal(err) - } - }() - - ipA, portA, err := nginxA.GetHostEndpoint(ctx, "80/tcp") - if err != nil { - t.Fatal(err) - } - - ipB, portB, err := nginxB.GetHostEndpoint(ctx, "80/tcp") - if err != nil { - t.Fatal(err) - } - - resp, err := http.Get(fmt.Sprintf("http://%s:%s", ipA, portA)) - if err != nil { - t.Fatal(err) - } - if resp.StatusCode != http.StatusOK { - t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode) - } - - resp, err = http.Get(fmt.Sprintf("http://%s:%s", ipB, portB)) - if err != nil { - t.Fatal(err) - } - if resp.StatusCode != http.StatusOK { - t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode) - } -} - -func TestLegacyContainerCreation(t *testing.T) { - ctx := context.Background() - - nginxPort := "80/tcp" - nginxC, err := RunContainer(ctx, "nginx", RequestContainer{ - ExportedPort: []string{ - nginxPort, - }, - }) - if err != nil { - t.Fatal(err) - } - defer func() { - err := nginxC.Terminate(ctx) - if err != nil { - t.Fatal(err) - } - }() - ip, port, err := nginxC.GetHostEndpoint(ctx, nginxPort) - if err != nil { - t.Fatal(err) - } - resp, err := http.Get(fmt.Sprintf("http://%s:%s", ip, port)) - if err != nil { - t.Fatal(err) - } - if resp.StatusCode != http.StatusOK { - t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode) - } -} - -func TestLegacyContainerCreationAndWaitForListeningPortLongEnough(t *testing.T) { - t.Skip("Wait needs to be fixed") - ctx := context.Background() - - nginxPort := "80/tcp" - // delayed-nginx will wait 2s before opening port - nginxC, err := RunContainer(ctx, "menedev/delayed-nginx:1.15.2", RequestContainer{ - ExportedPort: []string{ - nginxPort, - }, - WaitingFor: wait.ForListeningPort(nat.Port(nginxPort)), // default startupTimeout is 60s - }) - if err != nil { - t.Fatal(err) - } - defer func() { - err := nginxC.Terminate(ctx) - if err != nil { - t.Fatal(err) - } - }() - ip, port, err := nginxC.GetHostEndpoint(ctx, nginxPort) - if err != nil { - t.Fatal(err) - } - resp, err := http.Get(fmt.Sprintf("http://%s:%s", ip, port)) - if err != nil { - t.Fatal(err) - } - if resp.StatusCode != http.StatusOK { - t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode) - } -} - -func TestLegacyContainerCreationTimesOut(t *testing.T) { - t.Skip("Wait needs to be fixed") - ctx := context.Background() - // delayed-nginx will wait 2s before opening port - nginxC, err := RunContainer(ctx, "menedev/delayed-nginx:1.15.2", RequestContainer{ - ExportedPort: []string{ - "80/tcp", - }, - WaitingFor: wait.ForListeningPort(nat.Port("80")).WithStartupTimeout(1 * time.Second), - }) - if err == nil { - t.Error("Expected timeout") - err := nginxC.Terminate(ctx) - if err != nil { - t.Fatal(err) - } - } -} - -func TestLegacyContainerRespondsWithHttp200ForIndex(t *testing.T) { - ctx := context.Background() - - nginxPort := "80/tcp" - // delayed-nginx will wait 2s before opening port - nginxC, err := RunContainer(ctx, "nginx", RequestContainer{ - ExportedPort: []string{ - nginxPort, - }, - WaitingFor: wait.ForHTTP("/"), - }) - if err != nil { - t.Fatal(err) - } - defer func() { - err := nginxC.Terminate(ctx) - if err != nil { - t.Fatal(err) - } - }() - - ip, port, err := nginxC.GetHostEndpoint(ctx, nginxPort) - if err != nil { - t.Fatal(err) - } - resp, err := http.Get(fmt.Sprintf("http://%s:%s", ip, port)) - if err != nil { - t.Error(err) - } - if resp.StatusCode != http.StatusOK { - t.Errorf("Expected status code %d. Got %d.", http.StatusOK, resp.StatusCode) - } -} - -func TestLegacyContainerRespondsWithHttp404ForNonExistingPage(t *testing.T) { - ctx := context.Background() - - nginxPort := "80/tcp" - // delayed-nginx will wait 2s before opening port - nginxC, err := RunContainer(ctx, "nginx", RequestContainer{ - ExportedPort: []string{ - nginxPort, - }, - WaitingFor: wait.ForHTTP("/nonExistingPage").WithStatusCodeMatcher(func(status int) bool { - return status == http.StatusNotFound - }), - }) - if err != nil { - t.Fatal(err) - } - defer func() { - err := nginxC.Terminate(ctx) - if err != nil { - t.Fatal(err) - } - }() - - ip, port, err := nginxC.GetHostEndpoint(ctx, nginxPort) - if err != nil { - t.Fatal(err) - } - resp, err := http.Get(fmt.Sprintf("http://%s:%s/nonExistingPage", ip, port)) - if err != nil { - t.Error(err) - } - if resp.StatusCode != http.StatusNotFound { - t.Errorf("Expected status code %d. Got %d.", http.StatusNotFound, resp.StatusCode) - } -} - -func TestLegacyContainerCreationTimesOutWithHttp(t *testing.T) { - ctx := context.Background() - // delayed-nginx will wait 2s before opening port - nginxC, err := RunContainer(ctx, "menedev/delayed-nginx:1.15.2", RequestContainer{ - ExportedPort: []string{ - "80/tcp", - }, - WaitingFor: wait.ForHTTP("/").WithStartupTimeout(1 * time.Second), - }) - - if err == nil { - err := nginxC.Terminate(ctx) - if err != nil { - t.Fatal(err) - } - t.Error("Expected timeout") - } -}