|
| 1 | +package k8sutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/stretchr/testify/require" |
| 5 | + corev1 "k8s.io/api/core/v1" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestIsPodUnhealthy(t *testing.T) { |
| 10 | + andTrue := true |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + pod *corev1.Pod |
| 14 | + want bool |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "healthy pod with init containers", |
| 18 | + want: false, |
| 19 | + pod: &corev1.Pod{ |
| 20 | + Status: corev1.PodStatus{ |
| 21 | + Phase: "Running", |
| 22 | + Conditions: []corev1.PodCondition{ |
| 23 | + // ignored here |
| 24 | + }, |
| 25 | + InitContainerStatuses: []corev1.ContainerStatus{ |
| 26 | + { |
| 27 | + Name: "init", |
| 28 | + State: corev1.ContainerState{ |
| 29 | + Terminated: &corev1.ContainerStateTerminated{ |
| 30 | + ExitCode: 0, |
| 31 | + Reason: "Completed", |
| 32 | + }, |
| 33 | + }, |
| 34 | + Ready: true, |
| 35 | + RestartCount: 2, |
| 36 | + Image: "projectcontour/contour:v1.11.0", |
| 37 | + ImageID: "docker://sha256:12878e02b6f969de1456b51b0093f289fd195db956837ccd75aa679e9dac24d9", |
| 38 | + ContainerID: "docker://50fc0794fa1402b1a48521e2fd380a92521a69db70ad25c9699c91fccd839d70", |
| 39 | + }, |
| 40 | + }, |
| 41 | + ContainerStatuses: []corev1.ContainerStatus{ |
| 42 | + { |
| 43 | + Name: "contour", |
| 44 | + State: corev1.ContainerState{ |
| 45 | + Running: &corev1.ContainerStateRunning{}, |
| 46 | + }, |
| 47 | + Ready: true, |
| 48 | + RestartCount: 5, |
| 49 | + Image: "projectcontour/contour:v1.11.0", |
| 50 | + ImageID: "docker://sha256:12878e02b6f969de1456b51b0093f289fd195db956837ccd75aa679e9dac24d9", |
| 51 | + ContainerID: "docker://50fc0794fa1402b1a48521e2fd380a92521a69db70ad25c9699c91fccd839d70", |
| 52 | + Started: &andTrue, |
| 53 | + }, |
| 54 | + }, |
| 55 | + QOSClass: corev1.PodQOSBestEffort, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "running pod with one unhealthy container", |
| 61 | + want: true, |
| 62 | + pod: &corev1.Pod{ |
| 63 | + Status: corev1.PodStatus{ |
| 64 | + Phase: "Running", |
| 65 | + Conditions: []corev1.PodCondition{ |
| 66 | + // ignored here |
| 67 | + }, |
| 68 | + ContainerStatuses: []corev1.ContainerStatus{ |
| 69 | + { |
| 70 | + Name: "envoy", |
| 71 | + State: corev1.ContainerState{ |
| 72 | + Running: &corev1.ContainerStateRunning{}, |
| 73 | + }, |
| 74 | + Ready: false, |
| 75 | + RestartCount: 5, |
| 76 | + Started: &andTrue, |
| 77 | + }, |
| 78 | + { |
| 79 | + Name: "shutdown-manager", |
| 80 | + State: corev1.ContainerState{ |
| 81 | + Running: &corev1.ContainerStateRunning{}, |
| 82 | + }, |
| 83 | + Ready: true, |
| 84 | + RestartCount: 5, |
| 85 | + Started: &andTrue, |
| 86 | + }, |
| 87 | + }, |
| 88 | + QOSClass: corev1.PodQOSBestEffort, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + } |
| 93 | + for _, tt := range tests { |
| 94 | + t.Run(tt.name, func(t *testing.T) { |
| 95 | + req := require.New(t) |
| 96 | + |
| 97 | + got := IsPodUnhealthy(tt.pod) |
| 98 | + req.Equal(tt.want, got) |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
0 commit comments