From e80825573596b5ebce0cdcaa4a56c6dcb6862a38 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Tue, 12 Feb 2019 09:42:55 +0200 Subject: [PATCH] Aded tests for DispatcherService ChargerSv1 --- .../conf/samples/dispatchers/all/cgrates.json | 12 +- .../samples/dispatchers/all2/cgrates.json | 12 +- data/tariffplans/dispatchers/Attributes.csv | 1 + dispatchers/chargers_it_test.go | 244 ++++++------------ 4 files changed, 97 insertions(+), 172 deletions(-) diff --git a/data/conf/samples/dispatchers/all/cgrates.json b/data/conf/samples/dispatchers/all/cgrates.json index ecbf62691..61b72bd05 100644 --- a/data/conf/samples/dispatchers/all/cgrates.json +++ b/data/conf/samples/dispatchers/all/cgrates.json @@ -33,11 +33,11 @@ "enabled": true, }, -// "chargers": { -// "enabled": true, -// "attributes_conns": [ -// {"address": "*internal"}, -// ], -// }, +"chargers": { + "enabled": true, + "attributes_conns": [ + {"address": "*internal"}, + ], +}, } diff --git a/data/conf/samples/dispatchers/all2/cgrates.json b/data/conf/samples/dispatchers/all2/cgrates.json index 3fb7a46ed..14249df30 100644 --- a/data/conf/samples/dispatchers/all2/cgrates.json +++ b/data/conf/samples/dispatchers/all2/cgrates.json @@ -33,11 +33,11 @@ "enabled": true, }, -// "chargers": { -// "enabled": true, -// "attributes_conns": [ -// {"address": "*internal"}, -// ], -// }, +"chargers": { + "enabled": true, + "attributes_conns": [ + {"address": "*internal"}, + ], +}, } diff --git a/data/tariffplans/dispatchers/Attributes.csv b/data/tariffplans/dispatchers/Attributes.csv index 3904c4393..43fa47cbc 100644 --- a/data/tariffplans/dispatchers/Attributes.csv +++ b/data/tariffplans/dispatchers/Attributes.csv @@ -2,5 +2,6 @@ cgrates.org,ATTR_1001_SIMPLEAUTH,simpleauth,*string:Account:1001,,Password,*any,CGRateS.org,true,false,20 cgrates.org,ATTR_API_ATTR_FAKE_AUTH,*auth,*string:APIKey:12345,,APIMethods,*any,,true,false,20 cgrates.org,ATTR_API_ATTR_AUTH,*auth,*string:APIKey:attr12345,,APIMethods,*any,AttributeSv1.Ping&AttributeSv1.GetAttributeForEvent&AttributeSv1.ProcessEvent,true,false,20 +cgrates.org,ATTR_API_CHRG_AUTH,*auth,*string:APIKey:chrg12345,,APIMethods,*any,ChargerSv1.Ping&ChargerSv1.GetChargersForEvent&ChargerSv1.ProcessEvent,true,false,20 diff --git a/dispatchers/chargers_it_test.go b/dispatchers/chargers_it_test.go index 8d48b70b4..db19908c0 100755 --- a/dispatchers/chargers_it_test.go +++ b/dispatchers/chargers_it_test.go @@ -21,164 +21,134 @@ along with this program. If not, see package dispatchers import ( - "net/rpc" - "net/rpc/jsonrpc" "path" "reflect" "testing" "time" - "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" ) -var ( - dspCppCfgPath string - dspCppCfg *config.CGRConfig - dspCppRPC *rpc.Client - instCppCfgPath string - instCppCfg *config.CGRConfig - instCppRPC *rpc.Client -) - var sTestsDspCpp = []func(t *testing.T){ - testDspCppInitCfg, - testDspCppInitDataDb, - testDspCppResetStorDb, - testDspCppStartEngine, - testDspCppRPCConn, + testDspCppPingFailover, + testDspCppGetChtgFailover, + testDspCppPing, - testDspCppLoadData, - testDspCppAddAttributeWithPermision, testDspCppTestAuthKey, - testDspCppAddAttributesWithPermision2, testDspCppTestAuthKey2, - testDspCppKillEngine, } //Test start here func TestDspChargerS(t *testing.T) { + allEngine = newTestEngine(t, path.Join(dspDataDir, "conf", "samples", "dispatchers", "all"), true, true) + allEngine2 = newTestEngine(t, path.Join(dspDataDir, "conf", "samples", "dispatchers", "all2"), true, true) + attrEngine = newTestEngine(t, path.Join(dspDataDir, "conf", "samples", "dispatchers", "attributes"), true, true) + dispEngine = newTestEngine(t, path.Join(dspDataDir, "conf", "samples", "dispatchers", "dispatchers"), true, true) + allEngine.loadData(t, path.Join(dspDataDir, "tariffplans", "tutorial")) + allEngine2.loadData(t, path.Join(dspDataDir, "tariffplans", "oldtutorial")) + attrEngine.loadData(t, path.Join(dspDataDir, "tariffplans", "dispatchers")) + time.Sleep(500 * time.Millisecond) for _, stest := range sTestsDspCpp { t.Run("", stest) } + attrEngine.stopEngine(t) + dispEngine.stopEngine(t) + allEngine.stopEngine(t) + allEngine2.stopEngine(t) } -func testDspCppInitCfg(t *testing.T) { - var err error - dspCppCfgPath = path.Join(dspDataDir, "conf", "samples", "dispatchers", "dispatchers") - dspCppCfg, err = config.NewCGRConfigFromFolder(dspCppCfgPath) - if err != nil { +func testDspCppPingFailover(t *testing.T) { + var reply string + if err := allEngine.RCP.Call(utils.ChargerSv1Ping, utils.CGREvent{}, &reply); err != nil { t.Error(err) + } else if reply != utils.Pong { + t.Errorf("Received: %s", reply) } - dspCppCfg.DataFolderPath = dspDataDir // Share DataFolderPath through config towards StoreDb for Flush() - config.SetCgrConfig(dspCppCfg) - instCppCfgPath = path.Join(dspDataDir, "conf", "samples", "dispatchers", "attributes") - instCppCfg, err = config.NewCGRConfigFromFolder(instCppCfgPath) - if err != nil { + ev := CGREvWithApiKey{ + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + }, + APIKey: "chrg12345", + } + if err := dispEngine.RCP.Call(utils.ChargerSv1Ping, &ev, &reply); err != nil { t.Error(err) + } else if reply != utils.Pong { + t.Errorf("Received: %s", reply) } - instCppCfg.DataFolderPath = dspDataDir // Share DataFolderPath through config towards StoreDb for Flush() - config.SetCgrConfig(instCppCfg) + allEngine.stopEngine(t) + if err := dispEngine.RCP.Call(utils.ChargerSv1Ping, &ev, &reply); err != nil { + t.Error(err) + } else if reply != utils.Pong { + t.Errorf("Received: %s", reply) + } + allEngine2.stopEngine(t) + if err := dispEngine.RCP.Call(utils.ChargerSv1Ping, &ev, &reply); err == nil { + t.Errorf("Expected error but recived %v and reply %v\n", err, reply) + } + allEngine.startEngine(t) + allEngine2.startEngine(t) } -func testDspCppInitDataDb(t *testing.T) { - if err := engine.InitDataDb(instCppCfg); err != nil { - t.Fatal(err) +func testDspCppGetChtgFailover(t *testing.T) { + args := CGREvWithApiKey{ + APIKey: "chrg12345", + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + ID: "event1", + Event: map[string]interface{}{ + utils.EVENT_NAME: "Event1", + utils.Account: "1001", + }, + }, + } + eChargers := &engine.ChargerProfiles{ + &engine.ChargerProfile{ + Tenant: "cgrates.org", + ID: "DEFAULT", + FilterIDs: []string{}, + RunID: "*default", + AttributeIDs: []string{"*none"}, + Weight: 0, + }, + } + var reply *engine.ChargerProfiles + if err := dispEngine.RCP.Call(utils.ChargerSv1GetChargersForEvent, + args, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Expected error NOT_FOUND but recived %v and reply %v\n", err, reply) } -} -// Wipe out the cdr database -func testDspCppResetStorDb(t *testing.T) { - if err := engine.InitStorDb(instCppCfg); err != nil { - t.Fatal(err) - } -} + allEngine2.stopEngine(t) -// Start CGR Engine -func testDspCppStartEngine(t *testing.T) { - if _, err := engine.StartEngine(instCppCfgPath, dspDelay); err != nil { - t.Fatal(err) + if err := dispEngine.RCP.Call(utils.ChargerSv1GetChargersForEvent, + args, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eChargers, reply) { + t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(eChargers), utils.ToJSON(reply)) } - if _, err := engine.StartEngine(dspCppCfgPath, dspDelay); err != nil { - t.Fatal(err) - } -} -// Connect rpc client to rater -func testDspCppRPCConn(t *testing.T) { - var err error - instCppRPC, err = jsonrpc.Dial("tcp", instCppCfg.ListenCfg().RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed - if err != nil { - t.Fatal(err) - } - dspCppRPC, err = jsonrpc.Dial("tcp", dspCppCfg.ListenCfg().RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed - if err != nil { - t.Fatal(err) - } + allEngine2.startEngine(t) } func testDspCppPing(t *testing.T) { var reply string - if err := instCppRPC.Call(utils.ChargerSv1Ping, "", &reply); err != nil { + if err := allEngine.RCP.Call(utils.ChargerSv1Ping, utils.CGREvent{}, &reply); err != nil { t.Error(err) } else if reply != utils.Pong { t.Errorf("Received: %s", reply) } - if err := dspCppRPC.Call(utils.ChargerSv1Ping, "", &reply); err != nil { + if err := dispEngine.RCP.Call(utils.ChargerSv1Ping, &CGREvWithApiKey{ + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + }, + APIKey: "chrg12345", + }, &reply); err != nil { t.Error(err) } else if reply != utils.Pong { t.Errorf("Received: %s", reply) } } -func testDspCppLoadData(t *testing.T) { - var reply string - attrs := &utils.AttrLoadTpFromFolder{ - FolderPath: path.Join(dspDataDir, "tariffplans", "tutorial")} - if err := instCppRPC.Call("ApierV1.LoadTariffPlanFromFolder", attrs, &reply); err != nil { - t.Error(err) - } - time.Sleep(500 * time.Millisecond) -} - -func testDspCppAddAttributeWithPermision(t *testing.T) { - alsPrf := &engine.AttributeProfile{ - Tenant: "cgrates.org", - ID: "AuthKey", - Contexts: []string{utils.MetaAuth}, - FilterIDs: []string{"*string:APIKey:12345"}, - ActivationInterval: &utils.ActivationInterval{ - ActivationTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC), - }, - Attributes: []*engine.Attribute{ - { - FieldName: utils.APIMethods, - Initial: utils.META_ANY, - Substitute: config.NewRSRParsersMustCompile("ThresholdSv1.GetThresholdsForEvent", true, utils.INFIELD_SEP), - Append: true, - }, - }, - Weight: 20, - } - var result string - if err := instCppRPC.Call("ApierV1.SetAttributeProfile", alsPrf, &result); err != nil { - t.Error(err) - } else if result != utils.OK { - t.Error("Unexpected reply returned", result) - } - alsPrf.Compile() - var reply *engine.AttributeProfile - if err := instCppRPC.Call("ApierV1.GetAttributeProfile", - &utils.TenantID{Tenant: "cgrates.org", ID: "AuthKey"}, &reply); err != nil { - t.Error(err) - } - reply.Compile() - if !reflect.DeepEqual(alsPrf, reply) { - t.Errorf("Expecting : %+v, received: %+v", alsPrf, reply) - } -} - func testDspCppTestAuthKey(t *testing.T) { args := CGREvWithApiKey{ APIKey: "12345", @@ -191,52 +161,15 @@ func testDspCppTestAuthKey(t *testing.T) { }, } var reply *engine.ChargerProfiles - if err := dspCppRPC.Call(utils.ChargerSv1GetChargersForEvent, + if err := dispEngine.RCP.Call(utils.ChargerSv1GetChargersForEvent, args, &reply); err == nil || err.Error() != utils.ErrUnauthorizedApi.Error() { t.Error(err) } } -func testDspCppAddAttributesWithPermision2(t *testing.T) { - alsPrf := &engine.AttributeProfile{ - Tenant: "cgrates.org", - ID: "AuthKey", - Contexts: []string{utils.MetaAuth}, - FilterIDs: []string{"*string:APIKey:12345"}, - ActivationInterval: &utils.ActivationInterval{ - ActivationTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC), - }, - Attributes: []*engine.Attribute{ - { - FieldName: utils.APIMethods, - Initial: utils.META_ANY, - Substitute: config.NewRSRParsersMustCompile("ThresholdSv1.ProcessEvent&ChargerSv1.GetChargersForEvent", true, utils.INFIELD_SEP), - Append: true, - }, - }, - Weight: 20, - } - var result string - if err := instCppRPC.Call("ApierV1.SetAttributeProfile", alsPrf, &result); err != nil { - t.Error(err) - } else if result != utils.OK { - t.Error("Unexpected reply returned", result) - } - alsPrf.Compile() - var reply *engine.AttributeProfile - if err := instCppRPC.Call("ApierV1.GetAttributeProfile", - &utils.TenantID{Tenant: "cgrates.org", ID: "AuthKey"}, &reply); err != nil { - t.Error(err) - } - reply.Compile() - if !reflect.DeepEqual(alsPrf, reply) { - t.Errorf("Expecting : %+v, received: %+v", alsPrf, reply) - } -} - func testDspCppTestAuthKey2(t *testing.T) { args := CGREvWithApiKey{ - APIKey: "12345", + APIKey: "chrg12345", CGREvent: utils.CGREvent{ Tenant: "cgrates.org", ID: "event1", @@ -256,19 +189,10 @@ func testDspCppTestAuthKey2(t *testing.T) { }, } var reply *engine.ChargerProfiles - if err := dspCppRPC.Call(utils.ChargerSv1GetChargersForEvent, + if err := dispEngine.RCP.Call(utils.ChargerSv1GetChargersForEvent, args, &reply); err != nil { t.Error(err) } else if !reflect.DeepEqual(eChargers, reply) { t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(eChargers), utils.ToJSON(reply)) } } - -func testDspCppKillEngine(t *testing.T) { - if err := engine.KillEngine(dspDelay); err != nil { - t.Error(err) - } - if err := engine.KillEngine(dspDelay); err != nil { - t.Error(err) - } -}