Skip to content

Commit

Permalink
Move the file and mounts tests into a test package (#2270)
Browse files Browse the repository at this point in the history
* Move the file and mounts tests into a test package

This work has been extracted from #2202 and is related to #2180. See the original PR for the full context and reasoning.

This will help with the documentation, since all examples will now have the module prefixes.

* Rename the utils file
  • Loading branch information
Minivera committed Mar 1, 2024
1 parent 9d8e386 commit 38550a6
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 50 deletions.
49 changes: 29 additions & 20 deletions docker_files_test.go
@@ -1,4 +1,4 @@
package testcontainers
package testcontainers_test

import (
"context"
Expand All @@ -8,6 +8,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

Expand All @@ -21,10 +22,10 @@ func TestCopyFileToContainer(t *testing.T) {
t.Fatal(err)
}

container, err := GenericContainer(ctx, GenericContainerRequest{
ContainerRequest: ContainerRequest{
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "docker.io/bash",
Files: []ContainerFile{
Files: []testcontainers.ContainerFile{
{
HostFilePath: absPath,
ContainerFilePath: "/hello.sh",
Expand Down Expand Up @@ -57,10 +58,10 @@ func TestCopyFileToRunningContainer(t *testing.T) {
t.Fatal(err)
}

container, err := GenericContainer(ctx, GenericContainerRequest{
ContainerRequest: ContainerRequest{
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "docker.io/bash:5.2.26",
Files: []ContainerFile{
Files: []testcontainers.ContainerFile{
{
HostFilePath: waitForPath,
ContainerFilePath: "/waitForHello.sh",
Expand Down Expand Up @@ -98,10 +99,10 @@ func TestCopyDirectoryToContainer(t *testing.T) {
t.Fatal(err)
}

container, err := GenericContainer(ctx, GenericContainerRequest{
ContainerRequest: ContainerRequest{
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "docker.io/bash",
Files: []ContainerFile{
Files: []testcontainers.ContainerFile{
{
HostFilePath: dataDirectory,
// ContainerFile cannot create the parent directory, so we copy the scripts
Expand Down Expand Up @@ -136,10 +137,10 @@ func TestCopyDirectoryToRunningContainerAsFile(t *testing.T) {
t.Fatal(err)
}

container, err := GenericContainer(ctx, GenericContainerRequest{
ContainerRequest: ContainerRequest{
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "docker.io/bash",
Files: []ContainerFile{
Files: []testcontainers.ContainerFile{
{
HostFilePath: waitForPath,
ContainerFilePath: "/waitForHello.sh",
Expand All @@ -150,11 +151,15 @@ func TestCopyDirectoryToRunningContainerAsFile(t *testing.T) {
},
Started: true,
})
require.NoError(t, err)
if err != nil {
t.Fatal(err)
}

// as the container is started, we can create the directory first
_, _, err = container.Exec(ctx, []string{"mkdir", "-p", "/scripts"})
require.NoError(t, err)
if err != nil {
t.Fatal(err)
}

// because the container path is a directory, it will use the copy dir method as fallback
err = container.CopyFileToContainer(ctx, dataDirectory, "/scripts", 0o700)
Expand Down Expand Up @@ -182,10 +187,10 @@ func TestCopyDirectoryToRunningContainerAsDir(t *testing.T) {
t.Fatal(err)
}

container, err := GenericContainer(ctx, GenericContainerRequest{
ContainerRequest: ContainerRequest{
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "docker.io/bash",
Files: []ContainerFile{
Files: []testcontainers.ContainerFile{
{
HostFilePath: waitForPath,
ContainerFilePath: "/waitForHello.sh",
Expand All @@ -196,11 +201,15 @@ func TestCopyDirectoryToRunningContainerAsDir(t *testing.T) {
},
Started: true,
})
require.NoError(t, err)
if err != nil {
t.Fatal(err)
}

// as the container is started, we can create the directory first
_, _, err = container.Exec(ctx, []string{"mkdir", "-p", "/scripts"})
require.NoError(t, err)
if err != nil {
t.Fatal(err)
}

err = container.CopyDirToContainer(ctx, dataDirectory, "/scripts", 0o700)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions docker_mounts.go
Expand Up @@ -81,6 +81,12 @@ func (s DockerTmpfsMountSource) GetTmpfsOptions() *mount.TmpfsOptions {
return s.TmpfsOptions
}

// PrepareMounts maps the given []ContainerMount to the corresponding
// []mount.Mount for further processing
func (m ContainerMounts) PrepareMounts() []mount.Mount {
return mapToDockerMounts(m)
}

// mapToDockerMounts maps the given []ContainerMount to the corresponding
// []mount.Mount for further processing
func mapToDockerMounts(containerMounts ContainerMounts) []mount.Mount {
Expand Down
2 changes: 2 additions & 0 deletions file_test.go
@@ -1,3 +1,5 @@
// This test is testing very internal logic that should not be exported away from this package. We'll
// leave it in the main testcontainers package. Do not use for user facing examples.
package testcontainers

import (
Expand Down
62 changes: 32 additions & 30 deletions mounts_test.go
@@ -1,4 +1,4 @@
package testcontainers
package testcontainers_test

import (
"context"
Expand All @@ -7,51 +7,53 @@ import (
"github.com/docker/docker/api/types/mount"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/testcontainers/testcontainers-go"
)

func TestVolumeMount(t *testing.T) {
t.Parallel()
type args struct {
volumeName string
mountTarget ContainerMountTarget
mountTarget testcontainers.ContainerMountTarget
}
tests := []struct {
name string
args args
want ContainerMount
want testcontainers.ContainerMount
}{
{
name: "sample-data:/data",
args: args{volumeName: "sample-data", mountTarget: "/data"},
want: ContainerMount{Source: GenericVolumeMountSource{Name: "sample-data"}, Target: "/data"},
want: testcontainers.ContainerMount{Source: testcontainers.GenericVolumeMountSource{Name: "sample-data"}, Target: "/data"},
},
{
name: "web:/var/nginx/html",
args: args{volumeName: "web", mountTarget: "/var/nginx/html"},
want: ContainerMount{Source: GenericVolumeMountSource{Name: "web"}, Target: "/var/nginx/html"},
want: testcontainers.ContainerMount{Source: testcontainers.GenericVolumeMountSource{Name: "web"}, Target: "/var/nginx/html"},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equalf(t, tt.want, VolumeMount(tt.args.volumeName, tt.args.mountTarget), "VolumeMount(%v, %v)", tt.args.volumeName, tt.args.mountTarget)
assert.Equalf(t, tt.want, testcontainers.VolumeMount(tt.args.volumeName, tt.args.mountTarget), "VolumeMount(%v, %v)", tt.args.volumeName, tt.args.mountTarget)
})
}
}

func TestContainerMounts_PrepareMounts(t *testing.T) {
volumeOptions := &mount.VolumeOptions{
Labels: GenericLabels(),
Labels: testcontainers.GenericLabels(),
}

expectedLabels := GenericLabels()
expectedLabels := testcontainers.GenericLabels()
expectedLabels["hello"] = "world"

t.Parallel()
tests := []struct {
name string
mounts ContainerMounts
mounts testcontainers.ContainerMounts
want []mount.Mount
}{
{
Expand All @@ -61,7 +63,7 @@ func TestContainerMounts_PrepareMounts(t *testing.T) {
},
{
name: "Single volume mount",
mounts: ContainerMounts{{Source: GenericVolumeMountSource{Name: "app-data"}, Target: "/data"}},
mounts: testcontainers.ContainerMounts{{Source: testcontainers.GenericVolumeMountSource{Name: "app-data"}, Target: "/data"}},
want: []mount.Mount{
{
Type: mount.TypeVolume,
Expand All @@ -73,7 +75,7 @@ func TestContainerMounts_PrepareMounts(t *testing.T) {
},
{
name: "Single volume mount - read-only",
mounts: ContainerMounts{{Source: GenericVolumeMountSource{Name: "app-data"}, Target: "/data", ReadOnly: true}},
mounts: testcontainers.ContainerMounts{{Source: testcontainers.GenericVolumeMountSource{Name: "app-data"}, Target: "/data", ReadOnly: true}},
want: []mount.Mount{
{
Type: mount.TypeVolume,
Expand All @@ -86,9 +88,9 @@ func TestContainerMounts_PrepareMounts(t *testing.T) {
},
{
name: "Single volume mount - with options",
mounts: ContainerMounts{
mounts: testcontainers.ContainerMounts{
{
Source: DockerVolumeMountSource{
Source: testcontainers.DockerVolumeMountSource{
Name: "app-data",
VolumeOptions: &mount.VolumeOptions{
NoCopy: true,
Expand All @@ -115,7 +117,7 @@ func TestContainerMounts_PrepareMounts(t *testing.T) {

{
name: "Single tmpfs mount",
mounts: ContainerMounts{{Source: GenericTmpfsMountSource{}, Target: "/data"}},
mounts: testcontainers.ContainerMounts{{Source: testcontainers.GenericTmpfsMountSource{}, Target: "/data"}},
want: []mount.Mount{
{
Type: mount.TypeTmpfs,
Expand All @@ -124,8 +126,8 @@ func TestContainerMounts_PrepareMounts(t *testing.T) {
},
},
{
name: "Single tmpfs mount - read-only",
mounts: ContainerMounts{{Source: GenericTmpfsMountSource{}, Target: "/data", ReadOnly: true}},
name: "Single volume mount - read-only",
mounts: testcontainers.ContainerMounts{{Source: testcontainers.GenericTmpfsMountSource{}, Target: "/data", ReadOnly: true}},
want: []mount.Mount{
{
Type: mount.TypeTmpfs,
Expand All @@ -136,9 +138,9 @@ func TestContainerMounts_PrepareMounts(t *testing.T) {
},
{
name: "Single tmpfs mount - with options",
mounts: ContainerMounts{
mounts: testcontainers.ContainerMounts{
{
Source: DockerTmpfsMountSource{
Source: testcontainers.DockerTmpfsMountSource{
TmpfsOptions: &mount.TmpfsOptions{
SizeBytes: 50 * 1024 * 1024,
Mode: 0o644,
Expand All @@ -163,18 +165,18 @@ func TestContainerMounts_PrepareMounts(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equalf(t, tt.want, mapToDockerMounts(tt.mounts), "PrepareMounts()")
assert.Equalf(t, tt.want, tt.mounts.PrepareMounts(), "PrepareMounts()")
})
}
}

func TestCreateContainerWithVolume(t *testing.T) {
// volumeMounts {
req := ContainerRequest{
req := testcontainers.ContainerRequest{
Image: "alpine",
Mounts: ContainerMounts{
Mounts: testcontainers.ContainerMounts{
{
Source: GenericVolumeMountSource{
Source: testcontainers.GenericVolumeMountSource{
Name: "test-volume",
},
Target: "/data",
Expand All @@ -184,15 +186,15 @@ func TestCreateContainerWithVolume(t *testing.T) {
// }

ctx := context.Background()
c, err := GenericContainer(ctx, GenericContainerRequest{
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
require.NoError(t, err)
terminateContainerOnEnd(t, ctx, c)

// Check if volume is created
client, err := NewDockerClientWithOpts(ctx)
client, err := testcontainers.NewDockerClientWithOpts(ctx)
require.NoError(t, err)
defer client.Close()

Expand All @@ -202,11 +204,11 @@ func TestCreateContainerWithVolume(t *testing.T) {
}

func TestMountsReceiveRyukLabels(t *testing.T) {
req := ContainerRequest{
req := testcontainers.ContainerRequest{
Image: "alpine",
Mounts: ContainerMounts{
Mounts: testcontainers.ContainerMounts{
{
Source: GenericVolumeMountSource{
Source: testcontainers.GenericVolumeMountSource{
Name: "app-data",
},
Target: "/data",
Expand All @@ -215,19 +217,19 @@ func TestMountsReceiveRyukLabels(t *testing.T) {
}

ctx := context.Background()
c, err := GenericContainer(ctx, GenericContainerRequest{
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
require.NoError(t, err)
terminateContainerOnEnd(t, ctx, c)

// Check if volume is created with the expected labels
client, err := NewDockerClientWithOpts(ctx)
client, err := testcontainers.NewDockerClientWithOpts(ctx)
require.NoError(t, err)
defer client.Close()

volume, err := client.VolumeInspect(ctx, "app-data")
require.NoError(t, err)
assert.Equal(t, GenericLabels(), volume.Labels)
assert.Equal(t, testcontainers.GenericLabels(), volume.Labels)
}
21 changes: 21 additions & 0 deletions testhelpers_test.go
@@ -0,0 +1,21 @@
package testcontainers_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"

"github.com/testcontainers/testcontainers-go"
)

func terminateContainerOnEnd(tb testing.TB, ctx context.Context, ctr testcontainers.Container) {
tb.Helper()
if ctr == nil {
return
}
tb.Cleanup(func() {
tb.Log("terminating container")
require.NoError(tb, ctr.Terminate(ctx))
})
}

0 comments on commit 38550a6

Please sign in to comment.