2 * Copyright 2018 Intel Corporation, Inc
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
29 "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb"
31 pkgerrors "github.com/pkg/errors"
34 //Creating an embedded interface via anonymous variable
35 //This allows us to make mockDB satisfy the DatabaseConnection
36 //interface even if we are not implementing all the methods in it
37 type mockRBProfile struct {
39 // Items and err will be used to customize each test
40 // via a localized instantiation of mockRBProfile
45 func (m *mockRBProfile) Create(inp rb.Profile) (rb.Profile, error) {
47 return rb.Profile{}, m.Err
50 return m.Items[0], nil
53 func (m *mockRBProfile) Get(rbname, rbversion, prname string) (rb.Profile, error) {
55 return rb.Profile{}, m.Err
58 return m.Items[0], nil
61 func (m *mockRBProfile) List(rbname, rbversion string) ([]rb.Profile, error) {
63 return []rb.Profile{}, m.Err
69 func (m *mockRBProfile) Delete(rbname, rbversion, prname string) error {
73 func (m *mockRBProfile) Upload(rbname, rbversion, prname string, inp []byte) error {
77 func TestRBProfileCreateHandler(t *testing.T) {
78 testCases := []struct {
83 rbProClient *mockRBProfile
86 label: "Missing Body Failure",
87 expectedCode: http.StatusBadRequest,
88 rbProClient: &mockRBProfile{},
92 label: "Create New Profile for Definition",
93 expectedCode: http.StatusCreated,
94 reader: bytes.NewBuffer([]byte(`{
95 "rb-name":"test-rbdef",
97 "profile-name":"profile1",
98 "release-name":"testprofilereleasename",
99 "namespace":"default",
100 "kubernetes-version":"1.12.3"
102 expected: rb.Profile{
103 RBName: "test-rbdef",
105 ProfileName: "profile1",
106 ReleaseName: "testprofilereleasename",
107 Namespace: "default",
108 KubernetesVersion: "1.12.3",
110 rbProClient: &mockRBProfile{
111 //Items that will be returned by the mocked Client
114 RBName: "test-rbdef",
116 ProfileName: "profile1",
117 ReleaseName: "testprofilereleasename",
118 Namespace: "default",
119 KubernetesVersion: "1.12.3",
126 for _, testCase := range testCases {
127 t.Run(testCase.label, func(t *testing.T) {
128 request := httptest.NewRequest("POST", "/v1/rb/definition/test-rbdef/v1/profile",
130 resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil, nil, nil))
132 //Check returned code
133 if resp.StatusCode != testCase.expectedCode {
134 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
137 //Check returned body only if statusCreated
138 if resp.StatusCode == http.StatusCreated {
140 json.NewDecoder(resp.Body).Decode(&got)
142 if reflect.DeepEqual(testCase.expected, got) == false {
143 t.Errorf("createHandler returned unexpected body: got %v;"+
144 " expected %v", got, testCase.expected)
151 func TestRBProfileGetHandler(t *testing.T) {
153 testCases := []struct {
158 rbProClient *mockRBProfile
161 label: "Get Bundle Profile",
162 expectedCode: http.StatusOK,
163 expected: rb.Profile{
164 RBName: "test-rbdef",
166 ProfileName: "profile1",
167 ReleaseName: "testprofilereleasename",
168 Namespace: "default",
169 KubernetesVersion: "1.12.3",
172 rbProClient: &mockRBProfile{
173 // Profile that will be returned by the mockclient
176 RBName: "test-rbdef",
178 ProfileName: "profile1",
179 ReleaseName: "testprofilereleasename",
180 Namespace: "default",
181 KubernetesVersion: "1.12.3",
187 label: "Get Non-Existing Profile",
188 expectedCode: http.StatusNotFound,
189 prname: "non-existing-profile",
190 rbProClient: &mockRBProfile{
192 Err: pkgerrors.New("Error finding master table"),
196 label: "Faulty DB response",
197 expectedCode: http.StatusInternalServerError,
199 rbProClient: &mockRBProfile{
200 // list of Profiles that will be returned by the mockclient
201 Items: []rb.Profile{},
202 Err: pkgerrors.New("Internal Error"),
207 for _, testCase := range testCases {
208 t.Run(testCase.label, func(t *testing.T) {
209 request := httptest.NewRequest("GET", "/v1/rb/definition/test-rbdef/v1/profile/"+testCase.prname, nil)
210 resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil, nil, nil))
212 //Check returned code
213 if resp.StatusCode != testCase.expectedCode {
214 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
217 //Check returned body only if statusOK
218 if resp.StatusCode == http.StatusOK {
220 json.NewDecoder(resp.Body).Decode(&got)
222 if reflect.DeepEqual(testCase.expected, got) == false {
223 t.Errorf("listHandler returned unexpected body: got %v;"+
224 " expected %v", got, testCase.expected)
231 func TestRBProfileListHandler(t *testing.T) {
233 testCases := []struct {
237 expected []rb.Profile
239 rbProClient *mockRBProfile
244 label: "List Profiles",
245 expectedCode: http.StatusOK,
246 expected: []rb.Profile{
248 RBName: "test-rbdef",
250 ProfileName: "profile1",
251 ReleaseName: "testprofilereleasename",
253 KubernetesVersion: "1.12.3",
256 RBName: "test-rbdef",
258 ProfileName: "profile2",
259 ReleaseName: "testprofilereleasename",
261 KubernetesVersion: "1.12.3",
264 rbProClient: &mockRBProfile{
265 // list of Profiles that will be returned by the mockclient
268 RBName: "test-rbdef",
270 ProfileName: "profile1",
271 ReleaseName: "testprofilereleasename",
273 KubernetesVersion: "1.12.3",
276 RBName: "test-rbdef",
278 ProfileName: "profile2",
279 ReleaseName: "testprofilereleasename",
281 KubernetesVersion: "1.12.3",
288 for _, testCase := range testCases {
289 t.Run(testCase.label, func(t *testing.T) {
290 request := httptest.NewRequest("GET", "/v1/rb/definition/"+testCase.def+"/"+testCase.version+"/profile", nil)
291 resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil, nil, nil))
293 //Check returned code
294 if resp.StatusCode != testCase.expectedCode {
295 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
298 //Check returned body only if statusOK
299 if resp.StatusCode == http.StatusOK {
300 got := []rb.Profile{}
301 json.NewDecoder(resp.Body).Decode(&got)
303 // Since the order of returned slice is not guaranteed
304 // Check both and return error if both don't match
305 sort.Slice(got, func(i, j int) bool {
306 return got[i].ProfileName < got[j].ProfileName
308 // Sort both as it is not expected that testCase.expected
310 sort.Slice(testCase.expected, func(i, j int) bool {
311 return testCase.expected[i].ProfileName < testCase.expected[j].ProfileName
314 if reflect.DeepEqual(testCase.expected, got) == false {
315 t.Errorf("listHandler returned unexpected body: got %v;"+
316 " expected %v", got, testCase.expected)
323 func TestRBProfileDeleteHandler(t *testing.T) {
325 testCases := []struct {
329 rbProClient *mockRBProfile
332 label: "Delete Bundle Profile",
333 expectedCode: http.StatusNoContent,
335 rbProClient: &mockRBProfile{},
338 label: "Delete Non-Exiting Bundle Profile",
339 expectedCode: http.StatusInternalServerError,
340 prname: "non-existing",
341 rbProClient: &mockRBProfile{
342 Err: pkgerrors.New("Internal Error"),
347 for _, testCase := range testCases {
348 t.Run(testCase.label, func(t *testing.T) {
349 request := httptest.NewRequest("DELETE", "/v1/rb/definition/test-rbdef/v1/profile/"+testCase.prname, nil)
350 resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil, nil, nil))
352 //Check returned code
353 if resp.StatusCode != testCase.expectedCode {
354 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
360 func TestRBProfileUploadHandler(t *testing.T) {
362 testCases := []struct {
367 rbProClient *mockRBProfile
370 label: "Upload Bundle Profile Content",
371 expectedCode: http.StatusOK,
373 body: bytes.NewBuffer([]byte{
374 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
375 0x00, 0xff, 0xf2, 0x48, 0xcd,
377 rbProClient: &mockRBProfile{},
380 label: "Upload Invalid Bundle Profile Content",
381 expectedCode: http.StatusInternalServerError,
383 body: bytes.NewBuffer([]byte{
384 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
385 0x00, 0xff, 0xf2, 0x48, 0xcd,
387 rbProClient: &mockRBProfile{
388 Err: pkgerrors.New("Internal Error"),
392 label: "Upload Empty Body Content",
393 expectedCode: http.StatusBadRequest,
395 rbProClient: &mockRBProfile{},
399 for _, testCase := range testCases {
400 t.Run(testCase.label, func(t *testing.T) {
401 request := httptest.NewRequest("POST",
402 "/v1/rb/definition/test-rbdef/v1/profile/"+testCase.prname+"/content", testCase.body)
403 resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil, nil, nil))
405 //Check returned code
406 if resp.StatusCode != testCase.expectedCode {
407 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)