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.
26 "github.com/onap/multicloud-k8s/src/k8splugin/internal/app"
27 "github.com/onap/multicloud-k8s/src/k8splugin/internal/helm"
29 pkgerrors "github.com/pkg/errors"
30 "k8s.io/apimachinery/pkg/runtime/schema"
33 func TestBrokerCreateHandler(t *testing.T) {
34 testCases := []struct {
37 expected brokerPOSTResponse
39 instClient *mockInstanceClient
42 label: "Missing body failure",
43 expectedCode: http.StatusBadRequest,
46 label: "Invalid JSON request format",
47 input: bytes.NewBuffer([]byte("invalid")),
48 expectedCode: http.StatusUnprocessableEntity,
51 label: "Missing vf-module-*-id parameter",
52 input: bytes.NewBuffer([]byte(`{
53 "vf-module-model-customization-id": "84sdfkio938",
54 "vf-module-model-invariant-id": "123456qwerty",
58 "attribute_name": "vf_module_name",
59 "attribute_value": "test-vf-module-name"
62 "attribute_name": "k8s-rb-profile-name",
63 "attribute_value": "profile1"
68 expectedCode: http.StatusBadRequest,
71 label: "Missing parameter from sdnc_directives",
72 input: bytes.NewBuffer([]byte(`{
73 "vf-module-model-customization-id": "84sdfkio938",
74 "vf-module-model-invariant-id": "123456qwerty",
75 "vf-module-model-version-id": "123qweasdzxc",
79 "attribute_name": "vf_module_name",
80 "attribute_value": "test-vf-module-name"
85 expectedCode: http.StatusBadRequest,
88 label: "Succesfully create an Instance",
89 input: bytes.NewBuffer([]byte(`{
90 "vf-module-model-customization-id": "84sdfkio938",
91 "vf-module-model-invariant-id": "123456qwerty",
92 "vf-module-model-version-id": "123qweasdzxc",
96 "attribute_name": "vf_module_name",
97 "attribute_value": "test-vf-module-name"
100 "attribute_name": "k8s-rb-profile-name",
101 "attribute_value": "profile1"
106 expected: brokerPOSTResponse{
107 WorkloadID: "HaKpys8e",
108 TemplateType: "heat",
109 WorkloadStatus: "CREATE_COMPLETE",
110 TemplateResponse: []helm.KubernetesResource{
112 GVK: schema.GroupVersionKind{
116 Name: "test-deployment",
119 GVK: schema.GroupVersionKind{
123 Name: "test-service",
127 expectedCode: http.StatusCreated,
128 instClient: &mockInstanceClient{
129 items: []app.InstanceResponse{
132 Request: app.InstanceRequest{
133 RBName: "123456qwerty",
134 RBVersion: "123qweasdzxc",
135 ProfileName: "profile1",
136 CloudRegion: "region1",
138 Namespace: "testnamespace",
139 Resources: []helm.KubernetesResource{
141 GVK: schema.GroupVersionKind{
145 Name: "test-deployment",
148 GVK: schema.GroupVersionKind{
152 Name: "test-service",
161 for _, testCase := range testCases {
162 t.Run(testCase.label, func(t *testing.T) {
164 request := httptest.NewRequest("POST", "/cloudowner/cloudregion/infra_workload", testCase.input)
165 resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil))
167 if testCase.expectedCode != resp.StatusCode {
168 body, _ := ioutil.ReadAll(resp.Body)
170 t.Fatalf("Request method returned: \n%v\n and it was expected: \n%v", resp.StatusCode, testCase.expectedCode)
173 if resp.StatusCode == http.StatusCreated {
174 var response brokerPOSTResponse
175 err := json.NewDecoder(resp.Body).Decode(&response)
177 t.Fatalf("Parsing the returned response got an error (%s)", err)
179 if !reflect.DeepEqual(testCase.expected, response) {
180 t.Fatalf("TestGetHandler returned:\n result=%v\n expected=%v",
181 response, testCase.expected)
188 func TestBrokerGetHandler(t *testing.T) {
189 testCases := []struct {
193 expectedResponse brokerGETResponse
194 instClient *mockInstanceClient
197 label: "Fail to retrieve Instance",
199 expectedCode: http.StatusInternalServerError,
200 instClient: &mockInstanceClient{
201 err: pkgerrors.New("Internal error"),
205 label: "Succesful get an Instance",
207 expectedCode: http.StatusOK,
208 expectedResponse: brokerGETResponse{
209 TemplateType: "heat",
210 WorkloadID: "HaKpys8e",
211 WorkloadStatus: "CREATE_COMPLETE",
213 instClient: &mockInstanceClient{
214 items: []app.InstanceResponse{
217 Request: app.InstanceRequest{
218 RBName: "test-rbdef",
220 ProfileName: "profile1",
221 CloudRegion: "region1",
223 Namespace: "testnamespace",
224 Resources: []helm.KubernetesResource{
226 GVK: schema.GroupVersionKind{
230 Name: "test-deployment",
233 GVK: schema.GroupVersionKind{
237 Name: "test-service",
246 for _, testCase := range testCases {
247 t.Run(testCase.label, func(t *testing.T) {
248 request := httptest.NewRequest("GET", "/cloudowner/cloudregion/infra_workload/"+testCase.input, nil)
249 resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil))
251 if testCase.expectedCode != resp.StatusCode {
252 t.Fatalf("Request method returned: %v and it was expected: %v",
253 resp.StatusCode, testCase.expectedCode)
255 if resp.StatusCode == http.StatusOK {
256 var response brokerGETResponse
257 err := json.NewDecoder(resp.Body).Decode(&response)
259 t.Fatalf("Parsing the returned response got an error (%s)", err)
261 if !reflect.DeepEqual(testCase.expectedResponse, response) {
262 t.Fatalf("TestGetHandler returned:\n result=%v\n expected=%v",
263 response, testCase.expectedResponse)
270 func TestBrokerFindHandler(t *testing.T) {
271 testCases := []struct {
275 expectedResponse brokerGETResponse
276 instClient *mockInstanceClient
279 label: "Successful find an Instance",
280 input: "test-vf-module-name",
281 expectedCode: http.StatusOK,
282 expectedResponse: brokerGETResponse{
283 TemplateType: "heat",
284 WorkloadID: "HaKpys8e",
285 WorkloadStatus: "CREATE_COMPLETE",
286 WorkloadStatusReason: map[string]interface{}{
287 "stacks": []map[string]interface{}{
289 "stack_status": "CREATE_COMPLETE",
295 instClient: &mockInstanceClient{
296 miniitems: []app.InstanceMiniResponse{
299 Request: app.InstanceRequest{
300 RBName: "test-rbdef",
302 ProfileName: "profile1",
303 CloudRegion: "region1",
305 Namespace: "testnamespace",
311 label: "Fail to find an Instance",
312 input: "test-vf-module-name-1",
313 expectedCode: http.StatusOK,
314 expectedResponse: brokerGETResponse{
315 TemplateType: "heat",
317 WorkloadStatus: "GET_COMPLETE",
318 WorkloadStatusReason: map[string]interface{}{
319 "stacks": []map[string]interface{}{},
322 instClient: &mockInstanceClient{},
326 for _, testCase := range testCases {
327 t.Run(testCase.label, func(t *testing.T) {
328 request := httptest.NewRequest("GET", "/cloudowner/cloudregion/infra_workload?name="+testCase.input, nil)
329 resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil))
331 if testCase.expectedCode != resp.StatusCode {
332 t.Fatalf("Request method returned: %v and it was expected: %v",
333 resp.StatusCode, testCase.expectedCode)
335 if resp.StatusCode == http.StatusOK {
336 var response brokerGETResponse
337 err := json.NewDecoder(resp.Body).Decode(&response)
339 t.Fatalf("Parsing the returned response got an error (%s)", err)
341 if testCase.expectedResponse.WorkloadID != response.WorkloadID {
342 t.Fatalf("TestGetHandler returned:\n result=%v\n expected=%v",
343 response.WorkloadID, testCase.expectedResponse.WorkloadID)
345 tcStacks := testCase.expectedResponse.WorkloadStatusReason["stacks"].([]map[string]interface{})
346 if len(tcStacks) != 0 {
347 //We expect only one response in this testcase.
348 resStacks := response.WorkloadStatusReason["stacks"].([]interface{})[0].(map[string]interface{})
349 if !reflect.DeepEqual(tcStacks[0], resStacks) {
350 t.Fatalf("TestGetHandler returned:\n result=%v\n expected=%v",
359 func TestBrokerDeleteHandler(t *testing.T) {
360 testCases := []struct {
364 expectedResponse brokerDELETEResponse
365 instClient *mockInstanceClient
368 label: "Fail to destroy VNF",
370 expectedCode: http.StatusInternalServerError,
371 instClient: &mockInstanceClient{
372 err: pkgerrors.New("Internal error"),
376 label: "Succesful delete a VNF",
378 expectedCode: http.StatusAccepted,
379 expectedResponse: brokerDELETEResponse{
380 TemplateType: "heat",
381 WorkloadID: "HaKpys8e",
382 WorkloadStatus: "DELETE_COMPLETE",
384 instClient: &mockInstanceClient{},
388 for _, testCase := range testCases {
389 t.Run(testCase.label, func(t *testing.T) {
390 request := httptest.NewRequest("DELETE", "/cloudowner/cloudregion/infra_workload/"+testCase.input, nil)
391 resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil))
393 if testCase.expectedCode != resp.StatusCode {
394 t.Fatalf("Request method returned: %v and it was expected: %v", resp.StatusCode, testCase.expectedCode)
396 if resp.StatusCode == http.StatusOK {
397 var response brokerGETResponse
398 err := json.NewDecoder(resp.Body).Decode(&response)
400 t.Fatalf("Parsing the returned response got an error (%s)", err)
402 if !reflect.DeepEqual(testCase.expectedResponse, response) {
403 t.Fatalf("TestGetHandler returned:\n result=%v\n expected=%v",
404 response, testCase.expectedResponse)