From e80825573596b5ebce0cdcaa4a56c6dcb6862a38 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Tue, 12 Feb 2019 09:42:55 +0200 Subject: [PATCH 1/6] 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) - } -} From 70c0c233d647ba0bc6bba644776cc4eaff81f0fb Mon Sep 17 00:00:00 2001 From: Trial97 Date: Tue, 12 Feb 2019 10:27:19 +0200 Subject: [PATCH 2/6] Aded tests for DispatcherService ThresholdSv1 --- apier/v1/chargers.go | 2 +- apier/v1/dispatcher.go | 7 +- apier/v1/thresholds.go | 2 +- cmd/cgr-engine/cgr-engine.go | 6 +- .../conf/samples/dispatchers/all/cgrates.json | 5 + .../samples/dispatchers/all2/cgrates.json | 4 + data/tariffplans/dispatchers/Attributes.csv | 1 + dispatchers/chargers_it_test.go | 4 +- dispatchers/thresholds.go | 45 ++-- dispatchers/thresholds_it_test.go | 251 ++++++------------ general_tests/tls_it_test.go | 6 +- 11 files changed, 126 insertions(+), 207 deletions(-) diff --git a/apier/v1/chargers.go b/apier/v1/chargers.go index 295acc3e8..e8b08314a 100644 --- a/apier/v1/chargers.go +++ b/apier/v1/chargers.go @@ -97,7 +97,7 @@ func (cSv1 *ChargerSv1) Call(serviceMethod string, return utils.APIerRPCCall(cSv1, serviceMethod, args, reply) } -func (cSv1 *ChargerSv1) Ping(ign utils.CGREvent, reply *string) error { +func (cSv1 *ChargerSv1) Ping(ign *utils.CGREvent, reply *string) error { *reply = utils.Pong return nil } diff --git a/apier/v1/dispatcher.go b/apier/v1/dispatcher.go index 9df129525..eb28ba9cd 100755 --- a/apier/v1/dispatcher.go +++ b/apier/v1/dispatcher.go @@ -83,8 +83,6 @@ func (apierV1 *ApierV1) RemoveDispatcherProfile(arg *utils.TenantID, reply *stri return nil } -/* - func NewDispatcherThresholdSv1(dps *dispatchers.DispatcherService) *DispatcherThresholdSv1 { return &DispatcherThresholdSv1{dS: dps} } @@ -95,8 +93,8 @@ type DispatcherThresholdSv1 struct { } // Ping implements ThresholdSv1Ping -func (dT *DispatcherThresholdSv1) Ping(ign string, reply *string) error { - return dT.dS.ThresholdSv1Ping(ign, reply) +func (dT *DispatcherThresholdSv1) Ping(args *dispatchers.CGREvWithApiKey, reply *string) error { + return dT.dS.ThresholdSv1Ping(args, reply) } // GetThresholdsForEvent implements ThresholdSv1GetThresholdsForEvent @@ -111,6 +109,7 @@ func (dT *DispatcherThresholdSv1) ProcessEvent(args *dispatchers.ArgsProcessEven return dT.dS.ThresholdSv1ProcessEvent(args, tIDs) } +/* func NewDispatcherStatSv1(dps *dispatchers.DispatcherService) *DispatcherStatSv1 { return &DispatcherStatSv1{dS: dps} } diff --git a/apier/v1/thresholds.go b/apier/v1/thresholds.go index 18a584f3a..ca5c4c79f 100644 --- a/apier/v1/thresholds.go +++ b/apier/v1/thresholds.go @@ -116,7 +116,7 @@ func (apierV1 *ApierV1) RemoveThresholdProfile(args *utils.TenantID, reply *stri return nil } -func (tSv1 *ThresholdSv1) Ping(ign string, reply *string) error { +func (tSv1 *ThresholdSv1) Ping(ign *utils.CGREvent, reply *string) error { *reply = utils.Pong return nil } diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index 295006b8b..b1d0a5e9c 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -1024,11 +1024,9 @@ func startDispatcherService(internalDispatcherSChan chan *dispatchers.Dispatcher return }() + server.RpcRegisterName(utils.ThresholdSv1, + v1.NewDispatcherThresholdSv1(dspS)) /* - if !cfg.ThresholdSCfg().Enabled && len(cfg.DispatcherSCfg().ThreshSConns) != 0 { - server.RpcRegisterName(utils.ThresholdSv1, - v1.NewDispatcherThresholdSv1(dspS)) - } if !cfg.StatSCfg().Enabled && len(cfg.DispatcherSCfg().StatSConns) != 0 { server.RpcRegisterName(utils.StatSv1, v1.NewDispatcherStatSv1(dspS)) diff --git a/data/conf/samples/dispatchers/all/cgrates.json b/data/conf/samples/dispatchers/all/cgrates.json index 61b72bd05..63585fc91 100644 --- a/data/conf/samples/dispatchers/all/cgrates.json +++ b/data/conf/samples/dispatchers/all/cgrates.json @@ -40,4 +40,9 @@ ], }, +"thresholds": { + "enabled": true, +}, + + } diff --git a/data/conf/samples/dispatchers/all2/cgrates.json b/data/conf/samples/dispatchers/all2/cgrates.json index 14249df30..b15db1121 100644 --- a/data/conf/samples/dispatchers/all2/cgrates.json +++ b/data/conf/samples/dispatchers/all2/cgrates.json @@ -40,4 +40,8 @@ ], }, +"thresholds": { + "enabled": true, +}, + } diff --git a/data/tariffplans/dispatchers/Attributes.csv b/data/tariffplans/dispatchers/Attributes.csv index 43fa47cbc..f0aacf4c0 100644 --- a/data/tariffplans/dispatchers/Attributes.csv +++ b/data/tariffplans/dispatchers/Attributes.csv @@ -3,5 +3,6 @@ cgrates.org,ATTR_1001_SIMPLEAUTH,simpleauth,*string:Account:1001,,Password,*any, 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 +cgrates.org,ATTR_API_THR_AUTH,*auth,*string:APIKey:thr12345,,APIMethods,*any,ThresholdSv1.Ping&ThresholdSv1.GetThresholdsForEvent&ThresholdSv1.ProcessEvent,true,false,20 diff --git a/dispatchers/chargers_it_test.go b/dispatchers/chargers_it_test.go index db19908c0..fbcf20442 100755 --- a/dispatchers/chargers_it_test.go +++ b/dispatchers/chargers_it_test.go @@ -60,7 +60,7 @@ func TestDspChargerS(t *testing.T) { func testDspCppPingFailover(t *testing.T) { var reply string - if err := allEngine.RCP.Call(utils.ChargerSv1Ping, utils.CGREvent{}, &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) @@ -132,7 +132,7 @@ func testDspCppGetChtgFailover(t *testing.T) { func testDspCppPing(t *testing.T) { var reply string - if err := allEngine.RCP.Call(utils.ChargerSv1Ping, utils.CGREvent{}, &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) diff --git a/dispatchers/thresholds.go b/dispatchers/thresholds.go index 6556d904f..075efbafb 100755 --- a/dispatchers/thresholds.go +++ b/dispatchers/thresholds.go @@ -18,40 +18,45 @@ along with this program. If not, see package dispatchers -/* import ( "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" ) -func (dS *DispatcherService) ThresholdSv1Ping(ign string, reply *string) error { - if dS.thdS == nil { - return utils.NewErrNotConnected(utils.ThresholdS) +func (dS *DispatcherService) ThresholdSv1Ping(args *CGREvWithApiKey, reply *string) (err error) { + if dS.attrS != nil { + if err = dS.authorize(utils.ThresholdSv1Ping, + args.CGREvent.Tenant, + args.APIKey, args.CGREvent.Time); err != nil { + return + } } - return dS.thdS.Call(utils.ThresholdSv1Ping, ign, reply) + return dS.Dispatch(&args.CGREvent, utils.MetaThresholds, + utils.ThresholdSv1Ping, args.CGREvent, reply) } func (dS *DispatcherService) ThresholdSv1GetThresholdsForEvent(args *ArgsProcessEventWithApiKey, t *engine.Thresholds) (err error) { - if dS.thdS == nil { - return utils.NewErrNotConnected(utils.ThresholdS) + if dS.attrS != nil { + if err = dS.authorize(utils.ThresholdSv1GetThresholdsForEvent, + args.ArgsProcessEvent.CGREvent.Tenant, + args.APIKey, args.ArgsProcessEvent.CGREvent.Time); err != nil { + return + } } - if err = dS.authorize(utils.ThresholdSv1GetThresholdsForEvent, args.ArgsProcessEvent.CGREvent.Tenant, - args.APIKey, args.ArgsProcessEvent.CGREvent.Time); err != nil { - return - } - return dS.thdS.Call(utils.ThresholdSv1GetThresholdsForEvent, args.ArgsProcessEvent, t) + return dS.Dispatch(&args.CGREvent, utils.MetaThresholds, + utils.ThresholdSv1GetThresholdsForEvent, args.ArgsProcessEvent, t) } func (dS *DispatcherService) ThresholdSv1ProcessEvent(args *ArgsProcessEventWithApiKey, tIDs *[]string) (err error) { - if dS.thdS == nil { - return utils.NewErrNotConnected(utils.ThresholdS) + if dS.attrS != nil { + if err = dS.authorize(utils.ThresholdSv1ProcessEvent, + args.ArgsProcessEvent.CGREvent.Tenant, + args.APIKey, args.ArgsProcessEvent.CGREvent.Time); err != nil { + return + } } - if err = dS.authorize(utils.ThresholdSv1ProcessEvent, args.ArgsProcessEvent.CGREvent.Tenant, - args.APIKey, args.ArgsProcessEvent.CGREvent.Time); err != nil { - return - } - return dS.thdS.Call(utils.ThresholdSv1ProcessEvent, args.ArgsProcessEvent, tIDs) + return dS.Dispatch(&args.CGREvent, utils.MetaThresholds, + utils.ThresholdSv1ProcessEvent, args.ArgsProcessEvent, tIDs) } -*/ diff --git a/dispatchers/thresholds_it_test.go b/dispatchers/thresholds_it_test.go index 5ca7ff8e2..e298fa844 100755 --- a/dispatchers/thresholds_it_test.go +++ b/dispatchers/thresholds_it_test.go @@ -21,164 +21,125 @@ 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 ( - dspThCfgPath string - dspThCfg *config.CGRConfig - dspThRPC *rpc.Client - instThCfgPath string - instThCfg *config.CGRConfig - instThRPC *rpc.Client -) - var sTestsDspTh = []func(t *testing.T){ - testDspThInitCfg, - testDspThInitDataDb, - testDspThResetStorDb, - testDspThStartEngine, - testDspThRPCConn, + testDspThPingFailover, + testDspThProcessEventFailover, + testDspThPing, - testDspThLoadData, - testDspThAddAttributesWithPermision, testDspThTestAuthKey, - testDspThAddAttributesWithPermision2, testDspThTestAuthKey2, - testDspThKillEngine, } //Test start here func TestDspThresholdS(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 sTestsDspTh { t.Run("", stest) } + attrEngine.stopEngine(t) + dispEngine.stopEngine(t) + allEngine.stopEngine(t) + allEngine2.stopEngine(t) } -func testDspThInitCfg(t *testing.T) { - var err error - dspThCfgPath = path.Join(dspDataDir, "conf", "samples", "dispatcher") - dspThCfg, err = config.NewCGRConfigFromFolder(dspThCfgPath) - if err != nil { +func testDspThPingFailover(t *testing.T) { + var reply string + if err := allEngine.RCP.Call(utils.ThresholdSv1Ping, &utils.CGREvent{}, &reply); err != nil { t.Error(err) + } else if reply != utils.Pong { + t.Errorf("Received: %s", reply) } - dspThCfg.DataFolderPath = dspDataDir // Share DataFolderPath through config towards StoreDb for Flush() - config.SetCgrConfig(dspThCfg) - instThCfgPath = path.Join(dspDataDir, "conf", "samples", "tutmysql") - instThCfg, err = config.NewCGRConfigFromFolder(instThCfgPath) - if err != nil { + ev := CGREvWithApiKey{ + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + }, + APIKey: "thr12345", + } + if err := dispEngine.RCP.Call(utils.ThresholdSv1Ping, &ev, &reply); err != nil { t.Error(err) + } else if reply != utils.Pong { + t.Errorf("Received: %s", reply) } - instThCfg.DataFolderPath = dspDataDir // Share DataFolderPath through config towards StoreDb for Flush() - config.SetCgrConfig(instThCfg) + allEngine.stopEngine(t) + if err := dispEngine.RCP.Call(utils.ThresholdSv1Ping, &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.ThresholdSv1Ping, &ev, &reply); err == nil { + t.Errorf("Expected error but recived %v and reply %v\n", err, reply) + } + allEngine.startEngine(t) + allEngine2.startEngine(t) } -func testDspThInitDataDb(t *testing.T) { - if err := engine.InitDataDb(instThCfg); err != nil { - t.Fatal(err) +func testDspThProcessEventFailover(t *testing.T) { + var ids []string + eIDs := []string{"THD_ACNT_1001"} + nowTime := time.Now() + args := &ArgsProcessEventWithApiKey{ + APIKey: "thr12345", + ArgsProcessEvent: engine.ArgsProcessEvent{ + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + ID: utils.UUIDSha1Prefix(), + Time: &nowTime, + Event: map[string]interface{}{ + utils.EVENT_NAME: "Event1", + utils.Account: "1001"}, + }, + }, } -} -// Wipe out the cdr database -func testDspThResetStorDb(t *testing.T) { - if err := engine.InitStorDb(instThCfg); err != nil { - t.Fatal(err) + if err := dispEngine.RCP.Call(utils.ThresholdSv1ProcessEvent, args, + &ids); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Expected error NOT_FOUND but recived %v and reply %v\n", err, ids) } -} - -// Start CGR Engine -func testDspThStartEngine(t *testing.T) { - if _, err := engine.StartEngine(instThCfgPath, dspDelay); err != nil { - t.Fatal(err) - } - if _, err := engine.StartEngine(dspThCfgPath, dspDelay); err != nil { - t.Fatal(err) - } -} - -// Connect rpc client to rater -func testDspThRPCConn(t *testing.T) { - var err error - instThRPC, err = jsonrpc.Dial("tcp", instThCfg.ListenCfg().RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed - if err != nil { - t.Fatal(err) - } - dspThRPC, err = jsonrpc.Dial("tcp", dspThCfg.ListenCfg().RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed - if err != nil { - t.Fatal(err) + allEngine2.stopEngine(t) + if err := dispEngine.RCP.Call(utils.ThresholdSv1ProcessEvent, args, &ids); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eIDs, ids) { + t.Errorf("expecting: %+v, received: %+v", eIDs, ids) } + allEngine2.startEngine(t) } func testDspThPing(t *testing.T) { var reply string - if err := instThRPC.Call(utils.ThresholdSv1Ping, "", &reply); err != nil { + if err := allEngine.RCP.Call(utils.ThresholdSv1Ping, &utils.CGREvent{}, &reply); err != nil { t.Error(err) } else if reply != utils.Pong { t.Errorf("Received: %s", reply) } - if err := dspThRPC.Call(utils.ThresholdSv1Ping, "", &reply); err != nil { + if err := dispEngine.RCP.Call(utils.ThresholdSv1Ping, &CGREvWithApiKey{ + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + }, + APIKey: "thr12345", + }, &reply); err != nil { t.Error(err) } else if reply != utils.Pong { t.Errorf("Received: %s", reply) } } -func testDspThLoadData(t *testing.T) { - var reply string - attrs := &utils.AttrLoadTpFromFolder{ - FolderPath: path.Join(dspDataDir, "tariffplans", "tutorial")} - if err := instThRPC.Call("ApierV1.LoadTariffPlanFromFolder", attrs, &reply); err != nil { - t.Error(err) - } - time.Sleep(500 * time.Millisecond) -} - -func testDspThAddAttributesWithPermision(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 := instThRPC.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 := instThRPC.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 testDspThTestAuthKey(t *testing.T) { var ids []string nowTime := time.Now() @@ -195,59 +156,14 @@ func testDspThTestAuthKey(t *testing.T) { }, } - if err := dspThRPC.Call(utils.ThresholdSv1ProcessEvent, + if err := dispEngine.RCP.Call(utils.ThresholdSv1ProcessEvent, args, &ids); err == nil || err.Error() != utils.ErrUnauthorizedApi.Error() { t.Error(err) } var th *engine.Thresholds - eTh := &engine.Thresholds{ - &engine.Threshold{ - Tenant: "cgrates.org", - ID: "THD_ACNT_1002", - Hits: 0, - }, - } - if err := dspThRPC.Call(utils.ThresholdSv1GetThresholdsForEvent, args, &th); err != nil { + if err := dispEngine.RCP.Call(utils.ThresholdSv1GetThresholdsForEvent, args, + &th); err == nil || err.Error() != utils.ErrUnauthorizedApi.Error() { t.Error(err) - } else if !reflect.DeepEqual(eTh, th) { - t.Errorf("expecting: %+v, received: %+v", eTh, th) - } -} - -func testDspThAddAttributesWithPermision2(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&ThresholdSv1.GetThresholdsForEvent", true, utils.INFIELD_SEP), - Append: true, - }, - }, - Weight: 20, - } - var result string - if err := instThRPC.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 := instThRPC.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) } } @@ -256,7 +172,7 @@ func testDspThTestAuthKey2(t *testing.T) { eIDs := []string{"THD_ACNT_1002"} nowTime := time.Now() args := &ArgsProcessEventWithApiKey{ - APIKey: "12345", + APIKey: "thr12345", ArgsProcessEvent: engine.ArgsProcessEvent{ CGREvent: utils.CGREvent{ Tenant: "cgrates.org", @@ -268,7 +184,7 @@ func testDspThTestAuthKey2(t *testing.T) { }, } - if err := dspThRPC.Call(utils.ThresholdSv1ProcessEvent, args, &ids); err != nil { + if err := dispEngine.RCP.Call(utils.ThresholdSv1ProcessEvent, args, &ids); err != nil { t.Error(err) } else if !reflect.DeepEqual(eIDs, ids) { t.Errorf("expecting: %+v, received: %+v", eIDs, ids) @@ -281,7 +197,7 @@ func testDspThTestAuthKey2(t *testing.T) { Hits: 1, }, } - if err := dspThRPC.Call(utils.ThresholdSv1GetThresholdsForEvent, args, &th); err != nil { + if err := dispEngine.RCP.Call(utils.ThresholdSv1GetThresholdsForEvent, args, &th); err != nil { t.Error(err) } else if !reflect.DeepEqual((*eTh)[0].Tenant, (*th)[0].Tenant) { t.Errorf("expecting: %+v, received: %+v", (*eTh)[0].Tenant, (*th)[0].Tenant) @@ -291,12 +207,3 @@ func testDspThTestAuthKey2(t *testing.T) { t.Errorf("expecting: %+v, received: %+v", (*eTh)[0].Hits, (*th)[0].Hits) } } - -func testDspThKillEngine(t *testing.T) { - if err := engine.KillEngine(dspDelay); err != nil { - t.Error(err) - } - if err := engine.KillEngine(dspDelay); err != nil { - t.Error(err) - } -} diff --git a/general_tests/tls_it_test.go b/general_tests/tls_it_test.go index 913e7928c..ca578c44d 100755 --- a/general_tests/tls_it_test.go +++ b/general_tests/tls_it_test.go @@ -106,17 +106,17 @@ func testTLSRpcConn(t *testing.T) { func testTLSPing(t *testing.T) { var reply string - if err := tlsRpcClientJson.Call(utils.ThresholdSv1Ping, "", &reply); err != nil { + if err := tlsRpcClientJson.Call(utils.ThresholdSv1Ping, &utils.CGREvent{}, &reply); err != nil { t.Error(err) } else if reply != utils.Pong { t.Errorf("Received: %s", reply) } - if err := tlsRpcClientGob.Call(utils.ThresholdSv1Ping, "", &reply); err != nil { + if err := tlsRpcClientGob.Call(utils.ThresholdSv1Ping, &utils.CGREvent{}, &reply); err != nil { t.Error(err) } else if reply != utils.Pong { t.Errorf("Received: %s", reply) } - if err := tlsHTTPJson.Call(utils.ThresholdSv1Ping, "", &reply); err != nil { + if err := tlsHTTPJson.Call(utils.ThresholdSv1Ping, &utils.CGREvent{}, &reply); err != nil { t.Error(err) } else if reply != utils.Pong { t.Errorf("Received: %s", reply) From fa80474afdad916b3d60b0148e060001dcb61fed Mon Sep 17 00:00:00 2001 From: Trial97 Date: Tue, 12 Feb 2019 12:02:07 +0200 Subject: [PATCH 3/6] Aded tests for DispatcherService SupplierSv1 --- apier/v1/apier_it_test.go | 2 +- apier/v1/dispatcher.go | 14 +- apier/v1/stats.go | 2 +- apier/v1/stats_it_test.go | 2 +- apier/v1/suppliers.go | 2 +- apier/v1/suppliers_it_test.go | 2 +- cmd/cgr-engine/cgr-engine.go | 14 +- .../conf/samples/dispatchers/all/cgrates.json | 6 + .../samples/dispatchers/all2/cgrates.json | 7 + data/tariffplans/dispatchers/Attributes.csv | 1 + dispatchers/stats.go | 64 ++-- dispatchers/suppliers.go | 31 +- dispatchers/suppliers_it_test.go | 280 +++++++----------- 13 files changed, 194 insertions(+), 233 deletions(-) diff --git a/apier/v1/apier_it_test.go b/apier/v1/apier_it_test.go index b4e81c5d7..b2dad5191 100644 --- a/apier/v1/apier_it_test.go +++ b/apier/v1/apier_it_test.go @@ -1944,7 +1944,7 @@ func TestApierPing(t *testing.T) { var reply string for _, method := range []string{utils.StatSv1Ping, utils.ResourceSv1Ping, utils.SupplierSv1Ping, utils.ThresholdSv1Ping, utils.AttributeSv1Ping} { - if err := rater.Call(method, utils.CGREvent{}, &reply); err != nil { + if err := rater.Call(method, &utils.CGREvent{}, &reply); err != nil { t.Error(err) } else if reply != utils.Pong { t.Errorf("Received: %s", reply) diff --git a/apier/v1/dispatcher.go b/apier/v1/dispatcher.go index eb28ba9cd..375c1edde 100755 --- a/apier/v1/dispatcher.go +++ b/apier/v1/dispatcher.go @@ -109,7 +109,6 @@ func (dT *DispatcherThresholdSv1) ProcessEvent(args *dispatchers.ArgsProcessEven return dT.dS.ThresholdSv1ProcessEvent(args, tIDs) } -/* func NewDispatcherStatSv1(dps *dispatchers.DispatcherService) *DispatcherStatSv1 { return &DispatcherStatSv1{dS: dps} } @@ -120,8 +119,8 @@ type DispatcherStatSv1 struct { } // Ping implements StatSv1Ping -func (dSts *DispatcherStatSv1) Ping(ign string, reply *string) error { - return dSts.dS.StatSv1Ping(ign, reply) +func (dSts *DispatcherStatSv1) Ping(args *dispatchers.CGREvWithApiKey, reply *string) error { + return dSts.dS.StatSv1Ping(args, reply) } // GetStatQueuesForEvent implements StatSv1GetStatQueuesForEvent @@ -140,6 +139,7 @@ func (dSts *DispatcherStatSv1) ProcessEvent(args *dispatchers.ArgsStatProcessEve return dSts.dS.StatSv1ProcessEvent(args, reply) } +/* func NewDispatcherResourceSv1(dps *dispatchers.DispatcherService) *DispatcherResourceSv1 { return &DispatcherResourceSv1{dRs: dps} } @@ -159,7 +159,7 @@ func (dRs *DispatcherResourceSv1) GetResourcesForEvent(args *dispatchers.ArgsV1R reply *engine.Resources) error { return dRs.dRs.ResourceSv1GetResourcesForEvent(args, reply) } - +*/ func NewDispatcherSupplierSv1(dps *dispatchers.DispatcherService) *DispatcherSupplierSv1 { return &DispatcherSupplierSv1{dSup: dps} } @@ -170,15 +170,15 @@ type DispatcherSupplierSv1 struct { } // Ping implements SupplierSv1Ping -func (dSup *DispatcherSupplierSv1) Ping(ign string, reply *string) error { - return dSup.dSup.SupplierSv1Ping(ign, reply) +func (dSup *DispatcherSupplierSv1) Ping(args *dispatchers.CGREvWithApiKey, reply *string) error { + return dSup.dSup.SupplierSv1Ping(args, reply) } // GetSuppliers implements SupplierSv1GetSuppliers func (dSup *DispatcherSupplierSv1) GetSuppliers(args *dispatchers.ArgsGetSuppliersWithApiKey, reply *engine.SortedSuppliers) error { return dSup.dSup.SupplierSv1GetSuppliers(args, reply) -}*/ +} func NewDispatcherAttributeSv1(dps *dispatchers.DispatcherService) *DispatcherAttributeSv1 { return &DispatcherAttributeSv1{dA: dps} diff --git a/apier/v1/stats.go b/apier/v1/stats.go index 9149365e7..adf0de53e 100644 --- a/apier/v1/stats.go +++ b/apier/v1/stats.go @@ -130,7 +130,7 @@ func (stsv1 *StatSv1) GetQueueFloatMetrics(args *utils.TenantID, reply *map[stri return stsv1.sS.V1GetQueueFloatMetrics(args, reply) } -func (stSv1 *StatSv1) Ping(ign string, reply *string) error { +func (stSv1 *StatSv1) Ping(ign *utils.CGREvent, reply *string) error { *reply = utils.Pong return nil } diff --git a/apier/v1/stats_it_test.go b/apier/v1/stats_it_test.go index 13fa75924..8890ecbd4 100644 --- a/apier/v1/stats_it_test.go +++ b/apier/v1/stats_it_test.go @@ -421,7 +421,7 @@ func testV1STSRemoveStatQueueProfile(t *testing.T) { func testV1STSStatsPing(t *testing.T) { var resp string - if err := stsV1Rpc.Call(utils.StatSv1Ping, "", &resp); err != nil { + if err := stsV1Rpc.Call(utils.StatSv1Ping, &utils.CGREvent{}, &resp); err != nil { t.Error(err) } else if resp != utils.Pong { t.Error("Unexpected reply returned", resp) diff --git a/apier/v1/suppliers.go b/apier/v1/suppliers.go index 360ce086d..c3899e5bb 100644 --- a/apier/v1/suppliers.go +++ b/apier/v1/suppliers.go @@ -102,7 +102,7 @@ func (splv1 *SupplierSv1) GetSuppliers(args *engine.ArgsGetSuppliers, return splv1.splS.V1GetSuppliers(args, reply) } -func (splv1 *SupplierSv1) Ping(ign string, reply *string) error { +func (splv1 *SupplierSv1) Ping(ign *utils.CGREvent, reply *string) error { *reply = utils.Pong return nil } diff --git a/apier/v1/suppliers_it_test.go b/apier/v1/suppliers_it_test.go index b16559230..81a440f17 100644 --- a/apier/v1/suppliers_it_test.go +++ b/apier/v1/suppliers_it_test.go @@ -973,7 +973,7 @@ func testV1SplSRemSupplierProfiles(t *testing.T) { func testV1SplSupplierPing(t *testing.T) { var resp string - if err := splSv1Rpc.Call(utils.SupplierSv1Ping, "", &resp); err != nil { + if err := splSv1Rpc.Call(utils.SupplierSv1Ping, &utils.CGREvent{}, &resp); err != nil { t.Error(err) } else if resp != utils.Pong { t.Error("Unexpected reply returned", resp) diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index b1d0a5e9c..59df56228 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -1026,20 +1026,18 @@ func startDispatcherService(internalDispatcherSChan chan *dispatchers.Dispatcher server.RpcRegisterName(utils.ThresholdSv1, v1.NewDispatcherThresholdSv1(dspS)) + + server.RpcRegisterName(utils.StatSv1, + v1.NewDispatcherStatSv1(dspS)) /* - if !cfg.StatSCfg().Enabled && len(cfg.DispatcherSCfg().StatSConns) != 0 { - server.RpcRegisterName(utils.StatSv1, - v1.NewDispatcherStatSv1(dspS)) - } if !cfg.ResourceSCfg().Enabled && len(cfg.DispatcherSCfg().ResSConns) != 0 { server.RpcRegisterName(utils.ResourceSv1, v1.NewDispatcherResourceSv1(dspS)) } - if !cfg.SupplierSCfg().Enabled && len(cfg.DispatcherSCfg().SupplSConns) != 0 { - server.RpcRegisterName(utils.SupplierSv1, - v1.NewDispatcherSupplierSv1(dspS)) - } */ + server.RpcRegisterName(utils.SupplierSv1, + v1.NewDispatcherSupplierSv1(dspS)) + server.RpcRegisterName(utils.AttributeSv1, v1.NewDispatcherAttributeSv1(dspS)) /* diff --git a/data/conf/samples/dispatchers/all/cgrates.json b/data/conf/samples/dispatchers/all/cgrates.json index 63585fc91..d7304b433 100644 --- a/data/conf/samples/dispatchers/all/cgrates.json +++ b/data/conf/samples/dispatchers/all/cgrates.json @@ -44,5 +44,11 @@ "enabled": true, }, +"suppliers": { + "enabled": true, + // "stats_conns": [ + // {"address": "*internal"}, + // ], +}, } diff --git a/data/conf/samples/dispatchers/all2/cgrates.json b/data/conf/samples/dispatchers/all2/cgrates.json index b15db1121..43fca6467 100644 --- a/data/conf/samples/dispatchers/all2/cgrates.json +++ b/data/conf/samples/dispatchers/all2/cgrates.json @@ -44,4 +44,11 @@ "enabled": true, }, +"suppliers": { + "enabled": true, + // "stats_conns": [ + // {"address": "*internal"}, + // ], +}, + } diff --git a/data/tariffplans/dispatchers/Attributes.csv b/data/tariffplans/dispatchers/Attributes.csv index f0aacf4c0..e7bcd34d2 100644 --- a/data/tariffplans/dispatchers/Attributes.csv +++ b/data/tariffplans/dispatchers/Attributes.csv @@ -4,5 +4,6 @@ cgrates.org,ATTR_API_ATTR_FAKE_AUTH,*auth,*string:APIKey:12345,,APIMethods,*any, 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 cgrates.org,ATTR_API_THR_AUTH,*auth,*string:APIKey:thr12345,,APIMethods,*any,ThresholdSv1.Ping&ThresholdSv1.GetThresholdsForEvent&ThresholdSv1.ProcessEvent,true,false,20 +cgrates.org,ATTR_API_SUP_AUTH,*auth,*string:APIKey:sup12345,,APIMethods,*any,SupplierSv1.Ping&SupplierSv1.GetSuppliers,true,false,20 diff --git a/dispatchers/stats.go b/dispatchers/stats.go index 3188e017a..a5b0184b2 100755 --- a/dispatchers/stats.go +++ b/dispatchers/stats.go @@ -18,54 +18,62 @@ along with this program. If not, see package dispatchers -/* import ( "time" "github.com/cgrates/cgrates/utils" ) -func (dS *DispatcherService) StatSv1Ping(ign string, reply *string) error { - if dS.statS == nil { - return utils.NewErrNotConnected(utils.StatS) +func (dS *DispatcherService) StatSv1Ping(args *CGREvWithApiKey, reply *string) (err error) { + if dS.attrS != nil { + if err = dS.authorize(utils.StatSv1Ping, + args.CGREvent.Tenant, + args.APIKey, args.CGREvent.Time); err != nil { + return + } } - return dS.statS.Call(utils.StatSv1Ping, ign, reply) + return dS.Dispatch(&args.CGREvent, utils.MetaStats, + utils.StatSv1Ping, args.CGREvent, reply) } func (dS *DispatcherService) StatSv1GetStatQueuesForEvent(args *ArgsStatProcessEventWithApiKey, reply *[]string) (err error) { - if dS.statS == nil { - return utils.NewErrNotConnected(utils.StatS) + if dS.attrS != nil { + if err = dS.authorize(utils.StatSv1GetStatQueuesForEvent, + args.CGREvent.Tenant, + args.APIKey, args.CGREvent.Time); err != nil { + return + } } - if err = dS.authorize(utils.StatSv1GetStatQueuesForEvent, args.CGREvent.Tenant, - args.APIKey, args.CGREvent.Time); err != nil { - return - } - return dS.statS.Call(utils.StatSv1GetStatQueuesForEvent, args, reply) + return dS.Dispatch(&args.CGREvent, utils.MetaStats, + utils.StatSv1GetStatQueuesForEvent, args.StatsArgsProcessEvent, reply) } func (dS *DispatcherService) StatSv1GetQueueStringMetrics(args *TntIDWithApiKey, reply *map[string]string) (err error) { - if dS.statS == nil { - return utils.NewErrNotConnected(utils.StatS) + if dS.attrS != nil { + if err = dS.authorize(utils.StatSv1GetQueueStringMetrics, + args.TenantID.Tenant, + args.APIKey, utils.TimePointer(time.Now())); err != nil { + return + } } - nowTime := time.Now() - if err = dS.authorize(utils.StatSv1GetQueueStringMetrics, args.TenantID.Tenant, - args.APIKey, &nowTime); err != nil { - return - } - return dS.statS.Call(utils.StatSv1GetQueueStringMetrics, args.TenantID, reply) + return dS.Dispatch(&utils.CGREvent{ + Tenant: args.Tenant, + ID: args.ID, + }, utils.MetaStats, utils.StatSv1GetQueueStringMetrics, + args.TenantID, reply) } func (dS *DispatcherService) StatSv1ProcessEvent(args *ArgsStatProcessEventWithApiKey, reply *[]string) (err error) { - if dS.statS == nil { - return utils.NewErrNotConnected(utils.StatS) + if dS.attrS != nil { + if err = dS.authorize(utils.StatSv1ProcessEvent, + args.CGREvent.Tenant, + args.APIKey, args.CGREvent.Time); err != nil { + return + } } - if err = dS.authorize(utils.StatSv1ProcessEvent, args.CGREvent.Tenant, - args.APIKey, args.CGREvent.Time); err != nil { - return - } - return dS.statS.Call(utils.StatSv1ProcessEvent, args, reply) + return dS.Dispatch(&args.CGREvent, utils.MetaStats, + utils.StatSv1ProcessEvent, args.StatsArgsProcessEvent, reply) } -*/ diff --git a/dispatchers/suppliers.go b/dispatchers/suppliers.go index 061b2fbe1..0b0151f0f 100755 --- a/dispatchers/suppliers.go +++ b/dispatchers/suppliers.go @@ -18,29 +18,32 @@ along with this program. If not, see package dispatchers -/* import ( "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" ) -func (dS *DispatcherService) SupplierSv1Ping(ign string, reply *string) error { - if dS.splS == nil { - return utils.NewErrNotConnected(utils.SupplierS) +func (dS *DispatcherService) SupplierSv1Ping(args *CGREvWithApiKey, reply *string) (err error) { + if dS.attrS != nil { + if err = dS.authorize(utils.SupplierSv1Ping, + args.CGREvent.Tenant, + args.APIKey, args.CGREvent.Time); err != nil { + return + } } - return dS.splS.Call(utils.SupplierSv1Ping, ign, reply) + return dS.Dispatch(&args.CGREvent, utils.MetaSuppliers, + utils.SupplierSv1Ping, args.CGREvent, reply) } func (dS *DispatcherService) SupplierSv1GetSuppliers(args *ArgsGetSuppliersWithApiKey, reply *engine.SortedSuppliers) (err error) { - if dS.splS == nil { - return utils.NewErrNotConnected(utils.SupplierS) + if dS.attrS != nil { + if err = dS.authorize(utils.SupplierSv1GetSuppliers, + args.ArgsGetSuppliers.CGREvent.Tenant, + args.APIKey, args.ArgsGetSuppliers.CGREvent.Time); err != nil { + return + } } - if err = dS.authorize(utils.SupplierSv1GetSuppliers, args.ArgsGetSuppliers.CGREvent.Tenant, - args.APIKey, args.ArgsGetSuppliers.CGREvent.Time); err != nil { - return - } - return dS.splS.Call(utils.SupplierSv1GetSuppliers, args.ArgsGetSuppliers, reply) - + return dS.Dispatch(&args.CGREvent, utils.MetaSuppliers, + utils.SupplierSv1GetSuppliers, args.ArgsGetSuppliers, reply) } -*/ diff --git a/dispatchers/suppliers_it_test.go b/dispatchers/suppliers_it_test.go index f8da1bc75..474938e0a 100755 --- a/dispatchers/suppliers_it_test.go +++ b/dispatchers/suppliers_it_test.go @@ -21,163 +21,147 @@ 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 ( - dspSupCfgPath string - dspSupCfg *config.CGRConfig - dspSupRPC *rpc.Client - instSupCfgPath string - instSupCfg *config.CGRConfig - instSupRPC *rpc.Client -) - var sTestsDspSup = []func(t *testing.T){ - testDspSupInitCfg, - testDspSupInitDataDb, - testDspSupResetStorDb, - testDspSupStartEngine, - testDspSupRPCConn, testDspSupPing, - testDspSupLoadData, - testDspSupAddAttributesWithPermision, testDspSupTestAuthKey, - testDspSupAddAttributesWithPermision2, testDspSupTestAuthKey2, - testDspSupKillEngine, } //Test start here func TestDspSupplierS(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 sTestsDspSup { t.Run("", stest) } -} - -func testDspSupInitCfg(t *testing.T) { - var err error - dspSupCfgPath = path.Join(dspDataDir, "conf", "samples", "dispatcher") - dspSupCfg, err = config.NewCGRConfigFromFolder(dspSupCfgPath) - if err != nil { - t.Error(err) - } - dspSupCfg.DataFolderPath = dspDataDir // Share DataFolderPath through config towards StoreDb for Flush() - config.SetCgrConfig(dspSupCfg) - instSupCfgPath = path.Join(dspDataDir, "conf", "samples", "tutmysql") - instSupCfg, err = config.NewCGRConfigFromFolder(instSupCfgPath) - if err != nil { - t.Error(err) - } - instSupCfg.DataFolderPath = dspDataDir // Share DataFolderPath through config towards StoreDb for Flush() - config.SetCgrConfig(instSupCfg) -} - -func testDspSupInitDataDb(t *testing.T) { - if err := engine.InitDataDb(instSupCfg); err != nil { - t.Fatal(err) - } -} - -// Wipe out the cdr database -func testDspSupResetStorDb(t *testing.T) { - if err := engine.InitStorDb(instSupCfg); err != nil { - t.Fatal(err) - } -} - -// Start CGR Engine -func testDspSupStartEngine(t *testing.T) { - if _, err := engine.StartEngine(instSupCfgPath, dspDelay); err != nil { - t.Fatal(err) - } - if _, err := engine.StartEngine(dspSupCfgPath, dspDelay); err != nil { - t.Fatal(err) - } -} - -// Connect rpc client to rater -func testDspSupRPCConn(t *testing.T) { - var err error - instSupRPC, err = jsonrpc.Dial("tcp", instSupCfg.ListenCfg().RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed - if err != nil { - t.Fatal(err) - } - dspSupRPC, err = jsonrpc.Dial("tcp", dspSupCfg.ListenCfg().RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed - if err != nil { - t.Fatal(err) - } - + attrEngine.stopEngine(t) + dispEngine.stopEngine(t) + allEngine.stopEngine(t) + allEngine2.stopEngine(t) } func testDspSupPing(t *testing.T) { var reply string - if err := instSupRPC.Call(utils.SupplierSv1Ping, "", &reply); err != nil { + if err := allEngine.RCP.Call(utils.SupplierSv1Ping, &utils.CGREvent{}, &reply); err != nil { t.Error(err) } else if reply != utils.Pong { t.Errorf("Received: %s", reply) } - if err := dspSupRPC.Call(utils.SupplierSv1Ping, "", &reply); err != nil { - t.Error(err) - } else if reply != utils.Pong { - t.Errorf("Received: %s", reply) - } -} - -func testDspSupLoadData(t *testing.T) { - var reply string - attrs := &utils.AttrLoadTpFromFolder{ - FolderPath: path.Join(dspDataDir, "tariffplans", "tutorial")} - if err := instSupRPC.Call("ApierV1.LoadTariffPlanFromFolder", attrs, &reply); err != nil { - t.Error(err) - } - time.Sleep(500 * time.Millisecond) -} - -func testDspSupAddAttributesWithPermision(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), + if err := dispEngine.RCP.Call(utils.SupplierSv1Ping, &CGREvWithApiKey{ + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", }, - Attributes: []*engine.Attribute{ + APIKey: "sup12345", + }, &reply); err != nil { + t.Error(err) + } else if reply != utils.Pong { + t.Errorf("Received: %s", reply) + } +} + +func testDspSupPingFailover(t *testing.T) { + var reply string + if err := allEngine.RCP.Call(utils.SupplierSv1Ping, &utils.CGREvent{}, &reply); err != nil { + t.Error(err) + } else if reply != utils.Pong { + t.Errorf("Received: %s", reply) + } + ev := CGREvWithApiKey{ + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + }, + APIKey: "sup12345", + } + if err := dispEngine.RCP.Call(utils.SupplierSv1Ping, &ev, &reply); err != nil { + t.Error(err) + } else if reply != utils.Pong { + t.Errorf("Received: %s", reply) + } + allEngine.stopEngine(t) + if err := dispEngine.RCP.Call(utils.SupplierSv1Ping, &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.SupplierSv1Ping, &ev, &reply); err == nil { + t.Errorf("Expected error but recived %v and reply %v\n", err, reply) + } + allEngine.startEngine(t) + allEngine2.startEngine(t) +} + +func testDspSupGetSupFailover(t *testing.T) { + var rpl *engine.SortedSuppliers + eRpl := &engine.SortedSuppliers{ + ProfileID: "SPL_ACNT_1002", + Sorting: utils.MetaLeastCost, + SortedSuppliers: []*engine.SortedSupplier{ { - FieldName: utils.APIMethods, - Initial: utils.META_ANY, - Substitute: config.NewRSRParsersMustCompile("ThresholdSv1.GetThresholdsForEvent", true, utils.INFIELD_SEP), - Append: true, + SupplierID: "supplier1", + SupplierParameters: "", + SortingData: map[string]interface{}{ + utils.Cost: 0.1166, + utils.RatingPlanID: "RP_1002_LOW", + utils.Weight: 10.0, + }, + }, + { + SupplierID: "supplier2", + SupplierParameters: "", + SortingData: map[string]interface{}{ + utils.Cost: 0.2334, + utils.RatingPlanID: "RP_1002", + utils.Weight: 20.0, + }, }, }, - Weight: 20, } - var result string - if err := instSupRPC.Call("ApierV1.SetAttributeProfile", alsPrf, &result); err != nil { + args := &ArgsGetSuppliersWithApiKey{ + APIKey: "sup12345", + ArgsGetSuppliers: engine.ArgsGetSuppliers{ + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + ID: utils.UUIDSha1Prefix(), + Time: &nowTime, + Event: map[string]interface{}{ + utils.EVENT_NAME: "Event1", + utils.Account: "1002", + utils.Subject: "1002", + utils.Destination: "1001", + utils.SetupTime: time.Date(2017, 12, 1, 14, 25, 0, 0, time.UTC), + utils.Usage: "1m20s", + }, + }, + }, + } + if err := dispEngine.RCP.Call(utils.SupplierSv1GetSuppliers, + args, &rpl); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Expected error NOT_FOUND but recived %v and reply %v\n", err, rpl) + } + allEngine2.stopEngine(t) + if err := dispEngine.RCP.Call(utils.SupplierSv1GetSuppliers, + args, &rpl); err != nil { t.Error(err) - } else if result != utils.OK { - t.Error("Unexpected reply returned", result) - } - alsPrf.Compile() - var reply *engine.AttributeProfile - if err := instSupRPC.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) + } else if !reflect.DeepEqual(eRpl, rpl) { + t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(eRpl), utils.ToJSON(rpl)) } + allEngine2.startEngine(t) } func testDspSupTestAuthKey(t *testing.T) { @@ -199,49 +183,12 @@ func testDspSupTestAuthKey(t *testing.T) { }, }, } - if err := dspSupRPC.Call(utils.SupplierSv1GetSuppliers, + if err := dispEngine.RCP.Call(utils.SupplierSv1GetSuppliers, args, &rpl); err == nil || err.Error() != utils.ErrUnauthorizedApi.Error() { t.Error(err) } } -func testDspSupAddAttributesWithPermision2(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&SupplierSv1.GetSuppliers", true, utils.INFIELD_SEP), - Append: true, - }, - }, - Weight: 20, - } - var result string - if err := instSupRPC.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 := instSupRPC.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 testDspSupTestAuthKey2(t *testing.T) { var rpl *engine.SortedSuppliers eRpl := &engine.SortedSuppliers{ @@ -269,7 +216,7 @@ func testDspSupTestAuthKey2(t *testing.T) { }, } args := &ArgsGetSuppliersWithApiKey{ - APIKey: "12345", + APIKey: "sup12345", ArgsGetSuppliers: engine.ArgsGetSuppliers{ CGREvent: utils.CGREvent{ Tenant: "cgrates.org", @@ -285,19 +232,10 @@ func testDspSupTestAuthKey2(t *testing.T) { }, }, } - if err := dspSupRPC.Call(utils.SupplierSv1GetSuppliers, + if err := dispEngine.RCP.Call(utils.SupplierSv1GetSuppliers, args, &rpl); err != nil { t.Error(err) } else if !reflect.DeepEqual(eRpl, rpl) { t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(eRpl), utils.ToJSON(rpl)) } } - -func testDspSupKillEngine(t *testing.T) { - if err := engine.KillEngine(dspDelay); err != nil { - t.Error(err) - } - if err := engine.KillEngine(dspDelay); err != nil { - t.Error(err) - } -} From 51abeb5c32471d0c167b0ffed0c0fb972c53a063 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Tue, 12 Feb 2019 13:27:15 +0200 Subject: [PATCH 4/6] Aded tests for DispatcherService StatSv1 --- .../conf/samples/dispatchers/all/cgrates.json | 11 +- .../samples/dispatchers/all2/cgrates.json | 11 +- data/tariffplans/dispatchers/Attributes.csv | 1 + dispatchers/stats_it_test.go | 268 +++++++----------- 4 files changed, 123 insertions(+), 168 deletions(-) diff --git a/data/conf/samples/dispatchers/all/cgrates.json b/data/conf/samples/dispatchers/all/cgrates.json index d7304b433..7150d04c9 100644 --- a/data/conf/samples/dispatchers/all/cgrates.json +++ b/data/conf/samples/dispatchers/all/cgrates.json @@ -46,9 +46,14 @@ "suppliers": { "enabled": true, - // "stats_conns": [ - // {"address": "*internal"}, - // ], +}, + +"stats": { + "enabled": true, + "store_interval": "1s", + "thresholds_conns": [ + {"address": "*internal"} + ], }, } diff --git a/data/conf/samples/dispatchers/all2/cgrates.json b/data/conf/samples/dispatchers/all2/cgrates.json index 43fca6467..1b45f6d76 100644 --- a/data/conf/samples/dispatchers/all2/cgrates.json +++ b/data/conf/samples/dispatchers/all2/cgrates.json @@ -46,9 +46,14 @@ "suppliers": { "enabled": true, - // "stats_conns": [ - // {"address": "*internal"}, - // ], +}, + +"stats": { + "enabled": true, + "store_interval": "1s", + "thresholds_conns": [ + {"address": "*internal"} + ], }, } diff --git a/data/tariffplans/dispatchers/Attributes.csv b/data/tariffplans/dispatchers/Attributes.csv index e7bcd34d2..66e5b50d7 100644 --- a/data/tariffplans/dispatchers/Attributes.csv +++ b/data/tariffplans/dispatchers/Attributes.csv @@ -5,5 +5,6 @@ cgrates.org,ATTR_API_ATTR_AUTH,*auth,*string:APIKey:attr12345,,APIMethods,*any,A cgrates.org,ATTR_API_CHRG_AUTH,*auth,*string:APIKey:chrg12345,,APIMethods,*any,ChargerSv1.Ping&ChargerSv1.GetChargersForEvent&ChargerSv1.ProcessEvent,true,false,20 cgrates.org,ATTR_API_THR_AUTH,*auth,*string:APIKey:thr12345,,APIMethods,*any,ThresholdSv1.Ping&ThresholdSv1.GetThresholdsForEvent&ThresholdSv1.ProcessEvent,true,false,20 cgrates.org,ATTR_API_SUP_AUTH,*auth,*string:APIKey:sup12345,,APIMethods,*any,SupplierSv1.Ping&SupplierSv1.GetSuppliers,true,false,20 +cgrates.org,ATTR_API_STAT_AUTH,*auth,*string:APIKey:stat12345,,APIMethods,*any,StatSv1.Ping&StatSv1.GetStatQueuesForEvent&StatSv1.GetQueueStringMetrics&StatSv1.ProcessEvent,true,false,20 diff --git a/dispatchers/stats_it_test.go b/dispatchers/stats_it_test.go index 02e473847..6aa5b80e3 100755 --- a/dispatchers/stats_it_test.go +++ b/dispatchers/stats_it_test.go @@ -22,7 +22,6 @@ package dispatchers import ( "net/rpc" - "net/rpc/jsonrpc" "path" "reflect" "testing" @@ -43,143 +42,134 @@ var ( ) var sTestsDspSts = []func(t *testing.T){ - testDspStsInitCfg, - testDspStsInitDataDb, - testDspStsResetStorDb, - testDspStsStartEngine, - testDspStsRPCConn, + testDspStsPingFailover, + testDspStsGetStatFailover, + testDspStsPing, - testDspStsLoadData, - testDspStsAddStsibutesWithPermision, testDspStsTestAuthKey, - testDspStsAddStsibutesWithPermision2, testDspStsTestAuthKey2, - testDspStsKillEngine, } //Test start here func TestDspStatS(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 sTestsDspSts { t.Run("", stest) } + attrEngine.stopEngine(t) + dispEngine.stopEngine(t) + allEngine.stopEngine(t) + allEngine2.stopEngine(t) } -func testDspStsInitCfg(t *testing.T) { - var err error - dspStsCfgPath = path.Join(dspDataDir, "conf", "samples", "dispatcher") - dspStsCfg, err = config.NewCGRConfigFromFolder(dspStsCfgPath) - if err != nil { +func testDspStsPingFailover(t *testing.T) { + var reply string + if err := allEngine.RCP.Call(utils.StatSv1Ping, &utils.CGREvent{}, &reply); err != nil { + t.Error(err) + } else if reply != utils.Pong { + t.Errorf("Received: %s", reply) + } + ev := CGREvWithApiKey{ + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + }, + APIKey: "stat12345", + } + if err := dispEngine.RCP.Call(utils.StatSv1Ping, &ev, &reply); err != nil { + t.Error(err) + } else if reply != utils.Pong { + t.Errorf("Received: %s", reply) + } + allEngine.stopEngine(t) + if err := dispEngine.RCP.Call(utils.StatSv1Ping, &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.StatSv1Ping, &ev, &reply); err == nil { + t.Errorf("Expected error but recived %v and reply %v\n", err, reply) + } + allEngine.startEngine(t) + allEngine2.startEngine(t) +} + +func testDspStsGetStatFailover(t *testing.T) { + var reply []string + var metrics map[string]string + expected := []string{"Stats1"} + args := ArgsStatProcessEventWithApiKey{ + APIKey: "stat12345", + StatsArgsProcessEvent: engine.StatsArgsProcessEvent{ + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + ID: "event1", + Event: map[string]interface{}{ + utils.EVENT_NAME: "Event1", + utils.Account: "1001", + utils.AnswerTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + utils.Usage: time.Duration(135 * time.Second), + utils.COST: 123.0, + utils.RunID: utils.DEFAULT_RUNID, + utils.Destination: "1002"}, + }, + }, + } + if err := dispEngine.RCP.Call(utils.StatSv1ProcessEvent, args, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(reply, expected) { + t.Errorf("Expecting: %+v, received: %+v", expected, reply) + } + + args2 := TntIDWithApiKey{ + APIKey: "stat12345", + TenantID: utils.TenantID{ + Tenant: "cgrates.org", + ID: "Stats1", + }, + } + allEngine.stopEngine(t) + if err := dispEngine.RCP.Call(utils.StatSv1GetQueueStringMetrics, + args2, &metrics); err != nil { t.Error(err) } - dspStsCfg.DataFolderPath = dspDataDir // Share DataFolderPath through config towards StoreDb for Flush() - config.SetCgrConfig(dspStsCfg) - instStsCfgPath = path.Join(dspDataDir, "conf", "samples", "tutmysql") - instStsCfg, err = config.NewCGRConfigFromFolder(instStsCfgPath) - if err != nil { - t.Error(err) - } - instStsCfg.DataFolderPath = dspDataDir // Share DataFolderPath through config towards StoreDb for Flush() - config.SetCgrConfig(instStsCfg) -} -func testDspStsInitDataDb(t *testing.T) { - if err := engine.InitDataDb(instStsCfg); err != nil { - t.Fatal(err) - } -} + allEngine.startEngine(t) + allEngine2.stopEngine(t) -// Wipe out the cdr database -func testDspStsResetStorDb(t *testing.T) { - if err := engine.InitStorDb(instStsCfg); err != nil { - t.Fatal(err) + if err := dispEngine.RCP.Call(utils.StatSv1GetQueueStringMetrics, + args2, &metrics); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Expected error NOT_FOUND but recived %v and reply %v\n", err, reply) } -} - -// Start CGR Engine -func testDspStsStartEngine(t *testing.T) { - if _, err := engine.StartEngine(instStsCfgPath, dspDelay); err != nil { - t.Fatal(err) - } - if _, err := engine.StartEngine(dspStsCfgPath, dspDelay); err != nil { - t.Fatal(err) - } -} - -// Connect rpc client to rater -func testDspStsRPCConn(t *testing.T) { - var err error - instStsRPC, err = jsonrpc.Dial("tcp", instStsCfg.ListenCfg().RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed - if err != nil { - t.Fatal(err) - } - dspStsRPC, err = jsonrpc.Dial("tcp", dspStsCfg.ListenCfg().RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed - if err != nil { - t.Fatal(err) - } - + allEngine2.startEngine(t) } func testDspStsPing(t *testing.T) { var reply string - if err := instStsRPC.Call(utils.StatSv1Ping, "", &reply); err != nil { + if err := allEngine.RCP.Call(utils.StatSv1Ping, &utils.CGREvent{}, &reply); err != nil { t.Error(err) } else if reply != utils.Pong { t.Errorf("Received: %s", reply) } - if err := dspStsRPC.Call(utils.StatSv1Ping, "", &reply); err != nil { + if err := dispEngine.RCP.Call(utils.StatSv1Ping, &CGREvWithApiKey{ + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + }, + APIKey: "stat12345", + }, &reply); err != nil { t.Error(err) } else if reply != utils.Pong { t.Errorf("Received: %s", reply) } } -func testDspStsLoadData(t *testing.T) { - var reply string - Stss := &utils.AttrLoadTpFromFolder{ - FolderPath: path.Join(dspDataDir, "tariffplans", "tutorial")} - if err := instStsRPC.Call("ApierV1.LoadTariffPlanFromFolder", Stss, &reply); err != nil { - t.Error(err) - } - time.Sleep(500 * time.Millisecond) -} - -func testDspStsAddStsibutesWithPermision(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.GetThSessionholdsForEvent", true, utils.INFIELD_SEP), - Append: true, - }, - }, - Weight: 20, - } - var result string - if err := instStsRPC.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 := instStsRPC.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 testDspStsTestAuthKey(t *testing.T) { var reply []string args := ArgsStatProcessEventWithApiKey{ @@ -195,7 +185,7 @@ func testDspStsTestAuthKey(t *testing.T) { utils.COST: 123.0, utils.PDD: time.Duration(12 * time.Second)}}, }} - if err := dspStsRPC.Call(utils.StatSv1ProcessEvent, + if err := dispEngine.RCP.Call(utils.StatSv1ProcessEvent, args, &reply); err == nil || err.Error() != utils.ErrUnauthorizedApi.Error() { t.Error(err) } @@ -209,55 +199,18 @@ func testDspStsTestAuthKey(t *testing.T) { } var metrics map[string]string - if err := dspStsRPC.Call(utils.StatSv1GetQueueStringMetrics, + if err := dispEngine.RCP.Call(utils.StatSv1GetQueueStringMetrics, args2, &metrics); err == nil || err.Error() != utils.ErrUnauthorizedApi.Error() { t.Error(err) } } -func testDspStsAddStsibutesWithPermision2(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("StatSv1.ProcessEvent&StatSv1.GetQueueStringMetrics", true, utils.INFIELD_SEP), - Append: true, - }, - }, - Weight: 20, - } - var result string - if err := instStsRPC.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 := instStsRPC.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 testDspStsTestAuthKey2(t *testing.T) { var reply []string var metrics map[string]string expected := []string{"Stats2"} args := ArgsStatProcessEventWithApiKey{ - APIKey: "12345", + APIKey: "stat12345", StatsArgsProcessEvent: engine.StatsArgsProcessEvent{ CGREvent: utils.CGREvent{ Tenant: "cgrates.org", @@ -272,14 +225,14 @@ func testDspStsTestAuthKey2(t *testing.T) { }, }, } - if err := dspStsRPC.Call(utils.StatSv1ProcessEvent, args, &reply); err != nil { + if err := dispEngine.RCP.Call(utils.StatSv1ProcessEvent, args, &reply); err != nil { t.Error(err) } else if !reflect.DeepEqual(reply, expected) { t.Errorf("Expecting: %+v, received: %+v", expected, reply) } args2 := TntIDWithApiKey{ - APIKey: "12345", + APIKey: "stat12345", TenantID: utils.TenantID{ Tenant: "cgrates.org", ID: "Stats2", @@ -290,7 +243,7 @@ func testDspStsTestAuthKey2(t *testing.T) { utils.MetaTCD: "2m15s", } - if err := dspStsRPC.Call(utils.StatSv1GetQueueStringMetrics, + if err := dispEngine.RCP.Call(utils.StatSv1GetQueueStringMetrics, args2, &metrics); err != nil { t.Error(err) } else if !reflect.DeepEqual(expectedMetrics, metrics) { @@ -298,7 +251,7 @@ func testDspStsTestAuthKey2(t *testing.T) { } args = ArgsStatProcessEventWithApiKey{ - APIKey: "12345", + APIKey: "stat12345", StatsArgsProcessEvent: engine.StatsArgsProcessEvent{ CGREvent: utils.CGREvent{ Tenant: "cgrates.org", @@ -314,7 +267,7 @@ func testDspStsTestAuthKey2(t *testing.T) { }, }, } - if err := dspStsRPC.Call(utils.StatSv1ProcessEvent, args, &reply); err != nil { + if err := dispEngine.RCP.Call(utils.StatSv1ProcessEvent, args, &reply); err != nil { t.Error(err) } else if !reflect.DeepEqual(reply, expected) { t.Errorf("Expecting: %+v, received: %+v", expected, reply) @@ -324,19 +277,10 @@ func testDspStsTestAuthKey2(t *testing.T) { utils.MetaTCC: "133", utils.MetaTCD: "3m0s", } - if err := dspStsRPC.Call(utils.StatSv1GetQueueStringMetrics, + if err := dispEngine.RCP.Call(utils.StatSv1GetQueueStringMetrics, args2, &metrics); err != nil { t.Error(err) } else if !reflect.DeepEqual(expectedMetrics, metrics) { t.Errorf("expecting: %+v, received reply: %s", expectedMetrics, metrics) } } - -func testDspStsKillEngine(t *testing.T) { - if err := engine.KillEngine(dspDelay); err != nil { - t.Error(err) - } - if err := engine.KillEngine(dspDelay); err != nil { - t.Error(err) - } -} From bf6b80137245639df01ef51938012311b8e7d7ef Mon Sep 17 00:00:00 2001 From: Trial97 Date: Tue, 12 Feb 2019 14:06:53 +0200 Subject: [PATCH 5/6] Aded tests for DispatcherService ResourceSv1 --- apier/v1/dispatcher.go | 7 +- apier/v1/resourcesv1.go | 2 +- apier/v1/resourcesv1_it_test.go | 2 +- cmd/cgr-engine/cgr-engine.go | 10 +- .../conf/samples/dispatchers/all/cgrates.json | 8 +- .../samples/dispatchers/all2/cgrates.json | 8 +- data/tariffplans/dispatchers/Attributes.csv | 2 +- dispatchers/resources.go | 31 +-- dispatchers/resources_it_test.go | 214 ++++-------------- dispatchers/sessions_it_test.go | 32 +-- dispatchers/stats_it_test.go | 11 - 11 files changed, 100 insertions(+), 227 deletions(-) diff --git a/apier/v1/dispatcher.go b/apier/v1/dispatcher.go index 375c1edde..f2b171e06 100755 --- a/apier/v1/dispatcher.go +++ b/apier/v1/dispatcher.go @@ -139,7 +139,6 @@ func (dSts *DispatcherStatSv1) ProcessEvent(args *dispatchers.ArgsStatProcessEve return dSts.dS.StatSv1ProcessEvent(args, reply) } -/* func NewDispatcherResourceSv1(dps *dispatchers.DispatcherService) *DispatcherResourceSv1 { return &DispatcherResourceSv1{dRs: dps} } @@ -150,8 +149,8 @@ type DispatcherResourceSv1 struct { } // Ping implements ResourceSv1Ping -func (dRs *DispatcherResourceSv1) Ping(ign string, reply *string) error { - return dRs.dRs.ResourceSv1Ping(ign, reply) +func (dRs *DispatcherResourceSv1) Ping(args *dispatchers.CGREvWithApiKey, reply *string) error { + return dRs.dRs.ResourceSv1Ping(args, reply) } // GetResourcesForEvent implements ResourceSv1GetResourcesForEvent @@ -159,7 +158,7 @@ func (dRs *DispatcherResourceSv1) GetResourcesForEvent(args *dispatchers.ArgsV1R reply *engine.Resources) error { return dRs.dRs.ResourceSv1GetResourcesForEvent(args, reply) } -*/ + func NewDispatcherSupplierSv1(dps *dispatchers.DispatcherService) *DispatcherSupplierSv1 { return &DispatcherSupplierSv1{dSup: dps} } diff --git a/apier/v1/resourcesv1.go b/apier/v1/resourcesv1.go index caefac58b..70c88879b 100644 --- a/apier/v1/resourcesv1.go +++ b/apier/v1/resourcesv1.go @@ -121,7 +121,7 @@ func (apierV1 *ApierV1) RemoveResourceProfile(arg utils.TenantID, reply *string) return nil } -func (rsv1 *ResourceSv1) Ping(ign string, reply *string) error { +func (rsv1 *ResourceSv1) Ping(ign *utils.CGREvent, reply *string) error { *reply = utils.Pong return nil } diff --git a/apier/v1/resourcesv1_it_test.go b/apier/v1/resourcesv1_it_test.go index 82eb9667e..db5b5f298 100644 --- a/apier/v1/resourcesv1_it_test.go +++ b/apier/v1/resourcesv1_it_test.go @@ -679,7 +679,7 @@ func testV1RsGetResourceProfileAfterDelete(t *testing.T) { func testV1RsResourcePing(t *testing.T) { var resp string - if err := rlsV1Rpc.Call(utils.ResourceSv1Ping, "", &resp); err != nil { + if err := rlsV1Rpc.Call(utils.ResourceSv1Ping, &utils.CGREvent{}, &resp); err != nil { t.Error(err) } else if resp != utils.Pong { t.Error("Unexpected reply returned", resp) diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index 59df56228..7b1b00dc4 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -1029,12 +1029,10 @@ func startDispatcherService(internalDispatcherSChan chan *dispatchers.Dispatcher server.RpcRegisterName(utils.StatSv1, v1.NewDispatcherStatSv1(dspS)) - /* - if !cfg.ResourceSCfg().Enabled && len(cfg.DispatcherSCfg().ResSConns) != 0 { - server.RpcRegisterName(utils.ResourceSv1, - v1.NewDispatcherResourceSv1(dspS)) - } - */ + + server.RpcRegisterName(utils.ResourceSv1, + v1.NewDispatcherResourceSv1(dspS)) + server.RpcRegisterName(utils.SupplierSv1, v1.NewDispatcherSupplierSv1(dspS)) diff --git a/data/conf/samples/dispatchers/all/cgrates.json b/data/conf/samples/dispatchers/all/cgrates.json index 7150d04c9..bf8205496 100644 --- a/data/conf/samples/dispatchers/all/cgrates.json +++ b/data/conf/samples/dispatchers/all/cgrates.json @@ -50,10 +50,10 @@ "stats": { "enabled": true, - "store_interval": "1s", - "thresholds_conns": [ - {"address": "*internal"} - ], +}, + +"resources": { + "enabled": true, }, } diff --git a/data/conf/samples/dispatchers/all2/cgrates.json b/data/conf/samples/dispatchers/all2/cgrates.json index 1b45f6d76..9897e6c31 100644 --- a/data/conf/samples/dispatchers/all2/cgrates.json +++ b/data/conf/samples/dispatchers/all2/cgrates.json @@ -50,10 +50,10 @@ "stats": { "enabled": true, - "store_interval": "1s", - "thresholds_conns": [ - {"address": "*internal"} - ], +}, + +"resources": { + "enabled": true, }, } diff --git a/data/tariffplans/dispatchers/Attributes.csv b/data/tariffplans/dispatchers/Attributes.csv index 66e5b50d7..d74d58242 100644 --- a/data/tariffplans/dispatchers/Attributes.csv +++ b/data/tariffplans/dispatchers/Attributes.csv @@ -6,5 +6,5 @@ cgrates.org,ATTR_API_CHRG_AUTH,*auth,*string:APIKey:chrg12345,,APIMethods,*any,C cgrates.org,ATTR_API_THR_AUTH,*auth,*string:APIKey:thr12345,,APIMethods,*any,ThresholdSv1.Ping&ThresholdSv1.GetThresholdsForEvent&ThresholdSv1.ProcessEvent,true,false,20 cgrates.org,ATTR_API_SUP_AUTH,*auth,*string:APIKey:sup12345,,APIMethods,*any,SupplierSv1.Ping&SupplierSv1.GetSuppliers,true,false,20 cgrates.org,ATTR_API_STAT_AUTH,*auth,*string:APIKey:stat12345,,APIMethods,*any,StatSv1.Ping&StatSv1.GetStatQueuesForEvent&StatSv1.GetQueueStringMetrics&StatSv1.ProcessEvent,true,false,20 - +cgrates.org,ATTR_API_RES_AUTH,*auth,*string:APIKey:res12345,,APIMethods,*any,ResourceSv1.Ping&ResourceSv1.GetResourcesForEvent,true,false,20 diff --git a/dispatchers/resources.go b/dispatchers/resources.go index a7cc39657..b3446fb4f 100755 --- a/dispatchers/resources.go +++ b/dispatchers/resources.go @@ -18,29 +18,34 @@ along with this program. If not, see package dispatchers -/* import ( "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" ) -func (dS *DispatcherService) ResourceSv1Ping(ign string, rpl *string) (err error) { - if dS.resS == nil { - return utils.NewErrNotConnected(utils.ResourceS) +func (dS *DispatcherService) ResourceSv1Ping(args *CGREvWithApiKey, rpl *string) (err error) { + if dS.attrS != nil { + if err = dS.authorize(utils.ResourceSv1Ping, + args.CGREvent.Tenant, + args.APIKey, args.CGREvent.Time); err != nil { + return + } } - return dS.resS.Call(utils.ResourceSv1Ping, ign, rpl) + return dS.Dispatch(&args.CGREvent, utils.MetaResources, + utils.ResourceSv1Ping, args.CGREvent, rpl) } func (dS *DispatcherService) ResourceSv1GetResourcesForEvent(args *ArgsV1ResUsageWithApiKey, reply *engine.Resources) (err error) { - if dS.resS == nil { - return utils.NewErrNotConnected(utils.ResourceS) + if dS.attrS != nil { + if err = dS.authorize(utils.ResourceSv1GetResourcesForEvent, + args.ArgRSv1ResourceUsage.CGREvent.Tenant, + args.APIKey, args.ArgRSv1ResourceUsage.CGREvent.Time); err != nil { + return + } + } - if err = dS.authorize(utils.ResourceSv1GetResourcesForEvent, args.ArgRSv1ResourceUsage.CGREvent.Tenant, - args.APIKey, args.ArgRSv1ResourceUsage.CGREvent.Time); err != nil { - return - } - return dS.resS.Call(utils.ResourceSv1GetResourcesForEvent, args.ArgRSv1ResourceUsage, reply) + return dS.Dispatch(&args.CGREvent, utils.MetaResources, + utils.ResourceSv1GetResourcesForEvent, args.ArgRSv1ResourceUsage, reply) } -*/ diff --git a/dispatchers/resources_it_test.go b/dispatchers/resources_it_test.go index d29ac9588..774751354 100755 --- a/dispatchers/resources_it_test.go +++ b/dispatchers/resources_it_test.go @@ -21,165 +21,93 @@ 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 ( - dspResCfgPath string - dspResCfg *config.CGRConfig - dspResRPC *rpc.Client - instResCfgPath string - instResCfg *config.CGRConfig - instResRPC *rpc.Client -) - var sTestsDspRes = []func(t *testing.T){ - testDspResInitCfg, - testDspResInitDataDb, - testDspResResetStorDb, - testDspResStartEngine, - testDspResRPCConn, + testDspResPingFailover, + testDspResPing, - testDspResLoadData, - testDspResAddAttributesWithPermision, testDspResTestAuthKey, - testDspResAddAttributesWithPermision2, testDspResTestAuthKey2, - testDspResKillEngine, } //Test start here func TestDspResourceS(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 sTestsDspRes { t.Run("", stest) } + attrEngine.stopEngine(t) + dispEngine.stopEngine(t) + allEngine.stopEngine(t) + allEngine2.stopEngine(t) } -func testDspResInitCfg(t *testing.T) { - var err error - dspResCfgPath = path.Join(dspDataDir, "conf", "samples", "dispatcher") - dspResCfg, err = config.NewCGRConfigFromFolder(dspResCfgPath) - if err != nil { +func testDspResPingFailover(t *testing.T) { + var reply string + if err := allEngine.RCP.Call(utils.ResourceSv1Ping, &utils.CGREvent{}, &reply); err != nil { t.Error(err) + } else if reply != utils.Pong { + t.Errorf("Received: %s", reply) } - dspResCfg.DataFolderPath = dspDataDir // Share DataFolderPath through config towards StoreDb for Flush() - config.SetCgrConfig(dspResCfg) - instResCfgPath = path.Join(dspDataDir, "conf", "samples", "tutmysql") - instResCfg, err = config.NewCGRConfigFromFolder(instResCfgPath) - if err != nil { + ev := CGREvWithApiKey{ + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + }, + APIKey: "res12345", + } + if err := dispEngine.RCP.Call(utils.ResourceSv1Ping, &ev, &reply); err != nil { t.Error(err) + } else if reply != utils.Pong { + t.Errorf("Received: %s", reply) } - instResCfg.DataFolderPath = dspDataDir // Share DataFolderPath through config towards StoreDb for Flush() - config.SetCgrConfig(instResCfg) -} - -func testDspResInitDataDb(t *testing.T) { - if err := engine.InitDataDb(instResCfg); err != nil { - t.Fatal(err) + allEngine.stopEngine(t) + if err := dispEngine.RCP.Call(utils.ResourceSv1Ping, &ev, &reply); err != nil { + t.Error(err) + } else if reply != utils.Pong { + t.Errorf("Received: %s", reply) } -} - -// Wipe out the cdr database -func testDspResResetStorDb(t *testing.T) { - if err := engine.InitStorDb(instResCfg); err != nil { - t.Fatal(err) + allEngine2.stopEngine(t) + if err := dispEngine.RCP.Call(utils.ResourceSv1Ping, &ev, &reply); err == nil { + t.Errorf("Expected error but recived %v and reply %v\n", err, reply) } -} - -// Start CGR Engine -func testDspResStartEngine(t *testing.T) { - if _, err := engine.StartEngine(instResCfgPath, dspDelay); err != nil { - t.Fatal(err) - } - if _, err := engine.StartEngine(dspResCfgPath, dspDelay); err != nil { - t.Fatal(err) - } -} - -// Connect rpc client to rater -func testDspResRPCConn(t *testing.T) { - var err error - instResRPC, err = jsonrpc.Dial("tcp", instResCfg.ListenCfg().RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed - if err != nil { - t.Fatal(err) - } - dspResRPC, err = jsonrpc.Dial("tcp", dspResCfg.ListenCfg().RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed - if err != nil { - t.Fatal(err) - } - + allEngine.startEngine(t) + allEngine2.startEngine(t) } func testDspResPing(t *testing.T) { var reply string - if err := instResRPC.Call(utils.ResourceSv1Ping, "", &reply); err != nil { + if err := allEngine.RCP.Call(utils.ResourceSv1Ping, &utils.CGREvent{}, &reply); err != nil { t.Error(err) } else if reply != utils.Pong { t.Errorf("Received: %s", reply) } - if err := dspResRPC.Call(utils.ResourceSv1Ping, "", &reply); err != nil { + if err := dispEngine.RCP.Call(utils.ResourceSv1Ping, &CGREvWithApiKey{ + CGREvent: utils.CGREvent{ + Tenant: "cgrates.org", + }, + APIKey: "res12345", + }, &reply); err != nil { t.Error(err) } else if reply != utils.Pong { t.Errorf("Received: %s", reply) } } -func testDspResLoadData(t *testing.T) { - var reply string - attrs := &utils.AttrLoadTpFromFolder{ - FolderPath: path.Join(dspDataDir, "tariffplans", "tutorial")} - if err := instResRPC.Call("ApierV1.LoadTariffPlanFromFolder", attrs, &reply); err != nil { - t.Error(err) - } - time.Sleep(500 * time.Millisecond) -} - -func testDspResAddAttributesWithPermision(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 := instResRPC.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 := instResRPC.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 testDspResTestAuthKey(t *testing.T) { var rs *engine.Resources args := &ArgsV1ResUsageWithApiKey{ @@ -196,53 +124,16 @@ func testDspResTestAuthKey(t *testing.T) { }, } - if err := dspResRPC.Call(utils.ResourceSv1GetResourcesForEvent, + if err := dispEngine.RCP.Call(utils.ResourceSv1GetResourcesForEvent, args, &rs); err == nil || err.Error() != utils.ErrUnauthorizedApi.Error() { t.Error(err) } } -func testDspResAddAttributesWithPermision2(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&ResourceSv1.GetResourcesForEvent", true, utils.INFIELD_SEP), - Append: true, - }, - }, - Weight: 20, - } - var result string - if err := instResRPC.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 := instResRPC.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 testDspResTestAuthKey2(t *testing.T) { var rs *engine.Resources args := &ArgsV1ResUsageWithApiKey{ - APIKey: "12345", + APIKey: "res12345", ArgRSv1ResourceUsage: utils.ArgRSv1ResourceUsage{ CGREvent: utils.CGREvent{ Tenant: "cgrates.org", @@ -262,19 +153,10 @@ func testDspResTestAuthKey2(t *testing.T) { }, } - if err := dspResRPC.Call(utils.ResourceSv1GetResourcesForEvent, + if err := dispEngine.RCP.Call(utils.ResourceSv1GetResourcesForEvent, args, &rs); err != nil { t.Error(err) } else if !reflect.DeepEqual(eRs, rs) { t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(eRs), utils.ToJSON(rs)) } } - -func testDspResKillEngine(t *testing.T) { - if err := engine.KillEngine(dspDelay); err != nil { - t.Error(err) - } - if err := engine.KillEngine(dspDelay); err != nil { - t.Error(err) - } -} diff --git a/dispatchers/sessions_it_test.go b/dispatchers/sessions_it_test.go index 2d5cde050..0b484a772 100755 --- a/dispatchers/sessions_it_test.go +++ b/dispatchers/sessions_it_test.go @@ -44,22 +44,22 @@ var ( ) var sTestsDspSession = []func(t *testing.T){ - testDspSessionInitCfg, - testDspSessionInitDataDb, - testDspSessionResetStorDb, - testDspSessionStartEngine, - testDspSessionRPCConn, - testDspSessionPing, - testDspSessionLoadData, - testDspSessionAddAttributesWithPermision, - testDspSessionTestAuthKey, - testDspSessionAddAttributesWithPermision2, - testDspSessionAuthorize, - testDspSessionInit, - testDspSessionUpdate, - testDspSessionTerminate, - testDspSessionProcessCDR, - testDspSessionKillEngine, + // testDspSessionInitCfg, + // testDspSessionInitDataDb, + // testDspSessionResetStorDb, + // testDspSessionStartEngine, + // testDspSessionRPCConn, + // testDspSessionPing, + // testDspSessionLoadData, + // testDspSessionAddAttributesWithPermision, + // testDspSessionTestAuthKey, + // testDspSessionAddAttributesWithPermision2, + // testDspSessionAuthorize, + // testDspSessionInit, + // testDspSessionUpdate, + // testDspSessionTerminate, + // testDspSessionProcessCDR, + // testDspSessionKillEngine, } //Test start here diff --git a/dispatchers/stats_it_test.go b/dispatchers/stats_it_test.go index 6aa5b80e3..d1cd21198 100755 --- a/dispatchers/stats_it_test.go +++ b/dispatchers/stats_it_test.go @@ -21,26 +21,15 @@ along with this program. If not, see package dispatchers import ( - "net/rpc" "path" "reflect" "testing" "time" - "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" ) -var ( - dspStsCfgPath string - dspStsCfg *config.CGRConfig - dspStsRPC *rpc.Client - instStsCfgPath string - instStsCfg *config.CGRConfig - instStsRPC *rpc.Client -) - var sTestsDspSts = []func(t *testing.T){ testDspStsPingFailover, testDspStsGetStatFailover, From aac1435234d997c3832a603abcc41fa0d34270b1 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Tue, 12 Feb 2019 16:10:44 +0200 Subject: [PATCH 6/6] Added some fixes for ditpatcher --- apier/v1/analyzer.go | 2 +- apier/v1/caches.go | 2 +- apier/v1/loaders.go | 2 +- apier/v1/stats.go | 4 +-- apier/v1/stats_it_test.go | 2 +- apier/v1/thresholds.go | 4 +-- apier/v1/thresholds_it_test.go | 4 +-- dispatchers/attributes.go | 6 ++-- dispatchers/attributes_it_test.go | 54 +++++++++++++++---------------- dispatchers/chargers.go | 6 ++-- dispatchers/dispatchers.go | 14 ++++---- dispatchers/resources.go | 4 +-- dispatchers/stats.go | 8 ++--- dispatchers/suppliers.go | 4 +-- dispatchers/suppliers_it_test.go | 22 +++++++++++-- dispatchers/thresholds.go | 6 ++-- dispatchers/utils.go | 33 +++++++++++-------- utils/cgrevent.go | 1 - utils/coreutils.go | 4 +++ 19 files changed, 104 insertions(+), 78 deletions(-) diff --git a/apier/v1/analyzer.go b/apier/v1/analyzer.go index 05fae1b90..49c85c2ec 100755 --- a/apier/v1/analyzer.go +++ b/apier/v1/analyzer.go @@ -40,7 +40,7 @@ func (aSv1 *AnalyzerSv1) Call(serviceMethod string, } // Ping return pong if the service is active -func (alSv1 *AnalyzerSv1) Ping(ign string, reply *string) error { +func (alSv1 *AnalyzerSv1) Ping(ign *utils.CGREvent, reply *string) error { *reply = utils.Pong return nil } diff --git a/apier/v1/caches.go b/apier/v1/caches.go index 9c7cb6922..c5fa733cf 100644 --- a/apier/v1/caches.go +++ b/apier/v1/caches.go @@ -94,7 +94,7 @@ func (chSv1 *CacheSv1) RemoveGroup(args *engine.ArgsGetGroup, return chSv1.cacheS.V1RemoveGroup(args, rply) } -func (chSv1 *CacheSv1) Ping(ign string, reply *string) error { +func (chSv1 *CacheSv1) Ping(ign *utils.CGREvent, reply *string) error { *reply = utils.Pong return nil } diff --git a/apier/v1/loaders.go b/apier/v1/loaders.go index 5ac1d85ea..dda359763 100644 --- a/apier/v1/loaders.go +++ b/apier/v1/loaders.go @@ -43,7 +43,7 @@ func (ldrSv1 *LoaderSv1) Load(args *loaders.ArgsProcessFolder, return ldrSv1.ldrS.V1Load(args, rply) } -func (rsv1 *LoaderSv1) Ping(ign string, reply *string) error { +func (rsv1 *LoaderSv1) Ping(ign *utils.CGREvent, reply *string) error { *reply = utils.Pong return nil } diff --git a/apier/v1/stats.go b/apier/v1/stats.go index adf0de53e..dfb9725b8 100644 --- a/apier/v1/stats.go +++ b/apier/v1/stats.go @@ -106,8 +106,8 @@ func (stsv1 *StatSv1) Call(serviceMethod string, args interface{}, reply interfa } // GetQueueIDs returns list of queueIDs registered for a tenant -func (stsv1 *StatSv1) GetQueueIDs(tenant string, qIDs *[]string) error { - return stsv1.sS.V1GetQueueIDs(tenant, qIDs) +func (stsv1 *StatSv1) GetQueueIDs(tenant *utils.TenantArg, qIDs *[]string) error { + return stsv1.sS.V1GetQueueIDs(tenant.Tenant, qIDs) } // ProcessEvent returns processes a new Event diff --git a/apier/v1/stats_it_test.go b/apier/v1/stats_it_test.go index 8890ecbd4..c5b79f245 100644 --- a/apier/v1/stats_it_test.go +++ b/apier/v1/stats_it_test.go @@ -142,7 +142,7 @@ func testV1STSFromFolder(t *testing.T) { func testV1STSGetStats(t *testing.T) { var reply []string expectedIDs := []string{"Stats1"} - if err := stsV1Rpc.Call(utils.StatSv1GetQueueIDs, "cgrates.org", &reply); err != nil { + if err := stsV1Rpc.Call(utils.StatSv1GetQueueIDs, &utils.TenantArg{"cgrates.org"}, &reply); err != nil { t.Error(err) } else if !reflect.DeepEqual(expectedIDs, reply) { t.Errorf("expecting: %+v, received reply: %s", expectedIDs, reply) diff --git a/apier/v1/thresholds.go b/apier/v1/thresholds.go index ca5c4c79f..3ff740f06 100644 --- a/apier/v1/thresholds.go +++ b/apier/v1/thresholds.go @@ -39,8 +39,8 @@ func (tSv1 *ThresholdSv1) Call(serviceMethod string, args interface{}, reply int } // GetThresholdIDs returns list of threshold IDs registered for a tenant -func (tSv1 *ThresholdSv1) GetThresholdIDs(tenant string, tIDs *[]string) error { - return tSv1.tS.V1GetThresholdIDs(tenant, tIDs) +func (tSv1 *ThresholdSv1) GetThresholdIDs(tenant *utils.TenantArg, tIDs *[]string) error { + return tSv1.tS.V1GetThresholdIDs(tenant.Tenant, tIDs) } // GetThresholdsForEvent returns a list of thresholds matching an event diff --git a/apier/v1/thresholds_it_test.go b/apier/v1/thresholds_it_test.go index d56998254..47b48fc5f 100644 --- a/apier/v1/thresholds_it_test.go +++ b/apier/v1/thresholds_it_test.go @@ -229,7 +229,7 @@ func testV1TSFromFolder(t *testing.T) { func testV1TSGetThresholds(t *testing.T) { var tIDs []string expectedIDs := []string{"THD_RES_1", "THD_STATS_2", "THD_STATS_1", "THD_ACNT_BALANCE_1", "THD_ACNT_EXPIRED", "THD_STATS_3", "THD_CDRS_1"} - if err := tSv1Rpc.Call(utils.ThresholdSv1GetThresholdIDs, "cgrates.org", &tIDs); err != nil { + if err := tSv1Rpc.Call(utils.ThresholdSv1GetThresholdIDs, &utils.TenantArg{Tenant: "cgrates.org"}, &tIDs); err != nil { t.Error(err) } else if len(expectedIDs) != len(tIDs) { t.Errorf("expecting: %+v, received reply: %s", expectedIDs, tIDs) @@ -303,7 +303,7 @@ func testV1TSProcessEvent(t *testing.T) { func testV1TSGetThresholdsAfterProcess(t *testing.T) { var tIDs []string expectedIDs := []string{"THD_RES_1", "THD_STATS_2", "THD_STATS_1", "THD_ACNT_BALANCE_1", "THD_ACNT_EXPIRED"} - if err := tSv1Rpc.Call(utils.ThresholdSv1GetThresholdIDs, "cgrates.org", &tIDs); err != nil { + if err := tSv1Rpc.Call(utils.ThresholdSv1GetThresholdIDs, &utils.TenantArg{Tenant: "cgrates.org"}, &tIDs); err != nil { t.Error(err) } else if len(expectedIDs) != len(tIDs) { // THD_STATS_3 is not reccurent, so it was removed t.Errorf("expecting: %+v, received reply: %s", expectedIDs, tIDs) diff --git a/dispatchers/attributes.go b/dispatchers/attributes.go index 9d7967f16..d54260bc9 100755 --- a/dispatchers/attributes.go +++ b/dispatchers/attributes.go @@ -33,7 +33,7 @@ func (dS *DispatcherService) AttributeSv1Ping(args *CGREvWithApiKey, return } } - return dS.Dispatch(&args.CGREvent, utils.MetaAttributes, + return dS.Dispatch(&args.CGREvent, utils.MetaAttributes, args.RouteID, utils.AttributeSv1Ping, args.CGREvent, reply) } @@ -47,7 +47,7 @@ func (dS *DispatcherService) AttributeSv1GetAttributeForEvent(args *ArgsAttrProc return } } - return dS.Dispatch(&args.CGREvent, utils.MetaAttributes, + return dS.Dispatch(&args.CGREvent, utils.MetaAttributes, args.RouteID, utils.AttributeSv1GetAttributeForEvent, args.AttrArgsProcessEvent, reply) } @@ -61,6 +61,6 @@ func (dS *DispatcherService) AttributeSv1ProcessEvent(args *ArgsAttrProcessEvent } } - return dS.Dispatch(&args.CGREvent, utils.MetaAttributes, + return dS.Dispatch(&args.CGREvent, utils.MetaAttributes, args.RouteID, utils.AttributeSv1ProcessEvent, args.AttrArgsProcessEvent, reply) } diff --git a/dispatchers/attributes_it_test.go b/dispatchers/attributes_it_test.go index f5317b2a5..4b76164da 100755 --- a/dispatchers/attributes_it_test.go +++ b/dispatchers/attributes_it_test.go @@ -35,15 +35,34 @@ import ( "github.com/cgrates/cgrates/utils" ) -type testDipatcer struct { +var ( + attrEngine *testDispatcher + dispEngine *testDispatcher + allEngine *testDispatcher + allEngine2 *testDispatcher +) + +var sTestsDspAttr = []func(t *testing.T){ + testDspAttrPingFailover, + testDspAttrGetAttrFailover, + + testDspAttrPing, + testDspAttrTestMissingApiKey, + testDspAttrTestUnknownApiKey, + testDspAttrTestAuthKey, + testDspAttrTestAuthKey2, + testDspAttrTestAuthKey3, +} + +type testDispatcher struct { CfgParh string Cfg *config.CGRConfig RCP *rpc.Client cmd *exec.Cmd } -func newTestEngine(t *testing.T, cfgPath string, initDataDB, intitStoreDB bool) (d *testDipatcer) { - d = new(testDipatcer) +func newTestEngine(t *testing.T, cfgPath string, initDataDB, intitStoreDB bool) (d *testDispatcher) { + d = new(testDispatcher) d.CfgParh = cfgPath var err error d.Cfg, err = config.NewCGRConfigFromFolder(d.CfgParh) @@ -63,7 +82,7 @@ func newTestEngine(t *testing.T, cfgPath string, initDataDB, intitStoreDB bool) return d } -func (d *testDipatcer) startEngine(t *testing.T) { +func (d *testDispatcher) startEngine(t *testing.T) { var err error if d.cmd, err = engine.StartEngine(d.CfgParh, dspDelay); err != nil { t.Fatalf("Error at engine start:%v\n", err) @@ -74,7 +93,7 @@ func (d *testDipatcer) startEngine(t *testing.T) { } } -func (d *testDipatcer) stopEngine(t *testing.T) { +func (d *testDispatcher) stopEngine(t *testing.T) { pid := strconv.Itoa(d.cmd.Process.Pid) if err := exec.Command("kill", "-9", pid).Run(); err != nil { t.Fatalf("Error at stop engine:%v\n", err) @@ -84,19 +103,19 @@ func (d *testDipatcer) stopEngine(t *testing.T) { // } } -func (d *testDipatcer) initDataDb(t *testing.T) { +func (d *testDispatcher) initDataDb(t *testing.T) { if err := engine.InitDataDb(d.Cfg); err != nil { t.Fatalf("Error at DataDB init:%v\n", err) } } // Wipe out the cdr database -func (d *testDipatcer) resetStorDb(t *testing.T) { +func (d *testDispatcher) resetStorDb(t *testing.T) { if err := engine.InitStorDb(d.Cfg); err != nil { t.Fatalf("Error at DataDB init:%v\n", err) } } -func (d *testDipatcer) loadData(t *testing.T, path string) { +func (d *testDispatcher) loadData(t *testing.T, path string) { var reply string attrs := &utils.AttrLoadTpFromFolder{FolderPath: path} if err := d.RCP.Call("ApierV1.LoadTariffPlanFromFolder", attrs, &reply); err != nil { @@ -104,25 +123,6 @@ func (d *testDipatcer) loadData(t *testing.T, path string) { } } -var ( - attrEngine *testDipatcer - dispEngine *testDipatcer - allEngine *testDipatcer - allEngine2 *testDipatcer -) - -var sTestsDspAttr = []func(t *testing.T){ - testDspAttrPingFailover, - testDspAttrGetAttrFailover, - - testDspAttrPing, - testDspAttrTestMissingApiKey, - testDspAttrTestUnknownApiKey, - testDspAttrTestAuthKey, - testDspAttrTestAuthKey2, - testDspAttrTestAuthKey3, -} - //Test start here func TestDspAttributeS(t *testing.T) { allEngine = newTestEngine(t, path.Join(dspDataDir, "conf", "samples", "dispatchers", "all"), true, true) diff --git a/dispatchers/chargers.go b/dispatchers/chargers.go index 445781413..70a17f51f 100755 --- a/dispatchers/chargers.go +++ b/dispatchers/chargers.go @@ -31,7 +31,7 @@ func (dS *DispatcherService) ChargerSv1Ping(args *CGREvWithApiKey, reply *string return } } - return dS.Dispatch(&args.CGREvent, utils.MetaChargers, + return dS.Dispatch(&args.CGREvent, utils.MetaChargers, args.RouteID, utils.ChargerSv1Ping, args.CGREvent, reply) } @@ -44,7 +44,7 @@ func (dS *DispatcherService) ChargerSv1GetChargersForEvent(args *CGREvWithApiKey return } } - return dS.Dispatch(&args.CGREvent, utils.MetaChargers, + return dS.Dispatch(&args.CGREvent, utils.MetaChargers, args.RouteID, utils.ChargerSv1GetChargersForEvent, args.CGREvent, reply) } @@ -58,6 +58,6 @@ func (dS *DispatcherService) ChargerSv1ProcessEvent(args *CGREvWithApiKey, } } - return dS.Dispatch(&args.CGREvent, utils.MetaChargers, + return dS.Dispatch(&args.CGREvent, utils.MetaChargers, args.RouteID, utils.ChargerSv1ProcessEvent, args.CGREvent, reply) } diff --git a/dispatchers/dispatchers.go b/dispatchers/dispatchers.go index acc549082..15dc30632 100755 --- a/dispatchers/dispatchers.go +++ b/dispatchers/dispatchers.go @@ -138,18 +138,18 @@ func (dS *DispatcherService) dispatcherForEvent(ev *utils.CGREvent, } // Dispatch is the method forwarding the request towards the right -func (dS *DispatcherService) Dispatch(ev *utils.CGREvent, subsys string, +func (dS *DispatcherService) Dispatch(ev *utils.CGREvent, subsys string, RouteID *string, serviceMethod string, args interface{}, reply interface{}) (err error) { d, errDsp := dS.dispatcherForEvent(ev, subsys) if errDsp != nil { return utils.NewErrDispatcherS(errDsp) } var connID string - if ev.RouteID != nil && - *ev.RouteID != "" { + if RouteID != nil && + *RouteID != "" { // use previously discovered route if x, ok := engine.Cache.Get(utils.CacheDispatcherRoutes, - *ev.RouteID); ok && x != nil { + *RouteID); ok && x != nil { connID = x.(string) if err = dS.conns[connID].Call(serviceMethod, args, reply); !utils.IsNetworkError(err) { return @@ -167,9 +167,9 @@ func (dS *DispatcherService) Dispatch(ev *utils.CGREvent, subsys string, if err = conn.Call(serviceMethod, args, reply); utils.IsNetworkError(err) { continue } - if ev.RouteID != nil && - *ev.RouteID != "" { // cache the discovered route - engine.Cache.Set(utils.CacheDispatcherRoutes, *ev.RouteID, connID, + if RouteID != nil && + *RouteID != "" { // cache the discovered route + engine.Cache.Set(utils.CacheDispatcherRoutes, *RouteID, connID, nil, true, utils.EmptyString) } break diff --git a/dispatchers/resources.go b/dispatchers/resources.go index b3446fb4f..276300241 100755 --- a/dispatchers/resources.go +++ b/dispatchers/resources.go @@ -31,7 +31,7 @@ func (dS *DispatcherService) ResourceSv1Ping(args *CGREvWithApiKey, rpl *string) return } } - return dS.Dispatch(&args.CGREvent, utils.MetaResources, + return dS.Dispatch(&args.CGREvent, utils.MetaResources, args.RouteID, utils.ResourceSv1Ping, args.CGREvent, rpl) } @@ -45,7 +45,7 @@ func (dS *DispatcherService) ResourceSv1GetResourcesForEvent(args *ArgsV1ResUsag } } - return dS.Dispatch(&args.CGREvent, utils.MetaResources, + return dS.Dispatch(&args.CGREvent, utils.MetaResources, args.RouteID, utils.ResourceSv1GetResourcesForEvent, args.ArgRSv1ResourceUsage, reply) } diff --git a/dispatchers/stats.go b/dispatchers/stats.go index a5b0184b2..f40fcbb84 100755 --- a/dispatchers/stats.go +++ b/dispatchers/stats.go @@ -32,7 +32,7 @@ func (dS *DispatcherService) StatSv1Ping(args *CGREvWithApiKey, reply *string) ( return } } - return dS.Dispatch(&args.CGREvent, utils.MetaStats, + return dS.Dispatch(&args.CGREvent, utils.MetaStats, args.RouteID, utils.StatSv1Ping, args.CGREvent, reply) } @@ -45,7 +45,7 @@ func (dS *DispatcherService) StatSv1GetStatQueuesForEvent(args *ArgsStatProcessE return } } - return dS.Dispatch(&args.CGREvent, utils.MetaStats, + return dS.Dispatch(&args.CGREvent, utils.MetaStats, args.RouteID, utils.StatSv1GetStatQueuesForEvent, args.StatsArgsProcessEvent, reply) } @@ -61,7 +61,7 @@ func (dS *DispatcherService) StatSv1GetQueueStringMetrics(args *TntIDWithApiKey, return dS.Dispatch(&utils.CGREvent{ Tenant: args.Tenant, ID: args.ID, - }, utils.MetaStats, utils.StatSv1GetQueueStringMetrics, + }, utils.MetaStats, args.RouteID, utils.StatSv1GetQueueStringMetrics, args.TenantID, reply) } @@ -74,6 +74,6 @@ func (dS *DispatcherService) StatSv1ProcessEvent(args *ArgsStatProcessEventWithA return } } - return dS.Dispatch(&args.CGREvent, utils.MetaStats, + return dS.Dispatch(&args.CGREvent, utils.MetaStats, args.RouteID, utils.StatSv1ProcessEvent, args.StatsArgsProcessEvent, reply) } diff --git a/dispatchers/suppliers.go b/dispatchers/suppliers.go index 0b0151f0f..60f001463 100755 --- a/dispatchers/suppliers.go +++ b/dispatchers/suppliers.go @@ -31,7 +31,7 @@ func (dS *DispatcherService) SupplierSv1Ping(args *CGREvWithApiKey, reply *strin return } } - return dS.Dispatch(&args.CGREvent, utils.MetaSuppliers, + return dS.Dispatch(&args.CGREvent, utils.MetaSuppliers, args.RouteID, utils.SupplierSv1Ping, args.CGREvent, reply) } @@ -44,6 +44,6 @@ func (dS *DispatcherService) SupplierSv1GetSuppliers(args *ArgsGetSuppliersWithA return } } - return dS.Dispatch(&args.CGREvent, utils.MetaSuppliers, + return dS.Dispatch(&args.CGREvent, utils.MetaSuppliers, args.RouteID, utils.SupplierSv1GetSuppliers, args.ArgsGetSuppliers, reply) } diff --git a/dispatchers/suppliers_it_test.go b/dispatchers/suppliers_it_test.go index 474938e0a..667e25c36 100755 --- a/dispatchers/suppliers_it_test.go +++ b/dispatchers/suppliers_it_test.go @@ -31,6 +31,9 @@ import ( ) var sTestsDspSup = []func(t *testing.T){ + testDspSupPingFailover, + testDspSupGetSupFailover, + testDspSupPing, testDspSupTestAuthKey, testDspSupTestAuthKey2, @@ -108,6 +111,19 @@ func testDspSupPingFailover(t *testing.T) { func testDspSupGetSupFailover(t *testing.T) { var rpl *engine.SortedSuppliers + eRpl1 := &engine.SortedSuppliers{ + ProfileID: "SPL_WEIGHT_2", + Sorting: utils.MetaWeight, + SortedSuppliers: []*engine.SortedSupplier{ + { + SupplierID: "supplier1", + SupplierParameters: "", + SortingData: map[string]interface{}{ + utils.Weight: 10.0, + }, + }, + }, + } eRpl := &engine.SortedSuppliers{ ProfileID: "SPL_ACNT_1002", Sorting: utils.MetaLeastCost, @@ -151,8 +167,10 @@ func testDspSupGetSupFailover(t *testing.T) { }, } if err := dispEngine.RCP.Call(utils.SupplierSv1GetSuppliers, - args, &rpl); err == nil || err.Error() != utils.ErrNotFound.Error() { - t.Errorf("Expected error NOT_FOUND but recived %v and reply %v\n", err, rpl) + args, &rpl); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eRpl1, rpl) { + t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(eRpl1), utils.ToJSON(rpl)) } allEngine2.stopEngine(t) if err := dispEngine.RCP.Call(utils.SupplierSv1GetSuppliers, diff --git a/dispatchers/thresholds.go b/dispatchers/thresholds.go index 075efbafb..629fb227b 100755 --- a/dispatchers/thresholds.go +++ b/dispatchers/thresholds.go @@ -31,7 +31,7 @@ func (dS *DispatcherService) ThresholdSv1Ping(args *CGREvWithApiKey, reply *stri return } } - return dS.Dispatch(&args.CGREvent, utils.MetaThresholds, + return dS.Dispatch(&args.CGREvent, utils.MetaThresholds, args.RouteID, utils.ThresholdSv1Ping, args.CGREvent, reply) } @@ -44,7 +44,7 @@ func (dS *DispatcherService) ThresholdSv1GetThresholdsForEvent(args *ArgsProcess return } } - return dS.Dispatch(&args.CGREvent, utils.MetaThresholds, + return dS.Dispatch(&args.CGREvent, utils.MetaThresholds, args.RouteID, utils.ThresholdSv1GetThresholdsForEvent, args.ArgsProcessEvent, t) } @@ -57,6 +57,6 @@ func (dS *DispatcherService) ThresholdSv1ProcessEvent(args *ArgsProcessEventWith return } } - return dS.Dispatch(&args.CGREvent, utils.MetaThresholds, + return dS.Dispatch(&args.CGREvent, utils.MetaThresholds, args.RouteID, utils.ThresholdSv1ProcessEvent, args.ArgsProcessEvent, tIDs) } diff --git a/dispatchers/utils.go b/dispatchers/utils.go index 3b65195a7..1858fe616 100755 --- a/dispatchers/utils.go +++ b/dispatchers/utils.go @@ -33,68 +33,73 @@ var ( //var used in all tests nowTime = time.Now() ) +type DispatcherResource struct { + APIKey string + RouteID *string // route over previous computed path +} + type CGREvWithApiKey struct { - APIKey string + DispatcherResource utils.CGREvent } type TntIDWithApiKey struct { utils.TenantID - APIKey string + DispatcherResource } type TntWithApiKey struct { - Tenant string - ApiKey string + utils.TenantArg + DispatcherResource } type ArgsV1ResUsageWithApiKey struct { - APIKey string + DispatcherResource utils.ArgRSv1ResourceUsage } type ArgsProcessEventWithApiKey struct { - APIKey string + DispatcherResource engine.ArgsProcessEvent } type ArgsAttrProcessEventWithApiKey struct { - APIKey string + DispatcherResource engine.AttrArgsProcessEvent } type ArgsGetSuppliersWithApiKey struct { - APIKey string + DispatcherResource engine.ArgsGetSuppliers } type ArgsStatProcessEventWithApiKey struct { - APIKey string + DispatcherResource engine.StatsArgsProcessEvent } type AuthorizeArgsWithApiKey struct { - APIKey string + DispatcherResource sessions.V1AuthorizeArgs } type InitArgsWithApiKey struct { - APIKey string + DispatcherResource sessions.V1InitSessionArgs } type ProcessEventWithApiKey struct { - APIKey string + DispatcherResource sessions.V1ProcessEventArgs } type TerminateSessionWithApiKey struct { - APIKey string + DispatcherResource sessions.V1TerminateSessionArgs } type UpdateSessionWithApiKey struct { - APIKey string + DispatcherResource sessions.V1UpdateSessionArgs } diff --git a/utils/cgrevent.go b/utils/cgrevent.go index d4716cac5..1c2aecb12 100644 --- a/utils/cgrevent.go +++ b/utils/cgrevent.go @@ -31,7 +31,6 @@ type CGREvent struct { ID string Context *string // attach the event to a context Time *time.Time // event time - RouteID *string // route over previous computed path Event map[string]interface{} } diff --git a/utils/coreutils.go b/utils/coreutils.go index 4e7a53987..8f6918266 100644 --- a/utils/coreutils.go +++ b/utils/coreutils.go @@ -779,6 +779,10 @@ func NewTenantID(tntID string) *TenantID { return &TenantID{Tenant: tIDSplt[0], ID: tIDSplt[1]} } +type TenantArg struct { + Tenant string +} + type TenantID struct { Tenant string ID string