-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs_test.go
106 lines (97 loc) · 2.39 KB
/
jobs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"context"
"testing"
"time"
log "github.com/sirupsen/logrus"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
watch "k8s.io/apimachinery/pkg/watch"
fakeclientset "k8s.io/client-go/kubernetes/fake"
testcore "k8s.io/client-go/testing"
)
func init() {
log.SetLevel(log.DebugLevel)
}
func TestJobComplete(t *testing.T) {
description := StateDescription{
Namespace: "test-ns",
Type: JobResource,
LabelSelector: "app=test",
RequiredStates: []ResourceState{ResourceComplete},
}
fake := fakeclientset.NewSimpleClientset()
jobList := &batchv1.JobList{
Items: []batchv1.Job{
batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": "test",
},
Name: "job-1",
Namespace: "test-ns",
},
Status: batchv1.JobStatus{
Active: 1,
Succeeded: 0,
Failed: 0,
Conditions: []batchv1.JobCondition{
batchv1.JobCondition{
Type: batchv1.JobComplete,
Status: v1.ConditionFalse,
},
batchv1.JobCondition{
Type: batchv1.JobFailed,
Status: v1.ConditionFalse,
},
},
},
},
},
}
watcher := watch.NewFakeWithChanSize(1, false)
fake.PrependReactor("list", "jobs", func(action testcore.Action) (bool, runtime.Object, error) {
return true, jobList, nil
})
fake.PrependWatchReactor("jobs", testcore.DefaultWatchReactor(watcher, nil))
matcher := NewJobMatcher(fake, description)
go matcher.Start(context.Background())
// simulate real watch
for _, job := range jobList.Items {
watcher.Add(&job)
}
select {
case <-matcher.Done():
t.Fatal("matcher should not return")
default:
}
sleepDuration, _ := time.ParseDuration("1s")
time.Sleep(sleepDuration)
watcher.Modify(&batchv1.Job{
ObjectMeta: jobList.Items[0].ObjectMeta,
Status: batchv1.JobStatus{
Active: 0,
Succeeded: 1,
Failed: 0,
Conditions: []batchv1.JobCondition{
batchv1.JobCondition{
Type: batchv1.JobComplete,
Status: v1.ConditionTrue,
},
batchv1.JobCondition{
Type: batchv1.JobFailed,
Status: v1.ConditionFalse,
},
},
},
})
// wait for matcher
timeoutDuration, _ := time.ParseDuration("500ms")
select {
case <-time.After(timeoutDuration):
t.Fatalf("matcher did not return after 500ms")
case <-matcher.Done():
}
}