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 "k8splugin/internal/app"
27 "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 parameter failure",
52 input: bytes.NewBuffer([]byte(`{
53 "vf-module-model-customization-id": "84sdfkio938",
55 "definition-name": "test-rbdef",
56 "definition-version": "v1" }
58 expectedCode: http.StatusBadRequest,
61 label: "Succesfully create an Instance",
62 input: bytes.NewBuffer([]byte(`{
63 "vf-module-model-customization-id": "84sdfkio938",
65 "definition-name": "test-rbdef",
66 "definition-version": "v1",
67 "profile-name": "profile1"
70 expected: brokerPOSTResponse{
71 WorkloadID: "HaKpys8e",
73 TemplateResponse: []helm.KubernetesResource{
75 GVK: schema.GroupVersionKind{
79 Name: "test-deployment",
82 GVK: schema.GroupVersionKind{
90 expectedCode: http.StatusCreated,
91 instClient: &mockInstanceClient{
92 items: []app.InstanceResponse{
97 ProfileName: "profile1",
98 CloudRegion: "region1",
99 Namespace: "testnamespace",
100 Resources: []helm.KubernetesResource{
102 GVK: schema.GroupVersionKind{
106 Name: "test-deployment",
109 GVK: schema.GroupVersionKind{
113 Name: "test-service",
122 for _, testCase := range testCases {
123 t.Run(testCase.label, func(t *testing.T) {
125 request := httptest.NewRequest("POST", "/cloudowner/cloudregion/infra_workload", testCase.input)
126 resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil))
128 if testCase.expectedCode != resp.StatusCode {
129 body, _ := ioutil.ReadAll(resp.Body)
131 t.Fatalf("Request method returned: \n%v\n and it was expected: \n%v", resp.StatusCode, testCase.expectedCode)
134 if resp.StatusCode == http.StatusCreated {
135 var response brokerPOSTResponse
136 err := json.NewDecoder(resp.Body).Decode(&response)
138 t.Fatalf("Parsing the returned response got an error (%s)", err)
140 if !reflect.DeepEqual(testCase.expected, response) {
141 t.Fatalf("TestGetHandler returned:\n result=%v\n expected=%v",
142 response, testCase.expected)
149 func TestBrokerGetHandler(t *testing.T) {
150 testCases := []struct {
154 expectedResponse brokerGETResponse
155 instClient *mockInstanceClient
158 label: "Fail to retrieve Instance",
160 expectedCode: http.StatusInternalServerError,
161 instClient: &mockInstanceClient{
162 err: pkgerrors.New("Internal error"),
166 label: "Succesful get an Instance",
168 expectedCode: http.StatusOK,
169 expectedResponse: brokerGETResponse{
170 TemplateType: "heat",
171 WorkloadID: "HaKpys8e",
172 WorkloadStatus: "CREATED",
174 instClient: &mockInstanceClient{
175 items: []app.InstanceResponse{
178 RBName: "test-rbdef",
180 ProfileName: "profile1",
181 CloudRegion: "region1",
182 Namespace: "testnamespace",
183 Resources: []helm.KubernetesResource{
185 GVK: schema.GroupVersionKind{
189 Name: "test-deployment",
192 GVK: schema.GroupVersionKind{
196 Name: "test-service",
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))
210 if testCase.expectedCode != resp.StatusCode {
211 t.Fatalf("Request method returned: %v and it was expected: %v",
212 resp.StatusCode, testCase.expectedCode)
214 if resp.StatusCode == http.StatusOK {
215 var response brokerGETResponse
216 err := json.NewDecoder(resp.Body).Decode(&response)
218 t.Fatalf("Parsing the returned response got an error (%s)", err)
220 if !reflect.DeepEqual(testCase.expectedResponse, response) {
221 t.Fatalf("TestGetHandler returned:\n result=%v\n expected=%v",
222 response, testCase.expectedResponse)
229 func TestBrokerDeleteHandler(t *testing.T) {
230 testCases := []struct {
234 instClient *mockInstanceClient
237 label: "Fail to destroy VNF",
239 expectedCode: http.StatusInternalServerError,
240 instClient: &mockInstanceClient{
241 err: pkgerrors.New("Internal error"),
245 label: "Succesful delete a VNF",
247 expectedCode: http.StatusAccepted,
248 instClient: &mockInstanceClient{},
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))
257 if testCase.expectedCode != resp.StatusCode {
258 t.Fatalf("Request method returned: %v and it was expected: %v", resp.StatusCode, testCase.expectedCode)