From 54b1d8aa22fd0169939ecc7957db2313fc1f13db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 6 Feb 2024 17:35:28 +0100 Subject: [PATCH] fix: apply volume options only to volumes (#2201) * fix: apply volume options only to volumes * chore: rename test case * fix: update tests --- docker_mounts.go | 14 +++++++++++--- mounts_test.go | 24 +++++++++++------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/docker_mounts.go b/docker_mounts.go index 2efdb0c7e2..4906a90692 100644 --- a/docker_mounts.go +++ b/docker_mounts.go @@ -101,9 +101,6 @@ 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) { @@ -117,6 +114,17 @@ func mapToDockerMounts(containerMounts ContainerMounts) []mount.Mount { // The provided source type has no custom options } + if mountType == mount.TypeVolume { + if containerMount.VolumeOptions == nil { + containerMount.VolumeOptions = &mount.VolumeOptions{ + Labels: make(map[string]string), + } + } + for k, v := range GenericLabels() { + containerMount.VolumeOptions.Labels[k] = v + } + } + mounts = append(mounts, containerMount) } diff --git a/mounts_test.go b/mounts_test.go index a89e6355c0..6ec994c635 100644 --- a/mounts_test.go +++ b/mounts_test.go @@ -45,6 +45,9 @@ func TestContainerMounts_PrepareMounts(t *testing.T) { Labels: GenericLabels(), } + expectedLabels := GenericLabels() + expectedLabels["hello"] = "world" + t.Parallel() tests := []struct { name string @@ -104,9 +107,7 @@ func TestContainerMounts_PrepareMounts(t *testing.T) { Target: "/data", VolumeOptions: &mount.VolumeOptions{ NoCopy: true, - Labels: map[string]string{ - "hello": "world", - }, + Labels: expectedLabels, }, }, }, @@ -117,26 +118,24 @@ func TestContainerMounts_PrepareMounts(t *testing.T) { mounts: ContainerMounts{{Source: GenericTmpfsMountSource{}, Target: "/data"}}, want: []mount.Mount{ { - Type: mount.TypeTmpfs, - Target: "/data", - VolumeOptions: volumeOptions, + Type: mount.TypeTmpfs, + Target: "/data", }, }, }, { - name: "Single volume mount - read-only", + name: "Single tmpfs mount - read-only", mounts: ContainerMounts{{Source: GenericTmpfsMountSource{}, Target: "/data", ReadOnly: true}}, want: []mount.Mount{ { - Type: mount.TypeTmpfs, - Target: "/data", - ReadOnly: true, - VolumeOptions: volumeOptions, + Type: mount.TypeTmpfs, + Target: "/data", + ReadOnly: true, }, }, }, { - name: "Single volume mount - with options", + name: "Single tmpfs mount - with options", mounts: ContainerMounts{ { Source: DockerTmpfsMountSource{ @@ -156,7 +155,6 @@ func TestContainerMounts_PrepareMounts(t *testing.T) { SizeBytes: 50 * 1024 * 1024, Mode: 0o644, }, - VolumeOptions: volumeOptions, }, }, },