Skip to content

Commit 1122e1d

Browse files
committed
add e2e test for ttl seconds after finished in jobset
1 parent a315ef0 commit 1122e1d

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

pkg/util/testing/wrappers.go

+5
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ func (j *JobSetWrapper) DeletionTimestamp(deletionTimestamp *metav1.Time) *JobSe
153153

154154
}
155155

156+
func (j *JobSetWrapper) Finalizers(finalizers []string) *JobSetWrapper {
157+
j.ObjectMeta.Finalizers = finalizers
158+
return j
159+
}
160+
156161
// ReplicatedJobWrapper wraps a ReplicatedJob.
157162
type ReplicatedJobWrapper struct {
158163
jobset.ReplicatedJob

test/e2e/e2e_test.go

+52-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ var _ = ginkgo.Describe("JobSet", func() {
9595
gomega.Expect(k8sClient.Create(ctx, js)).Should(gomega.Succeed())
9696

9797
// We'll need to retry getting this newly created jobset, given that creation may not immediately happen.
98-
gomega.Eventually(k8sClient.Get(ctx, types.NamespacedName{Name: js.Name, Namespace: js.Namespace}, &jobset.JobSet{}), timeout, interval).Should(gomega.Succeed())
98+
gomega.Expect(k8sClient.Get(ctx, types.NamespacedName{Name: js.Name, Namespace: js.Namespace}, &jobset.JobSet{}), timeout, interval).Should(gomega.Succeed())
9999

100100
ginkgo.By("checking all jobs were created successfully")
101101
gomega.Eventually(util.NumJobs, timeout, interval).WithArguments(ctx, k8sClient, js).Should(gomega.Equal(util.NumExpectedJobs(js)))
@@ -105,6 +105,35 @@ var _ = ginkgo.Describe("JobSet", func() {
105105
util.JobSetCompleted(ctx, k8sClient, js, timeout)
106106
})
107107
})
108+
ginkgo.When("ttl seconds after finished is set", func() {
109+
ginkgo.It("should clean up the completed jobset after configured ttl seconds expire", func() {
110+
ctx := context.Background()
111+
112+
// Create JobSet.
113+
testFinalizer := "fake.example.com/blockDeletion"
114+
ginkgo.By("creating jobset with ttl seconds after finished")
115+
js := sleepTestJobSet(ns).Finalizers([]string{testFinalizer}).TTLSecondsAfterFinished(5).Obj()
116+
117+
// Verify jobset created successfully.
118+
ginkgo.By("checking that jobset creation succeeds")
119+
gomega.Expect(k8sClient.Create(ctx, js)).Should(gomega.Succeed())
120+
121+
// Check jobset status if specified.
122+
ginkgo.By("checking jobset condition")
123+
util.JobSetCompleted(ctx, k8sClient, js, timeout)
124+
125+
// We get the latest version of the jobset before removing the finalizer
126+
var fresh jobset.JobSet
127+
gomega.Expect(k8sClient.Get(ctx, types.NamespacedName{Name: js.Name, Namespace: js.Namespace}, &fresh)).Should(gomega.Succeed())
128+
129+
// We remove the jobset finalizer, so it can get deleted when ttl expires.
130+
util.RemoveJobSetFinalizer(ctx, k8sClient, &fresh, testFinalizer)
131+
132+
// Check jobset is cleaned up after ttl seconds.
133+
ginkgo.By("checking jobset is cleaned up after ttl seconds")
134+
util.JobSetDeleted(ctx, k8sClient, &fresh, timeout)
135+
})
136+
})
108137

109138
}) // end of Describe
110139

@@ -197,3 +226,25 @@ func pingTestJobSetSubdomain(ns *corev1.Namespace) *testing.JobSetWrapper {
197226
Replicas(int32(replicas)).
198227
Obj())
199228
}
229+
230+
func sleepTestJobSet(ns *corev1.Namespace) *testing.JobSetWrapper {
231+
jsName := "js"
232+
rjobName := "rjob"
233+
replicas := 4
234+
return testing.MakeJobSet(jsName, ns.Name).
235+
ReplicatedJob(testing.MakeReplicatedJob(rjobName).
236+
Job(testing.MakeJobTemplate("job", ns.Name).
237+
PodSpec(corev1.PodSpec{
238+
RestartPolicy: "Never",
239+
Containers: []corev1.Container{
240+
{
241+
Name: "sleep-test-container",
242+
Image: "bash:latest",
243+
Command: []string{"bash", "-c"},
244+
Args: []string{"sleep 20"},
245+
},
246+
},
247+
}).Obj()).
248+
Replicas(int32(replicas)).
249+
Obj())
250+
}

test/util/util.go

+21
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,24 @@ func ExpectJobsDeletionTimestamp(ctx context.Context, c client.Client, js *jobse
188188
return numJobs == 0, nil
189189
}, timeout, interval).Should(gomega.Equal(true))
190190
}
191+
192+
func JobSetDeleted(ctx context.Context, k8sClient client.Client, js *jobset.JobSet, timeout time.Duration) {
193+
ginkgo.By("checking jobset is deleted")
194+
gomega.Eventually(func() (bool, error) {
195+
err := k8sClient.Get(ctx, types.NamespacedName{Namespace: js.Namespace, Name: js.Name}, js)
196+
if apierrors.IsNotFound(err) {
197+
return true, nil
198+
}
199+
return false, err
200+
}, timeout, interval).Should(gomega.Equal(true))
201+
}
202+
203+
func RemoveJobSetFinalizer(ctx context.Context, k8sClient client.Client, js *jobset.JobSet, finalizer string) {
204+
ginkgo.By("removing jobset finalizers")
205+
for i, f := range js.Finalizers {
206+
if f == finalizer {
207+
js.Finalizers = append(js.Finalizers[:i], js.Finalizers[i+1:]...)
208+
}
209+
}
210+
gomega.Expect(k8sClient.Update(ctx, js)).Should(gomega.Succeed())
211+
}

0 commit comments

Comments
 (0)