Skip to content
Open
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
131 changes: 131 additions & 0 deletions pkg/util/helper/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,3 +767,134 @@ func Test_getServiceAccountNames(t *testing.T) {
})
}
}
func TestIsPodReady(t *testing.T) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The helper function IsPodReady in pod.go does not perform a nil check on the pod parameter before accessing pod.Status. This can lead to a panic if a nil pod is passed. Please consider updating IsPodReady to handle nil safely (e.g., returning false), and then add a corresponding test case in TestIsPodReady to verify this behavior.

tests := []struct {
name string
pod *corev1.Pod
want bool
}{
{
name: "pod with no conditions",
pod: &corev1.Pod{},
want: false,
},
{
name: "pod with Ready condition False",
pod: &corev1.Pod{
Status: corev1.PodStatus{
Conditions: []corev1.PodCondition{
{Type: corev1.PodReady, Status: corev1.ConditionFalse},
},
},
},
want: false,
},
{
name: "pod with Ready condition True",
pod: &corev1.Pod{
Status: corev1.PodStatus{
Conditions: []corev1.PodCondition{
{Type: corev1.PodReady, Status: corev1.ConditionTrue},
},
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsPodReady(tt.pod); got != tt.want {
t.Errorf("IsPodReady() = %v, want %v", got, tt.want)
}
})
}
}

func TestIsPodReadyConditionTrue(t *testing.T) {
tests := []struct {
name string
status corev1.PodStatus
want bool
}{
{
name: "no conditions",
status: corev1.PodStatus{},
want: false,
},
{
name: "Ready condition is False",
status: corev1.PodStatus{
Conditions: []corev1.PodCondition{
{Type: corev1.PodReady, Status: corev1.ConditionFalse},
},
},
want: false,
},
{
name: "Ready condition is Unknown",
status: corev1.PodStatus{
Conditions: []corev1.PodCondition{
{Type: corev1.PodReady, Status: corev1.ConditionUnknown},
},
},
want: false,
},
{
name: "Ready condition is True",
status: corev1.PodStatus{
Conditions: []corev1.PodCondition{
{Type: corev1.PodReady, Status: corev1.ConditionTrue},
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsPodReadyConditionTrue(tt.status); got != tt.want {
t.Errorf("IsPodReadyConditionTrue() = %v, want %v", got, tt.want)
}
})
}
}

func TestGetPodReadyCondition(t *testing.T) {
tests := []struct {
name string
status corev1.PodStatus
want *corev1.PodCondition
}{
{
name: "no conditions",
status: corev1.PodStatus{},
want: nil,
},
{
name: "Ready condition not present",
status: corev1.PodStatus{
Conditions: []corev1.PodCondition{
{Type: corev1.PodInitialized, Status: corev1.ConditionTrue},
},
},
want: nil,
},
{
name: "Ready condition present",
status: corev1.PodStatus{
Conditions: []corev1.PodCondition{
{Type: corev1.PodInitialized, Status: corev1.ConditionTrue},
{Type: corev1.PodReady, Status: corev1.ConditionTrue},
},
},
want: &corev1.PodCondition{Type: corev1.PodReady, Status: corev1.ConditionTrue},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := GetPodReadyCondition(tt.status)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetPodReadyCondition() = %v, want %v", got, tt.want)
}
})
}
}