-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopts_expect.go
More file actions
259 lines (233 loc) · 8.43 KB
/
opts_expect.go
File metadata and controls
259 lines (233 loc) · 8.43 KB
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
// SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors
// SPDX-License-Identifier: Apache-2.0
package testing
import (
"encoding/json"
"github.com/crossplane/crossplane-runtime/pkg/meta"
fnapi "github.com/crossplane/function-sdk-go/proto/v1"
"github.com/pkg/errors"
"google.golang.org/protobuf/encoding/protojson"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"github.com/dbinfrago/crossplane-function-test-framework/internal/util/maps"
"github.com/dbinfrago/crossplane-function-test-framework/internal/util/yaml"
)
// ResourceModifier modifies a [fnapi.Resource].
type ResourceModifier func(res *fnapi.Resource)
// WithReady sets the ready state of an [fnapi.Resource].
func WithReady(ready fnapi.Ready) ResourceModifier {
return func(res *fnapi.Resource) { res.Ready = ready }
}
// WithConnectionDetails sets the connection details of an [fnapi.Resource].
func WithConnectionDetails(cd map[string][]byte) ResourceModifier {
return func(res *fnapi.Resource) { res.ConnectionDetails = cd }
}
func WithoutAPIVersionAndKind() ResourceModifier {
return func(res *fnapi.Resource) {
delete(res.GetResource().GetFields(), "apiVersion")
delete(res.GetResource().GetFields(), "kind")
}
}
// WithManifestOverride is a modifier that merges the existing resource
// manifest with the given overrideYAML.
func WithManifestOverride(overrideYAML []byte) ResourceModifier {
return func(res *fnapi.Resource) {
override := map[string]interface{}{}
if err := yaml.Unmarshal(overrideYAML, &override); err != nil {
panic(err.Error())
}
overwriteResourceManifest(res, override)
}
}
// WithManifestOverride is a modifier that merges the existing resource
// manifest with the given override object.
func WithManifestOverrideObject(override runtime.Object) ResourceModifier {
return func(res *fnapi.Resource) {
overrideU, err := runtime.DefaultUnstructuredConverter.ToUnstructured(override)
if err != nil {
panic(err.Error())
}
overwriteResourceManifest(res, overrideU)
}
}
func overwriteResourceManifest(res *fnapi.Resource, override map[string]interface{}) {
resRaw, err := protojson.Marshal(res.GetResource())
if err != nil {
panic(err.Error())
}
original := map[string]interface{}{}
if err := json.Unmarshal(resRaw, &original); err != nil {
panic(err.Error())
}
merged := maps.Merge(original, override)
mergedRaw, err := json.Marshal(merged)
if err != nil {
panic(err.Error())
}
if err := protojson.Unmarshal(mergedRaw, res.GetResource()); err != nil {
panic(err.Error())
}
}
// DeleteNestedFieldPath from a resource.
func DeleteNestedFieldPath(fields ...string) ResourceModifier {
return func(res *fnapi.Resource) {
resRaw, err := protojson.Marshal(res.GetResource())
if err != nil {
panic(err.Error())
}
data := map[string]interface{}{}
if err := json.Unmarshal(resRaw, &data); err != nil {
panic(err.Error())
}
unstructured.RemoveNestedField(data, fields...)
updateRaw, err := json.Marshal(data)
if err != nil {
panic(err.Error())
}
if err := protojson.Unmarshal(updateRaw, res.GetResource()); err != nil {
panic(err.Error())
}
}
}
// ExpectDesiredCompositeObject expects the given [runtime.Object] as desired
// composite as result of the function.
func ExpectDesiredCompositeObject(o runtime.Object, mods ...ResourceModifier) TestFunctionOpt {
return func(tc *FunctionTest) {
res := &fnapi.Resource{
Resource: mustObjectAsStruct(o),
}
for _, m := range mods {
m(res)
}
tc.res.Desired.Composite = res
}
}
// ExpectDesiredCompositeYAML is the same as [ExpectDesiredCompositeObject] but
// reads the object from a single YAML document.
func ExpectDesiredCompositeYAML(rawYAML []byte, mods ...ResourceModifier) TestFunctionOpt {
return ExpectDesiredCompositeObject(mustUnstructuredFromYAML(rawYAML), mods...)
}
// ExpectDesiredCompositeJSON is the same as [ExpectDesiredCompositeObject] but
// reads the object from a JSON document.
func ExpectDesiredCompositeJSON(rawJSON []byte, mods ...ResourceModifier) TestFunctionOpt {
return ExpectDesiredCompositeObject(mustUnstructuredFromJSON(rawJSON), mods...)
}
// ExpectDesiredResourceObject adds an object to the expected outcome of a
// function.
func ExpectDesiredResourceObject(name string, o runtime.Object, mods ...ResourceModifier) TestFunctionOpt {
return func(tc *FunctionTest) {
res := &fnapi.Resource{
Resource: mustObjectAsStruct(o),
}
for _, m := range mods {
m(res)
}
tc.res.Desired.Resources[name] = res
}
}
// ExpectDesiredResourceYAML is the same as [ExpectDesiredResourceObject] but
// reads the object from a single YAML document.
func ExpectDesiredResourceYAML(name string, rawYAML []byte, mods ...ResourceModifier) TestFunctionOpt {
return ExpectDesiredResourceObject(name, mustUnstructuredFromYAML(rawYAML), mods...)
}
// ExpectDesiredResourceJSON is the same as [ExpectDesiredResourceObject] but
// reads the object from a JSON document.
func ExpectDesiredResourceJSON(name string, rawJSON []byte, mods ...ResourceModifier) TestFunctionOpt {
return ExpectDesiredResourceObject(name, mustUnstructuredFromJSON(rawJSON), mods...)
}
// IgnoreDesiredResources removes the resources from the expected desired
// resource response. If no resource with a given name does exist it is a noop.
func IgnoreDesiredResources(names ...string) TestFunctionOpt {
return func(tc *FunctionTest) {
for _, n := range names {
delete(tc.res.GetDesired().GetResources(), n)
}
}
}
// IgnoreDesiredResourcesInResponse removes resources from the functions generated
// response before comparing the response with the expected desired resources.
// Use it to ignore desired resources whose existence and content you don't
// want to verify, e.g. statically generated ones.
func IgnoreDesiredResourcesInResponse(resourceNames ...string) TestFunctionOpt {
return func(tc *FunctionTest) {
tc.ignoredDesired = append(tc.ignoredDesired, resourceNames...)
}
}
// ExpectedDesiredResourcesYAML reads all objects from a multi-document YAML and
// expected them as desired resources from the function.
//
// It uses the annotation [AnnotationKeyResourceName] to determine
// the name of the resource.
func ExpectDesiredResourcesYAML(rawYAML []byte, mods ...ResourceModifier) TestFunctionOpt {
return func(tc *FunctionTest) {
uList, err := yaml.UnmarshalObjects[*unstructured.Unstructured](rawYAML)
if err != nil {
panic(err.Error())
}
for _, u := range uList {
key := GetTestResourceName(u)
if key == "" {
panic("resource has no name annotation")
}
meta.RemoveAnnotations(u, AnnotationKeyResourceName)
str := mustObjectAsStruct(u)
res := &fnapi.Resource{
Resource: str,
// TODO: Set connection details and ready state
}
for _, mod := range mods {
mod(res)
}
tc.res.Desired.Resources[key] = res
}
}
}
// ExpectDesiredResourcesYAMLOverride loads the given objects from YAML and
// merges them with existing desired objects.
// It only modifies resources that are already desired.
func ExpectDesiredResourcesYAMLOverride(rawYAML []byte) TestFunctionOpt {
return func(tc *FunctionTest) {
if tc.res.GetDesired() == nil || len(tc.res.GetDesired().GetResources()) == 0 {
return
}
uList, err := yaml.UnmarshalObjects[*unstructured.Unstructured](rawYAML)
if err != nil {
panic(err.Error())
}
for _, u := range uList {
key := GetTestResourceName(u)
if key == "" {
panic("override resource has no name annotation")
}
meta.RemoveAnnotations(u, AnnotationKeyResourceName)
desiredRes, hasDesiredResource := tc.res.GetDesired().GetResources()[key]
if !hasDesiredResource {
continue
}
resRaw, err := protojson.Marshal(desiredRes.GetResource())
if err != nil {
panic(errors.Wrap(err, key).Error())
}
original := map[string]interface{}{}
if err := json.Unmarshal(resRaw, &original); err != nil {
panic(errors.Wrap(err, key).Error())
}
u.Object = maps.Merge(original, u.Object)
str := mustObjectAsStruct(u)
res := &fnapi.Resource{
Resource: str,
ConnectionDetails: desiredRes.GetConnectionDetails(),
Ready: desiredRes.GetReady(),
}
tc.res.Desired.Resources[key] = res
}
}
}
// ExpectResults expects a list of [fnapi.Result] from a function.
func ExpectResults(results []*fnapi.Result) TestFunctionOpt {
return func(tc *FunctionTest) { tc.res.Results = results }
}
// ExpectError expects an error from a TestFunctionOpt.
func ExpectError(err error) TestFunctionOpt {
return func(tc *FunctionTest) { tc.err = err }
}