Skip to content

Commit

Permalink
Tests for bind mounts
Browse files Browse the repository at this point in the history
Signed-off-by: Vishwas Siravara <[email protected]>
  • Loading branch information
vsiravar committed May 26, 2023
1 parent 590a984 commit 2decb9f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ffs/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,20 @@ func CreateTempDir(directoryPrefix string) string {
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
return tempDir
}

// CreateNestedDir creates a nested directory and returns the path of the created directory.
// It is the caller's responsibility to remove the directory when it is no longer needed.
func CreateNestedDir(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)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
}
34 changes: 34 additions & 0 deletions tests/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,40 @@ func Run(o *RunOption) {
verifyMountsInfo(actualMount, expectedMount)
})

ginkgo.It("should create nested bind mounts within a container", func() {
const (
outerDir = "/outer"
nestedDir = "/outer/nested"
)

// Create the nested directory on the host
hostDirectory := ffs.CreateNestedDir(outerDir)
nestedDirectory := ffs.CreateNestedDir(nestedDir)
defer ffs.DeleteDirectory(hostDirectory)

// 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")
ffs.WriteFile(nestedFilePath, "test")

// Mount nested directory first followed by parent directory
command.RunWithoutSuccessfulExit(o.BaseOpt, "run", "--rm", "--name", testContainerName,
"-v", nestedDirectory+":"+nestedDirectory,
"-v", tempDir+":"+hostDirectory,
defaultImage, "sh", "-c", "ls "+nestedDirectory)

// 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)
gomega.Expect(output).Should(gomega.ContainSubstring("file1.txt"))

// Test with env variable
})

ginkgo.It("should create a tmpfs mount using --mount type=tmpfs flag", func() {
tmpfsDir := "/tmpfsDir"
command.Run(o.BaseOpt, "run", "-d", "--name", testContainerName, "--mount",
Expand Down

0 comments on commit 2decb9f

Please sign in to comment.