-
Notifications
You must be signed in to change notification settings - Fork 0
/
features_get_fields.go
317 lines (290 loc) · 11.2 KB
/
features_get_fields.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
package kubernetes_ctx
import (
"fmt"
"github.com/stretchr/objx"
"github.com/xunleii/godog-kubernetes/helpers"
)
// ResourceHasField implements the GoDoc step
// - `Kubernetes resource <ApiGroupVersionKind> '<NamespacedName>' has '<FieldPath>'`
// It validates the fact that the specific resource has the field.
func ResourceHasField(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes resource (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' has '(`+RxFieldPath+`)'$`,
func(groupVersionKindStr, name, field string) (err error) {
_, exists, err := getResourceField(ctx, groupVersionKindStr, name, field)
switch {
case err != nil:
return err
case !exists:
return fmt.Errorf("field '%s' not found", field)
}
return nil
},
)
}
// ResourceDoesntHaveField implements the GoDoc step
// - `Kubernetes resource <ApiGroupVersionKind> '<NamespacedName>' doesn't have '<FieldPath>'`
// It validates the fact that the specific resource doesn't have the field.
func ResourceDoesntHaveField(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes resource (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' doesn't have '(`+RxFieldPath+`)'$`,
func(groupVersionKindStr, name, field string) (err error) {
_, exists, err := getResourceField(ctx, groupVersionKindStr, name, field)
switch {
case err != nil:
return err
case exists:
return fmt.Errorf("field '%s' found", field)
}
return nil
},
)
}
// ResourceHasFieldEqual implements the GoDoc step
// - `Kubernetes resource <ApiGroupVersionKind> '<NamespacedName>' has '<FieldPath>=<FieldValue>'`
// It validates the fact that the specific resource field has the given value.
func ResourceHasFieldEqual(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes resource (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' has '(`+RxFieldPath+`)=(.*)'$`,
func(groupVersionKindStr, name, field, value string) (err error) {
rval, exists, err := getResourceField(ctx, groupVersionKindStr, name, field)
switch {
case err != nil:
return err
case !exists:
return fmt.Errorf("field '%s' not found", field)
case rval != value:
return fmt.Errorf("field '%s' not equal to %s (current: %s)", field, value, rval)
}
return nil
},
)
}
// ResourceHasFieldNotEqual implements the GoDoc step
// - `Kubernetes resource <ApiGroupVersionKind> '<NamespacedName>' has '<FieldPath>!=<FieldValue>'`
// It validates the fact that the specific resource field is different than the given value.
func ResourceHasFieldNotEqual(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes resource (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' doesn't have '(`+RxFieldPath+`)=(.*)'$`,
func(groupVersionKindStr, name, field, value string) (err error) {
rval, exists, err := getResourceField(ctx, groupVersionKindStr, name, field)
switch {
case err != nil:
return err
case !exists:
return nil
case rval == value:
return fmt.Errorf("field '%s' equal to %s", field, value)
}
return nil
},
)
}
// getResourceField returns the resource field value and if it exists.
func getResourceField(ctx *FeatureContext, groupVersionKindStr, name, field string) (string, bool, error) {
groupVersionKind, err := helpers.GroupVersionKindFrom(groupVersionKindStr)
if err != nil {
return "", false, err
}
namespacedName, _ := helpers.NamespacedNameFrom(name)
obj, err := ctx.Get(groupVersionKind, namespacedName)
if err != nil {
return "", false, err
}
obj.SetGroupVersionKind(groupVersionKind)
obj.SetNamespace(namespacedName.Namespace)
obj.SetName(namespacedName.Name)
xmap := objx.Map(obj.Object)
if xmap.Has(field) {
return xmap.Get(field).String(), true, nil
}
return "", false, nil
}
// ResourceHasLabel implements the GoDoc step
// - `Kubernetes resource <ApiGroupVersionKind> '<NamespacedName>' has label '<LabelName>'`
// It validates the fact that the specific resource has the given label.
func ResourceHasLabel(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes resource (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' has label '(`+RxFieldPath+`)'$`,
func(groupVersionKindStr, name, label string) (err error) {
_, exists, err := getResourceLabel(ctx, groupVersionKindStr, name, label)
switch {
case err != nil:
return err
case !exists:
return fmt.Errorf("label '%s' not found", label)
}
return nil
},
)
}
// ResourceDoesntHaveLabel implements the GoDoc step
// - `Kubernetes resource <ApiGroupVersionKind> '<NamespacedName>' doesn't have label '<LabelName>'`
// It validates the fact that the specific resource doesn't have the given label.
func ResourceDoesntHaveLabel(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes resource (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' doesn't have label '(`+RxFieldPath+`)'$`,
func(groupVersionKindStr, name, label string) (err error) {
_, exists, err := getResourceLabel(ctx, groupVersionKindStr, name, label)
switch {
case err != nil && err.Error() != "no label found":
return err
case err == nil && exists:
return fmt.Errorf("label '%s' found", label)
}
return nil
},
)
}
// ResourceHasLabelEqual implements the GoDoc step
// - `Kubernetes resource <ApiGroupVersionKind> '<NamespacedName>' has label '<LabelName>=<LabelValue>'`
// It validates the fact that the specific resource label has the given value.
func ResourceHasLabelEqual(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes resource (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' has label '(`+RxFieldPath+`)=(.*)'$`,
func(groupVersionKindStr, name, label, value string) (err error) {
rval, exists, err := getResourceLabel(ctx, groupVersionKindStr, name, label)
switch {
case err != nil:
return err
case !exists:
return fmt.Errorf("label '%s' not found", label)
case rval != value:
return fmt.Errorf("label '%s' not equal to %s (current: %s)", label, value, rval)
}
return nil
},
)
}
// ResourceHasLabelNotEqual implements the GoDoc step
// - `Kubernetes resource <ApiGroupVersionKind> '<NamespacedName>' has label '<LabelName>!=<LabelValue>'`
// It validates the fact that the specific resource label doesn't have the given value.
func ResourceHasLabelNotEqual(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes resource (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' doesn't have label '(`+RxFieldPath+`)=(.*)'$`,
func(groupVersionKindStr, name, label, value string) (err error) {
rval, exists, err := getResourceLabel(ctx, groupVersionKindStr, name, label)
switch {
case err != nil:
return err
case !exists:
return nil
case rval == value:
return fmt.Errorf("label '%s' equal to %s", label, value)
}
return nil
},
)
}
// getResourceLabel returns the resource label value and if it exists.
func getResourceLabel(ctx *FeatureContext, groupVersionKindStr, name, label string) (string, bool, error) {
groupVersionKind, err := helpers.GroupVersionKindFrom(groupVersionKindStr)
if err != nil {
return "", false, err
}
namespacedName, _ := helpers.NamespacedNameFrom(name)
obj, err := ctx.Get(groupVersionKind, namespacedName)
if err != nil {
return "", false, err
}
labels := obj.GetLabels()
if labels == nil {
return "", false, fmt.Errorf("no label found")
}
value, exists := labels[label]
return value, exists, nil
}
// ResourceHasAnnotation implements the GoDoc step
// - `Kubernetes resource <ApiGroupVersionKind> '<NamespacedName>' has annotation '<AnnotationName>'`
// It validates the fact that the specific resource has the given annotation.
func ResourceHasAnnotation(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes resource (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' has annotation '(`+RxFieldPath+`)'$`,
func(groupVersionKindStr, name, annotation string) (err error) {
_, exists, err := getResourceAnnotation(ctx, groupVersionKindStr, name, annotation)
switch {
case err != nil:
return err
case !exists:
return fmt.Errorf("annotation '%s' not found", annotation)
}
return nil
},
)
}
// ResourceDoesntHaveAnnotation implements the GoDoc step
// - `Kubernetes resource <ApiGroupVersionKind> '<NamespacedName>' doesn't have annotation '<AnnotationName>'`
// It validates the fact that the specific resource doesn't have the given annotation.
func ResourceDoesntHaveAnnotation(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes resource (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' doesn't have annotation '(`+RxFieldPath+`)'$`,
func(groupVersionKindStr, name, annotation string) (err error) {
_, exists, err := getResourceAnnotation(ctx, groupVersionKindStr, name, annotation)
switch {
case err != nil && err.Error() != "no annotation found":
return err
case err == nil && exists:
return fmt.Errorf("annotation '%s' found", annotation)
}
return nil
},
)
}
// ResourceHasAnnotationEqual implements the GoDoc step
// - `Kubernetes resource <ApiGroupVersionKind> '<NamespacedName>' has annotation '<AnnotationName>=<AnnotationValue>'`
// It validates the fact that the specific resource annotation has the given value.
func ResourceHasAnnotationEqual(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes resource (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' has annotation '(`+RxFieldPath+`)=(.*)'$`,
func(groupVersionKindStr, name, annotation, value string) (err error) {
rval, exists, err := getResourceAnnotation(ctx, groupVersionKindStr, name, annotation)
switch {
case err != nil:
return err
case !exists:
return fmt.Errorf("annotation '%s' not found", annotation)
case rval != value:
return fmt.Errorf("annotation '%s' not equal to %s (current: %s)", annotation, value, rval)
}
return nil
},
)
}
// ResourceHasAnnotationNotEqual implements the GoDoc step
// - `Kubernetes resource <ApiGroupVersionKind> '<NamespacedName>' has annotation '<AnnotationName>!=<AnnotationValue>'`
// It validates the fact that the specific resource annotation doesn't have the given value.
func ResourceHasAnnotationNotEqual(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes resource (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' doesn't have annotation '(`+RxFieldPath+`)=(.*)'$`,
func(groupVersionKindStr, name, annotation, value string) (err error) {
rval, exists, err := getResourceAnnotation(ctx, groupVersionKindStr, name, annotation)
switch {
case err != nil:
return err
case !exists:
return nil
case rval == value:
return fmt.Errorf("annotation '%s' equal to %s", annotation, value)
}
return nil
},
)
}
// getResourceAnnotation returns the resource annotation value and if it exists.
func getResourceAnnotation(ctx *FeatureContext, groupVersionKindStr, name, annotation string) (string, bool, error) {
groupVersionKind, err := helpers.GroupVersionKindFrom(groupVersionKindStr)
if err != nil {
return "", false, err
}
namespacedName, _ := helpers.NamespacedNameFrom(name)
obj, err := ctx.Get(groupVersionKind, namespacedName)
if err != nil {
return "", false, err
}
annotations := obj.GetAnnotations()
if annotations == nil {
return "", false, fmt.Errorf("no annotation found")
}
value, exists := annotations[annotation]
return value, exists, nil
}