Skip to content

Commit 9a8fe35

Browse files
UT cases for garbagecollector package
1 parent fe03590 commit 9a8fe35

File tree

1 file changed

+381
-0
lines changed

1 file changed

+381
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,381 @@
1+
/*
2+
Copyright 2019 The Volcano Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package garbagecollector
18+
19+
import (
20+
"fmt"
21+
"testing"
22+
"time"
23+
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
26+
"volcano.sh/volcano/pkg/apis/batch/v1alpha1"
27+
volcanoclient "volcano.sh/volcano/pkg/client/clientset/versioned/fake"
28+
)
29+
30+
func TestGarbageCollector_ProcessJob(t *testing.T) {
31+
32+
}
33+
34+
func TestGarbageCollector_ProcessTTL(t *testing.T) {
35+
namespace := "test"
36+
var ttlSecond int32 = 3
37+
var ttlSecondZero int32
38+
testcases := []struct {
39+
Name string
40+
Job *v1alpha1.Job
41+
ExpectedVal bool
42+
ExpectedErr error
43+
}{
44+
{
45+
Name: "False Case",
46+
Job: &v1alpha1.Job{
47+
ObjectMeta: metav1.ObjectMeta{
48+
Name: "job1",
49+
Namespace: namespace,
50+
},
51+
Spec: v1alpha1.JobSpec{
52+
TTLSecondsAfterFinished: &ttlSecond,
53+
},
54+
Status: v1alpha1.JobStatus{
55+
State: v1alpha1.JobState{
56+
LastTransitionTime: metav1.NewTime(time.Now()),
57+
Phase: v1alpha1.Completed,
58+
},
59+
},
60+
},
61+
ExpectedVal: false,
62+
ExpectedErr: nil,
63+
},
64+
{
65+
Name: "True Case",
66+
Job: &v1alpha1.Job{
67+
ObjectMeta: metav1.ObjectMeta{
68+
Name: "job1",
69+
Namespace: namespace,
70+
},
71+
Spec: v1alpha1.JobSpec{
72+
TTLSecondsAfterFinished: &ttlSecondZero,
73+
},
74+
Status: v1alpha1.JobStatus{
75+
State: v1alpha1.JobState{
76+
LastTransitionTime: metav1.NewTime(time.Now()),
77+
Phase: v1alpha1.Completed,
78+
},
79+
},
80+
},
81+
ExpectedVal: true,
82+
ExpectedErr: nil,
83+
},
84+
}
85+
for i, testcase := range testcases {
86+
gc := New(volcanoclient.NewSimpleClientset())
87+
88+
expired, err := gc.processTTL(testcase.Job)
89+
if err != nil {
90+
t.Error("Did not expect error")
91+
}
92+
if expired != testcase.ExpectedVal {
93+
t.Errorf("Expected Return Value to be %t, but got %t in case %d", testcase.ExpectedVal, expired, i)
94+
}
95+
}
96+
}
97+
98+
func TestGarbageCollector_NeedsCleanup(t *testing.T) {
99+
namespace := "test"
100+
101+
var ttlSecond int32 = 3
102+
103+
testcases := []struct {
104+
Name string
105+
Job *v1alpha1.Job
106+
ExpectedVal bool
107+
}{
108+
{
109+
Name: "Success Case",
110+
Job: &v1alpha1.Job{
111+
ObjectMeta: metav1.ObjectMeta{
112+
Name: "job1",
113+
Namespace: namespace,
114+
},
115+
Spec: v1alpha1.JobSpec{
116+
TTLSecondsAfterFinished: &ttlSecond,
117+
},
118+
Status: v1alpha1.JobStatus{
119+
State: v1alpha1.JobState{
120+
Phase: v1alpha1.Completed,
121+
},
122+
},
123+
},
124+
ExpectedVal: true,
125+
},
126+
{
127+
Name: "Failure Case",
128+
Job: &v1alpha1.Job{
129+
ObjectMeta: metav1.ObjectMeta{
130+
Name: "job1",
131+
Namespace: namespace,
132+
},
133+
Spec: v1alpha1.JobSpec{
134+
TTLSecondsAfterFinished: &ttlSecond,
135+
},
136+
Status: v1alpha1.JobStatus{
137+
State: v1alpha1.JobState{
138+
Phase: v1alpha1.Running,
139+
},
140+
},
141+
},
142+
ExpectedVal: false,
143+
},
144+
}
145+
146+
for i, testcase := range testcases {
147+
finished := needsCleanup(testcase.Job)
148+
if finished != testcase.ExpectedVal {
149+
t.Errorf("Expected value to be %t, but got: %t in case %d", testcase.ExpectedVal, finished, i)
150+
}
151+
}
152+
}
153+
154+
func TestGarbageCollector_IsJobFinished(t *testing.T) {
155+
namespace := "test"
156+
157+
testcases := []struct {
158+
Name string
159+
Job *v1alpha1.Job
160+
ExpectedVal bool
161+
}{
162+
{
163+
Name: "Success Case",
164+
Job: &v1alpha1.Job{
165+
ObjectMeta: metav1.ObjectMeta{
166+
Name: "job1",
167+
Namespace: namespace,
168+
},
169+
Status: v1alpha1.JobStatus{
170+
State: v1alpha1.JobState{
171+
Phase: v1alpha1.Completed,
172+
},
173+
},
174+
},
175+
ExpectedVal: true,
176+
},
177+
{
178+
Name: "Success Case",
179+
Job: &v1alpha1.Job{
180+
ObjectMeta: metav1.ObjectMeta{
181+
Name: "job1",
182+
Namespace: namespace,
183+
},
184+
Status: v1alpha1.JobStatus{
185+
State: v1alpha1.JobState{
186+
Phase: v1alpha1.Running,
187+
},
188+
},
189+
},
190+
ExpectedVal: false,
191+
},
192+
}
193+
194+
for i, testcase := range testcases {
195+
finished := isJobFinished(testcase.Job)
196+
if finished != testcase.ExpectedVal {
197+
t.Errorf("Expected value to be %t, but got: %t in case %d", testcase.ExpectedVal, finished, i)
198+
}
199+
}
200+
}
201+
202+
func TestGarbageCollector_GetFinishAndExpireTime(t *testing.T) {
203+
namespace := "test"
204+
205+
var ttlSecond int32 = 3
206+
var ttlSecondFail int32 = 2
207+
208+
testTime := time.Date(1, 1, 1, 1, 1, 1, 0, time.UTC)
209+
210+
testcases := []struct {
211+
Name string
212+
Job *v1alpha1.Job
213+
ExpectedErr error
214+
}{
215+
{
216+
Name: "Success case",
217+
Job: &v1alpha1.Job{
218+
ObjectMeta: metav1.ObjectMeta{
219+
Name: "job1",
220+
Namespace: namespace,
221+
},
222+
Spec: v1alpha1.JobSpec{
223+
TTLSecondsAfterFinished: &ttlSecond,
224+
},
225+
Status: v1alpha1.JobStatus{
226+
State: v1alpha1.JobState{
227+
Phase: v1alpha1.Completed,
228+
LastTransitionTime: metav1.NewTime(testTime),
229+
},
230+
},
231+
},
232+
ExpectedErr: nil,
233+
},
234+
{
235+
Name: "Failure case",
236+
Job: &v1alpha1.Job{
237+
ObjectMeta: metav1.ObjectMeta{
238+
Name: "job1",
239+
Namespace: namespace,
240+
},
241+
Spec: v1alpha1.JobSpec{
242+
TTLSecondsAfterFinished: &ttlSecondFail,
243+
},
244+
Status: v1alpha1.JobStatus{
245+
State: v1alpha1.JobState{
246+
Phase: v1alpha1.Completed,
247+
LastTransitionTime: metav1.NewTime(testTime),
248+
},
249+
},
250+
},
251+
ExpectedErr: nil,
252+
},
253+
}
254+
255+
for i, testcase := range testcases {
256+
finishTime, expireTime, err := getFinishAndExpireTime(testcase.Job)
257+
if err != nil && err.Error() != testcase.ExpectedErr.Error() {
258+
t.Errorf("Expected Error to be: %s but got: %s in case %d", testcase.ExpectedErr, err, i)
259+
}
260+
261+
if finishTime != nil && metav1.NewTime(*finishTime) != testcase.Job.Status.State.LastTransitionTime {
262+
t.Errorf("Expected value to be: %v, but got: %v in case %d", testcase.Job.Status.State.LastTransitionTime, metav1.NewTime(*finishTime), i)
263+
}
264+
265+
if expireTime != nil && metav1.NewTime(*expireTime) != metav1.NewTime(testcase.Job.Status.State.LastTransitionTime.Add(time.Duration(*testcase.Job.Spec.TTLSecondsAfterFinished)*time.Second)) {
266+
t.Errorf("Expected value to be: %v, but got: %v in case %d", testcase.Job.Status.State.LastTransitionTime.Add(time.Duration(*testcase.Job.Spec.TTLSecondsAfterFinished)*time.Second), metav1.NewTime(*expireTime), i)
267+
}
268+
}
269+
}
270+
271+
func TestGarbageCollector_TimeLeft(t *testing.T) {
272+
namespace := "test"
273+
274+
var ttlSecond int32 = 3
275+
276+
testTime := time.Date(1, 1, 1, 1, 1, 1, 0, time.UTC)
277+
278+
testcases := []struct {
279+
Name string
280+
Job *v1alpha1.Job
281+
Time *time.Time
282+
ExpectedVal time.Duration
283+
ExpectedErr error
284+
}{
285+
{
286+
Name: "Success Case",
287+
Job: &v1alpha1.Job{
288+
ObjectMeta: metav1.ObjectMeta{
289+
Name: "job1",
290+
Namespace: namespace,
291+
},
292+
Spec: v1alpha1.JobSpec{
293+
TTLSecondsAfterFinished: &ttlSecond,
294+
},
295+
Status: v1alpha1.JobStatus{
296+
State: v1alpha1.JobState{
297+
Phase: v1alpha1.Completed,
298+
LastTransitionTime: metav1.NewTime(testTime),
299+
},
300+
},
301+
},
302+
Time: &testTime,
303+
ExpectedVal: time.Duration(3),
304+
ExpectedErr: nil,
305+
},
306+
{
307+
Name: "Failure Case",
308+
Job: &v1alpha1.Job{
309+
ObjectMeta: metav1.ObjectMeta{
310+
Name: "job1",
311+
Namespace: namespace,
312+
},
313+
Spec: v1alpha1.JobSpec{
314+
TTLSecondsAfterFinished: &ttlSecond,
315+
},
316+
Status: v1alpha1.JobStatus{
317+
State: v1alpha1.JobState{
318+
LastTransitionTime: metav1.NewTime(testTime),
319+
},
320+
},
321+
},
322+
Time: &testTime,
323+
ExpectedVal: time.Duration(3),
324+
ExpectedErr: fmt.Errorf("job %s/%s should not be cleaned up", "test", "job1"),
325+
},
326+
}
327+
328+
for i, testcase := range testcases {
329+
timeDuration, err := timeLeft(testcase.Job, testcase.Time)
330+
if err != nil && err.Error() != testcase.ExpectedErr.Error() {
331+
t.Errorf("Expected Error to be: %s but got: %s in case %d", testcase.ExpectedErr, err, i)
332+
}
333+
334+
if timeDuration != nil && timeDuration.Seconds() != float64(testcase.ExpectedVal*time.Second)/1e9 {
335+
t.Errorf("Expected Value to be: %v but got: %f in case %d", testcase.ExpectedVal, timeDuration.Seconds(), i)
336+
}
337+
}
338+
}
339+
340+
func TestGarbageCollector_JobFinishTime(t *testing.T) {
341+
namespace := "test"
342+
343+
testcases := []struct {
344+
Name string
345+
Job *v1alpha1.Job
346+
ExpectedVal error
347+
}{
348+
{
349+
Name: "Success Case",
350+
Job: &v1alpha1.Job{
351+
ObjectMeta: metav1.ObjectMeta{
352+
Name: "job1",
353+
Namespace: namespace,
354+
},
355+
Status: v1alpha1.JobStatus{
356+
State: v1alpha1.JobState{
357+
LastTransitionTime: metav1.NewTime(time.Now()),
358+
},
359+
},
360+
},
361+
ExpectedVal: nil,
362+
},
363+
{
364+
Name: "Failure Case",
365+
Job: &v1alpha1.Job{
366+
ObjectMeta: metav1.ObjectMeta{
367+
Name: "job1",
368+
Namespace: namespace,
369+
},
370+
},
371+
ExpectedVal: fmt.Errorf("unable to find the time when the Job %s/%s finished", "test", "job1"),
372+
},
373+
}
374+
375+
for i, testcase := range testcases {
376+
_, err := jobFinishTime(testcase.Job)
377+
if err != nil && err.Error() != testcase.ExpectedVal.Error() {
378+
t.Errorf("Expected Error to be: %s but got: %s in case %d", testcase.ExpectedVal, err, i)
379+
}
380+
}
381+
}

0 commit comments

Comments
 (0)