1 package kclient
2
3 import (
4 "errors"
5 "testing"
6
7 "github.com/google/go-cmp/cmp"
8 "github.com/google/go-cmp/cmp/cmpopts"
9 kerrors "k8s.io/apimachinery/pkg/api/errors"
10 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
11 "k8s.io/apimachinery/pkg/runtime"
12 "k8s.io/apimachinery/pkg/runtime/schema"
13 "k8s.io/client-go/dynamic"
14 "k8s.io/client-go/dynamic/fake"
15 ktesting "k8s.io/client-go/testing"
16
17 "github.com/redhat-developer/odo/pkg/api"
18 )
19
20 func TestClient_GetRegistryList(t *testing.T) {
21 type fields struct {
22 Namespace string
23 DynamicClient func() (dynamic.Interface, error)
24 }
25 tests := []struct {
26 name string
27 fields fields
28 want []api.Registry
29 wantErr bool
30 }{
31 {
32 name: "generic error when listing namespaced devfile registries",
33 fields: fields{
34 DynamicClient: func() (dynamic.Interface, error) {
35 scheme := runtime.NewScheme()
36 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
37 Group: "registry.devfile.io",
38 Version: "v1alpha1",
39 Kind: "ClusterDevfileRegistriesListList",
40 }, &unstructured.UnstructuredList{})
41 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
42 Group: "registry.devfile.io",
43 Version: "v1alpha1",
44 Kind: "DevfileRegistriesListList",
45 }, &unstructured.UnstructuredList{})
46 client := fake.NewSimpleDynamicClient(scheme)
47 client.PrependReactor("list", "devfileregistrieslists", func(action ktesting.Action) (handled bool, ret runtime.Object, err error) {
48 return true, nil, errors.New("some error")
49 })
50
51 return client, nil
52 },
53 },
54 wantErr: true,
55 },
56 {
57 name: "forbidden error when listing namespaced devfile registries, but generic error when listing cluster-scoped registries",
58 fields: fields{
59 DynamicClient: func() (dynamic.Interface, error) {
60 scheme := runtime.NewScheme()
61 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
62 Group: "registry.devfile.io",
63 Version: "v1alpha1",
64 Kind: "ClusterDevfileRegistriesListList",
65 }, &unstructured.UnstructuredList{})
66 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
67 Group: "registry.devfile.io",
68 Version: "v1alpha1",
69 Kind: "DevfileRegistriesListList",
70 }, &unstructured.UnstructuredList{})
71 client := fake.NewSimpleDynamicClient(scheme)
72 client.PrependReactor("list", "devfileregistrieslists", func(action ktesting.Action) (handled bool, ret runtime.Object, err error) {
73 return true, nil, kerrors.NewForbidden(schema.GroupResource{}, "some-name", errors.New("forbidden"))
74 })
75 client.PrependReactor("list", "clusterdevfileregistrieslists", func(action ktesting.Action) (handled bool, ret runtime.Object, err error) {
76 return true, nil, errors.New("some-error")
77 })
78
79 return client, nil
80 },
81 },
82 wantErr: true,
83 },
84 {
85 name: "forbidden errors when listing both namespaced and cluster-scoped registries",
86 fields: fields{
87 DynamicClient: func() (dynamic.Interface, error) {
88 scheme := runtime.NewScheme()
89 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
90 Group: "registry.devfile.io",
91 Version: "v1alpha1",
92 Kind: "ClusterDevfileRegistriesListList",
93 }, &unstructured.UnstructuredList{})
94 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
95 Group: "registry.devfile.io",
96 Version: "v1alpha1",
97 Kind: "DevfileRegistriesListList",
98 }, &unstructured.UnstructuredList{})
99 client := fake.NewSimpleDynamicClient(scheme)
100 client.PrependReactor("list", "devfileregistrieslists", func(action ktesting.Action) (handled bool, ret runtime.Object, err error) {
101 return true, nil, kerrors.NewForbidden(schema.GroupResource{}, "some-name", errors.New("forbidden"))
102 })
103 client.PrependReactor("list", "clusterdevfileregistrieslists", func(action ktesting.Action) (handled bool, ret runtime.Object, err error) {
104 return true, nil, kerrors.NewForbidden(schema.GroupResource{}, "some-name", errors.New("forbidden"))
105 })
106
107 return client, nil
108 },
109 },
110 wantErr: false,
111 },
112 {
113 name: "unauthorized errors when listing both namespaced and cluster-scoped registries",
114 fields: fields{
115 DynamicClient: func() (dynamic.Interface, error) {
116 scheme := runtime.NewScheme()
117 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
118 Group: "registry.devfile.io",
119 Version: "v1alpha1",
120 Kind: "ClusterDevfileRegistriesListList",
121 }, &unstructured.UnstructuredList{})
122 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
123 Group: "registry.devfile.io",
124 Version: "v1alpha1",
125 Kind: "DevfileRegistriesListList",
126 }, &unstructured.UnstructuredList{})
127 client := fake.NewSimpleDynamicClient(scheme)
128 client.PrependReactor("list", "devfileregistrieslists", func(action ktesting.Action) (handled bool, ret runtime.Object, err error) {
129 return true, nil, kerrors.NewUnauthorized("unauthorized")
130 })
131 client.PrependReactor("list", "clusterdevfileregistrieslists", func(action ktesting.Action) (handled bool, ret runtime.Object, err error) {
132 return true, nil, kerrors.NewUnauthorized("unauthorized")
133 })
134
135 return client, nil
136 },
137 },
138 wantErr: false,
139 },
140 {
141 name: "no registries in cluster",
142 fields: fields{
143 DynamicClient: func() (dynamic.Interface, error) {
144 scheme := runtime.NewScheme()
145 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
146 Group: "registry.devfile.io",
147 Version: "v1alpha1",
148 Kind: "ClusterDevfileRegistriesListList",
149 }, &unstructured.UnstructuredList{})
150 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
151 Group: "registry.devfile.io",
152 Version: "v1alpha1",
153 Kind: "DevfileRegistriesListList",
154 }, &unstructured.UnstructuredList{})
155
156 client := fake.NewSimpleDynamicClient(scheme)
157 return client, nil
158 },
159 },
160 wantErr: false,
161 },
162 {
163 name: "with invalid content in devfileRegistries spec",
164 fields: fields{
165 Namespace: "my-ns",
166 DynamicClient: func() (dynamic.Interface, error) {
167 scheme := runtime.NewScheme()
168 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
169 Group: "registry.devfile.io",
170 Version: "v1alpha1",
171 Kind: "ClusterDevfileRegistriesListList",
172 }, &unstructured.UnstructuredList{})
173 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
174 Group: "registry.devfile.io",
175 Version: "v1alpha1",
176 Kind: "DevfileRegistriesListList",
177 }, &unstructured.UnstructuredList{})
178
179 var invalidDevfileRegistry unstructured.Unstructured
180 err := invalidDevfileRegistry.UnmarshalJSON([]byte(`
181 {
182 "apiVersion": "registry.devfile.io/v1alpha1",
183 "kind": "DevfileRegistriesList",
184 "metadata": {
185 "name": "invalid-devfile-reg1",
186 "namespace": "my-ns"
187 },
188 "spec": {
189 "invalidField": [
190 {
191 "name": "devfile-reg01",
192 "url": "https://devfile-reg01.example.com"
193 }
194 ]
195 }
196 }`))
197 if err != nil {
198 return nil, err
199 }
200
201 var invalidClusterDevfileRegistry unstructured.Unstructured
202 err = invalidClusterDevfileRegistry.UnmarshalJSON([]byte(`
203 {
204 "apiVersion": "registry.devfile.io/v1alpha1",
205 "kind": "ClusterDevfileRegistriesList",
206 "metadata": {
207 "name": "invalid-cluster-devfile-reg1"
208 },
209 "spec": {
210 "invalidField": [
211 {
212 "name": "cluster-devfile-reg01",
213 "url": "https://cluster-devfile-reg01.example.com"
214 }
215 ]
216 }
217 }`))
218 if err != nil {
219 return nil, err
220 }
221
222 client := fake.NewSimpleDynamicClient(scheme, &invalidDevfileRegistry, &invalidClusterDevfileRegistry)
223 return client, nil
224 },
225 },
226 wantErr: false,
227 },
228 {
229 name: "with invalid structure in devfileRegistries spec of namespaced resource",
230 fields: fields{
231 Namespace: "my-ns",
232 DynamicClient: func() (dynamic.Interface, error) {
233 scheme := runtime.NewScheme()
234 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
235 Group: "registry.devfile.io",
236 Version: "v1alpha1",
237 Kind: "ClusterDevfileRegistriesListList",
238 }, &unstructured.UnstructuredList{})
239 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
240 Group: "registry.devfile.io",
241 Version: "v1alpha1",
242 Kind: "DevfileRegistriesListList",
243 }, &unstructured.UnstructuredList{})
244
245 var invalidDevfileRegistry unstructured.Unstructured
246 err := invalidDevfileRegistry.UnmarshalJSON([]byte(`
247 {
248 "apiVersion": "registry.devfile.io/v1alpha1",
249 "kind": "DevfileRegistriesList",
250 "metadata": {
251 "name": "invalid-devfile-reg1",
252 "namespace": "my-ns"
253 },
254 "spec": {
255 "devfileRegistries":
256 {
257 "name": "devfile-reg01",
258 "url": "https://devfile-reg01.example.com"
259 }
260 }
261 }`))
262 if err != nil {
263 return nil, err
264 }
265
266 client := fake.NewSimpleDynamicClient(scheme, &invalidDevfileRegistry)
267 return client, nil
268 },
269 },
270 wantErr: true,
271 },
272 {
273 name: "with invalid structure in devfileRegistries spec of cluster resource",
274 fields: fields{
275 DynamicClient: func() (dynamic.Interface, error) {
276 scheme := runtime.NewScheme()
277 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
278 Group: "registry.devfile.io",
279 Version: "v1alpha1",
280 Kind: "ClusterDevfileRegistriesListList",
281 }, &unstructured.UnstructuredList{})
282 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
283 Group: "registry.devfile.io",
284 Version: "v1alpha1",
285 Kind: "DevfileRegistriesListList",
286 }, &unstructured.UnstructuredList{})
287
288 var invalidClusterDevfileRegistry unstructured.Unstructured
289 err := invalidClusterDevfileRegistry.UnmarshalJSON([]byte(`
290 {
291 "apiVersion": "registry.devfile.io/v1alpha1",
292 "kind": "ClusterDevfileRegistriesList",
293 "metadata": {
294 "name": "invalid-cluster-devfile-reg1"
295 },
296 "spec": {
297 "devfileRegistries":
298 {
299 "name": "cluster-devfile-reg01",
300 "url": "https://cluster-devfile-reg01.example.com"
301 }
302 }
303 }`))
304 if err != nil {
305 return nil, err
306 }
307
308 client := fake.NewSimpleDynamicClient(scheme, &invalidClusterDevfileRegistry)
309 return client, nil
310 },
311 },
312 wantErr: true,
313 },
314 {
315 name: "with invalid content in devfileRegistries spec of namespaced resource",
316 fields: fields{
317 DynamicClient: func() (dynamic.Interface, error) {
318 scheme := runtime.NewScheme()
319 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
320 Group: "registry.devfile.io",
321 Version: "v1alpha1",
322 Kind: "ClusterDevfileRegistriesListList",
323 }, &unstructured.UnstructuredList{})
324 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
325 Group: "registry.devfile.io",
326 Version: "v1alpha1",
327 Kind: "DevfileRegistriesListList",
328 }, &unstructured.UnstructuredList{})
329
330 var invalidDevfileRegistry unstructured.Unstructured
331 err := invalidDevfileRegistry.UnmarshalJSON([]byte(`
332 {
333 "apiVersion": "registry.devfile.io/v1alpha1",
334 "kind": "DevfileRegistriesList",
335 "metadata": {
336 "name": "invalid-devfile-reg1",
337 "namespace": "my-ns"
338 },
339 "spec": {
340 "devfileRegistries": [
341 "not-a-map"
342 ]
343 }
344 }`))
345 if err != nil {
346 return nil, err
347 }
348
349 client := fake.NewSimpleDynamicClient(scheme, &invalidDevfileRegistry)
350 return client, nil
351 },
352 },
353 wantErr: true,
354 },
355 {
356 name: "with invalid name type in devfileRegistries spec of namespaced resource",
357 fields: fields{
358 DynamicClient: func() (dynamic.Interface, error) {
359 scheme := runtime.NewScheme()
360 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
361 Group: "registry.devfile.io",
362 Version: "v1alpha1",
363 Kind: "ClusterDevfileRegistriesListList",
364 }, &unstructured.UnstructuredList{})
365 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
366 Group: "registry.devfile.io",
367 Version: "v1alpha1",
368 Kind: "DevfileRegistriesListList",
369 }, &unstructured.UnstructuredList{})
370
371 var invalidDevfileRegistry unstructured.Unstructured
372 err := invalidDevfileRegistry.UnmarshalJSON([]byte(`
373 {
374 "apiVersion": "registry.devfile.io/v1alpha1",
375 "kind": "DevfileRegistriesList",
376 "metadata": {
377 "name": "invalid-devfile-reg1",
378 "namespace": "my-ns"
379 },
380 "spec": {
381 "devfileRegistries": [
382 {
383 "name": true,
384 "url": "https://devfile-reg01.example.com"
385 }
386 ]
387 }
388 }`))
389 if err != nil {
390 return nil, err
391 }
392
393 client := fake.NewSimpleDynamicClient(scheme, &invalidDevfileRegistry)
394 return client, nil
395 },
396 },
397 wantErr: true,
398 },
399 {
400 name: "with invalid url type in devfileRegistries spec of namespaced resource",
401 fields: fields{
402 DynamicClient: func() (dynamic.Interface, error) {
403 scheme := runtime.NewScheme()
404 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
405 Group: "registry.devfile.io",
406 Version: "v1alpha1",
407 Kind: "ClusterDevfileRegistriesListList",
408 }, &unstructured.UnstructuredList{})
409 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
410 Group: "registry.devfile.io",
411 Version: "v1alpha1",
412 Kind: "DevfileRegistriesListList",
413 }, &unstructured.UnstructuredList{})
414
415 var invalidDevfileRegistry unstructured.Unstructured
416 err := invalidDevfileRegistry.UnmarshalJSON([]byte(`
417 {
418 "apiVersion": "registry.devfile.io/v1alpha1",
419 "kind": "DevfileRegistriesList",
420 "metadata": {
421 "name": "invalid-devfile-reg1",
422 "namespace": "my-ns"
423 },
424 "spec": {
425 "devfileRegistries": [
426 {
427 "name": "my-reg",
428 "url": 1
429 }
430 ]
431 }
432 }`))
433 if err != nil {
434 return nil, err
435 }
436
437 client := fake.NewSimpleDynamicClient(scheme, &invalidDevfileRegistry)
438 return client, nil
439 },
440 },
441 wantErr: true,
442 },
443 {
444 name: "with invalid skipTLSVerify type in devfileRegistries spec of namespaced resource",
445 fields: fields{
446 DynamicClient: func() (dynamic.Interface, error) {
447 scheme := runtime.NewScheme()
448 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
449 Group: "registry.devfile.io",
450 Version: "v1alpha1",
451 Kind: "ClusterDevfileRegistriesListList",
452 }, &unstructured.UnstructuredList{})
453 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
454 Group: "registry.devfile.io",
455 Version: "v1alpha1",
456 Kind: "DevfileRegistriesListList",
457 }, &unstructured.UnstructuredList{})
458
459 var invalidDevfileRegistry unstructured.Unstructured
460 err := invalidDevfileRegistry.UnmarshalJSON([]byte(`
461 {
462 "apiVersion": "registry.devfile.io/v1alpha1",
463 "kind": "DevfileRegistriesList",
464 "metadata": {
465 "name": "invalid-devfile-reg1",
466 "namespace": "my-ns"
467 },
468 "spec": {
469 "devfileRegistries": [
470 {
471 "name": "my-reg",
472 "url": "https://devfile-reg01.example.com",
473 "skipTLSVerify": "not-an-int"
474 }
475 ]
476 }
477 }`))
478 if err != nil {
479 return nil, err
480 }
481
482 client := fake.NewSimpleDynamicClient(scheme, &invalidDevfileRegistry)
483 return client, nil
484 },
485 },
486 wantErr: true,
487 },
488 {
489 name: "with only namespaced registries",
490 fields: fields{
491 DynamicClient: func() (dynamic.Interface, error) {
492 scheme := runtime.NewScheme()
493 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
494 Group: "registry.devfile.io",
495 Version: "v1alpha1",
496 Kind: "ClusterDevfileRegistriesListList",
497 }, &unstructured.UnstructuredList{})
498 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
499 Group: "registry.devfile.io",
500 Version: "v1alpha1",
501 Kind: "DevfileRegistriesListList",
502 }, &unstructured.UnstructuredList{})
503
504 var nsRegistry1 unstructured.Unstructured
505 err := nsRegistry1.UnmarshalJSON([]byte(`
506 {
507 "apiVersion": "registry.devfile.io/v1alpha1",
508 "kind": "DevfileRegistriesList",
509 "metadata": {
510 "name": "devfile-reg1",
511 "namespace": "my-ns"
512 },
513 "spec": {
514 "devfileRegistries": [
515 {
516 "name": "devfile-reg11",
517 "url": "https://devfile-reg11.example.com"
518 },
519 {
520 "name": "devfile-reg12",
521 "url": "https://devfile-reg12.example.com",
522 "skipTLSVerify": true
523 }
524 ]
525 }
526 }`))
527 if err != nil {
528 return nil, err
529 }
530
531 var nsRegistry2 unstructured.Unstructured
532 err = nsRegistry2.UnmarshalJSON([]byte(`
533 {
534 "apiVersion": "registry.devfile.io/v1alpha1",
535 "kind": "DevfileRegistriesList",
536 "metadata": {
537 "name": "devfile-reg2",
538 "namespace": "my-ns"
539 },
540 "spec": {
541 "devfileRegistries": [
542 {
543 "name": "devfile-reg21",
544 "url": "https://devfile-reg21.example.com"
545 },
546 {
547 "name": "devfile-reg22",
548 "url": "https://devfile-reg22.example.com",
549 "skipTLSVerify": true
550 }
551 ]
552 }
553 }`))
554 if err != nil {
555 return nil, err
556 }
557
558 client := fake.NewSimpleDynamicClient(scheme, &nsRegistry1, &nsRegistry2)
559 return client, nil
560 },
561 },
562 wantErr: false,
563 want: []api.Registry{
564 {Name: "devfile-reg11", URL: "https://devfile-reg11.example.com", Secure: true},
565 {Name: "devfile-reg12", URL: "https://devfile-reg12.example.com"},
566 {Name: "devfile-reg21", URL: "https://devfile-reg21.example.com", Secure: true},
567 {Name: "devfile-reg22", URL: "https://devfile-reg22.example.com"},
568 },
569 },
570 {
571 name: "with only cluster-scoped registries",
572 fields: fields{
573 DynamicClient: func() (dynamic.Interface, error) {
574 scheme := runtime.NewScheme()
575 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
576 Group: "registry.devfile.io",
577 Version: "v1alpha1",
578 Kind: "ClusterDevfileRegistriesListList",
579 }, &unstructured.UnstructuredList{})
580 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
581 Group: "registry.devfile.io",
582 Version: "v1alpha1",
583 Kind: "DevfileRegistriesListList",
584 }, &unstructured.UnstructuredList{})
585
586 var clusterRegistry1 unstructured.Unstructured
587 err := clusterRegistry1.UnmarshalJSON([]byte(`
588 {
589 "apiVersion": "registry.devfile.io/v1alpha1",
590 "kind": "ClusterDevfileRegistriesList",
591 "metadata": {
592 "name": "cluster-devfile-reg1"
593 },
594 "spec": {
595 "devfileRegistries": [
596 {
597 "name": "cluster-devfile-reg11",
598 "url": "https://cluster-devfile-reg11.example.com"
599 },
600 {
601 "name": "cluster-devfile-reg12",
602 "url": "https://cluster-devfile-reg12.example.com",
603 "skipTLSVerify": true
604 }
605 ]
606 }
607 }`))
608 if err != nil {
609 return nil, err
610 }
611
612 var clusterRegistry2 unstructured.Unstructured
613 err = clusterRegistry2.UnmarshalJSON([]byte(`
614 {
615 "apiVersion": "registry.devfile.io/v1alpha1",
616 "kind": "ClusterDevfileRegistriesList",
617 "metadata": {
618 "name": "cluster-devfile-reg2"
619 },
620 "spec": {
621 "devfileRegistries": [
622 {
623 "name": "cluster-devfile-reg21",
624 "url": "https://cluster-devfile-reg21.example.com"
625 },
626 {
627 "name": "cluster-devfile-reg22",
628 "url": "https://cluster-devfile-reg22.example.com",
629 "skipTLSVerify": true
630 }
631 ]
632 }
633 }`))
634 if err != nil {
635 return nil, err
636 }
637
638 client := fake.NewSimpleDynamicClient(scheme, &clusterRegistry1, &clusterRegistry2)
639 return client, nil
640 },
641 },
642 wantErr: false,
643 want: []api.Registry{
644 {
645 Name: "cluster-devfile-reg11",
646 URL: "https://cluster-devfile-reg11.example.com",
647 Secure: true,
648 },
649 {Name: "cluster-devfile-reg12", URL: "https://cluster-devfile-reg12.example.com"},
650 {
651 Name: "cluster-devfile-reg21",
652 URL: "https://cluster-devfile-reg21.example.com",
653 Secure: true,
654 },
655 {Name: "cluster-devfile-reg22", URL: "https://cluster-devfile-reg22.example.com"},
656 },
657 },
658 {
659 name: "namespaced registries have higher priority over cluster-scoped ones",
660 fields: fields{
661 DynamicClient: func() (dynamic.Interface, error) {
662 scheme := runtime.NewScheme()
663 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
664 Group: "registry.devfile.io",
665 Version: "v1alpha1",
666 Kind: "ClusterDevfileRegistriesListList",
667 }, &unstructured.UnstructuredList{})
668 scheme.AddKnownTypeWithName(schema.GroupVersionKind{
669 Group: "registry.devfile.io",
670 Version: "v1alpha1",
671 Kind: "DevfileRegistriesListList",
672 }, &unstructured.UnstructuredList{})
673
674 var nsRegistryNoUrl unstructured.Unstructured
675 err := nsRegistryNoUrl.UnmarshalJSON([]byte(`
676 {
677 "apiVersion": "registry.devfile.io/v1alpha1",
678 "kind": "DevfileRegistriesList",
679 "metadata": {
680 "name": "nsRegistryNoUrl",
681 "namespace": "my-ns"
682 },
683 "spec": {
684 "devfileRegistries": [
685 {
686 "name": "devfile-reg00",
687 "url": ""
688 }
689 ]
690 }
691 }`))
692 if err != nil {
693 return nil, err
694 }
695
696 var nsRegistry1 unstructured.Unstructured
697 err = nsRegistry1.UnmarshalJSON([]byte(`
698 {
699 "apiVersion": "registry.devfile.io/v1alpha1",
700 "kind": "DevfileRegistriesList",
701 "metadata": {
702 "name": "devfile-reg1",
703 "namespace": "my-ns"
704 },
705 "spec": {
706 "devfileRegistries": [
707 {
708 "name": "devfile-reg11",
709 "url": "https://devfile-reg11.example.com"
710 },
711 {
712 "name": "devfile-reg12",
713 "url": "https://devfile-reg12.example.com",
714 "skipTLSVerify": true
715 }
716 ]
717 }
718 }`))
719 if err != nil {
720 return nil, err
721 }
722
723 var nsRegistry2 unstructured.Unstructured
724 err = nsRegistry2.UnmarshalJSON([]byte(`
725 {
726 "apiVersion": "registry.devfile.io/v1alpha1",
727 "kind": "DevfileRegistriesList",
728 "metadata": {
729 "name": "devfile-reg2",
730 "namespace": "my-ns"
731 },
732 "spec": {
733 "devfileRegistries": [
734 {
735 "name": "devfile-reg21",
736 "url": "https://devfile-reg21.example.com"
737 },
738 {
739 "name": "devfile-reg22",
740 "url": "https://devfile-reg22.example.com",
741 "skipTLSVerify": true
742 }
743 ]
744 }
745 }`))
746 if err != nil {
747 return nil, err
748 }
749
750 var clusterRegistry1 unstructured.Unstructured
751 err = clusterRegistry1.UnmarshalJSON([]byte(`
752 {
753 "apiVersion": "registry.devfile.io/v1alpha1",
754 "kind": "ClusterDevfileRegistriesList",
755 "metadata": {
756 "name": "cluster-devfile-reg1"
757 },
758 "spec": {
759 "devfileRegistries": [
760 {
761 "name": "cluster-devfile-reg11",
762 "url": "https://cluster-devfile-reg11.example.com"
763 },
764 {
765 "name": "cluster-devfile-reg12",
766 "url": "https://cluster-devfile-reg12.example.com",
767 "skipTLSVerify": true
768 }
769 ]
770 }
771 }`))
772 if err != nil {
773 return nil, err
774 }
775
776 var clusterRegistry2 unstructured.Unstructured
777 err = clusterRegistry2.UnmarshalJSON([]byte(`
778 {
779 "apiVersion": "registry.devfile.io/v1alpha1",
780 "kind": "ClusterDevfileRegistriesList",
781 "metadata": {
782 "name": "cluster-devfile-reg2"
783 },
784 "spec": {
785 "devfileRegistries": [
786 {
787 "name": "cluster-devfile-reg21",
788 "url": "https://cluster-devfile-reg21.example.com"
789 },
790 {
791 "name": "cluster-devfile-reg22",
792 "url": "https://cluster-devfile-reg22.example.com",
793 "skipTLSVerify": true
794 }
795 ]
796 }
797 }`))
798 if err != nil {
799 return nil, err
800 }
801
802 var clusterRegistryNoUrl unstructured.Unstructured
803 err = clusterRegistryNoUrl.UnmarshalJSON([]byte(`
804 {
805 "apiVersion": "registry.devfile.io/v1alpha1",
806 "kind": "ClusterDevfileRegistriesList",
807 "metadata": {
808 "name": "clusterRegistryNoUrl"
809 },
810 "spec": {
811 "devfileRegistries": [
812 {
813 "name": "cluster-devfile-reg00",
814 "url": ""
815 }
816 ]
817 }
818 }`))
819 if err != nil {
820 return nil, err
821 }
822
823 client := fake.NewSimpleDynamicClient(
824 scheme,
825 &clusterRegistryNoUrl,
826 &nsRegistry1,
827 &clusterRegistry1,
828 &nsRegistryNoUrl,
829 &nsRegistry2,
830 &clusterRegistry2,
831 )
832
833 return client, nil
834 },
835 },
836 wantErr: false,
837 want: []api.Registry{
838 {Name: "devfile-reg11", URL: "https://devfile-reg11.example.com", Secure: true},
839 {Name: "devfile-reg12", URL: "https://devfile-reg12.example.com"},
840 {Name: "devfile-reg21", URL: "https://devfile-reg21.example.com", Secure: true},
841 {Name: "devfile-reg22", URL: "https://devfile-reg22.example.com"},
842 {
843 Name: "cluster-devfile-reg11",
844 URL: "https://cluster-devfile-reg11.example.com",
845 Secure: true,
846 },
847 {Name: "cluster-devfile-reg12", URL: "https://cluster-devfile-reg12.example.com"},
848 {
849 Name: "cluster-devfile-reg21",
850 URL: "https://cluster-devfile-reg21.example.com",
851 Secure: true,
852 },
853 {Name: "cluster-devfile-reg22", URL: "https://cluster-devfile-reg22.example.com"},
854 },
855 },
856 }
857 for _, tt := range tests {
858 t.Run(tt.name, func(t *testing.T) {
859 dynamicClient, err := tt.fields.DynamicClient()
860 if err != nil {
861 t.Errorf("unable to create dynamic client: %v", err)
862 return
863 }
864
865 c := &Client{
866 Namespace: tt.fields.Namespace,
867 DynamicClient: dynamicClient,
868 }
869 got, err := c.GetRegistryList()
870 if (err != nil) != tt.wantErr {
871 t.Errorf("Client.GetRegistryList() error = %v, wantErr %v", err, tt.wantErr)
872 return
873 }
874 if diff := cmp.Diff(tt.want, got, cmpopts.EquateEmpty()); diff != "" {
875 t.Errorf("Client.GetRegistryList() mismatch (-want +got):\n%s", diff)
876 }
877 })
878 }
879 }
880
View as plain text