Skip to content

Commit

Permalink
move tests to dockertest_test package (#490)
Browse files Browse the repository at this point in the history
Co-authored-by: Arne Luenser <arne.luenser@ory.sh>
  • Loading branch information
siraj-mx51 and alnr authored Jun 19, 2024
1 parent 05f6347 commit 3328cf9
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions dockertest_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package dockertest
package dockertest_test

import (
"bytes"
Expand All @@ -17,19 +17,20 @@ 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"
)

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)
}
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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"},
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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{
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -456,15 +457,15 @@ 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++ {
t.Run(strconv.Itoa(i), func(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",
},
Expand All @@ -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)
}

0 comments on commit 3328cf9

Please sign in to comment.