From 4454815b9064fbf500ac7e164cf2fcb17ef6cda7 Mon Sep 17 00:00:00 2001 From: Drew Sessler Date: Mon, 6 Jan 2025 13:39:02 -0800 Subject: [PATCH] Add required Status fields and conditions to Job objects in tests. --- .../postgrescluster/pgbackrest_test.go | 18 ++++- .../postgrescluster/snapshots_test.go | 65 ++++++++++--------- 2 files changed, 50 insertions(+), 33 deletions(-) diff --git a/internal/controller/postgrescluster/pgbackrest_test.go b/internal/controller/postgrescluster/pgbackrest_test.go index 77d27fd29..5b024af64 100644 --- a/internal/controller/postgrescluster/pgbackrest_test.go +++ b/internal/controller/postgrescluster/pgbackrest_test.go @@ -2958,14 +2958,23 @@ func TestObserveRestoreEnv(t *testing.T) { }, } + currentTime := metav1.Now() + startTime := metav1.NewTime(currentTime.AddDate(0, 0, -1)) + restoreJob.Status.StartTime = &startTime + if completed != nil { if *completed { + restoreJob.Status.CompletionTime = ¤tTime restoreJob.Status.Conditions = append(restoreJob.Status.Conditions, batchv1.JobCondition{ Type: batchv1.JobComplete, Status: corev1.ConditionTrue, Reason: "test", Message: "test", - }) + }, + batchv1.JobCondition{ + Type: batchv1.JobSuccessCriteriaMet, + Status: corev1.ConditionTrue, + }) } else { restoreJob.Status.Conditions = append(restoreJob.Status.Conditions, batchv1.JobCondition{ Type: batchv1.JobComplete, @@ -2981,7 +2990,12 @@ func TestObserveRestoreEnv(t *testing.T) { Status: corev1.ConditionTrue, Reason: "test", Message: "test", - }) + }, + batchv1.JobCondition{ + Type: batchv1.JobFailureTarget, + Status: corev1.ConditionTrue, + }, + ) } else { restoreJob.Status.Conditions = append(restoreJob.Status.Conditions, batchv1.JobCondition{ Type: batchv1.JobFailed, diff --git a/internal/controller/postgrescluster/snapshots_test.go b/internal/controller/postgrescluster/snapshots_test.go index 0d1f12f3c..4c56b697f 100644 --- a/internal/controller/postgrescluster/snapshots_test.go +++ b/internal/controller/postgrescluster/snapshots_test.go @@ -471,10 +471,8 @@ func TestReconcileDedicatedSnapshotVolume(t *testing.T) { assert.NilError(t, r.apply(ctx, backupJob)) currentTime := metav1.Now() - backupJob.Status = batchv1.JobStatus{ - Succeeded: 1, - CompletionTime: ¤tTime, - } + startTime := metav1.NewTime(currentTime.AddDate(0, 0, -1)) + backupJob.Status = succeededJobStatus(startTime, currentTime) assert.NilError(t, r.Client.Status().Update(ctx, backupJob)) // Create instance set and volumes for reconcile @@ -514,17 +512,16 @@ func TestReconcileDedicatedSnapshotVolume(t *testing.T) { // Create times for jobs currentTime := metav1.Now() + currentStartTime := metav1.NewTime(currentTime.AddDate(0, 0, -1)) earlierTime := metav1.NewTime(currentTime.AddDate(-1, 0, 0)) + earlierStartTime := metav1.NewTime(earlierTime.AddDate(0, 0, -1)) // Create successful backup job backupJob := testBackupJob(cluster) assert.NilError(t, r.setControllerReference(cluster, backupJob)) assert.NilError(t, r.apply(ctx, backupJob)) - backupJob.Status = batchv1.JobStatus{ - Succeeded: 1, - CompletionTime: &earlierTime, - } + backupJob.Status = succeededJobStatus(earlierStartTime, earlierTime) assert.NilError(t, r.Client.Status().Update(ctx, backupJob)) // Create successful restore job @@ -535,10 +532,7 @@ func TestReconcileDedicatedSnapshotVolume(t *testing.T) { assert.NilError(t, r.setControllerReference(cluster, restoreJob)) assert.NilError(t, r.apply(ctx, restoreJob)) - restoreJob.Status = batchv1.JobStatus{ - Succeeded: 1, - CompletionTime: ¤tTime, - } + restoreJob.Status = succeededJobStatus(currentStartTime, currentTime) assert.NilError(t, r.Client.Status().Update(ctx, restoreJob)) // Create instance set and volumes for reconcile @@ -581,16 +575,14 @@ func TestReconcileDedicatedSnapshotVolume(t *testing.T) { // Create times for jobs currentTime := metav1.Now() earlierTime := metav1.NewTime(currentTime.AddDate(-1, 0, 0)) + startTime := metav1.NewTime(earlierTime.AddDate(0, 0, -1)) // Create successful backup job backupJob := testBackupJob(cluster) assert.NilError(t, r.setControllerReference(cluster, backupJob)) assert.NilError(t, r.apply(ctx, backupJob)) - backupJob.Status = batchv1.JobStatus{ - Succeeded: 1, - CompletionTime: &earlierTime, - } + backupJob.Status = succeededJobStatus(startTime, earlierTime) assert.NilError(t, r.Client.Status().Update(ctx, backupJob)) // Create failed restore job @@ -602,9 +594,8 @@ func TestReconcileDedicatedSnapshotVolume(t *testing.T) { assert.NilError(t, r.apply(ctx, restoreJob)) restoreJob.Status = batchv1.JobStatus{ - Succeeded: 0, - Failed: 1, - CompletionTime: ¤tTime, + Succeeded: 0, + Failed: 1, } assert.NilError(t, r.Client.Status().Update(ctx, restoreJob)) @@ -847,6 +838,7 @@ func TestGetLatestCompleteBackupJob(t *testing.T) { t.Run("OneCompleteBackupJob", func(t *testing.T) { currentTime := metav1.Now() + currentStartTime := metav1.NewTime(currentTime.AddDate(0, 0, -1)) job1 := testBackupJob(cluster) job1.Namespace = ns.Name @@ -863,10 +855,7 @@ func TestGetLatestCompleteBackupJob(t *testing.T) { // Get job1 and update Status. assert.NilError(t, r.Client.Get(ctx, client.ObjectKeyFromObject(job1), job1)) - job1.Status = batchv1.JobStatus{ - Succeeded: 1, - CompletionTime: ¤tTime, - } + job1.Status = succeededJobStatus(currentStartTime, currentTime) assert.NilError(t, r.Client.Status().Update(ctx, job1)) latestCompleteBackupJob, err := r.getLatestCompleteBackupJob(ctx, cluster) @@ -876,7 +865,9 @@ func TestGetLatestCompleteBackupJob(t *testing.T) { t.Run("TwoCompleteBackupJobs", func(t *testing.T) { currentTime := metav1.Now() + currentStartTime := metav1.NewTime(currentTime.AddDate(0, 0, -1)) earlierTime := metav1.NewTime(currentTime.AddDate(-1, 0, 0)) + earlierStartTime := metav1.NewTime(earlierTime.AddDate(0, 0, -1)) assert.Check(t, earlierTime.Before(¤tTime)) job1 := testBackupJob(cluster) @@ -894,19 +885,13 @@ func TestGetLatestCompleteBackupJob(t *testing.T) { // Get job1 and update Status. assert.NilError(t, r.Client.Get(ctx, client.ObjectKeyFromObject(job1), job1)) - job1.Status = batchv1.JobStatus{ - Succeeded: 1, - CompletionTime: ¤tTime, - } + job1.Status = succeededJobStatus(currentStartTime, currentTime) assert.NilError(t, r.Client.Status().Update(ctx, job1)) // Get job2 and update Status. assert.NilError(t, r.Client.Get(ctx, client.ObjectKeyFromObject(job2), job2)) - job2.Status = batchv1.JobStatus{ - Succeeded: 1, - CompletionTime: &earlierTime, - } + job2.Status = succeededJobStatus(earlierStartTime, earlierTime) assert.NilError(t, r.Client.Status().Update(ctx, job2)) latestCompleteBackupJob, err := r.getLatestCompleteBackupJob(ctx, cluster) @@ -1382,3 +1367,21 @@ func TestClusterUsingTablespaces(t *testing.T) { assert.Assert(t, clusterUsingTablespaces(ctx, cluster)) }) } + +func succeededJobStatus(startTime, completionTime metav1.Time) batchv1.JobStatus { + return batchv1.JobStatus{ + Succeeded: 1, + StartTime: &startTime, + CompletionTime: &completionTime, + Conditions: []batchv1.JobCondition{ + { + Type: batchv1.JobSuccessCriteriaMet, + Status: corev1.ConditionTrue, + }, + { + Type: batchv1.JobComplete, + Status: corev1.ConditionTrue, + }, + }, + } +}