@@ -30,6 +30,66 @@ import (
3030 runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
3131)
3232
33+ func TestContainerLogWithoutTailingNewLine (t * testing.T ) {
34+ testPodLogDir , err := ioutil .TempDir ("/tmp" , "container-log-without-tailing-newline" )
35+ require .NoError (t , err )
36+ defer os .RemoveAll (testPodLogDir )
37+
38+ t .Log ("Create a sandbox with log directory" )
39+ sbConfig := PodSandboxConfig ("sandbox" , "container-log-without-tailing-newline" ,
40+ WithPodLogDirectory (testPodLogDir ),
41+ )
42+ sb , err := runtimeService .RunPodSandbox (sbConfig )
43+ require .NoError (t , err )
44+ defer func () {
45+ assert .NoError (t , runtimeService .StopPodSandbox (sb ))
46+ assert .NoError (t , runtimeService .RemovePodSandbox (sb ))
47+ }()
48+
49+ const (
50+ testImage = "busybox"
51+ containerName = "test-container"
52+ )
53+ t .Logf ("Pull test image %q" , testImage )
54+ img , err := imageService .PullImage (& runtime.ImageSpec {Image : testImage }, nil )
55+ require .NoError (t , err )
56+ defer func () {
57+ assert .NoError (t , imageService .RemoveImage (& runtime.ImageSpec {Image : img }))
58+ }()
59+
60+ t .Log ("Create a container with log path" )
61+ cnConfig := ContainerConfig (
62+ containerName ,
63+ testImage ,
64+ WithCommand ("sh" , "-c" , "printf abcd" ),
65+ WithLogPath (containerName ),
66+ )
67+ cn , err := runtimeService .CreateContainer (sb , cnConfig , sbConfig )
68+ require .NoError (t , err )
69+
70+ t .Log ("Start the container" )
71+ require .NoError (t , runtimeService .StartContainer (cn ))
72+
73+ t .Log ("Wait for container to finish running" )
74+ require .NoError (t , Eventually (func () (bool , error ) {
75+ s , err := runtimeService .ContainerStatus (cn )
76+ if err != nil {
77+ return false , err
78+ }
79+ if s .GetState () == runtime .ContainerState_CONTAINER_EXITED {
80+ return true , nil
81+ }
82+ return false , nil
83+ }, time .Second , 30 * time .Second ))
84+
85+ t .Log ("Check container log" )
86+ content , err := ioutil .ReadFile (filepath .Join (testPodLogDir , containerName ))
87+ assert .NoError (t , err )
88+ checkContainerLog (t , string (content ), []string {
89+ fmt .Sprintf ("%s %s %s" , runtime .Stdout , runtime .LogTagPartial , "abcd" ),
90+ })
91+ }
92+
3393func TestLongContainerLog (t * testing.T ) {
3494 testPodLogDir , err := ioutil .TempDir ("/tmp" , "long-container-log" )
3595 require .NoError (t , err )
@@ -66,9 +126,9 @@ func TestLongContainerLog(t *testing.T) {
66126 longLineCmd := fmt .Sprintf ("i=0; while [ $i -lt %d ]; do printf %s; i=$((i+1)); done" , maxSize + 1 , "c" )
67127 cnConfig := ContainerConfig (
68128 containerName ,
69- "busybox" ,
129+ testImage ,
70130 WithCommand ("sh" , "-c" ,
71- fmt .Sprintf ("%s; echo; %s; echo; %s" , shortLineCmd , maxLenLineCmd , longLineCmd )),
131+ fmt .Sprintf ("%s; echo; %s; echo; %s; echo " , shortLineCmd , maxLenLineCmd , longLineCmd )),
72132 WithLogPath (containerName ),
73133 )
74134 cn , err := runtimeService .CreateContainer (sb , cnConfig , sbConfig )
0 commit comments