From b346b28b3acaae636d7b6b8b1e062e3a5bf407bf Mon Sep 17 00:00:00 2001 From: DanB Date: Tue, 21 Apr 2015 11:10:33 +0200 Subject: [PATCH] Complete kamailio tutorial test calls --- .../etc/kamailio/kamailio-cgrates.cfg | 5 + general_tests/tutorial_fs_calls_test.go | 22 +- general_tests/tutorial_kam_calls_test.go | 418 ++++++++++++++++-- 3 files changed, 405 insertions(+), 40 deletions(-) diff --git a/data/tutorials/kamevapi/kamailio/etc/kamailio/kamailio-cgrates.cfg b/data/tutorials/kamevapi/kamailio/etc/kamailio/kamailio-cgrates.cfg index 0d88e83a1..8557e93b3 100644 --- a/data/tutorials/kamevapi/kamailio/etc/kamailio/kamailio-cgrates.cfg +++ b/data/tutorials/kamevapi/kamailio/etc/kamailio/kamailio-cgrates.cfg @@ -34,11 +34,16 @@ route[CGR_AUTH_REQUEST] { } switch ($fU) { case 1001: + case 1006: + case 1007: $dlg_var(cgrReqType) = "*prepaid"; break; case 1002: $dlg_var(cgrReqType) = "*postpaid"; break; + case 1003: + $dlg_var(cgrReqType) = "*pseudoprepaid"; + break; default: $dlg_var(cgrReqType) = "*rated"; } diff --git a/general_tests/tutorial_fs_calls_test.go b/general_tests/tutorial_fs_calls_test.go index 153021048..a41f3e012 100644 --- a/general_tests/tutorial_fs_calls_test.go +++ b/general_tests/tutorial_fs_calls_test.go @@ -52,16 +52,6 @@ func TestTutFsCallsInitCfg(t *testing.T) { config.SetCgrConfig(tutFsCallsCfg) } -// Wipe out the cdr database -func TestTutFsCallsResetDb(t *testing.T) { - if !*testCalls { - return - } - if err := engine.InitStorDb(tutFsCallsCfg); err != nil { - t.Fatal(err) - } -} - // Remove data in both rating and accounting db func TestTutFsCallsResetDataDb(t *testing.T) { if !*testCalls { @@ -72,6 +62,16 @@ func TestTutFsCallsResetDataDb(t *testing.T) { } } +// Wipe out the cdr database +func TestTutFsCallsResetStorDb(t *testing.T) { + if !*testCalls { + return + } + if err := engine.InitStorDb(tutFsCallsCfg); err != nil { + t.Fatal(err) + } +} + // start FS server func TestTutFsCallsStartFS(t *testing.T) { if !*testCalls { @@ -149,7 +149,7 @@ func TestTutFsCallsCacheStats(t *testing.T) { } // Check items age -func TestApierGetCachedItemAge(t *testing.T) { +func TestTutFsCallsGetCachedItemAge(t *testing.T) { if !*testCalls { return } diff --git a/general_tests/tutorial_kam_calls_test.go b/general_tests/tutorial_kam_calls_test.go index a732dc16f..b311575ca 100644 --- a/general_tests/tutorial_kam_calls_test.go +++ b/general_tests/tutorial_kam_calls_test.go @@ -23,9 +23,12 @@ import ( "net/rpc/jsonrpc" "os" "path" + "reflect" + "strings" "testing" "time" + "github.com/cgrates/cgrates/apier/v1" "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" @@ -49,16 +52,6 @@ func TestTutKamCallsInitCfg(t *testing.T) { config.SetCgrConfig(tutKamCallsCfg) } -// Wipe out the cdr database -func TestTutKamCallsResetDb(t *testing.T) { - if !*testCalls { - return - } - if err := engine.InitStorDb(tutKamCallsCfg); err != nil { - t.Fatal(err) - } -} - // Remove data in both rating and accounting db func TestTutKamCallsResetDataDb(t *testing.T) { if !*testCalls { @@ -69,13 +62,23 @@ func TestTutKamCallsResetDataDb(t *testing.T) { } } -// start FS server +// Wipe out the cdr database +func TestTutKamCallsResetStorDb(t *testing.T) { + if !*testCalls { + return + } + if err := engine.InitStorDb(tutKamCallsCfg); err != nil { + t.Fatal(err) + } +} + +// start Kam server func TestTutKamCallsStartKam(t *testing.T) { if !*testCalls { return } engine.KillProcName("kamailio", *waitRater) - if err := engine.CallScript(path.Join(*dataDir, "tutorials", "kamevapi", "kamailio", "etc", "init.d", "kamailio"), "start", *waitRater); err != nil { + if err := engine.CallScript(path.Join(*dataDir, "tutorials", "kamevapi", "kamailio", "etc", "init.d", "kamailio"), "start", 3000); err != nil { t.Fatal(err) } } @@ -86,13 +89,12 @@ func TestTutKamCallsStartEngine(t *testing.T) { return } engine.KillProcName("cgr-engine", *waitRater) - if err := engine.CallScript(path.Join(*dataDir, "tutorials", "kamevapi", "cgrates", "etc", "init.d", "cgrates"), "start", *waitRater); err != nil { + if err := engine.CallScript(path.Join(*dataDir, "tutorials", "kamevapi", "cgrates", "etc", "init.d", "cgrates"), "start", 100); err != nil { t.Fatal(err) } } -// Restart FS so we make sure reconnects are working - +// Restart Kam so we make sure reconnects are working func TestTutKamCallsRestartKam(t *testing.T) { if !*testCalls { return @@ -129,6 +131,237 @@ func TestTutKamCallsLoadTariffPlanFromFolder(t *testing.T) { time.Sleep(time.Duration(*waitRater) * time.Millisecond) // Give time for scheduler to execute topups } +// Check loaded stats +func TestTutKamCallsCacheStats(t *testing.T) { + if !*testCalls { + return + } + var rcvStats *utils.CacheStats + expectedStats := &utils.CacheStats{Destinations: 3, RatingPlans: 3, RatingProfiles: 3, Actions: 6, SharedGroups: 1, RatingAliases: 1, AccountAliases: 1, DerivedChargers: 1} + var args utils.AttrCacheStats + if err := tutKamCallsRpc.Call("ApierV1.GetCacheStats", args, &rcvStats); err != nil { + t.Error("Got error on ApierV1.GetCacheStats: ", err.Error()) + } else if !reflect.DeepEqual(expectedStats, rcvStats) { + t.Errorf("Calling ApierV1.GetCacheStats expected: %v, received: %v", expectedStats, rcvStats) + } +} + +// Check items age +func TestTutKamCallsGetCachedItemAge(t *testing.T) { + if !*testCalls { + return + } + var rcvAge *utils.CachedItemAge + if err := tutKamCallsRpc.Call("ApierV1.GetCachedItemAge", "1002", &rcvAge); err != nil { + t.Error("Got error on ApierV1.GetCachedItemAge: ", err.Error()) + } else if rcvAge.Destination > time.Duration(2)*time.Second { + t.Errorf("Cache too old: %d", rcvAge) + } + if err := tutKamCallsRpc.Call("ApierV1.GetCachedItemAge", "RP_RETAIL1", &rcvAge); err != nil { + t.Error("Got error on ApierV1.GetCachedItemAge: ", err.Error()) + } else if rcvAge.RatingPlan > time.Duration(2)*time.Second { + t.Errorf("Cache too old: %d", rcvAge) + } + if err := tutKamCallsRpc.Call("ApierV1.GetCachedItemAge", "*out:cgrates.org:call:*any", &rcvAge); err != nil { + t.Error("Got error on ApierV1.GetCachedItemAge: ", err.Error()) + } else if rcvAge.RatingProfile > time.Duration(2)*time.Second { + t.Errorf("Cache too old: %d", rcvAge) + } + if err := tutKamCallsRpc.Call("ApierV1.GetCachedItemAge", "LOG_WARNING", &rcvAge); err != nil { + t.Error("Got error on ApierV1.GetCachedItemAge: ", err.Error()) + } else if rcvAge.Action > time.Duration(2)*time.Second { + t.Errorf("Cache too old: %d", rcvAge) + } + if err := tutKamCallsRpc.Call("ApierV1.GetCachedItemAge", "SHARED_A", &rcvAge); err != nil { + t.Error("Got error on ApierV1.GetCachedItemAge: ", err.Error()) + } else if rcvAge.SharedGroup > time.Duration(2)*time.Second { + t.Errorf("Cache too old: %d", rcvAge) + } + /* + if err := tutKamCallsRpc.Call("ApierV1.GetCachedItemAge", "1006", &rcvAge); err != nil { + t.Error("Got error on ApierV1.GetCachedItemAge: ", err.Error()) + } else if rcvAge.RatingAlias > time.Duration(2)*time.Second { + t.Errorf("Cache too old: %d", rcvAge) + } + if err := tutKamCallsRpc.Call("ApierV1.GetCachedItemAge", "1006", &rcvAge); err != nil { + t.Error("Got error on ApierV1.GetCachedItemAge: ", err.Error()) + } else if rcvAge.RatingAlias > time.Duration(2)*time.Second || rcvAge.AccountAlias > time.Duration(2)*time.Second { + t.Errorf("Cache too old: %d", rcvAge) + } + */ +} + +// Make sure account was debited properly +func TestTutKamCallsAccountsBefore(t *testing.T) { + if !*testCalls { + return + } + var reply *engine.Account + attrs := &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1001", Direction: "*out"} + if err := tutKamCallsRpc.Call("ApierV1.GetAccount", attrs, &reply); err != nil { + t.Error("Got error on ApierV1.GetAccount: ", err.Error()) + } else if reply.BalanceMap[engine.CREDIT+attrs.Direction].GetTotalValue() != 10.0 { // Make sure we debitted + t.Errorf("Calling ApierV1.GetBalance received: %f", reply.BalanceMap[engine.CREDIT+attrs.Direction].GetTotalValue()) + } + attrs = &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1002", Direction: "*out"} + if err := tutKamCallsRpc.Call("ApierV1.GetAccount", attrs, &reply); err != nil { + t.Error("Got error on ApierV1.GetAccount: ", err.Error()) + } else if reply.BalanceMap[engine.CREDIT+attrs.Direction].GetTotalValue() != 10.0 { // Make sure we debitted + t.Errorf("Calling ApierV1.GetBalance received: %f", reply.BalanceMap[engine.CREDIT+attrs.Direction].GetTotalValue()) + } + attrs = &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1003", Direction: "*out"} + if err := tutKamCallsRpc.Call("ApierV1.GetAccount", attrs, &reply); err != nil { + t.Error("Got error on ApierV1.GetAccount: ", err.Error()) + } else if reply.BalanceMap[engine.CREDIT+attrs.Direction].GetTotalValue() != 10.0 { // Make sure we debitted + t.Errorf("Calling ApierV1.GetBalance received: %f", reply.BalanceMap[engine.CREDIT+attrs.Direction].GetTotalValue()) + } + attrs = &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1004", Direction: "*out"} + if err := tutKamCallsRpc.Call("ApierV1.GetAccount", attrs, &reply); err != nil { + t.Error("Got error on ApierV1.GetAccount: ", err.Error()) + } else if reply.BalanceMap[engine.CREDIT+attrs.Direction].GetTotalValue() != 10.0 { // Make sure we debitted + t.Errorf("Calling ApierV1.GetBalance received: %f", reply.BalanceMap[engine.CREDIT+attrs.Direction].GetTotalValue()) + } + attrs = &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1007", Direction: "*out"} + if err := tutKamCallsRpc.Call("ApierV1.GetAccount", attrs, &reply); err != nil { + t.Error("Got error on ApierV1.GetAccount: ", err.Error()) + } else if reply.BalanceMap[engine.CREDIT+attrs.Direction].GetTotalValue() != 0.0 { // Make sure we debitted + t.Errorf("Calling ApierV1.GetBalance received: %f", reply.BalanceMap[engine.CREDIT+attrs.Direction].GetTotalValue()) + } + attrs = &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1005", Direction: "*out"} + if err := tutKamCallsRpc.Call("ApierV1.GetAccount", attrs, &reply); err == nil || !strings.HasSuffix(err.Error(), "does not exist") { + t.Error("Got error on ApierV1.GetAccount: %v", err) + } +} + +// Check call costs +func TestTutKamCallsGetCosts(t *testing.T) { + if !*testCalls { + return + } + tStart, _ := utils.ParseDate("2014-08-04T13:00:00Z") + tEnd, _ := utils.ParseDate("2014-08-04T13:00:20Z") + cd := engine.CallDescriptor{ + Direction: "*out", + Category: "call", + Tenant: "cgrates.org", + Subject: "1001", + Account: "1001", + Destination: "1002", + DurationIndex: 0, + TimeStart: tStart, + TimeEnd: tEnd, + } + var cc engine.CallCost + if err := tutKamCallsRpc.Call("Responder.GetCost", cd, &cc); err != nil { + t.Error("Got error on Responder.GetCost: ", err.Error()) + } else if cc.Cost != 0.3 { + t.Errorf("Calling Responder.GetCost got callcost: %v", cc.Cost) + } + tStart, _ = utils.ParseDate("2014-08-04T13:00:00Z") + tEnd, _ = utils.ParseDate("2014-08-04T13:01:25Z") + cd = engine.CallDescriptor{ + Direction: "*out", + Category: "call", + Tenant: "cgrates.org", + Subject: "1001", + Account: "1001", + Destination: "1002", + DurationIndex: 0, + TimeStart: tStart, + TimeEnd: tEnd, + } + if err := tutKamCallsRpc.Call("Responder.GetCost", cd, &cc); err != nil { + t.Error("Got error on Responder.GetCost: ", err.Error()) + } else if cc.Cost != 0.6209 { + t.Errorf("Calling Responder.GetCost got callcost: %v", cc.Cost) + } + tStart, _ = utils.ParseDate("2014-08-04T13:00:00Z") + tEnd, _ = utils.ParseDate("2014-08-04T13:00:20Z") + cd = engine.CallDescriptor{ + Direction: "*out", + Category: "call", + Tenant: "cgrates.org", + Subject: "1001", + Account: "1001", + Destination: "1003", + DurationIndex: 0, + TimeStart: tStart, + TimeEnd: tEnd, + } + if err := tutKamCallsRpc.Call("Responder.GetCost", cd, &cc); err != nil { + t.Error("Got error on Responder.GetCost: ", err.Error()) + } else if cc.Cost != 0.3 { + t.Errorf("Calling Responder.GetCost got callcost: %v", cc.Cost) + } + tStart, _ = utils.ParseDate("2014-08-04T13:00:00Z") + tEnd, _ = utils.ParseDate("2014-08-04T13:01:25Z") + cd = engine.CallDescriptor{ + Direction: "*out", + Category: "call", + Tenant: "cgrates.org", + Subject: "1001", + Account: "1001", + Destination: "1003", + DurationIndex: 0, + TimeStart: tStart, + TimeEnd: tEnd, + } + if err := tutKamCallsRpc.Call("Responder.GetCost", cd, &cc); err != nil { + t.Error("Got error on Responder.GetCost: ", err.Error()) + } else if cc.Cost != 1.2209 { + t.Errorf("Calling Responder.GetCost got callcost: %v", cc.Cost) + } + tStart, _ = utils.ParseDate("2014-08-04T13:00:00Z") + tEnd, _ = utils.ParseDate("2014-08-04T13:00:20Z") + cd = engine.CallDescriptor{ + Direction: "*out", + Category: "call", + Tenant: "cgrates.org", + Subject: "1001", + Account: "1001", + Destination: "1004", + DurationIndex: 0, + TimeStart: tStart, + TimeEnd: tEnd, + } + if err := tutKamCallsRpc.Call("Responder.GetCost", cd, &cc); err != nil { + t.Error("Got error on Responder.GetCost: ", err.Error()) + } else if cc.Cost != 0.3 { + t.Errorf("Calling Responder.GetCost got callcost: %v", cc.Cost) + } + tStart, _ = utils.ParseDate("2014-08-04T13:00:00Z") + tEnd, _ = utils.ParseDate("2014-08-04T13:01:25Z") + cd = engine.CallDescriptor{ + Direction: "*out", + Category: "call", + Tenant: "cgrates.org", + Subject: "1001", + Account: "1001", + Destination: "1004", + DurationIndex: 0, + TimeStart: tStart, + TimeEnd: tEnd, + } + if err := tutKamCallsRpc.Call("Responder.GetCost", cd, &cc); err != nil { + t.Error("Got error on Responder.GetCost: ", err.Error()) + } else if cc.Cost != 1.2209 { + t.Errorf("Calling Responder.GetCost got callcost: %v", cc.Cost) + } +} + +func TestTutKamCallsCdrStats(t *testing.T) { + if !*testCalls { + return + } + var queueIds []string + eQueueIds := []string{"*default", "CDRST1", "CDRST_1001", "CDRST_1002", "CDRST_1003"} + if err := tutKamCallsRpc.Call("CDRStatsV1.GetQueueIds", "", &queueIds); err != nil { + t.Error("Calling CDRStatsV1.GetQueueIds, got error: ", err.Error()) + } else if len(eQueueIds) != len(queueIds) { + t.Errorf("Expecting: %v, received: %v", eQueueIds, queueIds) + } +} + // Start Pjsua as listener and register it to receive calls func TestTutKamCallsStartPjsuaListener(t *testing.T) { if !*testCalls { @@ -137,7 +370,11 @@ func TestTutKamCallsStartPjsuaListener(t *testing.T) { var err error acnts := []*engine.PjsuaAccount{ &engine.PjsuaAccount{Id: "sip:1001@127.0.0.1", Username: "1001", Password: "CGRateS.org", Realm: "*", Registrar: "sip:127.0.0.1:5060"}, - &engine.PjsuaAccount{Id: "sip:1002@127.0.0.1", Username: "1002", Password: "CGRateS.org", Realm: "*", Registrar: "sip:127.0.0.1:5060"}} + &engine.PjsuaAccount{Id: "sip:1002@127.0.0.1", Username: "1002", Password: "CGRateS.org", Realm: "*", Registrar: "sip:127.0.0.1:5060"}, + &engine.PjsuaAccount{Id: "sip:1003@127.0.0.1", Username: "1003", Password: "CGRateS.org", Realm: "*", Registrar: "sip:127.0.0.1:5060"}, + &engine.PjsuaAccount{Id: "sip:1004@127.0.0.1", Username: "1004", Password: "CGRateS.org", Realm: "*", Registrar: "sip:127.0.0.1:5060"}, + &engine.PjsuaAccount{Id: "sip:1006@127.0.0.1", Username: "1006", Password: "CGRateS.org", Realm: "*", Registrar: "sip:127.0.0.1:5060"}, + &engine.PjsuaAccount{Id: "sip:1007@127.0.0.1", Username: "1007", Password: "CGRateS.org", Realm: "*", Registrar: "sip:127.0.0.1:5060"}} if tutKamCallsPjSuaListener, err = engine.StartPjsuaListener(acnts, *waitRater); err != nil { t.Fatal(err) } @@ -158,7 +395,7 @@ func TestTutKamCallsCall1002To1001(t *testing.T) { if !*testCalls { return } - if err := engine.PjsuaCallUri(&engine.PjsuaAccount{Id: "sip:1002@127.0.0.1", Username: "1002", Password: "1234", Realm: "*"}, "sip:1001@127.0.0.1", + if err := engine.PjsuaCallUri(&engine.PjsuaAccount{Id: "sip:1002@127.0.0.1", Username: "1002", Password: "CGRateS.org", Realm: "*"}, "sip:1001@127.0.0.1", "sip:127.0.0.1:5060", time.Duration(61)*time.Second, 5072); err != nil { t.Fatal(err) } @@ -168,7 +405,7 @@ func TestTutKamCallsCall1003To1001(t *testing.T) { if !*testCalls { return } - if err := engine.PjsuaCallUri(&engine.PjsuaAccount{Id: "sip:1003@127.0.0.1", Username: "1003", Password: "1234", Realm: "*"}, "sip:1001@127.0.0.1", + if err := engine.PjsuaCallUri(&engine.PjsuaAccount{Id: "sip:1003@127.0.0.1", Username: "1003", Password: "CGRateS.org", Realm: "*"}, "sip:1001@127.0.0.1", "sip:127.0.0.1:5060", time.Duration(63)*time.Second, 5073); err != nil { t.Fatal(err) } @@ -178,7 +415,7 @@ func TestTutKamCallsCall1004To1001(t *testing.T) { if !*testCalls { return } - if err := engine.PjsuaCallUri(&engine.PjsuaAccount{Id: "sip:1004@127.0.0.1", Username: "1004", Password: "1234", Realm: "*"}, "sip:1001@127.0.0.1", + if err := engine.PjsuaCallUri(&engine.PjsuaAccount{Id: "sip:1004@127.0.0.1", Username: "1004", Password: "CGRateS.org", Realm: "*"}, "sip:1001@127.0.0.1", "sip:127.0.0.1:5060", time.Duration(62)*time.Second, 5074); err != nil { t.Fatal(err) } @@ -188,7 +425,7 @@ func TestTutKamCallsCall1006To1002(t *testing.T) { if !*testCalls { return } - if err := engine.PjsuaCallUri(&engine.PjsuaAccount{Id: "sip:1006@127.0.0.1", Username: "1006", Password: "1234", Realm: "*"}, "sip:1002@127.0.0.1", + if err := engine.PjsuaCallUri(&engine.PjsuaAccount{Id: "sip:1006@127.0.0.1", Username: "1006", Password: "CGRateS.org", Realm: "*"}, "sip:1002@127.0.0.1", "sip:127.0.0.1:5060", time.Duration(64)*time.Second, 5075); err != nil { t.Fatal(err) } @@ -198,7 +435,7 @@ func TestTutKamCallsCall1007To1002(t *testing.T) { if !*testCalls { return } - if err := engine.PjsuaCallUri(&engine.PjsuaAccount{Id: "sip:1007@127.0.0.1", Username: "1007", Password: "1234", Realm: "*"}, "sip:1002@127.0.0.1", + if err := engine.PjsuaCallUri(&engine.PjsuaAccount{Id: "sip:1007@127.0.0.1", Username: "1007", Password: "CGRateS.org", Realm: "*"}, "sip:1002@127.0.0.1", "sip:127.0.0.1:5060", time.Duration(66)*time.Second, 5076); err != nil { t.Fatal(err) } @@ -216,11 +453,13 @@ func TestTutKamCallsAccount1001(t *testing.T) { t.Error("Got error on ApierV1.GetAccount: ", err.Error()) } else if reply.BalanceMap[engine.CREDIT+attrs.Direction].GetTotalValue() == 10.0 { // Make sure we debitted t.Errorf("Calling ApierV1.GetBalance received: %f", reply.BalanceMap[engine.CREDIT+attrs.Direction].GetTotalValue()) + } else if reply.Disabled == true { + t.Error("Account disabled") } } // Make sure account was debited properly -func TestTutKamCallsCdrs1001(t *testing.T) { +func TestTutKamCallsCdrs(t *testing.T) { if !*testCalls { return } @@ -232,13 +471,13 @@ func TestTutKamCallsCdrs1001(t *testing.T) { t.Error("Unexpected number of CDRs returned: ", len(reply)) } else { if reply[0].CdrSource != "KAMAILIO_CGR_CALL_END" { - t.Errorf("Unexpected CdrSource for CDR: %s", reply[0].CdrSource) + t.Errorf("Unexpected CdrSource for CDR: %+v", reply[0]) } if reply[0].ReqType != utils.META_PREPAID { - t.Errorf("Unexpected ReqType for CDR: %s", reply[0].ReqType) + t.Errorf("Unexpected ReqType for CDR: %+v", reply[0]) } if reply[0].Usage != "67" { // Usage as seconds - t.Errorf("Unexpected Usage for CDR: %+v", reply[0].Usage) + t.Errorf("Unexpected Usage for CDR: %+v", reply[0]) } } req = utils.RpcCdrsFilter{Accounts: []string{"1001"}, RunIds: []string{"derived_run1"}, FilterOnDerived: true} @@ -254,6 +493,129 @@ func TestTutKamCallsCdrs1001(t *testing.T) { t.Errorf("Unexpected Subject for CDR: %+v", reply[0]) } } + req = utils.RpcCdrsFilter{Accounts: []string{"1002"}, RunIds: []string{utils.META_DEFAULT}} + if err := tutKamCallsRpc.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].CdrSource != "KAMAILIO_CGR_CALL_END" { + t.Errorf("Unexpected CdrSource for CDR: %+v", reply[0]) + } + if reply[0].ReqType != utils.META_POSTPAID { + t.Errorf("Unexpected ReqType for CDR: %+v", reply[0]) + } + if reply[0].Destination != "1001" { + t.Errorf("Unexpected Destination for CDR: %+v", reply[0]) + } + if reply[0].Usage != "61" { // Usage as seconds + t.Errorf("Unexpected Usage for CDR: %+v", reply[0]) + } + } + req = utils.RpcCdrsFilter{Accounts: []string{"1003"}, RunIds: []string{utils.META_DEFAULT}} + if err := tutKamCallsRpc.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].CdrSource != "KAMAILIO_CGR_CALL_END" { + t.Errorf("Unexpected CdrSource for CDR: %+v", reply[0]) + } + if reply[0].ReqType != utils.META_PSEUDOPREPAID { + t.Errorf("Unexpected ReqType for CDR: %+v", reply[0]) + } + if reply[0].Destination != "1001" { + t.Errorf("Unexpected Destination for CDR: %+v", reply[0]) + } + if reply[0].Usage != "63" { // Usage as seconds + t.Errorf("Unexpected Usage for CDR: %+v", reply[0]) + } + } + req = utils.RpcCdrsFilter{Accounts: []string{"1004"}, RunIds: []string{utils.META_DEFAULT}} + if err := tutKamCallsRpc.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].CdrSource != "KAMAILIO_CGR_CALL_END" { + t.Errorf("Unexpected CdrSource for CDR: %+v", reply[0]) + } + if reply[0].ReqType != utils.META_RATED { + t.Errorf("Unexpected ReqType for CDR: %+v", reply[0]) + } + if reply[0].Destination != "1001" { + t.Errorf("Unexpected Destination for CDR: %+v", reply[0]) + } + if reply[0].Usage != "62" { // Usage as seconds + t.Errorf("Unexpected Usage for CDR: %+v", reply[0]) + } + } + req = utils.RpcCdrsFilter{Accounts: []string{"1006"}, RunIds: []string{utils.META_DEFAULT}} + if err := tutKamCallsRpc.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].CdrSource != "KAMAILIO_CGR_CALL_END" { + t.Errorf("Unexpected CdrSource for CDR: %+v", reply[0]) + } + if reply[0].ReqType != utils.META_PREPAID { + t.Errorf("Unexpected ReqType for CDR: %+v", reply[0]) + } + if reply[0].Destination != "1002" { + t.Errorf("Unexpected Destination for CDR: %+v", reply[0]) + } + if reply[0].Usage != "64" { // Usage as seconds + t.Errorf("Unexpected Usage for CDR: %+v", reply[0]) + } + } + req = utils.RpcCdrsFilter{Accounts: []string{"1007"}, RunIds: []string{utils.META_DEFAULT}} + if err := tutKamCallsRpc.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].CdrSource != "KAMAILIO_CGR_CALL_END" { + t.Errorf("Unexpected CdrSource for CDR: %+v", reply[0]) + } + if reply[0].ReqType != utils.META_PREPAID { + t.Errorf("Unexpected ReqType for CDR: %+v", reply[0]) + } + if reply[0].Destination != "1002" { + t.Errorf("Unexpected Destination for CDR: %+v", reply[0]) + } + if reply[0].Usage != "66" { // Usage as seconds + t.Errorf("Unexpected Usage for CDR: %+v", reply[0]) + } + } +} + +// Make sure account was debited properly +func TestTutKamCallsAccountFraud1001(t *testing.T) { + if !*testCalls { + return + } + var reply string + attrAddBlnc := &v1.AttrAddBalance{Tenant: "cgrates.org", Account: "1001", BalanceType: "*monetary", Direction: "*out", Value: 101} + if err := tutKamCallsRpc.Call("ApierV1.AddBalance", attrAddBlnc, &reply); err != nil { + t.Error("Got error on ApierV1.AddBalance: ", err.Error()) + } else if reply != "OK" { + t.Errorf("Calling ApierV1.AddBalance received: %s", reply) + } +} + +// Based on Fraud automatic mitigation, our account should be disabled +func TestTutKamCallsAccountDisabled1001(t *testing.T) { + if !*testCalls { + return + } + var reply *engine.Account + attrs := &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1001", Direction: "*out"} + if err := tutKamCallsRpc.Call("ApierV1.GetAccount", attrs, &reply); err != nil { + t.Error("Got error on ApierV1.GetAccount: ", err.Error()) + } else if reply.Disabled == false { + t.Error("Account should be disabled per fraud detection rules.") + } } func TestTutKamCallsStopPjsuaListener(t *testing.T) { @@ -274,11 +636,9 @@ func TestTutKamCallsStopCgrEngine(t *testing.T) { } } -func TestTutKamCallsStopKamailio(t *testing.T) { +func TestTutKamCallsStopKam(t *testing.T) { if !*testCalls { return } - if err := engine.CallScript(path.Join(*dataDir, "tutorials", "kamevapi", "kamailio", "etc", "init.d", "kamailio"), "stop", *waitRater); err != nil { - t.Fatal(err) - } + engine.KillProcName("kamailio", 1000) }