Skip to content

Commit

Permalink
fix: all mounts should contain the testcontainers labels (#2191)
Browse files Browse the repository at this point in the history
* fix: all mounts should contain the testcontainers labels

* fix: update tests

* chore: add tests for it
  • Loading branch information
mdelapenya authored Feb 2, 2024
1 parent bdab82e commit 69b8fea
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
3 changes: 3 additions & 0 deletions docker_mounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ func mapToDockerMounts(containerMounts ContainerMounts) []mount.Mount {
Source: m.Source.Source(),
ReadOnly: m.ReadOnly,
Target: m.Target.Target(),
VolumeOptions: &mount.VolumeOptions{
Labels: GenericLabels(),
},
}

switch typedMounter := m.Source.(type) {
Expand Down
64 changes: 52 additions & 12 deletions mounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func TestVolumeMount(t *testing.T) {
}

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

t.Parallel()
tests := []struct {
name string
Expand All @@ -57,9 +61,10 @@ func TestContainerMounts_PrepareMounts(t *testing.T) {
mounts: ContainerMounts{{Source: GenericVolumeMountSource{Name: "app-data"}, Target: "/data"}},
want: []mount.Mount{
{
Type: mount.TypeVolume,
Source: "app-data",
Target: "/data",
Type: mount.TypeVolume,
Source: "app-data",
Target: "/data",
VolumeOptions: volumeOptions,
},
},
},
Expand All @@ -68,10 +73,11 @@ func TestContainerMounts_PrepareMounts(t *testing.T) {
mounts: ContainerMounts{{Source: GenericVolumeMountSource{Name: "app-data"}, Target: "/data", ReadOnly: true}},
want: []mount.Mount{
{
Type: mount.TypeVolume,
Source: "app-data",
Target: "/data",
ReadOnly: true,
Type: mount.TypeVolume,
Source: "app-data",
Target: "/data",
ReadOnly: true,
VolumeOptions: volumeOptions,
},
},
},
Expand Down Expand Up @@ -111,8 +117,9 @@ func TestContainerMounts_PrepareMounts(t *testing.T) {
mounts: ContainerMounts{{Source: GenericTmpfsMountSource{}, Target: "/data"}},
want: []mount.Mount{
{
Type: mount.TypeTmpfs,
Target: "/data",
Type: mount.TypeTmpfs,
Target: "/data",
VolumeOptions: volumeOptions,
},
},
},
Expand All @@ -121,9 +128,10 @@ func TestContainerMounts_PrepareMounts(t *testing.T) {
mounts: ContainerMounts{{Source: GenericTmpfsMountSource{}, Target: "/data", ReadOnly: true}},
want: []mount.Mount{
{
Type: mount.TypeTmpfs,
Target: "/data",
ReadOnly: true,
Type: mount.TypeTmpfs,
Target: "/data",
ReadOnly: true,
VolumeOptions: volumeOptions,
},
},
},
Expand All @@ -148,6 +156,7 @@ func TestContainerMounts_PrepareMounts(t *testing.T) {
SizeBytes: 50 * 1024 * 1024,
Mode: 0o644,
},
VolumeOptions: volumeOptions,
},
},
},
Expand Down Expand Up @@ -193,3 +202,34 @@ func TestCreateContainerWithVolume(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "test-volume", volume.Name)
}

func TestMountsReceiveRyukLabels(t *testing.T) {
req := ContainerRequest{
Image: "alpine",
Mounts: ContainerMounts{
{
Source: GenericVolumeMountSource{
Name: "app-data",
},
Target: "/data",
},
},
}

ctx := context.Background()
c, err := GenericContainer(ctx, 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)
require.NoError(t, err)
defer client.Close()

volume, err := client.VolumeInspect(ctx, "app-data")
require.NoError(t, err)
assert.Equal(t, GenericLabels(), volume.Labels)
}

0 comments on commit 69b8fea

Please sign in to comment.