Skip to content

Commit c87ea76

Browse files
authored
Merge pull request #242 from Random-Liu/reopen-container-log-test
Add reopen container log test.
2 parents 78b9097 + e778671 commit c87ea76

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

hack/run-critest.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter
3030
sleep 10
3131

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

3536
# Run benchmark test cases
3637
critest b

pkg/validate/container.go

+44
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,36 @@ var _ = framework.KubeDescribe("Container", func() {
222222
}
223223
verifyLogContents(podConfig, logPath, expectedLogMessage)
224224
})
225+
226+
It("runtime should support reopening container log [Conformance]", func() {
227+
By("create container with log")
228+
logPath, containerID := createKeepLoggingContainer(rc, ic, "container-reopen-log-test-", podID, podConfig)
229+
230+
By("start container with log")
231+
startContainer(rc, containerID)
232+
233+
Eventually(func() []logMessage {
234+
return parseLogLine(podConfig, logPath)
235+
}, time.Minute, time.Second).ShouldNot(BeEmpty(), "container log should be generated")
236+
237+
By("rename container log")
238+
newLogPath := logPath + ".new"
239+
Expect(os.Rename(filepath.Join(podConfig.LogDirectory, logPath),
240+
filepath.Join(podConfig.LogDirectory, newLogPath))).To(Succeed())
241+
242+
By("reopen container log")
243+
Expect(rc.ReopenContainerLog(containerID)).To(Succeed())
244+
245+
Expect(pathExists(filepath.Join(podConfig.LogDirectory, logPath))).To(
246+
BeTrue(), "new container log file should be created")
247+
Eventually(func() []logMessage {
248+
return parseLogLine(podConfig, logPath)
249+
}, time.Minute, time.Second).ShouldNot(BeEmpty(), "new container log should be generated")
250+
oldLength := len(parseLogLine(podConfig, newLogPath))
251+
Consistently(func() int {
252+
return len(parseLogLine(podConfig, newLogPath))
253+
}, 5*time.Second, time.Second).Should(Equal(oldLength), "old container log should not change")
254+
})
225255
})
226256

227257
})
@@ -397,6 +427,20 @@ func createLogContainer(rc internalapi.RuntimeService, ic internalapi.ImageManag
397427
return containerConfig.LogPath, framework.CreateContainer(rc, ic, containerConfig, podID, podConfig)
398428
}
399429

430+
// createKeepLoggingContainer creates a container keeps logging defaultLog to output.
431+
func createKeepLoggingContainer(rc internalapi.RuntimeService, ic internalapi.ImageManagerService, prefix string, podID string, podConfig *runtimeapi.PodSandboxConfig) (string, string) {
432+
By("create a container with log and name")
433+
containerName := prefix + framework.NewUUID()
434+
path := fmt.Sprintf("%s.log", containerName)
435+
containerConfig := &runtimeapi.ContainerConfig{
436+
Metadata: framework.BuildContainerMetadata(containerName, framework.DefaultAttempt),
437+
Image: &runtimeapi.ImageSpec{Image: framework.DefaultContainerImage},
438+
Command: []string{"sh", "-c", "while true; do echo " + defaultLog + "; sleep 1; done"},
439+
LogPath: path,
440+
}
441+
return containerConfig.LogPath, framework.CreateContainer(rc, ic, containerConfig, podID, podConfig)
442+
}
443+
400444
// pathExists check whether 'path' does exist or not
401445
func pathExists(path string) bool {
402446
_, err := os.Stat(path)

0 commit comments

Comments
 (0)