4 * Copyright 2018 Intel Corporation, Inc
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 "k8splugin/internal/db"
29 pkgerrors "github.com/pkg/errors"
32 func TestCreateProfile(t *testing.T) {
33 testCases := []struct {
41 label: "Create Resource Bundle Profile",
43 UUID: "123e4567-e89b-12d3-a456-426655440000",
44 RBDID: "abcde123-e89b-8888-a456-986655447236",
45 Name: "testresourcebundle",
47 KubernetesVersion: "1.12.3",
50 UUID: "123e4567-e89b-12d3-a456-426655440000",
51 RBDID: "abcde123-e89b-8888-a456-986655447236",
52 Name: "testresourcebundle",
54 KubernetesVersion: "1.12.3",
58 Items: map[string]map[string][]byte{
59 "abcde123-e89b-8888-a456-986655447236": {
61 "{\"name\":\"testresourcebundle\"," +
62 "\"namespace\":\"default\"," +
63 "\"uuid\":\"123e4567-e89b-12d3-a456-426655440000\"," +
64 "\"kubernetesversion\":\"1.12.3\"}"),
70 label: "Failed Create Resource Bundle Profile",
71 expectedError: "Error Creating Profile",
73 Err: pkgerrors.New("Error Creating Profile"),
78 for _, testCase := range testCases {
79 t.Run(testCase.label, func(t *testing.T) {
80 db.DBconn = testCase.mockdb
81 impl := NewProfileClient()
82 got, err := impl.Create(testCase.inp)
84 if testCase.expectedError == "" {
85 t.Fatalf("Create returned an unexpected error %s", err)
87 if strings.Contains(err.Error(), testCase.expectedError) == false {
88 t.Fatalf("Create returned an unexpected error %s", err)
91 if reflect.DeepEqual(testCase.expected, got) == false {
92 t.Errorf("Create Resource Bundle returned unexpected body: got %v;"+
93 " expected %v", got, testCase.expected)
100 func TestListProfiles(t *testing.T) {
102 testCases := []struct {
109 label: "List Resource Bundle Profile",
112 UUID: "123e4567-e89b-12d3-a456-426655440000",
113 RBDID: "abcde123-e89b-8888-a456-986655447236",
114 Name: "testresourcebundle",
115 Namespace: "default",
116 KubernetesVersion: "1.12.3",
121 Items: map[string]map[string][]byte{
122 "123e4567-e89b-12d3-a456-426655440000": {
124 "{\"name\":\"testresourcebundle\"," +
125 "\"namespace\":\"default\"," +
126 "\"uuid\":\"123e4567-e89b-12d3-a456-426655440000\"," +
127 "\"rbdid\":\"abcde123-e89b-8888-a456-986655447236\"," +
128 "\"kubernetesversion\":\"1.12.3\"}"),
135 expectedError: "DB Error",
137 Err: pkgerrors.New("DB Error"),
142 for _, testCase := range testCases {
143 t.Run(testCase.label, func(t *testing.T) {
144 db.DBconn = testCase.mockdb
145 impl := NewProfileClient()
146 got, err := impl.List()
148 if testCase.expectedError == "" {
149 t.Fatalf("List returned an unexpected error %s", err)
151 if strings.Contains(err.Error(), testCase.expectedError) == false {
152 t.Fatalf("List returned an unexpected error %s", err)
155 // Since the order of returned slice is not guaranteed
156 // Check both and return error if both don't match
157 sort.Slice(got, func(i, j int) bool {
158 return got[i].UUID < got[j].UUID
160 // Sort both as it is not expected that testCase.expected
162 sort.Slice(testCase.expected, func(i, j int) bool {
163 return testCase.expected[i].UUID < testCase.expected[j].UUID
166 if reflect.DeepEqual(testCase.expected, got) == false {
167 t.Errorf("List Resource Bundle returned unexpected body: got %v;"+
168 " expected %v", got, testCase.expected)
175 func TestGetProfile(t *testing.T) {
177 testCases := []struct {
185 label: "Get Resource Bundle Profile",
186 inp: "123e4567-e89b-12d3-a456-426655440000",
188 UUID: "123e4567-e89b-12d3-a456-426655440000",
189 RBDID: "abcde123-e89b-8888-a456-986655447236",
190 Name: "testresourcebundle",
191 Namespace: "default",
192 KubernetesVersion: "1.12.3",
196 Items: map[string]map[string][]byte{
197 "123e4567-e89b-12d3-a456-426655440000": {
199 "{\"name\":\"testresourcebundle\"," +
200 "\"namespace\":\"default\"," +
201 "\"uuid\":\"123e4567-e89b-12d3-a456-426655440000\"," +
202 "\"rbdid\":\"abcde123-e89b-8888-a456-986655447236\"," +
203 "\"kubernetesversion\":\"1.12.3\"}"),
210 expectedError: "DB Error",
212 Err: pkgerrors.New("DB Error"),
217 for _, testCase := range testCases {
218 t.Run(testCase.label, func(t *testing.T) {
219 db.DBconn = testCase.mockdb
220 impl := NewProfileClient()
221 got, err := impl.Get(testCase.inp)
223 if testCase.expectedError == "" {
224 t.Fatalf("Get returned an unexpected error %s", err)
226 if strings.Contains(err.Error(), testCase.expectedError) == false {
227 t.Fatalf("Get returned an unexpected error %s", err)
230 if reflect.DeepEqual(testCase.expected, got) == false {
231 t.Errorf("Get Resource Bundle returned unexpected body: got %v;"+
232 " expected %v", got, testCase.expected)
239 func TestDeleteProfile(t *testing.T) {
241 testCases := []struct {
248 label: "Delete Resource Bundle Profile",
249 inp: "123e4567-e89b-12d3-a456-426655440000",
250 mockdb: &db.MockDB{},
253 label: "Delete Error",
254 expectedError: "DB Error",
256 Err: pkgerrors.New("DB Error"),
261 for _, testCase := range testCases {
262 t.Run(testCase.label, func(t *testing.T) {
263 db.DBconn = testCase.mockdb
264 impl := NewProfileClient()
265 err := impl.Delete(testCase.inp)
267 if testCase.expectedError == "" {
268 t.Fatalf("Delete returned an unexpected error %s", err)
270 if strings.Contains(err.Error(), testCase.expectedError) == false {
271 t.Fatalf("Delete returned an unexpected error %s", err)
278 func TestUploadProfile(t *testing.T) {
279 testCases := []struct {
287 label: "Upload Resource Bundle Profile",
288 inp: "123e4567-e89b-12d3-a456-426655440000",
290 0x1f, 0x8b, 0x08, 0x08, 0xb0, 0x6b, 0xf4, 0x5b,
291 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74,
292 0x61, 0x72, 0x00, 0xed, 0xce, 0x41, 0x0a, 0xc2,
293 0x30, 0x10, 0x85, 0xe1, 0xac, 0x3d, 0x45, 0x4e,
294 0x50, 0x12, 0xd2, 0xc4, 0xe3, 0x48, 0xa0, 0x01,
295 0x4b, 0x52, 0x0b, 0xed, 0x88, 0x1e, 0xdf, 0x48,
296 0x11, 0x5c, 0x08, 0xa5, 0x8b, 0x52, 0x84, 0xff,
297 0xdb, 0xbc, 0x61, 0x66, 0x16, 0x4f, 0xd2, 0x2c,
298 0x8d, 0x3c, 0x45, 0xed, 0xc8, 0x54, 0x21, 0xb4,
299 0xef, 0xb4, 0x67, 0x6f, 0xbe, 0x73, 0x61, 0x9d,
300 0xb2, 0xce, 0xd5, 0x55, 0xf0, 0xde, 0xd7, 0x3f,
301 0xdb, 0xd6, 0x49, 0x69, 0xb3, 0x67, 0xa9, 0x8f,
302 0xfb, 0x2c, 0x71, 0xd2, 0x5a, 0xc5, 0xee, 0x92,
303 0x73, 0x8e, 0x43, 0x7f, 0x4b, 0x3f, 0xff, 0xd6,
304 0xee, 0x7f, 0xea, 0x9a, 0x4a, 0x19, 0x1f, 0xe3,
305 0x54, 0xba, 0xd3, 0xd1, 0x55, 0x00, 0x00, 0x00,
306 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
307 0x00, 0x00, 0x00, 0x1b, 0xbc, 0x00, 0xb5, 0xe8,
308 0x4a, 0xf9, 0x00, 0x28, 0x00, 0x00,
311 Items: map[string]map[string][]byte{
312 "123e4567-e89b-12d3-a456-426655440000": {
314 "{\"name\":\"testresourcebundle\"," +
315 "\"namespace\":\"default\"," +
316 "\"uuid\":\"123e4567-e89b-12d3-a456-426655440000\"," +
317 "\"rbdid\":\"abcde123-e89b-8888-a456-986655447236\"," +
318 "\"kubernetesversion\":\"1.12.3\"}"),
324 label: "Upload with an Invalid Resource Bundle Profile",
325 inp: "123e4567-e89b-12d3-a456-426655440000",
326 expectedError: "Invalid Profile ID provided",
328 0x1f, 0x8b, 0x08, 0x08, 0xb0, 0x6b, 0xf4, 0x5b,
329 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74,
330 0x61, 0x72, 0x00, 0xed, 0xce, 0x41, 0x0a, 0xc2,
331 0x30, 0x10, 0x85, 0xe1, 0xac, 0x3d, 0x45, 0x4e,
332 0x50, 0x12, 0xd2, 0xc4, 0xe3, 0x48, 0xa0, 0x01,
333 0x4b, 0x52, 0x0b, 0xed, 0x88, 0x1e, 0xdf, 0x48,
334 0x11, 0x5c, 0x08, 0xa5, 0x8b, 0x52, 0x84, 0xff,
335 0xdb, 0xbc, 0x61, 0x66, 0x16, 0x4f, 0xd2, 0x2c,
336 0x8d, 0x3c, 0x45, 0xed, 0xc8, 0x54, 0x21, 0xb4,
337 0xef, 0xb4, 0x67, 0x6f, 0xbe, 0x73, 0x61, 0x9d,
338 0xb2, 0xce, 0xd5, 0x55, 0xf0, 0xde, 0xd7, 0x3f,
339 0xdb, 0xd6, 0x49, 0x69, 0xb3, 0x67, 0xa9, 0x8f,
340 0xfb, 0x2c, 0x71, 0xd2, 0x5a, 0xc5, 0xee, 0x92,
341 0x73, 0x8e, 0x43, 0x7f, 0x4b, 0x3f, 0xff, 0xd6,
342 0xee, 0x7f, 0xea, 0x9a, 0x4a, 0x19, 0x1f, 0xe3,
343 0x54, 0xba, 0xd3, 0xd1, 0x55, 0x00, 0x00, 0x00,
344 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
345 0x00, 0x00, 0x00, 0x1b, 0xbc, 0x00, 0xb5, 0xe8,
346 0x4a, 0xf9, 0x00, 0x28, 0x00, 0x00,
349 Items: map[string]map[string][]byte{
350 "123e4567-e89b-12d3-a456-426655441111": {
352 "{\"name\":\"testresourcebundle\"," +
353 "\"namespace\":\"default\"," +
354 "\"uuid\":\"123e4567-e89b-12d3-a456-426655441111\"," +
355 "\"rbdid\":\"abcde123-e89b-8888-a456-986655447236\"," +
356 "\"kubernetesversion\":\"1.12.3\"}"),
362 label: "Invalid File Format Error",
363 inp: "123e4567-e89b-12d3-a456-426655440000",
364 expectedError: "Error in file format",
366 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
367 0x00, 0xff, 0xf2, 0x48, 0xcd,
370 Items: map[string]map[string][]byte{
371 "123e4567-e89b-12d3-a456-426655440000": {
373 "{\"name\":\"testresourcebundle\"," +
374 "\"namespace\":\"default\"," +
375 "\"uuid\":\"123e4567-e89b-12d3-a456-426655440000\"," +
376 "\"rbdid\":\"abcde123-e89b-8888-a456-986655447236\"," +
377 "\"kubernetesversion\":\"1.12.3\"}"),
383 label: "Upload Error",
384 expectedError: "DB Error",
386 0x1f, 0x8b, 0x08, 0x08, 0xb0, 0x6b, 0xf4, 0x5b,
387 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74,
388 0x61, 0x72, 0x00, 0xed, 0xce, 0x41, 0x0a, 0xc2,
389 0x30, 0x10, 0x85, 0xe1, 0xac, 0x3d, 0x45, 0x4e,
390 0x50, 0x12, 0xd2, 0xc4, 0xe3, 0x48, 0xa0, 0x01,
391 0x4b, 0x52, 0x0b, 0xed, 0x88, 0x1e, 0xdf, 0x48,
392 0x11, 0x5c, 0x08, 0xa5, 0x8b, 0x52, 0x84, 0xff,
393 0xdb, 0xbc, 0x61, 0x66, 0x16, 0x4f, 0xd2, 0x2c,
394 0x8d, 0x3c, 0x45, 0xed, 0xc8, 0x54, 0x21, 0xb4,
395 0xef, 0xb4, 0x67, 0x6f, 0xbe, 0x73, 0x61, 0x9d,
396 0xb2, 0xce, 0xd5, 0x55, 0xf0, 0xde, 0xd7, 0x3f,
397 0xdb, 0xd6, 0x49, 0x69, 0xb3, 0x67, 0xa9, 0x8f,
398 0xfb, 0x2c, 0x71, 0xd2, 0x5a, 0xc5, 0xee, 0x92,
399 0x73, 0x8e, 0x43, 0x7f, 0x4b, 0x3f, 0xff, 0xd6,
400 0xee, 0x7f, 0xea, 0x9a, 0x4a, 0x19, 0x1f, 0xe3,
401 0x54, 0xba, 0xd3, 0xd1, 0x55, 0x00, 0x00, 0x00,
402 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
403 0x00, 0x00, 0x00, 0x1b, 0xbc, 0x00, 0xb5, 0xe8,
404 0x4a, 0xf9, 0x00, 0x28, 0x00, 0x00,
407 Err: pkgerrors.New("DB Error"),
412 for _, testCase := range testCases {
413 t.Run(testCase.label, func(t *testing.T) {
414 db.DBconn = testCase.mockdb
415 impl := NewProfileClient()
416 err := impl.Upload(testCase.inp, testCase.content)
418 if testCase.expectedError == "" {
419 t.Errorf("Upload returned an unexpected error %s", err)
421 if strings.Contains(err.Error(), testCase.expectedError) == false {
422 t.Errorf("Upload returned an unexpected error %s", err)
429 func TestDownloadProfile(t *testing.T) {
430 testCases := []struct {
438 label: "Download Resource Bundle Profile",
439 inp: "123e4567-e89b-12d3-a456-426655440000",
441 0x1f, 0x8b, 0x08, 0x08, 0xb0, 0x6b, 0xf4, 0x5b,
442 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74,
443 0x61, 0x72, 0x00, 0xed, 0xce, 0x41, 0x0a, 0xc2,
444 0x30, 0x10, 0x85, 0xe1, 0xac, 0x3d, 0x45, 0x4e,
445 0x50, 0x12, 0xd2, 0xc4, 0xe3, 0x48, 0xa0, 0x01,
446 0x4b, 0x52, 0x0b, 0xed, 0x88, 0x1e, 0xdf, 0x48,
447 0x11, 0x5c, 0x08, 0xa5, 0x8b, 0x52, 0x84, 0xff,
448 0xdb, 0xbc, 0x61, 0x66, 0x16, 0x4f, 0xd2, 0x2c,
449 0x8d, 0x3c, 0x45, 0xed, 0xc8, 0x54, 0x21, 0xb4,
450 0xef, 0xb4, 0x67, 0x6f, 0xbe, 0x73, 0x61, 0x9d,
451 0xb2, 0xce, 0xd5, 0x55, 0xf0, 0xde, 0xd7, 0x3f,
452 0xdb, 0xd6, 0x49, 0x69, 0xb3, 0x67, 0xa9, 0x8f,
453 0xfb, 0x2c, 0x71, 0xd2, 0x5a, 0xc5, 0xee, 0x92,
454 0x73, 0x8e, 0x43, 0x7f, 0x4b, 0x3f, 0xff, 0xd6,
455 0xee, 0x7f, 0xea, 0x9a, 0x4a, 0x19, 0x1f, 0xe3,
456 0x54, 0xba, 0xd3, 0xd1, 0x55, 0x00, 0x00, 0x00,
457 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
458 0x00, 0x00, 0x00, 0x1b, 0xbc, 0x00, 0xb5, 0xe8,
459 0x4a, 0xf9, 0x00, 0x28, 0x00, 0x00,
462 Items: map[string]map[string][]byte{
463 "123e4567-e89b-12d3-a456-426655440000": {
465 "{\"name\":\"testresourcebundle\"," +
466 "\"namespace\":\"default\"," +
467 "\"uuid\":\"123e4567-e89b-12d3-a456-426655440000\"," +
468 "\"rbdid\":\"abcde123-e89b-8888-a456-986655447236\"," +
469 "\"kubernetesversion\":\"1.12.3\"}"),
470 "content": []byte("H4sICLBr9FsAA3Rlc3QudGFyAO3OQQrCMBCF4aw9RU5" +
471 "QEtLE40igAUtSC+2IHt9IEVwIpYtShP/bvGFmFk/SLI08Re3IVCG077Rn" +
472 "b75zYZ2yztVV8N7XP9vWSWmzZ6mP+yxx0lrF7pJzjkN/Sz//1u5/6ppKG" +
473 "R/jVLrT0VUAAAAAAAAAAAAAAAAAABu8ALXoSvkAKAAA"),
479 label: "Download with an Invalid Resource Bundle Profile",
480 inp: "123e4567-e89b-12d3-a456-426655440000",
481 expectedError: "Invalid Profile ID provided",
483 Items: map[string]map[string][]byte{
484 "123e4567-e89b-12d3-a456-426655441111": {
486 "{\"name\":\"testresourcebundle\"," +
487 "\"uuid\":\"123e4567-e89b-12d3-a456-426655441111\"," +
488 "\"namespace\":\"default\"," +
489 "\"rbdid\":\"abcde123-e89b-8888-a456-986655447236\"," +
490 "\"kubernetesversion\":\"1.12.3\"}"),
496 label: "Download Error",
497 expectedError: "DB Error",
498 inp: "123e4567-e89b-12d3-a456-426655440000",
500 Err: pkgerrors.New("DB Error"),
505 for _, testCase := range testCases {
506 t.Run(testCase.label, func(t *testing.T) {
507 db.DBconn = testCase.mockdb
508 impl := NewProfileClient()
509 data, err := impl.Download(testCase.inp)
511 if testCase.expectedError == "" {
512 t.Errorf("Download returned an unexpected error %s", err)
514 if strings.Contains(err.Error(), testCase.expectedError) == false {
515 t.Errorf("Download returned an unexpected error %s", err)
518 if bytes.Equal(testCase.expected, data) == false {
519 t.Errorf("Download returned unexpected data: got %v - expected %v",
520 data, testCase.expected)