-
Notifications
You must be signed in to change notification settings - Fork 0
/
features_get.go
226 lines (197 loc) · 7.29 KB
/
features_get.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
package kubernetes_ctx
import (
"fmt"
"github.com/yudai/gojsondiff"
"github.com/yudai/gojsondiff/formatter"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/xunleii/godog-kubernetes/helpers"
)
// ResourceExists implements the GoDoc step
// - `Kubernetes has <ApiGroupVersionKind> '<NamespacedName>'`
// It validates the fact that Kubernetes has the specified resource.
func ResourceExists(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes has (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)'$`,
func(groupVersionKindStr, name string) error {
groupVersionKind, err := helpers.GroupVersionKindFrom(groupVersionKindStr)
if err != nil {
return err
}
namespacedName, _ := helpers.NamespacedNameFrom(name)
_, err = ctx.Get(groupVersionKind, namespacedName)
return err
},
)
}
// ResourceNotExists implements the GoDoc step
// - `Kubernetes doesn't have <ApiGroupVersionKind> '<NamespacedName>'`
// It validates the fact that Kubernetes doesn't have the specified resource.
func ResourceNotExists(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes doesn't have (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)'$`,
func(groupVersionKindStr, name string) error {
groupVersionKind, err := helpers.GroupVersionKindFrom(groupVersionKindStr)
if err != nil {
return err
}
namespacedName, _ := helpers.NamespacedNameFrom(name)
_, err = ctx.Get(groupVersionKind, namespacedName)
switch {
case errors.IsNotFound(err):
return nil
case err != nil:
return err
default:
return fmt.Errorf(`%s "%s" found`, groupVersionKindStr, name)
}
},
)
}
// ResourceIsSimilarTo implements the GoDoc step
// - `Kubernetes resource <ApiGroupVersionKind> '<NamespacedName>' is similar to '<NamespacedName>'`
// It compares two resources in order to determine if they are similar.
//
// NOTE: Two resources are similar if all fields except 'medatata' are the same.
func ResourceIsSimilarTo(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes resource (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' is similar to '(`+RxNamespacedName+`)'$`,
func(groupVersionKindStr, lname, rname string) (err error) {
lobj, err := getWithoutMetadata(ctx, groupVersionKindStr, lname)
if err != nil {
return err
}
robj, err := getWithoutMetadata(ctx, groupVersionKindStr, rname)
if err != nil {
return err
}
diff := diffObjects(lobj, robj)
switch {
case diff != "":
return fmt.Errorf("resources %s '%s' and '%s' are not similar: %s", groupVersionKindStr, lname, rname, diff)
}
return nil
},
)
}
// ResourceIsNotSimilarTo implements the GoDoc step
// - `Kubernetes resource <ApiGroupVersionKind> '<NamespacedName>' is not similar to '<NamespacedName>'`
// It compares two resources in order to determine if they are not similar.
//
// NOTE: Two resources are similar if all fields except 'medatata' are the same.
func ResourceIsNotSimilarTo(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes resource (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' is not similar to '(`+RxNamespacedName+`)'$`,
func(groupVersionKindStr, lname, rname string) (err error) {
lobj, err := getWithoutMetadata(ctx, groupVersionKindStr, lname)
if err != nil {
return err
}
robj, err := getWithoutMetadata(ctx, groupVersionKindStr, rname)
if err != nil {
return err
}
diff := gojsondiff.New().CompareObjects(lobj.Object, robj.Object)
if !diff.Modified() {
return fmt.Errorf("resources %s '%s' and '%s' are similar", groupVersionKindStr, lname, rname)
}
return nil
},
)
}
// getWithoutMetadata returns resource without metadata field.
func getWithoutMetadata(ctx *FeatureContext, groupVersionKindStr, name string) (*unstructured.Unstructured, error) {
groupVersionKind, err := helpers.GroupVersionKindFrom(groupVersionKindStr)
if err != nil {
return nil, err
}
namespacedName, _ := helpers.NamespacedNameFrom(name)
obj, err := ctx.Get(groupVersionKind, namespacedName)
if err != nil {
return nil, err
}
delete(obj.Object, "metadata")
return obj, nil
}
// ResourceIsEqualTo implements the GoDoc step
// - `Kubernetes resource <ApiGroupVersionKind> '<NamespacedName>' is equal to '<NamespacedName>'`
// It compares two resources in order to determine if they are equal.
//
// NOTE: Two resources are equal if all fields except unique fields ('metadata.name',
// 'metadata.namespace', 'metadata.uid' and 'metadata.resourceVersion') are the same.
func ResourceIsEqualTo(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes resource (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' is equal to '(`+RxNamespacedName+`)'$`,
func(groupVersionKindStr, lname, rname string) (err error) {
lobj, err := getWithoutUniqueFields(ctx, groupVersionKindStr, lname)
if err != nil {
return err
}
robj, err := getWithoutUniqueFields(ctx, groupVersionKindStr, rname)
if err != nil {
return err
}
diff := diffObjects(lobj, robj)
switch {
case diff != "":
return fmt.Errorf("resources %s '%s' and '%s' are not equal: %s", groupVersionKindStr, lname, rname, diff)
}
return nil
},
)
}
// ResourceIsNotEqualTo implements the GoDoc step
// - `Kubernetes resource <ApiGroupVersionKind> '<NamespacedName>' is not equal to '<NamespacedName>'`
// It compares two resources in order to determine if they are not equal.
//
// NOTE: Two resources are equal if all fields except unique fields ('metadata.name',
// 'metadata.namespace', 'metadata.uid' and 'metadata.resourceVersion') are the same.
func ResourceIsNotEqualTo(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes resource (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' is not equal to '(`+RxNamespacedName+`)'$`,
func(groupVersionKindStr, lname, rname string) (err error) {
lobj, err := getWithoutUniqueFields(ctx, groupVersionKindStr, lname)
if err != nil {
return err
}
robj, err := getWithoutUniqueFields(ctx, groupVersionKindStr, rname)
if err != nil {
return err
}
diff := gojsondiff.New().CompareObjects(lobj.Object, robj.Object)
if !diff.Modified() {
return fmt.Errorf("resources %s '%s' and '%s' are equal", groupVersionKindStr, lname, rname)
}
return nil
},
)
}
// getWithoutUniqFields returns resources without unique fields ('metadata.name',
// 'metadata.namespace', 'metadata.uid' and 'metadata.resourceVersion').
func getWithoutUniqueFields(ctx *FeatureContext, groupVersionKindStr, name string) (*unstructured.Unstructured, error) {
groupVersionKind, err := helpers.GroupVersionKindFrom(groupVersionKindStr)
if err != nil {
return nil, err
}
namespacedName, _ := helpers.NamespacedNameFrom(name)
obj, err := ctx.Get(groupVersionKind, namespacedName)
if err != nil {
return nil, err
}
obj.SetName("")
obj.SetNamespace("")
obj.SetUID("")
obj.SetResourceVersion("")
return obj, nil
}
// diffObjects return a readable diff if the given objects are different.
func diffObjects(lobj, robj *unstructured.Unstructured) string {
diff := gojsondiff.New().CompareObjects(lobj.Object, robj.Object)
if diff.Modified() {
outDiff, _ := formatter.
NewAsciiFormatter(lobj.Object, formatter.AsciiFormatterConfig{Coloring: false, ShowArrayIndex: true}).
Format(diff)
return fmt.Sprintf("\n%s", outDiff)
}
return ""
}