Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refactor bind mount tests for windows #89

Merged
merged 3 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ffs/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ func CreateNestedDir(dirPath string) string {
return fullPath
}

// CreateFilePathInHome creates a filepath and returns the path.
func CreateFilePathInHome(dirPath string) string {
ginglis13 marked this conversation as resolved.
Show resolved Hide resolved
homeDir, err := os.UserHomeDir()
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
fullPath := filepath.Join(homeDir, dirPath)
return fullPath
}

// DeleteDirectory deletes the directory including nested directories.
func DeleteDirectory(directoryPath string) {
err := os.RemoveAll(directoryPath)
Expand Down
62 changes: 47 additions & 15 deletions tests/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -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)
Expand All @@ -434,6 +441,7 @@ func Run(o *RunOption) {
})

ginkgo.It("should set the bind mount as readonly with --mount <src>=/src,<target>=/target,ro", func() {
var err error
file := ffs.CreateTempFile("bar.txt", "foo")
fileDir := filepath.Dir(file)
ginkgo.DeferCleanup(os.RemoveAll, fileDir)
Expand All @@ -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)
ginglis13 marked this conversation as resolved.
Show resolved Hide resolved

// 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"))
})

Expand Down Expand Up @@ -706,3 +722,19 @@ func verifyMountsInfo(actual []MountJSON, want []MountJSON) {
}
}
}

func getWslPath(winPath string) (string, 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
}
Loading