Skip to content

Commit

Permalink
add e2e test for ttl seconds after finished in jobset
Browse files Browse the repository at this point in the history
  • Loading branch information
dejanzele committed Apr 16, 2024
1 parent a315ef0 commit ea522c4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/util/testing/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ func (j *JobSetWrapper) DeletionTimestamp(deletionTimestamp *metav1.Time) *JobSe

}

func (j *JobSetWrapper) Finalizers(finalizers []string) *JobSetWrapper {
j.ObjectMeta.Finalizers = finalizers
return j
}

// ReplicatedJobWrapper wraps a ReplicatedJob.
type ReplicatedJobWrapper struct {
jobset.ReplicatedJob
Expand Down
51 changes: 51 additions & 0 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,35 @@ var _ = ginkgo.Describe("JobSet", func() {
util.JobSetCompleted(ctx, k8sClient, js, timeout)
})
})
ginkgo.When("ttl seconds after finished is set", func() {
ginkgo.It("should clean up the completed jobset after configured ttl seconds expire", func() {
ctx := context.Background()

// Create JobSet.
testFinalizer := "fake.example.com/blockDeletion"
ginkgo.By("creating jobset with ttl seconds after finished")
js := sleepTestJobSet(ns).Finalizers([]string{testFinalizer}).TTLSecondsAfterFinished(5).Obj()

// Verify jobset created successfully.
ginkgo.By("checking that jobset creation succeeds")
gomega.Expect(k8sClient.Create(ctx, js)).Should(gomega.Succeed())

// Check jobset status if specified.
ginkgo.By("checking jobset condition")
util.JobSetCompleted(ctx, k8sClient, js, timeout)

// We get the latest version of the jobset before removing the finalizer
var fresh jobset.JobSet
gomega.Expect(k8sClient.Get(ctx, types.NamespacedName{Name: js.Name, Namespace: js.Namespace}, &fresh)).Should(gomega.Succeed())

// We remove the jobset finalizer, so it can get deleted when ttl expires.
util.RemoveJobSetFinalizer(ctx, k8sClient, &fresh, testFinalizer)

// Check jobset is cleaned up after ttl seconds.
ginkgo.By("checking jobset is cleaned up after ttl seconds")
util.JobSetDeleted(ctx, k8sClient, &fresh, timeout)
})
})

}) // end of Describe

Expand Down Expand Up @@ -197,3 +226,25 @@ func pingTestJobSetSubdomain(ns *corev1.Namespace) *testing.JobSetWrapper {
Replicas(int32(replicas)).
Obj())
}

func sleepTestJobSet(ns *corev1.Namespace) *testing.JobSetWrapper {
jsName := "js"
rjobName := "rjob"
replicas := 4
return testing.MakeJobSet(jsName, ns.Name).
ReplicatedJob(testing.MakeReplicatedJob(rjobName).
Job(testing.MakeJobTemplate("job", ns.Name).
PodSpec(corev1.PodSpec{
RestartPolicy: "Never",
Containers: []corev1.Container{
{
Name: "sleep-test-container",
Image: "bash:latest",
Command: []string{"bash", "-c"},
Args: []string{"sleep 20"},
},
},
}).Obj()).
Replicas(int32(replicas)).
Obj())
}
21 changes: 21 additions & 0 deletions test/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,24 @@ func ExpectJobsDeletionTimestamp(ctx context.Context, c client.Client, js *jobse
return numJobs == 0, nil
}, timeout, interval).Should(gomega.Equal(true))
}

func JobSetDeleted(ctx context.Context, k8sClient client.Client, js *jobset.JobSet, timeout time.Duration) {
ginkgo.By("checking jobset is deleted")
gomega.Eventually(func() (bool, error) {
err := k8sClient.Get(ctx, types.NamespacedName{Namespace: js.Namespace, Name: js.Name}, js)
if apierrors.IsNotFound(err) {
return true, nil
}
return false, err
}, timeout, interval).Should(gomega.Equal(true))
}

func RemoveJobSetFinalizer(ctx context.Context, k8sClient client.Client, js *jobset.JobSet, finalizer string) {
ginkgo.By("removing jobset finalizers")
for i, f := range js.Finalizers {
if f == finalizer {
js.Finalizers = append(js.Finalizers[:i], js.Finalizers[i+1:]...)
}
}
gomega.Expect(k8sClient.Update(ctx, js)).Should(gomega.Succeed())
}

0 comments on commit ea522c4

Please sign in to comment.