forked from openshift/library-go
/
test_helpers.go
215 lines (183 loc) · 7.96 KB
/
test_helpers.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
package v1helpers
import (
"fmt"
"strconv"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
corev1listers "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
operatorv1 "github.com/openshift/api/operator/v1"
)
// NewFakeSharedIndexInformer returns a fake shared index informer, suitable to use in static pod controller unit tests.
func NewFakeSharedIndexInformer() cache.SharedIndexInformer {
return &fakeSharedIndexInformer{}
}
type fakeSharedIndexInformer struct{}
func (fakeSharedIndexInformer) AddEventHandler(handler cache.ResourceEventHandler) {
}
func (fakeSharedIndexInformer) AddEventHandlerWithResyncPeriod(handler cache.ResourceEventHandler, resyncPeriod time.Duration) {
}
func (fakeSharedIndexInformer) GetStore() cache.Store {
panic("implement me")
}
func (fakeSharedIndexInformer) GetController() cache.Controller {
panic("implement me")
}
func (fakeSharedIndexInformer) Run(stopCh <-chan struct{}) {
panic("implement me")
}
func (fakeSharedIndexInformer) HasSynced() bool {
panic("implement me")
}
func (fakeSharedIndexInformer) LastSyncResourceVersion() string {
panic("implement me")
}
func (fakeSharedIndexInformer) AddIndexers(indexers cache.Indexers) error {
panic("implement me")
}
func (fakeSharedIndexInformer) GetIndexer() cache.Indexer {
panic("implement me")
}
// NewFakeStaticPodOperatorClient returns a fake operator client suitable to use in static pod controller unit tests.
func NewFakeStaticPodOperatorClient(
staticPodSpec *operatorv1.StaticPodOperatorSpec, staticPodStatus *operatorv1.StaticPodOperatorStatus,
triggerStatusErr func(rv string, status *operatorv1.StaticPodOperatorStatus) error,
triggerSpecErr func(rv string, spec *operatorv1.StaticPodOperatorSpec) error) StaticPodOperatorClient {
return &fakeStaticPodOperatorClient{
fakeStaticPodOperatorSpec: staticPodSpec,
fakeStaticPodOperatorStatus: staticPodStatus,
resourceVersion: "0",
triggerStatusUpdateError: triggerStatusErr,
triggerSpecUpdateError: triggerSpecErr,
}
}
type fakeStaticPodOperatorClient struct {
fakeOperatorStatus *operatorv1.OperatorStatus
fakeStaticPodOperatorSpec *operatorv1.StaticPodOperatorSpec
fakeStaticPodOperatorStatus *operatorv1.StaticPodOperatorStatus
resourceVersion string
triggerStatusUpdateError func(rv string, status *operatorv1.StaticPodOperatorStatus) error
triggerSpecUpdateError func(rv string, status *operatorv1.StaticPodOperatorSpec) error
}
func (c *fakeStaticPodOperatorClient) Informer() cache.SharedIndexInformer {
return &fakeSharedIndexInformer{}
}
func (c *fakeStaticPodOperatorClient) GetStaticPodOperatorState() (*operatorv1.StaticPodOperatorSpec, *operatorv1.StaticPodOperatorStatus, string, error) {
return c.fakeStaticPodOperatorSpec, c.fakeStaticPodOperatorStatus, c.resourceVersion, nil
}
func (c *fakeStaticPodOperatorClient) GetStaticPodOperatorStateWithQuorum() (*operatorv1.StaticPodOperatorSpec, *operatorv1.StaticPodOperatorStatus, string, error) {
return c.fakeStaticPodOperatorSpec, c.fakeStaticPodOperatorStatus, c.resourceVersion, nil
}
func (c *fakeStaticPodOperatorClient) UpdateStaticPodOperatorStatus(resourceVersion string, status *operatorv1.StaticPodOperatorStatus) (*operatorv1.StaticPodOperatorStatus, error) {
if c.resourceVersion != resourceVersion {
return nil, errors.NewConflict(schema.GroupResource{Group: operatorv1.GroupName, Resource: "TestOperatorConfig"}, "instance", fmt.Errorf("invalid resourceVersion"))
}
rv, err := strconv.Atoi(resourceVersion)
if err != nil {
return nil, err
}
c.resourceVersion = strconv.Itoa(rv + 1)
if c.triggerStatusUpdateError != nil {
if err := c.triggerStatusUpdateError(resourceVersion, status); err != nil {
return nil, err
}
}
c.fakeStaticPodOperatorStatus = status
return c.fakeStaticPodOperatorStatus, nil
}
func (c *fakeStaticPodOperatorClient) UpdateStaticPodOperatorSpec(resourceVersion string, spec *operatorv1.StaticPodOperatorSpec) (*operatorv1.StaticPodOperatorSpec, string, error) {
if c.resourceVersion != resourceVersion {
return nil, "", errors.NewConflict(schema.GroupResource{Group: operatorv1.GroupName, Resource: "TestOperatorConfig"}, "instance", fmt.Errorf("invalid resourceVersion"))
}
rv, err := strconv.Atoi(resourceVersion)
if err != nil {
return nil, "", err
}
c.resourceVersion = strconv.Itoa(rv + 1)
if c.triggerSpecUpdateError != nil {
if err := c.triggerSpecUpdateError(resourceVersion, spec); err != nil {
return nil, "", err
}
}
c.fakeStaticPodOperatorSpec = spec
return c.fakeStaticPodOperatorSpec, c.resourceVersion, nil
}
func (c *fakeStaticPodOperatorClient) GetOperatorState() (*operatorv1.OperatorSpec, *operatorv1.OperatorStatus, string, error) {
return &c.fakeStaticPodOperatorSpec.OperatorSpec, &c.fakeStaticPodOperatorStatus.OperatorStatus, "", nil
}
func (c *fakeStaticPodOperatorClient) UpdateOperatorSpec(string, *operatorv1.OperatorSpec) (spec *operatorv1.OperatorSpec, resourceVersion string, err error) {
panic("not supported")
}
func (c *fakeStaticPodOperatorClient) UpdateOperatorStatus(string, *operatorv1.OperatorStatus) (status *operatorv1.OperatorStatus, err error) {
panic("not supported")
}
// NewFakeNodeLister returns a fake node lister suitable to use in node controller unit test
func NewFakeNodeLister(client kubernetes.Interface) corev1listers.NodeLister {
return &fakeNodeLister{client: client}
}
type fakeNodeLister struct {
client kubernetes.Interface
}
func (n *fakeNodeLister) List(selector labels.Selector) ([]*corev1.Node, error) {
nodes, err := n.client.CoreV1().Nodes().List(metav1.ListOptions{LabelSelector: selector.String()})
if err != nil {
return nil, err
}
ret := []*corev1.Node{}
for i := range nodes.Items {
ret = append(ret, &nodes.Items[i])
}
return ret, nil
}
func (n *fakeNodeLister) Get(name string) (*corev1.Node, error) {
panic("implement me")
}
func (n *fakeNodeLister) ListWithPredicate(predicate corev1listers.NodeConditionPredicate) ([]*corev1.Node, error) {
panic("implement me")
}
// NewFakeOperatorClient returns a fake operator client suitable to use in static pod controller unit tests.
func NewFakeOperatorClient(spec *operatorv1.OperatorSpec, status *operatorv1.OperatorStatus, triggerErr func(rv string, status *operatorv1.OperatorStatus) error) OperatorClient {
return &fakeOperatorClient{
fakeOperatorSpec: spec,
fakeOperatorStatus: status,
resourceVersion: "0",
triggerStatusUpdateError: triggerErr,
}
}
type fakeOperatorClient struct {
fakeOperatorSpec *operatorv1.OperatorSpec
fakeOperatorStatus *operatorv1.OperatorStatus
resourceVersion string
triggerStatusUpdateError func(rv string, status *operatorv1.OperatorStatus) error
}
func (c *fakeOperatorClient) Informer() cache.SharedIndexInformer {
return &fakeSharedIndexInformer{}
}
func (c *fakeOperatorClient) GetOperatorState() (*operatorv1.OperatorSpec, *operatorv1.OperatorStatus, string, error) {
return c.fakeOperatorSpec, c.fakeOperatorStatus, c.resourceVersion, nil
}
func (c *fakeOperatorClient) UpdateOperatorStatus(resourceVersion string, status *operatorv1.OperatorStatus) (*operatorv1.OperatorStatus, error) {
if c.resourceVersion != resourceVersion {
return nil, errors.NewConflict(schema.GroupResource{Group: operatorv1.GroupName, Resource: "TestOperatorConfig"}, "instance", fmt.Errorf("invalid resourceVersion"))
}
rv, err := strconv.Atoi(resourceVersion)
if err != nil {
return nil, err
}
c.resourceVersion = strconv.Itoa(rv + 1)
if c.triggerStatusUpdateError != nil {
if err := c.triggerStatusUpdateError(resourceVersion, status); err != nil {
return nil, err
}
}
c.fakeOperatorStatus = status
return c.fakeOperatorStatus, nil
}
func (c *fakeOperatorClient) UpdateOperatorSpec(string, *operatorv1.OperatorSpec) (spec *operatorv1.OperatorSpec, resourceVersion string, err error) {
panic("not supported")
}