Resource sync microservice files.
[multicloud/k8s.git] / src / rsync / pkg / internal / rb / definition_test.go
1 /*
2  * Copyright 2018 Intel Corporation, Inc
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package rb
18
19 import (
20         "bytes"
21         "github.com/onap/multicloud-k8s/src/k8splugin/internal/db"
22         "reflect"
23         "sort"
24         "strings"
25         "testing"
26
27         pkgerrors "github.com/pkg/errors"
28 )
29
30 func TestCreateDefinition(t *testing.T) {
31         testCases := []struct {
32                 label         string
33                 inp           Definition
34                 expectedError string
35                 mockdb        *db.MockDB
36                 expected      Definition
37         }{
38                 {
39                         label: "Create Resource Bundle Definition",
40                         inp: Definition{
41                                 RBName:      "testresourcebundle",
42                                 RBVersion:   "v1",
43                                 Description: "testresourcebundle",
44                                 ChartName:   "",
45                         },
46                         expected: Definition{
47                                 RBName:      "testresourcebundle",
48                                 RBVersion:   "v1",
49                                 Description: "testresourcebundle",
50                                 ChartName:   "",
51                         },
52                         expectedError: "",
53                         mockdb:        &db.MockDB{},
54                 },
55                 {
56                         label:         "Failed Create Resource Bundle Definition",
57                         expectedError: "Error Creating Definition",
58                         mockdb: &db.MockDB{
59                                 Err: pkgerrors.New("Error Creating Definition"),
60                         },
61                 },
62         }
63
64         for _, testCase := range testCases {
65                 t.Run(testCase.label, func(t *testing.T) {
66                         db.DBconn = testCase.mockdb
67                         impl := NewDefinitionClient()
68                         got, err := impl.Create(testCase.inp)
69                         if err != nil {
70                                 if testCase.expectedError == "" {
71                                         t.Fatalf("Create returned an unexpected error %s", err)
72                                 }
73                                 if strings.Contains(err.Error(), testCase.expectedError) == false {
74                                         t.Fatalf("Create returned an unexpected error %s", err)
75                                 }
76                         } else {
77                                 if reflect.DeepEqual(testCase.expected, got) == false {
78                                         t.Errorf("Create Resource Bundle returned unexpected body: got %v;"+
79                                                 " expected %v", got, testCase.expected)
80                                 }
81                         }
82                 })
83         }
84 }
85
86 func TestListDefinition(t *testing.T) {
87
88         testCases := []struct {
89                 label         string
90                 name          string
91                 expectedError string
92                 mockdb        *db.MockDB
93                 expected      []Definition
94         }{
95                 {
96                         label: "List Resource Bundle Definition",
97                         name:  "testresourcebundle",
98                         expected: []Definition{
99                                 {
100                                         RBName:      "testresourcebundle",
101                                         RBVersion:   "v1",
102                                         Description: "testresourcebundle",
103                                         ChartName:   "testchart",
104                                 },
105                                 {
106                                         RBName:      "testresourcebundle",
107                                         RBVersion:   "v2",
108                                         Description: "testresourcebundle_version2",
109                                         ChartName:   "testchart",
110                                 },
111                         },
112                         expectedError: "",
113                         mockdb: &db.MockDB{
114                                 Items: map[string]map[string][]byte{
115                                         DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): {
116                                                 "defmetadata": []byte(
117                                                         "{\"rb-name\":\"testresourcebundle\"," +
118                                                                 "\"description\":\"testresourcebundle\"," +
119                                                                 "\"rb-version\":\"v1\"," +
120                                                                 "\"chart-name\":\"testchart\"}"),
121                                         },
122                                         DefinitionKey{RBName: "testresourcebundle", RBVersion: "v2"}.String(): {
123                                                 "defmetadata": []byte(
124                                                         "{\"rb-name\":\"testresourcebundle\"," +
125                                                                 "\"description\":\"testresourcebundle_version2\"," +
126                                                                 "\"rb-version\":\"v2\"," +
127                                                                 "\"chart-name\":\"testchart\"}"),
128                                         },
129                                 },
130                         },
131                 },
132                 {
133                         label:         "List Error",
134                         expectedError: "DB Error",
135                         mockdb: &db.MockDB{
136                                 Err: pkgerrors.New("DB Error"),
137                         },
138                 },
139         }
140
141         for _, testCase := range testCases {
142                 t.Run(testCase.label, func(t *testing.T) {
143                         db.DBconn = testCase.mockdb
144                         impl := NewDefinitionClient()
145                         got, err := impl.List(testCase.name)
146                         if err != nil {
147                                 if testCase.expectedError == "" {
148                                         t.Fatalf("List returned an unexpected error %s", err)
149                                 }
150                                 if strings.Contains(err.Error(), testCase.expectedError) == false {
151                                         t.Fatalf("List returned an unexpected error %s", err)
152                                 }
153                         } else {
154                                 // Since the order of returned slice is not guaranteed
155                                 // Check both and return error if both don't match
156                                 sort.Slice(got, func(i, j int) bool {
157                                         return got[i].RBVersion < got[j].RBVersion
158                                 })
159                                 // Sort both as it is not expected that testCase.expected
160                                 // is sorted
161                                 sort.Slice(testCase.expected, func(i, j int) bool {
162                                         return testCase.expected[i].RBVersion < testCase.expected[j].RBVersion
163                                 })
164
165                                 if reflect.DeepEqual(testCase.expected, got) == false {
166                                         t.Errorf("List Resource Bundle returned unexpected body: got %v;"+
167                                                 " expected %v", got, testCase.expected)
168                                 }
169                         }
170                 })
171         }
172 }
173
174 func TestGetDefinition(t *testing.T) {
175
176         testCases := []struct {
177                 label         string
178                 name          string
179                 version       string
180                 expectedError string
181                 mockdb        *db.MockDB
182                 inp           string
183                 expected      Definition
184         }{
185                 {
186                         label:   "Get Resource Bundle Definition",
187                         name:    "testresourcebundle",
188                         version: "v1",
189                         expected: Definition{
190                                 RBName:      "testresourcebundle",
191                                 RBVersion:   "v1",
192                                 Description: "testresourcebundle",
193                                 ChartName:   "testchart",
194                         },
195                         expectedError: "",
196                         mockdb: &db.MockDB{
197                                 Items: map[string]map[string][]byte{
198                                         DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): {
199                                                 "defmetadata": []byte(
200                                                         "{\"rb-name\":\"testresourcebundle\"," +
201                                                                 "\"description\":\"testresourcebundle\"," +
202                                                                 "\"rb-version\":\"v1\"," +
203                                                                 "\"chart-name\":\"testchart\"}"),
204                                         },
205                                 },
206                         },
207                 },
208                 {
209                         label:         "Get Error",
210                         expectedError: "DB Error",
211                         mockdb: &db.MockDB{
212                                 Err: pkgerrors.New("DB Error"),
213                         },
214                 },
215         }
216
217         for _, testCase := range testCases {
218                 t.Run(testCase.label, func(t *testing.T) {
219                         db.DBconn = testCase.mockdb
220                         impl := NewDefinitionClient()
221                         got, err := impl.Get(testCase.name, testCase.version)
222                         if err != nil {
223                                 if testCase.expectedError == "" {
224                                         t.Fatalf("Get returned an unexpected error %s", err)
225                                 }
226                                 if strings.Contains(err.Error(), testCase.expectedError) == false {
227                                         t.Fatalf("Get returned an unexpected error %s", err)
228                                 }
229                         } else {
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)
233                                 }
234                         }
235                 })
236         }
237 }
238
239 func TestDeleteDefinition(t *testing.T) {
240
241         testCases := []struct {
242                 label         string
243                 name          string
244                 version       string
245                 expectedError string
246                 mockdb        *db.MockDB
247         }{
248                 {
249                         label:   "Delete Resource Bundle Definition",
250                         name:    "testresourcebundle",
251                         version: "v1",
252                         mockdb:  &db.MockDB{},
253                 },
254                 {
255                         label:         "Delete Error",
256                         expectedError: "DB Error",
257                         mockdb: &db.MockDB{
258                                 Err: pkgerrors.New("DB Error"),
259                         },
260                 },
261         }
262
263         for _, testCase := range testCases {
264                 t.Run(testCase.label, func(t *testing.T) {
265                         db.DBconn = testCase.mockdb
266                         impl := NewDefinitionClient()
267                         err := impl.Delete(testCase.name, testCase.version)
268                         if err != nil {
269                                 if testCase.expectedError == "" {
270                                         t.Fatalf("Delete returned an unexpected error %s", err)
271                                 }
272                                 if strings.Contains(err.Error(), testCase.expectedError) == false {
273                                         t.Fatalf("Delete returned an unexpected error %s", err)
274                                 }
275                         }
276                 })
277         }
278 }
279
280 func TestUploadDefinition(t *testing.T) {
281         testCases := []struct {
282                 label         string
283                 name, version string
284                 content       []byte
285                 expectedError string
286                 mockdb        *db.MockDB
287         }{
288                 {
289                         label:   "Upload With Chart Name Detection",
290                         name:    "testresourcebundle",
291                         version: "v1",
292                         //Binary format for testchart/Chart.yaml
293                         content: []byte{
294                                 0x1f, 0x8b, 0x08, 0x08, 0xb3, 0xeb, 0x86, 0x5c,
295                                 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x63, 0x68,
296                                 0x61, 0x72, 0x74, 0x2e, 0x74, 0x61, 0x72, 0x00,
297                                 0xed, 0xd2, 0x41, 0x4b, 0xc3, 0x30, 0x18, 0xc6,
298                                 0xf1, 0x9c, 0xfb, 0x29, 0xde, 0x4f, 0x50, 0x93,
299                                 0x36, 0x69, 0x60, 0x37, 0xd9, 0x45, 0xf0, 0xee,
300                                 0x55, 0xe2, 0x16, 0xb1, 0x74, 0xed, 0x46, 0x9a,
301                                 0x0d, 0xfc, 0xf6, 0xae, 0x83, 0x4d, 0x91, 0x89,
302                                 0x97, 0x0d, 0x91, 0xfd, 0x7f, 0x87, 0x84, 0x90,
303                                 0x90, 0xbc, 0xe1, 0x79, 0x73, 0x1c, 0xf3, 0xe2,
304                                 0x2d, 0xa4, 0x7c, 0xa7, 0xae, 0x46, 0xef, 0x79,
305                                 0xef, 0xa6, 0xd9, 0x78, 0xa7, 0xbf, 0xce, 0x47,
306                                 0xca, 0xd4, 0xd6, 0x1a, 0xd7, 0xb8, 0xa6, 0xb6,
307                                 0x4a, 0x1b, 0x5b, 0xbb, 0x4a, 0x89, 0xbb, 0x5e,
308                                 0x49, 0x9f, 0xb6, 0x63, 0x0e, 0x49, 0x44, 0x85,
309                                 0xe5, 0x73, 0xd7, 0x75, 0xa1, 0x6f, 0x87, 0x78,
310                                 0xf6, 0xdc, 0x6f, 0xfb, 0xff, 0x54, 0x3e, 0xe5,
311                                 0x3f, 0x9f, 0xc6, 0xf2, 0x3d, 0xf4, 0xab, 0x4b,
312                                 0xbf, 0x31, 0x05, 0xdc, 0x34, 0xf6, 0xc7, 0xfc,
313                                 0x4d, 0xe5, 0xbf, 0xe5, 0xdf, 0x54, 0xde, 0x2b,
314                                 0xd1, 0x97, 0x2e, 0xe4, 0x9c, 0x1b, 0xcf, 0x3f,
315                                 0x6c, 0xda, 0xa7, 0x98, 0xc6, 0x76, 0x3d, 0xcc,
316                                 0x64, 0x67, 0x8a, 0x65, 0x1c, 0x17, 0xa9, 0xdd,
317                                 0xe4, 0xc3, 0xfa, 0x5e, 0x1e, 0xe2, 0xaa, 0x97,
318                                 0x43, 0x7b, 0xc8, 0xeb, 0x3a, 0xc9, 0xe3, 0xf6,
319                                 0x25, 0xa6, 0x21, 0xee, 0x7b, 0xa6, 0x18, 0x42,
320                                 0x1f, 0x67, 0x72, 0xea, 0x9e, 0x62, 0x77, 0xbc,
321                                 0x44, 0x97, 0xa6, 0xd4, 0xc5, 0x5f, 0x7f, 0x0b,
322                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
323                                 0x00, 0x00, 0x00, 0x00, 0xb8, 0x09, 0x1f, 0xae,
324                                 0x48, 0xfe, 0xe8, 0x00, 0x28, 0x00, 0x00,
325                         },
326                         mockdb: &db.MockDB{
327                                 Items: map[string]map[string][]byte{
328                                         DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): {
329                                                 "defmetadata": []byte(
330                                                         "{\"rb-name\":\"testresourcebundle\"," +
331                                                                 "\"description\":\"testresourcebundle\"," +
332                                                                 "\"rb-version\":\"v1\"}"),
333                                         },
334                                 },
335                         },
336                 },
337                 {
338                         label:   "Upload With Chart Name",
339                         name:    "testresourcebundle",
340                         version: "v1",
341                         content: []byte{
342                                 0x1f, 0x8b, 0x08, 0x08, 0xb0, 0x6b, 0xf4, 0x5b,
343                                 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74,
344                                 0x61, 0x72, 0x00, 0xed, 0xce, 0x41, 0x0a, 0xc2,
345                                 0x30, 0x10, 0x85, 0xe1, 0xac, 0x3d, 0x45, 0x4e,
346                                 0x50, 0x12, 0xd2, 0xc4, 0xe3, 0x48, 0xa0, 0x01,
347                                 0x4b, 0x52, 0x0b, 0xed, 0x88, 0x1e, 0xdf, 0x48,
348                                 0x11, 0x5c, 0x08, 0xa5, 0x8b, 0x52, 0x84, 0xff,
349                                 0xdb, 0xbc, 0x61, 0x66, 0x16, 0x4f, 0xd2, 0x2c,
350                                 0x8d, 0x3c, 0x45, 0xed, 0xc8, 0x54, 0x21, 0xb4,
351                                 0xef, 0xb4, 0x67, 0x6f, 0xbe, 0x73, 0x61, 0x9d,
352                                 0xb2, 0xce, 0xd5, 0x55, 0xf0, 0xde, 0xd7, 0x3f,
353                                 0xdb, 0xd6, 0x49, 0x69, 0xb3, 0x67, 0xa9, 0x8f,
354                                 0xfb, 0x2c, 0x71, 0xd2, 0x5a, 0xc5, 0xee, 0x92,
355                                 0x73, 0x8e, 0x43, 0x7f, 0x4b, 0x3f, 0xff, 0xd6,
356                                 0xee, 0x7f, 0xea, 0x9a, 0x4a, 0x19, 0x1f, 0xe3,
357                                 0x54, 0xba, 0xd3, 0xd1, 0x55, 0x00, 0x00, 0x00,
358                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
359                                 0x00, 0x00, 0x00, 0x1b, 0xbc, 0x00, 0xb5, 0xe8,
360                                 0x4a, 0xf9, 0x00, 0x28, 0x00, 0x00,
361                         },
362                         mockdb: &db.MockDB{
363                                 Items: map[string]map[string][]byte{
364                                         DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): {
365                                                 "defmetadata": []byte(
366                                                         "{\"rb-name\":\"testresourcebundle\"," +
367                                                                 "\"description\":\"testresourcebundle\"," +
368                                                                 "\"rb-version\":\"v1\"," +
369                                                                 "\"chart-name\":\"firewall\"}"),
370                                         },
371                                 },
372                         },
373                 },
374                 {
375                         label:         "Upload Without Chart.yaml",
376                         name:          "testresourcebundle",
377                         version:       "v1",
378                         expectedError: "Unable to detect chart name",
379                         content: []byte{
380                                 0x1f, 0x8b, 0x08, 0x08, 0xb0, 0x6b, 0xf4, 0x5b,
381                                 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74,
382                                 0x61, 0x72, 0x00, 0xed, 0xce, 0x41, 0x0a, 0xc2,
383                                 0x30, 0x10, 0x85, 0xe1, 0xac, 0x3d, 0x45, 0x4e,
384                                 0x50, 0x12, 0xd2, 0xc4, 0xe3, 0x48, 0xa0, 0x01,
385                                 0x4b, 0x52, 0x0b, 0xed, 0x88, 0x1e, 0xdf, 0x48,
386                                 0x11, 0x5c, 0x08, 0xa5, 0x8b, 0x52, 0x84, 0xff,
387                                 0xdb, 0xbc, 0x61, 0x66, 0x16, 0x4f, 0xd2, 0x2c,
388                                 0x8d, 0x3c, 0x45, 0xed, 0xc8, 0x54, 0x21, 0xb4,
389                                 0xef, 0xb4, 0x67, 0x6f, 0xbe, 0x73, 0x61, 0x9d,
390                                 0xb2, 0xce, 0xd5, 0x55, 0xf0, 0xde, 0xd7, 0x3f,
391                                 0xdb, 0xd6, 0x49, 0x69, 0xb3, 0x67, 0xa9, 0x8f,
392                                 0xfb, 0x2c, 0x71, 0xd2, 0x5a, 0xc5, 0xee, 0x92,
393                                 0x73, 0x8e, 0x43, 0x7f, 0x4b, 0x3f, 0xff, 0xd6,
394                                 0xee, 0x7f, 0xea, 0x9a, 0x4a, 0x19, 0x1f, 0xe3,
395                                 0x54, 0xba, 0xd3, 0xd1, 0x55, 0x00, 0x00, 0x00,
396                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
397                                 0x00, 0x00, 0x00, 0x1b, 0xbc, 0x00, 0xb5, 0xe8,
398                                 0x4a, 0xf9, 0x00, 0x28, 0x00, 0x00,
399                         },
400                         mockdb: &db.MockDB{
401                                 Items: map[string]map[string][]byte{
402                                         DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): {
403                                                 "defmetadata": []byte(
404                                                         "{\"rb-name\":\"testresourcebundle\"," +
405                                                                 "\"description\":\"testresourcebundle\"," +
406                                                                 "\"rb-version\":\"v1\"," +
407                                                                 "\"chart-name\":\"firewall\"}"),
408                                         },
409                                 },
410                         },
411                 },
412                 {
413                         label:         "Upload with an Invalid Resource Bundle Definition",
414                         name:          "testresourcebundle",
415                         version:       "v1",
416                         expectedError: "Invalid Definition ID provided",
417                         content: []byte{
418                                 0x1f, 0x8b, 0x08, 0x08, 0xb0, 0x6b, 0xf4, 0x5b,
419                                 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74,
420                                 0x61, 0x72, 0x00, 0xed, 0xce, 0x41, 0x0a, 0xc2,
421                                 0x30, 0x10, 0x85, 0xe1, 0xac, 0x3d, 0x45, 0x4e,
422                                 0x50, 0x12, 0xd2, 0xc4, 0xe3, 0x48, 0xa0, 0x01,
423                                 0x4b, 0x52, 0x0b, 0xed, 0x88, 0x1e, 0xdf, 0x48,
424                                 0x11, 0x5c, 0x08, 0xa5, 0x8b, 0x52, 0x84, 0xff,
425                                 0xdb, 0xbc, 0x61, 0x66, 0x16, 0x4f, 0xd2, 0x2c,
426                                 0x8d, 0x3c, 0x45, 0xed, 0xc8, 0x54, 0x21, 0xb4,
427                                 0xef, 0xb4, 0x67, 0x6f, 0xbe, 0x73, 0x61, 0x9d,
428                                 0xb2, 0xce, 0xd5, 0x55, 0xf0, 0xde, 0xd7, 0x3f,
429                                 0xdb, 0xd6, 0x49, 0x69, 0xb3, 0x67, 0xa9, 0x8f,
430                                 0xfb, 0x2c, 0x71, 0xd2, 0x5a, 0xc5, 0xee, 0x92,
431                                 0x73, 0x8e, 0x43, 0x7f, 0x4b, 0x3f, 0xff, 0xd6,
432                                 0xee, 0x7f, 0xea, 0x9a, 0x4a, 0x19, 0x1f, 0xe3,
433                                 0x54, 0xba, 0xd3, 0xd1, 0x55, 0x00, 0x00, 0x00,
434                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
435                                 0x00, 0x00, 0x00, 0x1b, 0xbc, 0x00, 0xb5, 0xe8,
436                                 0x4a, 0xf9, 0x00, 0x28, 0x00, 0x00,
437                         },
438                         mockdb: &db.MockDB{
439                                 Items: map[string]map[string][]byte{
440                                         DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): {
441                                                 "defmetadata": []byte(
442                                                         "{\"rb-name\":\"testresourcebundle\"," +
443                                                                 "\"description\":\"testresourcebundle\"," +
444                                                                 "\"rb-version\":\"v1\"," +
445                                                                 "\"chart-name\":\"firewall\"}"),
446                                         },
447                                 },
448                         },
449                 },
450                 {
451                         label:         "Invalid File Format Error",
452                         name:          "testresourcebundle",
453                         version:       "v1",
454                         expectedError: "Error in file format",
455                         content: []byte{
456                                 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
457                                 0x00, 0xff, 0xf2, 0x48, 0xcd,
458                         },
459                         mockdb: &db.MockDB{
460                                 Items: map[string]map[string][]byte{
461                                         DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): {
462                                                 "defmetadata": []byte(
463                                                         "{\"rb-name\":\"testresourcebundle\"," +
464                                                                 "\"description\":\"testresourcebundle\"," +
465                                                                 "\"rb-version\":\"v1\"," +
466                                                                 "\"chart-name\":\"firewall\"}"),
467                                         },
468                                 },
469                         },
470                 },
471                 {
472                         label:         "Upload Error",
473                         expectedError: "DB Error",
474                         content: []byte{
475                                 0x1f, 0x8b, 0x08, 0x08, 0xb0, 0x6b, 0xf4, 0x5b,
476                                 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74,
477                                 0x61, 0x72, 0x00, 0xed, 0xce, 0x41, 0x0a, 0xc2,
478                                 0x30, 0x10, 0x85, 0xe1, 0xac, 0x3d, 0x45, 0x4e,
479                                 0x50, 0x12, 0xd2, 0xc4, 0xe3, 0x48, 0xa0, 0x01,
480                                 0x4b, 0x52, 0x0b, 0xed, 0x88, 0x1e, 0xdf, 0x48,
481                                 0x11, 0x5c, 0x08, 0xa5, 0x8b, 0x52, 0x84, 0xff,
482                                 0xdb, 0xbc, 0x61, 0x66, 0x16, 0x4f, 0xd2, 0x2c,
483                                 0x8d, 0x3c, 0x45, 0xed, 0xc8, 0x54, 0x21, 0xb4,
484                                 0xef, 0xb4, 0x67, 0x6f, 0xbe, 0x73, 0x61, 0x9d,
485                                 0xb2, 0xce, 0xd5, 0x55, 0xf0, 0xde, 0xd7, 0x3f,
486                                 0xdb, 0xd6, 0x49, 0x69, 0xb3, 0x67, 0xa9, 0x8f,
487                                 0xfb, 0x2c, 0x71, 0xd2, 0x5a, 0xc5, 0xee, 0x92,
488                                 0x73, 0x8e, 0x43, 0x7f, 0x4b, 0x3f, 0xff, 0xd6,
489                                 0xee, 0x7f, 0xea, 0x9a, 0x4a, 0x19, 0x1f, 0xe3,
490                                 0x54, 0xba, 0xd3, 0xd1, 0x55, 0x00, 0x00, 0x00,
491                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
492                                 0x00, 0x00, 0x00, 0x1b, 0xbc, 0x00, 0xb5, 0xe8,
493                                 0x4a, 0xf9, 0x00, 0x28, 0x00, 0x00,
494                         },
495                         mockdb: &db.MockDB{
496                                 Err: pkgerrors.New("DB Error"),
497                         },
498                 },
499         }
500
501         for _, testCase := range testCases {
502                 t.Run(testCase.label, func(t *testing.T) {
503                         db.DBconn = testCase.mockdb
504                         impl := NewDefinitionClient()
505                         err := impl.Upload(testCase.name, testCase.version, testCase.content)
506                         if err != nil {
507                                 if testCase.expectedError == "" {
508                                         t.Errorf("Upload returned an unexpected error %s", err)
509                                 }
510                                 if strings.Contains(err.Error(), testCase.expectedError) == false {
511                                         t.Errorf("Upload returned an unexpected error %s", err)
512                                 }
513                         }
514                 })
515         }
516 }
517
518 func TestDownloadDefinition(t *testing.T) {
519         testCases := []struct {
520                 label         string
521                 name, version string
522                 expected      []byte
523                 expectedError string
524                 mockdb        *db.MockDB
525         }{
526                 {
527                         label:   "Download Resource Bundle Definition",
528                         name:    "testresourcebundle",
529                         version: "v1",
530                         expected: []byte{
531                                 0x1f, 0x8b, 0x08, 0x08, 0xb0, 0x6b, 0xf4, 0x5b,
532                                 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74,
533                                 0x61, 0x72, 0x00, 0xed, 0xce, 0x41, 0x0a, 0xc2,
534                                 0x30, 0x10, 0x85, 0xe1, 0xac, 0x3d, 0x45, 0x4e,
535                                 0x50, 0x12, 0xd2, 0xc4, 0xe3, 0x48, 0xa0, 0x01,
536                                 0x4b, 0x52, 0x0b, 0xed, 0x88, 0x1e, 0xdf, 0x48,
537                                 0x11, 0x5c, 0x08, 0xa5, 0x8b, 0x52, 0x84, 0xff,
538                                 0xdb, 0xbc, 0x61, 0x66, 0x16, 0x4f, 0xd2, 0x2c,
539                                 0x8d, 0x3c, 0x45, 0xed, 0xc8, 0x54, 0x21, 0xb4,
540                                 0xef, 0xb4, 0x67, 0x6f, 0xbe, 0x73, 0x61, 0x9d,
541                                 0xb2, 0xce, 0xd5, 0x55, 0xf0, 0xde, 0xd7, 0x3f,
542                                 0xdb, 0xd6, 0x49, 0x69, 0xb3, 0x67, 0xa9, 0x8f,
543                                 0xfb, 0x2c, 0x71, 0xd2, 0x5a, 0xc5, 0xee, 0x92,
544                                 0x73, 0x8e, 0x43, 0x7f, 0x4b, 0x3f, 0xff, 0xd6,
545                                 0xee, 0x7f, 0xea, 0x9a, 0x4a, 0x19, 0x1f, 0xe3,
546                                 0x54, 0xba, 0xd3, 0xd1, 0x55, 0x00, 0x00, 0x00,
547                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
548                                 0x00, 0x00, 0x00, 0x1b, 0xbc, 0x00, 0xb5, 0xe8,
549                                 0x4a, 0xf9, 0x00, 0x28, 0x00, 0x00,
550                         },
551                         mockdb: &db.MockDB{
552                                 Items: map[string]map[string][]byte{
553                                         DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): {
554                                                 "defmetadata": []byte(
555                                                         "{\"rb-name\":\"testresourcebundle\"," +
556                                                                 "\"description\":\"testresourcebundle\"," +
557                                                                 "\"rb-version\":\"v1\"," +
558                                                                 "\"chart-name\":\"firewall\"}"),
559                                                 "defcontent": []byte("H4sICLBr9FsAA3Rlc3QudGFyAO3OQQrCMBCF4aw9RU5" +
560                                                         "QEtLE40igAUtSC+2IHt9IEVwIpYtShP/bvGFmFk/SLI08Re3IVCG077Rn" +
561                                                         "b75zYZ2yztVV8N7XP9vWSWmzZ6mP+yxx0lrF7pJzjkN/Sz//1u5/6ppKG" +
562                                                         "R/jVLrT0VUAAAAAAAAAAAAAAAAAABu8ALXoSvkAKAAA"),
563                                         },
564                                 },
565                         },
566                 },
567                 {
568                         label:         "Download with an Invalid Resource Bundle Definition",
569                         name:          "testresourcebundle",
570                         version:       "v2",
571                         expectedError: "Invalid Definition ID provided",
572                         mockdb: &db.MockDB{
573                                 Items: map[string]map[string][]byte{
574                                         DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): {
575                                                 "defmetadata": []byte(
576                                                         "{\"rb-name\":\"testresourcebundle\"," +
577                                                                 "\"description\":\"testresourcebundle\"," +
578                                                                 "\"rb-version\":\"v1\"," +
579                                                                 "\"chart-name\":\"firewall\"}"),
580                                         },
581                                 },
582                         },
583                 },
584                 {
585                         label:         "Download Error",
586                         expectedError: "DB Error",
587                         mockdb: &db.MockDB{
588                                 Err: pkgerrors.New("DB Error"),
589                         },
590                 },
591         }
592
593         for _, testCase := range testCases {
594                 t.Run(testCase.label, func(t *testing.T) {
595                         db.DBconn = testCase.mockdb
596                         impl := NewDefinitionClient()
597                         data, err := impl.Download(testCase.name, testCase.version)
598                         if err != nil {
599                                 if testCase.expectedError == "" {
600                                         t.Errorf("Download returned an unexpected error %s", err)
601                                 }
602                                 if strings.Contains(err.Error(), testCase.expectedError) == false {
603                                         t.Errorf("Download returned an unexpected error %s", err)
604                                 }
605                         } else {
606                                 if bytes.Equal(testCase.expected, data) == false {
607                                         t.Errorf("Download returned unexpected data: got %v - expected %v",
608                                                 data, testCase.expected)
609                                 }
610                         }
611                 })
612         }
613 }