diff --git a/modules/clickhouse/clickhouse_test.go b/modules/clickhouse/clickhouse_test.go index 2b522598df..0a0d302127 100644 --- a/modules/clickhouse/clickhouse_test.go +++ b/modules/clickhouse/clickhouse_test.go @@ -207,14 +207,7 @@ func TestClickHouseWithZookeeper(t *testing.T) { // withZookeeper { zkPort := nat.Port("2181/tcp") - zkcontainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ - ContainerRequest: testcontainers.ContainerRequest{ - ExposedPorts: []string{zkPort.Port()}, - Image: "zookeeper:3.7", - WaitingFor: wait.ForListeningPort(zkPort), - }, - Started: true, - }) + zkcontainer, err := testcontainers.Run(ctx, "zookeeper:3.7", testcontainers.WithExposedPorts(zkPort.Port()), testcontainers.WithWaitStrategy(wait.ForListeningPort(zkPort))) testcontainers.CleanupContainer(t, zkcontainer) require.NoError(t, err) diff --git a/modules/k6/examples_test.go b/modules/k6/examples_test.go index c842814d4c..57c3543e2e 100644 --- a/modules/k6/examples_test.go +++ b/modules/k6/examples_test.go @@ -17,18 +17,7 @@ func ExampleRun() { // create a container with the httpbin application that will be the target // for the test script that runs in the k6 container - gcr := testcontainers.GenericContainerRequest{ - ProviderType: testcontainers.ProviderDocker, - ContainerRequest: testcontainers.ContainerRequest{ - Image: "kennethreitz/httpbin", - ExposedPorts: []string{ - "80", - }, - WaitingFor: wait.ForExposedPort(), - }, - Started: true, - } - httpbin, err := testcontainers.GenericContainer(ctx, gcr) + httpbin, err := testcontainers.Run(ctx, "kennethreitz/httpbin", testcontainers.WithExposedPorts("80/tcp"), testcontainers.WithWaitStrategy(wait.ForExposedPort())) defer func() { if err := testcontainers.TerminateContainer(httpbin); err != nil { log.Printf("failed to terminate container: %s", err) diff --git a/modules/localstack/localstack_test.go b/modules/localstack/localstack_test.go index cf37cd2547..066c7ad395 100644 --- a/modules/localstack/localstack_test.go +++ b/modules/localstack/localstack_test.go @@ -224,35 +224,31 @@ func TestStartV2WithNetwork(t *testing.T) { require.NoError(t, err) require.NotNil(t, localstack) - networkName := nw.Name - - cli, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ - ContainerRequest: testcontainers.ContainerRequest{ - Image: "amazon/aws-cli:2.7.27", - Networks: []string{networkName}, - Entrypoint: []string{"tail"}, - Cmd: []string{"-f", "/dev/null"}, - Env: map[string]string{ - "AWS_ACCESS_KEY_ID": "accesskey", - "AWS_SECRET_ACCESS_KEY": "secretkey", - "AWS_REGION": "eu-west-1", - }, - WaitingFor: wait.ForExec([]string{ - "/usr/local/bin/aws", "sqs", "create-queue", "--queue-name", "baz", "--region", "eu-west-1", - "--endpoint-url", "http://localstack:4566", "--no-verify-ssl", + cli, err := testcontainers.Run( + ctx, "amazon/aws-cli:2.7.27", + network.WithNetwork([]string{"cli"}, nw), + testcontainers.WithEntrypoint("tail"), + testcontainers.WithCmd("-f", "/dev/null"), + testcontainers.WithEnv(map[string]string{ + "AWS_ACCESS_KEY_ID": "accesskey", + "AWS_SECRET_ACCESS_KEY": "secretkey", + "AWS_REGION": "eu-west-1", + }), + testcontainers.WithWaitStrategy(wait.ForExec([]string{ + "/usr/local/bin/aws", "sqs", "create-queue", "--queue-name", "baz", "--region", "eu-west-1", + "--endpoint-url", "http://localstack:4566", "--no-verify-ssl", + }). + WithStartupTimeout(time.Second*10). + WithExitCodeMatcher(func(exitCode int) bool { + return exitCode == 0 }). - WithStartupTimeout(time.Second * 10). - WithExitCodeMatcher(func(exitCode int) bool { - return exitCode == 0 - }). - WithResponseMatcher(func(r io.Reader) bool { - respBytes, _ := io.ReadAll(r) - resp := string(respBytes) - return strings.Contains(resp, "http://localstack:4566") - }), - }, - Started: true, - }) + WithResponseMatcher(func(r io.Reader) bool { + respBytes, _ := io.ReadAll(r) + resp := string(respBytes) + return strings.Contains(resp, "http://localstack:4566") + }), + ), + ) testcontainers.CleanupContainer(t, cli) require.NoError(t, err) require.NotNil(t, cli) diff --git a/modules/redpanda/redpanda_test.go b/modules/redpanda/redpanda_test.go index da90cb7d79..ba231a722d 100644 --- a/modules/redpanda/redpanda_test.go +++ b/modules/redpanda/redpanda_test.go @@ -587,22 +587,12 @@ func TestRedpandaListener_Simple(t *testing.T) { // 3. Start KCat container // withListenerKcat { - kcat, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ - ContainerRequest: testcontainers.ContainerRequest{ - Image: "confluentinc/cp-kcat:7.4.1", - Networks: []string{ - rpNetwork.Name, - }, - Entrypoint: []string{ - "sh", - }, - Cmd: []string{ - "-c", - "tail -f /dev/null", - }, - }, - Started: true, - }) + kcat, err := testcontainers.Run( + ctx, "confluentinc/cp-kcat:7.4.1", + network.WithNetwork([]string{"kcat"}, rpNetwork), + testcontainers.WithEntrypoint("sh"), + testcontainers.WithCmd("-c", "tail -f /dev/null"), + ) // } testcontainers.CleanupContainer(t, kcat) require.NoError(t, err) diff --git a/modules/registry/examples_test.go b/modules/registry/examples_test.go index e73dc045b4..1eed7dc1d9 100644 --- a/modules/registry/examples_test.go +++ b/modules/registry/examples_test.go @@ -73,20 +73,18 @@ func ExampleRun_withAuthentication() { // build a custom redis image from the private registry, // using RegistryName of the container as the registry. - redisC, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{ - ContainerRequest: testcontainers.ContainerRequest{ - FromDockerfile: testcontainers.FromDockerfile{ - Context: filepath.Join("testdata", "redis"), - BuildArgs: map[string]*string{ - "REGISTRY_HOST": ®istryHost, - }, + redisC, err := testcontainers.Run( + context.Background(), "", + testcontainers.WithDockerfile(testcontainers.FromDockerfile{ + Context: filepath.Join("testdata", "redis"), + BuildArgs: map[string]*string{ + "REGISTRY_HOST": ®istryHost, }, - AlwaysPullImage: true, // make sure the authentication takes place - ExposedPorts: []string{"6379/tcp"}, - WaitingFor: wait.ForLog("Ready to accept connections"), - }, - Started: true, - }) + }), + testcontainers.WithAlwaysPull(), // make sure the authentication takes place + testcontainers.WithExposedPorts("6379/tcp"), + testcontainers.WithWaitStrategy(wait.ForLog("Ready to accept connections")), + ) defer func() { if err := testcontainers.TerminateContainer(redisC); err != nil { log.Printf("failed to terminate container: %s", err) @@ -155,22 +153,19 @@ func ExampleRun_pushImage() { repo := registryContainer.RegistryName + "/customredis" tag := "v1.2.3" - redisC, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{ - ContainerRequest: testcontainers.ContainerRequest{ - FromDockerfile: testcontainers.FromDockerfile{ - Context: filepath.Join("testdata", "redis"), - BuildArgs: map[string]*string{ - "REGISTRY_HOST": ®istryHost, - }, - Repo: repo, - Tag: tag, + redisC, err := testcontainers.Run(context.Background(), "", + testcontainers.WithDockerfile(testcontainers.FromDockerfile{ + Context: filepath.Join("testdata", "redis"), + BuildArgs: map[string]*string{ + "REGISTRY_HOST": ®istryHost, }, - AlwaysPullImage: true, // make sure the authentication takes place - ExposedPorts: []string{"6379/tcp"}, - WaitingFor: wait.ForLog("Ready to accept connections"), - }, - Started: true, - }) + Repo: repo, + Tag: tag, + }), + testcontainers.WithAlwaysPull(), // make sure the authentication takes place + testcontainers.WithExposedPorts("6379/tcp"), + testcontainers.WithWaitStrategy(wait.ForLog("Ready to accept connections")), + ) defer func() { if err := testcontainers.TerminateContainer(redisC); err != nil { log.Printf("failed to terminate container: %s", err) @@ -247,14 +242,10 @@ func ExampleRun_pushImage() { return } - newRedisC, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{ - ContainerRequest: testcontainers.ContainerRequest{ - Image: taggedImage, - ExposedPorts: []string{"6379/tcp"}, - WaitingFor: wait.ForLog("Ready to accept connections"), - }, - Started: true, - }) + newRedisC, err := testcontainers.Run(context.Background(), taggedImage, + testcontainers.WithExposedPorts("6379/tcp"), + testcontainers.WithWaitStrategy(wait.ForLog("Ready to accept connections")), + ) defer func() { if err := testcontainers.TerminateContainer(newRedisC); err != nil { log.Printf("failed to terminate container: %s", err) diff --git a/modules/registry/registry_test.go b/modules/registry/registry_test.go index 3315269fb5..81adbd2655 100644 --- a/modules/registry/registry_test.go +++ b/modules/registry/registry_test.go @@ -96,20 +96,18 @@ func TestRunContainer_authenticated(t *testing.T) { t.Run("build images with wrong credentials fails", func(tt *testing.T) { setAuthConfig(tt, registryHost, "foo", "bar") - redisC, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{ - ContainerRequest: testcontainers.ContainerRequest{ - FromDockerfile: testcontainers.FromDockerfile{ - Context: filepath.Join("testdata", "redis"), - BuildArgs: map[string]*string{ - "REGISTRY_HOST": ®istryHost, - }, + redisC, err := testcontainers.Run( + context.Background(), "", + testcontainers.WithDockerfile(testcontainers.FromDockerfile{ + Context: filepath.Join("testdata", "redis"), + BuildArgs: map[string]*string{ + "REGISTRY_HOST": ®istryHost, }, - AlwaysPullImage: true, // make sure the authentication takes place - ExposedPorts: []string{"6379/tcp"}, - WaitingFor: wait.ForLog("Ready to accept connections"), - }, - Started: true, - }) + }), + testcontainers.WithAlwaysPull(), // make sure the authentication takes place + testcontainers.WithExposedPorts("6379/tcp"), + testcontainers.WithWaitStrategy(wait.ForLog("Ready to accept connections")), + ) testcontainers.CleanupContainer(tt, redisC) require.Error(tt, err) require.Contains(tt, err.Error(), "unauthorized: authentication required") @@ -123,20 +121,18 @@ func TestRunContainer_authenticated(t *testing.T) { // The container should start because the authentication // is correct. - redisC, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{ - ContainerRequest: testcontainers.ContainerRequest{ - FromDockerfile: testcontainers.FromDockerfile{ - Context: filepath.Join("testdata", "redis"), - BuildArgs: map[string]*string{ - "REGISTRY_HOST": ®istryHost, - }, + redisC, err := testcontainers.Run( + context.Background(), "", + testcontainers.WithDockerfile(testcontainers.FromDockerfile{ + Context: filepath.Join("testdata", "redis"), + BuildArgs: map[string]*string{ + "REGISTRY_HOST": ®istryHost, }, - AlwaysPullImage: true, // make sure the authentication takes place - ExposedPorts: []string{"6379/tcp"}, - WaitingFor: wait.ForLog("Ready to accept connections"), - }, - Started: true, - }) + }), + testcontainers.WithAlwaysPull(), // make sure the authentication takes place + testcontainers.WithExposedPorts("6379/tcp"), + testcontainers.WithWaitStrategy(wait.ForLog("Ready to accept connections")), + ) testcontainers.CleanupContainer(tt, redisC) require.NoError(tt, err) @@ -256,20 +252,17 @@ func TestRunContainer_wrongData(t *testing.T) { // The container won't be able to start because the data // directory is wrong. - redisC, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{ - ContainerRequest: testcontainers.ContainerRequest{ - FromDockerfile: testcontainers.FromDockerfile{ - Context: filepath.Join("testdata", "redis"), - BuildArgs: map[string]*string{ - "REGISTRY_HOST": ®istryHost, - }, + redisC, err := testcontainers.Run(context.Background(), "", + testcontainers.WithDockerfile(testcontainers.FromDockerfile{ + Context: filepath.Join("testdata", "redis"), + BuildArgs: map[string]*string{ + "REGISTRY_HOST": ®istryHost, }, - AlwaysPullImage: true, // make sure the authentication takes place - ExposedPorts: []string{"6379/tcp"}, - WaitingFor: wait.ForLog("Ready to accept connections"), - }, - Started: true, - }) + }), + testcontainers.WithAlwaysPull(), // make sure the authentication takes place + testcontainers.WithExposedPorts("6379/tcp"), + testcontainers.WithWaitStrategy(wait.ForLog("Ready to accept connections")), + ) testcontainers.CleanupContainer(t, redisC) require.ErrorContains(t, err, "manifest unknown") } diff --git a/modules/socat/examples_test.go b/modules/socat/examples_test.go index 8c2d70be49..0f6fa619b1 100644 --- a/modules/socat/examples_test.go +++ b/modules/socat/examples_test.go @@ -26,17 +26,10 @@ func ExampleRun() { } }() - ctr, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ - ContainerRequest: testcontainers.ContainerRequest{ - Image: "testcontainers/helloworld:1.2.0", - ExposedPorts: []string{"8080/tcp"}, - Networks: []string{nw.Name}, - NetworkAliases: map[string][]string{ - nw.Name: {"helloworld"}, - }, - }, - Started: true, - }) + ctr, err := testcontainers.Run(ctx, "testcontainers/helloworld:1.2.0", + testcontainers.WithExposedPorts("8080/tcp"), + network.WithNetwork([]string{"helloworld"}, nw), + ) if err != nil { log.Printf("failed to create container: %v", err) return @@ -106,17 +99,10 @@ func ExampleRun_multipleTargets() { // } // createHelloWorldContainer { - ctr, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ - ContainerRequest: testcontainers.ContainerRequest{ - Image: "testcontainers/helloworld:1.2.0", - ExposedPorts: []string{"8080/tcp"}, - Networks: []string{nw.Name}, - NetworkAliases: map[string][]string{ - nw.Name: {"helloworld"}, - }, - }, - Started: true, - }) + ctr, err := testcontainers.Run(ctx, "testcontainers/helloworld:1.2.0", + network.WithNetwork([]string{"helloworld"}, nw), + testcontainers.WithExposedPorts("8080/tcp"), + ) if err != nil { log.Printf("failed to create container: %v", err) return diff --git a/modules/socat/socat_test.go b/modules/socat/socat_test.go index 414cc4d7b6..1b279bdb0b 100644 --- a/modules/socat/socat_test.go +++ b/modules/socat/socat_test.go @@ -30,17 +30,10 @@ func TestRun_helloWorld(t *testing.T) { testcontainers.CleanupNetwork(t, nw) require.NoError(t, err) - ctr, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ - ContainerRequest: testcontainers.ContainerRequest{ - Image: "testcontainers/helloworld:1.2.0", - ExposedPorts: []string{"8080/tcp"}, - Networks: []string{nw.Name}, - NetworkAliases: map[string][]string{ - nw.Name: {"helloworld"}, - }, - }, - Started: true, - }) + ctr, err := testcontainers.Run(ctx, "testcontainers/helloworld:1.2.0", + network.WithNetwork([]string{"helloworld"}, nw), + testcontainers.WithExposedPorts("8080/tcp"), + ) testcontainers.CleanupContainer(t, ctr) require.NoError(t, err) @@ -78,17 +71,10 @@ func TestRun_helloWorldDifferentPort(t *testing.T) { testcontainers.CleanupNetwork(t, nw) require.NoError(t, err) - ctr, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ - ContainerRequest: testcontainers.ContainerRequest{ - Image: "testcontainers/helloworld:1.2.0", - ExposedPorts: []string{"8080/tcp"}, - Networks: []string{nw.Name}, - NetworkAliases: map[string][]string{ - nw.Name: {"helloworld"}, - }, - }, - Started: true, - }) + ctr, err := testcontainers.Run(ctx, "testcontainers/helloworld:1.2.0", + network.WithNetwork([]string{"helloworld"}, nw), + testcontainers.WithExposedPorts("8080/tcp"), + ) testcontainers.CleanupContainer(t, ctr) require.NoError(t, err) @@ -132,17 +118,10 @@ func TestRun_helloWorld_WrongImage(t *testing.T) { testcontainers.CleanupNetwork(t, nw) require.NoError(t, err) - ctr, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ - ContainerRequest: testcontainers.ContainerRequest{ - Image: "testcontainers/helloworld:1.2.0", - ExposedPorts: []string{"8080/tcp"}, - Networks: []string{nw.Name}, - NetworkAliases: map[string][]string{ - nw.Name: {"helloworld"}, - }, - }, - Started: true, - }) + ctr, err := testcontainers.Run(ctx, "testcontainers/helloworld:1.2.0", + network.WithNetwork([]string{"helloworld"}, nw), + testcontainers.WithExposedPorts("8080/tcp"), + ) testcontainers.CleanupContainer(t, ctr) require.NoError(t, err) @@ -167,17 +146,10 @@ func TestRun_multipleTargets(t *testing.T) { testcontainers.CleanupNetwork(t, nw) require.NoError(t, err) - ctr, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ - ContainerRequest: testcontainers.ContainerRequest{ - Image: "testcontainers/helloworld:1.2.0", - ExposedPorts: []string{"8080/tcp"}, - Networks: []string{nw.Name}, - NetworkAliases: map[string][]string{ - nw.Name: {"helloworld"}, - }, - }, - Started: true, - }) + ctr, err := testcontainers.Run(ctx, "testcontainers/helloworld:1.2.0", + network.WithNetwork([]string{"helloworld"}, nw), + testcontainers.WithExposedPorts("8080/tcp"), + ) testcontainers.CleanupContainer(t, ctr) require.NoError(t, err)