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