Adding cluster meta data and saving in etcd
[multicloud/k8s.git] / src / orchestrator / pkg / appcontext / appcontext_test.go
1 /*
2  * Copyright 2020 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 appcontext
18
19 import (
20         "fmt"
21         pkgerrors "github.com/pkg/errors"
22         "strings"
23         "testing"
24 )
25
26 // Mock run time context
27 type MockRunTimeContext struct {
28         Items map[string]interface{}
29         Err   error
30 }
31
32 type MockCompositeAppMeta struct {
33         Project      string
34         CompositeApp string
35         Version      string
36         Release      string
37 }
38
39 func (c *MockRunTimeContext) RtcCreate() (interface{}, error) {
40         var key string = "/context/9345674458787728/"
41
42         if c.Items == nil {
43                 c.Items = make(map[string]interface{})
44         }
45         c.Items[key] = "9345674458787728"
46         return interface{}(key), c.Err
47
48 }
49
50 func (c *MockRunTimeContext) RtcAddMeta(meta interface{}) error {
51         var cid string = "/context/9345674458787728/"
52         key := cid + "meta" + "/"
53         if c.Items == nil {
54                 c.Items = make(map[string]interface{})
55         }
56         c.Items[key] = meta
57         return nil
58 }
59
60 func (c *MockRunTimeContext) RtcInit() (interface{}, error) {
61         var id string = "9345674458787728"
62         return id, c.Err
63 }
64
65 func (c *MockRunTimeContext) RtcLoad(id interface{}) (interface{}, error) {
66         str := "/context/" + fmt.Sprintf("%v", id) + "/"
67         return interface{}(str), c.Err
68 }
69
70 func (c *MockRunTimeContext) RtcGet() (interface{}, error) {
71         var key string = "/context/9345674458787728/"
72         return key, c.Err
73 }
74
75 func (c *MockRunTimeContext) RtcGetMeta() (interface{}, error) {
76         meta := CompositeAppMeta{Project: "pn", CompositeApp: "ca", Version: "v", Release: "rName"}
77         return meta, nil
78 }
79
80 func (c *MockRunTimeContext) RtcAddLevel(handle interface{}, level string, value string) (interface{}, error) {
81         str := fmt.Sprintf("%v", handle) + level + "/" + value + "/"
82         c.Items[str] = value
83         return nil, c.Err
84
85 }
86
87 func (c *MockRunTimeContext) RtcAddOneLevel(handle interface{}, level string, value interface{}) (interface{}, error) {
88         str := fmt.Sprintf("%v", handle) + level + "/"
89         c.Items[str] = value
90         return nil, c.Err
91
92 }
93
94 func (c *MockRunTimeContext) RtcAddResource(handle interface{}, resname string, value interface{}) (interface{}, error) {
95         str := fmt.Sprintf("%v", handle) + "resource" + "/" + resname + "/"
96         c.Items[str] = value
97         return nil, c.Err
98
99 }
100
101 func (c *MockRunTimeContext) RtcAddInstruction(handle interface{}, level string, insttype string, value interface{}) (interface{}, error) {
102         str := fmt.Sprintf("%v", handle) + level + "/" + insttype + "/"
103         c.Items[str] = value
104         return nil, c.Err
105 }
106
107 func (c *MockRunTimeContext) RtcDeletePair(handle interface{}) error {
108         str := fmt.Sprintf("%v", handle)
109         delete(c.Items, str)
110         return c.Err
111 }
112
113 func (c *MockRunTimeContext) RtcDeletePrefix(handle interface{}) error {
114         for k := range c.Items {
115                 delete(c.Items, k)
116         }
117         return c.Err
118 }
119
120 func (c *MockRunTimeContext) RtcGetHandles(handle interface{}) ([]interface{}, error) {
121         var keys []interface{}
122
123         for k := range c.Items {
124                 keys = append(keys, string(k))
125         }
126         return keys, c.Err
127 }
128
129 func (c *MockRunTimeContext) RtcGetValue(handle interface{}, value interface{}) error {
130         key := fmt.Sprintf("%v", handle)
131         var s *string
132         s = value.(*string)
133         for kvKey, kvValue := range c.Items {
134                 if kvKey == key {
135                         *s = kvValue.(string)
136                         return c.Err
137                 }
138         }
139         return c.Err
140 }
141
142 func (c *MockRunTimeContext) RtcUpdateValue(handle interface{}, value interface{}) error {
143         key := fmt.Sprintf("%v", handle)
144         c.Items[key] = value
145         return c.Err
146 }
147
148 func TestCreateCompositeApp(t *testing.T) {
149         var ac = AppContext{}
150         testCases := []struct {
151                 label         string
152                 mockRtcontext *MockRunTimeContext
153                 expectedError string
154                 meta          interface{}
155         }{
156                 {
157                         label:         "Success case",
158                         mockRtcontext: &MockRunTimeContext{},
159                         meta:          interface{}(MockCompositeAppMeta{Project: "Testproject", CompositeApp: "TestCompApp", Version: "CompAppVersion", Release: "TestRelease"}),
160                 },
161                 {
162                         label:         "Create returns error case",
163                         mockRtcontext: &MockRunTimeContext{Err: pkgerrors.Errorf("Error creating run time context:")},
164                         expectedError: "Error creating run time context:",
165                         meta:          interface{}(MockCompositeAppMeta{Project: "Testproject", CompositeApp: "TestCompApp", Version: "CompAppVersion", Release: "TestRelease"}),
166                 },
167         }
168
169         for _, testCase := range testCases {
170                 t.Run(testCase.label, func(t *testing.T) {
171                         ac.rtc = testCase.mockRtcontext
172                         _, err := ac.CreateCompositeApp()
173                         if err != nil {
174                                 if !strings.Contains(string(err.Error()), testCase.expectedError) {
175                                         t.Fatalf("Method returned an error (%s)", err)
176                                 }
177                         }
178
179                 })
180         }
181 }
182
183 func TestGetCompositeApp(t *testing.T) {
184         var ac = AppContext{}
185         testCases := []struct {
186                 label         string
187                 mockRtcontext *MockRunTimeContext
188                 expectedError string
189         }{
190                 {
191                         label:         "Success case",
192                         mockRtcontext: &MockRunTimeContext{},
193                 },
194                 {
195                         label:         "Get returns error case",
196                         mockRtcontext: &MockRunTimeContext{Err: pkgerrors.Errorf("Error getting run time context:")},
197                         expectedError: "Error getting run time context:",
198                 },
199         }
200
201         for _, testCase := range testCases {
202                 t.Run(testCase.label, func(t *testing.T) {
203                         ac.rtc = testCase.mockRtcontext
204                         _, err := ac.GetCompositeAppHandle()
205                         if err != nil {
206                                 if !strings.Contains(string(err.Error()), testCase.expectedError) {
207                                         t.Fatalf("Method returned an error (%s)", err)
208                                 }
209                         }
210
211                 })
212         }
213 }
214
215 func TestDeleteCompositeApp(t *testing.T) {
216         var ac = AppContext{}
217         testCases := []struct {
218                 label         string
219                 mockRtcontext *MockRunTimeContext
220                 expectedError string
221         }{
222                 {
223                         label:         "Success case",
224                         mockRtcontext: &MockRunTimeContext{},
225                 },
226                 {
227                         label:         "Delete returns error case",
228                         mockRtcontext: &MockRunTimeContext{Err: pkgerrors.Errorf("Error deleting run time context:")},
229                         expectedError: "Error deleting run time context:",
230                 },
231         }
232
233         for _, testCase := range testCases {
234                 t.Run(testCase.label, func(t *testing.T) {
235                         ac.rtc = testCase.mockRtcontext
236                         err := ac.DeleteCompositeApp()
237                         if err != nil {
238                                 if !strings.Contains(string(err.Error()), testCase.expectedError) {
239                                         t.Fatalf("Method returned an error (%s)", err)
240                                 }
241                         }
242
243                 })
244         }
245 }
246
247 func TestAddApp(t *testing.T) {
248         var ac = AppContext{}
249         testCases := []struct {
250                 label         string
251                 mockRtcontext *MockRunTimeContext
252                 key           interface{}
253                 expectedError string
254                 meta          interface{}
255         }{
256                 {
257                         label:         "Success case",
258                         mockRtcontext: &MockRunTimeContext{},
259                         key:           "/context/9345674458787728/",
260                         meta:          interface{}(MockCompositeAppMeta{Project: "Testproject", CompositeApp: "TestCompApp", Version: "CompAppVersion", Release: "TestRelease"}),
261                 },
262                 {
263                         label:         "Error case for adding app",
264                         mockRtcontext: &MockRunTimeContext{Err: pkgerrors.Errorf("Error adding app to run time context:")},
265                         key:           "/context/9345674458787728/",
266                         expectedError: "Error adding app to run time context:",
267                         meta:          interface{}(MockCompositeAppMeta{Project: "Testproject", CompositeApp: "TestCompApp", Version: "CompAppVersion", Release: "TestRelease"}),
268                 },
269         }
270
271         for _, testCase := range testCases {
272                 t.Run(testCase.label, func(t *testing.T) {
273                         ac.rtc = testCase.mockRtcontext
274                         _, err := ac.CreateCompositeApp()
275                         _, err = ac.AddApp(testCase.key, "testapp1")
276                         if err != nil {
277                                 if !strings.Contains(string(err.Error()), testCase.expectedError) {
278                                         t.Fatalf("Method returned an error (%s)", err)
279                                 }
280                         }
281
282                 })
283         }
284 }
285
286 func TestGetAppHandle(t *testing.T) {
287         var ac = AppContext{}
288         testCases := []struct {
289                 label         string
290                 mockRtcontext *MockRunTimeContext
291                 key           interface{}
292                 appname       string
293                 expectedError string
294         }{
295                 {
296                         label:         "Success case",
297                         mockRtcontext: &MockRunTimeContext{},
298                         key:           "/context/9345674458787728/",
299                         appname:       "testapp1",
300                 },
301                 {
302                         label:         "Invalid app name case",
303                         mockRtcontext: &MockRunTimeContext{},
304                         key:           "/context/9345674458787728/",
305                         appname:       "",
306                 },
307                 {
308                         label:         "Delete returns error case",
309                         mockRtcontext: &MockRunTimeContext{Err: pkgerrors.Errorf("Error getting app handle from run time context:")},
310                         key:           "/context/9345674458787728/",
311                         appname:       "testapp1",
312                         expectedError: "Error getting app handle from run time context:",
313                 },
314         }
315
316         for _, testCase := range testCases {
317                 t.Run(testCase.label, func(t *testing.T) {
318                         ac.rtc = testCase.mockRtcontext
319                         _, err := ac.GetAppHandle(testCase.appname)
320                         if err != nil {
321                                 if !strings.Contains(string(err.Error()), testCase.expectedError) {
322                                         t.Fatalf("Method returned an error (%s)", err)
323                                 }
324                         }
325
326                 })
327         }
328 }