Repair cdrs_post_failover_it_test in general_tests

This commit is contained in:
ionutboangiu
2022-03-23 17:18:14 +02:00
committed by Dan Christian Bogos
parent b40094e2c7
commit 9d3c3e50d3
4 changed files with 357 additions and 325 deletions

View File

@@ -1,86 +1,90 @@
{
// CGRateS Configuration file
//
// Used in apier_local_tests
// Starts rater, cdrs and mediator connecting over internal channel
"general": {
"log_level": 7,
"node_id": "TestFailCDRS",
"poster_attempts": 1, // number of attempts before considering post request failed (eg: *httpPost, CDR exports)
"failed_posts_ttl": "1s", // time to wait before writing the failed posts in a single file
"failed_posts_dir": "/tmp/failed_posts" // directory path where we store failed requests
},
"general": {
"log_level": 7,
"node_id": "TestFailCDRS",
"poster_attempts": 1,
"failed_posts_ttl": "1s",
"failed_posts_dir": "/tmp/failed_posts"
},
"rals": {
"enabled": true, // enable Rater service: <true|false>
"thresholds_conns": ["*localhost"],
},
"rals": {
"enabled": true,
"thresholds_conns": ["*localhost"]
},
"schedulers": {
"enabled": true,
},
"schedulers": {
"enabled": true
},
"cdrs": {
"enabled": true,
"attributes_conns":["*internal"],
"chargers_conns":["*localhost"],
"rals_conns": ["*localhost"],
"stats_conns": ["*localhost"],
"thresholds_conns": ["*localhost"],
"online_cdr_exports": ["s3_test_file"],
"ees_conns": ["*localhost"]
},
"cdrs": {
"enabled": true,
"attributes_conns":["*internal"],
"chargers_conns":["*localhost"],
"rals_conns": ["*localhost"],
"stats_conns": ["*localhost"],
"thresholds_conns": ["*localhost"],
"online_cdr_exports": ["s3_test_file"],
"ees_conns": ["*localhost"]
},
"attributes": {
"enabled": true,
},
"attributes": {
"enabled": true
},
"stats": {
"enabled": true,
"store_interval": "1s",
"thresholds_conns": ["*internal"],
},
"stats": {
"enabled": true,
"store_interval": "1s",
"thresholds_conns": ["*internal"]
},
"thresholds": {
"enabled": true,
"store_interval": "1s",
},
"thresholds": {
"enabled": true,
"store_interval": "1s"
},
"chargers": {
"enabled": true,
"attributes_conns": ["*internal"],
},
"chargers": {
"enabled": true,
"attributes_conns": ["*internal"]
},
"admins": {
"enabled": true,
"scheduler_conns": ["*internal"],
},
"admins": {
"enabled": true,
"scheduler_conns": ["*internal"]
},
"ees": {
"enabled": true,
"attributes_conns": ["*internal"],
"exporters": [
"ees": {
"enabled": true,
"attributes_conns": ["*internal"],
"exporters": [
{
"id": "s3_test_file",
"type": "*s3JSONMap",
"export_path": "http://s3.us-east-2.amazonaws.com/",
"opts": {
"awsRegion": "eu-west-2",
"awsKey": "testkey",
"awsSecret": "testsecret",
"s3BucketID": "cgrates-cdrs"
},
"attempts": 1,
"failed_posts_dir": "/tmp/failed_posts",
"fields":[
{"tag": "*originID", "path": "*exp.*originID", "type": "*variable", "value": "~*opts.*originID"}
]
}
]
},
"loaders": [
{
"id": "s3_test_file",
"type": "*s3JSONMap",
// export_path for s3: "endpoint"
"export_path": "http://s3.us-east-2.amazonaws.com/",
"opts": {
// posible options for s3:
"awsRegion": "eu-west-2",
"awsKey": "testkey",
"awsSecret": "testsecret",
"s3BucketID": "cgrates-cdrs",
// "awsToken": "sessionToken",
},
"attempts": 1,
"fields":[
{"tag": "*originID", "path": "*exp.*originID", "type": "*variable", "value": "~*opts.*originID"},
],
},
"id": "*default",
"enabled": true,
"tenant": "cgrates.org",
"lockfile_path": ".cgr.lck",
"tp_in_dir": "/usr/share/cgrates/tariffplans/testit",
"tp_out_dir": ""
}
]
},
}

