-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler_test.go
134 lines (111 loc) · 3.78 KB
/
handler_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
/*
Copyright 2022.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package reconciler_test
import (
"context"
"errors"
"fmt"
"testing"
"time"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logr "sigs.k8s.io/controller-runtime/pkg/log"
reconciler "github.com/angelokurtis/reconciler/v2"
)
func TestOperations(t *testing.T) {
t.Run("should throw error when an operation fails", func(t *testing.T) {
r := reconciler.Chain[client.Object](
&withoutError{},
&withError{},
&withRequeueIn2min{},
)
_, err := r.Reconcile(context.TODO(), nil)
if err == nil {
t.Errorf("expect error, got %+v", err)
}
})
t.Run("should requeue in 2m when an operation have this result", func(t *testing.T) {
r := reconciler.Chain[client.Object](
&withRequeueIn2min{},
&withoutError{},
)
result, err := r.Reconcile(context.TODO(), nil)
if err != nil || result.RequeueAfter != 2*time.Minute {
t.Errorf("expect requeue in 2min, got result=%+v err=%+v", result, err)
}
})
t.Run("should requeue now when an operation have this result", func(t *testing.T) {
r := reconciler.Chain[client.Object](
&withRequeue{},
&withoutError{},
)
result, err := r.Reconcile(context.TODO(), nil)
if err != nil || !result.Requeue {
t.Errorf("expect requeue true, got result=%+v err=%+v", result, err)
}
})
t.Run("should finish all operations when none requeue or fail", func(t *testing.T) {
r := reconciler.Chain[client.Object](
&withoutError{},
&withoutError{},
)
result, err := r.Reconcile(context.TODO(), nil)
if err != nil || result.Requeue || result.RequeueAfter > 0 {
t.Errorf("expect empty result, got result=%+v err=%+v", result, err)
}
})
t.Run("should throw error and requeue in 5m when an operation fails with this result", func(t *testing.T) {
r := reconciler.Chain[client.Object](
&withoutError{},
&withRequeueError{},
)
result, err := r.Reconcile(context.TODO(), nil)
if err == nil || result.RequeueAfter != 5*time.Minute {
t.Errorf("expect error and requeue in 5min, got result=%+v err=%+v", result, err)
}
})
}
type withoutError struct {
reconciler.Funcs[client.Object]
}
func (w *withoutError) Reconcile(ctx context.Context, obj client.Object) (ctrl.Result, error) {
logr.FromContext(ctx).V(0).Info(fmt.Sprintf("%T", w))
return w.Next(ctx, obj)
}
type withError struct {
reconciler.Funcs[client.Object]
}
func (w *withError) Reconcile(ctx context.Context, obj client.Object) (ctrl.Result, error) {
return w.RequeueOnErr(ctx, errors.New("reconcile with error"))
}
type withRequeue struct {
reconciler.Funcs[client.Object]
}
func (w *withRequeue) Reconcile(ctx context.Context, obj client.Object) (ctrl.Result, error) {
return w.Requeue(ctx)
}
type withRequeueIn2min struct {
reconciler.Funcs[client.Object]
}
func (w *withRequeueIn2min) Reconcile(ctx context.Context, obj client.Object) (ctrl.Result, error) {
return w.RequeueAfter(ctx, 2*time.Minute)
}
type withRequeueError struct {
reconciler.Funcs[client.Object]
}
func (w *withRequeueError) Reconcile(ctx context.Context, obj client.Object) (ctrl.Result, error) {
err := apierrors.NewAlreadyExists(schema.GroupResource{Resource: "foos"}, "bar")
return w.RequeueOnErrAfter(ctx, err, 5*time.Minute)
}