From d1cc9b39a33e335c56e68e1305d27bc036363406 Mon Sep 17 00:00:00 2001 From: Austin Abro Date: Sun, 9 Feb 2025 15:32:46 +0000 Subject: [PATCH] tests for status reader Signed-off-by: Austin Abro --- pkg/kube/job_status_reader_test.go | 93 ++++++++++++++++-------- pkg/kube/pod_status_reader.go | 8 +-- pkg/kube/pod_status_reader_test.go | 110 ++++++++++++++++++++--------- 3 files changed, 145 insertions(+), 66 deletions(-) diff --git a/pkg/kube/job_status_reader_test.go b/pkg/kube/job_status_reader_test.go index 98b994030..cd0dcedeb 100644 --- a/pkg/kube/job_status_reader_test.go +++ b/pkg/kube/job_status_reader_test.go @@ -30,7 +30,8 @@ import ( "sigs.k8s.io/cli-utils/pkg/kstatus/status" ) -func toUnstructured(obj runtime.Object) (*unstructured.Unstructured, error) { +func toUnstructured(t *testing.T, obj runtime.Object) (*unstructured.Unstructured, error) { + t.Helper() // If the incoming object is already unstructured, perform a deep copy first // otherwise DefaultUnstructuredConverter ends up returning the inner map without // making a copy. @@ -45,35 +46,69 @@ func toUnstructured(obj runtime.Object) (*unstructured.Unstructured, error) { } func TestJobConditions(t *testing.T) { - job := &batchv1.Job{ - ObjectMeta: metav1.ObjectMeta{ - Name: "job", + t.Parallel() + tests := []struct { + name string + job *batchv1.Job + expectedStatus status.Status + }{ + { + name: "job without Complete condition returns InProgress status", + job: &batchv1.Job{ + ObjectMeta: metav1.ObjectMeta{ + Name: "job-no-condition", + }, + Spec: batchv1.JobSpec{}, + Status: batchv1.JobStatus{}, + }, + expectedStatus: status.InProgressStatus, }, - Spec: batchv1.JobSpec{}, - Status: batchv1.JobStatus{}, - } - - t.Run("job without Complete condition returns InProgress status", func(t *testing.T) { - us, err := toUnstructured(job) - assert.NoError(t, err) - result, err := jobConditions(us) - assert.NoError(t, err) - assert.Equal(t, status.InProgressStatus, result.Status) - }) - - t.Run("job with Complete condition as True returns Current status", func(t *testing.T) { - job.Status = batchv1.JobStatus{ - Conditions: []batchv1.JobCondition{ - { - Type: batchv1.JobComplete, - Status: corev1.ConditionTrue, + { + name: "job with Complete condition as True returns Current status", + job: &batchv1.Job{ + ObjectMeta: metav1.ObjectMeta{ + Name: "job-complete", + }, + Spec: batchv1.JobSpec{}, + Status: batchv1.JobStatus{ + Conditions: []batchv1.JobCondition{ + { + Type: batchv1.JobComplete, + Status: corev1.ConditionTrue, + }, + }, + }, + }, + expectedStatus: status.CurrentStatus, + }, + { + name: "job with Failed condition as True returns Failed status", + job: &batchv1.Job{ + ObjectMeta: metav1.ObjectMeta{ + Name: "job-failed", + }, + Spec: batchv1.JobSpec{}, + Status: batchv1.JobStatus{ + Conditions: []batchv1.JobCondition{ + { + Type: batchv1.JobFailed, + Status: corev1.ConditionTrue, + }, + }, }, }, - } - us, err := toUnstructured(job) - assert.NoError(t, err) - result, err := jobConditions(us) - assert.NoError(t, err) - assert.Equal(t, status.CurrentStatus, result.Status) - }) + expectedStatus: status.FailedStatus, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + us, err := toUnstructured(t, tc.job) + assert.NoError(t, err) + result, err := jobConditions(us) + assert.NoError(t, err) + assert.Equal(t, tc.expectedStatus, result.Status) + }) + } } diff --git a/pkg/kube/pod_status_reader.go b/pkg/kube/pod_status_reader.go index 25e427966..752f73ac1 100644 --- a/pkg/kube/pod_status_reader.go +++ b/pkg/kube/pod_status_reader.go @@ -62,8 +62,8 @@ func (j *customPodStatusReader) ReadStatusForObject(ctx context.Context, reader func podConditions(u *unstructured.Unstructured) (*status.Result, error) { obj := u.UnstructuredContent() phase := status.GetStringField(obj, ".status.phase", "") - switch phase { - case string(v1.PodSucceeded): + switch v1.PodPhase(phase) { + case v1.PodSucceeded: message := fmt.Sprintf("pod %s succeeded", u.GetName()) return &status.Result{ Status: status.CurrentStatus, @@ -76,7 +76,7 @@ func podConditions(u *unstructured.Unstructured) (*status.Result, error) { }, }, }, nil - case string(v1.PodFailed): + case v1.PodFailed: message := fmt.Sprintf("pod %s failed", u.GetName()) return &status.Result{ Status: status.FailedStatus, @@ -90,8 +90,6 @@ func podConditions(u *unstructured.Unstructured) (*status.Result, error) { }, }, }, nil - case string(v1.PodPending): - case string(v1.PodRunning): } message := "Pod in progress" diff --git a/pkg/kube/pod_status_reader_test.go b/pkg/kube/pod_status_reader_test.go index 2604ef026..bb08f041a 100644 --- a/pkg/kube/pod_status_reader_test.go +++ b/pkg/kube/pod_status_reader_test.go @@ -28,39 +28,85 @@ import ( ) func TestPodConditions(t *testing.T) { - t.Parallel() - - //TODO add some more tests here and parallelize - - t.Run("pod without status returns in progress", func(t *testing.T) { - pod := &v1.Pod{ - ObjectMeta: metav1.ObjectMeta{ - Name: "pod", + tests := []struct { + name string + pod *v1.Pod + expectedStatus status.Status + }{ + { + name: "pod without status returns in progress", + pod: &v1.Pod{ + ObjectMeta: metav1.ObjectMeta{Name: "pod-no-status"}, + Spec: v1.PodSpec{}, + Status: v1.PodStatus{}, }, - Spec: v1.PodSpec{}, - Status: v1.PodStatus{}, - } - us, err := toUnstructured(pod) - assert.NoError(t, err) - result, err := podConditions(us) - assert.NoError(t, err) - assert.Equal(t, status.InProgressStatus, result.Status) - }) - - t.Run("pod succeeded returns Current status", func(t *testing.T) { - pod := &v1.Pod{ - ObjectMeta: metav1.ObjectMeta{ - Name: "pod", + expectedStatus: status.InProgressStatus, + }, + { + name: "pod succeeded returns current status", + pod: &v1.Pod{ + ObjectMeta: metav1.ObjectMeta{Name: "pod-succeeded"}, + Spec: v1.PodSpec{}, + Status: v1.PodStatus{ + Phase: v1.PodSucceeded, + }, + }, + expectedStatus: status.CurrentStatus, + }, + { + name: "pod failed returns failed status", + pod: &v1.Pod{ + ObjectMeta: metav1.ObjectMeta{Name: "pod-failed"}, + Spec: v1.PodSpec{}, + Status: v1.PodStatus{ + Phase: v1.PodFailed, + }, }, - Spec: v1.PodSpec{}, - Status: v1.PodStatus{ - Phase: v1.PodSucceeded, + expectedStatus: status.FailedStatus, + }, + { + name: "pod pending returns in progress status", + pod: &v1.Pod{ + ObjectMeta: metav1.ObjectMeta{Name: "pod-pending"}, + Spec: v1.PodSpec{}, + Status: v1.PodStatus{ + Phase: v1.PodPending, + }, }, - } - us, err := toUnstructured(pod) - assert.NoError(t, err) - result, err := podConditions(us) - assert.NoError(t, err) - assert.Equal(t, status.CurrentStatus, result.Status) - }) + expectedStatus: status.InProgressStatus, + }, + { + name: "pod running returns in progress status", + pod: &v1.Pod{ + ObjectMeta: metav1.ObjectMeta{Name: "pod-running"}, + Spec: v1.PodSpec{}, + Status: v1.PodStatus{ + Phase: v1.PodRunning, + }, + }, + expectedStatus: status.InProgressStatus, + }, + { + name: "pod with unknown phase returns in progress status", + pod: &v1.Pod{ + ObjectMeta: metav1.ObjectMeta{Name: "pod-unknown"}, + Spec: v1.PodSpec{}, + Status: v1.PodStatus{ + Phase: v1.PodUnknown, + }, + }, + expectedStatus: status.InProgressStatus, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + us, err := toUnstructured(t, tc.pod) + assert.NoError(t, err) + result, err := podConditions(us) + assert.NoError(t, err) + assert.Equal(t, tc.expectedStatus, result.Status) + }) + } }