From 51f85600c688b92b8453c10e9d16716e6cdef368 Mon Sep 17 00:00:00 2001 From: Vishwas Siravara Date: Mon, 25 Sep 2023 09:20:39 -0700 Subject: [PATCH 1/3] refactor bind mount tests for windows Signed-off-by: Vishwas Siravara --- ffs/create.go | 10 ++++++++ tests/run.go | 64 +++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/ffs/create.go b/ffs/create.go index 9347243..9772581 100644 --- a/ffs/create.go +++ b/ffs/create.go @@ -72,6 +72,16 @@ func CreateNestedDir(dirPath string) string { return fullPath } +// CreateFilePathInHome creates a filepath and returns the path. +func CreateFilePathInHome(dirPath string) string { + homeDir, err := os.UserHomeDir() + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + fullPath := filepath.Join(homeDir, dirPath) + err = os.MkdirAll(fullPath, 0o740) + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + return fullPath +} + // DeleteDirectory deletes the directory including nested directories. func DeleteDirectory(directoryPath string) { err := os.RemoveAll(directoryPath) diff --git a/tests/run.go b/tests/run.go index 8d74a3d..9de1769 100644 --- a/tests/run.go +++ b/tests/run.go @@ -10,6 +10,7 @@ import ( "net/http" "os" "path/filepath" + "runtime" "strings" "time" @@ -420,12 +421,18 @@ func Run(o *RunOption) { }) ginkgo.It("should create a bind mount in a container", func() { + var err error file := ffs.CreateTempFile("bar.txt", "foo") fileDir := filepath.Dir(file) ginkgo.DeferCleanup(os.RemoveAll, fileDir) command.Run(o.BaseOpt, "run", "-d", "--name", testContainerName, "--mount", fmt.Sprintf("type=bind,source=%s,target=%s", fileDir, destDir), defaultImage, "sleep", "infinity") + + if runtime.GOOS == "windows" { + fileDir, err = getWslPath(fileDir) + gomega.Expect(err).Should(gomega.BeNil()) + } expectedMount := []MountJSON{makeMount(bindType, fileDir, destDir, "", true)} actualMount := getContainerMounts(o.BaseOpt, testContainerName) verifyMountsInfo(actualMount, expectedMount) @@ -434,6 +441,7 @@ func Run(o *RunOption) { }) ginkgo.It("should set the bind mount as readonly with --mount =/src,=/target,ro", func() { + var err error file := ffs.CreateTempFile("bar.txt", "foo") fileDir := filepath.Dir(file) ginkgo.DeferCleanup(os.RemoveAll, fileDir) @@ -442,41 +450,49 @@ func Run(o *RunOption) { command.New(o.BaseOpt, "run", "-i", "--name", testContainerName, "--mount", fmt.Sprintf("type=bind,source=%s,target=%s,ro", fileDir, destDir), defaultImage).WithStdin(gbytes.BufferWithBytes(cmd)).WithoutSuccessfulExit().Run() + + if runtime.GOOS == "windows" { + fileDir, err = getWslPath(fileDir) + gomega.Expect(err).Should(gomega.BeNil()) + } expectedMount := []MountJSON{makeMount(bindType, fileDir, destDir, "ro", false)} actualMount := getContainerMounts(o.BaseOpt, testContainerName) verifyMountsInfo(actualMount, expectedMount) }) ginkgo.It("should create nested bind mounts within a container", func() { - const ( - outerDir = "/outer" - nestedDir = "/outer/nested" - ) - + var err error // Create the nested directory on the host - hostDirectory := ffs.CreateNestedDir(outerDir) - nestedDirectory := ffs.CreateNestedDir(nestedDir) - defer ffs.DeleteDirectory(hostDirectory) + containerOuterDir := ffs.CreateFilePathInHome("outer") + nestedHostDir := ffs.CreateNestedDir(filepath.Join("outer", "nested")) + nestedContainerDir := nestedHostDir + if runtime.GOOS == "windows" { + nestedContainerDir, err = getWslPath(nestedHostDir) + gomega.Expect(err).Should(gomega.BeNil()) + containerOuterDir, err = getWslPath(containerOuterDir) + gomega.Expect(err).Should(gomega.BeNil()) + } + defer ffs.DeleteDirectory(nestedHostDir) // Directory on host to be mounted at hostDirectory in container tempDir := ffs.CreateTempDir("some_dir") defer ffs.DeleteDirectory(tempDir) // Write a file to the nested directory - nestedFilePath := filepath.Join(nestedDirectory, "file1.txt") + nestedFilePath := filepath.Join(nestedHostDir, "file1.txt") ffs.WriteFile(nestedFilePath, "test") // Mount nested directory first followed by parent directory output := command.StdoutStr(o.BaseOpt, "run", "--rm", "--name", testContainerName, - "-v", nestedDirectory+":"+nestedDirectory, - "-v", tempDir+":"+hostDirectory, - defaultImage, "sh", "-c", "ls "+nestedDirectory) + "-v", nestedHostDir+":"+nestedContainerDir, + "-v", tempDir+":"+containerOuterDir, + defaultImage, "sh", "-c", "ls "+nestedContainerDir) gomega.Expect(output).Should(gomega.ContainSubstring("file1.txt")) // Mount parent directory first followed by nested output = command.StdoutStr(o.BaseOpt, "run", "--rm", "--name", testContainerName2, - "-v", tempDir+":"+hostDirectory, - "-v", nestedDirectory+":"+nestedDirectory, - defaultImage, "sh", "-c", "ls "+nestedDirectory) + "-v", tempDir+":"+containerOuterDir, + "-v", nestedHostDir+":"+nestedContainerDir, + defaultImage, "sh", "-c", "ls "+nestedContainerDir) gomega.Expect(output).Should(gomega.ContainSubstring("file1.txt")) }) @@ -706,3 +722,21 @@ func verifyMountsInfo(actual []MountJSON, want []MountJSON) { } } } + +func getWslPath(winPath string) (string, error) { + var err error + + path, err := filepath.Abs(filepath.Clean(winPath)) + if err != nil { + return "", err + } + if len(path) >= 2 && path[1] == ':' { + drive := strings.ToLower(string(path[0])) + remainingPath := "" + if len(path) > 3 { + remainingPath = path[3:] + } + return filepath.ToSlash(filepath.Join(string(filepath.Separator), "mnt", drive, remainingPath)), nil + } + return path, nil +} From 51dc110bc4bdb6c0326219625630f88632d3a983 Mon Sep 17 00:00:00 2001 From: Vishwas Siravara Date: Mon, 25 Sep 2023 10:24:25 -0700 Subject: [PATCH 2/3] leftover clean up Signed-off-by: Vishwas Siravara --- tests/run.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/run.go b/tests/run.go index 9de1769..76a20f6 100644 --- a/tests/run.go +++ b/tests/run.go @@ -724,8 +724,6 @@ func verifyMountsInfo(actual []MountJSON, want []MountJSON) { } func getWslPath(winPath string) (string, error) { - var err error - path, err := filepath.Abs(filepath.Clean(winPath)) if err != nil { return "", err From 2b72636b5aa6df64f56cb8aef48b8c860e599a7a Mon Sep 17 00:00:00 2001 From: Vishwas Siravara Date: Mon, 25 Sep 2023 14:46:06 -0700 Subject: [PATCH 3/3] fix CreateFilePathInHome helper Signed-off-by: Vishwas Siravara --- ffs/create.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/ffs/create.go b/ffs/create.go index 9772581..2e06a94 100644 --- a/ffs/create.go +++ b/ffs/create.go @@ -77,8 +77,6 @@ func CreateFilePathInHome(dirPath string) string { homeDir, err := os.UserHomeDir() gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) fullPath := filepath.Join(homeDir, dirPath) - err = os.MkdirAll(fullPath, 0o740) - gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) return fullPath }