From 3328cf9343b8091879695d1489aa3154544e7e23 Mon Sep 17 00:00:00 2001 From: siraj-mx51 <110873062+siraj-mx51@users.noreply.github.com> Date: Wed, 19 Jun 2024 22:59:55 +1000 Subject: [PATCH] move tests to dockertest_test package (#490) Co-authored-by: Arne Luenser --- dockertest_test.go | 57 +++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/dockertest_test.go b/dockertest_test.go index 29721bfb..9f2f34de 100644 --- a/dockertest_test.go +++ b/dockertest_test.go @@ -1,7 +1,7 @@ // Copyright © 2023 Ory Corp // SPDX-License-Identifier: Apache-2.0 -package dockertest +package dockertest_test import ( "bytes" @@ -17,6 +17,7 @@ import ( _ "github.com/go-sql-driver/mysql" _ "github.com/lib/pq" + "github.com/ory/dockertest/v3" dc "github.com/ory/dockertest/v3/docker" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -24,12 +25,12 @@ import ( var ( docker = os.Getenv("DOCKER_URL") - pool *Pool + pool *dockertest.Pool ) func TestMain(m *testing.M) { var err error - pool, err = NewPool(docker) + pool, err = dockertest.NewPool(docker) if err != nil { log.Fatalf("Could not construct pool: %s", err) } @@ -59,7 +60,7 @@ func TestPostgres(t *testing.T) { } func TestMongo(t *testing.T) { - options := &RunOptions{ + options := &dockertest.RunOptions{ Repository: "mongo", Tag: "3.3.12", Cmd: []string{"mongod", "--smallfiles", "--port", "3000"}, @@ -89,7 +90,7 @@ func TestMongo(t *testing.T) { } func TestMysqlWithPlatform(t *testing.T) { - resource, err := pool.RunWithOptions(&RunOptions{ + resource, err := pool.RunWithOptions(&dockertest.RunOptions{ Repository: "mysql", Tag: "8.0", Env: []string{"MYSQL_ROOT_PASSWORD=secret"}, @@ -113,7 +114,7 @@ func TestMysqlWithPlatform(t *testing.T) { func TestContainerWithName(t *testing.T) { resource, err := pool.RunWithOptions( - &RunOptions{ + &dockertest.RunOptions{ Name: "db", Repository: "postgres", Tag: "9.5", @@ -129,7 +130,7 @@ func TestContainerWithLabels(t *testing.T) { "my": "label", } resource, err := pool.RunWithOptions( - &RunOptions{ + &dockertest.RunOptions{ Name: "db", Repository: "postgres", Tag: "9.5", @@ -145,7 +146,7 @@ func TestContainerWithLabels(t *testing.T) { func TestContainerWithUser(t *testing.T) { user := "1001:1001" resource, err := pool.RunWithOptions( - &RunOptions{ + &dockertest.RunOptions{ Name: "db", Repository: "postgres", Tag: "9.5", @@ -164,7 +165,7 @@ func TestContainerWithUser(t *testing.T) { func TestContainerWithTty(t *testing.T) { resource, err := pool.RunWithOptions( - &RunOptions{ + &dockertest.RunOptions{ Name: "db", Repository: "postgres", Tag: "9.5", @@ -183,7 +184,7 @@ func TestContainerWithTty(t *testing.T) { func TestContainerWithPortBinding(t *testing.T) { resource, err := pool.RunWithOptions( - &RunOptions{ + &dockertest.RunOptions{ Repository: "postgres", Tag: "9.5", PortBindings: map[dc.Port][]dc.PortBinding{ @@ -229,14 +230,14 @@ CMD sleep 10 )) resource, err := pool.BuildAndRunWithBuildOptions( - &BuildOptions{ + &dockertest.BuildOptions{ ContextDir: dir, Dockerfile: "Dockerfile", BuildArgs: []dc.BuildArg{ {Name: "foo", Value: "bar"}, }, }, - &RunOptions{ + &dockertest.RunOptions{ Name: "buildarg-test", }, func(hc *dc.HostConfig) { hc.AutoRemove = true @@ -246,7 +247,7 @@ CMD sleep 10 var stdout bytes.Buffer exitCode, err := resource.Exec( []string{"cat", "/build-time-value"}, - ExecOptions{StdOut: &stdout}, + dockertest.ExecOptions{StdOut: &stdout}, ) require.NoError(t, err) require.Zero(t, exitCode) @@ -285,7 +286,7 @@ func TestExpire(t *testing.T) { func TestContainerWithShMzSize(t *testing.T) { shmemsize := int64(1024 * 1024) resource, err := pool.RunWithOptions( - &RunOptions{ + &dockertest.RunOptions{ Name: "db", Repository: "postgres", Tag: "9.5", @@ -300,7 +301,7 @@ func TestContainerWithShMzSize(t *testing.T) { func TestContainerByName(t *testing.T) { got, err := pool.RunWithOptions( - &RunOptions{ + &dockertest.RunOptions{ Name: "db", Repository: "postgres", Tag: "9.5", @@ -318,7 +319,7 @@ func TestContainerByName(t *testing.T) { func TestRemoveContainerByName(t *testing.T) { _, err := pool.RunWithOptions( - &RunOptions{ + &dockertest.RunOptions{ Name: "db", Repository: "postgres", Tag: "9.5", @@ -330,7 +331,7 @@ func TestRemoveContainerByName(t *testing.T) { require.NoError(t, err) resource, err := pool.RunWithOptions( - &RunOptions{ + &dockertest.RunOptions{ Name: "db", Repository: "postgres", Tag: "9.5", @@ -360,7 +361,7 @@ func TestExec(t *testing.T) { var stdout bytes.Buffer exitCode, err := resource.Exec( []string{"psql", "-qtAX", "-U", "postgres", "-c", "SHOW server_version"}, - ExecOptions{StdOut: &stdout}, + dockertest.ExecOptions{StdOut: &stdout}, ) require.NoError(t, err) require.Zero(t, exitCode) @@ -373,19 +374,19 @@ func TestNetworking_on_start(t *testing.T) { require.NoError(t, err) defer network.Close() - resourceFirst, err := pool.RunWithOptions(&RunOptions{ + resourceFirst, err := pool.RunWithOptions(&dockertest.RunOptions{ Repository: "postgres", Tag: "9.5", - Networks: []*Network{network}, + Networks: []*dockertest.Network{network}, Env: []string{"POSTGRES_PASSWORD=secret"}, }) require.NoError(t, err) defer resourceFirst.Close() - resourceSecond, err := pool.RunWithOptions(&RunOptions{ + resourceSecond, err := pool.RunWithOptions(&dockertest.RunOptions{ Repository: "postgres", Tag: "11", - Networks: []*Network{network}, + Networks: []*dockertest.Network{network}, Env: []string{"POSTGRES_PASSWORD=secret"}, }) require.NoError(t, err) @@ -446,7 +447,7 @@ func TestNetworking_after_start(t *testing.T) { var stdout bytes.Buffer exitCode, err := resourceFirst.Exec( []string{"psql", "-qtAX", "-h", resourceSecond.GetIPInNetwork(network), "-U", "postgres", "-c", "SHOW server_version"}, - ExecOptions{StdOut: &stdout, Env: []string{"PGPASSWORD=secret"}}, + dockertest.ExecOptions{StdOut: &stdout, Env: []string{"PGPASSWORD=secret"}}, ) require.NoError(t, err) require.Zero(t, exitCode) @@ -456,7 +457,7 @@ func TestNetworking_after_start(t *testing.T) { func TestClientRaceCondition(t *testing.T) { // Shadow pool so that we can have a fresh client with nil pool.Client.serverAPIVersion - pool, err := NewPool(docker) + pool, err := dockertest.NewPool(docker) require.NoError(t, err) for i := 0; i < 10; i++ { @@ -464,7 +465,7 @@ func TestClientRaceCondition(t *testing.T) { // Tests must be run in parallel to recreate the issue t.Parallel() resource, _ := pool.RunWithOptions( - &RunOptions{ + &dockertest.RunOptions{ Repository: "postgres", Tag: "13.4", }, @@ -475,17 +476,17 @@ func TestClientRaceCondition(t *testing.T) { } func TestExecStatus(t *testing.T) { - resource, err := pool.RunWithOptions(&RunOptions{ + resource, err := pool.RunWithOptions(&dockertest.RunOptions{ Repository: "alpine", Tag: "3.16", Cmd: []string{"tail", "-f", "/dev/null"}, }) require.NoError(t, err) defer resource.Close() - exitCode, err := resource.Exec([]string{"/bin/false"}, ExecOptions{}) + exitCode, err := resource.Exec([]string{"/bin/false"}, dockertest.ExecOptions{}) require.NoError(t, err) require.Equal(t, 1, exitCode) - exitCode, err = resource.Exec([]string{"/bin/sh", "-c", "/bin/sleep 2 && exit 42"}, ExecOptions{}) + exitCode, err = resource.Exec([]string{"/bin/sh", "-c", "/bin/sleep 2 && exit 42"}, dockertest.ExecOptions{}) require.NoError(t, err) require.Equal(t, 42, exitCode) }