View File

@@ -1,94 +1,99 @@
{
// CGRateS Configuration file
//
// Used in apier_local_tests
// Starts rater, cdrs and mediator connecting over internal channel
"general": {
"log_level": 7,
"poster_attempts": 1, // number of attempts before considering post request failed (eg: *httpPost, CDR exports)
"failed_posts_ttl": "1s", // time to wait before writing the failed posts in a single file
"failed_posts_dir": "/tmp/failed_posts" // directory path where we store failed requests
},
"general": {
"log_level": 7,
"poster_attempts": 1,
"failed_posts_ttl": "1s",
"failed_posts_dir": "/tmp/failed_posts"
},
"data_db": {
"db_type": "mongo",
"db_name": "10",
"db_port": 27017,
},
"data_db": {
"db_type": "mongo",
"db_name": "10",
"db_port": 27017
},
"rals": {
"enabled": true, // enable Rater service: <true|false>
"thresholds_conns": ["*localhost"],
},
"rals": {
"enabled": true,
"thresholds_conns": ["*localhost"]
},
"schedulers": {
"enabled": true,
},
"schedulers": {
"enabled": true
},
"cdrs": {
"enabled": true,
"attributes_conns":["*internal"],
"chargers_conns":["*localhost"],
"rals_conns": ["*localhost"],
"stats_conns": ["*localhost"],
"thresholds_conns": ["*localhost"],
"online_cdr_exports": ["s3_test_file"],
"ees_conns": ["*localhost"]
},
"cdrs": {
"enabled": true,
"attributes_conns":["*internal"],
"chargers_conns":["*localhost"],
"rals_conns": ["*localhost"],
"stats_conns": ["*localhost"],
"thresholds_conns": ["*localhost"],
"online_cdr_exports": ["s3_test_file"],
"ees_conns": ["*localhost"]
},
"attributes": {
"enabled": true,
},
"attributes": {
"enabled": true
},
"stats": {
"enabled": true,
"store_interval": "1s",
"thresholds_conns": ["*internal"],
},
"stats": {
"enabled": true,
"store_interval": "1s",
"thresholds_conns": ["*internal"]
},
"thresholds": {
"enabled": true,
"store_interval": "1s",
},
"thresholds": {
"enabled": true,
"store_interval": "1s"
},
"chargers": {
"enabled": true,
"attributes_conns": ["*internal"],
},
"chargers": {
"enabled": true,
"attributes_conns": ["*internal"]
},
"admins": {
"enabled": true,
"scheduler_conns": ["*internal"],
},
"admins": {
"enabled": true,
"scheduler_conns": ["*internal"]
},
"ees": {
"enabled": true,
"attributes_conns": ["*internal"],
"exporters": [
"ees": {
"enabled": true,
"attributes_conns": ["*internal"],
"exporters": [
{
"id": "s3_test_file",
"type": "*s3JSONMap",
"export_path": "http://s3.us-east-2.amazonaws.com/",
"opts": {
"awsRegion": "eu-west-2",
"awsKey": "testkey",
"awsSecret": "testsecret",
"s3BucketID": "cgrates-cdrs"
},
"attempts": 1,
"failed_posts_dir": "/tmp/failed_posts",
"fields":[
{"tag": "*originID", "path": "*exp.*originID", "type": "*variable", "value": "~*opts.*originID"}
]
}
]
},
"loaders": [
{
"id": "s3_test_file",
"type": "*s3JSONMap",
// export_path for s3: "endpoint"
"export_path": "http://s3.us-east-2.amazonaws.com/",
"opts": {
// posible options for s3:
"awsRegion": "eu-west-2",
"awsKey": "testkey",
"awsSecret": "testsecret",
"s3BucketID": "cgrates-cdrs",
// "awsToken": "sessionToken",
},
"attempts": 1,
"fields":[
{"tag": "*originID", "path": "*exp.*originID", "type": "*variable", "value": "~*opts.*originID"},
],
"id": "*default",
"enabled": true,
"tenant": "cgrates.org",
"lockfile_path": ".cgr.lck",
"tp_in_dir": "/usr/share/cgrates/tariffplans/testit",
"tp_out_dir": ""
}
]
},

View File

@@ -83,6 +83,7 @@
// "awsToken": "sessionToken",
},
"attempts": 1,
"failed_posts_dir": "/tmp/failed_posts",
"fields":[
{"tag": "*originID", "path": "*exp.*originID", "type": "*variable", "value": "~*opts.*originID"},
],
@@ -90,5 +91,16 @@
]
},
"loaders": [
{
"id": "*default",
"enabled": true,
"tenant": "cgrates.org",
"lockfile_path": ".cgr.lck",
"tp_in_dir": "/usr/share/cgrates/tariffplans/testit",
"tp_out_dir": ""
}
]
},

