1 // SPDX-License-Identifier: Apache-2.0
\r
2 // Copyright (c) 2020 Intel Corporation
\r
11 pkgerrors "github.com/pkg/errors"
\r
14 // Mock run time context
\r
15 type MockRunTimeContext struct {
\r
16 Items map[string]interface{}
\r
20 type MockCompositeAppMeta struct {
\r
27 func (c *MockRunTimeContext) RtcCreate() (interface{}, error) {
\r
28 var key string = "/context/9345674458787728/"
\r
31 c.Items = make(map[string]interface{})
\r
33 c.Items[key] = "9345674458787728"
\r
34 return interface{}(key), c.Err
\r
38 func (c *MockRunTimeContext) RtcAddMeta(meta interface{}) error {
\r
39 var cid string = "/context/9345674458787728/"
\r
40 key := cid + "meta" + "/"
\r
42 c.Items = make(map[string]interface{})
\r
48 func (c *MockRunTimeContext) RtcInit() (interface{}, error) {
\r
49 var id string = "9345674458787728"
\r
53 func (c *MockRunTimeContext) RtcLoad(id interface{}) (interface{}, error) {
\r
54 str := "/context/" + fmt.Sprintf("%v", id) + "/"
\r
55 return interface{}(str), c.Err
\r
58 func (c *MockRunTimeContext) RtcGet() (interface{}, error) {
\r
59 var key string = "/context/9345674458787728/"
\r
63 func (c *MockRunTimeContext) RtcGetMeta() (interface{}, error) {
\r
64 meta := CompositeAppMeta{Project: "pn", CompositeApp: "ca", Version: "v", Release: "rName"}
\r
68 func (c *MockRunTimeContext) RtcAddLevel(handle interface{}, level string, value string) (interface{}, error) {
\r
69 str := fmt.Sprintf("%v", handle) + level + "/" + value + "/"
\r
70 c.Items[str] = value
\r
75 func (c *MockRunTimeContext) RtcAddOneLevel(handle interface{}, level string, value interface{}) (interface{}, error) {
\r
76 str := fmt.Sprintf("%v", handle) + level + "/"
\r
77 c.Items[str] = value
\r
82 func (c *MockRunTimeContext) RtcAddResource(handle interface{}, resname string, value interface{}) (interface{}, error) {
\r
83 str := fmt.Sprintf("%v", handle) + "resource" + "/" + resname + "/"
\r
84 c.Items[str] = value
\r
89 func (c *MockRunTimeContext) RtcAddInstruction(handle interface{}, level string, insttype string, value interface{}) (interface{}, error) {
\r
90 str := fmt.Sprintf("%v", handle) + level + "/" + insttype + "/"
\r
91 c.Items[str] = value
\r
95 func (c *MockRunTimeContext) RtcDeletePair(handle interface{}) error {
\r
96 str := fmt.Sprintf("%v", handle)
\r
97 delete(c.Items, str)
\r
101 func (c *MockRunTimeContext) RtcDeletePrefix(handle interface{}) error {
\r
102 for k := range c.Items {
\r
108 func (c *MockRunTimeContext) RtcGetHandles(handle interface{}) ([]interface{}, error) {
\r
109 var keys []interface{}
\r
111 for k := range c.Items {
\r
112 keys = append(keys, string(k))
\r
117 func (c *MockRunTimeContext) RtcGetValue(handle interface{}, value interface{}) error {
\r
118 key := fmt.Sprintf("%v", handle)
\r
120 s = value.(*string)
\r
121 for kvKey, kvValue := range c.Items {
\r
123 *s = kvValue.(string)
\r
130 func (c *MockRunTimeContext) RtcUpdateValue(handle interface{}, value interface{}) error {
\r
131 key := fmt.Sprintf("%v", handle)
\r
132 c.Items[key] = value
\r
136 func TestCreateCompositeApp(t *testing.T) {
\r
137 var ac = AppContext{}
\r
138 testCases := []struct {
\r
140 mockRtcontext *MockRunTimeContext
\r
141 expectedError string
\r
145 label: "Success case",
\r
146 mockRtcontext: &MockRunTimeContext{},
\r
147 meta: interface{}(MockCompositeAppMeta{Project: "Testproject", CompositeApp: "TestCompApp", Version: "CompAppVersion", Release: "TestRelease"}),
\r
150 label: "Create returns error case",
\r
151 mockRtcontext: &MockRunTimeContext{Err: pkgerrors.Errorf("Error creating run time context:")},
\r
152 expectedError: "Error creating run time context:",
\r
153 meta: interface{}(MockCompositeAppMeta{Project: "Testproject", CompositeApp: "TestCompApp", Version: "CompAppVersion", Release: "TestRelease"}),
\r
157 for _, testCase := range testCases {
\r
158 t.Run(testCase.label, func(t *testing.T) {
\r
159 ac.rtc = testCase.mockRtcontext
\r
160 _, err := ac.CreateCompositeApp()
\r
162 if !strings.Contains(string(err.Error()), testCase.expectedError) {
\r
163 t.Fatalf("Method returned an error (%s)", err)
\r
171 func TestGetCompositeApp(t *testing.T) {
\r
172 var ac = AppContext{}
\r
173 testCases := []struct {
\r
175 mockRtcontext *MockRunTimeContext
\r
176 expectedError string
\r
179 label: "Success case",
\r
180 mockRtcontext: &MockRunTimeContext{},
\r
183 label: "Get returns error case",
\r
184 mockRtcontext: &MockRunTimeContext{Err: pkgerrors.Errorf("Error getting run time context:")},
\r
185 expectedError: "Error getting run time context:",
\r
189 for _, testCase := range testCases {
\r
190 t.Run(testCase.label, func(t *testing.T) {
\r
191 ac.rtc = testCase.mockRtcontext
\r
192 _, err := ac.GetCompositeAppHandle()
\r
194 if !strings.Contains(string(err.Error()), testCase.expectedError) {
\r
195 t.Fatalf("Method returned an error (%s)", err)
\r
203 func TestDeleteCompositeApp(t *testing.T) {
\r
204 var ac = AppContext{}
\r
205 testCases := []struct {
\r
207 mockRtcontext *MockRunTimeContext
\r
208 expectedError string
\r
211 label: "Success case",
\r
212 mockRtcontext: &MockRunTimeContext{},
\r
215 label: "Delete returns error case",
\r
216 mockRtcontext: &MockRunTimeContext{Err: pkgerrors.Errorf("Error deleting run time context:")},
\r
217 expectedError: "Error deleting run time context:",
\r
221 for _, testCase := range testCases {
\r
222 t.Run(testCase.label, func(t *testing.T) {
\r
223 ac.rtc = testCase.mockRtcontext
\r
224 err := ac.DeleteCompositeApp()
\r
226 if !strings.Contains(string(err.Error()), testCase.expectedError) {
\r
227 t.Fatalf("Method returned an error (%s)", err)
\r
235 func TestAddApp(t *testing.T) {
\r
236 var ac = AppContext{}
\r
237 testCases := []struct {
\r
239 mockRtcontext *MockRunTimeContext
\r
241 expectedError string
\r
245 label: "Success case",
\r
246 mockRtcontext: &MockRunTimeContext{},
\r
247 key: "/context/9345674458787728/",
\r
248 meta: interface{}(MockCompositeAppMeta{Project: "Testproject", CompositeApp: "TestCompApp", Version: "CompAppVersion", Release: "TestRelease"}),
\r
251 label: "Error case for adding app",
\r
252 mockRtcontext: &MockRunTimeContext{Err: pkgerrors.Errorf("Error adding app to run time context:")},
\r
253 key: "/context/9345674458787728/",
\r
254 expectedError: "Error adding app to run time context:",
\r
255 meta: interface{}(MockCompositeAppMeta{Project: "Testproject", CompositeApp: "TestCompApp", Version: "CompAppVersion", Release: "TestRelease"}),
\r
259 for _, testCase := range testCases {
\r
260 t.Run(testCase.label, func(t *testing.T) {
\r
261 ac.rtc = testCase.mockRtcontext
\r
262 _, err := ac.CreateCompositeApp()
\r
263 _, err = ac.AddApp(testCase.key, "testapp1")
\r
265 if !strings.Contains(string(err.Error()), testCase.expectedError) {
\r
266 t.Fatalf("Method returned an error (%s)", err)
\r
274 func TestGetAppHandle(t *testing.T) {
\r
275 var ac = AppContext{}
\r
276 testCases := []struct {
\r
278 mockRtcontext *MockRunTimeContext
\r
281 expectedError string
\r
284 label: "Success case",
\r
285 mockRtcontext: &MockRunTimeContext{},
\r
286 key: "/context/9345674458787728/",
\r
287 appname: "testapp1",
\r
290 label: "Invalid app name case",
\r
291 mockRtcontext: &MockRunTimeContext{},
\r
292 key: "/context/9345674458787728/",
\r
296 label: "Delete returns error case",
\r
297 mockRtcontext: &MockRunTimeContext{Err: pkgerrors.Errorf("Error getting app handle from run time context:")},
\r
298 key: "/context/9345674458787728/",
\r
299 appname: "testapp1",
\r
300 expectedError: "Error getting app handle from run time context:",
\r
304 for _, testCase := range testCases {
\r
305 t.Run(testCase.label, func(t *testing.T) {
\r
306 ac.rtc = testCase.mockRtcontext
\r
307 _, err := ac.GetAppHandle(testCase.appname)
\r
309 if !strings.Contains(string(err.Error()), testCase.expectedError) {
\r
310 t.Fatalf("Method returned an error (%s)", err)
\r