Skip to content

Commit

Permalink
fix: missing tests for suspending jobs (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
aclevername authored Feb 12, 2024
1 parent 524ea3e commit a6a05e1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
48 changes: 46 additions & 2 deletions controllers/promise_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"regexp"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -806,8 +807,8 @@ var _ = Describe("PromiseController", func() {
})

When("the promise is updated repeatedly", func() {
It("ensures only the last 5 jobs are kept", func() {
var timestamp time.Time
var timestamp time.Time
BeforeEach(func() {
for i := 0; i < 10; i++ {
if i == 6 {
timestamp = time.Now()
Expand All @@ -825,13 +826,47 @@ var _ = Describe("PromiseController", func() {
Expect(result).To(Equal(ctrl.Result{}))
}

})

It("ensures only the last 5 jobs are kept", func() {
jobs := &batchv1.JobList{}
Expect(fakeK8sClient.List(ctx, jobs)).To(Succeed())
Expect(jobs.Items).To(HaveLen(5))
for _, job := range jobs.Items {
Expect(job.CreationTimestamp.Time).To(BeTemporally(">", timestamp))
}
})

When("a pipeline is in progress", func() {
BeforeEach(func() {
//Mark the latest job as still running
jobs := &batchv1.JobList{}
Expect(fakeK8sClient.List(ctx, jobs)).To(Succeed())
latestJob := jobs.Items[len(jobs.Items)-1]
latestJob.Status.Conditions = nil
latestJob.Status.Succeeded = 0
Expect(fakeK8sClient.Status().Update(ctx, &latestJob)).To(Succeed())

_, err := t.reconcileUntilCompletion(reconciler, promise, &opts{
funcs: []func(client.Object) error{autoMarkCRDAsEstablished},
})
t.reconcileCount = 0
Expect(err).To(MatchError("reconcile loop detected"))
})

It("suspends all existing jobs apart from the latest", func() {
jobs := &batchv1.JobList{}
Expect(fakeK8sClient.List(ctx, jobs)).To(Succeed())
Expect(jobs.Items).To(HaveLen(5))
sortedJobs := sortJobsByCreationDateTime(jobs.Items)
for x := 0; x < len(jobs.Items)-1; x++ {
job := sortedJobs[x]
Expect(job.Spec.Suspend).NotTo(BeNil())
Expect(*job.Spec.Suspend).To(BeTrue())
}
Expect(jobs.Items[len(jobs.Items)-1].Spec.Suspend).To(BeNil())
})
})
})
})

Expand Down Expand Up @@ -913,3 +948,12 @@ func autoMarkCRDAsEstablished(obj client.Object) error {
}
return nil
}

func sortJobsByCreationDateTime(jobs []batchv1.Job) []batchv1.Job {
sort.Slice(jobs, func(i, j int) bool {
t1 := jobs[i].GetCreationTimestamp().Time
t2 := jobs[j].GetCreationTimestamp().Time
return t1.Before(t2)
})
return jobs
}
2 changes: 1 addition & 1 deletion controllers/shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func autoCompleteJobAndCreateWork(labels map[string]string, workName string) fun
//Fake library doesn't set timestamp, and we need it set for comparing age
//of jobs. This ensures its set once, and only when its first created, and
//that they differ by a large enough amont (time.Now() alone was not enough)
job.CreationTimestamp = metav1.NewTime(time.Now().Add(time.Duration(callCount) * time.Hour))
job.CreationTimestamp = metav1.NewTime(time.Now().Add(time.Duration(callCount) * time.Minute))
err := fakeK8sClient.Update(ctx, job)
if err != nil {
return err
Expand Down

0 comments on commit a6a05e1

Please sign in to comment.