-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(kubernetes): IsPodContainerInState
Signed-off-by: Chin-Ya Huang <chin-ya.huang@suse.com>
- Loading branch information
Showing
1 changed file
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
|
||
} |