Skip to content

Commit

Permalink
Merge pull request #148 from miaoyq/volume-host-dir-unexisted
Browse files Browse the repository at this point in the history
Add validation for volume when host path doesn't exist
  • Loading branch information
feiskyer authored Sep 29, 2017
2 parents 955e01a + a693dd9 commit 67f63af
Showing 1 changed file with 41 additions and 9 deletions.
50 changes: 41 additions & 9 deletions pkg/validate/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,39 @@ var _ = framework.KubeDescribe("Container", func() {

It("runtime should support starting container with volume [Conformance]", func() {
By("create host path and flag file")
hostPath, flagFile := createHostPath(podID)
hostPath, _ := createHostPath(podID)

defer os.RemoveAll(hostPath) // clean up the TempDir

By("create container with volume")
containerID := createVolumeContainer(rc, ic, "container-with-volume-test-", podID, podConfig, hostPath, flagFile)
containerID := createVolumeContainer(rc, ic, "container-with-volume-test-", podID, podConfig, hostPath)

By("test start container with volume")
testStartContainer(rc, containerID)

By("test container exit code")
Eventually(func() int32 {
return getContainerStatus(rc, containerID).ExitCode
}, time.Minute, time.Second*4).Should(Equal(int32(0)))
By("check whether 'hostPath' contains file or dir in container")
command := []string{"ls", "-A", hostPath}
output := execSyncContainer(rc, containerID, command)
Expect(len(output)).NotTo(BeZero(), "len(output) should not be zero.")
})

It("runtime should support starting container with volume when host path doesn't exist", func() {
By("get a host path that doesn't exist")
hostPath, _ := createHostPath(podID)
os.RemoveAll(hostPath) // make sure the host path doesn't exist

defer os.RemoveAll(hostPath) // clean up the TempDir

By("create container with volume")
containerID := createVolumeContainer(rc, ic, "container-with-volume-test-", podID, podConfig, hostPath)

By("test start container with volume")
testStartContainer(rc, containerID)

By("check whether 'hostPath' does exist or not in host")
Eventually(func() bool {
return pathExists(hostPath)
}, time.Minute, time.Second*4).Should(Equal(true))
})
})

Expand Down Expand Up @@ -460,14 +479,14 @@ func createHostPath(podID string) (string, string) {
}

// createVolumeContainer creates a container with volume and the prefix of containerName and fails if it gets error.
func createVolumeContainer(rc internalapi.RuntimeService, ic internalapi.ImageManagerService, prefix string, podID string, podConfig *runtimeapi.PodSandboxConfig, hostPath, flagFile string) string {
func createVolumeContainer(rc internalapi.RuntimeService, ic internalapi.ImageManagerService, prefix string, podID string, podConfig *runtimeapi.PodSandboxConfig, hostPath string) string {
By("create a container with volume and name")
containerName := prefix + framework.NewUUID()
containerConfig := &runtimeapi.ContainerConfig{
Metadata: framework.BuildContainerMetadata(containerName, framework.DefaultAttempt),
Image: &runtimeapi.ImageSpec{Image: framework.DefaultContainerImage},
// mount host path to the same directory in container, and check if flag file exists
Command: []string{"sh", "-c", "test -f " + filepath.Join(hostPath, flagFile)},
Command: []string{"sh", "-c", "top"},
// mount host path to the same directory in container, and will check if hostPath isn't empty
Mounts: []*runtimeapi.Mount{
{
HostPath: hostPath,
Expand All @@ -493,6 +512,19 @@ func createLogContainer(rc internalapi.RuntimeService, ic internalapi.ImageManag
return containerConfig.LogPath, framework.CreateContainer(rc, ic, containerConfig, podID, podConfig)
}

// pathExists check whether 'path' does exist or not
func pathExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
framework.ExpectNoError(err, "failed to check whether %q Exists: %v", path, err)
return false
}

// parseDockerJSONLog parses logs in Docker JSON log format.
// Docker JSON log format example:
// {"log":"content 1","stream":"stdout","time":"2016-10-20T18:39:20.57606443Z"}
Expand Down

0 comments on commit 67f63af

Please sign in to comment.