-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
buildpod_controller_test.go
404 lines (371 loc) · 12.9 KB
/
buildpod_controller_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package buildpod
import (
"errors"
"reflect"
"testing"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
kapi "k8s.io/kubernetes/pkg/api"
kfakeexternal "k8s.io/kubernetes/pkg/client/clientset_generated/clientset/fake"
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
kinformers "k8s.io/kubernetes/pkg/client/informers/informers_generated/internalversion"
buildapi "github.com/openshift/origin/pkg/build/api"
buildclient "github.com/openshift/origin/pkg/build/client"
"github.com/openshift/origin/pkg/build/controller/common"
"github.com/openshift/origin/pkg/client/testclient"
)
type errBuildUpdater struct{}
func (ec *errBuildUpdater) Update(namespace string, build *buildapi.Build) error {
return errors.New("UpdateBuild error!")
}
type customBuildUpdater struct {
UpdateFunc func(namespace string, build *buildapi.Build) error
}
func (c *customBuildUpdater) Update(namespace string, build *buildapi.Build) error {
return c.UpdateFunc(namespace, build)
}
func mockPod(status kapi.PodPhase, exitCode int, startTime *metav1.Time) *kapi.Pod {
return &kapi.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "data-build-build",
Namespace: "namespace",
Annotations: map[string]string{
buildapi.BuildAnnotation: "data-build",
},
},
Status: kapi.PodStatus{
Phase: status,
ContainerStatuses: []kapi.ContainerStatus{
{
State: kapi.ContainerState{
Terminated: &kapi.ContainerStateTerminated{ExitCode: int32(exitCode)},
},
},
},
StartTime: startTime,
},
}
}
func mockBuild(phase buildapi.BuildPhase, output buildapi.BuildOutput) *buildapi.Build {
return &buildapi.Build{
ObjectMeta: metav1.ObjectMeta{
Name: "data-build",
Namespace: "namespace",
Annotations: map[string]string{
buildapi.BuildConfigAnnotation: "test-bc",
},
Labels: map[string]string{
"name": "dataBuild",
// TODO: Switch this test to use Serial policy
buildapi.BuildRunPolicyLabel: string(buildapi.BuildRunPolicyParallel),
buildapi.BuildConfigLabel: "test-bc",
},
},
Spec: buildapi.BuildSpec{
CommonSpec: buildapi.CommonSpec{
Source: buildapi.BuildSource{
Git: &buildapi.GitBuildSource{
URI: "http://my.build.com/the/build/Dockerfile",
},
ContextDir: "contextimage",
},
Strategy: buildapi.BuildStrategy{
DockerStrategy: &buildapi.DockerBuildStrategy{},
},
Output: output,
},
},
Status: buildapi.BuildStatus{
Phase: phase,
},
}
}
type FakeIndexer struct {
*cache.FakeCustomStore
}
func mockBuildPodController(build *buildapi.Build, buildUpdater buildclient.BuildUpdater) *BuildPodController {
buildInformer := cache.NewSharedIndexInformer(&cache.ListWatch{}, &buildapi.Build{}, 2*time.Minute, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
kubeInformerFactory := kinformers.NewSharedInformerFactory(&fake.Clientset{}, 0)
podInformer := kubeInformerFactory.Core().InternalVersion().Pods()
fakeSecret := &kapi.Secret{}
fakeSecret.Name = "fakeSecret"
fakeSecret.Namespace = "namespace"
kclient := fake.NewSimpleClientset(fakeSecret)
osclient := testclient.NewSimpleFake()
c := NewBuildPodController(buildInformer, podInformer, kclient, kfakeexternal.NewSimpleClientset(), osclient)
if build != nil {
c.buildStore.Indexer.Add(build)
}
if buildUpdater != nil {
c.buildUpdater = buildUpdater
}
return c
}
func TestHandlePod(t *testing.T) {
type handlePodTest struct {
matchID bool
inStatus buildapi.BuildPhase
outStatus buildapi.BuildPhase
startTimestamp *metav1.Time
completionTimestamp *metav1.Time
podStatus kapi.PodPhase
exitCode int
buildUpdater buildclient.BuildUpdater
}
dummy := metav1.Now()
curtime := &dummy
tests := []handlePodTest{
{ // 0
matchID: false,
inStatus: buildapi.BuildPhasePending,
outStatus: buildapi.BuildPhasePending,
podStatus: kapi.PodPending,
exitCode: 0,
startTimestamp: nil,
completionTimestamp: nil,
},
{ // 1
matchID: true,
inStatus: buildapi.BuildPhasePending,
outStatus: buildapi.BuildPhasePending,
podStatus: kapi.PodPending,
exitCode: 0,
startTimestamp: nil,
completionTimestamp: nil,
},
{ // 2
matchID: true,
inStatus: buildapi.BuildPhasePending,
outStatus: buildapi.BuildPhaseRunning,
podStatus: kapi.PodRunning,
exitCode: 0,
startTimestamp: curtime,
completionTimestamp: nil,
},
{ // 3
matchID: true,
inStatus: buildapi.BuildPhaseRunning,
outStatus: buildapi.BuildPhaseComplete,
podStatus: kapi.PodSucceeded,
exitCode: 0,
startTimestamp: curtime,
completionTimestamp: curtime,
},
{ // 4
matchID: true,
inStatus: buildapi.BuildPhaseRunning,
outStatus: buildapi.BuildPhaseFailed,
podStatus: kapi.PodFailed,
exitCode: -1,
startTimestamp: curtime,
completionTimestamp: curtime,
},
{ // 5
matchID: true,
inStatus: buildapi.BuildPhaseRunning,
outStatus: buildapi.BuildPhaseComplete,
podStatus: kapi.PodSucceeded,
exitCode: 0,
buildUpdater: &errBuildUpdater{},
startTimestamp: curtime,
completionTimestamp: curtime,
},
{ // 6
matchID: true,
inStatus: buildapi.BuildPhaseCancelled,
outStatus: buildapi.BuildPhaseCancelled,
podStatus: kapi.PodFailed,
exitCode: 0,
startTimestamp: nil,
completionTimestamp: nil,
},
}
for i, tc := range tests {
build := mockBuild(tc.inStatus, buildapi.BuildOutput{})
// Default build updater to retrieve updated build
if tc.buildUpdater == nil {
tc.buildUpdater = &customBuildUpdater{
UpdateFunc: func(namespace string, updatedBuild *buildapi.Build) error {
build = updatedBuild
return nil
},
}
}
ctrl := mockBuildPodController(build, tc.buildUpdater)
pod := mockPod(tc.podStatus, tc.exitCode, curtime)
if tc.matchID {
build.Name = "name"
}
err := ctrl.HandlePod(pod)
if tc.buildUpdater != nil && reflect.TypeOf(tc.buildUpdater).Elem().Name() == "errBuildUpdater" {
if err == nil {
t.Errorf("(%d) Expected error, got none", i)
}
// can't check tc.outStatus because the local build object does get updated
// in this test (but would not updated in etcd)
continue
}
if build.Status.Phase != tc.outStatus {
t.Errorf("(%d) Expected %s, got %s!", i, tc.outStatus, build.Status.Phase)
}
if tc.inStatus != buildapi.BuildPhaseCancelled && tc.inStatus != buildapi.BuildPhaseComplete && !common.HasBuildPodNameAnnotation(build) {
t.Errorf("(%d) Build does not have pod name annotation. %#v", i, build)
}
if tc.startTimestamp == nil && build.Status.StartTimestamp != nil {
t.Errorf("(%d) Expected nil start timestamp, got %v!", i, build.Status.StartTimestamp)
}
if tc.startTimestamp != nil && build.Status.StartTimestamp == nil {
t.Errorf("(%d) nil start timestamp!", i)
}
if tc.startTimestamp != nil && !tc.startTimestamp.Before(*build.Status.StartTimestamp) && tc.startTimestamp.Time != build.Status.StartTimestamp.Time {
t.Errorf("(%d) Expected build start timestamp %v to be equal to or later than %v!", i, build.Status.StartTimestamp, tc.startTimestamp)
}
if tc.completionTimestamp == nil && build.Status.CompletionTimestamp != nil {
t.Errorf("(%d) Expected nil completion timestamp, got %v!", i, build.Status.CompletionTimestamp)
}
if tc.completionTimestamp != nil && build.Status.CompletionTimestamp == nil {
t.Errorf("(%d) nil completion timestamp!", i)
}
if tc.completionTimestamp != nil && !tc.completionTimestamp.Before(*build.Status.CompletionTimestamp) && tc.completionTimestamp.Time != build.Status.CompletionTimestamp.Time {
t.Errorf("(%d) Expected build completion timestamp %v to be equal to or later than %v!", i, build.Status.CompletionTimestamp, tc.completionTimestamp)
}
}
}
func TestHandleBuildPodDeletionOK(t *testing.T) {
updateWasCalled := false
// only not finished build (buildutil.IsBuildComplete) should be handled
build := mockBuild(buildapi.BuildPhaseRunning, buildapi.BuildOutput{})
ctrl := mockBuildPodController(build, &customBuildUpdater{
UpdateFunc: func(namespace string, build *buildapi.Build) error {
updateWasCalled = true
return nil
},
})
pod := mockPod(kapi.PodSucceeded, 0, nil)
err := ctrl.HandleBuildPodDeletion(pod)
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if !updateWasCalled {
t.Error("UpdateBuild was not called when it should have been!")
}
}
func TestHandlePipelineBuildPodDeletionOK(t *testing.T) {
updateWasCalled := false
// only not finished build (buildutil.IsBuildComplete) should be handled
build := mockBuild(buildapi.BuildPhaseRunning, buildapi.BuildOutput{})
build.Spec.Strategy.JenkinsPipelineStrategy = &buildapi.JenkinsPipelineBuildStrategy{}
ctrl := mockBuildPodController(build, &customBuildUpdater{
UpdateFunc: func(namespace string, build *buildapi.Build) error {
updateWasCalled = true
return nil
},
})
pod := mockPod(kapi.PodSucceeded, 0, nil)
err := ctrl.HandleBuildPodDeletion(pod)
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if updateWasCalled {
t.Error("UpdateBuild called when it should not have been!")
}
}
func TestHandleBuildPodDeletionOKFinishedBuild(t *testing.T) {
updateWasCalled := false
// finished build buildutil.IsBuildComplete should not be handled
build := mockBuild(buildapi.BuildPhaseComplete, buildapi.BuildOutput{})
ctrl := mockBuildPodController(build, &customBuildUpdater{
UpdateFunc: func(namespace string, build *buildapi.Build) error {
updateWasCalled = true
return nil
},
})
pod := mockPod(kapi.PodSucceeded, 0, nil)
err := ctrl.HandleBuildPodDeletion(pod)
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if updateWasCalled {
t.Error("UpdateBuild was called when it should not!")
}
}
func TestHandleBuildPodDeletionOKErroneousBuild(t *testing.T) {
updateWasCalled := false
// erroneous builds should not be handled
build := mockBuild(buildapi.BuildPhaseError, buildapi.BuildOutput{})
ctrl := mockBuildPodController(build, &customBuildUpdater{
UpdateFunc: func(namespace string, build *buildapi.Build) error {
updateWasCalled = true
return nil
},
})
pod := mockPod(kapi.PodSucceeded, 0, nil)
err := ctrl.HandleBuildPodDeletion(pod)
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if updateWasCalled {
t.Error("UpdateBuild was called when it should not!")
}
}
type fakeIndexer struct {
*cache.FakeCustomStore
}
func (*fakeIndexer) Index(indexName string, obj interface{}) ([]interface{}, error) { return nil, nil }
func (*fakeIndexer) IndexKeys(indexName, indexKey string) ([]string, error) { return nil, nil }
func (*fakeIndexer) ListIndexFuncValues(indexName string) []string { return nil }
func (*fakeIndexer) ByIndex(indexName, indexKey string) ([]interface{}, error) { return nil, nil }
func (*fakeIndexer) GetIndexers() cache.Indexers { return nil }
func (*fakeIndexer) AddIndexers(newIndexers cache.Indexers) error { return nil }
func newErrIndexer(err error) cache.Indexer {
return &fakeIndexer{
&cache.FakeCustomStore{
GetByKeyFunc: func(key string) (interface{}, bool, error) {
return nil, true, err
},
},
}
}
func TestHandleBuildPodDeletionBuildGetError(t *testing.T) {
ctrl := mockBuildPodController(nil, &customBuildUpdater{})
ctrl.buildStore.Indexer = newErrIndexer(errors.New("random"))
pod := mockPod(kapi.PodSucceeded, 0, nil)
err := ctrl.HandleBuildPodDeletion(pod)
if err == nil {
t.Error("Expected random error, but got none!")
}
if err != nil && err.Error() != "random" {
t.Errorf("Expected random error, got: %v", err)
}
}
func TestHandleBuildPodDeletionBuildNotExists(t *testing.T) {
updateWasCalled := false
ctrl := mockBuildPodController(nil, &customBuildUpdater{
UpdateFunc: func(namespace string, build *buildapi.Build) error {
updateWasCalled = true
return nil
},
})
pod := mockPod(kapi.PodSucceeded, 0, nil)
err := ctrl.HandleBuildPodDeletion(pod)
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if updateWasCalled {
t.Error("UpdateBuild was called when it should not!")
}
}
func TestHandleBuildPodDeletionBuildUpdateError(t *testing.T) {
build := mockBuild(buildapi.BuildPhaseRunning, buildapi.BuildOutput{})
ctrl := mockBuildPodController(build, &customBuildUpdater{
UpdateFunc: func(namespace string, build *buildapi.Build) error {
return errors.New("random")
},
})
pod := mockPod(kapi.PodSucceeded, 0, nil)
err := ctrl.HandleBuildPodDeletion(pod)
if err == nil {
t.Error("Expected random error, but got none!")
}
}