From faeb45d69f1fc170b39ab4e1b1b4c365abbfdc79 Mon Sep 17 00:00:00 2001 From: He Simei Date: Tue, 5 May 2015 10:30:59 +0800 Subject: [PATCH] fix host dir, to test volume r/w with different containers within a pod Signed-off-by: He Simei --- hack/verify-flags/excluded-flags.txt | 2 + hack/verify-flags/known-flags.txt | 2 + test/e2e/empty_dir.go | 9 ++-- test/e2e/host_path.go | 34 +++++++++---- test/images/mount-tester-user/Dockerfile | 2 +- test/images/mount-tester-user/Makefile | 2 +- test/images/mount-tester/Makefile | 2 +- test/images/mount-tester/mt.go | 63 +++++++++++++++++++++--- 8 files changed, 91 insertions(+), 25 deletions(-) diff --git a/hack/verify-flags/excluded-flags.txt b/hack/verify-flags/excluded-flags.txt index 10f6857783..260c496bbb 100644 --- a/hack/verify-flags/excluded-flags.txt +++ b/hack/verify-flags/excluded-flags.txt @@ -20,3 +20,5 @@ test_args up_to up_to valid_flag +retry_time +file_content_in_loop diff --git a/hack/verify-flags/known-flags.txt b/hack/verify-flags/known-flags.txt index ac111cd4bb..97d1ab85ec 100644 --- a/hack/verify-flags/known-flags.txt +++ b/hack/verify-flags/known-flags.txt @@ -242,3 +242,5 @@ user-whitelist watch-only whitelist-override-label www-prefix +retry_time +file_content_in_loop diff --git a/test/e2e/empty_dir.go b/test/e2e/empty_dir.go index 87d7e90d4c..a8724cab93 100644 --- a/test/e2e/empty_dir.go +++ b/test/e2e/empty_dir.go @@ -18,21 +18,21 @@ package e2e import ( "fmt" - "path" - "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/latest" "k8s.io/kubernetes/pkg/util" + "path" . "github.com/onsi/ginkgo" ) const ( - testImageRootUid = "gcr.io/google_containers/mounttest:0.3" - testImageNonRootUid = "gcr.io/google_containers/mounttest-user:0.1" + testImageRootUid = "gcr.io/google_containers/mounttest:0.4" + testImageNonRootUid = "gcr.io/google_containers/mounttest-user:0.2" ) var _ = Describe("EmptyDir volumes", func() { + f := NewFramework("emptydir") It("volume on tmpfs should have the correct mode", func() { @@ -204,7 +204,6 @@ func formatMedium(medium api.StorageMedium) string { func testPodWithVolume(image, path string, source *api.EmptyDirVolumeSource) *api.Pod { podName := "pod-" + string(util.NewUUID()) - return &api.Pod{ TypeMeta: api.TypeMeta{ Kind: "Pod", diff --git a/test/e2e/host_path.go b/test/e2e/host_path.go index f9d9d26df4..6e55c62bd5 100644 --- a/test/e2e/host_path.go +++ b/test/e2e/host_path.go @@ -21,7 +21,6 @@ import ( "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/latest" client "k8s.io/kubernetes/pkg/client/unversioned" - "k8s.io/kubernetes/pkg/util" "os" "path" @@ -77,18 +76,24 @@ var _ = Describe("hostPath", func() { It("should support r/w", func() { volumePath := "/test-volume" filePath := path.Join(volumePath, "test-file") + retryDuration := 180 source := &api.HostPathVolumeSource{ Path: "/tmp", } pod := testPodWithHostVol(volumePath, source) pod.Spec.Containers[0].Args = []string{ - fmt.Sprintf("--fs_type=%v", volumePath), - fmt.Sprintf("--rw_new_file=%v", filePath), + fmt.Sprintf("--new_file_0644=%v", filePath), fmt.Sprintf("--file_mode=%v", filePath), } - testContainerOutputInNamespace("hostPath r/w", c, pod, 0, []string{ - "mode of file \"/test-volume/test-file\": -rw-r--r--", + + pod.Spec.Containers[1].Args = []string{ + fmt.Sprintf("--file_content_in_loop=%v", filePath), + fmt.Sprintf("--retry_time=%d", retryDuration), + } + //Read the content of the file with the second container to + //verify volumes being shared properly among continers within the pod. + testContainerOutputInNamespace("hostPath r/w", c, pod, 1, []string{ "content of file \"/test-volume/test-file\": mount-tester new file", }, namespace.Name, ) @@ -96,8 +101,9 @@ var _ = Describe("hostPath", func() { }) //These constants are borrowed from the other test. -//const containerName = "test-container" //const volumeName = "test-volume" +const containerName1 = "test-container-1" +const containerName2 = "test-container-2" func mount(source *api.HostPathVolumeSource) []api.Volume { return []api.Volume{ @@ -112,7 +118,7 @@ func mount(source *api.HostPathVolumeSource) []api.Volume { //TODO: To merge this with the emptyDir tests, we can make source a lambda. func testPodWithHostVol(path string, source *api.HostPathVolumeSource) *api.Pod { - podName := "pod-" + string(util.NewUUID()) + podName := "pod-host-path-test" return &api.Pod{ TypeMeta: api.TypeMeta{ @@ -125,8 +131,18 @@ func testPodWithHostVol(path string, source *api.HostPathVolumeSource) *api.Pod Spec: api.PodSpec{ Containers: []api.Container{ { - Name: containerName, - Image: "gcr.io/google_containers/mounttest:0.2", + Name: containerName1, + Image: "gcr.io/google_containers/mounttest:0.4", + VolumeMounts: []api.VolumeMount{ + { + Name: volumeName, + MountPath: path, + }, + }, + }, + { + Name: containerName2, + Image: "gcr.io/google_containers/mounttest:0.4", VolumeMounts: []api.VolumeMount{ { Name: volumeName, diff --git a/test/images/mount-tester-user/Dockerfile b/test/images/mount-tester-user/Dockerfile index 136d92d3a4..732dc6788f 100644 --- a/test/images/mount-tester-user/Dockerfile +++ b/test/images/mount-tester-user/Dockerfile @@ -12,5 +12,5 @@ # See the License for the specific language governing permissions and # limitations under the License. -FROM gcr.io/google_containers/mounttest:0.3 +FROM gcr.io/google_containers/mounttest:0.4 USER 1001 diff --git a/test/images/mount-tester-user/Makefile b/test/images/mount-tester-user/Makefile index 0cb05d763b..2d12d0badb 100644 --- a/test/images/mount-tester-user/Makefile +++ b/test/images/mount-tester-user/Makefile @@ -1,6 +1,6 @@ all: push -TAG = 0.1 +TAG = 0.2 image: sudo docker build -t gcr.io/google_containers/mounttest-user:$(TAG) . diff --git a/test/images/mount-tester/Makefile b/test/images/mount-tester/Makefile index 01e6f584a5..0d012c70c1 100644 --- a/test/images/mount-tester/Makefile +++ b/test/images/mount-tester/Makefile @@ -1,6 +1,6 @@ all: push -TAG = 0.3 +TAG = 0.4 mt: mt.go CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' ./mt.go diff --git a/test/images/mount-tester/mt.go b/test/images/mount-tester/mt.go index 3e6fd3b382..a481228d47 100644 --- a/test/images/mount-tester/mt.go +++ b/test/images/mount-tester/mt.go @@ -22,26 +22,31 @@ import ( "io/ioutil" "os" "syscall" + "time" ) var ( - fsTypePath = "" - fileModePath = "" - filePermPath = "" - readFileContentPath = "" - newFilePath0644 = "" - newFilePath0666 = "" - newFilePath0777 = "" + fsTypePath = "" + fileModePath = "" + filePermPath = "" + newFilePath0644 = "" + newFilePath0666 = "" + newFilePath0777 = "" + readFileContentPath = "" + readFileContentInLoopPath = "" + retryDuration = 180 ) func init() { flag.StringVar(&fsTypePath, "fs_type", "", "Path to print the fs type for") flag.StringVar(&fileModePath, "file_mode", "", "Path to print the mode bits of") flag.StringVar(&filePermPath, "file_perm", "", "Path to print the perms of") - flag.StringVar(&readFileContentPath, "file_content", "", "Path to read the file content from") flag.StringVar(&newFilePath0644, "new_file_0644", "", "Path to write to and read from with perm 0644") flag.StringVar(&newFilePath0666, "new_file_0666", "", "Path to write to and read from with perm 0666") flag.StringVar(&newFilePath0777, "new_file_0777", "", "Path to write to and read from with perm 0777") + flag.StringVar(&readFileContentPath, "file_content", "", "Path to read the file content from") + flag.StringVar(&readFileContentInLoopPath, "file_content_in_loop", "", "Path to read the file content in loop from") + flag.IntVar(&retryDuration, "retry_time", 180, "Retry time during the loop") } // This program performs some tests on the filesystem as dictated by the @@ -101,6 +106,11 @@ func main() { errs = append(errs, err) } + err = readFileContentInLoop(readFileContentInLoopPath, retryDuration) + if err != nil { + errs = append(errs, err) + } + if len(errs) != 0 { os.Exit(1) } @@ -191,3 +201,40 @@ func readWriteNewFile(path string, perm os.FileMode) error { return readFileContent(path) } + +func readFileContentInLoop(path string, retryDuration int) error { + if path == "" { + return nil + } + var content []byte + content = testFileContent(path, retryDuration) + + fmt.Printf("content of file %q: %v\n", path, string(content)) + + return nil +} + +func testFileContent(filePath string, retryDuration int) []byte { + var ( + contentBytes []byte + err error + ) + + retryTime := time.Second * time.Duration(retryDuration) + for start := time.Now(); time.Since(start) < retryTime; time.Sleep(2 * time.Second) { + contentBytes, err = ioutil.ReadFile(filePath) + if err == nil { + //Expected content "mount-tester new file\n", length 22 + if len(contentBytes) == 22 { + break + } else { + fmt.Printf("Unexpected length of file: found %d, expected %d.Retry", len(contentBytes), 22) + } + } else { + fmt.Printf("Error read file %s: %v, retry", filePath, err) + } + + } + + return contentBytes +}