From 18485c16b787ce5d545c6fbd1b588b09d8737c89 Mon Sep 17 00:00:00 2001 From: Chin-Ya Huang Date: Fri, 19 Apr 2024 16:55:50 +0800 Subject: [PATCH] test(kubernetes): IsPodContainerInState Signed-off-by: Chin-Ya Huang --- kubernetes/pod_test.go | 169 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 kubernetes/pod_test.go diff --git a/kubernetes/pod_test.go b/kubernetes/pod_test.go new file mode 100644 index 00000000..bf9c9567 --- /dev/null +++ b/kubernetes/pod_test.go @@ -0,0 +1,169 @@ +package kubernetes + +import ( + . "gopkg.in/check.v1" + + corev1 "k8s.io/api/core/v1" +) + +func (s *TestSuite) TestIsPodContainerInState(c *C) { + containerName := "test" + type testCase struct { + pod *corev1.Pod + conditionFunc func(*corev1.ContainerStatus) bool + expectedState bool + } + testCases := map[string]testCase{ + "IsPodContainerInState(...): container is completed": { + pod: &corev1.Pod{ + Status: corev1.PodStatus{ + ContainerStatuses: []corev1.ContainerStatus{ + { + Name: containerName, + State: corev1.ContainerState{ + Terminated: &corev1.ContainerStateTerminated{ + ExitCode: 0, + }, + }, + }, + }, + }, + }, + conditionFunc: IsContainerCompleted, + expectedState: true, + }, + "IsPodContainerInState(...): container is not completed": { + pod: &corev1.Pod{ + Status: corev1.PodStatus{ + ContainerStatuses: []corev1.ContainerStatus{ + { + Name: containerName, + State: corev1.ContainerState{ + Terminated: &corev1.ContainerStateTerminated{ + ExitCode: 1, + }, + }, + }, + }, + }, + }, + conditionFunc: IsContainerCompleted, + expectedState: false, + }, + "IsPodContainerInState(...): init container is completed": { + pod: &corev1.Pod{ + Status: corev1.PodStatus{ + InitContainerStatuses: []corev1.ContainerStatus{ + { + Name: containerName, + State: corev1.ContainerState{ + Terminated: &corev1.ContainerStateTerminated{ + ExitCode: 0, + }, + }, + }, + }, + }, + }, + conditionFunc: IsContainerCompleted, + expectedState: true, + }, + "IsPodContainerInState(...): init container is not completed": { + pod: &corev1.Pod{ + Status: corev1.PodStatus{ + InitContainerStatuses: []corev1.ContainerStatus{ + { + Name: containerName, + State: corev1.ContainerState{ + Terminated: &corev1.ContainerStateTerminated{ + ExitCode: 1, + }, + }, + }, + }, + }, + }, + conditionFunc: IsContainerCompleted, + expectedState: false, + }, + "IsPodContainerInState(...): container is initializing": { + pod: &corev1.Pod{ + Status: corev1.PodStatus{ + ContainerStatuses: []corev1.ContainerStatus{ + { + Name: containerName, + State: corev1.ContainerState{ + Waiting: &corev1.ContainerStateWaiting{ + Reason: "PodInitializing", + }, + }, + }, + }, + }, + }, + conditionFunc: IsContainerInitializing, + expectedState: true, + }, + "IsPodContainerInState(...): container is ready": { + pod: &corev1.Pod{ + Status: corev1.PodStatus{ + ContainerStatuses: []corev1.ContainerStatus{ + { + Name: containerName, + Ready: true, + }, + }, + }, + }, + conditionFunc: IsContainerReady, + expectedState: true, + }, + + "IsPodContainerInState(...): container is restarted": { + pod: &corev1.Pod{ + Status: corev1.PodStatus{ + ContainerStatuses: []corev1.ContainerStatus{ + { + Name: containerName, + State: corev1.ContainerState{ + Terminated: &corev1.ContainerStateTerminated{ + ExitCode: 0, + }, + }, + RestartCount: 1, + }, + }, + }, + }, + conditionFunc: IsContainerRestarted, + expectedState: true, + }, + + "IsPodContainerInState(...): container is waiting crash loop back off": { + pod: &corev1.Pod{ + Status: corev1.PodStatus{ + ContainerStatuses: []corev1.ContainerStatus{ + { + Name: containerName, + State: corev1.ContainerState{ + Waiting: &corev1.ContainerStateWaiting{ + Reason: "CrashLoopBackOff", + }, + }, + }, + }, + }, + }, + conditionFunc: IsContainerWaitingCrashLoopBackOff, + expectedState: true, + }, + } + + for testName, testCase := range testCases { + c.Logf("testing kubernetes.%v", testName) + + isInState := IsPodContainerInState(testCase.pod, containerName, testCase.conditionFunc) + c.Assert(isInState, Equals, testCase.expectedState) + } + +}