Skip to content

Commit

Permalink
fix: apply volume options only to volumes (#2201)
Browse files Browse the repository at this point in the history
* fix: apply volume options only to volumes

* chore: rename test case

* fix: update tests
  • Loading branch information
mdelapenya committed Feb 6, 2024
1 parent f012d34 commit 54b1d8a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
14 changes: 11 additions & 3 deletions docker_mounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}

Expand Down
24 changes: 11 additions & 13 deletions mounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func TestContainerMounts_PrepareMounts(t *testing.T) {
Labels: GenericLabels(),
}

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

t.Parallel()
tests := []struct {
name string
Expand Down Expand Up @@ -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,
},
},
},
Expand All @@ -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{
Expand All @@ -156,7 +155,6 @@ func TestContainerMounts_PrepareMounts(t *testing.T) {
SizeBytes: 50 * 1024 * 1024,
Mode: 0o644,
},
VolumeOptions: volumeOptions,
},
},
},
Expand Down

0 comments on commit 54b1d8a

Please sign in to comment.