@@ -27,11 +27,12 @@ import (
27
27
28
28
"github.com/stretchr/testify/assert"
29
29
30
- "k8s.io/api/core/v1"
30
+ v1 "k8s.io/api/core/v1"
31
31
"k8s.io/apimachinery/pkg/api/errors"
32
32
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33
33
"k8s.io/apimachinery/pkg/runtime"
34
34
"k8s.io/apimachinery/pkg/runtime/schema"
35
+ "k8s.io/apimachinery/pkg/util/diff"
35
36
clientset "k8s.io/client-go/kubernetes"
36
37
"k8s.io/client-go/kubernetes/fake"
37
38
core "k8s.io/client-go/testing"
@@ -569,6 +570,16 @@ func TestTerminatePod(t *testing.T) {
569
570
t .Logf ("update the pod's status to Failed. TerminatePod should preserve this status update." )
570
571
firstStatus := getRandomPodStatus ()
571
572
firstStatus .Phase = v1 .PodFailed
573
+ firstStatus .InitContainerStatuses = []v1.ContainerStatus {
574
+ {Name : "init-test-1" },
575
+ {Name : "init-test-2" , State : v1.ContainerState {Terminated : & v1.ContainerStateTerminated {Reason : "InitTest" , ExitCode : 0 }}},
576
+ {Name : "init-test-3" , State : v1.ContainerState {Terminated : & v1.ContainerStateTerminated {Reason : "InitTest" , ExitCode : 3 }}},
577
+ }
578
+ firstStatus .ContainerStatuses = []v1.ContainerStatus {
579
+ {Name : "test-1" },
580
+ {Name : "test-2" , State : v1.ContainerState {Terminated : & v1.ContainerStateTerminated {Reason : "Test" , ExitCode : 2 }}},
581
+ {Name : "test-3" , State : v1.ContainerState {Terminated : & v1.ContainerStateTerminated {Reason : "Test" , ExitCode : 0 }}},
582
+ }
572
583
syncer .SetPodStatus (testPod , firstStatus )
573
584
574
585
t .Logf ("set the testPod to a pod with Phase running, to simulate a stale pod" )
@@ -586,6 +597,26 @@ func TestTerminatePod(t *testing.T) {
586
597
assert .False (t , newStatus .InitContainerStatuses [i ].State .Terminated == nil , "expected init containers to be terminated" )
587
598
}
588
599
600
+ expectUnknownState := v1.ContainerState {Terminated : & v1.ContainerStateTerminated {Reason : "ContainerStatusUnknown" , Message : "The container could not be located when the pod was terminated" , ExitCode : 137 }}
601
+ if ! reflect .DeepEqual (newStatus .InitContainerStatuses [0 ].State , expectUnknownState ) {
602
+ t .Errorf ("terminated container state not defaulted: %s" , diff .ObjectReflectDiff (newStatus .InitContainerStatuses [0 ].State , expectUnknownState ))
603
+ }
604
+ if ! reflect .DeepEqual (newStatus .InitContainerStatuses [1 ].State , firstStatus .InitContainerStatuses [1 ].State ) {
605
+ t .Errorf ("existing terminated container state not preserved: %#v" , newStatus .ContainerStatuses )
606
+ }
607
+ if ! reflect .DeepEqual (newStatus .InitContainerStatuses [2 ].State , firstStatus .InitContainerStatuses [2 ].State ) {
608
+ t .Errorf ("existing terminated container state not preserved: %#v" , newStatus .ContainerStatuses )
609
+ }
610
+ if ! reflect .DeepEqual (newStatus .ContainerStatuses [0 ].State , expectUnknownState ) {
611
+ t .Errorf ("terminated container state not defaulted: %s" , diff .ObjectReflectDiff (newStatus .ContainerStatuses [0 ].State , expectUnknownState ))
612
+ }
613
+ if ! reflect .DeepEqual (newStatus .ContainerStatuses [1 ].State , firstStatus .ContainerStatuses [1 ].State ) {
614
+ t .Errorf ("existing terminated container state not preserved: %#v" , newStatus .ContainerStatuses )
615
+ }
616
+ if ! reflect .DeepEqual (newStatus .ContainerStatuses [2 ].State , firstStatus .ContainerStatuses [2 ].State ) {
617
+ t .Errorf ("existing terminated container state not preserved: %#v" , newStatus .ContainerStatuses )
618
+ }
619
+
589
620
t .Logf ("we expect the previous status update to be preserved." )
590
621
assert .Equal (t , newStatus .Phase , firstStatus .Phase )
591
622
assert .Equal (t , newStatus .Message , firstStatus .Message )
0 commit comments