Merge "Change format of the network file"
[multicloud/k8s.git] / src / k8splugin / api / brokerhandler_test.go
1 /*
2 Copyright 2018 Intel Corporation.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6     http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software
8 distributed under the License is distributed on an "AS IS" BASIS,
9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 See the License for the specific language governing permissions and
11 limitations under the License.
12 */
13
14 package api
15
16 import (
17         "bytes"
18         "encoding/json"
19         "io"
20         "io/ioutil"
21         "net/http"
22         "net/http/httptest"
23         "reflect"
24         "testing"
25
26         "k8splugin/internal/app"
27         "k8splugin/internal/helm"
28
29         pkgerrors "github.com/pkg/errors"
30         "k8s.io/apimachinery/pkg/runtime/schema"
31 )
32
33 func TestBrokerCreateHandler(t *testing.T) {
34         testCases := []struct {
35                 label        string
36                 input        io.Reader
37                 expected     brokerPOSTResponse
38                 expectedCode int
39                 instClient   *mockInstanceClient
40         }{
41                 {
42                         label:        "Missing body failure",
43                         expectedCode: http.StatusBadRequest,
44                 },
45                 {
46                         label:        "Invalid JSON request format",
47                         input:        bytes.NewBuffer([]byte("invalid")),
48                         expectedCode: http.StatusUnprocessableEntity,
49                 },
50                 {
51                         label: "Missing parameter failure",
52                         input: bytes.NewBuffer([]byte(`{
53                                 "vf-module-model-customization-id": "84sdfkio938",
54                                 "user_directives": {
55                                         "definition-name": "test-rbdef",
56                                         "definition-version": "v1"                              }
57                         }`)),
58                         expectedCode: http.StatusBadRequest,
59                 },
60                 {
61                         label: "Succesfully create an Instance",
62                         input: bytes.NewBuffer([]byte(`{
63                                 "vf-module-model-customization-id": "84sdfkio938",
64                                 "user_directives": {
65                                         "definition-name": "test-rbdef",
66                                         "definition-version": "v1",
67                                         "profile-name": "profile1"
68                                 }
69                         }`)),
70                         expected: brokerPOSTResponse{
71                                 WorkloadID:   "HaKpys8e",
72                                 TemplateType: "heat",
73                                 TemplateResponse: []helm.KubernetesResource{
74                                         {
75                                                 GVK: schema.GroupVersionKind{
76                                                         Group:   "apps",
77                                                         Version: "v1",
78                                                         Kind:    "Deployment"},
79                                                 Name: "test-deployment",
80                                         },
81                                         {
82                                                 GVK: schema.GroupVersionKind{
83                                                         Group:   "",
84                                                         Version: "v1",
85                                                         Kind:    "Service"},
86                                                 Name: "test-service",
87                                         },
88                                 },
89                         },
90                         expectedCode: http.StatusCreated,
91                         instClient: &mockInstanceClient{
92                                 items: []app.InstanceResponse{
93                                         {
94                                                 ID:          "HaKpys8e",
95                                                 RBName:      "test-rbdef",
96                                                 RBVersion:   "v1",
97                                                 ProfileName: "profile1",
98                                                 CloudRegion: "region1",
99                                                 Namespace:   "testnamespace",
100                                                 Resources: []helm.KubernetesResource{
101                                                         {
102                                                                 GVK: schema.GroupVersionKind{
103                                                                         Group:   "apps",
104                                                                         Version: "v1",
105                                                                         Kind:    "Deployment"},
106                                                                 Name: "test-deployment",
107                                                         },
108                                                         {
109                                                                 GVK: schema.GroupVersionKind{
110                                                                         Group:   "",
111                                                                         Version: "v1",
112                                                                         Kind:    "Service"},
113                                                                 Name: "test-service",
114                                                         },
115                                                 },
116                                         },
117                                 },
118                         },
119                 },
120         }
121
122         for _, testCase := range testCases {
123                 t.Run(testCase.label, func(t *testing.T) {
124
125                         request := httptest.NewRequest("POST", "/cloudowner/cloudregion/infra_workload", testCase.input)
126                         resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil))
127
128                         if testCase.expectedCode != resp.StatusCode {
129                                 body, _ := ioutil.ReadAll(resp.Body)
130                                 t.Log(string(body))
131                                 t.Fatalf("Request method returned: \n%v\n and it was expected: \n%v", resp.StatusCode, testCase.expectedCode)
132                         }
133
134                         if resp.StatusCode == http.StatusCreated {
135                                 var response brokerPOSTResponse
136                                 err := json.NewDecoder(resp.Body).Decode(&response)
137                                 if err != nil {
138                                         t.Fatalf("Parsing the returned response got an error (%s)", err)
139                                 }
140                                 if !reflect.DeepEqual(testCase.expected, response) {
141                                         t.Fatalf("TestGetHandler returned:\n result=%v\n expected=%v",
142                                                 response, testCase.expected)
143                                 }
144                         }
145                 })
146         }
147 }
148
149 func TestBrokerGetHandler(t *testing.T) {
150         testCases := []struct {
151                 label            string
152                 input            string
153                 expectedCode     int
154                 expectedResponse brokerGETResponse
155                 instClient       *mockInstanceClient
156         }{
157                 {
158                         label:        "Fail to retrieve Instance",
159                         input:        "HaKpys8e",
160                         expectedCode: http.StatusInternalServerError,
161                         instClient: &mockInstanceClient{
162                                 err: pkgerrors.New("Internal error"),
163                         },
164                 },
165                 {
166                         label:        "Succesful get an Instance",
167                         input:        "HaKpys8e",
168                         expectedCode: http.StatusOK,
169                         expectedResponse: brokerGETResponse{
170                                 TemplateType:   "heat",
171                                 WorkloadID:     "HaKpys8e",
172                                 WorkloadStatus: "CREATED",
173                         },
174                         instClient: &mockInstanceClient{
175                                 items: []app.InstanceResponse{
176                                         {
177                                                 ID:          "HaKpys8e",
178                                                 RBName:      "test-rbdef",
179                                                 RBVersion:   "v1",
180                                                 ProfileName: "profile1",
181                                                 CloudRegion: "region1",
182                                                 Namespace:   "testnamespace",
183                                                 Resources: []helm.KubernetesResource{
184                                                         {
185                                                                 GVK: schema.GroupVersionKind{
186                                                                         Group:   "apps",
187                                                                         Version: "v1",
188                                                                         Kind:    "Deployment"},
189                                                                 Name: "test-deployment",
190                                                         },
191                                                         {
192                                                                 GVK: schema.GroupVersionKind{
193                                                                         Group:   "",
194                                                                         Version: "v1",
195                                                                         Kind:    "Service"},
196                                                                 Name: "test-service",
197                                                         },
198                                                 },
199                                         },
200                                 },
201                         },
202                 },
203         }
204
205         for _, testCase := range testCases {
206                 t.Run(testCase.label, func(t *testing.T) {
207                         request := httptest.NewRequest("GET", "/cloudowner/cloudregion/infra_workload/"+testCase.input, nil)
208                         resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil))
209
210                         if testCase.expectedCode != resp.StatusCode {
211                                 t.Fatalf("Request method returned: %v and it was expected: %v",
212                                         resp.StatusCode, testCase.expectedCode)
213                         }
214                         if resp.StatusCode == http.StatusOK {
215                                 var response brokerGETResponse
216                                 err := json.NewDecoder(resp.Body).Decode(&response)
217                                 if err != nil {
218                                         t.Fatalf("Parsing the returned response got an error (%s)", err)
219                                 }
220                                 if !reflect.DeepEqual(testCase.expectedResponse, response) {
221                                         t.Fatalf("TestGetHandler returned:\n result=%v\n expected=%v",
222                                                 response, testCase.expectedResponse)
223                                 }
224                         }
225                 })
226         }
227 }
228
229 func TestBrokerDeleteHandler(t *testing.T) {
230         testCases := []struct {
231                 label        string
232                 input        string
233                 expectedCode int
234                 instClient   *mockInstanceClient
235         }{
236                 {
237                         label:        "Fail to destroy VNF",
238                         input:        "HaKpys8e",
239                         expectedCode: http.StatusInternalServerError,
240                         instClient: &mockInstanceClient{
241                                 err: pkgerrors.New("Internal error"),
242                         },
243                 },
244                 {
245                         label:        "Succesful delete a VNF",
246                         input:        "HaKpys8e",
247                         expectedCode: http.StatusAccepted,
248                         instClient:   &mockInstanceClient{},
249                 },
250         }
251
252         for _, testCase := range testCases {
253                 t.Run(testCase.label, func(t *testing.T) {
254                         request := httptest.NewRequest("DELETE", "/cloudowner/cloudregion/infra_workload/"+testCase.input, nil)
255                         resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil))
256
257                         if testCase.expectedCode != resp.StatusCode {
258                                 t.Fatalf("Request method returned: %v and it was expected: %v", resp.StatusCode, testCase.expectedCode)
259                         }
260                 })
261         }
262 }