diff options
-rw-r--r-- | src/orchestrator/pkg/appcontext/appcontext.go | 59 | ||||
-rw-r--r-- | src/orchestrator/pkg/appcontext/appcontext_test.go | 78 | ||||
-rw-r--r-- | src/orchestrator/pkg/rtcontext/rtcontext.go | 66 | ||||
-rw-r--r-- | src/orchestrator/pkg/rtcontext/rtcontext_test.go | 415 |
4 files changed, 365 insertions, 253 deletions
diff --git a/src/orchestrator/pkg/appcontext/appcontext.go b/src/orchestrator/pkg/appcontext/appcontext.go index 357402b3..bad5fa47 100644 --- a/src/orchestrator/pkg/appcontext/appcontext.go +++ b/src/orchestrator/pkg/appcontext/appcontext.go @@ -24,17 +24,22 @@ import ( type AppContext struct { initDone bool - rtcObj rtcontext.RunTimeContext - rtc rtcontext.Rtcontext + rtcObj rtcontext.RunTimeContext + rtc rtcontext.Rtcontext } // Init app context -func (ac *AppContext) InitAppContext() { - if !ac.initDone { - ac.rtcObj = rtcontext.RunTimeContext{} - ac.initDone = true - } +func (ac *AppContext) InitAppContext() (interface{}, error) { + ac.rtcObj = rtcontext.RunTimeContext{} + ac.rtc = &ac.rtcObj + return ac.rtc.RtcInit() +} + +// Load app context that was previously created +func (ac *AppContext) LoadAppContext(cid interface{}) (interface{}, error) { + ac.rtcObj = rtcontext.RunTimeContext{} ac.rtc = &ac.rtcObj + return ac.rtc.RtcLoad(cid) } // Create a new context and returns the handle @@ -47,7 +52,7 @@ func (ac *AppContext) CreateCompositeApp() (interface{}, error) { } // Deletes the entire context -func (ac *AppContext) DeleteCompositeApp() (error) { +func (ac *AppContext) DeleteCompositeApp() error { h, err := ac.rtc.RtcGet() if err != nil { return err @@ -61,9 +66,9 @@ func (ac *AppContext) DeleteCompositeApp() (error) { //Returns the handles for a given composite app context func (ac *AppContext) GetCompositeApp() (interface{}, error) { - h, err := ac.rtc.RtcGet() - if err != nil { - return nil, err + h, err := ac.rtc.RtcGet() + if err != nil { + return nil, err } return h, nil } @@ -77,8 +82,8 @@ func (ac *AppContext) AddApp(handle interface{}, appname string) (interface{}, e return h, nil } -//Delete app from the context and everything underneth -func (ac *AppContext) DeleteApp(handle interface{}) (error) { +//Delete app from the context and everything underneth +func (ac *AppContext) DeleteApp(handle interface{}) error { err := ac.rtc.RtcDeletePrefix(handle) if err != nil { return err @@ -120,7 +125,7 @@ func (ac *AppContext) AddCluster(handle interface{}, clustername string) (interf } //Delete cluster from the context and everything underneth -func (ac *AppContext) DeleteCluster(handle interface{}) (error) { +func (ac *AppContext) DeleteCluster(handle interface{}) error { err := ac.rtc.RtcDeletePrefix(handle) if err != nil { return err @@ -156,7 +161,7 @@ func (ac *AppContext) GetClusterHandle(appname string, clustername string) (inte } //Add resource under app and cluster -func (ac *AppContext) AddResource(handle interface{},resname string, value interface{}) (interface{}, error) { +func (ac *AppContext) AddResource(handle interface{}, resname string, value interface{}) (interface{}, error) { h, err := ac.rtc.RtcAddResource(handle, resname, value) if err != nil { return nil, err @@ -165,7 +170,7 @@ func (ac *AppContext) AddResource(handle interface{},resname string, value inter } //Delete resource given the handle -func (ac *AppContext) DeleteResource(handle interface{}) (error) { +func (ac *AppContext) DeleteResource(handle interface{}) error { err := ac.rtc.RtcDeletePair(handle) if err != nil { return err @@ -201,7 +206,7 @@ func (ac *AppContext) GetResourceHandle(appname string, clustername string, resn } //Update the resource value usign the given handle -func (ac *AppContext) UpdateResourceValue(handle interface{}, value interface{}) (error) { +func (ac *AppContext) UpdateResourceValue(handle interface{}, value interface{}) error { return ac.rtc.RtcUpdateValue(handle, value) } @@ -217,11 +222,11 @@ func (ac *AppContext) AddInstruction(handle interface{}, level string, insttype if err != nil { return nil, err } - return h,nil + return h, nil } //Delete instruction under gievn handle -func (ac *AppContext) DeleteInstruction(handle interface{}) (error) { +func (ac *AppContext) DeleteInstruction(handle interface{}) error { err := ac.rtc.RtcDeletePair(handle) if err != nil { return err @@ -244,11 +249,11 @@ func (ac *AppContext) GetAppInstruction(insttype string) (interface{}, error) { if err != nil { return nil, err } - return v,nil + return v, nil } //Update the instruction usign the given handle -func (ac *AppContext) UpdateInstructionValue(handle interface{}, value interface{}) (error) { +func (ac *AppContext) UpdateInstructionValue(handle interface{}, value interface{}) error { return ac.rtc.RtcUpdateValue(handle, value) } @@ -264,10 +269,10 @@ func (ac *AppContext) GetResourceInstruction(appname string, clustername string, s := fmt.Sprintf("%v", rh) + "app/" + appname + "/cluster/" + clustername + "/resource/instruction/" + insttype var v string err = ac.rtc.RtcGetValue(s, &v) - if err != nil { - return nil, err - } - return v,nil + if err != nil { + return nil, err + } + return v, nil } //Return all the handles under the composite app @@ -276,7 +281,7 @@ func (ac *AppContext) GetAllHandles(handle interface{}) ([]interface{}, error) { if err != nil { return nil, err } - return hs,nil + return hs, nil } //Returns the value for a given handle @@ -286,5 +291,5 @@ func (ac *AppContext) GetValue(handle interface{}) (interface{}, error) { if err != nil { return nil, err } - return v,nil + return v, nil } diff --git a/src/orchestrator/pkg/appcontext/appcontext_test.go b/src/orchestrator/pkg/appcontext/appcontext_test.go index a6f3e94e..3fde3a9e 100644 --- a/src/orchestrator/pkg/appcontext/appcontext_test.go +++ b/src/orchestrator/pkg/appcontext/appcontext_test.go @@ -18,15 +18,15 @@ package appcontext import ( "fmt" - "testing" - "strings" pkgerrors "github.com/pkg/errors" + "strings" + "testing" ) // Mock run time context type MockRunTimeContext struct { Items map[string]interface{} - Err error + Err error } func (c *MockRunTimeContext) RtcCreate() (interface{}, error) { @@ -40,6 +40,16 @@ func (c *MockRunTimeContext) RtcCreate() (interface{}, error) { } +func (c *MockRunTimeContext) RtcInit() (interface{}, error) { + var id string = "9345674458787728" + return id, c.Err +} + +func (c *MockRunTimeContext) RtcLoad(id interface{}) (interface{}, error) { + str := "/context/" + fmt.Sprintf("%v", id) + "/" + return interface{}(str), c.Err +} + func (c *MockRunTimeContext) RtcGet() (interface{}, error) { var key string = "/context/9345674458787728/" return key, c.Err @@ -65,13 +75,13 @@ func (c *MockRunTimeContext) RtcAddInstruction(handle interface{}, level string, return nil, c.Err } -func (c *MockRunTimeContext) RtcDeletePair(handle interface{}) (error) { +func (c *MockRunTimeContext) RtcDeletePair(handle interface{}) error { str := fmt.Sprintf("%v", handle) delete(c.Items, str) return c.Err } -func (c *MockRunTimeContext) RtcDeletePrefix(handle interface{}) (error) { +func (c *MockRunTimeContext) RtcDeletePrefix(handle interface{}) error { for k, _ := range c.Items { delete(c.Items, k) } @@ -87,7 +97,7 @@ func (c *MockRunTimeContext) RtcGetHandles(handle interface{}) ([]interface{}, e return keys, c.Err } -func (c *MockRunTimeContext) RtcGetValue(handle interface{}, value interface{}) (error) { +func (c *MockRunTimeContext) RtcGetValue(handle interface{}, value interface{}) error { key := fmt.Sprintf("%v", handle) var s *string s = value.(*string) @@ -100,7 +110,7 @@ func (c *MockRunTimeContext) RtcGetValue(handle interface{}, value interface{}) return c.Err } -func (c *MockRunTimeContext) RtcUpdateValue(handle interface{}, value interface{}) (error) { +func (c *MockRunTimeContext) RtcUpdateValue(handle interface{}, value interface{}) error { key := fmt.Sprintf("%v", handle) c.Items[key] = value return c.Err @@ -109,16 +119,16 @@ func (c *MockRunTimeContext) RtcUpdateValue(handle interface{}, value interface{ func TestCreateCompositeApp(t *testing.T) { var ac = AppContext{} testCases := []struct { - label string + label string mockRtcontext *MockRunTimeContext expectedError string }{ { - label: "Success case", + label: "Success case", mockRtcontext: &MockRunTimeContext{}, }, { - label: "Create returns error case", + label: "Create returns error case", mockRtcontext: &MockRunTimeContext{Err: pkgerrors.Errorf("Error creating run time context:")}, expectedError: "Error creating run time context:", }, @@ -141,16 +151,16 @@ func TestCreateCompositeApp(t *testing.T) { func TestGetCompositeApp(t *testing.T) { var ac = AppContext{} testCases := []struct { - label string + label string mockRtcontext *MockRunTimeContext expectedError string }{ { - label: "Success case", + label: "Success case", mockRtcontext: &MockRunTimeContext{}, }, { - label: "Get returns error case", + label: "Get returns error case", mockRtcontext: &MockRunTimeContext{Err: pkgerrors.Errorf("Error getting run time context:")}, expectedError: "Error getting run time context:", }, @@ -173,16 +183,16 @@ func TestGetCompositeApp(t *testing.T) { func TestDeleteCompositeApp(t *testing.T) { var ac = AppContext{} testCases := []struct { - label string + label string mockRtcontext *MockRunTimeContext expectedError string }{ { - label: "Success case", + label: "Success case", mockRtcontext: &MockRunTimeContext{}, }, { - label: "Delete returns error case", + label: "Delete returns error case", mockRtcontext: &MockRunTimeContext{Err: pkgerrors.Errorf("Error deleting run time context:")}, expectedError: "Error deleting run time context:", }, @@ -205,20 +215,20 @@ func TestDeleteCompositeApp(t *testing.T) { func TestAddApp(t *testing.T) { var ac = AppContext{} testCases := []struct { - label string + label string mockRtcontext *MockRunTimeContext - key interface{} + key interface{} expectedError string }{ { - label: "Success case", + label: "Success case", mockRtcontext: &MockRunTimeContext{}, - key: "/context/9345674458787728/", + key: "/context/9345674458787728/", }, { - label: "Delete returns error case", + label: "Delete returns error case", mockRtcontext: &MockRunTimeContext{Err: pkgerrors.Errorf("Error adding app to run time context:")}, - key: "/context/9345674458787728/", + key: "/context/9345674458787728/", expectedError: "Error adding app to run time context:", }, } @@ -241,29 +251,29 @@ func TestAddApp(t *testing.T) { func TestGetAppHandle(t *testing.T) { var ac = AppContext{} testCases := []struct { - label string + label string mockRtcontext *MockRunTimeContext - key interface{} - appname string + key interface{} + appname string expectedError string }{ { - label: "Success case", + label: "Success case", mockRtcontext: &MockRunTimeContext{}, - key: "/context/9345674458787728/", - appname: "testapp1", + key: "/context/9345674458787728/", + appname: "testapp1", }, { - label: "Invalid app name case", + label: "Invalid app name case", mockRtcontext: &MockRunTimeContext{}, - key: "/context/9345674458787728/", - appname: "", + key: "/context/9345674458787728/", + appname: "", }, { - label: "Delete returns error case", + label: "Delete returns error case", mockRtcontext: &MockRunTimeContext{Err: pkgerrors.Errorf("Error getting app handle from run time context:")}, - key: "/context/9345674458787728/", - appname: "testapp1", + key: "/context/9345674458787728/", + appname: "testapp1", expectedError: "Error getting app handle from run time context:", }, } diff --git a/src/orchestrator/pkg/rtcontext/rtcontext.go b/src/orchestrator/pkg/rtcontext/rtcontext.go index e1f1c03b..1835251a 100644 --- a/src/orchestrator/pkg/rtcontext/rtcontext.go +++ b/src/orchestrator/pkg/rtcontext/rtcontext.go @@ -18,11 +18,11 @@ package rtcontext import ( "fmt" - "math/rand" - "time" - "strings" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/contextdb" pkgerrors "github.com/pkg/errors" + "math/rand" + "strings" + "time" ) const maxrand = 0x7fffffffffffffff @@ -33,27 +33,59 @@ type RunTimeContext struct { } type Rtcontext interface { + RtcInit() (interface{}, error) + RtcLoad(interface{}) (interface{}, error) RtcCreate() (interface{}, error) RtcGet() (interface{}, error) RtcAddLevel(handle interface{}, level string, value string) (interface{}, error) RtcAddResource(handle interface{}, resname string, value interface{}) (interface{}, error) RtcAddInstruction(handle interface{}, level string, insttype string, value interface{}) (interface{}, error) - RtcDeletePair(handle interface{}) (error) - RtcDeletePrefix(handle interface{}) (error) + RtcDeletePair(handle interface{}) error + RtcDeletePrefix(handle interface{}) error RtcGetHandles(handle interface{}) ([]interface{}, error) - RtcGetValue(handle interface{}, value interface{}) (error) - RtcUpdateValue(handle interface{}, value interface{}) (error) + RtcGetValue(handle interface{}, value interface{}) error + RtcUpdateValue(handle interface{}, value interface{}) error } -//Create context by assiging a new id -func (rtc *RunTimeContext) RtcCreate() (interface{}, error) { - +//Intialize context by assiging a new id +func (rtc *RunTimeContext) RtcInit() (interface{}, error) { + if rtc.cid != nil { + return nil, pkgerrors.Errorf("Error, context already initialized") + } ra := rand.New(rand.NewSource(time.Now().UnixNano())) rn := ra.Int63n(maxrand) id := fmt.Sprintf("%v", rn) cid := (prefix + id + "/") rtc.cid = interface{}(cid) + return interface{}(id), nil + +} +//Load context using the given id +func (rtc *RunTimeContext) RtcLoad(id interface{}) (interface{}, error) { + str := fmt.Sprintf("%v", id) + if str == "" { + return nil, pkgerrors.Errorf("Not a valid context id") + } + cid := (prefix + str + "/") + rtc.cid = interface{}(cid) + handle, err := rtc.RtcGet() + if err != nil { + return nil, pkgerrors.Errorf("Error finding the context id: %s", err.Error()) + } + return handle, nil +} + +//Create context using the id and prefix +func (rtc *RunTimeContext) RtcCreate() (interface{}, error) { + cid := fmt.Sprintf("%v", rtc.cid) + if cid == "" { + return nil, pkgerrors.Errorf("Error, context not intialized") + } + if !strings.HasPrefix(cid, prefix) { + return nil, pkgerrors.Errorf("Not a valid run time context prefix") + } + id := strings.SplitN(cid, "/", 4)[2] err := contextdb.Db.Put(cid, id) if err != nil { return nil, pkgerrors.Errorf("Error creating run time context: %s", err.Error()) @@ -139,16 +171,16 @@ func (rtc *RunTimeContext) RtcAddInstruction(handle interface{}, level string, i if level == "" { return nil, pkgerrors.Errorf("Not a valid run time context level") } - if insttype == "" { + if insttype == "" { return nil, pkgerrors.Errorf("Not a valid run time context instruction type") } if value == nil { return nil, pkgerrors.Errorf("Not a valid run time context instruction value") } - k := str + level + "/" + "instruction" + "/" + insttype +"/" + k := str + level + "/" + "instruction" + "/" + insttype + "/" err := contextdb.Db.Put(k, fmt.Sprintf("%v", value)) - if err != nil { + if err != nil { return nil, pkgerrors.Errorf("Error adding run time context instruction: %s", err.Error()) } @@ -156,7 +188,7 @@ func (rtc *RunTimeContext) RtcAddInstruction(handle interface{}, level string, i } //Delete the key value pair using given handle -func (rtc *RunTimeContext) RtcDeletePair(handle interface{}) (error) { +func (rtc *RunTimeContext) RtcDeletePair(handle interface{}) error { str := fmt.Sprintf("%v", handle) sid := fmt.Sprintf("%v", rtc.cid) if !strings.HasPrefix(str, sid) { @@ -172,7 +204,7 @@ func (rtc *RunTimeContext) RtcDeletePair(handle interface{}) (error) { } // Delete all handles underneath the given handle -func (rtc *RunTimeContext) RtcDeletePrefix(handle interface{}) (error) { +func (rtc *RunTimeContext) RtcDeletePrefix(handle interface{}) error { str := fmt.Sprintf("%v", handle) sid := fmt.Sprintf("%v", rtc.cid) if !strings.HasPrefix(str, sid) { @@ -207,7 +239,7 @@ func (rtc *RunTimeContext) RtcGetHandles(handle interface{}) ([]interface{}, err } // Get the value for a given handle -func (rtc *RunTimeContext) RtcGetValue(handle interface{}, value interface{}) (error) { +func (rtc *RunTimeContext) RtcGetValue(handle interface{}, value interface{}) error { str := fmt.Sprintf("%v", handle) sid := fmt.Sprintf("%v", rtc.cid) if !strings.HasPrefix(str, sid) { @@ -223,7 +255,7 @@ func (rtc *RunTimeContext) RtcGetValue(handle interface{}, value interface{}) (e } // Update the value of a given handle -func (rtc *RunTimeContext) RtcUpdateValue(handle interface{}, value interface{}) (error) { +func (rtc *RunTimeContext) RtcUpdateValue(handle interface{}, value interface{}) error { str := fmt.Sprintf("%v", handle) sid := fmt.Sprintf("%v", rtc.cid) if !strings.HasPrefix(str, sid) { diff --git a/src/orchestrator/pkg/rtcontext/rtcontext_test.go b/src/orchestrator/pkg/rtcontext/rtcontext_test.go index 29df9a25..e9610ef0 100644 --- a/src/orchestrator/pkg/rtcontext/rtcontext_test.go +++ b/src/orchestrator/pkg/rtcontext/rtcontext_test.go @@ -17,21 +17,20 @@ package rtcontext import ( - "testing" - "strings" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/contextdb" pkgerrors "github.com/pkg/errors" + "strings" + "testing" ) // MockContextDb for mocking contextdb type MockContextDb struct { - Items map[string]interface{} Err error } // Put function -func (c *MockContextDb) Put(key string, val interface{}) (error) { +func (c *MockContextDb) Put(key string, val interface{}) error { if c.Items == nil { c.Items = make(map[string]interface{}) } @@ -40,7 +39,7 @@ func (c *MockContextDb) Put(key string, val interface{}) (error) { } // Get function -func (c *MockContextDb) Get(key string, val interface{}) (error) { +func (c *MockContextDb) Get(key string, val interface{}) error { var s *string s = val.(*string) for kvKey, kvValue := range c.Items { @@ -53,13 +52,13 @@ func (c *MockContextDb) Get(key string, val interface{}) (error) { } // Delete function -func (c *MockContextDb) Delete(key string) (error) { +func (c *MockContextDb) Delete(key string) error { delete(c.Items, key) return c.Err } // Delete all function -func (c *MockContextDb) DeleteAll(key string) (error) { +func (c *MockContextDb) DeleteAll(key string) error { for kvKey, _ := range c.Items { delete(c.Items, kvKey) } @@ -80,19 +79,86 @@ func (c *MockContextDb) HealthCheck() error { return nil } -func TestRtcCreate(t *testing.T) { +func TestRtcInit(t *testing.T) { var rtc = RunTimeContext{} testCases := []struct { - label string + label string + mockContextDb *MockContextDb + expectedError string + }{ + { + label: "Success case", + mockContextDb: &MockContextDb{}, + }, + { + label: "Init returns error case", + mockContextDb: &MockContextDb{Err: pkgerrors.Errorf("Client not intialized")}, + expectedError: "Error, context already initialized", + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + contextdb.Db = testCase.mockContextDb + _, err := rtc.RtcInit() + if err != nil { + if !strings.Contains(string(err.Error()), testCase.expectedError) { + t.Fatalf("Method returned an error (%s)", err) + } + } + + }) + } +} + +func TestRtcLoad(t *testing.T) { + var rtc = RunTimeContext{""} + testCases := []struct { + label string + mockContextDb *MockContextDb + id string + expectedError string + }{ + { + label: "Success case", + id: "5345674458787728", + mockContextDb: &MockContextDb{}, + }, + { + label: "reinit returns error case", + mockContextDb: &MockContextDb{Err: pkgerrors.Errorf("Client not intialized")}, + id: "8885674458787728", + expectedError: "Error finding the context id:", + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + contextdb.Db = testCase.mockContextDb + _, err := rtc.RtcLoad("5345674458787728") + if err != nil { + if !strings.Contains(string(err.Error()), testCase.expectedError) { + t.Fatalf("Method returned an error (%s)", err) + } + } + + }) + } +} + +func TestRtcCreate(t *testing.T) { + var rtc = RunTimeContext{"/context/5345674458787728/"} + testCases := []struct { + label string mockContextDb *MockContextDb expectedError string }{ { - label: "Success case", + label: "Success case", mockContextDb: &MockContextDb{}, }, { - label: "Create returns error case", + label: "Create returns error case", mockContextDb: &MockContextDb{Err: pkgerrors.Errorf("Client not intialized")}, expectedError: "Error creating run time context:", }, @@ -113,24 +179,23 @@ func TestRtcCreate(t *testing.T) { } func TestRtcGet(t *testing.T) { - var rtc = RunTimeContext{} - var rtc1 = RunTimeContext{"/context/5345674458787728/"} + var rtc = RunTimeContext{"/context/5345674458787728/"} testCases := []struct { - label string + label string mockContextDb *MockContextDb expectedError string }{ { - label: "Success case", + label: "Success case", mockContextDb: &MockContextDb{}, }, { - label: "Get returns error case", + label: "Get returns error case", mockContextDb: &MockContextDb{Err: pkgerrors.Errorf("Client not intialized")}, expectedError: "Error getting run time context metadata:", }, { - label: "Context handle does not match", + label: "Context handle does not match", mockContextDb: &MockContextDb{Err: nil}, expectedError: "Error matching run time context metadata", }, @@ -139,36 +204,36 @@ func TestRtcGet(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { switch testCase.label { - case "Success case": - contextdb.Db = testCase.mockContextDb - chandle, err := rtc.RtcCreate() - if err != nil { - t.Fatalf("Create returned an error (%s)", err) - } - ghandle, err := rtc.RtcGet() - if err != nil { - t.Fatalf("Get returned an error (%s)", err) - } - if ( chandle != ghandle ) { - t.Fatalf("Create and Get does not match") - } - case "Get returns error case": - contextdb.Db = testCase.mockContextDb - _, err := rtc.RtcGet() - if err != nil { - if !strings.Contains(string(err.Error()), testCase.expectedError) { - t.Fatalf("Method returned an error (%s)", err) - } + case "Success case": + contextdb.Db = testCase.mockContextDb + chandle, err := rtc.RtcCreate() + if err != nil { + t.Fatalf("Create returned an error (%s)", err) + } + ghandle, err := rtc.RtcGet() + if err != nil { + t.Fatalf("Get returned an error (%s)", err) + } + if chandle != ghandle { + t.Fatalf("Create and Get does not match") + } + case "Get returns error case": + contextdb.Db = testCase.mockContextDb + _, err := rtc.RtcGet() + if err != nil { + if !strings.Contains(string(err.Error()), testCase.expectedError) { + t.Fatalf("Method returned an error (%s)", err) } - case "Context handle does not match": - contextdb.Db = testCase.mockContextDb - contextdb.Db.Put("/context/5345674458787728/", "6345674458787728") - _, err := rtc1.RtcGet() - if err != nil { - if !strings.Contains(string(err.Error()), testCase.expectedError) { - t.Fatalf("Method returned an error (%s)", err) - } + } + case "Context handle does not match": + contextdb.Db = testCase.mockContextDb + contextdb.Db.Put("/context/5345674458787728/", "6345674458787728") + _, err := rtc.RtcGet() + if err != nil { + if !strings.Contains(string(err.Error()), testCase.expectedError) { + t.Fatalf("Method returned an error (%s)", err) } + } } }) } @@ -177,50 +242,50 @@ func TestRtcGet(t *testing.T) { func TestRtcAddLevel(t *testing.T) { var rtc = RunTimeContext{"/context/3528435435454354/"} testCases := []struct { - label string + label string mockContextDb *MockContextDb - handle interface{} - level string - value string + handle interface{} + level string + value string expectedError string }{ { - label: "Success case", + label: "Success case", mockContextDb: &MockContextDb{}, - handle: "/context/3528435435454354/", - level: "app", - value: "testapp1", + handle: "/context/3528435435454354/", + level: "app", + value: "testapp1", }, { - label: "Not a valid rtc handle", + label: "Not a valid rtc handle", mockContextDb: &MockContextDb{}, - handle: "/context/9528435435454354/", - level: "app", - value: "testapp1", + handle: "/context/9528435435454354/", + level: "app", + value: "testapp1", expectedError: "Not a valid run time context handle", }, { - label: "Not a valid rtc level", + label: "Not a valid rtc level", mockContextDb: &MockContextDb{}, - handle: "/context/3528435435454354/", - level: "", - value: "testapp1", + handle: "/context/3528435435454354/", + level: "", + value: "testapp1", expectedError: "Not a valid run time context level", }, { - label: "Not a valid rtc value", + label: "Not a valid rtc value", mockContextDb: &MockContextDb{}, - handle: "/context/3528435435454354/", - level: "app", - value: "", + handle: "/context/3528435435454354/", + level: "app", + value: "", expectedError: "Not a valid run time context level value", }, { - label: "Put returns error", + label: "Put returns error", mockContextDb: &MockContextDb{Err: pkgerrors.Errorf("Client not intialized")}, - handle: "/context/3528435435454354/", - level: "app", - value: "testapp1", + handle: "/context/3528435435454354/", + level: "app", + value: "testapp1", expectedError: "Error adding run time context level:", }, } @@ -241,50 +306,50 @@ func TestRtcAddLevel(t *testing.T) { func TestRtcAddResource(t *testing.T) { var rtc = RunTimeContext{"/context/3528435435454354/"} testCases := []struct { - label string + label string mockContextDb *MockContextDb - handle interface{} - resname string - value interface{} + handle interface{} + resname string + value interface{} expectedError string }{ { - label: "Success case", + label: "Success case", mockContextDb: &MockContextDb{}, - handle: "/context/3528435435454354/app/apptest1/cluster/cluster1/", - resname: "R1", - value: "res1", + handle: "/context/3528435435454354/app/apptest1/cluster/cluster1/", + resname: "R1", + value: "res1", }, { - label: "Not a valid rtc handle", + label: "Not a valid rtc handle", mockContextDb: &MockContextDb{}, - handle: "/context/9528435435454354/app/apptest1/cluster/cluster1/", - resname: "R1", - value: "res1", + handle: "/context/9528435435454354/app/apptest1/cluster/cluster1/", + resname: "R1", + value: "res1", expectedError: "Not a valid run time context handle", }, { - label: "Not a valid rtc resource name", + label: "Not a valid rtc resource name", mockContextDb: &MockContextDb{}, - handle: "/context/3528435435454354/app/apptest1/cluster/cluster1/", - resname: "", - value: "res1", + handle: "/context/3528435435454354/app/apptest1/cluster/cluster1/", + resname: "", + value: "res1", expectedError: "Not a valid run time context resource name", }, { - label: "Not a valid rtc value", + label: "Not a valid rtc value", mockContextDb: &MockContextDb{}, - handle: "/context/3528435435454354/app/apptest1/cluster/cluster1/", - resname: "R1", - value: nil, + handle: "/context/3528435435454354/app/apptest1/cluster/cluster1/", + resname: "R1", + value: nil, expectedError: "Not a valid run time context resource value", }, { - label: "Put returns error", + label: "Put returns error", mockContextDb: &MockContextDb{Err: pkgerrors.Errorf("Client not intialized")}, - handle: "/context/3528435435454354/app/apptest1/cluster/cluster1/", - resname: "R1", - value: "res1", + handle: "/context/3528435435454354/app/apptest1/cluster/cluster1/", + resname: "R1", + value: "res1", expectedError: "Error adding run time context resource:", }, } @@ -305,65 +370,65 @@ func TestRtcAddResource(t *testing.T) { func TestRtcAddInstruction(t *testing.T) { var rtc = RunTimeContext{"/context/3528435435454354/"} testCases := []struct { - label string + label string mockContextDb *MockContextDb - handle interface{} - level string - insttype string - value interface{} + handle interface{} + level string + insttype string + value interface{} expectedError string }{ { - label: "Success case", + label: "Success case", mockContextDb: &MockContextDb{}, - handle: "/context/3528435435454354/app/apptest1/cluster/cluster1/", - level: "resource", - insttype: "order", - value: "{resorder: [R3, R1, R2]}", + handle: "/context/3528435435454354/app/apptest1/cluster/cluster1/", + level: "resource", + insttype: "order", + value: "{resorder: [R3, R1, R2]}", }, { - label: "Not a valid rtc handle", + label: "Not a valid rtc handle", mockContextDb: &MockContextDb{}, - handle: "/context/9528435435454354/app/apptest1/cluster/cluster1/", - level: "resource", - insttype: "order", - value: "{resorder: [R3, R1, R2]}", + handle: "/context/9528435435454354/app/apptest1/cluster/cluster1/", + level: "resource", + insttype: "order", + value: "{resorder: [R3, R1, R2]}", expectedError: "Not a valid run time context handle", }, { - label: "Not a valid rtc level name", + label: "Not a valid rtc level name", mockContextDb: &MockContextDb{}, - handle: "/context/3528435435454354/app/apptest1/cluster/cluster1/", - level: "", - insttype: "order", - value: "{resorder: [R3, R1, R2]}", + handle: "/context/3528435435454354/app/apptest1/cluster/cluster1/", + level: "", + insttype: "order", + value: "{resorder: [R3, R1, R2]}", expectedError: "Not a valid run time context level", }, { - label: "Not a valid rtc instruction type", + label: "Not a valid rtc instruction type", mockContextDb: &MockContextDb{}, - handle: "/context/3528435435454354/app/apptest1/cluster/cluster1/", - level: "resource", - insttype: "", - value: "{resorder: [R3, R1, R2]}", + handle: "/context/3528435435454354/app/apptest1/cluster/cluster1/", + level: "resource", + insttype: "", + value: "{resorder: [R3, R1, R2]}", expectedError: "Not a valid run time context instruction type", }, { - label: "Not a valid rtc value", + label: "Not a valid rtc value", mockContextDb: &MockContextDb{}, - handle: "/context/3528435435454354/app/apptest1/cluster/cluster1/", - level: "resource", - insttype: "order", - value: nil, + handle: "/context/3528435435454354/app/apptest1/cluster/cluster1/", + level: "resource", + insttype: "order", + value: nil, expectedError: "Not a valid run time context instruction value", }, { - label: "Put returns error", + label: "Put returns error", mockContextDb: &MockContextDb{Err: pkgerrors.Errorf("Client not intialized")}, - handle: "/context/3528435435454354/app/apptest1/cluster/cluster1/", - level: "resource", - insttype: "order", - value: "{resorder: [R3, R1, R2]}", + handle: "/context/3528435435454354/app/apptest1/cluster/cluster1/", + level: "resource", + insttype: "order", + value: "{resorder: [R3, R1, R2]}", expectedError: "Error adding run time context instruction:", }, } @@ -384,34 +449,34 @@ func TestRtcAddInstruction(t *testing.T) { func TestRtcGetHandles(t *testing.T) { var rtc = RunTimeContext{"/context/5345674458787728/"} testCases := []struct { - label string + label string mockContextDb *MockContextDb - key interface{} + key interface{} expectedError string }{ { - label: "Not valid input handle case", + label: "Not valid input handle case", mockContextDb: &MockContextDb{}, - key: "/context/3528435435454354/", + key: "/context/3528435435454354/", expectedError: "Not a valid run time context handle", }, { - label: "Contextdb call returns error case", + label: "Contextdb call returns error case", mockContextDb: &MockContextDb{Err: pkgerrors.Errorf("Key does not exist")}, - key: "/context/5345674458787728/", + key: "/context/5345674458787728/", expectedError: "Error getting run time context handles:", }, { - label: "Success case", + label: "Success case", mockContextDb: &MockContextDb{}, - key: "/context/5345674458787728/", + key: "/context/5345674458787728/", }, } for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { contextdb.Db = testCase.mockContextDb - if testCase.label == "Success case" { + if testCase.label == "Success case" { contextdb.Db.Put("/context/5345674458787728/", 5345674458787728) } _, err := rtc.RtcGetHandles(testCase.key) @@ -427,34 +492,34 @@ func TestRtcGetHandles(t *testing.T) { func TestRtcGetValue(t *testing.T) { var rtc = RunTimeContext{"/context/5345674458787728/"} testCases := []struct { - label string + label string mockContextDb *MockContextDb - key interface{} + key interface{} expectedError string }{ { - label: "Not valid input handle case", + label: "Not valid input handle case", mockContextDb: &MockContextDb{}, - key: "/context/3528435435454354/", + key: "/context/3528435435454354/", expectedError: "Not a valid run time context handle", }, { - label: "Contextdb call returns error case", + label: "Contextdb call returns error case", mockContextDb: &MockContextDb{Err: pkgerrors.Errorf("Key does not exist")}, - key: "/context/5345674458787728/", + key: "/context/5345674458787728/", expectedError: "Error getting run time context value:", }, { - label: "Success case", + label: "Success case", mockContextDb: &MockContextDb{}, - key: "/context/5345674458787728/", + key: "/context/5345674458787728/", }, } for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { contextdb.Db = testCase.mockContextDb - if testCase.label == "Success case" { + if testCase.label == "Success case" { contextdb.Db.Put("/context/5345674458787728/", "5345674458787728") } var val string @@ -471,38 +536,38 @@ func TestRtcGetValue(t *testing.T) { func TestRtcUpdateValue(t *testing.T) { var rtc = RunTimeContext{"/context/5345674458787728/"} testCases := []struct { - label string + label string mockContextDb *MockContextDb - key interface{} - value interface{} + key interface{} + value interface{} expectedError string }{ { - label: "Not valid input handle case", + label: "Not valid input handle case", mockContextDb: &MockContextDb{}, - key: "/context/3528435435454354/", - value: "{apporder: [app1, app2, app3]}", + key: "/context/3528435435454354/", + value: "{apporder: [app1, app2, app3]}", expectedError: "Not a valid run time context handle", }, { - label: "Contextdb call returns error case", + label: "Contextdb call returns error case", mockContextDb: &MockContextDb{Err: pkgerrors.Errorf("Key does not exist")}, - key: "/context/5345674458787728/", - value: "{apporder: [app1, app2, app3]}", + key: "/context/5345674458787728/", + value: "{apporder: [app1, app2, app3]}", expectedError: "Error updating run time context value:", }, { - label: "Success case", + label: "Success case", mockContextDb: &MockContextDb{}, - key: "/context/5345674458787728/", - value: "{apporder: [app2, app3, app1]}", + key: "/context/5345674458787728/", + value: "{apporder: [app2, app3, app1]}", }, } for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { contextdb.Db = testCase.mockContextDb - if testCase.label == "Success case" { + if testCase.label == "Success case" { contextdb.Db.Put("/context/5345674458787728/", "5345674458787728") } err := rtc.RtcUpdateValue(testCase.key, testCase.value) @@ -518,27 +583,27 @@ func TestRtcUpdateValue(t *testing.T) { func TestRtcDeletePair(t *testing.T) { var rtc = RunTimeContext{"/context/5345674458787728/"} testCases := []struct { - label string + label string mockContextDb *MockContextDb - key interface{} + key interface{} expectedError string }{ { - label: "Not valid input handle case", + label: "Not valid input handle case", mockContextDb: &MockContextDb{}, - key: "/context/3528435435454354/", + key: "/context/3528435435454354/", expectedError: "Not a valid run time context handle", }, { - label: "Contextdb call returns error case", + label: "Contextdb call returns error case", mockContextDb: &MockContextDb{Err: pkgerrors.Errorf("Key does not exist")}, - key: "/context/5345674458787728/", + key: "/context/5345674458787728/", expectedError: "Error deleting run time context pair:", }, { - label: "Success case", + label: "Success case", mockContextDb: &MockContextDb{}, - key: "/context/5345674458787728/", + key: "/context/5345674458787728/", }, } @@ -558,27 +623,27 @@ func TestRtcDeletePair(t *testing.T) { func TestRtcDeletePrefix(t *testing.T) { var rtc = RunTimeContext{"/context/5345674458787728/"} testCases := []struct { - label string + label string mockContextDb *MockContextDb - key interface{} + key interface{} expectedError string }{ { - label: "Not valid input handle case", + label: "Not valid input handle case", mockContextDb: &MockContextDb{}, - key: "/context/3528435435454354/", + key: "/context/3528435435454354/", expectedError: "Not a valid run time context handle", }, { - label: "Contextdb call returns error case", + label: "Contextdb call returns error case", mockContextDb: &MockContextDb{Err: pkgerrors.Errorf("Key does not exist")}, - key: "/context/5345674458787728/", + key: "/context/5345674458787728/", expectedError: "Error deleting run time context with prefix:", }, { - label: "Success case", + label: "Success case", mockContextDb: &MockContextDb{}, - key: "/context/5345674458787728/", + key: "/context/5345674458787728/", }, } |