-
Notifications
You must be signed in to change notification settings - Fork 2
/
job_test.go
148 lines (130 loc) · 3.56 KB
/
job_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"context"
"io/ioutil"
"net/http"
"strings"
"testing"
"time"
"github.com/pkg/errors"
"github.com/sethvargo/go-githubactions"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/rest/fake"
)
type jobClientMock struct {
createFn func(*v1.Job) (*v1.Job, error)
getFn func(string, metav1.GetOptions) (*v1.Job, error)
throwErr bool
}
func (j jobClientMock) Create(job *v1.Job) (*v1.Job, error) {
return j.createFn(job)
}
func (j jobClientMock) Get(name string, options metav1.GetOptions) (*v1.Job, error) {
return j.getFn(name, options)
}
type podClientMock struct {
logsChecked bool
}
func (p *podClientMock) GetLogs(name string, opts *corev1.PodLogOptions) *rest.Request {
c := fake.CreateHTTPClient(func(r *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader("pod log stream")),
}, nil
})
fakeClient := fake.RESTClient{Client: c}
p.logsChecked = true
return fakeClient.Request()
}
func (p *podClientMock) List(opts metav1.ListOptions) (*corev1.PodList, error) {
podMeta := metav1.ObjectMeta{Name: "test-job-pod"}
return &corev1.PodList{Items: []corev1.Pod{{ObjectMeta: podMeta}}}, nil
}
type noopLogger struct{}
func (noopLogger) Debugf(msg string, args ...interface{}) {}
func (noopLogger) Errorf(msg string, args ...interface{}) {}
func (noopLogger) Fatalf(msg string, args ...interface{}) {}
func (noopLogger) Warningf(msg string, args ...interface{}) {}
func TestRunJob(t *testing.T) {
testCases := []struct {
desc string
condition v1.JobConditionType
createJobError bool
getJobError bool
checkLogs bool
wantErr bool
}{
{
desc: "job created succesfully, job succeeded",
condition: v1.JobComplete,
checkLogs: true,
wantErr: false,
},
{
desc: "job created successfully, job failed",
condition: v1.JobFailed,
checkLogs: true,
wantErr: true,
},
{
desc: "create job failure",
condition: v1.JobFailed,
createJobError: true,
checkLogs: false,
wantErr: true,
},
{
desc: "get job failure",
condition: v1.JobFailed,
getJobError: true,
checkLogs: false,
wantErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
assert := assert.New(t)
jm := newJobClientMock(tc.condition, tc.createJobError, tc.getJobError)
pc := podClientMock{}
r := NewJobRunner(jm, &pc, 1*time.Second, githubactions.New())
_, err := r.RunJob(context.Background(), "test-job", "ns", "my-image")
assert.Equal(tc.checkLogs, pc.logsChecked)
if tc.wantErr {
assert.NotNil(err)
} else {
assert.NoError(err)
}
})
}
}
func newJobClientMock(condition v1.JobConditionType, createError, getError bool) jobClient {
jobMeta := metav1.ObjectMeta{Name: "test-job"}
return jobClientMock{
createFn: func(*v1.Job) (*v1.Job, error) {
if createError {
return nil, errors.New("error creating job")
}
return &v1.Job{ObjectMeta: jobMeta}, nil
},
getFn: func(string, metav1.GetOptions) (*v1.Job, error) {
if getError {
return nil, errors.New("error finding job")
}
return &v1.Job{
ObjectMeta: jobMeta,
Status: v1.JobStatus{
Conditions: []v1.JobCondition{{
Type: condition,
Status: corev1.ConditionTrue,
Reason: "ConditionReason",
Message: "This is what happened",
}},
},
}, nil
},
}
}