2 * Copyright 2020 Intel Corporation, Inc
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 pkgerrors "github.com/pkg/errors"
26 // Mock run time context
27 type MockRunTimeContext struct {
28 Items map[string]interface{}
32 type MockCompositeAppMeta struct {
39 func (c *MockRunTimeContext) RtcCreate() (interface{}, error) {
40 var key string = "/context/9345674458787728/"
43 c.Items = make(map[string]interface{})
45 c.Items[key] = "9345674458787728"
46 return interface{}(key), c.Err
50 func (c *MockRunTimeContext) RtcAddMeta(meta interface{}) error {
51 var cid string = "/context/9345674458787728/"
52 key := cid + "meta" + "/"
54 c.Items = make(map[string]interface{})
60 func (c *MockRunTimeContext) RtcInit() (interface{}, error) {
61 var id string = "9345674458787728"
65 func (c *MockRunTimeContext) RtcLoad(id interface{}) (interface{}, error) {
66 str := "/context/" + fmt.Sprintf("%v", id) + "/"
67 return interface{}(str), c.Err
70 func (c *MockRunTimeContext) RtcGet() (interface{}, error) {
71 var key string = "/context/9345674458787728/"
75 func (c *MockRunTimeContext) RtcGetMeta() (interface{}, error) {
76 meta := CompositeAppMeta{Project: "pn", CompositeApp: "ca", Version: "v", Release: "rName"}
80 func (c *MockRunTimeContext) RtcAddLevel(handle interface{}, level string, value string) (interface{}, error) {
81 str := fmt.Sprintf("%v", handle) + level + "/" + value + "/"
87 func (c *MockRunTimeContext) RtcAddOneLevel(handle interface{}, level string, value interface{}) (interface{}, error) {
88 str := fmt.Sprintf("%v", handle) + level + "/"
94 func (c *MockRunTimeContext) RtcAddResource(handle interface{}, resname string, value interface{}) (interface{}, error) {
95 str := fmt.Sprintf("%v", handle) + "resource" + "/" + resname + "/"
101 func (c *MockRunTimeContext) RtcAddInstruction(handle interface{}, level string, insttype string, value interface{}) (interface{}, error) {
102 str := fmt.Sprintf("%v", handle) + level + "/" + insttype + "/"
107 func (c *MockRunTimeContext) RtcDeletePair(handle interface{}) error {
108 str := fmt.Sprintf("%v", handle)
113 func (c *MockRunTimeContext) RtcDeletePrefix(handle interface{}) error {
114 for k := range c.Items {
120 func (c *MockRunTimeContext) RtcGetHandles(handle interface{}) ([]interface{}, error) {
121 var keys []interface{}
123 for k := range c.Items {
124 keys = append(keys, string(k))
129 func (c *MockRunTimeContext) RtcGetValue(handle interface{}, value interface{}) error {
130 key := fmt.Sprintf("%v", handle)
133 for kvKey, kvValue := range c.Items {
135 *s = kvValue.(string)
142 func (c *MockRunTimeContext) RtcUpdateValue(handle interface{}, value interface{}) error {
143 key := fmt.Sprintf("%v", handle)
148 func TestCreateCompositeApp(t *testing.T) {
149 var ac = AppContext{}
150 testCases := []struct {
152 mockRtcontext *MockRunTimeContext
157 label: "Success case",
158 mockRtcontext: &MockRunTimeContext{},
159 meta: interface{}(MockCompositeAppMeta{Project: "Testproject", CompositeApp: "TestCompApp", Version: "CompAppVersion", Release: "TestRelease"}),
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"}),
169 for _, testCase := range testCases {
170 t.Run(testCase.label, func(t *testing.T) {
171 ac.rtc = testCase.mockRtcontext
172 _, err := ac.CreateCompositeApp()
174 if !strings.Contains(string(err.Error()), testCase.expectedError) {
175 t.Fatalf("Method returned an error (%s)", err)
183 func TestGetCompositeApp(t *testing.T) {
184 var ac = AppContext{}
185 testCases := []struct {
187 mockRtcontext *MockRunTimeContext
191 label: "Success case",
192 mockRtcontext: &MockRunTimeContext{},
195 label: "Get returns error case",
196 mockRtcontext: &MockRunTimeContext{Err: pkgerrors.Errorf("Error getting run time context:")},
197 expectedError: "Error getting run time context:",
201 for _, testCase := range testCases {
202 t.Run(testCase.label, func(t *testing.T) {
203 ac.rtc = testCase.mockRtcontext
204 _, err := ac.GetCompositeAppHandle()
206 if !strings.Contains(string(err.Error()), testCase.expectedError) {
207 t.Fatalf("Method returned an error (%s)", err)
215 func TestDeleteCompositeApp(t *testing.T) {
216 var ac = AppContext{}
217 testCases := []struct {
219 mockRtcontext *MockRunTimeContext
223 label: "Success case",
224 mockRtcontext: &MockRunTimeContext{},
227 label: "Delete returns error case",
228 mockRtcontext: &MockRunTimeContext{Err: pkgerrors.Errorf("Error deleting run time context:")},
229 expectedError: "Error deleting run time context:",
233 for _, testCase := range testCases {
234 t.Run(testCase.label, func(t *testing.T) {
235 ac.rtc = testCase.mockRtcontext
236 err := ac.DeleteCompositeApp()
238 if !strings.Contains(string(err.Error()), testCase.expectedError) {
239 t.Fatalf("Method returned an error (%s)", err)
247 func TestAddApp(t *testing.T) {
248 var ac = AppContext{}
249 testCases := []struct {
251 mockRtcontext *MockRunTimeContext
257 label: "Success case",
258 mockRtcontext: &MockRunTimeContext{},
259 key: "/context/9345674458787728/",
260 meta: interface{}(MockCompositeAppMeta{Project: "Testproject", CompositeApp: "TestCompApp", Version: "CompAppVersion", Release: "TestRelease"}),
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"}),
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")
277 if !strings.Contains(string(err.Error()), testCase.expectedError) {
278 t.Fatalf("Method returned an error (%s)", err)
286 func TestGetAppHandle(t *testing.T) {
287 var ac = AppContext{}
288 testCases := []struct {
290 mockRtcontext *MockRunTimeContext
296 label: "Success case",
297 mockRtcontext: &MockRunTimeContext{},
298 key: "/context/9345674458787728/",
302 label: "Invalid app name case",
303 mockRtcontext: &MockRunTimeContext{},
304 key: "/context/9345674458787728/",
308 label: "Delete returns error case",
309 mockRtcontext: &MockRunTimeContext{Err: pkgerrors.Errorf("Error getting app handle from run time context:")},
310 key: "/context/9345674458787728/",
312 expectedError: "Error getting app handle from run time context:",
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)
321 if !strings.Contains(string(err.Error()), testCase.expectedError) {
322 t.Fatalf("Method returned an error (%s)", err)