1 package kclient
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/google/go-cmp/cmp"
8 "k8s.io/apimachinery/pkg/watch"
9
10 corev1 "k8s.io/api/core/v1"
11 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12 "k8s.io/apimachinery/pkg/runtime"
13
14 ktesting "k8s.io/client-go/testing"
15 )
16
17 func TestCreateTLSSecret(t *testing.T) {
18 tests := []struct {
19 name string
20 objectMeta metav1.ObjectMeta
21 host string
22 wantErr bool
23 }{
24 {
25 name: "Case: Valid Secret name",
26 objectMeta: metav1.ObjectMeta{
27 Name: "testComponent-tlssecret",
28 OwnerReferences: []metav1.OwnerReference{
29 {
30 APIVersion: "1",
31 Kind: "fakeOwnerReference",
32 Name: "testDeployment",
33 },
34 },
35 },
36 host: "1.2.3.4.nip.io",
37 wantErr: false,
38 },
39 {
40 name: "Case: Invalid Secret name",
41 objectMeta: metav1.ObjectMeta{
42 Name: "",
43 OwnerReferences: []metav1.OwnerReference{
44 {
45 APIVersion: "1",
46 Kind: "fakeOwnerReference",
47 Name: "testDeployment",
48 },
49 },
50 },
51 host: "1.2.3.4.nip.io",
52 wantErr: true,
53 },
54 }
55 for _, tt := range tests {
56 t.Run(tt.name, func(t *testing.T) {
57
58 fkclient, fkclientset := FakeNew()
59 fkclient.Namespace = "default"
60
61 fkclientset.Kubernetes.PrependReactor("create", "secrets", func(action ktesting.Action) (bool, runtime.Object, error) {
62 secret := corev1.Secret{
63 ObjectMeta: tt.objectMeta,
64 }
65 return true, &secret, nil
66 })
67 selfsignedcert, err := GenerateSelfSignedCertificate(tt.host)
68 if err != nil {
69 t.Errorf("fkclient.GenerateSelfSignedCertificate unexpected error %v", err)
70 }
71 createdTLSSceret, err := fkclient.CreateTLSSecret(selfsignedcert.CertPem, selfsignedcert.KeyPem, tt.objectMeta)
72
73 if !tt.wantErr == (err != nil) {
74 t.Errorf("fkclient.CreateIngressExtensionV1 unexpected error %v, wantErr %v", err, tt.wantErr)
75 }
76 if err == nil {
77 if len(fkclientset.Kubernetes.Actions()) != 1 {
78 t.Errorf("expected 1 action, got: %v", fkclientset.Kubernetes.Actions())
79 } else {
80 if createdTLSSceret.Name != tt.objectMeta.Name {
81 t.Errorf("secret name does not match the expected name, expected: %s, got %s", tt.objectMeta.Name, createdTLSSceret.Name)
82 }
83 }
84 }
85
86 })
87 }
88 }
89
90 func TestGenerateSelfSignedCertificate(t *testing.T) {
91
92 tests := []struct {
93 name string
94 host string
95 }{
96 {
97 name: "test1",
98 host: "1.2.3.4.nip.io",
99 },
100 }
101
102 for _, tt := range tests {
103 t.Run(tt.name, func(t *testing.T) {
104
105 cert, err := GenerateSelfSignedCertificate(tt.host)
106 if err != nil {
107 t.Errorf("Unexpected error %v", err)
108 }
109 if cert.CertPem == nil || cert.KeyPem == nil || len(cert.CertPem) == 0 || len(cert.KeyPem) == 0 {
110 t.Errorf("Invalid certificate created")
111 }
112
113 })
114 }
115 }
116
117 func TestGetSecret(t *testing.T) {
118 tests := []struct {
119 name string
120 secretNS string
121 secretName string
122 wantErr bool
123 want *corev1.Secret
124 }{
125 {
126 name: "Case: Valid request for retrieving a secret",
127 secretNS: "",
128 secretName: "foo",
129 want: &corev1.Secret{
130 ObjectMeta: metav1.ObjectMeta{
131 Name: "foo",
132 },
133 },
134 wantErr: false,
135 },
136 {
137 name: "Case: Invalid request for retrieving a secret",
138 secretNS: "",
139 secretName: "foo2",
140 want: &corev1.Secret{
141 ObjectMeta: metav1.ObjectMeta{
142 Name: "foo",
143 },
144 },
145 wantErr: true,
146 },
147 }
148 for _, tt := range tests {
149 t.Run(tt.name, func(t *testing.T) {
150 fakeClient, fakeClientSet := FakeNew()
151
152
153 fakeClientSet.Kubernetes.PrependReactor("get", "secrets", func(action ktesting.Action) (bool, runtime.Object, error) {
154 if tt.want.Name != tt.secretName {
155 return true, nil, fmt.Errorf("'get' called with a different secret name")
156 }
157 return true, tt.want, nil
158 })
159
160 returnValue, err := fakeClient.GetSecret(tt.secretName, tt.secretNS)
161
162
163 if err == nil && returnValue != tt.want {
164 t.Errorf("error in return value got: %v, expected %v", returnValue, tt.want)
165 }
166
167 if !tt.wantErr == (err != nil) {
168 t.Errorf("\nclient.GetSecret(secretNS, secretName) unexpected error %v, wantErr %v", err, tt.wantErr)
169 }
170 })
171 }
172 }
173
174 func TestListSecrets(t *testing.T) {
175
176 tests := []struct {
177 name string
178 secretList corev1.SecretList
179 output []corev1.Secret
180 wantErr bool
181 }{
182 {
183 name: "Case 1: Ensure secrets are properly listed",
184 secretList: corev1.SecretList{
185 Items: []corev1.Secret{
186 {
187 ObjectMeta: metav1.ObjectMeta{
188 Name: "secret1",
189 },
190 },
191 {
192 ObjectMeta: metav1.ObjectMeta{
193 Name: "secret2",
194 },
195 },
196 },
197 },
198 output: []corev1.Secret{
199 {
200 ObjectMeta: metav1.ObjectMeta{
201 Name: "secret1",
202 },
203 },
204 {
205 ObjectMeta: metav1.ObjectMeta{
206 Name: "secret2",
207 },
208 },
209 },
210
211 wantErr: false,
212 },
213 }
214
215 for _, tt := range tests {
216 client, fakeClientSet := FakeNew()
217
218 fakeClientSet.Kubernetes.PrependReactor("list", "secrets", func(action ktesting.Action) (bool, runtime.Object, error) {
219 return true, &tt.secretList, nil
220 })
221
222 secretsList, err := client.ListSecrets("")
223
224 if diff := cmp.Diff(tt.output, secretsList); diff != "" {
225 t.Errorf("Client.ListSecrets() secretsList mismatch (-want +got):\n%s", diff)
226 }
227
228 if err == nil && !tt.wantErr {
229 if len(fakeClientSet.Kubernetes.Actions()) != 1 {
230 t.Errorf("expected 1 action in ListSecrets got: %v", fakeClientSet.Kubernetes.Actions())
231 }
232 } else if err == nil && tt.wantErr {
233 t.Error("test failed, expected: false, got true")
234 } else if err != nil && !tt.wantErr {
235 t.Errorf("test failed, expected: no error, got error: %s", err.Error())
236 }
237 }
238 }
239
240 func TestWaitAndGetSecret(t *testing.T) {
241
242 tests := []struct {
243 name string
244 secretName string
245 namespace string
246 wantErr bool
247 }{
248 {
249 name: "Case 1: no error expected",
250 secretName: "ruby",
251 namespace: "dummy",
252 wantErr: false,
253 },
254
255 {
256 name: "Case 2: error expected",
257 secretName: "",
258 namespace: "dummy",
259 wantErr: true,
260 },
261 }
262
263 for _, tt := range tests {
264 t.Run(tt.name, func(t *testing.T) {
265
266 fkclient, fkclientset := FakeNew()
267 fkWatch := watch.NewFake()
268
269
270 go func() {
271 fkWatch.Modify(&corev1.Secret{
272 ObjectMeta: metav1.ObjectMeta{
273 Name: tt.secretName,
274 },
275 })
276 }()
277
278 fkclientset.Kubernetes.PrependWatchReactor("secrets", func(action ktesting.Action) (handled bool, ret watch.Interface, err error) {
279 if len(tt.secretName) == 0 {
280 return true, nil, fmt.Errorf("error watching secret")
281 }
282 return true, fkWatch, nil
283 })
284
285 pod, err := fkclient.WaitAndGetSecret(tt.secretName, tt.namespace)
286
287 if !tt.wantErr == (err != nil) {
288 t.Errorf(" client.WaitAndGetSecret(string, string) unexpected error %v, wantErr %v", err, tt.wantErr)
289 return
290 }
291
292 if len(fkclientset.Kubernetes.Actions()) != 1 {
293 t.Errorf("expected 1 action in WaitAndGetSecret got: %v", fkclientset.Kubernetes.Actions())
294 }
295
296 if err == nil {
297 if pod.Name != tt.secretName {
298 t.Errorf("secret name is not matching to expected name, expected: %s, got %s", tt.secretName, pod.Name)
299 }
300 }
301 })
302 }
303 }
304
View as plain text