forked from crossplane-contrib/function-patch-and-transform
-
Notifications
You must be signed in to change notification settings - Fork 1
/
connection_test.go
118 lines (112 loc) · 3.34 KB
/
connection_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
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
package main
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/crossplane/crossplane-runtime/pkg/resource/fake"
"github.com/crossplane/crossplane-runtime/pkg/test"
"github.com/stevendborrelli/function-conditional-patch-and-transform/input/v1beta1"
)
func TestExtractConnectionDetails(t *testing.T) {
type args struct {
cd resource.Composed
data managed.ConnectionDetails
cfg []v1beta1.ConnectionDetail
}
type want struct {
conn managed.ConnectionDetails
err error
}
cases := map[string]struct {
reason string
args args
want want
}{
"MissingNameError": {
reason: "We should return an error if a connection detail is missing a name.",
args: args{
cfg: []v1beta1.ConnectionDetail{
{
// A nameless connection detail.
Type: v1beta1.ConnectionDetailTypeFromValue,
},
},
},
want: want{
err: errors.Wrap(errors.New("name: Required value: name is required"), "invalid"),
},
},
"FetchConfigSuccess": {
reason: "Should extract only the selected set of secret keys",
args: args{
cd: &fake.Composed{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Generation: 4,
},
},
data: managed.ConnectionDetails{
"foo": []byte("a"),
"bar": []byte("b"),
},
cfg: []v1beta1.ConnectionDetail{
{
Type: v1beta1.ConnectionDetailTypeFromConnectionSecretKey,
Name: "bar",
FromConnectionSecretKey: ptr.To[string]("bar"),
},
{
Type: v1beta1.ConnectionDetailTypeFromConnectionSecretKey,
Name: "none",
FromConnectionSecretKey: ptr.To[string]("none"),
},
{
Type: v1beta1.ConnectionDetailTypeFromConnectionSecretKey,
Name: "convfoo",
FromConnectionSecretKey: ptr.To[string]("foo"),
},
{
Type: v1beta1.ConnectionDetailTypeFromValue,
Name: "fixed",
Value: ptr.To[string]("value"),
},
{
Type: v1beta1.ConnectionDetailTypeFromFieldPath,
Name: "name",
FromFieldPath: ptr.To[string]("objectMeta.name"),
},
{
Type: v1beta1.ConnectionDetailTypeFromFieldPath,
Name: "generation",
FromFieldPath: ptr.To[string]("objectMeta.generation"),
},
},
},
want: want{
conn: managed.ConnectionDetails{
"convfoo": []byte("a"),
"bar": []byte("b"),
"fixed": []byte("value"),
"name": []byte("test"),
"generation": []byte("4"),
},
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
conn, err := ExtractConnectionDetails(tc.args.cd, tc.args.data, tc.args.cfg...)
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
t.Errorf("\n%s\nExtractConnectionDetails(...): -want, +got:\n%s", tc.reason, diff)
}
if diff := cmp.Diff(tc.want.conn, conn, cmpopts.EquateEmpty()); diff != "" {
t.Errorf("\n%s\nExtractConnectionDetails(...): -want, +got:\n%s", tc.reason, diff)
}
})
}
}