Skip to content

Commit 4c1629e

Browse files
test(container): add unit test for volume subpath preservation (#265)
- Add a unit test to verify that `GetCreateHostConfig` preserves volume subpath in `HostConfig.Mounts`, addressing potential issues with subpath handling during container recreation. - Add a `WithMounts` helper in `container_mock_test.go` to support mount configuration in tests. Successful testing confirms that this fork of Watchtower honors Docker volume subpaths, including avoiding the issue described in containrrr#2017
1 parent 7cab283 commit 4c1629e

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

pkg/container/container_mock_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
dockerContainerType "github.com/docker/docker/api/types/container"
77
dockerImageType "github.com/docker/docker/api/types/image"
8+
dockerMountType "github.com/docker/docker/api/types/mount"
89
dockerNetworkType "github.com/docker/docker/api/types/network"
910
dockerNat "github.com/docker/go-connections/nat"
1011
)
@@ -108,3 +109,9 @@ func WithNetworkSettings(
108109
c.NetworkSettings.Networks = networks
109110
}
110111
}
112+
113+
func WithMounts(mounts []dockerMountType.Mount) MockContainerUpdate {
114+
return func(c *dockerContainerType.InspectResponse, _ *dockerImageType.InspectResponse) {
115+
c.HostConfig.Mounts = mounts
116+
}
117+
}

pkg/container/container_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
dockerContainerType "github.com/docker/docker/api/types/container"
1212
dockerImageType "github.com/docker/docker/api/types/image"
13+
dockerMountType "github.com/docker/docker/api/types/mount"
1314
dockerNetworkType "github.com/docker/docker/api/types/network"
1415
dockerNat "github.com/docker/go-connections/nat"
1516

@@ -719,4 +720,36 @@ var _ = ginkgo.Describe("the container", func() {
719720
})
720721
})
721722
})
723+
724+
ginkgo.Describe("GetCreateHostConfig", func() {
725+
ginkgo.When("container has a volume mount with subpath", func() {
726+
ginkgo.It("preserves the subpath in the host config mounts", func() {
727+
volumeMount := dockerMountType.Mount{
728+
Type: dockerMountType.TypeVolume,
729+
Source: "test_volume",
730+
Target: "/config/nest",
731+
VolumeOptions: &dockerMountType.VolumeOptions{
732+
Subpath: "ha/nest",
733+
},
734+
}
735+
736+
container := MockContainer(WithMounts([]dockerMountType.Mount{volumeMount}))
737+
738+
hostConfig := container.GetCreateHostConfig()
739+
740+
gomega.Expect(hostConfig.Mounts).To(gomega.HaveLen(1), "Expected exactly one mount")
741+
mount := hostConfig.Mounts[0]
742+
gomega.Expect(mount.Type).
743+
To(gomega.Equal(dockerMountType.TypeVolume), "Mount type should be volume")
744+
gomega.Expect(mount.Source).
745+
To(gomega.Equal("test_volume"), "Mount source should match")
746+
gomega.Expect(mount.Target).
747+
To(gomega.Equal("/config/nest"), "Mount target should match")
748+
gomega.Expect(mount.VolumeOptions).
749+
ToNot(gomega.BeNil(), "VolumeOptions should be set")
750+
gomega.Expect(mount.VolumeOptions.Subpath).
751+
To(gomega.Equal("ha/nest"), "Subpath should be preserved")
752+
})
753+
})
754+
})
722755
})

0 commit comments

Comments
 (0)