Skip to content

Commit

Permalink
Add reopen container log test.
Browse files Browse the repository at this point in the history
Signed-off-by: Lantao Liu <[email protected]>
  • Loading branch information
Random-Liu committed Feb 13, 2018
1 parent 653cc8c commit e778671
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion hack/run-critest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter
sleep 10

# Run e2e test cases
critest v
# Skip reopen container log test because docker doesn't support it.
critest --skip="runtime should support reopening container log" v

# Run benchmark test cases
critest b
44 changes: 44 additions & 0 deletions pkg/validate/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,36 @@ var _ = framework.KubeDescribe("Container", func() {
}
verifyLogContents(podConfig, logPath, expectedLogMessage)
})

It("runtime should support reopening container log [Conformance]", func() {
By("create container with log")
logPath, containerID := createKeepLoggingContainer(rc, ic, "container-reopen-log-test-", podID, podConfig)

By("start container with log")
startContainer(rc, containerID)

Eventually(func() []logMessage {
return parseLogLine(podConfig, logPath)
}, time.Minute, time.Second).ShouldNot(BeEmpty(), "container log should be generated")

By("rename container log")
newLogPath := logPath + ".new"
Expect(os.Rename(filepath.Join(podConfig.LogDirectory, logPath),
filepath.Join(podConfig.LogDirectory, newLogPath))).To(Succeed())

By("reopen container log")
Expect(rc.ReopenContainerLog(containerID)).To(Succeed())

Expect(pathExists(filepath.Join(podConfig.LogDirectory, logPath))).To(
BeTrue(), "new container log file should be created")
Eventually(func() []logMessage {
return parseLogLine(podConfig, logPath)
}, time.Minute, time.Second).ShouldNot(BeEmpty(), "new container log should be generated")
oldLength := len(parseLogLine(podConfig, newLogPath))
Consistently(func() int {
return len(parseLogLine(podConfig, newLogPath))
}, 5*time.Second, time.Second).Should(Equal(oldLength), "old container log should not change")
})
})

})
Expand Down Expand Up @@ -397,6 +427,20 @@ func createLogContainer(rc internalapi.RuntimeService, ic internalapi.ImageManag
return containerConfig.LogPath, framework.CreateContainer(rc, ic, containerConfig, podID, podConfig)
}

// createKeepLoggingContainer creates a container keeps logging defaultLog to output.
func createKeepLoggingContainer(rc internalapi.RuntimeService, ic internalapi.ImageManagerService, prefix string, podID string, podConfig *runtimeapi.PodSandboxConfig) (string, string) {
By("create a container with log and name")
containerName := prefix + framework.NewUUID()
path := fmt.Sprintf("%s.log", containerName)
containerConfig := &runtimeapi.ContainerConfig{
Metadata: framework.BuildContainerMetadata(containerName, framework.DefaultAttempt),
Image: &runtimeapi.ImageSpec{Image: framework.DefaultContainerImage},
Command: []string{"sh", "-c", "while true; do echo " + defaultLog + "; sleep 1; done"},
LogPath: path,
}
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)
Expand Down

0 comments on commit e778671

Please sign in to comment.