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

feat: Tests for bind mounts #66

Merged
merged 4 commits into from
May 26, 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
15 changes: 15 additions & 0 deletions fenv/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// Package fenv Package path implements utility routines for working with environment variables
package fenv

import (
"os"
)

// GetEnv retrieves the value of an environment variable.
// It returns an empty string if the variable is not set.
func GetEnv(key string) string {
return os.Getenv(key)
}
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())
}
76 changes: 76 additions & 0 deletions tests/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/runfinch/common-tests/fenv"

"github.com/runfinch/common-tests/command"
"github.com/runfinch/common-tests/ffs"
Expand Down Expand Up @@ -446,6 +447,81 @@ func Run(o *RunOption) {
verifyMountsInfo(actualMount, expectedMount)
})

// TODO: Remove FINCH_DOCKER_COMPAT=1 check when FINCH_DOCKER_COMPAT flag is removed in finch
ginkgo.It("should create nested bind mounts within a container when FINCH_DOCKER_COMPAT is not set", func() {
const (
outerDir = "/outer"
nestedDir = "/outer/nested"
)

if fenv.GetEnv("FINCH_DOCKER_COMPAT") == "1" {
ginkgo.Skip("Skipping test: FINCH_DOCKER_COMPAT is set to 1")
}
// 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
// Upstream issue: /~https://github.com/containerd/nerdctl/issues/2254
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"))
})

// TODO: Remove FINCH_DOCKER_COMPAT=1 check when FINCH_DOCKER_COMPAT flag is removed in finch
// /~https://github.com/runfinch/finch/pull/417/files

ginkgo.It("should create nested bind mounts within a container when FINCH_DOCKER_COMPAT is set", func() {
const (
outerDir = "/outer"
nestedDir = "/outer/nested"
)
if fenv.GetEnv("FINCH_DOCKER_COMPAT") != "1" {
ginkgo.Skip("Skipping test: FINCH_DOCKER_COMPAT is not set to 1")
}
// 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
output := command.StdoutStr(o.BaseOpt, "run", "--rm", "--name", testContainerName,
"-v", nestedDirectory+":"+nestedDirectory,
"-v", tempDir+":"+hostDirectory,
defaultImage, "sh", "-c", "ls "+nestedDirectory)
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)
gomega.Expect(output).Should(gomega.ContainSubstring("file1.txt"))
})

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