Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve pkg/k8sutil/pod.go ut coverage #570

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions pkg/k8sutil/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,144 @@ func Test_Resourcereqs(t *testing.T) {
})
}
}
func Test_IsPodInTerminatedState(t *testing.T) {
tests := []struct {
name string
args *corev1.Pod
want bool
}{
{
name: "pod in failed state",
args: &corev1.Pod{
Status: corev1.PodStatus{
Phase: corev1.PodFailed,
},
},
want: true,
},
{
name: "pod in succeeded state",
args: &corev1.Pod{
Status: corev1.PodStatus{
Phase: corev1.PodSucceeded,
},
},
want: true,
},
{
name: "pod in running state",
args: &corev1.Pod{
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
},
},
want: false,
},
{
name: "pod in pending state",
args: &corev1.Pod{
Status: corev1.PodStatus{
Phase: corev1.PodPending,
},
},
want: false,
},
{
name: "pod in unknown state",
args: &corev1.Pod{
Status: corev1.PodStatus{
Phase: corev1.PodUnknown,
},
},
want: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := IsPodInTerminatedState(test.args)
assert.Equal(t, test.want, got)
})
}
}
func Test_AllContainersCreated(t *testing.T) {
tests := []struct {
name string
args *corev1.Pod
want bool
}{
{
name: "all containers created",
args: &corev1.Pod{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{},
{},
},
},
Status: corev1.PodStatus{
ContainerStatuses: []corev1.ContainerStatus{
{},
{},
},
},
},
want: true,
},
{
name: "not all containers created",
args: &corev1.Pod{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{},
{},
},
},
Status: corev1.PodStatus{
ContainerStatuses: []corev1.ContainerStatus{
{},
},
},
},
want: false,
},
{
name: "no containers created",
args: &corev1.Pod{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{},
},
},
Status: corev1.PodStatus{
ContainerStatuses: []corev1.ContainerStatus{},
},
},
want: false,
},
{
name: "more container statuses than containers",
args: &corev1.Pod{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{},
},
},
Status: corev1.PodStatus{
ContainerStatuses: []corev1.ContainerStatus{
{},
{},
},
},
},
want: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := AllContainersCreated(test.args)
assert.Equal(t, test.want, got)
})
}
}
Loading