Implemented AppIntents and GenPlaceIntents
[multicloud/k8s.git] / src / orchestrator / api / clusterhandler_test.go
1 /*
2  * Copyright 2020 Intel Corporation, Inc
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package api
18
19 import (
20         "bytes"
21         "encoding/json"
22         "io"
23         "mime/multipart"
24         "net/http"
25         "net/http/httptest"
26         "net/textproto"
27         "reflect"
28         "testing"
29
30         moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module"
31
32         pkgerrors "github.com/pkg/errors"
33 )
34
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
46         Err                  error
47 }
48
49 func (m *mockClusterManager) CreateClusterProvider(inp moduleLib.ClusterProvider) (moduleLib.ClusterProvider, error) {
50         if m.Err != nil {
51                 return moduleLib.ClusterProvider{}, m.Err
52         }
53
54         return m.ClusterProviderItems[0], nil
55 }
56
57 func (m *mockClusterManager) GetClusterProvider(name string) (moduleLib.ClusterProvider, error) {
58         if m.Err != nil {
59                 return moduleLib.ClusterProvider{}, m.Err
60         }
61
62         return m.ClusterProviderItems[0], nil
63 }
64
65 func (m *mockClusterManager) GetClusterProviders() ([]moduleLib.ClusterProvider, error) {
66         if m.Err != nil {
67                 return []moduleLib.ClusterProvider{}, m.Err
68         }
69
70         return m.ClusterProviderItems, nil
71 }
72
73 func (m *mockClusterManager) DeleteClusterProvider(name string) error {
74         return m.Err
75 }
76
77 func (m *mockClusterManager) CreateCluster(provider string, inp moduleLib.Cluster, inq moduleLib.ClusterContent) (moduleLib.Cluster, error) {
78         if m.Err != nil {
79                 return moduleLib.Cluster{}, m.Err
80         }
81
82         return m.ClusterItems[0], nil
83 }
84
85 func (m *mockClusterManager) GetCluster(provider, name string) (moduleLib.Cluster, error) {
86         if m.Err != nil {
87                 return moduleLib.Cluster{}, m.Err
88         }
89
90         return m.ClusterItems[0], nil
91 }
92
93 func (m *mockClusterManager) GetClusterContent(provider, name string) (moduleLib.ClusterContent, error) {
94         if m.Err != nil {
95                 return moduleLib.ClusterContent{}, m.Err
96         }
97
98         return m.ClusterContentItems[0], nil
99 }
100
101 func (m *mockClusterManager) GetClusters(provider string) ([]moduleLib.Cluster, error) {
102         if m.Err != nil {
103                 return []moduleLib.Cluster{}, m.Err
104         }
105
106         return m.ClusterItems, nil
107 }
108
109 func (m *mockClusterManager) DeleteCluster(provider, name string) error {
110         return m.Err
111 }
112
113 func (m *mockClusterManager) CreateClusterLabel(provider, cluster string, inp moduleLib.ClusterLabel) (moduleLib.ClusterLabel, error) {
114         if m.Err != nil {
115                 return moduleLib.ClusterLabel{}, m.Err
116         }
117
118         return m.ClusterLabelItems[0], nil
119 }
120
121 func (m *mockClusterManager) GetClusterLabel(provider, cluster, label string) (moduleLib.ClusterLabel, error) {
122         if m.Err != nil {
123                 return moduleLib.ClusterLabel{}, m.Err
124         }
125
126         return m.ClusterLabelItems[0], nil
127 }
128
129 func (m *mockClusterManager) GetClusterLabels(provider, cluster string) ([]moduleLib.ClusterLabel, error) {
130         if m.Err != nil {
131                 return []moduleLib.ClusterLabel{}, m.Err
132         }
133
134         return m.ClusterLabelItems, nil
135 }
136
137 func (m *mockClusterManager) DeleteClusterLabel(provider, cluster, label string) error {
138         return m.Err
139 }
140
141 func (m *mockClusterManager) CreateClusterKvPairs(provider, cluster string, inp moduleLib.ClusterKvPairs) (moduleLib.ClusterKvPairs, error) {
142         if m.Err != nil {
143                 return moduleLib.ClusterKvPairs{}, m.Err
144         }
145
146         return m.ClusterKvPairsItems[0], nil
147 }
148
149 func (m *mockClusterManager) GetClusterKvPairs(provider, cluster, kvpair string) (moduleLib.ClusterKvPairs, error) {
150         if m.Err != nil {
151                 return moduleLib.ClusterKvPairs{}, m.Err
152         }
153
154         return m.ClusterKvPairsItems[0], nil
155 }
156
157 func (m *mockClusterManager) GetAllClusterKvPairs(provider, cluster string) ([]moduleLib.ClusterKvPairs, error) {
158         if m.Err != nil {
159                 return []moduleLib.ClusterKvPairs{}, m.Err
160         }
161
162         return m.ClusterKvPairsItems, nil
163 }
164
165 func (m *mockClusterManager) DeleteClusterKvPairs(provider, cluster, kvpair string) error {
166         return m.Err
167 }
168
169 func TestClusterProviderCreateHandler(t *testing.T) {
170         testCases := []struct {
171                 label         string
172                 reader        io.Reader
173                 expected      moduleLib.ClusterProvider
174                 expectedCode  int
175                 clusterClient *mockClusterManager
176         }{
177                 {
178                         label:         "Missing Cluster Provider Body Failure",
179                         expectedCode:  http.StatusBadRequest,
180                         clusterClient: &mockClusterManager{},
181                 },
182                 {
183                         label:        "Create Cluster Provider",
184                         expectedCode: http.StatusCreated,
185                         reader: bytes.NewBuffer([]byte(`{
186                                         "metadata": {
187                                                 "name": "clusterProviderTest",
188                                                 "description": "testClusterProvider",
189                                                 "userData1": "some user data 1",
190                                                 "userData2": "some user data 2"
191                                         }
192                                 }`)),
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",
199                                 },
200                         },
201                         clusterClient: &mockClusterManager{
202                                 //Items that will be returned by the mocked Client
203                                 ClusterProviderItems: []moduleLib.ClusterProvider{
204                                         {
205                                                 Metadata: moduleLib.Metadata{
206                                                         Name:        "clusterProviderTest",
207                                                         Description: "testClusterProvider",
208                                                         UserData1:   "some user data 1",
209                                                         UserData2:   "some user data 2",
210                                                 },
211                                         },
212                                 },
213                         },
214                 },
215                 {
216                         label: "Missing ClusterProvider Name in Request Body",
217                         reader: bytes.NewBuffer([]byte(`{
218                                         "metadata": {
219                                                 "description": "this is a test cluster provider",
220                                                 "userData1": "some user data 1",
221                                                 "userData2": "some user data 2"
222                                         }
223                                 }`)),
224                         expectedCode:  http.StatusBadRequest,
225                         clusterClient: &mockClusterManager{},
226                 },
227         }
228
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))
233
234                         //Check returned code
235                         if resp.StatusCode != testCase.expectedCode {
236                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
237                         }
238
239                         //Check returned body only if statusCreated
240                         if resp.StatusCode == http.StatusCreated {
241                                 got := moduleLib.ClusterProvider{}
242                                 json.NewDecoder(resp.Body).Decode(&got)
243
244                                 if reflect.DeepEqual(testCase.expected, got) == false {
245                                         t.Errorf("createHandler returned unexpected body: got %v;"+
246                                                 " expected %v", got, testCase.expected)
247                                 }
248                         }
249                 })
250         }
251 }
252
253 func TestClusterProviderGetAllHandler(t *testing.T) {
254
255         testCases := []struct {
256                 label         string
257                 expected      []moduleLib.ClusterProvider
258                 name, version string
259                 expectedCode  int
260                 clusterClient *mockClusterManager
261         }{
262                 {
263                         label:        "Get Cluster Provider",
264                         expectedCode: http.StatusOK,
265                         expected: []moduleLib.ClusterProvider{
266                                 {
267                                         Metadata: moduleLib.Metadata{
268                                                 Name:        "testClusterProvider1",
269                                                 Description: "testClusterProvider 1 description",
270                                                 UserData1:   "some user data 1",
271                                                 UserData2:   "some user data 2",
272                                         },
273                                 },
274                                 {
275                                         Metadata: moduleLib.Metadata{
276                                                 Name:        "testClusterProvider2",
277                                                 Description: "testClusterProvider 2 description",
278                                                 UserData1:   "some user data A",
279                                                 UserData2:   "some user data B",
280                                         },
281                                 },
282                         },
283                         clusterClient: &mockClusterManager{
284                                 //Items that will be returned by the mocked Client
285                                 ClusterProviderItems: []moduleLib.ClusterProvider{
286                                         {
287                                                 Metadata: moduleLib.Metadata{
288                                                         Name:        "testClusterProvider1",
289                                                         Description: "testClusterProvider 1 description",
290                                                         UserData1:   "some user data 1",
291                                                         UserData2:   "some user data 2",
292                                                 },
293                                         },
294                                         {
295                                                 Metadata: moduleLib.Metadata{
296                                                         Name:        "testClusterProvider2",
297                                                         Description: "testClusterProvider 2 description",
298                                                         UserData1:   "some user data A",
299                                                         UserData2:   "some user data B",
300                                                 },
301                                         },
302                                 },
303                         },
304                 },
305         }
306
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))
311
312                         //Check returned code
313                         if resp.StatusCode != testCase.expectedCode {
314                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
315                         }
316
317                         //Check returned body only if statusOK
318                         if resp.StatusCode == http.StatusOK {
319                                 got := []moduleLib.ClusterProvider{}
320                                 json.NewDecoder(resp.Body).Decode(&got)
321
322                                 if reflect.DeepEqual(testCase.expected, got) == false {
323                                         t.Errorf("listHandler returned unexpected body: got %v;"+
324                                                 " expected %v", got, testCase.expected)
325                                 }
326                         }
327                 })
328         }
329 }
330
331 func TestClusterProviderGetHandler(t *testing.T) {
332
333         testCases := []struct {
334                 label         string
335                 expected      moduleLib.ClusterProvider
336                 name, version string
337                 expectedCode  int
338                 clusterClient *mockClusterManager
339         }{
340                 {
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",
349                                 },
350                         },
351                         name: "testClusterProvider",
352                         clusterClient: &mockClusterManager{
353                                 //Items that will be returned by the mocked Client
354                                 ClusterProviderItems: []moduleLib.ClusterProvider{
355                                         {
356                                                 Metadata: moduleLib.Metadata{
357                                                         Name:        "testClusterProvider",
358                                                         Description: "testClusterProvider description",
359                                                         UserData1:   "some user data 1",
360                                                         UserData2:   "some user data 2",
361                                                 },
362                                         },
363                                 },
364                         },
365                 },
366                 {
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"),
373                         },
374                 },
375         }
376
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))
381
382                         //Check returned code
383                         if resp.StatusCode != testCase.expectedCode {
384                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
385                         }
386
387                         //Check returned body only if statusOK
388                         if resp.StatusCode == http.StatusOK {
389                                 got := moduleLib.ClusterProvider{}
390                                 json.NewDecoder(resp.Body).Decode(&got)
391
392                                 if reflect.DeepEqual(testCase.expected, got) == false {
393                                         t.Errorf("listHandler returned unexpected body: got %v;"+
394                                                 " expected %v", got, testCase.expected)
395                                 }
396                         }
397                 })
398         }
399 }
400
401 func TestClusterProviderDeleteHandler(t *testing.T) {
402
403         testCases := []struct {
404                 label         string
405                 name          string
406                 version       string
407                 expectedCode  int
408                 clusterClient *mockClusterManager
409         }{
410                 {
411                         label:         "Delete Cluster Provider",
412                         expectedCode:  http.StatusNoContent,
413                         name:          "testClusterProvider",
414                         clusterClient: &mockClusterManager{},
415                 },
416                 {
417                         label:        "Delete Non-Existing Cluster Provider",
418                         expectedCode: http.StatusInternalServerError,
419                         name:         "testClusterProvider",
420                         clusterClient: &mockClusterManager{
421                                 Err: pkgerrors.New("Internal Error"),
422                         },
423                 },
424         }
425
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))
430
431                         //Check returned code
432                         if resp.StatusCode != testCase.expectedCode {
433                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
434                         }
435                 })
436         }
437 }
438
439 func TestClusterCreateHandler(t *testing.T) {
440         testCases := []struct {
441                 label         string
442                 metadata      string
443                 kubeconfig    string
444                 expected      moduleLib.Cluster
445                 expectedCode  int
446                 clusterClient *mockClusterManager
447         }{
448                 {
449                         label:         "Missing Cluster Body Failure",
450                         expectedCode:  http.StatusBadRequest,
451                         clusterClient: &mockClusterManager{},
452                 },
453                 {
454                         label:        "Create Cluster",
455                         expectedCode: http.StatusCreated,
456                         metadata: `
457 {
458  "metadata": {
459   "name": "clusterTest",
460   "description": "this is test cluster",
461   "userData1": "some cluster data abc",
462   "userData2": "some cluster data def"
463  }
464 }`,
465                         kubeconfig: `test contents
466 of a file attached
467 to the creation
468 of clusterTest
469 `,
470                         expected: moduleLib.Cluster{
471                                 Metadata: moduleLib.Metadata{
472                                         Name:        "clusterTest",
473                                         Description: "testCluster",
474                                         UserData1:   "some user data 1",
475                                         UserData2:   "some user data 2",
476                                 },
477                         },
478                         clusterClient: &mockClusterManager{
479                                 //Items that will be returned by the mocked Client
480                                 ClusterProviderItems: []moduleLib.ClusterProvider{
481                                         {
482                                                 Metadata: moduleLib.Metadata{
483                                                         Name:        "clusterProvider1",
484                                                         Description: "ClusterProvider 1 description",
485                                                         UserData1:   "some user data 1",
486                                                         UserData2:   "some user data 2",
487                                                 },
488                                         },
489                                 },
490                                 ClusterItems: []moduleLib.Cluster{
491                                         {
492                                                 Metadata: moduleLib.Metadata{
493                                                         Name:        "clusterTest",
494                                                         Description: "testCluster",
495                                                         UserData1:   "some user data 1",
496                                                         UserData2:   "some user data 2",
497                                                 },
498                                         },
499                                 },
500                                 ClusterContentItems: []moduleLib.ClusterContent{
501                                         {
502                                                 Kubeconfig: "dGVzdCBjb250ZW50cwpvZiBhIGZpbGUgYXR0YWNoZWQKdG8gdGhlIGNyZWF0aW9uCm9mIGNsdXN0ZXJUZXN0Cg==",
503                                         },
504                                 },
505                         },
506                 },
507                 {
508                         label:        "Missing Cluster Name in Request Body",
509                         expectedCode: http.StatusBadRequest,
510                         metadata: `
511 {
512  "metadata": {
513   "description": "this is test cluster",
514   "userData1": "some cluster data abc",
515   "userData2": "some cluster data def"
516  }
517 }`,
518                         kubeconfig: `test contents
519 of a file attached
520 to the creation
521 of clusterTest
522 `,
523                         clusterClient: &mockClusterManager{},
524                 },
525         }
526
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))
537                         multiwr.Close()
538
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))
542
543                         //Check returned code
544                         if resp.StatusCode != testCase.expectedCode {
545                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
546                         }
547
548                         //Check returned body only if statusCreated
549                         if resp.StatusCode == http.StatusCreated {
550                                 got := moduleLib.Cluster{}
551                                 json.NewDecoder(resp.Body).Decode(&got)
552
553                                 if reflect.DeepEqual(testCase.expected, got) == false {
554                                         t.Errorf("createHandler returned unexpected body: got %v;"+
555                                                 " expected %v", got, testCase.expected)
556                                 }
557                         }
558                 })
559         }
560 }
561
562 func TestClusterGetAllHandler(t *testing.T) {
563
564         testCases := []struct {
565                 label         string
566                 expected      []moduleLib.Cluster
567                 name, version string
568                 expectedCode  int
569                 clusterClient *mockClusterManager
570         }{
571                 {
572                         label:        "Get Clusters",
573                         expectedCode: http.StatusOK,
574                         expected: []moduleLib.Cluster{
575                                 {
576                                         Metadata: moduleLib.Metadata{
577                                                 Name:        "testCluster1",
578                                                 Description: "testCluster 1 description",
579                                                 UserData1:   "some user data 1",
580                                                 UserData2:   "some user data 2",
581                                         },
582                                 },
583                                 {
584                                         Metadata: moduleLib.Metadata{
585                                                 Name:        "testCluster2",
586                                                 Description: "testCluster 2 description",
587                                                 UserData1:   "some user data A",
588                                                 UserData2:   "some user data B",
589                                         },
590                                 },
591                         },
592                         clusterClient: &mockClusterManager{
593                                 //Items that will be returned by the mocked Client
594                                 ClusterItems: []moduleLib.Cluster{
595                                         {
596                                                 Metadata: moduleLib.Metadata{
597                                                         Name:        "testCluster1",
598                                                         Description: "testCluster 1 description",
599                                                         UserData1:   "some user data 1",
600                                                         UserData2:   "some user data 2",
601                                                 },
602                                         },
603                                         {
604                                                 Metadata: moduleLib.Metadata{
605                                                         Name:        "testCluster2",
606                                                         Description: "testCluster 2 description",
607                                                         UserData1:   "some user data A",
608                                                         UserData2:   "some user data B",
609                                                 },
610                                         },
611                                 },
612                                 ClusterContentItems: []moduleLib.ClusterContent{
613                                         // content here doesn't matter - just needs to be present
614                                         {
615                                                 Kubeconfig: "dGVzdCBjb250ZW50cwpvZiBhIGZpbGUgYXR0YWNoZWQKdG8gdGhlIGNyZWF0aW9uCm9mIGNsdXN0ZXJUZXN0Cg==",
616                                         },
617                                         {
618                                                 Kubeconfig: "dGVzdCBjb250ZW50cwpvZiBhIGZpbGUgYXR0YWNoZWQKdG8gdGhlIGNyZWF0aW9uCm9mIGNsdXN0ZXJUZXN0Cg==",
619                                         },
620                                 },
621                         },
622                 },
623         }
624
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))
629
630                         //Check returned code
631                         if resp.StatusCode != testCase.expectedCode {
632                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
633                         }
634
635                         //Check returned body only if statusOK
636                         if resp.StatusCode == http.StatusOK {
637                                 got := []moduleLib.Cluster{}
638                                 json.NewDecoder(resp.Body).Decode(&got)
639
640                                 if reflect.DeepEqual(testCase.expected, got) == false {
641                                         t.Errorf("listHandler returned unexpected body: got %v;"+
642                                                 " expected %v", got, testCase.expected)
643                                 }
644                         }
645                 })
646         }
647 }
648
649 func TestClusterGetHandler(t *testing.T) {
650
651         testCases := []struct {
652                 label         string
653                 expected      moduleLib.Cluster
654                 name, version string
655                 accept        string
656                 expectedCode  int
657                 clusterClient *mockClusterManager
658         }{
659                 {
660                         label:        "Get Cluster with Accept: application/json",
661                         accept:       "application/json",
662                         expectedCode: http.StatusOK,
663                         expected: moduleLib.Cluster{
664                                 Metadata: moduleLib.Metadata{
665                                         Name:        "testCluster",
666                                         Description: "testCluster description",
667                                         UserData1:   "some user data 1",
668                                         UserData2:   "some user data 2",
669                                 },
670                         },
671                         name: "testCluster",
672                         clusterClient: &mockClusterManager{
673                                 //Items that will be returned by the mocked Client
674                                 ClusterItems: []moduleLib.Cluster{
675                                         {
676                                                 Metadata: moduleLib.Metadata{
677                                                         Name:        "testCluster",
678                                                         Description: "testCluster description",
679                                                         UserData1:   "some user data 1",
680                                                         UserData2:   "some user data 2",
681                                                 },
682                                         },
683                                 },
684                                 ClusterContentItems: []moduleLib.ClusterContent{
685                                         {
686                                                 Kubeconfig: "dGVzdCBjb250ZW50cwpvZiBhIGZpbGUgYXR0YWNoZWQKdG8gdGhlIGNyZWF0aW9uCm9mIGNsdXN0ZXJUZXN0Cg==",
687                                         },
688                                 },
689                         },
690                 },
691                 {
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"),
699                         },
700                 },
701         }
702
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)
708                         }
709                         resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient, nil, nil))
710
711                         //Check returned code
712                         if resp.StatusCode != testCase.expectedCode {
713                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
714                         }
715
716                         //Check returned body only if statusOK
717                         if resp.StatusCode == http.StatusOK {
718                                 got := moduleLib.Cluster{}
719                                 json.NewDecoder(resp.Body).Decode(&got)
720
721                                 if reflect.DeepEqual(testCase.expected, got) == false {
722                                         t.Errorf("listHandler returned unexpected body: got %v;"+
723                                                 " expected %v", got, testCase.expected)
724                                 }
725                         }
726                 })
727         }
728 }
729
730 func TestClusterGetContentHandler(t *testing.T) {
731
732         testCases := []struct {
733                 label         string
734                 expected      string
735                 name, version string
736                 accept        string
737                 expectedCode  int
738                 clusterClient *mockClusterManager
739         }{
740                 {
741                         label:        "Get Cluster Content with Accept: application/octet-stream",
742                         accept:       "application/octet-stream",
743                         expectedCode: http.StatusOK,
744                         expected: `test contents
745 of a file attached
746 to the creation
747 of clusterTest
748 `,
749                         name: "testCluster",
750                         clusterClient: &mockClusterManager{
751                                 //Items that will be returned by the mocked Client
752                                 ClusterItems: []moduleLib.Cluster{
753                                         {
754                                                 Metadata: moduleLib.Metadata{
755                                                         Name:        "testCluster",
756                                                         Description: "testCluster description",
757                                                         UserData1:   "some user data 1",
758                                                         UserData2:   "some user data 2",
759                                                 },
760                                         },
761                                 },
762                                 ClusterContentItems: []moduleLib.ClusterContent{
763                                         {
764                                                 Kubeconfig: "dGVzdCBjb250ZW50cwpvZiBhIGZpbGUgYXR0YWNoZWQKdG8gdGhlIGNyZWF0aW9uCm9mIGNsdXN0ZXJUZXN0Cg==",
765                                         },
766                                 },
767                         },
768                 },
769                 {
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"),
777                         },
778                 },
779         }
780
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)
786                         }
787                         resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient, nil, nil))
788
789                         //Check returned code
790                         if resp.StatusCode != testCase.expectedCode {
791                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
792                         }
793
794                         //Check returned body only if statusOK
795                         if resp.StatusCode == http.StatusOK {
796                                 body := new(bytes.Buffer)
797                                 body.ReadFrom(resp.Body)
798                                 got := body.String()
799
800                                 if reflect.DeepEqual(testCase.expected, got) == false {
801                                         t.Errorf("listHandler returned unexpected body: got %v;"+
802                                                 " expected %v", got, testCase.expected)
803                                 }
804                         }
805                 })
806         }
807 }
808
809 func TestClusterDeleteHandler(t *testing.T) {
810
811         testCases := []struct {
812                 label         string
813                 name          string
814                 version       string
815                 expectedCode  int
816                 clusterClient *mockClusterManager
817         }{
818                 {
819                         label:         "Delete Cluster",
820                         expectedCode:  http.StatusNoContent,
821                         name:          "testCluster",
822                         clusterClient: &mockClusterManager{},
823                 },
824                 {
825                         label:        "Delete Non-Existing Cluster",
826                         expectedCode: http.StatusInternalServerError,
827                         name:         "testCluster",
828                         clusterClient: &mockClusterManager{
829                                 Err: pkgerrors.New("Internal Error"),
830                         },
831                 },
832         }
833
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))
838
839                         //Check returned code
840                         if resp.StatusCode != testCase.expectedCode {
841                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
842                         }
843                 })
844         }
845 }
846
847 func TestClusterLabelCreateHandler(t *testing.T) {
848         testCases := []struct {
849                 label         string
850                 reader        io.Reader
851                 expected      moduleLib.ClusterLabel
852                 expectedCode  int
853                 clusterClient *mockClusterManager
854         }{
855                 {
856                         label:         "Missing Cluster Label Body Failure",
857                         expectedCode:  http.StatusBadRequest,
858                         clusterClient: &mockClusterManager{},
859                 },
860                 {
861                         label:        "Create Cluster Label",
862                         expectedCode: http.StatusCreated,
863                         reader: bytes.NewBuffer([]byte(`{
864                                         "label-name": "test-label"
865                                 }`)),
866                         expected: moduleLib.ClusterLabel{
867                                 LabelName: "test-label",
868                         },
869                         clusterClient: &mockClusterManager{
870                                 //Items that will be returned by the mocked Client
871                                 ClusterLabelItems: []moduleLib.ClusterLabel{
872                                         {
873                                                 LabelName: "test-label",
874                                         },
875                                 },
876                         },
877                 },
878         }
879
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))
884
885                         //Check returned code
886                         if resp.StatusCode != testCase.expectedCode {
887                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
888                         }
889
890                         //Check returned body only if statusCreated
891                         if resp.StatusCode == http.StatusCreated {
892                                 got := moduleLib.ClusterLabel{}
893                                 json.NewDecoder(resp.Body).Decode(&got)
894
895                                 if reflect.DeepEqual(testCase.expected, got) == false {
896                                         t.Errorf("createHandler returned unexpected body: got %v;"+
897                                                 " expected %v", got, testCase.expected)
898                                 }
899                         }
900                 })
901         }
902 }
903
904 func TestClusterLabelsGetHandler(t *testing.T) {
905
906         testCases := []struct {
907                 label         string
908                 expected      []moduleLib.ClusterLabel
909                 name, version string
910                 expectedCode  int
911                 clusterClient *mockClusterManager
912         }{
913                 {
914                         label:        "Get Cluster Labels",
915                         expectedCode: http.StatusOK,
916                         expected: []moduleLib.ClusterLabel{
917                                 {
918                                         LabelName: "test-label1",
919                                 },
920                                 {
921                                         LabelName: "test-label-two",
922                                 },
923                                 {
924                                         LabelName: "test-label-3",
925                                 },
926                         },
927                         clusterClient: &mockClusterManager{
928                                 //Items that will be returned by the mocked Client
929                                 ClusterLabelItems: []moduleLib.ClusterLabel{
930                                         {
931                                                 LabelName: "test-label1",
932                                         },
933                                         {
934                                                 LabelName: "test-label-two",
935                                         },
936                                         {
937                                                 LabelName: "test-label-3",
938                                         },
939                                 },
940                         },
941                 },
942         }
943
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))
948
949                         //Check returned code
950                         if resp.StatusCode != testCase.expectedCode {
951                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
952                         }
953
954                         //Check returned body only if statusOK
955                         if resp.StatusCode == http.StatusOK {
956                                 got := []moduleLib.ClusterLabel{}
957                                 json.NewDecoder(resp.Body).Decode(&got)
958
959                                 if reflect.DeepEqual(testCase.expected, got) == false {
960                                         t.Errorf("listHandler returned unexpected body: got %v;"+
961                                                 " expected %v", got, testCase.expected)
962                                 }
963                         }
964                 })
965         }
966 }
967
968 func TestClusterLabelGetHandler(t *testing.T) {
969
970         testCases := []struct {
971                 label         string
972                 expected      moduleLib.ClusterLabel
973                 name, version string
974                 expectedCode  int
975                 clusterClient *mockClusterManager
976         }{
977                 {
978                         label:        "Get Cluster Label",
979                         expectedCode: http.StatusOK,
980                         expected: moduleLib.ClusterLabel{
981                                 LabelName: "testlabel",
982                         },
983                         name: "testlabel",
984                         clusterClient: &mockClusterManager{
985                                 //Items that will be returned by the mocked Client
986                                 ClusterLabelItems: []moduleLib.ClusterLabel{
987                                         {
988                                                 LabelName: "testlabel",
989                                         },
990                                 },
991                         },
992                 },
993                 {
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"),
1000                         },
1001                 },
1002         }
1003
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))
1008
1009                         //Check returned code
1010                         if resp.StatusCode != testCase.expectedCode {
1011                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
1012                         }
1013
1014                         //Check returned body only if statusOK
1015                         if resp.StatusCode == http.StatusOK {
1016                                 got := moduleLib.ClusterLabel{}
1017                                 json.NewDecoder(resp.Body).Decode(&got)
1018
1019                                 if reflect.DeepEqual(testCase.expected, got) == false {
1020                                         t.Errorf("listHandler returned unexpected body: got %v;"+
1021                                                 " expected %v", got, testCase.expected)
1022                                 }
1023                         }
1024                 })
1025         }
1026 }
1027
1028 func TestClusterLabelDeleteHandler(t *testing.T) {
1029
1030         testCases := []struct {
1031                 label         string
1032                 name          string
1033                 version       string
1034                 expectedCode  int
1035                 clusterClient *mockClusterManager
1036         }{
1037                 {
1038                         label:         "Delete Cluster Label",
1039                         expectedCode:  http.StatusNoContent,
1040                         name:          "testClusterLabel",
1041                         clusterClient: &mockClusterManager{},
1042                 },
1043                 {
1044                         label:        "Delete Non-Existing Cluster Label",
1045                         expectedCode: http.StatusInternalServerError,
1046                         name:         "testClusterLabel",
1047                         clusterClient: &mockClusterManager{
1048                                 Err: pkgerrors.New("Internal Error"),
1049                         },
1050                 },
1051         }
1052
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))
1057
1058                         //Check returned code
1059                         if resp.StatusCode != testCase.expectedCode {
1060                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
1061                         }
1062                 })
1063         }
1064 }
1065
1066 func TestClusterKvPairsCreateHandler(t *testing.T) {
1067         testCases := []struct {
1068                 label         string
1069                 reader        io.Reader
1070                 expected      moduleLib.ClusterKvPairs
1071                 expectedCode  int
1072                 clusterClient *mockClusterManager
1073         }{
1074                 {
1075                         label:         "Missing Cluster KvPairs Body Failure",
1076                         expectedCode:  http.StatusBadRequest,
1077                         clusterClient: &mockClusterManager{},
1078                 },
1079                 {
1080                         label:        "Create Cluster KvPairs",
1081                         expectedCode: http.StatusCreated,
1082                         reader: bytes.NewBuffer([]byte(`{
1083                                         "metadata": {
1084                                                 "name": "ClusterKvPair1",
1085                                                 "description": "test cluster kv pairs",
1086                                                 "userData1": "some user data 1",
1087                                                 "userData2": "some user data 2"
1088                                         },
1089                                         "spec": {
1090                                                 "kv": [
1091                                                         {
1092                                                                 "key1": "value1"
1093                                                         },
1094                                                         {
1095                                                                 "key2": "value2"
1096                                                         }
1097                                                 ]
1098                                         }
1099                                 }`)),
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",
1106                                 },
1107                                 Spec: moduleLib.ClusterKvSpec{
1108                                         Kv: []map[string]interface{}{
1109                                                 {
1110                                                         "key1": "value1",
1111                                                 },
1112                                                 {
1113                                                         "key2": "value2",
1114                                                 },
1115                                         },
1116                                 },
1117                         },
1118                         clusterClient: &mockClusterManager{
1119                                 //Items that will be returned by the mocked Client
1120                                 ClusterKvPairsItems: []moduleLib.ClusterKvPairs{
1121                                         {
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",
1127                                                 },
1128                                                 Spec: moduleLib.ClusterKvSpec{
1129                                                         Kv: []map[string]interface{}{
1130                                                                 {
1131                                                                         "key1": "value1",
1132                                                                 },
1133                                                                 {
1134                                                                         "key2": "value2",
1135                                                                 },
1136                                                         },
1137                                                 },
1138                                         },
1139                                 },
1140                         },
1141                 },
1142         }
1143
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))
1148
1149                         //Check returned code
1150                         if resp.StatusCode != testCase.expectedCode {
1151                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
1152                         }
1153
1154                         //Check returned body only if statusCreated
1155                         if resp.StatusCode == http.StatusCreated {
1156                                 got := moduleLib.ClusterKvPairs{}
1157                                 json.NewDecoder(resp.Body).Decode(&got)
1158
1159                                 if reflect.DeepEqual(testCase.expected, got) == false {
1160                                         t.Errorf("createHandler returned unexpected body: got %v;"+
1161                                                 " expected %v", got, testCase.expected)
1162                                 }
1163                         }
1164                 })
1165         }
1166 }
1167
1168 func TestClusterKvPairsGetAllHandler(t *testing.T) {
1169
1170         testCases := []struct {
1171                 label         string
1172                 expected      []moduleLib.ClusterKvPairs
1173                 name, version string
1174                 expectedCode  int
1175                 clusterClient *mockClusterManager
1176         }{
1177                 {
1178                         label:        "Get Cluster KvPairs",
1179                         expectedCode: http.StatusOK,
1180                         expected: []moduleLib.ClusterKvPairs{
1181                                 {
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",
1187                                         },
1188                                         Spec: moduleLib.ClusterKvSpec{
1189                                                 Kv: []map[string]interface{}{
1190                                                         {
1191                                                                 "key1": "value1",
1192                                                         },
1193                                                         {
1194                                                                 "key2": "value2",
1195                                                         },
1196                                                 },
1197                                         },
1198                                 },
1199                                 {
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",
1205                                         },
1206                                         Spec: moduleLib.ClusterKvSpec{
1207                                                 Kv: []map[string]interface{}{
1208                                                         {
1209                                                                 "keyA": "valueA",
1210                                                         },
1211                                                         {
1212                                                                 "keyB": "valueB",
1213                                                         },
1214                                                 },
1215                                         },
1216                                 },
1217                         },
1218                         clusterClient: &mockClusterManager{
1219                                 //Items that will be returned by the mocked Client
1220                                 ClusterKvPairsItems: []moduleLib.ClusterKvPairs{
1221                                         {
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",
1227                                                 },
1228                                                 Spec: moduleLib.ClusterKvSpec{
1229                                                         Kv: []map[string]interface{}{
1230                                                                 {
1231                                                                         "key1": "value1",
1232                                                                 },
1233                                                                 {
1234                                                                         "key2": "value2",
1235                                                                 },
1236                                                         },
1237                                                 },
1238                                         },
1239                                         {
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",
1245                                                 },
1246                                                 Spec: moduleLib.ClusterKvSpec{
1247                                                         Kv: []map[string]interface{}{
1248                                                                 {
1249                                                                         "keyA": "valueA",
1250                                                                 },
1251                                                                 {
1252                                                                         "keyB": "valueB",
1253                                                                 },
1254                                                         },
1255                                                 },
1256                                         },
1257                                 },
1258                         },
1259                 },
1260         }
1261
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))
1266
1267                         //Check returned code
1268                         if resp.StatusCode != testCase.expectedCode {
1269                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
1270                         }
1271
1272                         //Check returned body only if statusOK
1273                         if resp.StatusCode == http.StatusOK {
1274                                 got := []moduleLib.ClusterKvPairs{}
1275                                 json.NewDecoder(resp.Body).Decode(&got)
1276
1277                                 if reflect.DeepEqual(testCase.expected, got) == false {
1278                                         t.Errorf("listHandler returned unexpected body: got %v;"+
1279                                                 " expected %v", got, testCase.expected)
1280                                 }
1281                         }
1282                 })
1283         }
1284 }
1285
1286 func TestClusterKvPairsGetHandler(t *testing.T) {
1287
1288         testCases := []struct {
1289                 label         string
1290                 expected      moduleLib.ClusterKvPairs
1291                 name, version string
1292                 expectedCode  int
1293                 clusterClient *mockClusterManager
1294         }{
1295                 {
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",
1304                                 },
1305                                 Spec: moduleLib.ClusterKvSpec{
1306                                         Kv: []map[string]interface{}{
1307                                                 {
1308                                                         "keyA": "valueA",
1309                                                 },
1310                                                 {
1311                                                         "keyB": "valueB",
1312                                                 },
1313                                         },
1314                                 },
1315                         },
1316                         name: "ClusterKvPair2",
1317                         clusterClient: &mockClusterManager{
1318                                 //Items that will be returned by the mocked Client
1319                                 ClusterKvPairsItems: []moduleLib.ClusterKvPairs{
1320                                         {
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",
1326                                                 },
1327                                                 Spec: moduleLib.ClusterKvSpec{
1328                                                         Kv: []map[string]interface{}{
1329                                                                 {
1330                                                                         "keyA": "valueA",
1331                                                                 },
1332                                                                 {
1333                                                                         "keyB": "valueB",
1334                                                                 },
1335                                                         },
1336                                                 },
1337                                         },
1338                                 },
1339                         },
1340                 },
1341                 {
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"),
1348                         },
1349                 },
1350         }
1351
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))
1356
1357                         //Check returned code
1358                         if resp.StatusCode != testCase.expectedCode {
1359                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
1360                         }
1361
1362                         //Check returned body only if statusOK
1363                         if resp.StatusCode == http.StatusOK {
1364                                 got := moduleLib.ClusterKvPairs{}
1365                                 json.NewDecoder(resp.Body).Decode(&got)
1366
1367                                 if reflect.DeepEqual(testCase.expected, got) == false {
1368                                         t.Errorf("listHandler returned unexpected body: got %v;"+
1369                                                 " expected %v", got, testCase.expected)
1370                                 }
1371                         }
1372                 })
1373         }
1374 }
1375
1376 func TestClusterKvPairsDeleteHandler(t *testing.T) {
1377
1378         testCases := []struct {
1379                 label         string
1380                 name          string
1381                 version       string
1382                 expectedCode  int
1383                 clusterClient *mockClusterManager
1384         }{
1385                 {
1386                         label:         "Delete Cluster KV Pairs",
1387                         expectedCode:  http.StatusNoContent,
1388                         name:          "testClusterKvPairs",
1389                         clusterClient: &mockClusterManager{},
1390                 },
1391                 {
1392                         label:        "Delete Non-Existing Cluster KV Pairs",
1393                         expectedCode: http.StatusInternalServerError,
1394                         name:         "testClusterKvPairs",
1395                         clusterClient: &mockClusterManager{
1396                                 Err: pkgerrors.New("Internal Error"),
1397                         },
1398                 },
1399         }
1400
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))
1405
1406                         //Check returned code
1407                         if resp.StatusCode != testCase.expectedCode {
1408                                 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
1409                         }
1410                 })
1411         }
1412 }