-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathsecret_controller_test.go
158 lines (135 loc) · 3.99 KB
/
secret_controller_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
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
package controllers
import (
"testing"
"github.com/kuadrant/authorino/api/v1beta1"
"gotest.tools/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
controllerruntime "sigs.k8s.io/controller-runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
type fakeReconciler struct {
Reconciled bool
Finished chan bool
}
func (r *fakeReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
defer close(r.Finished)
r.Finished <- true
r.Reconciled = true
return ctrl.Result{}, nil
}
type secretReconcilerTest struct {
secret v1.Secret
service v1beta1.Service
serviceReconciler *fakeReconciler
secretReconciler *SecretReconciler
}
func newSecretReconcilerTest(secretLabels map[string]string) secretReconcilerTest {
secret := v1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "bill",
Namespace: "authorino",
Labels: secretLabels,
},
Data: map[string][]byte{
"api_key": []byte("123456"),
},
}
service := v1beta1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "config.authorino.3scale.net/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "service1",
Namespace: "authorino",
},
Spec: v1beta1.ServiceSpec{
Hosts: []string{"echo-api"},
Identity: []*v1beta1.Identity{
{
Name: "friends",
APIKey: &v1beta1.Identity_APIKey{
LabelSelectors: map[string]string{
"authorino.3scale.net/managed-by": "authorino",
"target": "echo-api",
},
},
},
},
Metadata: []*v1beta1.Metadata{},
Authorization: []*v1beta1.Authorization{},
},
Status: v1beta1.ServiceStatus{
Ready: false,
},
}
scheme := runtime.NewScheme()
_ = v1beta1.AddToScheme(scheme)
_ = v1.AddToScheme(scheme)
// Create a fake client with a service and a secret.
client := fake.NewFakeClientWithScheme(scheme, &service, &secret)
serviceReconciler := &fakeReconciler{
Finished: make(chan bool),
}
secretReconciler := &SecretReconciler{
Client: client,
Log: ctrl.Log.WithName("reconcilerTest"),
Scheme: nil,
SecretLabel: "authorino.3scale.net/managed-by",
ServiceReconciler: serviceReconciler,
}
t := secretReconcilerTest{
secret,
service,
serviceReconciler,
secretReconciler,
}
return t
}
func (t *secretReconcilerTest) reconcile() (reconcile.Result, error) {
return t.secretReconciler.Reconcile(controllerruntime.Request{
NamespacedName: types.NamespacedName{
Namespace: t.secret.Namespace,
Name: t.secret.Name,
},
})
}
func TestMissingAuthorinoLabel(t *testing.T) {
// secret missing the authorino "managed-by" label
reconcilerTest := newSecretReconcilerTest(map[string]string{
"authorino.3scale.net/managed-by": "authorino",
})
_, err := reconcilerTest.reconcile()
assert.Check(t, !reconcilerTest.serviceReconciler.Reconciled)
assert.NilError(t, err)
}
func TestSameLabelsAsService(t *testing.T) {
// secret with the authorino "managed-by" label and the same labels as the service
reconcilerTest := newSecretReconcilerTest(map[string]string{
"authorino.3scale.net/managed-by": "authorino",
"target": "echo-api",
})
_, err := reconcilerTest.reconcile()
finished := <-reconcilerTest.serviceReconciler.Finished
assert.Check(t, finished)
assert.Check(t, reconcilerTest.serviceReconciler.Reconciled)
assert.NilError(t, err)
}
func TestUnmatchingLabels(t *testing.T) {
// secret with the authorino "managed-by" label but not the same labels as the service
reconcilerTest := newSecretReconcilerTest(map[string]string{
"authorino.3scale.net/managed-by": "authorino",
})
_, err := reconcilerTest.reconcile()
assert.Check(t, !reconcilerTest.serviceReconciler.Reconciled)
assert.NilError(t, err)
}