mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
ApierV2 tests redesign, use build tags and subtests for code compressing and good readability
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
// +build integration
|
||||
|
||||
/*
|
||||
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
|
||||
Copyright (C) ITsysCOM GmbH
|
||||
@@ -30,16 +32,17 @@ import (
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
var testIT = flag.Bool("integration", false, "Perform the tests only on local test environment, not by default.")
|
||||
var (
|
||||
testIT = flag.Bool("integration", false, "Perform the tests only on local test environment, not by default.")
|
||||
dataDir = flag.String("data_dir", "/usr/share/cgrates", "CGR data dir path here")
|
||||
waitRater = flag.Int("wait_rater", 500, "Number of miliseconds to wait for rater to start and cache")
|
||||
)
|
||||
|
||||
var apierCfgPath string
|
||||
var apierCfg *config.CGRConfig
|
||||
var apierRPC *rpc.Client
|
||||
|
||||
func TestApierV2itLoadConfig(t *testing.T) {
|
||||
if !*testIT {
|
||||
return
|
||||
}
|
||||
apierCfgPath = path.Join(*dataDir, "conf", "samples", "tutmysql")
|
||||
if apierCfg, err = config.NewCGRConfigFromFolder(tpCfgPath); err != nil {
|
||||
t.Error(err)
|
||||
@@ -48,9 +51,6 @@ func TestApierV2itLoadConfig(t *testing.T) {
|
||||
|
||||
// Remove data in both rating and accounting db
|
||||
func TestApierV2itResetDataDb(t *testing.T) {
|
||||
if !*testIT {
|
||||
return
|
||||
}
|
||||
if err := engine.InitDataDb(apierCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -58,9 +58,6 @@ func TestApierV2itResetDataDb(t *testing.T) {
|
||||
|
||||
// Wipe out the cdr database
|
||||
func TestApierV2itResetStorDb(t *testing.T) {
|
||||
if !*testIT {
|
||||
return
|
||||
}
|
||||
if err := engine.InitStorDb(apierCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -68,9 +65,6 @@ func TestApierV2itResetStorDb(t *testing.T) {
|
||||
|
||||
// Start CGR Engine
|
||||
func TestApierV2itStartEngine(t *testing.T) {
|
||||
if !*testIT {
|
||||
return
|
||||
}
|
||||
if _, err := engine.StopStartEngine(apierCfgPath, 200); err != nil { // Mongo requires more time to start
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -78,9 +72,6 @@ func TestApierV2itStartEngine(t *testing.T) {
|
||||
|
||||
// Connect rpc client to rater
|
||||
func TestApierV2itRpcConn(t *testing.T) {
|
||||
if !*testIT {
|
||||
return
|
||||
}
|
||||
apierRPC, err = jsonrpc.Dial("tcp", apierCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -88,9 +79,6 @@ func TestApierV2itRpcConn(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestApierV2itAddBalance(t *testing.T) {
|
||||
if !*testIT {
|
||||
return
|
||||
}
|
||||
attrs := &utils.AttrSetBalance{
|
||||
Tenant: "cgrates.org",
|
||||
Account: "dan",
|
||||
@@ -112,9 +100,6 @@ func TestApierV2itAddBalance(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestApierV2itSetAction(t *testing.T) {
|
||||
if !*testIT {
|
||||
return
|
||||
}
|
||||
attrs := utils.AttrSetActions{ActionsId: "DISABLE_ACCOUNT", Actions: []*utils.TPAction{
|
||||
&utils.TPAction{Identifier: engine.DISABLE_ACCOUNT, Weight: 10.0},
|
||||
}}
|
||||
@@ -131,9 +116,6 @@ func TestApierV2itSetAction(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestApierV2itSetAccountActionTriggers(t *testing.T) {
|
||||
if !*testIT {
|
||||
return
|
||||
}
|
||||
attrs := AttrSetAccountActionTriggers{
|
||||
Tenant: "cgrates.org",
|
||||
Account: "dan",
|
||||
@@ -165,9 +147,6 @@ func TestApierV2itSetAccountActionTriggers(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestApierV2itFraudMitigation(t *testing.T) {
|
||||
if !*testIT {
|
||||
return
|
||||
}
|
||||
attrs := &utils.AttrSetBalance{
|
||||
Tenant: "cgrates.org",
|
||||
Account: "dan",
|
||||
@@ -206,9 +185,6 @@ func TestApierV2itFraudMitigation(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestApierV2itKillEngine(t *testing.T) {
|
||||
if !*testIT {
|
||||
return
|
||||
}
|
||||
if err := engine.KillEngine(delay); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// +build integration
|
||||
|
||||
/*
|
||||
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
|
||||
Copyright (C) ITsysCOM GmbH
|
||||
@@ -29,46 +31,87 @@ import (
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
var cdrsMongoCfgPath string
|
||||
var cdrsMongoCfg *config.CGRConfig
|
||||
var cdrsMongoRpc *rpc.Client
|
||||
var cdrsCfgPath string
|
||||
var cdrsCfg *config.CGRConfig
|
||||
var cdrsRpc *rpc.Client
|
||||
var cdrsConfDIR string // run the tests for specific configuration
|
||||
|
||||
func TestV2CdrsMongoInitConfig(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
// subtests to be executed for each confDIR
|
||||
var sTestsCDRsIT = []func(t *testing.T){
|
||||
testV2CDRsInitConfig,
|
||||
testV2CDRsInitDataDb,
|
||||
testV2CDRsInitCdrDb,
|
||||
testV2CDRsInjectUnratedCdr,
|
||||
testV2CDRsStartEngine,
|
||||
testV2CDRsRpcConn,
|
||||
testV2CDRsProcessCdrRated,
|
||||
testV2CDRsProcessCdrRaw,
|
||||
testV2CDRsGetCdrs,
|
||||
testV2CDRsCountCdrs,
|
||||
testV2CDRsProcessPrepaidCdr,
|
||||
testV2CDRsRateWithoutTP,
|
||||
testV2CDRsLoadTariffPlanFromFolder,
|
||||
testV2CDRsRateWithTP,
|
||||
engine.KillEngineTest,
|
||||
}
|
||||
|
||||
// Tests starting here
|
||||
|
||||
func TestCDRsITMySQL(t *testing.T) {
|
||||
cdrsConfDIR = "cdrsv2mysql"
|
||||
for _, stest := range sTestsCDRsIT {
|
||||
t.Run(cdrsConfDIR, stest)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCDRsITpg(t *testing.T) {
|
||||
cdrsConfDIR = "cdrsv2psql"
|
||||
for _, stest := range sTestsCDRsIT {
|
||||
t.Run(cdrsConfDIR, stest)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCDRsITMongo(t *testing.T) {
|
||||
cdrsConfDIR = "cdrsv2mongo"
|
||||
for _, stest := range sTestsCDRsIT {
|
||||
t.Run(cdrsConfDIR, stest)
|
||||
}
|
||||
}
|
||||
|
||||
func testV2CDRsInitConfig(t *testing.T) {
|
||||
var err error
|
||||
cdrsMongoCfgPath = path.Join(*dataDir, "conf", "samples", "cdrsv2mongo")
|
||||
if cdrsMongoCfg, err = config.NewCGRConfigFromFolder(cdrsMongoCfgPath); err != nil {
|
||||
cdrsCfgPath = path.Join(*dataDir, "conf", "samples", cdrsConfDIR)
|
||||
if cdrsCfg, err = config.NewCGRConfigFromFolder(cdrsCfgPath); err != nil {
|
||||
t.Fatal("Got config error: ", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CdrsMongoInitDataDb(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
if err := engine.InitDataDb(cdrsMongoCfg); err != nil {
|
||||
func testV2CDRsInitDataDb(t *testing.T) {
|
||||
if err := engine.InitDataDb(cdrsCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// InitDb so we can rely on count
|
||||
func TestV2CdrsMongoInitCdrDb(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
if err := engine.InitStorDb(cdrsMongoCfg); err != nil {
|
||||
func testV2CDRsInitCdrDb(t *testing.T) {
|
||||
if err := engine.InitStorDb(cdrsCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CdrsMongoInjectUnratedCdr(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
func testV2CDRsInjectUnratedCdr(t *testing.T) {
|
||||
var db engine.CdrStorage
|
||||
switch cdrsConfDIR {
|
||||
case "cdrsv2mysql":
|
||||
db, err = engine.NewMySQLStorage(cdrsCfg.StorDBHost, cdrsCfg.StorDBPort, cdrsCfg.StorDBName, cdrsCfg.StorDBUser, cdrsCfg.StorDBPass,
|
||||
cdrsCfg.StorDBMaxOpenConns, cdrsCfg.StorDBMaxIdleConns)
|
||||
case "cdrsv2psql":
|
||||
db, err = engine.NewPostgresStorage(cdrsCfg.StorDBHost, cdrsCfg.StorDBPort, cdrsCfg.StorDBName, cdrsCfg.StorDBUser, cdrsCfg.StorDBPass,
|
||||
cdrsCfg.StorDBMaxOpenConns, cdrsCfg.StorDBMaxIdleConns)
|
||||
case "cdrsv2mongo":
|
||||
db, err = engine.NewMongoStorage(cdrsCfg.StorDBHost, cdrsCfg.StorDBPort, cdrsCfg.StorDBName,
|
||||
cdrsCfg.StorDBUser, cdrsCfg.StorDBPass, cdrsCfg.StorDBCDRSIndexes, nil, 10)
|
||||
}
|
||||
mongoDb, err := engine.NewMongoStorage(cdrsMongoCfg.StorDBHost, cdrsMongoCfg.StorDBPort, cdrsMongoCfg.StorDBName,
|
||||
cdrsMongoCfg.StorDBUser, cdrsMongoCfg.StorDBPass, cdrsMongoCfg.StorDBCDRSIndexes, nil, 10)
|
||||
if err != nil {
|
||||
t.Error("Error on opening database connection: ", err)
|
||||
return
|
||||
@@ -79,36 +122,26 @@ func TestV2CdrsMongoInjectUnratedCdr(t *testing.T) {
|
||||
SetupTime: time.Date(2015, 11, 21, 10, 47, 24, 0, time.UTC), AnswerTime: time.Date(2015, 11, 21, 10, 47, 26, 0, time.UTC),
|
||||
Usage: time.Duration(10) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"},
|
||||
Cost: -1}
|
||||
if err := mongoDb.SetCDR(strCdr1, false); err != nil {
|
||||
if err := db.SetCDR(strCdr1, false); err != nil {
|
||||
t.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CdrsMongoStartEngine(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
if _, err := engine.StopStartEngine(cdrsMongoCfgPath, *waitRater); err != nil {
|
||||
func testV2CDRsStartEngine(t *testing.T) {
|
||||
if _, err := engine.StopStartEngine(cdrsCfgPath, *waitRater); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Connect rpc client to rater
|
||||
func TestV2CdrsMongoRpcConn(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
var err error
|
||||
cdrsMongoRpc, err = jsonrpc.Dial("tcp", cdrsMongoCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
|
||||
func testV2CDRsRpcConn(t *testing.T) {
|
||||
cdrsRpc, err = jsonrpc.Dial("tcp", cdrsCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
|
||||
if err != nil {
|
||||
t.Fatal("Could not connect to rater: ", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CdrsMongoProcessCdrRated(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
func testV2CDRsProcessCdrRated(t *testing.T) {
|
||||
cdr := &engine.CDR{
|
||||
CGRID: utils.Sha1("dsafdsaf", time.Date(2015, 12, 13, 18, 15, 26, 0, time.UTC).String()), RunID: utils.DEFAULT_RUNID,
|
||||
OrderID: 123, ToR: utils.VOICE, OriginID: "dsafdsaf",
|
||||
@@ -119,17 +152,14 @@ func TestV2CdrsMongoProcessCdrRated(t *testing.T) {
|
||||
Cost: 1.01, CostSource: "TestV2CdrsMongoProcessCdrRated", Rated: true,
|
||||
}
|
||||
var reply string
|
||||
if err := cdrsMongoRpc.Call("CdrsV2.ProcessCdr", cdr, &reply); err != nil {
|
||||
if err := cdrsRpc.Call("CdrsV2.ProcessCdr", cdr, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CdrsMongoProcessCdrRaw(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
func testV2CDRsProcessCdrRaw(t *testing.T) {
|
||||
cdr := &engine.CDR{
|
||||
CGRID: utils.Sha1("abcdeftg", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), OrderID: 123, RunID: utils.MetaRaw,
|
||||
ToR: utils.VOICE, OriginID: "abcdeftg",
|
||||
@@ -139,7 +169,7 @@ func TestV2CdrsMongoProcessCdrRaw(t *testing.T) {
|
||||
Usage: time.Duration(10) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"},
|
||||
}
|
||||
var reply string
|
||||
if err := cdrsMongoRpc.Call("CdrsV2.ProcessCdr", cdr, &reply); err != nil {
|
||||
if err := cdrsRpc.Call("CdrsV2.ProcessCdr", cdr, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
@@ -147,54 +177,48 @@ func TestV2CdrsMongoProcessCdrRaw(t *testing.T) {
|
||||
time.Sleep(time.Duration(*waitRater) * time.Millisecond)
|
||||
}
|
||||
|
||||
func TestV2CdrsMongoGetCdrs(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
func testV2CDRsGetCdrs(t *testing.T) {
|
||||
var reply []*engine.ExternalCDR
|
||||
req := utils.RPCCDRsFilter{}
|
||||
if err := cdrsMongoRpc.Call("ApierV2.GetCdrs", req, &reply); err != nil {
|
||||
if err := cdrsRpc.Call("ApierV2.GetCdrs", req, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if len(reply) != 4 { // 1 injected, 1 rated, 1 *raw and it's pair in *default run
|
||||
t.Error("Unexpected number of CDRs returned: ", len(reply))
|
||||
}
|
||||
// CDRs with rating errors
|
||||
req = utils.RPCCDRsFilter{RunIDs: []string{utils.META_DEFAULT}, MinCost: utils.Float64Pointer(-1.0), MaxCost: utils.Float64Pointer(0.0)}
|
||||
if err := cdrsMongoRpc.Call("ApierV2.GetCdrs", req, &reply); err != nil {
|
||||
if err := cdrsRpc.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: ", reply)
|
||||
}
|
||||
// CDRs Rated
|
||||
req = utils.RPCCDRsFilter{RunIDs: []string{utils.META_DEFAULT}}
|
||||
if err := cdrsMongoRpc.Call("ApierV2.GetCdrs", req, &reply); err != nil {
|
||||
if err := cdrsRpc.Call("ApierV2.GetCdrs", req, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if len(reply) != 2 {
|
||||
t.Error("Unexpected number of CDRs returned: ", reply)
|
||||
}
|
||||
// Raw CDRs
|
||||
req = utils.RPCCDRsFilter{RunIDs: []string{utils.MetaRaw}}
|
||||
if err := cdrsMongoRpc.Call("ApierV2.GetCdrs", req, &reply); err != nil {
|
||||
if err := cdrsRpc.Call("ApierV2.GetCdrs", req, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if len(reply) != 2 {
|
||||
t.Error("Unexpected number of CDRs returned: ", reply)
|
||||
}
|
||||
// Skip Errors
|
||||
req = utils.RPCCDRsFilter{RunIDs: []string{utils.META_DEFAULT}, MinCost: utils.Float64Pointer(0.0), MaxCost: utils.Float64Pointer(-1.0)}
|
||||
if err := cdrsMongoRpc.Call("ApierV2.GetCdrs", req, &reply); err != nil {
|
||||
if err := cdrsRpc.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: ", reply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CdrsMongoCountCdrs(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
func testV2CDRsCountCdrs(t *testing.T) {
|
||||
var reply int64
|
||||
req := utils.AttrGetCdrs{}
|
||||
if err := cdrsMongoRpc.Call("ApierV2.CountCdrs", req, &reply); err != nil {
|
||||
if err := cdrsRpc.Call("ApierV2.CountCdrs", req, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != 4 {
|
||||
t.Error("Unexpected number of CDRs returned: ", reply)
|
||||
@@ -202,10 +226,7 @@ func TestV2CdrsMongoCountCdrs(t *testing.T) {
|
||||
}
|
||||
|
||||
// Make sure *prepaid does not block until finding previous costs
|
||||
func TestV2CdrsMongoProcessPrepaidCdr(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
func testV2CDRsProcessPrepaidCdr(t *testing.T) {
|
||||
var reply string
|
||||
cdrs := []*engine.CDR{
|
||||
&engine.CDR{CGRID: utils.Sha1("dsafdsaf2", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), OrderID: 123, ToR: utils.VOICE, OriginID: "dsafdsaf",
|
||||
@@ -229,7 +250,7 @@ func TestV2CdrsMongoProcessPrepaidCdr(t *testing.T) {
|
||||
}
|
||||
tStart := time.Now()
|
||||
for _, cdr := range cdrs {
|
||||
if err := cdrsMongoRpc.Call("CdrsV2.ProcessCdr", cdr, &reply); err != nil {
|
||||
if err := cdrsRpc.Call("CdrsV2.ProcessCdr", cdr, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
@@ -240,15 +261,12 @@ func TestV2CdrsMongoProcessPrepaidCdr(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CdrsMongoRateWithoutTP(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
func testV2CDRsRateWithoutTP(t *testing.T) {
|
||||
rawCdrCGRID := utils.Sha1("bbb1", time.Date(2015, 11, 21, 10, 47, 24, 0, time.UTC).String())
|
||||
// Rate the injected CDR, should not rate it since we have no TP loaded
|
||||
attrs := utils.AttrRateCdrs{CgrIds: []string{rawCdrCGRID}}
|
||||
var reply string
|
||||
if err := cdrsMongoRpc.Call("CdrsV2.RateCdrs", attrs, &reply); err != nil {
|
||||
if err := cdrsRpc.Call("CdrsV2.RateCdrs", attrs, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
@@ -256,7 +274,7 @@ func TestV2CdrsMongoRateWithoutTP(t *testing.T) {
|
||||
time.Sleep(time.Duration(*waitRater) * time.Millisecond)
|
||||
var cdrs []*engine.ExternalCDR
|
||||
req := utils.RPCCDRsFilter{CGRIDs: []string{rawCdrCGRID}, RunIDs: []string{utils.META_DEFAULT}}
|
||||
if err := cdrsMongoRpc.Call("ApierV2.GetCdrs", req, &cdrs); err != nil {
|
||||
if err := cdrsRpc.Call("ApierV2.GetCdrs", req, &cdrs); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if len(cdrs) != 1 { // Injected CDR did not have a charging run
|
||||
t.Error("Unexpected number of CDRs returned: ", len(cdrs))
|
||||
@@ -267,29 +285,20 @@ func TestV2CdrsMongoRateWithoutTP(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CdrsMongoLoadTariffPlanFromFolder(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
func testV2CDRsLoadTariffPlanFromFolder(t *testing.T) {
|
||||
var loadInst utils.LoadInstance
|
||||
attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "tutorial")}
|
||||
if err := cdrsMongoRpc.Call("ApierV2.LoadTariffPlanFromFolder", attrs, &loadInst); err != nil {
|
||||
if err := cdrsRpc.Call("ApierV2.LoadTariffPlanFromFolder", attrs, &loadInst); err != nil {
|
||||
t.Error(err)
|
||||
} else if loadInst.RatingLoadID == "" || loadInst.AccountingLoadID == "" {
|
||||
// ReThink load instance
|
||||
//t.Error("Empty loadId received, loadInstance: ", loadInst)
|
||||
}
|
||||
time.Sleep(time.Duration(*waitRater) * time.Millisecond) // Give time for scheduler to execute topups
|
||||
}
|
||||
|
||||
func TestV2CdrsMongoRateWithTP(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
func testV2CDRsRateWithTP(t *testing.T) {
|
||||
rawCdrCGRID := utils.Sha1("bbb1", time.Date(2015, 11, 21, 10, 47, 24, 0, time.UTC).String())
|
||||
attrs := utils.AttrRateCdrs{CgrIds: []string{rawCdrCGRID}}
|
||||
var reply string
|
||||
if err := cdrsMongoRpc.Call("CdrsV2.RateCdrs", attrs, &reply); err != nil {
|
||||
if err := cdrsRpc.Call("CdrsV2.RateCdrs", attrs, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
@@ -297,7 +306,7 @@ func TestV2CdrsMongoRateWithTP(t *testing.T) {
|
||||
time.Sleep(time.Duration(*waitRater) * time.Millisecond)
|
||||
var cdrs []*engine.ExternalCDR
|
||||
req := utils.RPCCDRsFilter{CGRIDs: []string{rawCdrCGRID}, RunIDs: []string{utils.META_DEFAULT}}
|
||||
if err := cdrsMongoRpc.Call("ApierV2.GetCdrs", req, &cdrs); err != nil {
|
||||
if err := cdrsRpc.Call("ApierV2.GetCdrs", req, &cdrs); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if len(cdrs) != 1 {
|
||||
t.Error("Unexpected number of CDRs returned: ", len(cdrs))
|
||||
@@ -307,12 +316,3 @@ func TestV2CdrsMongoRateWithTP(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CdrsMongoKillEngine(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
if err := engine.KillEngine(*waitRater); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
@@ -1,397 +0,0 @@
|
||||
/*
|
||||
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
|
||||
Copyright (C) ITsysCOM GmbH
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package v2
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"net/rpc"
|
||||
"net/rpc/jsonrpc"
|
||||
"path"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
var testLocal = flag.Bool("local", false, "Perform the tests only on local test environment, not by default.") // This flag will be passed here via "go test -local" args
|
||||
var dataDir = flag.String("data_dir", "/usr/share/cgrates", "CGR data dir path here")
|
||||
var waitRater = flag.Int("wait_rater", 500, "Number of miliseconds to wait for rater to start and cache")
|
||||
|
||||
var cdrsCfgPath string
|
||||
var cdrsCfg *config.CGRConfig
|
||||
var cdrsRpc *rpc.Client
|
||||
|
||||
func TestV2CDRsMySQLInitConfig(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
var err error
|
||||
cdrsCfgPath = path.Join(*dataDir, "conf", "samples", "cdrsv2mysql")
|
||||
if cdrsCfg, err = config.NewCGRConfigFromFolder(cdrsCfgPath); err != nil {
|
||||
t.Fatal("Got config error: ", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CDRsMySQLInitDataDb(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
if err := engine.InitDataDb(cdrsCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// InitDb so we can rely on count
|
||||
func TestV2CDRsMySQLInitCdrDb(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
if err := engine.InitStorDb(cdrsCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CDRsMySQLInjectUnratedCdr(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
mysqlDb, err := engine.NewMySQLStorage(cdrsCfg.StorDBHost, cdrsCfg.StorDBPort, cdrsCfg.StorDBName, cdrsCfg.StorDBUser, cdrsCfg.StorDBPass,
|
||||
cdrsCfg.StorDBMaxOpenConns, cdrsCfg.StorDBMaxIdleConns)
|
||||
if err != nil {
|
||||
t.Error("Error on opening database connection: ", err)
|
||||
return
|
||||
}
|
||||
strCdr1 := &engine.CDR{CGRID: utils.Sha1("bbb1", time.Date(2015, 11, 21, 10, 47, 24, 0, time.UTC).String()), RunID: utils.MetaRaw,
|
||||
ToR: utils.VOICE, OriginID: "bbb1", OriginHost: "192.168.1.1", Source: "TestV2CDRsMySQLInjectUnratedCdr", RequestType: utils.META_RATED,
|
||||
Direction: "*out", Tenant: "cgrates.org", Category: "call", Account: "1001", Subject: "1001", Destination: "1002",
|
||||
SetupTime: time.Date(2015, 11, 21, 10, 47, 24, 0, time.UTC), AnswerTime: time.Date(2015, 11, 21, 10, 47, 26, 0, time.UTC),
|
||||
Usage: time.Duration(10) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"},
|
||||
Cost: -1}
|
||||
if err := mysqlDb.SetCDR(strCdr1, false); err != nil {
|
||||
t.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CDRsMySQLStartEngine(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
if _, err := engine.StopStartEngine(cdrsCfgPath, *waitRater); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Connect rpc client to rater
|
||||
func TestV2CDRsMySQLRpcConn(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
var err error
|
||||
cdrsRpc, err = jsonrpc.Dial("tcp", cdrsCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
|
||||
if err != nil {
|
||||
t.Fatal("Could not connect to rater: ", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CDRsMySQLProcessCdrRated(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
cdr := &engine.CDR{
|
||||
CGRID: utils.Sha1("dsafdsaf", time.Date(2015, 12, 13, 18, 15, 26, 0, time.UTC).String()), RunID: utils.DEFAULT_RUNID,
|
||||
OrderID: 123, ToR: utils.VOICE, OriginID: "dsafdsaf",
|
||||
OriginHost: "192.168.1.1", Source: "TestV2CDRsMySQLProcessCdrRated", RequestType: utils.META_RATED, Direction: "*out", Tenant: "cgrates.org", Category: "call",
|
||||
Account: "1001", Subject: "1001", Destination: "1002",
|
||||
SetupTime: time.Date(2015, 12, 13, 18, 15, 26, 0, time.UTC), AnswerTime: time.Date(2015, 12, 13, 18, 15, 26, 0, time.UTC),
|
||||
Usage: time.Duration(10) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"},
|
||||
Cost: 1.01, CostSource: "TestV2CDRsMySQLProcessCdrRated", Rated: true,
|
||||
}
|
||||
var reply string
|
||||
if err := cdrsRpc.Call("CdrsV2.ProcessCdr", cdr, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CDRsMySQLProcessCdrRaw(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
cdr := &engine.CDR{
|
||||
CGRID: utils.Sha1("abcdeftg", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), OrderID: 123, RunID: utils.MetaRaw,
|
||||
ToR: utils.VOICE, OriginID: "abcdeftg",
|
||||
OriginHost: "192.168.1.1", Source: "TestV2CDRsMySQLProcessCdrRaw", RequestType: utils.META_RATED, Direction: "*out", Tenant: "cgrates.org", Category: "call",
|
||||
Account: "1002", Subject: "1002", Destination: "1002",
|
||||
SetupTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), AnswerTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC),
|
||||
Usage: time.Duration(10) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"},
|
||||
}
|
||||
var reply string
|
||||
if err := cdrsRpc.Call("CdrsV2.ProcessCdr", cdr, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
}
|
||||
time.Sleep(time.Duration(*waitRater) * time.Millisecond)
|
||||
}
|
||||
|
||||
func TestV2CDRsMySQLGetCdrs(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
var reply []*engine.ExternalCDR
|
||||
req := utils.RPCCDRsFilter{}
|
||||
if err := cdrsRpc.Call("ApierV2.GetCdrs", req, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if len(reply) != 4 { // 1 injected, 1 rated, 1 *raw and it's pair in *default run
|
||||
t.Error("Unexpected number of CDRs returned: ", len(reply))
|
||||
}
|
||||
// CDRs with rating errors
|
||||
req = utils.RPCCDRsFilter{RunIDs: []string{utils.META_DEFAULT}, MinCost: utils.Float64Pointer(-1.0), MaxCost: utils.Float64Pointer(0.0)}
|
||||
if err := cdrsRpc.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: ", reply)
|
||||
}
|
||||
// CDRs Rated
|
||||
req = utils.RPCCDRsFilter{RunIDs: []string{utils.META_DEFAULT}}
|
||||
if err := cdrsRpc.Call("ApierV2.GetCdrs", req, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if len(reply) != 2 {
|
||||
t.Error("Unexpected number of CDRs returned: ", reply)
|
||||
}
|
||||
// Raw CDRs
|
||||
req = utils.RPCCDRsFilter{RunIDs: []string{utils.MetaRaw}}
|
||||
if err := cdrsRpc.Call("ApierV2.GetCdrs", req, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if len(reply) != 2 {
|
||||
t.Error("Unexpected number of CDRs returned: ", reply)
|
||||
}
|
||||
// Skip Errors
|
||||
req = utils.RPCCDRsFilter{RunIDs: []string{utils.META_DEFAULT}, MinCost: utils.Float64Pointer(0.0), MaxCost: utils.Float64Pointer(-1.0)}
|
||||
if err := cdrsRpc.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: ", reply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CDRsMySQLCountCdrs(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
var reply int64
|
||||
req := utils.AttrGetCdrs{}
|
||||
if err := cdrsRpc.Call("ApierV2.CountCdrs", req, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != 4 {
|
||||
t.Error("Unexpected number of CDRs returned: ", reply)
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure *prepaid does not block until finding previous costs
|
||||
func TestV2CDRsMySQLProcessPrepaidCdr(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
var reply string
|
||||
cdrs := []*engine.CDR{
|
||||
&engine.CDR{CGRID: utils.Sha1("dsafdsaf2", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), OrderID: 123, ToR: utils.VOICE, OriginID: "dsafdsaf",
|
||||
OriginHost: "192.168.1.1", Source: "TestV2CDRsMySQLProcessPrepaidCdr1", RequestType: utils.META_PREPAID, Direction: "*out", Tenant: "cgrates.org", Category: "call", Account: "1001", Subject: "1001", Destination: "1002",
|
||||
SetupTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), AnswerTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), RunID: utils.DEFAULT_RUNID,
|
||||
Usage: time.Duration(10) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, Cost: 1.01, Rated: true,
|
||||
},
|
||||
&engine.CDR{CGRID: utils.Sha1("abcdeftg2", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), OrderID: 123, ToR: utils.VOICE, OriginID: "dsafdsaf",
|
||||
OriginHost: "192.168.1.1", Source: "TestV2CDRsMySQLProcessPrepaidCdr2", RequestType: utils.META_PREPAID, Direction: "*out", Tenant: "cgrates.org", Category: "call", Account: "1002", Subject: "1002", Destination: "1002",
|
||||
SetupTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), AnswerTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), RunID: utils.DEFAULT_RUNID,
|
||||
Usage: time.Duration(10) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, Cost: 1.01,
|
||||
},
|
||||
&engine.CDR{CGRID: utils.Sha1("aererfddf2", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), OrderID: 123, ToR: utils.VOICE, OriginID: "dsafdsaf",
|
||||
OriginHost: "192.168.1.1", Source: "TestV2CDRsMySQLProcessPrepaidCdr3", RequestType: utils.META_PREPAID, Direction: "*out", Tenant: "cgrates.org", Category: "call", Account: "1003", Subject: "1003", Destination: "1002",
|
||||
SetupTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), AnswerTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), RunID: utils.DEFAULT_RUNID,
|
||||
Usage: time.Duration(10) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, Cost: 1.01,
|
||||
},
|
||||
}
|
||||
tStart := time.Now()
|
||||
for _, cdr := range cdrs {
|
||||
if err := cdrsRpc.Call("CdrsV2.ProcessCdr", cdr, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
}
|
||||
}
|
||||
if processDur := time.Now().Sub(tStart); processDur > 1*time.Second {
|
||||
t.Error("Unexpected processing time", processDur)
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CDRsMySQLRateWithoutTP(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
//"d32a571d7bcbc6700fd35c1c0c5c6f458a62e260"
|
||||
rawCdrCGRID := utils.Sha1("bbb1", time.Date(2015, 11, 21, 10, 47, 24, 0, time.UTC).String())
|
||||
// Rate the injected CDR, should not rate it since we have no TP loaded
|
||||
attrs := utils.AttrRateCdrs{CgrIds: []string{rawCdrCGRID}}
|
||||
var reply string
|
||||
if err := cdrsRpc.Call("CdrsV2.RateCdrs", attrs, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
}
|
||||
var cdrs []*engine.ExternalCDR
|
||||
req := utils.RPCCDRsFilter{CGRIDs: []string{rawCdrCGRID}, RunIDs: []string{utils.META_DEFAULT}}
|
||||
if err := cdrsRpc.Call("ApierV2.GetCdrs", req, &cdrs); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if len(cdrs) != 1 { // Injected CDR did not have a charging run
|
||||
t.Error("Unexpected number of CDRs returned: ", len(cdrs))
|
||||
} else {
|
||||
if cdrs[0].Cost != -1 {
|
||||
t.Errorf("Unexpected CDR returned: %+v", cdrs[0])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CDRsMySQLLoadTariffPlanFromFolder(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
var loadInst utils.LoadInstance
|
||||
attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "tutorial")}
|
||||
if err := cdrsRpc.Call("ApierV2.LoadTariffPlanFromFolder", attrs, &loadInst); err != nil {
|
||||
t.Error(err)
|
||||
} else if loadInst.RatingLoadID == "" || loadInst.AccountingLoadID == "" {
|
||||
// ReThink load instance
|
||||
//t.Error("Empty loadId received, loadInstance: ", loadInst)
|
||||
}
|
||||
time.Sleep(time.Duration(*waitRater) * time.Millisecond) // Give time for scheduler to execute topups
|
||||
}
|
||||
|
||||
func TestV2CDRsMySQLRateWithTP(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
rawCdrCGRID := utils.Sha1("bbb1", time.Date(2015, 11, 21, 10, 47, 24, 0, time.UTC).String())
|
||||
attrs := utils.AttrRateCdrs{CgrIds: []string{rawCdrCGRID}}
|
||||
var reply string
|
||||
if err := cdrsRpc.Call("CdrsV2.RateCdrs", attrs, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
}
|
||||
var cdrs []*engine.ExternalCDR
|
||||
req := utils.RPCCDRsFilter{CGRIDs: []string{rawCdrCGRID}, RunIDs: []string{utils.META_DEFAULT}}
|
||||
if err := cdrsRpc.Call("ApierV2.GetCdrs", req, &cdrs); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if len(cdrs) != 1 {
|
||||
t.Error("Unexpected number of CDRs returned: ", len(cdrs))
|
||||
} else {
|
||||
if cdrs[0].Cost != 0.3 {
|
||||
t.Errorf("Unexpected CDR returned: %+v", cdrs[0])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// Benchmark speed of processing 1000 CDRs
|
||||
func TestV2CDRsMySQLProcessRatedExternalCdrBenchmark(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
cdr := &engine.ExternalCDR{ToR: utils.VOICE,
|
||||
OriginID: "benchratedcdr", OriginHost: "192.168.1.1", Source: utils.UNIT_TEST, RequestType: utils.META_RATED, Direction: utils.OUT,
|
||||
Tenant: "cgrates.org", Category: "call", Account: "1003", Subject: "1003", Destination: "1001", Supplier: "SUPPL1",
|
||||
SetupTime: "2014-08-04T13:00:00Z", AnswerTime: "2014-08-04T13:00:07Z",
|
||||
Usage: "15", PDD: "7.0", ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"},
|
||||
}
|
||||
var reply string
|
||||
tStart := time.Now()
|
||||
nrCdrs := 1000
|
||||
for i := 0; i < nrCdrs; i++ {
|
||||
cdr.OriginID = "benchratedcdr" + strconv.Itoa(i)
|
||||
if err := cdrsRpc.Call("CdrsV2.ProcessExternalCdr", cdr, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
}
|
||||
}
|
||||
if durExec := time.Now().Sub(tStart); durExec > time.Duration(1)*time.Second {
|
||||
t.Errorf("Processing of %d rated CDRs took: %v", nrCdrs, durExec)
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark speed of re-rating 1000 CDRs
|
||||
func TestV2CDRsMySQLReRateWithTPBenchmark(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
var nrCdrs int64
|
||||
req := utils.AttrRateCdrs{RerateRated: true, RerateErrors: true}
|
||||
if err := cdrsRpc.Call("ApierV2.CountCdrs", req, &nrCdrs); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
}
|
||||
tStart := time.Now()
|
||||
var reply string
|
||||
if err := cdrsRpc.Call("CdrsV2.RateCdrs", req, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
}
|
||||
if durExec := time.Now().Sub(tStart); durExec > time.Duration(1)*time.Second {
|
||||
t.Errorf("Rerating of %d rated CDRs took: %v", nrCdrs, durExec)
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark speed of processing 1000 postpaid CDRs
|
||||
func TestV2CDRsMySQLProcessPostpaidExternalCdrBenchmark(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
cdr := &engine.ExternalCDR{ToR: utils.VOICE,
|
||||
OriginID: "benchpostpaidcdr", OriginHost: "192.168.1.1", Source: utils.UNIT_TEST, RequestType: utils.META_POSTPAID, Direction: utils.OUT,
|
||||
Tenant: "cgrates.org", Category: "call", Account: "1001", Subject: "1001", Destination: "1002", Supplier: "SUPPL1",
|
||||
SetupTime: "2014-08-04T13:00:00Z", AnswerTime: "2014-08-04T13:00:07Z",
|
||||
Usage: "15", PDD: "7.0", ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"},
|
||||
}
|
||||
var reply string
|
||||
tStart := time.Now()
|
||||
nrCdrs := 1000
|
||||
for i := 0; i < nrCdrs; i++ {
|
||||
cdr.OriginID = "benchpostpaidcdr" + strconv.Itoa(i)
|
||||
if err := cdrsRpc.Call("CdrsV2.ProcessExternalCdr", cdr, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
}
|
||||
}
|
||||
if durExec := time.Now().Sub(tStart); durExec > time.Duration(1)*time.Second {
|
||||
t.Errorf("Processing of %d postpaid CDRs took: %v", nrCdrs, durExec)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
func TestV2CDRsMySQLKillEngine(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
if err := engine.KillEngine(*waitRater); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
@@ -1,393 +0,0 @@
|
||||
/*
|
||||
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
|
||||
Copyright (C) ITsysCOM GmbH
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package v2
|
||||
|
||||
import (
|
||||
"net/rpc"
|
||||
"net/rpc/jsonrpc"
|
||||
"os/exec"
|
||||
"path"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
var cdrsPsqlCfgPath string
|
||||
var cdrsPsqlCfg *config.CGRConfig
|
||||
var cdrsPsqlRpc *rpc.Client
|
||||
var cmdEngineCdrPsql *exec.Cmd
|
||||
|
||||
func TestV2CDRsPSQLInitConfig(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
var err error
|
||||
cdrsPsqlCfgPath = path.Join(*dataDir, "conf", "samples", "cdrsv2psql")
|
||||
if cdrsPsqlCfg, err = config.NewCGRConfigFromFolder(cdrsPsqlCfgPath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CDRsPSQLInitDataDb(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
if err := engine.InitDataDb(cdrsPsqlCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// InitDb so we can rely on count
|
||||
func TestV2CDRsPSQLInitCdrDb(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
if err := engine.InitStorDb(cdrsPsqlCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CDRsPSQLInjectUnratedCdr(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
psqlDb, err := engine.NewPostgresStorage(cdrsPsqlCfg.StorDBHost, cdrsPsqlCfg.StorDBPort, cdrsPsqlCfg.StorDBName, cdrsPsqlCfg.StorDBUser, cdrsPsqlCfg.StorDBPass,
|
||||
cdrsPsqlCfg.StorDBMaxOpenConns, cdrsPsqlCfg.StorDBMaxIdleConns)
|
||||
if err != nil {
|
||||
t.Error("Error on opening database connection: ", err)
|
||||
return
|
||||
}
|
||||
strCdr1 := &engine.CDR{CGRID: utils.Sha1("bbb1", time.Date(2015, 11, 21, 10, 47, 24, 0, time.UTC).String()), RunID: utils.MetaRaw,
|
||||
ToR: utils.VOICE, OriginID: "bbb1", OriginHost: "192.168.1.1", Source: "TestV2CDRsPSQLInjectUnratedCdr", RequestType: utils.META_RATED,
|
||||
Direction: "*out", Tenant: "cgrates.org", Category: "call", Account: "1001", Subject: "1001", Destination: "1002",
|
||||
SetupTime: time.Date(2015, 11, 21, 10, 47, 24, 0, time.UTC), AnswerTime: time.Date(2015, 11, 21, 10, 47, 26, 0, time.UTC),
|
||||
Usage: time.Duration(10) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"},
|
||||
Cost: -1}
|
||||
if err := psqlDb.SetCDR(strCdr1, false); err != nil {
|
||||
t.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CDRsPSQLStartEngine(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
if _, err := engine.StopStartEngine(cdrsPsqlCfgPath, *waitRater); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Connect rpc client to rater
|
||||
func TestV2CDRsPSQLRpcConn(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
var err error
|
||||
cdrsPsqlRpc, err = jsonrpc.Dial("tcp", cdrsPsqlCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
|
||||
if err != nil {
|
||||
t.Fatal("Could not connect to rater: ", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CDRsPSQLProcessCdrRated(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
cdr := &engine.CDR{
|
||||
CGRID: utils.Sha1("dsafdsaf", time.Date(2015, 12, 13, 18, 15, 26, 0, time.UTC).String()), RunID: utils.DEFAULT_RUNID,
|
||||
OrderID: 123, ToR: utils.VOICE, OriginID: "dsafdsaf",
|
||||
OriginHost: "192.168.1.1", Source: "TestV2CDRsPSQLProcessCdrRated", RequestType: utils.META_RATED, Direction: "*out", Tenant: "cgrates.org", Category: "call",
|
||||
Account: "1001", Subject: "1001", Destination: "1002",
|
||||
SetupTime: time.Date(2015, 12, 13, 18, 15, 26, 0, time.UTC), AnswerTime: time.Date(2015, 12, 13, 18, 15, 26, 0, time.UTC),
|
||||
Usage: time.Duration(10) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"},
|
||||
Cost: 1.01, CostSource: "TestV2CDRsPSQLProcessCdrRated", Rated: true,
|
||||
}
|
||||
var reply string
|
||||
if err := cdrsPsqlRpc.Call("CdrsV2.ProcessCDR", cdr, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CDRsPSQLProcessCdrRaw(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
cdr := &engine.CDR{
|
||||
CGRID: utils.Sha1("abcdeftg", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), OrderID: 123, RunID: utils.MetaRaw,
|
||||
ToR: utils.VOICE, OriginID: "abcdeftg",
|
||||
OriginHost: "192.168.1.1", Source: "TestV2CDRsPSQLProcessCdrRaw", RequestType: utils.META_RATED, Direction: "*out", Tenant: "cgrates.org", Category: "call",
|
||||
Account: "1002", Subject: "1002", Destination: "1002",
|
||||
SetupTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), AnswerTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC),
|
||||
Usage: time.Duration(10) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"},
|
||||
}
|
||||
var reply string
|
||||
if err := cdrsPsqlRpc.Call("CdrsV2.ProcessCDR", cdr, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
}
|
||||
time.Sleep(time.Duration(*waitRater) * time.Millisecond)
|
||||
}
|
||||
|
||||
func TestV2CDRsPSQLGetCdrs(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
var reply []*engine.ExternalCDR
|
||||
req := utils.RPCCDRsFilter{}
|
||||
if err := cdrsPsqlRpc.Call("ApierV2.GetCdrs", req, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if len(reply) != 4 { // 1 injected, 1 rated, 1 *raw and it's pair in *default run
|
||||
t.Error("Unexpected number of CDRs returned: ", len(reply))
|
||||
}
|
||||
// CDRs with rating errors
|
||||
req = utils.RPCCDRsFilter{RunIDs: []string{utils.META_DEFAULT}, MinCost: utils.Float64Pointer(-1.0), MaxCost: utils.Float64Pointer(0.0)}
|
||||
if err := cdrsPsqlRpc.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: ", reply)
|
||||
}
|
||||
// CDRs Rated
|
||||
req = utils.RPCCDRsFilter{RunIDs: []string{utils.META_DEFAULT}}
|
||||
if err := cdrsPsqlRpc.Call("ApierV2.GetCdrs", req, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if len(reply) != 2 {
|
||||
t.Error("Unexpected number of CDRs returned: ", reply)
|
||||
}
|
||||
// Raw CDRs
|
||||
req = utils.RPCCDRsFilter{RunIDs: []string{utils.MetaRaw}}
|
||||
if err := cdrsPsqlRpc.Call("ApierV2.GetCdrs", req, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if len(reply) != 2 {
|
||||
t.Error("Unexpected number of CDRs returned: ", reply)
|
||||
}
|
||||
// Skip Errors
|
||||
req = utils.RPCCDRsFilter{RunIDs: []string{utils.META_DEFAULT}, MinCost: utils.Float64Pointer(0.0), MaxCost: utils.Float64Pointer(-1.0)}
|
||||
if err := cdrsPsqlRpc.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: ", reply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CDRsPSQLCountCdrs(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
var reply int64
|
||||
req := utils.AttrGetCdrs{}
|
||||
if err := cdrsPsqlRpc.Call("ApierV2.CountCdrs", req, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != 4 {
|
||||
t.Error("Unexpected number of CDRs returned: ", reply)
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure *prepaid does not block until finding previous costs
|
||||
func TestV2CDRsPSQLProcessPrepaidCdr(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
var reply string
|
||||
cdrs := []*engine.CDR{
|
||||
&engine.CDR{CGRID: utils.Sha1("dsafdsaf2", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), OrderID: 123, ToR: utils.VOICE, OriginID: "dsafdsaf",
|
||||
OriginHost: "192.168.1.1", Source: "TestV2CDRsPSQLProcessPrepaidCdr1", RequestType: utils.META_PREPAID, Direction: "*out", Tenant: "cgrates.org", Category: "call", Account: "1001", Subject: "1001", Destination: "1002",
|
||||
SetupTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), AnswerTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), RunID: utils.DEFAULT_RUNID,
|
||||
Usage: time.Duration(10) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, Cost: 1.01, Rated: true,
|
||||
},
|
||||
&engine.CDR{CGRID: utils.Sha1("abcdeftg2", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), OrderID: 123, ToR: utils.VOICE, OriginID: "dsafdsaf",
|
||||
OriginHost: "192.168.1.1", Source: "TestV2CDRsPSQLProcessPrepaidCdr2", RequestType: utils.META_PREPAID, Direction: "*out", Tenant: "cgrates.org", Category: "call", Account: "1002", Subject: "1002", Destination: "1002",
|
||||
SetupTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), AnswerTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), RunID: utils.DEFAULT_RUNID,
|
||||
Usage: time.Duration(10) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, Cost: 1.01,
|
||||
},
|
||||
&engine.CDR{CGRID: utils.Sha1("aererfddf2", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), OrderID: 123, ToR: utils.VOICE, OriginID: "dsafdsaf",
|
||||
OriginHost: "192.168.1.1", Source: "TestV2CDRsPSQLProcessPrepaidCdr3", RequestType: utils.META_PREPAID, Direction: "*out", Tenant: "cgrates.org", Category: "call", Account: "1003", Subject: "1003", Destination: "1002",
|
||||
SetupTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), AnswerTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC), RunID: utils.DEFAULT_RUNID,
|
||||
Usage: time.Duration(10) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, Cost: 1.01,
|
||||
},
|
||||
}
|
||||
tStart := time.Now()
|
||||
for _, cdr := range cdrs {
|
||||
if err := cdrsPsqlRpc.Call("CdrsV2.ProcessCDR", cdr, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
}
|
||||
}
|
||||
if processDur := time.Now().Sub(tStart); processDur > 1*time.Second {
|
||||
t.Error("Unexpected processing time", processDur)
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CDRsPSQLRateWithoutTP(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
rawCdrCGRID := utils.Sha1("bbb1", time.Date(2015, 11, 21, 10, 47, 24, 0, time.UTC).String())
|
||||
// Rate the injected CDR, should not rate it since we have no TP loaded
|
||||
attrs := utils.AttrRateCdrs{CgrIds: []string{rawCdrCGRID}}
|
||||
var reply string
|
||||
if err := cdrsPsqlRpc.Call("CdrsV2.RateCdrs", attrs, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
}
|
||||
var cdrs []*engine.ExternalCDR
|
||||
req := utils.RPCCDRsFilter{CGRIDs: []string{rawCdrCGRID}, RunIDs: []string{utils.META_DEFAULT}}
|
||||
if err := cdrsPsqlRpc.Call("ApierV2.GetCdrs", req, &cdrs); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if len(cdrs) != 1 { // Injected CDR did not have a charging run
|
||||
t.Error("Unexpected number of CDRs returned: ", len(cdrs))
|
||||
} else {
|
||||
if cdrs[0].Cost != -1 {
|
||||
t.Errorf("Unexpected CDR returned: %+v", cdrs[0])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestV2CDRsPSQLLoadTariffPlanFromFolder(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
var loadInst utils.LoadInstance
|
||||
attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "tutorial")}
|
||||
if err := cdrsPsqlRpc.Call("ApierV2.LoadTariffPlanFromFolder", attrs, &loadInst); err != nil {
|
||||
t.Error(err)
|
||||
} else if loadInst.RatingLoadID == "" || loadInst.AccountingLoadID == "" {
|
||||
// ReThink load instance
|
||||
//t.Error("Empty loadId received, loadInstance: ", loadInst)
|
||||
}
|
||||
time.Sleep(time.Duration(*waitRater) * time.Millisecond) // Give time for scheduler to execute topups
|
||||
}
|
||||
|
||||
func TestV2CDRsPSQLRateWithTP(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
rawCdrCGRID := utils.Sha1("bbb1", time.Date(2015, 11, 21, 10, 47, 24, 0, time.UTC).String())
|
||||
attrs := utils.AttrRateCdrs{CgrIds: []string{rawCdrCGRID}}
|
||||
var reply string
|
||||
if err := cdrsPsqlRpc.Call("CdrsV2.RateCdrs", attrs, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
}
|
||||
var cdrs []*engine.ExternalCDR
|
||||
req := utils.RPCCDRsFilter{CGRIDs: []string{rawCdrCGRID}, RunIDs: []string{utils.META_DEFAULT}}
|
||||
if err := cdrsPsqlRpc.Call("ApierV2.GetCdrs", req, &cdrs); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if len(cdrs) != 1 {
|
||||
t.Error("Unexpected number of CDRs returned: ", len(cdrs))
|
||||
} else {
|
||||
if cdrs[0].Cost != 0.3 {
|
||||
t.Errorf("Unexpected CDR returned: %+v", cdrs[0])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// Benchmark speed of processing 1000 CDRs
|
||||
func TestV2CDRsPSQLProcessRatedExternalCdrBenchmark(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
cdr := &engine.ExternalCDR{ToR: utils.VOICE,
|
||||
OriginID: "benchratedcdr", OriginHost: "192.168.1.1", Source: utils.UNIT_TEST, RequestType: utils.META_RATED, Direction: utils.OUT,
|
||||
Tenant: "cgrates.org", Category: "call", Account: "1003", Subject: "1003", Destination: "1001", Supplier: "SUPPL1",
|
||||
SetupTime: "2014-08-04T13:00:00Z", AnswerTime: "2014-08-04T13:00:07Z",
|
||||
Usage: "15", PDD: "7.0", ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"},
|
||||
}
|
||||
var reply string
|
||||
tStart := time.Now()
|
||||
nrCdrs := 1000
|
||||
for i := 0; i < nrCdrs; i++ {
|
||||
cdr.OriginID = "benchratedcdr" + strconv.Itoa(i)
|
||||
if err := cdrsPsqlRpc.Call("CdrsV2.ProcessExternalCdr", cdr, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
}
|
||||
}
|
||||
if durExec := time.Now().Sub(tStart); durExec > time.Duration(1)*time.Second {
|
||||
t.Errorf("Processing of %d rated CDRs took: %v", nrCdrs, durExec)
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark speed of re-rating 1000 CDRs
|
||||
func TestV2CDRsPSQLReRateWithTPBenchmark(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
var nrCdrs int64
|
||||
req := utils.AttrRateCdrs{RerateRated: true, RerateErrors: true}
|
||||
if err := cdrsPsqlRpc.Call("ApierV2.CountCdrs", req, &nrCdrs); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
}
|
||||
tStart := time.Now()
|
||||
var reply string
|
||||
if err := cdrsPsqlRpc.Call("CdrsV2.RateCdrs", req, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
}
|
||||
if durExec := time.Now().Sub(tStart); durExec > time.Duration(1)*time.Second {
|
||||
t.Errorf("Rerating of %d rated CDRs took: %v", nrCdrs, durExec)
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark speed of processing 1000 postpaid CDRs
|
||||
func TestV2CDRsPSQLProcessPostpaidExternalCdrBenchmark(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
cdr := &engine.ExternalCDR{ToR: utils.VOICE,
|
||||
OriginID: "benchpostpaidcdr", OriginHost: "192.168.1.1", Source: utils.UNIT_TEST, RequestType: utils.META_POSTPAID, Direction: utils.OUT,
|
||||
Tenant: "cgrates.org", Category: "call", Account: "1001", Subject: "1001", Destination: "1002", Supplier: "SUPPL1",
|
||||
SetupTime: "2014-08-04T13:00:00Z", AnswerTime: "2014-08-04T13:00:07Z",
|
||||
Usage: "15", PDD: "7.0", ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"},
|
||||
}
|
||||
var reply string
|
||||
tStart := time.Now()
|
||||
nrCdrs := 1000
|
||||
for i := 0; i < nrCdrs; i++ {
|
||||
cdr.OriginID = "benchpostpaidcdr" + strconv.Itoa(i)
|
||||
if err := cdrsPsqlRpc.Call("CdrsV2.ProcessExternalCdr", cdr, &reply); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if reply != utils.OK {
|
||||
t.Error("Unexpected reply received: ", reply)
|
||||
}
|
||||
}
|
||||
if durExec := time.Now().Sub(tStart); durExec > time.Duration(1)*time.Second {
|
||||
t.Errorf("Processing of %d postpaid CDRs took: %v", nrCdrs, durExec)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
func TestV2CDRsPSQLKillEngine(t *testing.T) {
|
||||
if !*testLocal {
|
||||
return
|
||||
}
|
||||
if err := engine.KillEngine(*waitRater); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
// +build integration
|
||||
|
||||
/*
|
||||
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
|
||||
Copyright (C) ITsysCOM GmbH
|
||||
@@ -18,7 +20,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package v2
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"net/rpc"
|
||||
"net/rpc/jsonrpc"
|
||||
"path"
|
||||
@@ -31,26 +32,58 @@ import (
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
var testTP = flag.Bool("tp", false, "Perform the tests for TariffPlans, not by default.") // Separate from integration so we can run on multiple DBs without involving all other tests on each run
|
||||
var configDIR = flag.String("config_dir", "tutmysql", "Relative path towards a config directory under samples prefix")
|
||||
|
||||
var tpCfgPath string
|
||||
var tpCfg *config.CGRConfig
|
||||
var tpRPC *rpc.Client
|
||||
var err error
|
||||
|
||||
var testTPid = "V2TestTPit"
|
||||
var delay int
|
||||
var configDIR string // relative path towards a config directory under samples prefix
|
||||
|
||||
func TestTPitLoadConfig(t *testing.T) {
|
||||
if !*testTP {
|
||||
return
|
||||
var (
|
||||
testTPid = "V2TestTPit"
|
||||
)
|
||||
|
||||
// subtests to be executed for each confDIR
|
||||
var sTestsTutIT = []func(t *testing.T){
|
||||
testTPitLoadConfig,
|
||||
testTPitResetDataDb,
|
||||
testTPitResetStorDb,
|
||||
testTPitStartEngine,
|
||||
testTPitRpcConn,
|
||||
testTPitTimings,
|
||||
testTPitDestinations,
|
||||
engine.KillEngineTest,
|
||||
}
|
||||
|
||||
// Tests starting here
|
||||
|
||||
func TestITMySQLTutorial(t *testing.T) {
|
||||
configDIR = "tutmysql"
|
||||
for _, stest := range sTestsTutIT {
|
||||
t.Run(configDIR, stest)
|
||||
}
|
||||
tpCfgPath = path.Join(*dataDir, "conf", "samples", *configDIR)
|
||||
}
|
||||
|
||||
func TestITpgTutorial(t *testing.T) {
|
||||
configDIR = "tutpostgres"
|
||||
for _, stest := range sTestsTutIT {
|
||||
t.Run(configDIR, stest)
|
||||
}
|
||||
}
|
||||
|
||||
func TestITMongoTutorial(t *testing.T) {
|
||||
configDIR = "tutmongo"
|
||||
for _, stest := range sTestsTutIT {
|
||||
t.Run(configDIR, stest)
|
||||
}
|
||||
}
|
||||
|
||||
func testTPitLoadConfig(t *testing.T) {
|
||||
tpCfgPath = path.Join(*dataDir, "conf", "samples", configDIR)
|
||||
if tpCfg, err = config.NewCGRConfigFromFolder(tpCfgPath); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
switch *configDIR {
|
||||
switch configDIR {
|
||||
case "tutmongo": // Mongo needs more time to reset db, need to investigate
|
||||
delay = 4000
|
||||
default:
|
||||
@@ -59,40 +92,28 @@ func TestTPitLoadConfig(t *testing.T) {
|
||||
}
|
||||
|
||||
// Remove data in both rating and accounting db
|
||||
func TestTPitResetDataDb(t *testing.T) {
|
||||
if !*testTP {
|
||||
return
|
||||
}
|
||||
func testTPitResetDataDb(t *testing.T) {
|
||||
if err := engine.InitDataDb(tpCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Wipe out the cdr database
|
||||
func TestTPitResetStorDb(t *testing.T) {
|
||||
if !*testTP {
|
||||
return
|
||||
}
|
||||
func testTPitResetStorDb(t *testing.T) {
|
||||
if err := engine.InitStorDb(tpCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Start CGR Engine
|
||||
func TestTPitStartEngine(t *testing.T) {
|
||||
if !*testTP {
|
||||
return
|
||||
}
|
||||
func testTPitStartEngine(t *testing.T) {
|
||||
if _, err := engine.StopStartEngine(tpCfgPath, delay); err != nil { // Mongo requires more time to start
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Connect rpc client to rater
|
||||
func TestTPitRpcConn(t *testing.T) {
|
||||
if !*testTP {
|
||||
return
|
||||
}
|
||||
func testTPitRpcConn(t *testing.T) {
|
||||
var err error
|
||||
tpRPC, err = jsonrpc.Dial("tcp", tpCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
|
||||
if err != nil {
|
||||
@@ -100,10 +121,7 @@ func TestTPitRpcConn(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTPitTimings(t *testing.T) {
|
||||
if !*testTP {
|
||||
return
|
||||
}
|
||||
func testTPitTimings(t *testing.T) {
|
||||
// PEAK,*any,*any,*any,1;2;3;4;5,08:00:00
|
||||
tmPeak := &utils.ApierTPTiming{
|
||||
TPid: testTPid,
|
||||
@@ -186,10 +204,7 @@ func TestTPitTimings(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTPitDestinations(t *testing.T) {
|
||||
if !*testTP {
|
||||
return
|
||||
}
|
||||
func testTPitDestinations(t *testing.T) {
|
||||
var reply string
|
||||
// DST_1002,1002
|
||||
dst1002 := &utils.TPDestination{TPid: testTPid, DestinationId: "DST_1002", Prefixes: []string{"1002"}}
|
||||
@@ -237,12 +252,3 @@ func TestTPitDestinations(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestTPitKillEngine(t *testing.T) {
|
||||
if !*testTP {
|
||||
return
|
||||
}
|
||||
if err := engine.KillEngine(delay); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/cgrates/config"
|
||||
@@ -85,6 +86,13 @@ func KillEngine(waitEngine int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// KillEngineTest is included in tests to shutdown the CGRateS processes
|
||||
func KillEngineTest(t *testing.T) {
|
||||
if err := KillEngine(100); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func StopStartEngine(cfgPath string, waitEngine int) (*exec.Cmd, error) {
|
||||
KillEngine(waitEngine)
|
||||
return StartEngine(cfgPath, waitEngine)
|
||||
|
||||
@@ -4,18 +4,9 @@ gen=$?
|
||||
echo 'go test github.com/cgrates/cgrates/apier/v1 -local'
|
||||
go test github.com/cgrates/cgrates/apier/v1 -local
|
||||
ap1=$?
|
||||
echo 'go test github.com/cgrates/cgrates/apier/v2 -local -integration'
|
||||
go test github.com/cgrates/cgrates/apier/v2 -local
|
||||
echo 'go test github.com/cgrates/cgrates/apier/v2 -tags=integration'
|
||||
go test github.com/cgrates/cgrates/apier/v2 -tags=integration
|
||||
ap2=$?
|
||||
echo 'go test github.com/cgrates/cgrates/apier/v2 -tp -config_dir=tutmysql'
|
||||
go test github.com/cgrates/cgrates/apier/v2 -tp -config_dir=tutmysql
|
||||
tpmysql=$?
|
||||
echo 'go test github.com/cgrates/cgrates/apier/v2 -tp -config_dir=tutpostgres'
|
||||
go test github.com/cgrates/cgrates/apier/v2 -tp -config_dir=tutpostgres
|
||||
tppg=$?
|
||||
echo 'go test github.com/cgrates/cgrates/apier/v2 -tp -config_dir=tutmongo'
|
||||
go test github.com/cgrates/cgrates/apier/v2 -tp -config_dir=tutmongo
|
||||
tpmongo=$?
|
||||
echo 'go test github.com/cgrates/cgrates/engine -local -integration'
|
||||
go test github.com/cgrates/cgrates/engine -local -integration
|
||||
en=$?
|
||||
@@ -38,4 +29,4 @@ echo 'go test github.com/cgrates/cgrates/sessionmanager -integration'
|
||||
go test github.com/cgrates/cgrates/sessionmanager -integration
|
||||
smg=$?
|
||||
|
||||
exit $gen && $ap1 && $ap2 && $tpmysql && $tppg && $tpmongo && $en && $cdrc && $cfg && $utl && $gnr && $agts && $smg
|
||||
exit $gen && $ap1 && $ap2 && $en && $cdrc && $cfg && $utl && $gnr && $agts && $smg
|
||||
Reference in New Issue
Block a user