View File

@@ -21,199 +21,210 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package general_tests
// import (
// "net/rpc"
// "os"
// "path"
// "testing"
// "time"
import (
"os"
"path"
"testing"
"time"
// "github.com/cgrates/cgrates/config"
// "github.com/cgrates/cgrates/ees"
// "github.com/cgrates/cgrates/engine"
// "github.com/cgrates/cgrates/utils"
// )
"github.com/cgrates/birpc"
"github.com/cgrates/birpc/context"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/ees"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/loaders"
"github.com/cgrates/cgrates/utils"
)
// var (
// cdrsPostFailCfgPath string
// cdrsPostFailCfg *config.CGRConfig
// cdrsPostFailRpc *rpc.Client
// cdrsPostFailConfDIR string // run the tests for specific configuration
var (
cdrsPostFailCfgPath string
cdrsPostFailCfg *config.CGRConfig
cdrsPostFailRpc *birpc.Client
cdrsPostFailConfDIR string // run the tests for specific configuration
// // subtests to be executed for each confDIR
// sTestsCDRsPostFailIT = []func(t *testing.T){
// testCDRsPostFailoverInitConfig,
// testCDRsPostFailoverInitDataDb,
// testCDRsPostFailoverInitCdrDb,
// testCDRsPostFailoverStartEngine,
// testCDRsPostFailoverRpcConn,
// testCDRsPostFailoverLoadTariffPlanFromFolder,
// testCDRsPostFailoverProcessCDR,
// testCDRsPostFailoverToFile,
// testCDRsPostFailoverKillEngine,
// }
// )
// subtests to be executed for each confDIR
sTestsCDRsPostFailIT = []func(t *testing.T){
testCDRsPostFailoverInitConfig,
testCDRsPostFailoverInitDataDb,
testCDRsPostFailoverStartEngine,
testCDRsPostFailoverRpcConn,
testCDRsPostFailoverLoadTariffPlanFromFolder,
testCDRsPostFailoverProcessCDR,
testCDRsPostFailoverToFile,
testCDRsPostFailoverKillEngine,
}
)
// // Tests starting here
// func TestCDRsPostFailoverIT(t *testing.T) {
// switch *dbType {
// case utils.MetaInternal:
// cdrsPostFailConfDIR = "cdrsv_failover_internal"
// case utils.MetaMySQL:
// cdrsPostFailConfDIR = "cdrsv_failover_mysql"
// case utils.MetaMongo:
// cdrsPostFailConfDIR = "cdrsv_failover_mongo"
// case utils.MetaPostgres:
// t.SkipNow()
// default:
// t.Fatal("Unknown Database type")
// }
// Tests starting here
func TestCDRsPostFailoverIT(t *testing.T) {
switch *dbType {
case utils.MetaInternal:
cdrsPostFailConfDIR = "cdrsv_failover_internal"
case utils.MetaMySQL:
cdrsPostFailConfDIR = "cdrsv_failover_mysql"
case utils.MetaMongo:
cdrsPostFailConfDIR = "cdrsv_failover_mongo"
case utils.MetaPostgres:
t.SkipNow()
default:
t.Fatal("Unknown Database type")
}
// for _, stest := range sTestsCDRsPostFailIT {
// t.Run(cdrsPostFailConfDIR, stest)
// }
// }
for _, stest := range sTestsCDRsPostFailIT {
t.Run(cdrsPostFailConfDIR, stest)
}
}
// func testCDRsPostFailoverInitConfig(t *testing.T) {
// var err error
// cdrsPostFailCfgPath = path.Join(*dataDir, "conf", "samples", cdrsPostFailConfDIR)
// if cdrsPostFailCfg, err = config.NewCGRConfigFromPath(cdrsPostFailCfgPath); err != nil {
// t.Fatal("Got config error: ", err.Error())
// }
// if err = os.RemoveAll(cdrsPostFailCfg.GeneralCfg().FailedPostsDir); err != nil {
// t.Error(err)
// }
// if err = os.MkdirAll(cdrsPostFailCfg.GeneralCfg().FailedPostsDir, 0755); err != nil {
// t.Error(err)
// }
// }
func testCDRsPostFailoverInitConfig(t *testing.T) {
var err error
cdrsPostFailCfgPath = path.Join(*dataDir, "conf", "samples", cdrsPostFailConfDIR)
if cdrsPostFailCfg, err = config.NewCGRConfigFromPath(context.Background(), cdrsPostFailCfgPath); err != nil {
t.Fatal("Got config error: ", err.Error())
}
if err = os.RemoveAll(cdrsPostFailCfg.GeneralCfg().FailedPostsDir); err != nil {
t.Error(err)
}
if err = os.MkdirAll(cdrsPostFailCfg.GeneralCfg().FailedPostsDir, 0755); err != nil {
t.Error(err)
}
}
// func testCDRsPostFailoverInitDataDb(t *testing.T) {
// if err := engine.InitDataDB(cdrsPostFailCfg); err != nil {
// t.Fatal(err)
// }
// }
func testCDRsPostFailoverInitDataDb(t *testing.T) {
if err := engine.InitDataDB(cdrsPostFailCfg); err != nil {
t.Fatal(err)
}
}
// func testCDRsPostFailoverStartEngine(t *testing.T) {
// if _, err := engine.StopStartEngine(cdrsPostFailCfgPath, *waitRater); err != nil {
// t.Fatal(err)
// }
// }
func testCDRsPostFailoverStartEngine(t *testing.T) {
if _, err := engine.StopStartEngine(cdrsPostFailCfgPath, *waitRater); err != nil {
t.Fatal(err)
}
}
// // Connect rpc client to rater
// func testCDRsPostFailoverRpcConn(t *testing.T) {
// cdrsPostFailRpc, err = newRPCClient(cdrsPostFailCfg.ListenCfg()) // We connect over JSON so we can also troubleshoot if needed
// if err != nil {
// t.Fatal("Could not connect to rater: ", err.Error())
// }
// }
// Connect rpc client to rater
func testCDRsPostFailoverRpcConn(t *testing.T) {
cdrsPostFailRpc, err = newRPCClient(cdrsPostFailCfg.ListenCfg()) // We connect over JSON so we can also troubleshoot if needed
if err != nil {
t.Fatal("Could not connect to rater: ", err.Error())
}
}
// func testCDRsPostFailoverLoadTariffPlanFromFolder(t *testing.T) {
// var loadInst utils.LoadInstance
// if err := cdrsPostFailRpc.Call(utils.APIerSv2LoadTariffPlanFromFolder,
// &utils.AttrLoadTpFromFolder{FolderPath: path.Join(
// *dataDir, "tariffplans", "testit")}, &loadInst); err != nil {
// t.Error(err)
// }
// time.Sleep(time.Duration(*waitRater) * time.Millisecond) // Give time for scheduler to execute topups
// var resp string
// if err := cdrsPostFailRpc.Call(utils.APIerSv1RemoveChargerProfile,
// &utils.TenantID{Tenant: "cgrates.org", ID: "SupplierCharges"}, &resp); err != nil {
// t.Error(err)
// } else if resp != utils.OK {
// t.Error("Unexpected reply returned", resp)
// }
// var reply *engine.ChargerProfile
// if err := cdrsPostFailRpc.Call(utils.APIerSv1GetChargerProfile,
// &utils.TenantID{Tenant: "cgrates.org", ID: "SupplierCharges"},
// &reply); err == nil || err.Error() != utils.ErrNotFound.Error() {
// t.Error(err)
// }
// }
func testCDRsPostFailoverLoadTariffPlanFromFolder(t *testing.T) {
caching := utils.MetaReload
if cdrsPostFailCfg.DataDbCfg().Type == utils.Internal {
caching = utils.MetaNone
}
var reply string
if err := cdrsPostFailRpc.Call(context.Background(), utils.LoaderSv1Run,
&loaders.ArgsProcessFolder{
APIOpts: map[string]interface{}{
utils.MetaStopOnError: true,
utils.MetaCache: caching,
},
}, &reply); err != nil {
t.Error(err)
} else if reply != utils.OK {
t.Error("Unexpected reply returned:", reply)
}
// func testCDRsPostFailoverProcessCDR(t *testing.T) {
// args := &utils.CGREvent{
// ID: "1",
// Tenant: "cgrates.org",
// Event: map[string]interface{}{
// utils.OriginID: "testCDRsPostFailoverProcessCDR",
// utils.OriginHost: "192.168.1.1",
// utils.Source: "testCDRsPostFailoverProcessCDR",
// utils.RequestType: utils.MetaRated,
// utils.Category: "call",
// utils.AccountField: "testCDRsPostFailoverProcessCDR",
// utils.Subject: "ANY2CNT",
// utils.Destination: "+4986517174963",
// utils.AnswerTime: time.Date(2018, 8, 24, 16, 00, 26, 0, time.UTC),
// utils.Usage: time.Minute,
// "field_extr1": "val_extr1",
// "fieldextr2": "valextr2",
// },
// APIOpts: map[string]interface{}{
// utils.OptsCDRsExport: true,
// utils.OptsAttributeS: false,
// // utils.MetaRalS:false,
// utils.OptsChargerS: false,
// utils.OptsCDRsStore: false,
// utils.OptsThresholdS: false,
// utils.OptsStatS: false,
// },
// }
time.Sleep(time.Duration(*waitRater) * time.Millisecond) // Give time for scheduler to execute topups
var resp string
if err := cdrsPostFailRpc.Call(context.Background(), utils.AdminSv1RemoveChargerProfile,
&utils.TenantID{Tenant: "cgrates.org", ID: "SupplierCharges"}, &resp); err != nil {
t.Error(err)
} else if resp != utils.OK {
t.Error("Unexpected reply returned", resp)
}
var replyChargerPrf *engine.ChargerProfile
if err := cdrsPostFailRpc.Call(context.Background(), utils.AdminSv1GetChargerProfile,
&utils.TenantID{Tenant: "cgrates.org", ID: "SupplierCharges"},
&replyChargerPrf); err == nil || err.Error() != utils.ErrNotFound.Error() {
t.Error(err)
}
}
// var reply string
// if err := cdrsPostFailRpc.Call(utils.CDRsV1ProcessEvent, args, &reply); err != nil {
// t.Error("Unexpected error: ", err.Error())
// } else if reply != utils.OK {
// t.Error("Unexpected reply received: ", reply)
// }
// args.ID = "2"
// args.Event[utils.OriginID] = "2"
// if err := cdrsPostFailRpc.Call(utils.CDRsV1ProcessEvent, args, &reply); err != nil {
// t.Error("Unexpected error: ", err.Error())
// } else if reply != utils.OK {
// t.Error("Unexpected reply received: ", reply)
// }
// args.ID = "3"
// args.Event[utils.OriginID] = "3"
// if err := cdrsPostFailRpc.Call(utils.CDRsV1ProcessEvent, args, &reply); err != nil {
// t.Error("Unexpected error: ", err.Error())
// } else if reply != utils.OK {
// t.Error("Unexpected reply received: ", reply)
// }
// }
func testCDRsPostFailoverProcessCDR(t *testing.T) {
args := &utils.CGREvent{
ID: "1",
Tenant: "cgrates.org",
Event: map[string]interface{}{
utils.OriginID: "testCDRsPostFailoverProcessCDR",
utils.OriginHost: "192.168.1.1",
utils.Source: "testCDRsPostFailoverProcessCDR",
utils.RequestType: utils.MetaRated,
utils.Category: "call",
utils.AccountField: "testCDRsPostFailoverProcessCDR",
utils.Subject: "ANY2CNT",
utils.Destination: "+4986517174963",
utils.AnswerTime: time.Date(2018, 8, 24, 16, 00, 26, 0, time.UTC),
utils.Usage: time.Minute,
"field_extr1": "val_extr1",
"fieldextr2": "valextr2",
},
APIOpts: map[string]interface{}{
utils.OptsCDRsExport: true,
utils.OptsAttributeS: false,
utils.OptsChargerS: false,
utils.OptsCDRsStore: false,
utils.OptsThresholdS: false,
utils.OptsStatS: false,
},
}
// func testCDRsPostFailoverToFile(t *testing.T) {
// time.Sleep(2 * time.Second)
// filesInDir, _ := os.ReadDir(cdrsPostFailCfg.GeneralCfg().FailedPostsDir)
// if len(filesInDir) == 0 {
// t.Fatalf("No files in directory: %s", cdrsPostFailCfg.GeneralCfg().FailedPostsDir)
// }
// for _, file := range filesInDir { // First file in directory is the one we need, harder to find it's name out of config
// fileName := file.Name()
// filePath := path.Join(cdrsPostFailCfg.GeneralCfg().FailedPostsDir, fileName)
var reply string
if err := cdrsPostFailRpc.Call(context.Background(), utils.CDRsV1ProcessEvent, args, &reply); err != nil {
t.Error("Unexpected error: ", err.Error())
} else if reply != utils.OK {
t.Error("Unexpected reply received: ", reply)
}
args.ID = "2"
args.Event[utils.OriginID] = "2"
if err := cdrsPostFailRpc.Call(context.Background(), utils.CDRsV1ProcessEvent, args, &reply); err != nil {
t.Error("Unexpected error: ", err.Error())
} else if reply != utils.OK {
t.Error("Unexpected reply received: ", reply)
}
args.ID = "3"
args.Event[utils.OriginID] = "3"
if err := cdrsPostFailRpc.Call(context.Background(), utils.CDRsV1ProcessEvent, args, &reply); err != nil {
t.Error("Unexpected error: ", err.Error())
} else if reply != utils.OK {
t.Error("Unexpected reply received: ", reply)
}
}
// ev, err := ees.NewExportEventsFromFile(filePath)
// if err != nil {
// t.Errorf("<%s> for file <%s>", err, fileName)
// continue
// } else if len(ev.Events) == 0 {
// t.Error("Expected at least one event")
// continue
// }
// if ev.Format != utils.MetaS3jsonMap {
// t.Errorf("Expected event to use %q received: %q", utils.MetaS3jsonMap, ev.Format)
// }
// if len(ev.Events) != 3 {
// t.Errorf("Expected all the events to be saved in the same file, ony %v saved in this file.", len(ev.Events))
// }
// }
// }
func testCDRsPostFailoverToFile(t *testing.T) {
time.Sleep(2 * time.Second)
filesInDir, _ := os.ReadDir(cdrsPostFailCfg.GeneralCfg().FailedPostsDir)
if len(filesInDir) == 0 {
t.Fatalf("No files in directory: %s", cdrsPostFailCfg.GeneralCfg().FailedPostsDir)
}
for _, file := range filesInDir { // First file in directory is the one we need, harder to find it's name out of config
fileName := file.Name()
filePath := path.Join(cdrsPostFailCfg.GeneralCfg().FailedPostsDir, fileName)
// func testCDRsPostFailoverKillEngine(t *testing.T) {
// if err = os.RemoveAll(cdrsPostFailCfg.GeneralCfg().FailedPostsDir); err != nil {
// t.Error(err)
// }
// if err := engine.KillEngine(*waitRater); err != nil {
// t.Error(err)
// }
// }
ev, err := ees.NewExportEventsFromFile(filePath)
if err != nil {
t.Errorf("<%s> for file <%s>", err, fileName)
continue
} else if len(ev.Events) == 0 {
t.Error("Expected at least one event")
continue
}
if ev.Format != utils.MetaS3jsonMap {
t.Errorf("Expected event to use %q received: %q", utils.MetaS3jsonMap, ev.Format)
}
if len(ev.Events) != 3 {
t.Errorf("Expected all the events to be saved in the same file, ony %v saved in this file.", len(ev.Events))
}
}
}
func testCDRsPostFailoverKillEngine(t *testing.T) {
if err = os.RemoveAll(cdrsPostFailCfg.GeneralCfg().FailedPostsDir); err != nil {
t.Error(err)
}
if err := engine.KillEngine(*waitRater); err != nil {
t.Error(err)
}
}