package status import ( "testing" "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" ) func TestIsPodReady(t *testing.T) { testCases := []struct { name string status corev1.PodStatus expected bool }{ { name: "Pod is running and has ready condition true", status: corev1.PodStatus{ Phase: corev1.PodRunning, Conditions: []corev1.PodCondition{ { Type: corev1.PodReady, Status: corev1.ConditionTrue, }, }, }, expected: true, }, { name: "Pod is running but has ready condition false", status: corev1.PodStatus{ Phase: corev1.PodRunning, Conditions: []corev1.PodCondition{ { Type: corev1.PodReady, Status: corev1.ConditionFalse, }, }, }, expected: false, }, { name: "Pod is not running but has ready condition true", status: corev1.PodStatus{ Phase: corev1.PodPending, Conditions: []corev1.PodCondition{ { Type: corev1.PodReady, Status: corev1.ConditionTrue, }, }, }, expected: false, }, { name: "Pod is not running and has ready condition false", status: corev1.PodStatus{ Phase: corev1.PodFailed, Conditions: []corev1.PodCondition{ { Type: corev1.PodReady, Status: corev1.ConditionFalse, }, }, }, expected: false, }, { name: "Pod is not running and has no ready condition", status: corev1.PodStatus{ Phase: corev1.PodUnknown, Conditions: []corev1.PodCondition{}, }, expected: false, }, { name: "Pod is running but has no ready condition", status: corev1.PodStatus{ Phase: corev1.PodRunning, Conditions: []corev1.PodCondition{}, }, expected: false, }, { name: "Pod is running and has ready condition true (among other conditions)", status: corev1.PodStatus{ Phase: corev1.PodRunning, Conditions: []corev1.PodCondition{ { Type: corev1.PodInitialized, Status: corev1.ConditionTrue, }, { Type: corev1.PodReady, Status: corev1.ConditionTrue, }, { Type: corev1.PodScheduled, Status: corev1.ConditionTrue, }, }, }, expected: true, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { actual := IsPodReady(&corev1.Pod{Status: tc.status}) require.Equal(t, tc.expected, actual) }) } } func TestIsContainerCLBO(t *testing.T) { testCases := []struct { name string status corev1.ContainerStatus threshold uint16 expected bool }{ { name: "Not ready - Has waiting state CLBO - Over threshold", status: corev1.ContainerStatus{ Ready: false, State: corev1.ContainerState{ Waiting: &corev1.ContainerStateWaiting{ Reason: "CrashLoopBackOff", }, }, RestartCount: 101, }, threshold: 100, expected: true, }, { name: "Not ready - Has waiting state CLBO - Under threshold", status: corev1.ContainerStatus{ Ready: false, State: corev1.ContainerState{ Waiting: &corev1.ContainerStateWaiting{ Reason: "CrashLoopBackOff", }, }, RestartCount: 0, }, threshold: 1, expected: false, }, { name: "Not ready - Has waiting state CLBO - Equal to threshold", status: corev1.ContainerStatus{ Ready: false, State: corev1.ContainerState{ Waiting: &corev1.ContainerStateWaiting{ Reason: "CrashLoopBackOff", }, }, RestartCount: 100, }, threshold: 100, expected: false, }, { name: "Not ready - Has non-CLBO waiting state - Over threshold", status: corev1.ContainerStatus{ Ready: false, State: corev1.ContainerState{ Waiting: &corev1.ContainerStateWaiting{ Reason: "Waiting", }, }, RestartCount: 300, }, threshold: 100, expected: false, }, { name: "Ready - Has waiting state CLBO - Over threshold", status: corev1.ContainerStatus{ Ready: true, State: corev1.ContainerState{ Waiting: &corev1.ContainerStateWaiting{ Reason: "CrashLoopBackOff", }, }, RestartCount: 200, }, threshold: 100, expected: false, }, { name: "Ready - Has no waiting state - Over threshold", status: corev1.ContainerStatus{ Ready: true, State: corev1.ContainerState{}, RestartCount: 200, }, threshold: 100, expected: false, }, { name: "Ready - Has no waiting state - Under threshold", status: corev1.ContainerStatus{ Ready: true, State: corev1.ContainerState{}, RestartCount: 0, }, threshold: 100, expected: false, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { actual := IsContainerCLBO(tc.status, tc.threshold) require.Equal(t, tc.expected, actual) }) } }