forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
327 lines (275 loc) · 11.1 KB
/
util.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
/*
Copyright 2017 The Kubernetes Authors.
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 util
import (
"fmt"
"testing"
"time"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1"
v1beta1servicecatalog "github.com/kubernetes-incubator/service-catalog/pkg/client/clientset_generated/clientset/typed/servicecatalog/v1beta1"
)
// WaitForBrokerCondition waits for the status of the named broker to contain
// a condition whose type and status matches the supplied one.
func WaitForBrokerCondition(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, name string, condition v1beta1.ServiceBrokerCondition) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for broker %v condition %#v", name, condition)
broker, err := client.ClusterServiceBrokers().Get(name, metav1.GetOptions{})
if nil != err {
return false, fmt.Errorf("error getting Broker %v: %v", name, err)
}
if len(broker.Status.Conditions) == 0 {
return false, nil
}
for _, cond := range broker.Status.Conditions {
if condition.Type == cond.Type && condition.Status == cond.Status {
return true, nil
}
}
return false, nil
},
)
}
// WaitForBrokerToNotExist waits for the Broker with the given name to no
// longer exist.
func WaitForBrokerToNotExist(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, name string) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for broker %v to not exist", name)
_, err := client.ClusterServiceBrokers().Get(name, metav1.GetOptions{})
if nil == err {
return false, nil
}
if errors.IsNotFound(err) {
return true, nil
}
return false, nil
},
)
}
// WaitForClusterServiceClassToExist waits for the ClusterServiceClass with the given name
// to exist.
func WaitForClusterServiceClassToExist(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, name string) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for serviceClass %v to exist", name)
_, err := client.ClusterServiceClasses().Get(name, metav1.GetOptions{})
if nil == err {
return true, nil
}
return false, nil
},
)
}
// WaitForClusterServiceClassToExist waits for the ClusterServiceClass with the given name
// to exist.
func WaitForClusterServicePlanToExist(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, name string) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for ClusterServicePlan %v to exist", name)
_, err := client.ClusterServicePlans().Get(name, metav1.GetOptions{})
if nil == err {
return true, nil
}
return false, nil
},
)
}
// WaitForClusterServicePlanToNotExist waits for the ClusterServicePlan with the given name
// to not exist.
func WaitForClusterServicePlanToNotExist(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, name string) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for ClusterServicePlan %q to not exist", name)
_, err := client.ClusterServicePlans().Get(name, metav1.GetOptions{})
if nil == err {
return false, nil
}
if errors.IsNotFound(err) {
return true, nil
}
return false, nil
},
)
}
// WaitForClusterServiceClassToNotExist waits for the ClusterServiceClass with the given
// name to no longer exist.
func WaitForClusterServiceClassToNotExist(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, name string) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for serviceClass %v to not exist", name)
_, err := client.ClusterServiceClasses().Get(name, metav1.GetOptions{})
if nil == err {
return false, nil
}
if errors.IsNotFound(err) {
return true, nil
}
return false, nil
},
)
}
// WaitForInstanceCondition waits for the status of the named instance to
// contain a condition whose type and status matches the supplied one.
func WaitForInstanceCondition(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, namespace, name string, condition v1beta1.ServiceInstanceCondition) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for instance %v/%v condition %#v", namespace, name, condition)
instance, err := client.ServiceInstances(namespace).Get(name, metav1.GetOptions{})
if nil != err {
return false, fmt.Errorf("error getting Instance %v/%v: %v", namespace, name, err)
}
if len(instance.Status.Conditions) == 0 {
return false, nil
}
for _, cond := range instance.Status.Conditions {
if condition.Type == cond.Type && condition.Status == cond.Status {
return true, nil
}
}
return false, nil
},
)
}
// WaitForInstanceToNotExist waits for the Instance with the given name to no
// longer exist.
func WaitForInstanceToNotExist(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, namespace, name string) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for instance %v/%v to not exist", namespace, name)
_, err := client.ServiceInstances(namespace).Get(name, metav1.GetOptions{})
if nil == err {
return false, nil
}
if errors.IsNotFound(err) {
return true, nil
}
return false, nil
},
)
}
// WaitForInstanceReconciledGeneration waits for the status of the named instance to
// have the specified reconciled generation.
func WaitForInstanceReconciledGeneration(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, namespace, name string, reconciledGeneration int64) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for instance %v/%v to have reconciled generation of %v", namespace, name, reconciledGeneration)
instance, err := client.ServiceInstances(namespace).Get(name, metav1.GetOptions{})
if nil != err {
return false, fmt.Errorf("error getting Instance %v/%v: %v", namespace, name, err)
}
if instance.Status.ReconciledGeneration == reconciledGeneration {
return true, nil
}
return false, nil
},
)
}
// WaitForBindingCondition waits for the status of the named binding to
// contain a condition whose type and status matches the supplied one.
func WaitForBindingCondition(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, namespace, name string, condition v1beta1.ServiceBindingCondition) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for binding %v/%v condition %#v", namespace, name, condition)
binding, err := client.ServiceBindings(namespace).Get(name, metav1.GetOptions{})
if nil != err {
return false, fmt.Errorf("error getting Binding %v/%v: %v", namespace, name, err)
}
if len(binding.Status.Conditions) == 0 {
return false, nil
}
for _, cond := range binding.Status.Conditions {
if condition.Type == cond.Type && condition.Status == cond.Status {
return true, nil
}
}
return false, nil
},
)
}
// WaitForBindingToNotExist waits for the Binding with the given name to no
// longer exist.
func WaitForBindingToNotExist(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, namespace, name string) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for binding %v/%v to not exist", namespace, name)
_, err := client.ServiceBindings(namespace).Get(name, metav1.GetOptions{})
if nil == err {
return false, nil
}
if errors.IsNotFound(err) {
return true, nil
}
return false, nil
},
)
}
// WaitForBindingReconciledGeneration waits for the status of the named binding to
// have the specified reconciled generation.
func WaitForBindingReconciledGeneration(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, namespace, name string, reconciledGeneration int64) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for binding %v/%v to have reconciled generation of %v", namespace, name, reconciledGeneration)
binding, err := client.ServiceBindings(namespace).Get(name, metav1.GetOptions{})
if nil != err {
return false, fmt.Errorf("error getting ServiceBinding %v/%v: %v", namespace, name, err)
}
if binding.Status.ReconciledGeneration == reconciledGeneration {
return true, nil
}
return false, nil
},
)
}
// AssertServiceInstanceCondition asserts that the instance's status contains
// the given condition type, status, and reason.
func AssertServiceInstanceCondition(t *testing.T, instance *v1beta1.ServiceInstance, conditionType v1beta1.ServiceInstanceConditionType, status v1beta1.ConditionStatus, reason ...string) {
foundCondition := false
for _, condition := range instance.Status.Conditions {
if condition.Type == conditionType {
foundCondition = true
if condition.Status != status {
t.Fatalf("%v condition had unexpected status; expected %v, got %v", conditionType, status, condition.Status)
}
if len(reason) == 1 && condition.Reason != reason[0] {
t.Fatalf("unexpected reason; expected %v, got %v", reason[0], condition.Reason)
}
}
}
if !foundCondition {
t.Fatalf("%v condition not found", conditionType)
}
}
// AssertServiceBindingCondition asserts that the binding's status contains
// the given condition type, status, and reason.
func AssertServiceBindingCondition(t *testing.T, binding *v1beta1.ServiceBinding, conditionType v1beta1.ServiceBindingConditionType, status v1beta1.ConditionStatus, reason ...string) {
foundCondition := false
for _, condition := range binding.Status.Conditions {
if condition.Type == conditionType {
foundCondition = true
if condition.Status != status {
t.Fatalf("%v condition had unexpected status; expected %v, got %v", conditionType, status, condition.Status)
}
if len(reason) == 1 && condition.Reason != reason[0] {
t.Fatalf("unexpected reason; expected %v, got %v", reason[0], condition.Reason)
}
}
}
if !foundCondition {
t.Fatalf("%v condition not found", conditionType)
}
}