2 * Copyright 2020 Intel Corporation, Inc
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
30 moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module"
32 pkgerrors "github.com/pkg/errors"
35 //Creating an embedded interface via anonymous variable
36 //This allows us to make mockDB satisfy the DatabaseConnection
37 //interface even if we are not implementing all the methods in it
38 type mockClusterManager struct {
39 // Items and err will be used to customize each test
40 // via a localized instantiation of mockClusterManager
41 ClusterProviderItems []moduleLib.ClusterProvider
42 ClusterItems []moduleLib.Cluster
43 ClusterContentItems []moduleLib.ClusterContent
44 ClusterLabelItems []moduleLib.ClusterLabel
45 ClusterKvPairsItems []moduleLib.ClusterKvPairs
49 func (m *mockClusterManager) CreateClusterProvider(inp moduleLib.ClusterProvider) (moduleLib.ClusterProvider, error) {
51 return moduleLib.ClusterProvider{}, m.Err
54 return m.ClusterProviderItems[0], nil
57 func (m *mockClusterManager) GetClusterProvider(name string) (moduleLib.ClusterProvider, error) {
59 return moduleLib.ClusterProvider{}, m.Err
62 return m.ClusterProviderItems[0], nil
65 func (m *mockClusterManager) GetClusterProviders() ([]moduleLib.ClusterProvider, error) {
67 return []moduleLib.ClusterProvider{}, m.Err
70 return m.ClusterProviderItems, nil
73 func (m *mockClusterManager) DeleteClusterProvider(name string) error {
77 func (m *mockClusterManager) CreateCluster(provider string, inp moduleLib.Cluster, inq moduleLib.ClusterContent) (moduleLib.Cluster, error) {
79 return moduleLib.Cluster{}, m.Err
82 return m.ClusterItems[0], nil
85 func (m *mockClusterManager) GetCluster(provider, name string) (moduleLib.Cluster, error) {
87 return moduleLib.Cluster{}, m.Err
90 return m.ClusterItems[0], nil
93 func (m *mockClusterManager) GetClusterContent(provider, name string) (moduleLib.ClusterContent, error) {
95 return moduleLib.ClusterContent{}, m.Err
98 return m.ClusterContentItems[0], nil
101 func (m *mockClusterManager) GetClusters(provider string) ([]moduleLib.Cluster, error) {
103 return []moduleLib.Cluster{}, m.Err
106 return m.ClusterItems, nil
109 func (m *mockClusterManager) DeleteCluster(provider, name string) error {
113 func (m *mockClusterManager) CreateClusterLabel(provider, cluster string, inp moduleLib.ClusterLabel) (moduleLib.ClusterLabel, error) {
115 return moduleLib.ClusterLabel{}, m.Err
118 return m.ClusterLabelItems[0], nil
121 func (m *mockClusterManager) GetClusterLabel(provider, cluster, label string) (moduleLib.ClusterLabel, error) {
123 return moduleLib.ClusterLabel{}, m.Err
126 return m.ClusterLabelItems[0], nil
129 func (m *mockClusterManager) GetClusterLabels(provider, cluster string) ([]moduleLib.ClusterLabel, error) {
131 return []moduleLib.ClusterLabel{}, m.Err
134 return m.ClusterLabelItems, nil
137 func (m *mockClusterManager) DeleteClusterLabel(provider, cluster, label string) error {
141 func (m *mockClusterManager) CreateClusterKvPairs(provider, cluster string, inp moduleLib.ClusterKvPairs) (moduleLib.ClusterKvPairs, error) {
143 return moduleLib.ClusterKvPairs{}, m.Err
146 return m.ClusterKvPairsItems[0], nil
149 func (m *mockClusterManager) GetClusterKvPairs(provider, cluster, kvpair string) (moduleLib.ClusterKvPairs, error) {
151 return moduleLib.ClusterKvPairs{}, m.Err
154 return m.ClusterKvPairsItems[0], nil
157 func (m *mockClusterManager) GetAllClusterKvPairs(provider, cluster string) ([]moduleLib.ClusterKvPairs, error) {
159 return []moduleLib.ClusterKvPairs{}, m.Err
162 return m.ClusterKvPairsItems, nil
165 func (m *mockClusterManager) DeleteClusterKvPairs(provider, cluster, kvpair string) error {
169 func TestClusterProviderCreateHandler(t *testing.T) {
170 testCases := []struct {
173 expected moduleLib.ClusterProvider
175 clusterClient *mockClusterManager
178 label: "Missing Cluster Provider Body Failure",
179 expectedCode: http.StatusBadRequest,
180 clusterClient: &mockClusterManager{},
183 label: "Create Cluster Provider",
184 expectedCode: http.StatusCreated,
185 reader: bytes.NewBuffer([]byte(`{
187 "name": "clusterProviderTest",
188 "description": "testClusterProvider",
189 "userData1": "some user data 1",
190 "userData2": "some user data 2"
193 expected: moduleLib.ClusterProvider{
194 Metadata: moduleLib.Metadata{
195 Name: "clusterProviderTest",
196 Description: "testClusterProvider",
197 UserData1: "some user data 1",
198 UserData2: "some user data 2",
201 clusterClient: &mockClusterManager{
202 //Items that will be returned by the mocked Client
203 ClusterProviderItems: []moduleLib.ClusterProvider{
205 Metadata: moduleLib.Metadata{
206 Name: "clusterProviderTest",
207 Description: "testClusterProvider",
208 UserData1: "some user data 1",
209 UserData2: "some user data 2",
216 label: "Missing ClusterProvider Name in Request Body",
217 reader: bytes.NewBuffer([]byte(`{
219 "description": "this is a test cluster provider",
220 "userData1": "some user data 1",
221 "userData2": "some user data 2"
224 expectedCode: http.StatusBadRequest,
225 clusterClient: &mockClusterManager{},
229 for _, testCase := range testCases {
230 t.Run(testCase.label, func(t *testing.T) {
231 request := httptest.NewRequest("POST", "/v2/cluster-providers", testCase.reader)
232 resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient, nil, nil, nil, nil))
234 //Check returned code
235 if resp.StatusCode != testCase.expectedCode {
236 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
239 //Check returned body only if statusCreated
240 if resp.StatusCode == http.StatusCreated {
241 got := moduleLib.ClusterProvider{}
242 json.NewDecoder(resp.Body).Decode(&got)
244 if reflect.DeepEqual(testCase.expected, got) == false {
245 t.Errorf("createHandler returned unexpected body: got %v;"+
246 " expected %v", got, testCase.expected)
253 func TestClusterProviderGetAllHandler(t *testing.T) {
255 testCases := []struct {
257 expected []moduleLib.ClusterProvider
260 clusterClient *mockClusterManager
263 label: "Get Cluster Provider",
264 expectedCode: http.StatusOK,
265 expected: []moduleLib.ClusterProvider{
267 Metadata: moduleLib.Metadata{
268 Name: "testClusterProvider1",
269 Description: "testClusterProvider 1 description",
270 UserData1: "some user data 1",
271 UserData2: "some user data 2",
275 Metadata: moduleLib.Metadata{
276 Name: "testClusterProvider2",
277 Description: "testClusterProvider 2 description",
278 UserData1: "some user data A",
279 UserData2: "some user data B",
283 clusterClient: &mockClusterManager{
284 //Items that will be returned by the mocked Client
285 ClusterProviderItems: []moduleLib.ClusterProvider{
287 Metadata: moduleLib.Metadata{
288 Name: "testClusterProvider1",
289 Description: "testClusterProvider 1 description",
290 UserData1: "some user data 1",
291 UserData2: "some user data 2",
295 Metadata: moduleLib.Metadata{
296 Name: "testClusterProvider2",
297 Description: "testClusterProvider 2 description",
298 UserData1: "some user data A",
299 UserData2: "some user data B",
307 for _, testCase := range testCases {
308 t.Run(testCase.label, func(t *testing.T) {
309 request := httptest.NewRequest("GET", "/v2/cluster-providers", nil)
310 resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient, nil, nil, nil, nil))
312 //Check returned code
313 if resp.StatusCode != testCase.expectedCode {
314 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
317 //Check returned body only if statusOK
318 if resp.StatusCode == http.StatusOK {
319 got := []moduleLib.ClusterProvider{}
320 json.NewDecoder(resp.Body).Decode(&got)
322 if reflect.DeepEqual(testCase.expected, got) == false {
323 t.Errorf("listHandler returned unexpected body: got %v;"+
324 " expected %v", got, testCase.expected)
331 func TestClusterProviderGetHandler(t *testing.T) {
333 testCases := []struct {
335 expected moduleLib.ClusterProvider
338 clusterClient *mockClusterManager
341 label: "Get Cluster Provider",
342 expectedCode: http.StatusOK,
343 expected: moduleLib.ClusterProvider{
344 Metadata: moduleLib.Metadata{
345 Name: "testClusterProvider",
346 Description: "testClusterProvider description",
347 UserData1: "some user data 1",
348 UserData2: "some user data 2",
351 name: "testClusterProvider",
352 clusterClient: &mockClusterManager{
353 //Items that will be returned by the mocked Client
354 ClusterProviderItems: []moduleLib.ClusterProvider{
356 Metadata: moduleLib.Metadata{
357 Name: "testClusterProvider",
358 Description: "testClusterProvider description",
359 UserData1: "some user data 1",
360 UserData2: "some user data 2",
367 label: "Get Non-Existing Cluster Provider",
368 expectedCode: http.StatusInternalServerError,
369 name: "nonexistingclusterprovider",
370 clusterClient: &mockClusterManager{
371 ClusterProviderItems: []moduleLib.ClusterProvider{},
372 Err: pkgerrors.New("Internal Error"),
377 for _, testCase := range testCases {
378 t.Run(testCase.label, func(t *testing.T) {
379 request := httptest.NewRequest("GET", "/v2/cluster-providers/"+testCase.name, nil)
380 resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient, nil, nil, nil, nil))
382 //Check returned code
383 if resp.StatusCode != testCase.expectedCode {
384 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
387 //Check returned body only if statusOK
388 if resp.StatusCode == http.StatusOK {
389 got := moduleLib.ClusterProvider{}
390 json.NewDecoder(resp.Body).Decode(&got)
392 if reflect.DeepEqual(testCase.expected, got) == false {
393 t.Errorf("listHandler returned unexpected body: got %v;"+
394 " expected %v", got, testCase.expected)
401 func TestClusterProviderDeleteHandler(t *testing.T) {
403 testCases := []struct {
408 clusterClient *mockClusterManager
411 label: "Delete Cluster Provider",
412 expectedCode: http.StatusNoContent,
413 name: "testClusterProvider",
414 clusterClient: &mockClusterManager{},
417 label: "Delete Non-Existing Cluster Provider",
418 expectedCode: http.StatusInternalServerError,
419 name: "testClusterProvider",
420 clusterClient: &mockClusterManager{
421 Err: pkgerrors.New("Internal Error"),
426 for _, testCase := range testCases {
427 t.Run(testCase.label, func(t *testing.T) {
428 request := httptest.NewRequest("DELETE", "/v2/cluster-providers/"+testCase.name, nil)
429 resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient, nil, nil, nil, nil))
431 //Check returned code
432 if resp.StatusCode != testCase.expectedCode {
433 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
439 func TestClusterCreateHandler(t *testing.T) {
440 testCases := []struct {
444 expected moduleLib.Cluster
446 clusterClient *mockClusterManager
449 label: "Missing Cluster Body Failure",
450 expectedCode: http.StatusBadRequest,
451 clusterClient: &mockClusterManager{},
454 label: "Create Cluster",
455 expectedCode: http.StatusCreated,
459 "name": "clusterTest",
460 "description": "this is test cluster",
461 "userData1": "some cluster data abc",
462 "userData2": "some cluster data def"
465 kubeconfig: `test contents
470 expected: moduleLib.Cluster{
471 Metadata: moduleLib.Metadata{
473 Description: "testCluster",
474 UserData1: "some user data 1",
475 UserData2: "some user data 2",
478 clusterClient: &mockClusterManager{
479 //Items that will be returned by the mocked Client
480 ClusterProviderItems: []moduleLib.ClusterProvider{
482 Metadata: moduleLib.Metadata{
483 Name: "clusterProvider1",
484 Description: "ClusterProvider 1 description",
485 UserData1: "some user data 1",
486 UserData2: "some user data 2",
490 ClusterItems: []moduleLib.Cluster{
492 Metadata: moduleLib.Metadata{
494 Description: "testCluster",
495 UserData1: "some user data 1",
496 UserData2: "some user data 2",
500 ClusterContentItems: []moduleLib.ClusterContent{
502 Kubeconfig: "dGVzdCBjb250ZW50cwpvZiBhIGZpbGUgYXR0YWNoZWQKdG8gdGhlIGNyZWF0aW9uCm9mIGNsdXN0ZXJUZXN0Cg==",
508 label: "Missing Cluster Name in Request Body",
509 expectedCode: http.StatusBadRequest,
513 "description": "this is test cluster",
514 "userData1": "some cluster data abc",
515 "userData2": "some cluster data def"
518 kubeconfig: `test contents
523 clusterClient: &mockClusterManager{},
527 for _, testCase := range testCases {
528 t.Run(testCase.label, func(t *testing.T) {
529 // Create the multipart test Request body
530 body := new(bytes.Buffer)
531 multiwr := multipart.NewWriter(body)
532 multiwr.SetBoundary("------------------------f77f80a7092eb312")
533 pw, _ := multiwr.CreatePart(textproto.MIMEHeader{"Content-Type": {"application/json"}, "Content-Disposition": {"form-data; name=metadata"}})
534 pw.Write([]byte(testCase.metadata))
535 pw, _ = multiwr.CreateFormFile("file", "kubeconfig")
536 pw.Write([]byte(testCase.kubeconfig))
539 request := httptest.NewRequest("POST", "/v2/cluster-providers/clusterProvider1/clusters", bytes.NewBuffer(body.Bytes()))
540 request.Header.Set("Content-Type", multiwr.FormDataContentType())
541 resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient, nil, nil, nil, nil))
543 //Check returned code
544 if resp.StatusCode != testCase.expectedCode {
545 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
548 //Check returned body only if statusCreated
549 if resp.StatusCode == http.StatusCreated {
550 got := moduleLib.Cluster{}
551 json.NewDecoder(resp.Body).Decode(&got)
553 if reflect.DeepEqual(testCase.expected, got) == false {
554 t.Errorf("createHandler returned unexpected body: got %v;"+
555 " expected %v", got, testCase.expected)
562 func TestClusterGetAllHandler(t *testing.T) {
564 testCases := []struct {
566 expected []moduleLib.Cluster
569 clusterClient *mockClusterManager
572 label: "Get Clusters",
573 expectedCode: http.StatusOK,
574 expected: []moduleLib.Cluster{
576 Metadata: moduleLib.Metadata{
577 Name: "testCluster1",
578 Description: "testCluster 1 description",
579 UserData1: "some user data 1",
580 UserData2: "some user data 2",
584 Metadata: moduleLib.Metadata{
585 Name: "testCluster2",
586 Description: "testCluster 2 description",
587 UserData1: "some user data A",
588 UserData2: "some user data B",
592 clusterClient: &mockClusterManager{
593 //Items that will be returned by the mocked Client
594 ClusterItems: []moduleLib.Cluster{
596 Metadata: moduleLib.Metadata{
597 Name: "testCluster1",
598 Description: "testCluster 1 description",
599 UserData1: "some user data 1",
600 UserData2: "some user data 2",
604 Metadata: moduleLib.Metadata{
605 Name: "testCluster2",
606 Description: "testCluster 2 description",
607 UserData1: "some user data A",
608 UserData2: "some user data B",
612 ClusterContentItems: []moduleLib.ClusterContent{
613 // content here doesn't matter - just needs to be present
615 Kubeconfig: "dGVzdCBjb250ZW50cwpvZiBhIGZpbGUgYXR0YWNoZWQKdG8gdGhlIGNyZWF0aW9uCm9mIGNsdXN0ZXJUZXN0Cg==",
618 Kubeconfig: "dGVzdCBjb250ZW50cwpvZiBhIGZpbGUgYXR0YWNoZWQKdG8gdGhlIGNyZWF0aW9uCm9mIGNsdXN0ZXJUZXN0Cg==",
625 for _, testCase := range testCases {
626 t.Run(testCase.label, func(t *testing.T) {
627 request := httptest.NewRequest("GET", "/v2/cluster-providers/clusterProvder1/clusters", nil)
628 resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient, nil, nil, nil, nil))
630 //Check returned code
631 if resp.StatusCode != testCase.expectedCode {
632 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
635 //Check returned body only if statusOK
636 if resp.StatusCode == http.StatusOK {
637 got := []moduleLib.Cluster{}
638 json.NewDecoder(resp.Body).Decode(&got)
640 if reflect.DeepEqual(testCase.expected, got) == false {
641 t.Errorf("listHandler returned unexpected body: got %v;"+
642 " expected %v", got, testCase.expected)
649 func TestClusterGetHandler(t *testing.T) {
651 testCases := []struct {
653 expected moduleLib.Cluster
657 clusterClient *mockClusterManager
660 label: "Get Cluster with Accept: application/json",
661 accept: "application/json",
662 expectedCode: http.StatusOK,
663 expected: moduleLib.Cluster{
664 Metadata: moduleLib.Metadata{
666 Description: "testCluster description",
667 UserData1: "some user data 1",
668 UserData2: "some user data 2",
672 clusterClient: &mockClusterManager{
673 //Items that will be returned by the mocked Client
674 ClusterItems: []moduleLib.Cluster{
676 Metadata: moduleLib.Metadata{
678 Description: "testCluster description",
679 UserData1: "some user data 1",
680 UserData2: "some user data 2",
684 ClusterContentItems: []moduleLib.ClusterContent{
686 Kubeconfig: "dGVzdCBjb250ZW50cwpvZiBhIGZpbGUgYXR0YWNoZWQKdG8gdGhlIGNyZWF0aW9uCm9mIGNsdXN0ZXJUZXN0Cg==",
692 label: "Get Non-Existing Cluster",
693 accept: "application/json",
694 expectedCode: http.StatusInternalServerError,
695 name: "nonexistingcluster",
696 clusterClient: &mockClusterManager{
697 ClusterItems: []moduleLib.Cluster{},
698 Err: pkgerrors.New("Internal Error"),
703 for _, testCase := range testCases {
704 t.Run(testCase.label, func(t *testing.T) {
705 request := httptest.NewRequest("GET", "/v2/cluster-providers/clusterProvider1/clusters/"+testCase.name, nil)
706 if len(testCase.accept) > 0 {
707 request.Header.Set("Accept", testCase.accept)
709 resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient, nil, nil, nil, nil))
711 //Check returned code
712 if resp.StatusCode != testCase.expectedCode {
713 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
716 //Check returned body only if statusOK
717 if resp.StatusCode == http.StatusOK {
718 got := moduleLib.Cluster{}
719 json.NewDecoder(resp.Body).Decode(&got)
721 if reflect.DeepEqual(testCase.expected, got) == false {
722 t.Errorf("listHandler returned unexpected body: got %v;"+
723 " expected %v", got, testCase.expected)
730 func TestClusterGetContentHandler(t *testing.T) {
732 testCases := []struct {
738 clusterClient *mockClusterManager
741 label: "Get Cluster Content with Accept: application/octet-stream",
742 accept: "application/octet-stream",
743 expectedCode: http.StatusOK,
744 expected: `test contents
750 clusterClient: &mockClusterManager{
751 //Items that will be returned by the mocked Client
752 ClusterItems: []moduleLib.Cluster{
754 Metadata: moduleLib.Metadata{
756 Description: "testCluster description",
757 UserData1: "some user data 1",
758 UserData2: "some user data 2",
762 ClusterContentItems: []moduleLib.ClusterContent{
764 Kubeconfig: "dGVzdCBjb250ZW50cwpvZiBhIGZpbGUgYXR0YWNoZWQKdG8gdGhlIGNyZWF0aW9uCm9mIGNsdXN0ZXJUZXN0Cg==",
770 label: "Get Non-Existing Cluster",
771 accept: "application/octet-stream",
772 expectedCode: http.StatusInternalServerError,
773 name: "nonexistingcluster",
774 clusterClient: &mockClusterManager{
775 ClusterItems: []moduleLib.Cluster{},
776 Err: pkgerrors.New("Internal Error"),
781 for _, testCase := range testCases {
782 t.Run(testCase.label, func(t *testing.T) {
783 request := httptest.NewRequest("GET", "/v2/cluster-providers/clusterProvider1/clusters/"+testCase.name, nil)
784 if len(testCase.accept) > 0 {
785 request.Header.Set("Accept", testCase.accept)
787 resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient, nil, nil, nil, nil))
789 //Check returned code
790 if resp.StatusCode != testCase.expectedCode {
791 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
794 //Check returned body only if statusOK
795 if resp.StatusCode == http.StatusOK {
796 body := new(bytes.Buffer)
797 body.ReadFrom(resp.Body)
800 if reflect.DeepEqual(testCase.expected, got) == false {
801 t.Errorf("listHandler returned unexpected body: got %v;"+
802 " expected %v", got, testCase.expected)
809 func TestClusterDeleteHandler(t *testing.T) {
811 testCases := []struct {
816 clusterClient *mockClusterManager
819 label: "Delete Cluster",
820 expectedCode: http.StatusNoContent,
822 clusterClient: &mockClusterManager{},
825 label: "Delete Non-Existing Cluster",
826 expectedCode: http.StatusInternalServerError,
828 clusterClient: &mockClusterManager{
829 Err: pkgerrors.New("Internal Error"),
834 for _, testCase := range testCases {
835 t.Run(testCase.label, func(t *testing.T) {
836 request := httptest.NewRequest("DELETE", "/v2/cluster-providers/clusterProvider1/clusters/"+testCase.name, nil)
837 resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient, nil, nil, nil, nil))
839 //Check returned code
840 if resp.StatusCode != testCase.expectedCode {
841 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
847 func TestClusterLabelCreateHandler(t *testing.T) {
848 testCases := []struct {
851 expected moduleLib.ClusterLabel
853 clusterClient *mockClusterManager
856 label: "Missing Cluster Label Body Failure",
857 expectedCode: http.StatusBadRequest,
858 clusterClient: &mockClusterManager{},
861 label: "Create Cluster Label",
862 expectedCode: http.StatusCreated,
863 reader: bytes.NewBuffer([]byte(`{
864 "label-name": "test-label"
866 expected: moduleLib.ClusterLabel{
867 LabelName: "test-label",
869 clusterClient: &mockClusterManager{
870 //Items that will be returned by the mocked Client
871 ClusterLabelItems: []moduleLib.ClusterLabel{
873 LabelName: "test-label",
880 for _, testCase := range testCases {
881 t.Run(testCase.label, func(t *testing.T) {
882 request := httptest.NewRequest("POST", "/v2/cluster-providers/cp1/clusters/cl1/labels", testCase.reader)
883 resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient, nil, nil, nil, nil))
885 //Check returned code
886 if resp.StatusCode != testCase.expectedCode {
887 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
890 //Check returned body only if statusCreated
891 if resp.StatusCode == http.StatusCreated {
892 got := moduleLib.ClusterLabel{}
893 json.NewDecoder(resp.Body).Decode(&got)
895 if reflect.DeepEqual(testCase.expected, got) == false {
896 t.Errorf("createHandler returned unexpected body: got %v;"+
897 " expected %v", got, testCase.expected)
904 func TestClusterLabelsGetHandler(t *testing.T) {
906 testCases := []struct {
908 expected []moduleLib.ClusterLabel
911 clusterClient *mockClusterManager
914 label: "Get Cluster Labels",
915 expectedCode: http.StatusOK,
916 expected: []moduleLib.ClusterLabel{
918 LabelName: "test-label1",
921 LabelName: "test-label-two",
924 LabelName: "test-label-3",
927 clusterClient: &mockClusterManager{
928 //Items that will be returned by the mocked Client
929 ClusterLabelItems: []moduleLib.ClusterLabel{
931 LabelName: "test-label1",
934 LabelName: "test-label-two",
937 LabelName: "test-label-3",
944 for _, testCase := range testCases {
945 t.Run(testCase.label, func(t *testing.T) {
946 request := httptest.NewRequest("GET", "/v2/cluster-providers/cp1/clusters/cl1/labels", nil)
947 resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient, nil, nil, nil, nil))
949 //Check returned code
950 if resp.StatusCode != testCase.expectedCode {
951 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
954 //Check returned body only if statusOK
955 if resp.StatusCode == http.StatusOK {
956 got := []moduleLib.ClusterLabel{}
957 json.NewDecoder(resp.Body).Decode(&got)
959 if reflect.DeepEqual(testCase.expected, got) == false {
960 t.Errorf("listHandler returned unexpected body: got %v;"+
961 " expected %v", got, testCase.expected)
968 func TestClusterLabelGetHandler(t *testing.T) {
970 testCases := []struct {
972 expected moduleLib.ClusterLabel
975 clusterClient *mockClusterManager
978 label: "Get Cluster Label",
979 expectedCode: http.StatusOK,
980 expected: moduleLib.ClusterLabel{
981 LabelName: "testlabel",
984 clusterClient: &mockClusterManager{
985 //Items that will be returned by the mocked Client
986 ClusterLabelItems: []moduleLib.ClusterLabel{
988 LabelName: "testlabel",
994 label: "Get Non-Existing Cluster Label",
995 expectedCode: http.StatusInternalServerError,
996 name: "nonexistingclusterlabel",
997 clusterClient: &mockClusterManager{
998 ClusterLabelItems: []moduleLib.ClusterLabel{},
999 Err: pkgerrors.New("Internal Error"),
1004 for _, testCase := range testCases {
1005 t.Run(testCase.label, func(t *testing.T) {
1006 request := httptest.NewRequest("GET", "/v2/cluster-providers/clusterProvider1/clusters/cl1/labels/"+testCase.name, nil)
1007 resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient, nil, nil, nil, nil))
1009 //Check returned code
1010 if resp.StatusCode != testCase.expectedCode {
1011 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
1014 //Check returned body only if statusOK
1015 if resp.StatusCode == http.StatusOK {
1016 got := moduleLib.ClusterLabel{}
1017 json.NewDecoder(resp.Body).Decode(&got)
1019 if reflect.DeepEqual(testCase.expected, got) == false {
1020 t.Errorf("listHandler returned unexpected body: got %v;"+
1021 " expected %v", got, testCase.expected)
1028 func TestClusterLabelDeleteHandler(t *testing.T) {
1030 testCases := []struct {
1035 clusterClient *mockClusterManager
1038 label: "Delete Cluster Label",
1039 expectedCode: http.StatusNoContent,
1040 name: "testClusterLabel",
1041 clusterClient: &mockClusterManager{},
1044 label: "Delete Non-Existing Cluster Label",
1045 expectedCode: http.StatusInternalServerError,
1046 name: "testClusterLabel",
1047 clusterClient: &mockClusterManager{
1048 Err: pkgerrors.New("Internal Error"),
1053 for _, testCase := range testCases {
1054 t.Run(testCase.label, func(t *testing.T) {
1055 request := httptest.NewRequest("DELETE", "/v2/cluster-providers/cp1/clusters/cl1/labels/"+testCase.name, nil)
1056 resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient, nil, nil, nil, nil))
1058 //Check returned code
1059 if resp.StatusCode != testCase.expectedCode {
1060 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
1066 func TestClusterKvPairsCreateHandler(t *testing.T) {
1067 testCases := []struct {
1070 expected moduleLib.ClusterKvPairs
1072 clusterClient *mockClusterManager
1075 label: "Missing Cluster KvPairs Body Failure",
1076 expectedCode: http.StatusBadRequest,
1077 clusterClient: &mockClusterManager{},
1080 label: "Create Cluster KvPairs",
1081 expectedCode: http.StatusCreated,
1082 reader: bytes.NewBuffer([]byte(`{
1084 "name": "ClusterKvPair1",
1085 "description": "test cluster kv pairs",
1086 "userData1": "some user data 1",
1087 "userData2": "some user data 2"
1100 expected: moduleLib.ClusterKvPairs{
1101 Metadata: moduleLib.Metadata{
1102 Name: "ClusterKvPair1",
1103 Description: "test cluster kv pairs",
1104 UserData1: "some user data 1",
1105 UserData2: "some user data 2",
1107 Spec: moduleLib.ClusterKvSpec{
1108 Kv: []map[string]interface{}{
1118 clusterClient: &mockClusterManager{
1119 //Items that will be returned by the mocked Client
1120 ClusterKvPairsItems: []moduleLib.ClusterKvPairs{
1122 Metadata: moduleLib.Metadata{
1123 Name: "ClusterKvPair1",
1124 Description: "test cluster kv pairs",
1125 UserData1: "some user data 1",
1126 UserData2: "some user data 2",
1128 Spec: moduleLib.ClusterKvSpec{
1129 Kv: []map[string]interface{}{
1144 for _, testCase := range testCases {
1145 t.Run(testCase.label, func(t *testing.T) {
1146 request := httptest.NewRequest("POST", "/v2/cluster-providers/cp1/clusters/cl1/kv-pairs", testCase.reader)
1147 resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient, nil, nil, nil, nil))
1149 //Check returned code
1150 if resp.StatusCode != testCase.expectedCode {
1151 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
1154 //Check returned body only if statusCreated
1155 if resp.StatusCode == http.StatusCreated {
1156 got := moduleLib.ClusterKvPairs{}
1157 json.NewDecoder(resp.Body).Decode(&got)
1159 if reflect.DeepEqual(testCase.expected, got) == false {
1160 t.Errorf("createHandler returned unexpected body: got %v;"+
1161 " expected %v", got, testCase.expected)
1168 func TestClusterKvPairsGetAllHandler(t *testing.T) {
1170 testCases := []struct {
1172 expected []moduleLib.ClusterKvPairs
1173 name, version string
1175 clusterClient *mockClusterManager
1178 label: "Get Cluster KvPairs",
1179 expectedCode: http.StatusOK,
1180 expected: []moduleLib.ClusterKvPairs{
1182 Metadata: moduleLib.Metadata{
1183 Name: "ClusterKvPair1",
1184 Description: "test cluster kv pairs",
1185 UserData1: "some user data 1",
1186 UserData2: "some user data 2",
1188 Spec: moduleLib.ClusterKvSpec{
1189 Kv: []map[string]interface{}{
1200 Metadata: moduleLib.Metadata{
1201 Name: "ClusterKvPair2",
1202 Description: "test cluster kv pairs",
1203 UserData1: "some user data A",
1204 UserData2: "some user data B",
1206 Spec: moduleLib.ClusterKvSpec{
1207 Kv: []map[string]interface{}{
1218 clusterClient: &mockClusterManager{
1219 //Items that will be returned by the mocked Client
1220 ClusterKvPairsItems: []moduleLib.ClusterKvPairs{
1222 Metadata: moduleLib.Metadata{
1223 Name: "ClusterKvPair1",
1224 Description: "test cluster kv pairs",
1225 UserData1: "some user data 1",
1226 UserData2: "some user data 2",
1228 Spec: moduleLib.ClusterKvSpec{
1229 Kv: []map[string]interface{}{
1240 Metadata: moduleLib.Metadata{
1241 Name: "ClusterKvPair2",
1242 Description: "test cluster kv pairs",
1243 UserData1: "some user data A",
1244 UserData2: "some user data B",
1246 Spec: moduleLib.ClusterKvSpec{
1247 Kv: []map[string]interface{}{
1262 for _, testCase := range testCases {
1263 t.Run(testCase.label, func(t *testing.T) {
1264 request := httptest.NewRequest("GET", "/v2/cluster-providers/cp1/clusters/cl1/kv-pairs", nil)
1265 resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient, nil, nil, nil, nil))
1267 //Check returned code
1268 if resp.StatusCode != testCase.expectedCode {
1269 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
1272 //Check returned body only if statusOK
1273 if resp.StatusCode == http.StatusOK {
1274 got := []moduleLib.ClusterKvPairs{}
1275 json.NewDecoder(resp.Body).Decode(&got)
1277 if reflect.DeepEqual(testCase.expected, got) == false {
1278 t.Errorf("listHandler returned unexpected body: got %v;"+
1279 " expected %v", got, testCase.expected)
1286 func TestClusterKvPairsGetHandler(t *testing.T) {
1288 testCases := []struct {
1290 expected moduleLib.ClusterKvPairs
1291 name, version string
1293 clusterClient *mockClusterManager
1296 label: "Get Cluster KV Pairs",
1297 expectedCode: http.StatusOK,
1298 expected: moduleLib.ClusterKvPairs{
1299 Metadata: moduleLib.Metadata{
1300 Name: "ClusterKvPair2",
1301 Description: "test cluster kv pairs",
1302 UserData1: "some user data A",
1303 UserData2: "some user data B",
1305 Spec: moduleLib.ClusterKvSpec{
1306 Kv: []map[string]interface{}{
1316 name: "ClusterKvPair2",
1317 clusterClient: &mockClusterManager{
1318 //Items that will be returned by the mocked Client
1319 ClusterKvPairsItems: []moduleLib.ClusterKvPairs{
1321 Metadata: moduleLib.Metadata{
1322 Name: "ClusterKvPair2",
1323 Description: "test cluster kv pairs",
1324 UserData1: "some user data A",
1325 UserData2: "some user data B",
1327 Spec: moduleLib.ClusterKvSpec{
1328 Kv: []map[string]interface{}{
1342 label: "Get Non-Existing Cluster KV Pairs",
1343 expectedCode: http.StatusInternalServerError,
1344 name: "nonexistingclusterkvpairs",
1345 clusterClient: &mockClusterManager{
1346 ClusterKvPairsItems: []moduleLib.ClusterKvPairs{},
1347 Err: pkgerrors.New("Internal Error"),
1352 for _, testCase := range testCases {
1353 t.Run(testCase.label, func(t *testing.T) {
1354 request := httptest.NewRequest("GET", "/v2/cluster-providers/clusterProvider1/clusters/cl1/kv-pairs/"+testCase.name, nil)
1355 resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient, nil, nil, nil, nil))
1357 //Check returned code
1358 if resp.StatusCode != testCase.expectedCode {
1359 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
1362 //Check returned body only if statusOK
1363 if resp.StatusCode == http.StatusOK {
1364 got := moduleLib.ClusterKvPairs{}
1365 json.NewDecoder(resp.Body).Decode(&got)
1367 if reflect.DeepEqual(testCase.expected, got) == false {
1368 t.Errorf("listHandler returned unexpected body: got %v;"+
1369 " expected %v", got, testCase.expected)
1376 func TestClusterKvPairsDeleteHandler(t *testing.T) {
1378 testCases := []struct {
1383 clusterClient *mockClusterManager
1386 label: "Delete Cluster KV Pairs",
1387 expectedCode: http.StatusNoContent,
1388 name: "testClusterKvPairs",
1389 clusterClient: &mockClusterManager{},
1392 label: "Delete Non-Existing Cluster KV Pairs",
1393 expectedCode: http.StatusInternalServerError,
1394 name: "testClusterKvPairs",
1395 clusterClient: &mockClusterManager{
1396 Err: pkgerrors.New("Internal Error"),
1401 for _, testCase := range testCases {
1402 t.Run(testCase.label, func(t *testing.T) {
1403 request := httptest.NewRequest("DELETE", "/v2/cluster-providers/cp1/clusters/cl1/kv-pairs/"+testCase.name, nil)
1404 resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient, nil, nil, nil, nil))
1406 //Check returned code
1407 if resp.StatusCode != testCase.expectedCode {
1408 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)