From 02057eecdb86aac90896941de152d284a2a10069 Mon Sep 17 00:00:00 2001 From: TeoV Date: Fri, 23 Mar 2018 10:19:47 -0400 Subject: [PATCH] Update test calls with subtests --- data/tariffplans/tutorial/AccountActions.csv | 2 + .../tariffplans/tutorial/DestinationRates.csv | 3 + data/tariffplans/tutorial/Destinations.csv | 3 + data/tariffplans/tutorial/Filters.csv | 10 +- data/tariffplans/tutorial/RatingPlans.csv | 7 +- data/tariffplans/tutorial/RatingProfiles.csv | 2 + data/tariffplans/tutorial/Resources.csv | 1 + data/tariffplans/tutorial/Stats.csv | 2 +- data/tariffplans/tutorial/Suppliers.csv | 3 + data/tariffplans/tutorial/Thresholds.csv | 3 + .../cgrates/etc/cgrates/cgrates.json | 13 +- engine/cdrs.go | 4 +- engine/fscdr.go | 3 +- engine/fscdr_test.go | 2 +- general_tests/tut_fs_calls_test.go | 210 ++++++++++++------ 15 files changed, 185 insertions(+), 83 deletions(-) diff --git a/data/tariffplans/tutorial/AccountActions.csv b/data/tariffplans/tutorial/AccountActions.csv index 97bcd5f64..587940d28 100644 --- a/data/tariffplans/tutorial/AccountActions.csv +++ b/data/tariffplans/tutorial/AccountActions.csv @@ -4,3 +4,5 @@ cgrates.org,1002,PACKAGE_10,STANDARD_TRIGGERS,, cgrates.org,1003,PACKAGE_10,STANDARD_TRIGGERS,, cgrates.org,1004,PACKAGE_10,STANDARD_TRIGGERS,, cgrates.org,1007,USE_SHARED_A,STANDARD_TRIGGERS,, +cgrates.org,1011,PACKAGE_1001,STANDARD_TRIGGERS,, +cgrates.org,1012,PACKAGE_10,STANDARD_TRIGGERS,, diff --git a/data/tariffplans/tutorial/DestinationRates.csv b/data/tariffplans/tutorial/DestinationRates.csv index ee3352fc3..b49ffea38 100644 --- a/data/tariffplans/tutorial/DestinationRates.csv +++ b/data/tariffplans/tutorial/DestinationRates.csv @@ -9,3 +9,6 @@ DR_SPECIAL_1002,DST_1002,RT_1CNT,*up,4,0, DR_1007_MAXCOST_DISC,DST_1007,RT_1CNT_PER_SEC,*up,4,0.62,*disconnect DR_1007_MAXCOST_FREE,DST_1007,RT_1CNT_PER_SEC,*up,4,0.62,*free DR_GENERIC,*any,RT_GENERIC_1,*up,4,0, +DR_1005_MAXCOST_DISC,DST_1005,RT_1CNT_PER_SEC,*up,4,0.12,*disconnect +DR_1011_10CNT,DST_1011,RT_10CNT,*up,4,0, +DR_1012_20CNT,DST_1012,RT_20CNT,*up,4,0, diff --git a/data/tariffplans/tutorial/Destinations.csv b/data/tariffplans/tutorial/Destinations.csv index 823d3a72f..f87e000ee 100644 --- a/data/tariffplans/tutorial/Destinations.csv +++ b/data/tariffplans/tutorial/Destinations.csv @@ -6,3 +6,6 @@ DST_FS,10 DST_DE_MOBILE,+49151 DST_DE_MOBILE,+49161 DST_DE_MOBILE,+49171 +DST_1005,1005 +DST_1011,1011 +DST_1012,1012 diff --git a/data/tariffplans/tutorial/Filters.csv b/data/tariffplans/tutorial/Filters.csv index 976c9645f..1129aec52 100644 --- a/data/tariffplans/tutorial/Filters.csv +++ b/data/tariffplans/tutorial/Filters.csv @@ -29,5 +29,11 @@ cgrates.org,FLTR_RES_GR3,*string,Account,3001,2014-07-29T15:00:00Z cgrates.org,FLTR_CDRS,*cdr_stats,,CDRST1:*min_ASR:34;CDRST_1001:*min_ASR:20,2014-07-29T15:00:00Z cgrates.org,FLTR_STS1,*string,Account,1001;1002,2014-07-29T15:00:00Z cgrates.org,FLTR_CDR_UPDATE,*string,EventType,CDR,2014-07-29T15:00:00Z -cgrates.org,FLTR_ACNT_1001_1002,*string,Account,1001;1002,2014-07-29T15:00:00Z -cgrates.org,FLTR_ACNT_1001_1002,*string,RunID,*default, +cgrates.org,FLTR_ACNT_1011_1012,*string,Account,1011;1012,2014-07-29T15:00:00Z +cgrates.org,FLTR_ACNT_1011_1012,*string,RunID,*default, +cgrates.org,FLTR_ACNT_1011,*string,Account,1011,2014-07-29T15:00:00Z +cgrates.org,FLTR_ACNT_1012,*string,Account,1012,2014-07-29T15:00:00Z +cgrates.org,FLTR_2,*string,Account,1011;1012,2014-07-29T15:00:00Z +cgrates.org,FLTR_2,*prefix,Destination,10;20, +cgrates.org,FLTR_2,*rsr,,Subject(~^1.*1$);Destination(1012), + diff --git a/data/tariffplans/tutorial/RatingPlans.csv b/data/tariffplans/tutorial/RatingPlans.csv index 59dc663d7..9559dea4c 100644 --- a/data/tariffplans/tutorial/RatingPlans.csv +++ b/data/tariffplans/tutorial/RatingPlans.csv @@ -4,6 +4,7 @@ RP_RETAIL1,DR_FS_10CNT,OFFPEAK_MORNING,10 RP_RETAIL1,DR_FS_10CNT,OFFPEAK_EVENING,10 RP_RETAIL1,DR_FS_10CNT,OFFPEAK_WEEKEND,10 RP_RETAIL1,DR_1007_MAXCOST_DISC,*any,10 +RP_RETAIL1,DR_1005_MAXCOST_DISC,*any,10 RP_RETAIL2,DR_1002_20CNT,PEAK,10 RP_RETAIL2,DR_1003_20CNT,PEAK,10 RP_RETAIL2,DR_FS_40CNT,PEAK,10 @@ -17,5 +18,9 @@ RP_RETAIL2,DR_FS_10CNT,OFFPEAK_MORNING,10 RP_RETAIL2,DR_FS_10CNT,OFFPEAK_EVENING,10 RP_RETAIL2,DR_FS_10CNT,OFFPEAK_WEEKEND,10 RP_RETAIL2,DR_1007_MAXCOST_FREE,*any,10 +RP_RETAIL2,DR_1005_MAXCOST_DISC,*any,10 RP_SPECIAL_1002,DR_SPECIAL_1002,*any,10 -RP_GENERIC,DR_GENERIC,*any,10 \ No newline at end of file +RP_GENERIC,DR_GENERIC,*any,10 +RP_1011,DR_1012_20CNT,*any,10 +RP_1011,DR_FS_10CNT,*any,10 +RP_1012,DR_1011_10CNT,*any,10 \ No newline at end of file diff --git a/data/tariffplans/tutorial/RatingProfiles.csv b/data/tariffplans/tutorial/RatingProfiles.csv index 2375cff20..b88e9dbb2 100644 --- a/data/tariffplans/tutorial/RatingProfiles.csv +++ b/data/tariffplans/tutorial/RatingProfiles.csv @@ -9,3 +9,5 @@ *out,cgrates.org,lcr_profile2,suppl1,2014-01-14T00:00:00Z,RP_RETAIL2,,STATS_SUPPL1 *out,cgrates.org,lcr_profile2,suppl2,2014-01-14T00:00:00Z,RP_RETAIL1,,STATS_SUPPL2 *out,cgrates.org,lcr_profile2,suppl3,2014-01-14T00:00:00Z,RP_SPECIAL_1002,, +*out,cgrates.org,call,1011,2014-01-14T00:00:00Z,RP_1011,, +*out,cgrates.org,call,1012,2014-01-14T00:00:00Z,RP_1012,, \ No newline at end of file diff --git a/data/tariffplans/tutorial/Resources.csv b/data/tariffplans/tutorial/Resources.csv index 0f47b20a6..9a1a7b72c 100644 --- a/data/tariffplans/tutorial/Resources.csv +++ b/data/tariffplans/tutorial/Resources.csv @@ -2,3 +2,4 @@ cgrates.org,ResGroup1,FLTR_1,2014-07-29T15:00:00Z,1s,7,,false,false,20, cgrates.org,ResGroup2,FLTR_DST_FS,2014-07-29T15:00:00Z,3600s,8,SPECIAL_1002,false,true,10, cgrates.org,ResGroup3,FLTR_RES_GR3,2014-07-29T15:00:00Z,*unlimited,3,,true,false,20, +cgrates.org,ResGroup4,FLTR_2,2014-07-29T15:00:00Z,1s,7,,false,false,20, diff --git a/data/tariffplans/tutorial/Stats.csv b/data/tariffplans/tutorial/Stats.csv index 52fd78228..cc0913e3f 100644 --- a/data/tariffplans/tutorial/Stats.csv +++ b/data/tariffplans/tutorial/Stats.csv @@ -1,4 +1,4 @@ #Tenant[0],Id[1],FilterIDs[2],ActivationInterval[3],QueueLength[4],TTL[5],Metrics[6],MetricParams[7],Blocker[8],Stored[9],Weight[10],MinItems[11],ThresholdIDs[12] cgrates.org,Stats1,FLTR_STS1,2014-07-29T15:00:00Z,100,1s,*asr;*acc;*tcc;*acd;*tcd;*pdd,,true,true,20,2,THRESH1;THRESH2 cgrates.org,Stats1,FLTR_STS1,2014-07-29T15:00:00Z,100,1s,*sum;*average,Value,true,true,20,2,THRESH1;THRESH2 -cgrates.org,Stats2,FLTR_ACNT_1001_1002,2014-07-29T15:00:00Z,100,1s,*tcc;*tcd,,false,true,30,0, +cgrates.org,Stats2,FLTR_ACNT_1011_1012,2014-07-29T15:00:00Z,100,1s,*tcc;*tcd,,false,true,30,0, diff --git a/data/tariffplans/tutorial/Suppliers.csv b/data/tariffplans/tutorial/Suppliers.csv index 835129fac..587c0b63f 100644 --- a/data/tariffplans/tutorial/Suppliers.csv +++ b/data/tariffplans/tutorial/Suppliers.csv @@ -6,3 +6,6 @@ cgrates.org,SPL_WEIGHT_1,FLTR_ACNT_1007,,,,supplier3,FLTR_ACNT_dan,,,,,15,,, cgrates.org,SPL_LEASTCOST_1,FLTR_1,2017-11-27T00:00:00Z,*least_cost,,supplier1,,,RP_SPECIAL_1002,,,10,false,,10 cgrates.org,SPL_LEASTCOST_1,,,,,supplier2,,,RP_RETAIL1,,,20,,, cgrates.org,SPL_LEASTCOST_1,,,,,supplier3,,,RP_SPECIAL_1002,,,15,,, +cgrates.org,SPL_LEASTCOST_2,FLTR_2,2017-11-27T00:00:00Z,*least_cost,,supplier1,,,RP_1012,,,10,false,,10 +cgrates.org,SPL_LEASTCOST_2,,,,,supplier2,,,RP_1011,,,20,,, +cgrates.org,SPL_LEASTCOST_2,,,,,supplier3,,,RP_1012,,,15,,, diff --git a/data/tariffplans/tutorial/Thresholds.csv b/data/tariffplans/tutorial/Thresholds.csv index d82fc7ec1..159a9da52 100644 --- a/data/tariffplans/tutorial/Thresholds.csv +++ b/data/tariffplans/tutorial/Thresholds.csv @@ -6,3 +6,6 @@ cgrates.org,THD_STATS_2,FLTR_STATS_2,2014-07-29T15:00:00Z,true,1,1s,false,10,DIS cgrates.org,THD_STATS_3,FLTR_STATS_3,2014-07-29T15:00:00Z,false,1,1s,false,10,TOPUP_100SMS_DE_MOBILE,false cgrates.org,THD_RES_1,FLTR_RES_1,2014-07-29T15:00:00Z,true,1,1s,false,10,LOG_WARNING,false cgrates.org,THD_CDRS_1,FLTR_ACNT_1007;FLTR_CDR_UPDATE,2014-07-29T15:00:00Z,false,1,1s,false,10,LOG_WARNING,false +cgrates.org,THD_ACNT_1011,FLTR_ACNT_1011,2014-07-29T15:00:00Z,false,1,1s,false,10,LOG_WARNING,false +cgrates.org,THD_ACNT_1012,FLTR_ACNT_1012,2014-07-29T15:00:00Z,true,1,1s,false,10,LOG_WARNING,false + diff --git a/data/tutorials/fs_evsock/cgrates/etc/cgrates/cgrates.json b/data/tutorials/fs_evsock/cgrates/etc/cgrates/cgrates.json index 041c84c15..44fa10878 100644 --- a/data/tutorials/fs_evsock/cgrates/etc/cgrates/cgrates.json +++ b/data/tutorials/fs_evsock/cgrates/etc/cgrates/cgrates.json @@ -33,10 +33,10 @@ {"address": "127.0.0.1:2012", "transport": "*json"} ], "pubsubs_conns": [ - {"address": "127.0.0.1:2012", "transport": "*json"} + {"address": "*internal"} ], "attributes_conns": [ - {"address": "127.0.0.1:2012", "transport": "*json"} + {"address": "*internal"} ], }, @@ -49,6 +49,7 @@ "stats_conns": [ {"address": "127.0.0.1:2012", "transport": "*json"} ], + "sessions_cost_retries": 5, }, @@ -61,13 +62,13 @@ {"address": "127.0.0.1:2012", "transport": "*json"} ], "resources_conns": [ - {"address": "127.0.0.1:2012", "transport": "*json"} + {"address": "*internal"} ], "suppliers_conns": [ - {"address": "127.0.0.1:2012", "transport": "*json"} + {"address": "*internal"} ], "attributes_conns": [ - {"address": "127.0.0.1:2012", "transport": "*json"} + {"address": "*internal"} ], "stats_conns": [ {"address": "127.0.0.1:2012", "transport": "*json"} @@ -101,7 +102,7 @@ "resources": { "enabled": true, "thresholds_conns": [ - {"address": "127.0.0.1:2012", "transport": "*json"} + {"address": "*internal"} ], "string_indexed_fields": ["Account"], "prefix_indexed_fields": ["Destination"], diff --git a/engine/cdrs.go b/engine/cdrs.go index 775ffd7d7..67136b950 100644 --- a/engine/cdrs.go +++ b/engine/cdrs.go @@ -443,8 +443,8 @@ func (self *CdrServer) rateCDR(cdr *CDR) ([]*CDR, error) { return cdrsRated, nil } else { //calculate CDR as for pseudoprepaid utils.Logger.Warning( - fmt.Sprintf(" WARNING: Could not find CallCostLog for cgrid: %s, source: %s, runid: %s, originID: %s, will recalculate", - cdr.CGRID, utils.MetaSessionS, cdr.RunID, cdr.OriginID)) + fmt.Sprintf(" WARNING: Could not find CallCostLog for cgrid: %s, source: %s, runid: %s, originID: %s originHost: %s, will recalculate", + cdr.CGRID, utils.MetaSessionS, cdr.RunID, cdr.OriginID, cdr.OriginHost)) qryCC, err = self.getCostFromRater(cdr) } } else { diff --git a/engine/fscdr.go b/engine/fscdr.go index f481bee8a..4e33da09a 100644 --- a/engine/fscdr.go +++ b/engine/fscdr.go @@ -41,7 +41,6 @@ const ( FS_HANGUP_TIME = "end_epoch" FS_DURATION = "billsec" FS_USERNAME = "user_name" - FS_IP = "sip_local_network_addr" FS_CDR_SOURCE = "freeswitch_json" FS_SIP_REQUSER = "sip_req_user" // Apps like FusionPBX do not set dialed_extension, alternative being destination_number but that comes in customer profile, not in vars FS_PROGRESS_MEDIAMSEC = "progress_mediamsec" @@ -139,7 +138,7 @@ func (fsCdr FSCdr) AsCDR(timezone string) *CDR { storCdr.CGRID = fsCdr.getCGRID() storCdr.ToR = utils.VOICE storCdr.OriginID = fsCdr.vars[FS_UUID] - storCdr.OriginHost = fsCdr.vars[FS_IP] + storCdr.OriginHost = fsCdr.vars[FsIPv4] storCdr.Source = FS_CDR_SOURCE storCdr.RequestType = utils.FirstNonEmpty(fsCdr.vars[utils.CGR_REQTYPE], fsCdr.cgrCfg.DefaultReqType) storCdr.Tenant = utils.FirstNonEmpty(fsCdr.vars[utils.CGR_TENANT], fsCdr.cgrCfg.DefaultTenant) diff --git a/engine/fscdr_test.go b/engine/fscdr_test.go index 6c67ae24c..d9efa1544 100644 --- a/engine/fscdr_test.go +++ b/engine/fscdr_test.go @@ -421,7 +421,7 @@ func TestFsCdrCDRFields(t *testing.T) { expctCDR := &CDR{ CGRID: "24b5766be325fa751fab5a0a06373e106f33a257", ToR: utils.VOICE, OriginID: "3da8bf84-c133-4959-9e24-e72875cb33a1", - OriginHost: "10.10.10.204", Source: "freeswitch_json", Category: "call", + OriginHost: "", Source: "freeswitch_json", Category: "call", RequestType: utils.META_RATED, Tenant: "cgrates.org", Account: "1001", Subject: "1001", Destination: "1002", SetupTime: setupTime, diff --git a/general_tests/tut_fs_calls_test.go b/general_tests/tut_fs_calls_test.go index 241d427ca..b94bc0f5f 100755 --- a/general_tests/tut_fs_calls_test.go +++ b/general_tests/tut_fs_calls_test.go @@ -41,8 +41,46 @@ var tutFsCallsRpc *rpc.Client var tutFsCallsPjSuaListener *os.File var waitRater = flag.Int("wait_rater", 100, "Number of miliseconds to wait for rater to start and cache") var dataDir = flag.String("data_dir", "/usr/share/cgrates", "CGR data dir path here") +var fsConfig = flag.String("fsConfig", "/usr/share/cgrates/tutorials/fs_evsock", "FreeSwitch tutorial folder") -func TestFSCallInitCfg(t *testing.T) { +var sTestsCalls = []func(t *testing.T){ + testCallInitCfg, + testCallResetDataDb, + testCallResetStorDb, + testCallStartFS, + testCallStartEngine, + testCallRestartFS, + testCallRpcConn, + testCallLoadTariffPlanFromFolder, + testCallAccountsBefore, + testCallStartPjsuaListener, + testCallCheckResourceBeforeAllocation, + testCallCheckThreshold1001Before, + testCallCheckThreshold1002Before, + testCallCall1001To1002, + testCallCall1002To1001, + testCallGetActiveSessions, + testCallCheckResourceAllocation, + testCallAccount1001, + testCallCheckThreshold1001After, + testCallCheckThreshold1002After, + testCallCheckResourceRelease, + testCall1001Cdrs, + testCallStatMetrics, + testCallStopPjsuaListener, + testCallStopCgrEngine, + testCallStopFS, +} + +//Test start here +func TestFSCall(t *testing.T) { + + for _, stest := range sTestsCalls { + t.Run("", stest) + } +} + +func testCallInitCfg(t *testing.T) { // Init config first var err error tutFsCallsCfg, err = config.NewCGRConfigFromFolder(path.Join(*dataDir, "tutorials", "fs_evsock", "cgrates", "etc", "cgrates")) @@ -54,21 +92,21 @@ func TestFSCallInitCfg(t *testing.T) { } // Remove data in both rating and accounting db -func TestFSCallResetDataDb(t *testing.T) { +func testCallResetDataDb(t *testing.T) { if err := engine.InitDataDb(tutFsCallsCfg); err != nil { t.Fatal(err) } } // Wipe out the cdr database -func TestFSCallResetStorDb(t *testing.T) { +func testCallResetStorDb(t *testing.T) { if err := engine.InitStorDb(tutFsCallsCfg); err != nil { t.Fatal(err) } } // start FS server -func TestFSCallStartFS(t *testing.T) { +func testCallStartFS(t *testing.T) { engine.KillProcName("freeswitch", 5000) if err := engine.CallScript(path.Join(*dataDir, "tutorials", "fs_evsock", "freeswitch", "etc", "init.d", "freeswitch"), "start", 3000); err != nil { t.Fatal(err) @@ -76,7 +114,7 @@ func TestFSCallStartFS(t *testing.T) { } // Start CGR Engine -func TestFSCallStartEngine(t *testing.T) { +func testCallStartEngine(t *testing.T) { engine.KillProcName("cgr-engine", *waitRater) if err := engine.CallScript(path.Join(*dataDir, "tutorials", "fs_evsock", "cgrates", "etc", "init.d", "cgrates"), "start", 100); err != nil { t.Fatal(err) @@ -84,14 +122,14 @@ func TestFSCallStartEngine(t *testing.T) { } // Restart FS so we make sure reconnects are working -func TestFSCallRestartFS(t *testing.T) { +func testCallRestartFS(t *testing.T) { if err := engine.CallScript(path.Join(*dataDir, "tutorials", "fs_evsock", "freeswitch", "etc", "init.d", "freeswitch"), "restart", 5000); err != nil { t.Fatal(err) } } // Connect rpc client to rater -func TestFSCallRpcConn(t *testing.T) { +func testCallRpcConn(t *testing.T) { var err error tutFsCallsRpc, err = jsonrpc.Dial("tcp", tutFsCallsCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed if err != nil { @@ -101,7 +139,7 @@ func TestFSCallRpcConn(t *testing.T) { } // Load the tariff plan, creating accounts and their balances -func TestFSCallLoadTariffPlanFromFolder(t *testing.T) { +func testCallLoadTariffPlanFromFolder(t *testing.T) { reply := "" attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "tutorial")} if err := tutFsCallsRpc.Call("ApierV1.LoadTariffPlanFromFolder", attrs, &reply); err != nil { @@ -113,7 +151,7 @@ func TestFSCallLoadTariffPlanFromFolder(t *testing.T) { } // Make sure account was debited properly -func TestFSCallAccountsBefore(t *testing.T) { +func testCallAccountsBefore(t *testing.T) { var reply *engine.Account attrs := &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1001"} if err := tutFsCallsRpc.Call("ApierV2.GetAccount", attrs, &reply); err != nil { @@ -124,63 +162,85 @@ func TestFSCallAccountsBefore(t *testing.T) { } // Start Pjsua as listener and register it to receive calls -func TestFSCallStartPjsuaListener(t *testing.T) { +func testCallStartPjsuaListener(t *testing.T) { var err error acnts := []*engine.PjsuaAccount{ - &engine.PjsuaAccount{Id: "sip:1001@192.168.56.202", - Username: "1001", Password: "CGRateS.org", Realm: "*", Registrar: "sip:192.168.56.202:5060"}, - &engine.PjsuaAccount{Id: "sip:1002@192.168.56.202", - Username: "1002", Password: "CGRateS.org", Realm: "*", Registrar: "sip:192.168.56.202:5060"}} + &engine.PjsuaAccount{Id: "sip:1011@192.168.56.202", + Username: "1011", Password: "CGRateS.org", Realm: "*", Registrar: "sip:192.168.56.202:5060"}, + &engine.PjsuaAccount{Id: "sip:1012@192.168.56.202", + Username: "1012", Password: "CGRateS.org", Realm: "*", Registrar: "sip:192.168.56.202:5060"}} if tutFsCallsPjSuaListener, err = engine.StartPjsuaListener( acnts, 5070, time.Duration(*waitRater)*time.Millisecond); err != nil { t.Fatal(err) } } -func TestFSCallCheckResourceBeforeAllocation(t *testing.T) { +func testCallCheckResourceBeforeAllocation(t *testing.T) { var rs *engine.Resources args := &utils.ArgRSv1ResourceUsage{ CGREvent: utils.CGREvent{ Tenant: "cgrates.org", Event: map[string]interface{}{ - utils.Account: "1001", - utils.Subject: "1001", - utils.Destination: "1002"}, + utils.Account: "1011", + utils.Subject: "1011", + utils.Destination: "1012"}, }} if err := tutFsCallsRpc.Call(utils.ResourceSv1GetResourcesForEvent, args, &rs); err != nil { t.Error(err) } else if len(*rs) != 2 { - t.Errorf("Resources: %+v", rs) + t.Errorf("Resources: %+v", utils.ToJSON(rs)) } for _, r := range *rs { - if r.ID == "ResGroup1" && + if r.ID == "ResGroup4" && (len(r.Usages) != 0 || len(r.TTLIdx) != 0) { - t.Errorf("Unexpected resource: %+v", r) + t.Errorf("Unexpected resource: %+v", utils.ToJSON(r)) } } } +func testCallCheckThreshold1001Before(t *testing.T) { + var td engine.Threshold + eTd := engine.Threshold{Tenant: "cgrates.org", ID: "THD_ACNT_1011", Hits: 0} + if err := tutFsCallsRpc.Call(utils.ThresholdSv1GetThreshold, + &utils.TenantID{Tenant: "cgrates.org", ID: "THD_ACNT_1011"}, &td); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eTd, td) { + t.Errorf("expecting: %+v, received: %+v", eTd, td) + } +} + +func testCallCheckThreshold1002Before(t *testing.T) { + var td engine.Threshold + eTd := engine.Threshold{Tenant: "cgrates.org", ID: "THD_ACNT_1012", Hits: 0} + if err := tutFsCallsRpc.Call(utils.ThresholdSv1GetThreshold, + &utils.TenantID{Tenant: "cgrates.org", ID: "THD_ACNT_1012"}, &td); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eTd, td) { + t.Errorf("expecting: %+v, received: %+v", eTd, td) + } +} + // Call from 1001 (prepaid) to 1002 -func TestFSCallCall1001To1002(t *testing.T) { +func testCallCall1001To1002(t *testing.T) { if err := engine.PjsuaCallUri( - &engine.PjsuaAccount{Id: "sip:1001@192.168.56.202", Username: "1001", Password: "CGRateS.org", Realm: "*"}, - "sip:1002@192.168.56.202", "sip:192.168.56.202:5060", time.Duration(67)*time.Second, 5071); err != nil { + &engine.PjsuaAccount{Id: "sip:1011@192.168.56.202", Username: "1011", Password: "CGRateS.org", Realm: "*"}, + "sip:1012@192.168.56.202", "sip:192.168.56.202:5060", time.Duration(67)*time.Second, 5071); err != nil { t.Fatal(err) } time.Sleep(1 * time.Second) } // Call from 1002 (postpaid) to 1001 -func TestFSCallCall1002To1001(t *testing.T) { +func testCallCall1002To1001(t *testing.T) { if err := engine.PjsuaCallUri( - &engine.PjsuaAccount{Id: "sip:1002@192.168.56.202", Username: "1002", Password: "CGRateS.org", Realm: "*"}, - "sip:1001@192.168.56.202", "sip:192.168.56.202:5060", time.Duration(65)*time.Second, 5072); err != nil { + &engine.PjsuaAccount{Id: "sip:1012@192.168.56.202", Username: "1012", Password: "CGRateS.org", Realm: "*"}, + "sip:1011@192.168.56.202", "sip:192.168.56.202:5060", time.Duration(65)*time.Second, 5072); err != nil { t.Fatal(err) } } // GetActiveSessions -func TestFSCallGetActiveSessions(t *testing.T) { +func testCallGetActiveSessions(t *testing.T) { var reply *[]*sessions.ActiveSession expected := &[]*sessions.ActiveSession{ &sessions.ActiveSession{ @@ -188,9 +248,9 @@ func TestFSCallGetActiveSessions(t *testing.T) { ReqType: "*prepaid", Tenant: "cgrates.org", Category: "call", - Account: "1001", - Subject: "1001", - Destination: "1002", + Account: "1011", + Subject: "1011", + Destination: "1012", }, } if err := tutFsCallsRpc.Call("SessionSv1.GetActiveSessions", @@ -210,40 +270,35 @@ func TestFSCallGetActiveSessions(t *testing.T) { } } -func TestFSCallCheckResourceAllocation(t *testing.T) { +// Check if the resource was Allocated +func testCallCheckResourceAllocation(t *testing.T) { var rs *engine.Resources args := &utils.ArgRSv1ResourceUsage{ CGREvent: utils.CGREvent{ Tenant: "cgrates.org", Event: map[string]interface{}{ - utils.Account: "1001", - utils.Subject: "1001", - utils.Destination: "1002"}, + utils.Account: "1011", + utils.Subject: "1011", + utils.Destination: "1012"}, }} if err := tutFsCallsRpc.Call(utils.ResourceSv1GetResourcesForEvent, args, &rs); err != nil { t.Error(err) } else if len(*rs) != 2 { - t.Errorf("Resources: %+v", rs) + t.Errorf("Resources: %+v", utils.ToJSON(rs)) } for _, r := range *rs { - if r.ID == "ResGroup1" && + if r.ID == "ResGroup4" && (len(r.Usages) != 1 || len(r.TTLIdx) != 1) { - t.Errorf("Unexpected resource: %+v", r) + t.Errorf("Unexpected resource: %+v", utils.ToJSON(r)) } } } -// get account while call is on -// add threshold non recurent -// while call is on threshold is there -// for 1001 -> 1002 non recurent -// for 1002 -> 1001 recurent acnd check if was executed - // Make sure account was debited properly -func TestFSCallAccount1001(t *testing.T) { +func testCallAccount1001(t *testing.T) { time.Sleep(time.Duration(80) * time.Second) // Allow calls to finish before start querying the results var reply *engine.Account - attrs := &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1001"} + attrs := &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1011"} if err := tutFsCallsRpc.Call("ApierV2.GetAccount", attrs, &reply); err != nil { t.Error(err.Error()) } else if reply.BalanceMap[utils.MONETARY].GetTotalValue() == 10.0 { // Make sure we debitted @@ -253,15 +308,39 @@ func TestFSCallAccount1001(t *testing.T) { } } -func TestFSCallCheckResourceRelease(t *testing.T) { +func testCallCheckThreshold1001After(t *testing.T) { + var td engine.Threshold + if err := tutFsCallsRpc.Call(utils.ThresholdSv1GetThreshold, + &utils.TenantID{Tenant: "cgrates.org", ID: "THD_ACNT_1011"}, &td); err != nil && + err.Error() != utils.ErrNotFound.Error() { + t.Error(err) + } +} + +func testCallCheckThreshold1002After(t *testing.T) { + var td engine.Threshold + eTd := engine.Threshold{Tenant: "cgrates.org", ID: "THD_ACNT_1012", Hits: 4} + if err := tutFsCallsRpc.Call(utils.ThresholdSv1GetThreshold, + &utils.TenantID{Tenant: "cgrates.org", ID: "THD_ACNT_1012"}, &td); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eTd.Tenant, td.Tenant) { + t.Errorf("expecting: %+v, received: %+v", eTd.Tenant, td.Tenant) + } else if !reflect.DeepEqual(eTd.ID, td.ID) { + t.Errorf("expecting: %+v, received: %+v", eTd.ID, td.ID) + } else if !reflect.DeepEqual(eTd.Hits, td.Hits) { + t.Errorf("expecting: %+v, received: %+v", eTd.Hits, td.Hits) + } +} + +func testCallCheckResourceRelease(t *testing.T) { var rs *engine.Resources args := &utils.ArgRSv1ResourceUsage{ CGREvent: utils.CGREvent{ Tenant: "cgrates.org", Event: map[string]interface{}{ - utils.Account: "1001", - utils.Subject: "1001", - utils.Destination: "1002"}, + utils.Account: "1011", + utils.Subject: "1011", + utils.Destination: "1012"}, }} if err := tutFsCallsRpc.Call(utils.ResourceSv1GetResourcesForEvent, args, &rs); err != nil { t.Error(err) @@ -269,46 +348,41 @@ func TestFSCallCheckResourceRelease(t *testing.T) { t.Errorf("Resources: %+v", rs) } for _, r := range *rs { - if r.ID == "ResGroup1" && + if r.ID == "ResGroup4" && (len(r.Usages) != 0 || len(r.TTLIdx) != 0) { t.Errorf("Unexpected resource: %+v", r) } } } -// after call end threshold should't be there - -// get cdr and check source of cdr to be *sessions - // Make sure account was debited properly -func TestTutFsCalls1001Cdrs(t *testing.T) { +func testCall1001Cdrs(t *testing.T) { var reply []*engine.ExternalCDR - req := utils.RPCCDRsFilter{RunIDs: []string{utils.META_DEFAULT}, Accounts: []string{"1001"}, DestinationPrefixes: []string{"1002"}} + req := utils.RPCCDRsFilter{RunIDs: []string{utils.META_DEFAULT}, Accounts: []string{"1011"}, DestinationPrefixes: []string{"1012"}} if err := tutFsCallsRpc.Call("ApierV2.GetCdrs", req, &reply); err != nil { t.Error("Unexpected error: ", err.Error()) } else if len(reply) != 1 { t.Error("Unexpected number of CDRs returned: ", len(reply)) } else { if reply[0].Source != "freeswitch_json" { - t.Errorf("Unexpected Source for CDR: %+v", reply[0]) + t.Errorf("Unexpected Source for CDR: %+v", reply[0].Source) } if reply[0].RequestType != utils.META_PREPAID { - t.Errorf("Unexpected RequestType for CDR: %+v", reply[0]) + t.Errorf("Unexpected RequestType for CDR: %+v", reply[0].RequestType) } if reply[0].Usage != "1m7s" { // Usage as seconds - t.Errorf("Unexpected Usage for CDR: %+v", utils.ToJSON(reply[0].Usage)) + t.Errorf("Unexpected Usage for CDR: %+v", reply[0].Usage) } - if reply[0].Cost == -1.0 { // Cost was not calculated - t.Errorf("Unexpected Cost for CDR: %+v", reply[0]) + if reply[0].CostSource != utils.MetaSessionS { + t.Errorf("Unexpected CostSource for CDR: %+v", reply[0].CostSource) } } - // verifica CDR in sessions_cost si daca il gaseste il scrie cu *sessions la cost_source } -func TestFSCallStatMetrics2(t *testing.T) { +func testCallStatMetrics(t *testing.T) { var metrics map[string]string expectedMetrics := map[string]string{ - utils.MetaTCC: "1.8451", + utils.MetaTCC: "0.91589", utils.MetaTCD: "2m12s", } if err := tutFsCallsRpc.Call(utils.StatSv1GetQueueStringMetrics, @@ -319,17 +393,17 @@ func TestFSCallStatMetrics2(t *testing.T) { } } -func TestFSCallStopPjsuaListener(t *testing.T) { +func testCallStopPjsuaListener(t *testing.T) { tutFsCallsPjSuaListener.Write([]byte("q\n")) // Close pjsua time.Sleep(time.Duration(1) * time.Second) // Allow pjsua to finish it's tasks, eg un-REGISTER } -func TestFSCallStopCgrEngine(t *testing.T) { +func testCallStopCgrEngine(t *testing.T) { if err := engine.KillEngine(100); err != nil { t.Error(err) } } -func TestFSCallStopFS(t *testing.T) { +func testCallStopFS(t *testing.T) { engine.KillProcName("freeswitch", 1000) }