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.
28 "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb"
30 pkgerrors "github.com/pkg/errors"
33 //Creating an embedded interface via anonymous variable
34 //This allows us to make mockDB satisfy the DatabaseConnection
35 //interface even if we are not implementing all the methods in it
36 type mockRBProfile struct {
38 // Items and err will be used to customize each test
39 // via a localized instantiation of mockRBProfile
44 func (m *mockRBProfile) Create(inp rb.Profile) (rb.Profile, error) {
46 return rb.Profile{}, m.Err
49 return m.Items[0], nil
52 func (m *mockRBProfile) Get(rbname, rbversion, prname string) (rb.Profile, error) {
54 return rb.Profile{}, m.Err
57 return m.Items[0], nil
60 func (m *mockRBProfile) Delete(rbname, rbversion, prname string) error {
64 func (m *mockRBProfile) Upload(rbname, rbversion, prname string, inp []byte) error {
68 func TestRBProfileCreateHandler(t *testing.T) {
69 testCases := []struct {
74 rbProClient *mockRBProfile
77 label: "Missing Body Failure",
78 expectedCode: http.StatusBadRequest,
79 rbProClient: &mockRBProfile{},
83 label: "Create New Profile for Definition",
84 expectedCode: http.StatusCreated,
85 reader: bytes.NewBuffer([]byte(`{
86 "rb-name":"test-rbdef",
88 "profile-name":"profile1",
89 "release-name":"testprofilereleasename",
90 "namespace":"default",
91 "kubernetes-version":"1.12.3"
96 ProfileName: "profile1",
97 ReleaseName: "testprofilereleasename",
99 KubernetesVersion: "1.12.3",
101 rbProClient: &mockRBProfile{
102 //Items that will be returned by the mocked Client
105 RBName: "test-rbdef",
107 ProfileName: "profile1",
108 ReleaseName: "testprofilereleasename",
109 Namespace: "default",
110 KubernetesVersion: "1.12.3",
117 for _, testCase := range testCases {
118 t.Run(testCase.label, func(t *testing.T) {
119 request := httptest.NewRequest("POST", "/v1/rb/definition/test-rbdef/v1/profile",
121 resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil))
123 //Check returned code
124 if resp.StatusCode != testCase.expectedCode {
125 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
128 //Check returned body only if statusCreated
129 if resp.StatusCode == http.StatusCreated {
131 json.NewDecoder(resp.Body).Decode(&got)
133 if reflect.DeepEqual(testCase.expected, got) == false {
134 t.Errorf("createHandler returned unexpected body: got %v;"+
135 " expected %v", got, testCase.expected)
142 func TestRBProfileGetHandler(t *testing.T) {
144 testCases := []struct {
149 rbProClient *mockRBProfile
152 label: "Get Bundle Profile",
153 expectedCode: http.StatusOK,
154 expected: rb.Profile{
155 RBName: "test-rbdef",
157 ProfileName: "profile1",
158 ReleaseName: "testprofilereleasename",
159 Namespace: "default",
160 KubernetesVersion: "1.12.3",
163 rbProClient: &mockRBProfile{
164 // Profile that will be returned by the mockclient
167 RBName: "test-rbdef",
169 ProfileName: "profile1",
170 ReleaseName: "testprofilereleasename",
171 Namespace: "default",
172 KubernetesVersion: "1.12.3",
178 label: "Get Non-Exiting Bundle Profile",
179 expectedCode: http.StatusInternalServerError,
180 prname: "non-existing-profile",
181 rbProClient: &mockRBProfile{
182 // list of Profiles that will be returned by the mockclient
183 Items: []rb.Profile{},
184 Err: pkgerrors.New("Internal Error"),
189 for _, testCase := range testCases {
190 t.Run(testCase.label, func(t *testing.T) {
191 request := httptest.NewRequest("GET", "/v1/rb/definition/test-rbdef/v1/profile/"+testCase.prname, nil)
192 resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil))
194 //Check returned code
195 if resp.StatusCode != testCase.expectedCode {
196 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
199 //Check returned body only if statusOK
200 if resp.StatusCode == http.StatusOK {
202 json.NewDecoder(resp.Body).Decode(&got)
204 if reflect.DeepEqual(testCase.expected, got) == false {
205 t.Errorf("listHandler returned unexpected body: got %v;"+
206 " expected %v", got, testCase.expected)
213 func TestRBProfileDeleteHandler(t *testing.T) {
215 testCases := []struct {
219 rbProClient *mockRBProfile
222 label: "Delete Bundle Profile",
223 expectedCode: http.StatusNoContent,
225 rbProClient: &mockRBProfile{},
228 label: "Delete Non-Exiting Bundle Profile",
229 expectedCode: http.StatusInternalServerError,
230 prname: "non-existing",
231 rbProClient: &mockRBProfile{
232 Err: pkgerrors.New("Internal Error"),
237 for _, testCase := range testCases {
238 t.Run(testCase.label, func(t *testing.T) {
239 request := httptest.NewRequest("DELETE", "/v1/rb/definition/test-rbdef/v1/profile/"+testCase.prname, nil)
240 resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil))
242 //Check returned code
243 if resp.StatusCode != testCase.expectedCode {
244 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
250 func TestRBProfileUploadHandler(t *testing.T) {
252 testCases := []struct {
257 rbProClient *mockRBProfile
260 label: "Upload Bundle Profile Content",
261 expectedCode: http.StatusOK,
263 body: bytes.NewBuffer([]byte{
264 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
265 0x00, 0xff, 0xf2, 0x48, 0xcd,
267 rbProClient: &mockRBProfile{},
270 label: "Upload Invalid Bundle Profile Content",
271 expectedCode: http.StatusInternalServerError,
273 body: bytes.NewBuffer([]byte{
274 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
275 0x00, 0xff, 0xf2, 0x48, 0xcd,
277 rbProClient: &mockRBProfile{
278 Err: pkgerrors.New("Internal Error"),
282 label: "Upload Empty Body Content",
283 expectedCode: http.StatusBadRequest,
285 rbProClient: &mockRBProfile{},
289 for _, testCase := range testCases {
290 t.Run(testCase.label, func(t *testing.T) {
291 request := httptest.NewRequest("POST",
292 "/v1/rb/definition/test-rbdef/v1/profile/"+testCase.prname+"/content", testCase.body)
293 resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil))
295 //Check returned code
296 if resp.StatusCode != testCase.expectedCode {
297 t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)