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