This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathsecret_test.go
121 lines (110 loc) · 2.91 KB
/
secret_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
119
120
121
package annotations
import (
"testing"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"github.com/redhat-developer/service-binding-operator/pkg/testutils"
"github.com/redhat-developer/service-binding-operator/test/mocks"
)
func TestSecretHandler(t *testing.T) {
type args struct {
name string
value string
service map[string]interface{}
resources []runtime.Object
expected map[string]interface{}
}
assertHandler := func(args args) func(*testing.T) {
return func(t *testing.T) {
f := mocks.NewFake(t, "test")
for _, r := range args.resources {
f.AddMockResource(r)
}
restMapper := testutils.BuildTestRESTMapper()
bindingInfo, err := NewBindingInfo(args.name, args.value)
require.NoError(t, err)
handler, err := NewSecretHandler(
f.FakeDynClient(),
bindingInfo,
unstructured.Unstructured{Object: args.service},
restMapper,
)
require.NoError(t, err)
got, err := handler.Handle()
require.NoError(t, err)
require.NotNil(t, got)
require.Equal(t, args.expected, got.Data)
}
}
t.Run("secret/scalar", assertHandler(args{
name: "servicebindingoperator.redhat.io/status.dbCredentials-password",
value: "binding:env:object:secret",
service: map[string]interface{}{
"metadata": map[string]interface{}{
"namespace": "the-namespace",
},
"status": map[string]interface{}{
"dbCredentials": "the-secret-resource-name",
},
},
resources: []runtime.Object{
&corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "the-namespace",
Name: "the-secret-resource-name",
},
Data: map[string][]byte{
"password": []byte("hunter2"),
},
},
},
expected: map[string]interface{}{
"secret": map[string]interface{}{
"password": "hunter2",
},
},
}))
t.Run("secret/map", assertHandler(args{
name: "servicebindingoperator.redhat.io/status.dbCredentials",
value: "binding:env:object:secret",
service: map[string]interface{}{
"metadata": map[string]interface{}{
"namespace": "the-namespace",
},
"status": map[string]interface{}{
"dbCredentials": "the-secret-resource-name",
},
},
resources: []runtime.Object{
&corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "the-namespace",
Name: "the-secret-resource-name",
},
Data: map[string][]byte{
"password": []byte("hunter2"),
"username": []byte("AzureDiamond"),
},
},
},
expected: map[string]interface{}{
"secret": map[string]interface{}{
"status": map[string]interface{}{
"dbCredentials": map[string]interface{}{
"username": "AzureDiamond",
"password": "hunter2",
},
},
},
},
}))
}