generated from crossplane/function-template-go
-
Notifications
You must be signed in to change notification settings - Fork 37
/
context_test.go
93 lines (86 loc) · 2.34 KB
/
context_test.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
package main
import (
"testing"
"github.com/crossplane/crossplane-runtime/pkg/logging"
fnv1beta1 "github.com/crossplane/function-sdk-go/proto/v1beta1"
"github.com/crossplane/function-sdk-go/resource"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"google.golang.org/protobuf/testing/protocmp"
)
func TestMergeContext(t *testing.T) {
type args struct {
val map[string]interface{}
req *fnv1beta1.RunFunctionRequest
}
type want struct {
us map[string]any
err error
}
cases := map[string]struct {
reason string
args args
want want
}{
"NoContextAtKey": {
reason: "When there is no existing context data at the key to merge, return the value",
args: args{
req: &fnv1beta1.RunFunctionRequest{
Context: nil,
},
val: map[string]interface{}{"hello": "world"},
},
want: want{
us: map[string]interface{}{"hello": "world"},
err: nil,
},
},
"SuccessfulMerge": {
reason: "Confirm that keys are merged with source overwriting destination",
args: args{
req: &fnv1beta1.RunFunctionRequest{
Context: resource.MustStructJSON(`{"apiextensions.crossplane.io/environment":{"complex":{"a":"b","c":{"d":"e","f":"1","overWrite": "fromContext"}}}}`),
},
val: map[string]interface{}{
"newKey": "newValue",
"apiextensions.crossplane.io/environment": map[string]any{
"complex": map[string]any{
"c": map[string]any{
"overWrite": "fromFunction",
},
},
},
},
},
want: want{
us: map[string]interface{}{
"apiextensions.crossplane.io/environment": map[string]any{
"complex": map[string]any{
"a": "b",
"c": map[string]any{
"d": "e",
"f": "1",
"overWrite": "fromFunction",
},
},
},
"newKey": "newValue"},
err: nil,
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
f := &Function{
log: logging.NewNopLogger(),
}
rsp, err := f.MergeContext(tc.args.req, tc.args.val)
if diff := cmp.Diff(tc.want.us, rsp, protocmp.Transform()); diff != "" {
t.Errorf("%s\nf.MergeContext(...): -want rsp, +got rsp:\n%s", tc.reason, diff)
}
if diff := cmp.Diff(tc.want.err, err, cmpopts.EquateErrors()); diff != "" {
t.Errorf("%s\nf.RunFunction(...): -want err, +got err:\n%s", tc.reason, diff)
}
})
}
}