@@ -222,6 +222,36 @@ var _ = framework.KubeDescribe("Container", func() {
222
222
}
223
223
verifyLogContents (podConfig , logPath , expectedLogMessage )
224
224
})
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
+ })
225
255
})
226
256
227
257
})
@@ -397,6 +427,20 @@ func createLogContainer(rc internalapi.RuntimeService, ic internalapi.ImageManag
397
427
return containerConfig .LogPath , framework .CreateContainer (rc , ic , containerConfig , podID , podConfig )
398
428
}
399
429
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
+
400
444
// pathExists check whether 'path' does exist or not
401
445
func pathExists (path string ) bool {
402
446
_ , err := os .Stat (path )
0 commit comments