diff --git a/data/conf/samples/tutmysql_internal/cgrates.json b/data/conf/samples/tutmysql_internal/cgrates.json new file mode 100644 index 000000000..e4a29daa8 --- /dev/null +++ b/data/conf/samples/tutmysql_internal/cgrates.json @@ -0,0 +1,289 @@ +{ +// CGRateS Configuration file +// + + +"general": { + "log_level": 7, +}, + + +"listen": { + "rpc_json": ":2012", + "rpc_gob": ":2013", + "http": ":2080", +}, + +"data_db": { // database used to store runtime data (eg: accounts, cdr stats) + "db_type": "redis", // data_db type: + "db_port": 6379, // data_db port to reach the database + "db_name": "10", // data_db database name to connect to +}, + +"stor_db": { + "db_password": "CGRateS.org", +}, + + +"cache":{ + "destinations": {"limit": 10000, "ttl":"0s", "precache": true}, + "reverse_destinations": {"limit": 10000, "ttl":"0s", "precache": true}, + "rating_plans": {"limit": 10000, "ttl":"0s","precache": true}, + "rating_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "actions": {"limit": 10000, "ttl":"0s", "precache": true}, + "action_plans": {"limit": 10000, "ttl":"0s", "precache": true}, + "account_action_plans": {"limit": 10000, "ttl":"0s", "precache": true}, + "action_triggers": {"limit": 10000, "ttl":"0s", "precache": true}, + "shared_groups": {"limit": 10000, "ttl":"0s", "precache": true}, + "resource_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "resources": {"limit": 10000, "ttl":"0s", "precache": true}, + "statqueues": {"limit": 10000, "ttl":"0s", "precache": true}, + "statqueue_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "thresholds": {"limit": 10000, "ttl":"0s", "precache": true}, + "threshold_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "filters": {"limit": 10000, "ttl":"0s", "precache": true}, + "supplier_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "attribute_profiles": {"limit": 10000, "ttl":"0s", "precache": true}, + "resource_filter_indexes" :{"limit": 10000, "ttl":"0s"}, + "stat_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "threshold_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "supplier_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "attribute_filter_indexes" : {"limit": 10000, "ttl":"0s"}, + "charger_filter_indexes" : {"limit": 10000, "ttl":"0s"}, +}, + + +"rals": { + "enabled": true, + "thresholds_conns": [ + {"address": "*internal"}, + ], +}, + + +"scheduler": { + "enabled": true, + "cdrs_conns": [ + {"address": "*internal"}, + ], +}, + + +"cdrs": { + "enabled": true, + "chargers_conns":[ + {"address": "*internal"}, + ], +}, + + +"cdre": { + "TestTutITExportCDR": { + "content_fields": [ + {"tag": "CGRID", "type": "*composed", "value": "~CGRID"}, + {"tag": "RunID", "type": "*composed", "value": "~RunID"}, + {"tag":"OriginID", "type": "*composed", "value": "~OriginID"}, + {"tag":"RequestType", "type": "*composed", "value": "~RequestType"}, + {"tag":"Tenant", "type": "*composed", "value": "~Tenant"}, + {"tag":"Category", "type": "*composed", "value": "~Category"}, + {"tag":"Account", "type": "*composed", "value": "~Account"}, + {"tag":"Destination", "type": "*composed", "value": "~Destination"}, + {"tag":"AnswerTime", "type": "*composed", "value": "~AnswerTime", "layout": "2006-01-02T15:04:05Z07:00"}, + {"tag":"Usage", "type": "*composed", "value": "~Usage"}, + {"tag":"Cost", "type": "*composed", "value": "~Cost", "rounding_decimals": 4}, + {"tag":"MatchedDestinationID", "type": "*composed", "value": "~CostDetails:s/\"MatchedDestId\":.*_(\\w{4})/${1}/:s/\"MatchedDestId\":\"INTERNAL\"/ON010/"}, + ], + }, +}, + +"loaders": [ + { + "id": "TeoLoader", // identifier of the Loader + "enabled": false, // starts as service: . + "dry_run": false, // do not send the CDRs to CDRS, just parse them + "run_delay": 0, // sleep interval in seconds between consecutive runs, 0 to use automation via inotify + "lock_filename": ".cgr.lock", // Filename containing concurrency lock in case of delayed processing + "caches_conns": [ + {"address": "*internal"}, // address where to reach the CacheS for data reload, empty for no reloads <""|*internal|x.y.z.y:1234> + ], + "field_separator": ",", // separator used in case of csv files + "tp_in_dir": "/tmp/In", // absolute path towards the directory where the CDRs are stored + "tp_out_dir": "/tmp/Out", // absolute path towards the directory where processed CDRs will be moved + "data":[ + { + "type": "*resources", // data source type + "file_name": "Resources.csv", // file name in the tp_in_dir + "fields": [ + {"tag": "Tenant", "field_id": "Tenant", "type": "*composed", "value": "~0", "mandatory": true}, + {"tag": "ID", "field_id": "ID", "type": "*composed", "value": "~1", "mandatory": true}, + {"tag": "FilterIDs", "field_id": "FilterIDs", "type": "*composed", "value": "~2"}, + {"tag": "ActivationInterval", "field_id": "ActivationInterval", "type": "*composed", "value": "~3"}, + {"tag": "TTL", "field_id": "UsageTTL", "type": "*composed", "value": "~4"}, + {"tag": "Limit", "field_id": "Limit", "type": "*composed", "value": "~5"}, + {"tag": "AllocationMessage", "field_id": "AllocationMessage", "type": "*composed", "value": "~6"}, + {"tag": "Blocker", "field_id": "Blocker", "type": "*composed", "value": "~7"}, + {"tag": "Stored", "field_id": "Stored", "type": "*composed", "value": "~8"}, + {"tag": "Weight", "field_id": "Weight", "type": "*composed", "value": "~9"}, + {"tag": "ThresholdIDs", "field_id": "ThresholdIDs", "type": "*composed", "value": "~10"}, + ], + }, + { + "type": "*stats", // data source type + "file_name": "Stats.csv", // file name in the tp_in_dir + "fields": [ + {"tag": "Tenant", "field_id": "Tenant", "type": "*composed", "value": "~0", "mandatory": true}, + {"tag": "ID", "field_id": "ID", "type": "*composed", "value": "~1", "mandatory": true}, + {"tag": "FilterIDs", "field_id": "FilterIDs", "type": "*composed", "value": "~2"}, + {"tag": "ActivationInterval", "field_id": "ActivationInterval", "type": "*composed", "value": "~3"}, + {"tag": "QueueLength", "field_id": "QueueLength", "type": "*composed", "value": "~4"}, + {"tag": "TTL", "field_id": "TTL", "type": "*composed", "value": "~5"}, + {"tag": "Metrics", "field_id": "Metrics", "type": "*composed", "value": "~6"}, + {"tag": "MetricParams", "field_id": "Parameters", "type": "*composed", "value": "~7"}, + {"tag": "Blocker", "field_id": "Blocker", "type": "*composed", "value": "~8"}, + {"tag": "Stored", "field_id": "Stored", "type": "*composed", "value": "~9"}, + {"tag": "Weight", "field_id": "Weight", "type": "*composed", "value": "~10"}, + {"tag": "MinItems", "field_id": "MinItems", "type": "*composed", "value": "~11"}, + {"tag": "ThresholdIDs", "field_id": "ThresholdIDs", "type": "*composed", "value": "~12"}, + ], + }, + { + "type": "*thresholds", // data source type + "file_name": "Thresholds.csv", // file name in the tp_in_dir + "fields": [ + {"tag": "Tenant", "field_id": "Tenant", "type": "*composed", "value": "~0", "mandatory": true}, + {"tag": "ID", "field_id": "ID", "type": "*composed", "value": "~1", "mandatory": true}, + {"tag": "FilterIDs", "field_id": "FilterIDs", "type": "*composed", "value": "~2"}, + {"tag": "ActivationInterval", "field_id": "ActivationInterval", "type": "*composed", "value": "~3"}, + {"tag": "MaxHits", "field_id": "MaxHits", "type": "*composed", "value": "~4"}, + {"tag": "MinHits", "field_id": "MinHits", "type": "*composed", "value": "~5"}, + {"tag": "MinSleep", "field_id": "MinSleep", "type": "*composed", "value": "~6"}, + {"tag": "Blocker", "field_id": "Blocker", "type": "*composed", "value": "~7"}, + {"tag": "Weight", "field_id": "Weight", "type": "*composed", "value": "~8"}, + {"tag": "ActionIDs", "field_id": "ActionIDs", "type": "*composed", "value": "~9"}, + {"tag": "Async", "field_id": "Async", "type": "*composed", "value": "~10"}, + ], + }, + { + "type": "*suppliers", // data source type + "file_name": "Suppliers.csv", // file name in the tp_in_dir + "fields": [ + {"tag": "Tenant", "field_id": "Tenant", "type": "*composed", "value": "~0", "mandatory": true}, + {"tag": "ID", "field_id": "ID", "type": "*composed", "value": "~1", "mandatory": true}, + {"tag": "FilterIDs", "field_id": "FilterIDs", "type": "*composed", "value": "~2"}, + {"tag": "ActivationInterval", "field_id": "ActivationInterval", "type": "*composed", "value": "~3"}, + {"tag": "Sorting", "field_id": "Sorting", "type": "*composed", "value": "~4"}, + {"tag": "SortingParamameters", "field_id": "SortingParamameters", "type": "*composed", "value": "~5"}, + {"tag": "SupplierID", "field_id": "SupplierID", "type": "*composed", "value": "~6"}, + {"tag": "SupplierFilterIDs", "field_id": "SupplierFilterIDs", "type": "*composed", "value": "~7"}, + {"tag": "SupplierAccountIDs", "field_id": "SupplierAccountIDs", "type": "*composed", "value": "~8"}, + {"tag": "SupplierRatingPlanIDs", "field_id": "SupplierRatingPlanIDs", "type": "*composed", "value": "~9"}, + {"tag": "SupplierResourceIDs", "field_id": "SupplierResourceIDs", "type": "*composed", "value": "~10"}, + {"tag": "SupplierStatIDs", "field_id": "SupplierStatIDs", "type": "*composed", "value": "~11"}, + {"tag": "SupplierWeight", "field_id": "SupplierWeight", "type": "*composed", "value": "~12"}, + {"tag": "SupplierBlocker", "field_id": "SupplierBlocker", "type": "*composed", "value": "~13"}, + {"tag": "SupplierParameters", "field_id": "SupplierParameters", "type": "*composed", "value": "~14"}, + {"tag": "Weight", "field_id": "Weight", "type": "*composed", "value": "~15"}, + ], + }, + ], + }, + { + "id": "FilterLoader", // identifier of the Loader + "enabled": false, // starts as service: . + "dry_run": false, // do not send the CDRs to CDRS, just parse them + "run_delay": 0, // sleep interval in seconds between consecutive runs, 0 to use automation via inotify + "lock_filename": ".cgr.lock", // Filename containing concurrency lock in case of delayed processing + "caches_conns": [ + {"address": "*internal"}, // address where to reach the CacheS for data reload, empty for no reloads <""|*internal|x.y.z.y:1234> + ], + "field_separator": ",", // separator used in case of csv files + "tp_in_dir": "/tmp/FilterIn", // absolute path towards the directory where the CDRs are stored + "tp_out_dir": "/tmp/FilterOut", // absolute path towards the directory where processed CDRs will be moved + "data":[ + { + "type": "*filters", // data source type + "file_name": "Filters.csv", // file name in the tp_in_dir + "fields": [ + {"tag": "Tenant", "field_id": "Tenant", "type": "*composed", "value": "~0", "mandatory": true}, + {"tag": "ID", "field_id": "ID", "type": "*composed", "value": "~1", "mandatory": true}, + {"tag": "FilterType", "field_id": "FilterType", "type": "*composed", "value": "~2"}, + {"tag": "FilterFieldName", "field_id": "FilterFieldName", "type": "*composed", "value": "~3"}, + {"tag": "FilterFieldValues", "field_id": "FilterFieldValues", "type": "*composed", "value": "~4"}, + {"tag": "ActivationInterval", "field_id": "ActivationInterval", "type": "*composed", "value": "~5"}, + ], + }, + ], + }, +], + + + +"attributes": { + "enabled": true, +}, + + +"chargers": { + "enabled": true, + "attributes_conns": [ + {"address": "*internal"}, + ], +}, + + +"resources": { + "enabled": true, + "store_interval": "1s", + "thresholds_conns": [ + {"address": "*internal"} + ], +}, + + +"stats": { + "enabled": true, + "store_interval": "1s", + "thresholds_conns": [ + {"address": "*internal"} + ], +}, + +"thresholds": { + "enabled": true, + "store_interval": "1s", +}, + + +"suppliers": { + "enabled": true, + "prefix_indexed_fields":["Destination",], + "stats_conns": [ + {"address": "*internal"}, + ], +}, + + +"sessions": { + "enabled": true, + "suppliers_conns": [ + {"address": "*internal"} + ], + "resources_conns": [ + {"address": "*internal"} + ], + "attributes_conns": [ + {"address": "*internal"} + ], + "rals_conns": [ + {"address": "*internal"} + ], +}, + + +"migrator":{ + "out_stordb_password": "CGRateS.org", + "users_filters":["Account"], +}, + + +} diff --git a/general_tests/session_it_test.go b/general_tests/session_it_test.go new file mode 100644 index 000000000..3a8e0d056 --- /dev/null +++ b/general_tests/session_it_test.go @@ -0,0 +1,191 @@ +// +build integration + +/* +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 +*/ +package general_tests + +import ( + "net/rpc" + "net/rpc/jsonrpc" + "path" + "testing" + "time" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/sessions" + "github.com/cgrates/cgrates/utils" +) + +var ( + sesCfgPath string + sesCfg *config.CGRConfig + sesRPC *rpc.Client + sesAccount = "refundAcc" + sesTenant = "cgrates.org" +) + +// test for 0 balance with session terminate with 1s usage +func TestSesItLoadConfig(t *testing.T) { + sesCfgPath = path.Join(*dataDir, "conf", "samples", "tutmysql_internal") + if sesCfg, err = config.NewCGRConfigFromFolder(sesCfgPath); err != nil { + t.Error(err) + } +} + +func TestSesItResetDataDB(t *testing.T) { + if err := engine.InitDataDb(sesCfg); err != nil { + t.Fatal(err) + } +} + +func TestSesItResetStorDb(t *testing.T) { + if err := engine.InitStorDb(sesCfg); err != nil { + t.Fatal(err) + } +} + +func TestSesItStartEngine(t *testing.T) { + if _, err := engine.StopStartEngine(sesCfgPath, *waitRater); err != nil { + t.Fatal(err) + } +} + +func TestSesItRPCConn(t *testing.T) { + var err error + sesRPC, err = jsonrpc.Dial("tcp", sesCfg.ListenCfg().RPCJSONListen) + if err != nil { + t.Fatal(err) + } +} + +func TestSesItLoadFromFolder(t *testing.T) { + var reply string + attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "testit")} + if err := sesRPC.Call("ApierV1.LoadTariffPlanFromFolder", attrs, &reply); err != nil { + t.Error(err) + } + time.Sleep(500 * time.Millisecond) +} + +func testAccountBalance2(t *testing.T, sracc, srten, balType string, expected float64) { + var acnt *engine.Account + attrs := &utils.AttrGetAccount{ + Tenant: srten, + Account: sracc, + } + if err := sesRPC.Call("ApierV2.GetAccount", attrs, &acnt); err != nil { + t.Error(err) + } else if rply := acnt.BalanceMap[balType].GetTotalValue(); rply != expected { + t.Errorf("Expecting: %v, received: %v", + expected, rply) + } +} + +func TestSesItAddVoiceBalance(t *testing.T) { + attrSetBalance := utils.AttrSetBalance{ + Tenant: sesTenant, + Account: sesAccount, + BalanceType: utils.MONETARY, + BalanceID: utils.StringPointer("TestDynamicDebitBalance"), + Value: utils.Float64Pointer(0), + RatingSubject: utils.StringPointer("*zero1s"), + } + var reply string + if err := sesRPC.Call("ApierV2.SetBalance", attrSetBalance, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("Received: %s", reply) + } + t.Run("TestAddVoiceBalance", func(t *testing.T) { testAccountBalance2(t, sesAccount, sesTenant, utils.MONETARY, 0) }) +} + +func TestSesItInitSession(t *testing.T) { + args1 := &sessions.V1InitSessionArgs{ + InitSession: true, + CGREvent: utils.CGREvent{ + Tenant: sesTenant, + ID: "TestSesItInitiateSession", + Event: map[string]interface{}{ + utils.Tenant: sesTenant, + utils.Category: "call", + utils.ToR: utils.VOICE, + utils.OriginID: "TestRefund", + utils.RequestType: utils.META_PREPAID, + utils.Account: sesAccount, + utils.Subject: "TEST", + utils.Destination: "TEST", + utils.SetupTime: time.Date(2018, time.January, 7, 16, 60, 0, 0, time.UTC), + utils.AnswerTime: time.Date(2018, time.January, 7, 16, 60, 10, 0, time.UTC), + utils.Usage: 5 * time.Second, + }, + }, + } + var rply1 sessions.V1InitSessionReply + if err := sesRPC.Call(utils.SessionSv1InitiateSession, + args1, &rply1); err != nil { + t.Error(err) + return + } else if *rply1.MaxUsage != 0 { + t.Errorf("Unexpected MaxUsage: %v", rply1.MaxUsage) + } + t.Run("TestInitSession", func(t *testing.T) { testAccountBalance2(t, sesAccount, sesTenant, utils.MONETARY, 0) }) +} + +func TestSesItTerminateSession(t *testing.T) { + args := &sessions.V1TerminateSessionArgs{ + TerminateSession: true, + CGREvent: utils.CGREvent{ + Tenant: sesTenant, + ID: "TestSesItUpdateSession", + Event: map[string]interface{}{ + utils.Tenant: sesTenant, + utils.Category: "call", + utils.ToR: utils.VOICE, + utils.OriginID: "TestRefund", + utils.RequestType: utils.META_PREPAID, + utils.Account: sesAccount, + utils.Subject: "TEST", + utils.Destination: "TEST", + utils.SetupTime: time.Date(2018, time.January, 7, 16, 60, 0, 0, time.UTC), + utils.AnswerTime: time.Date(2018, time.January, 7, 16, 60, 10, 0, time.UTC), + utils.Usage: 1 * time.Second, + }, + }, + } + var rply string + if err := sesRPC.Call(utils.SessionSv1TerminateSession, + args, &rply); err != nil { + t.Error(err) + } + if rply != utils.OK { + t.Errorf("Unexpected reply: %s", rply) + } + aSessions := make([]*sessions.ActiveSession, 0) + if err := sesRPC.Call(utils.SessionSv1GetActiveSessions, nil, &aSessions); err == nil || + err.Error() != utils.ErrNotFound.Error() { + t.Error(err) + } + t.Run("TestTerminateSession", func(t *testing.T) { testAccountBalance2(t, sesAccount, sesTenant, utils.MONETARY, 0) }) +} + +func TestSesItStopCgrEngine(t *testing.T) { + if err := engine.KillEngine(100); err != nil { + t.Error(err) + } +} diff --git a/sessions/sessions.go b/sessions/sessions.go index bf96e1391..5f538fdd8 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -485,7 +485,9 @@ func (sS *SessionS) debitSession(s *Session, sRunIdx int, dur time.Duration, ec := engine.NewEventCostFromCallCost(cc, s.CGRID, sr.Event.GetStringIgnoreErrors(utils.RunID)) if sr.EventCost == nil { - sr.EventCost = ec + if ccDuration != time.Duration(0) { + sr.EventCost = ec + } } else { sr.EventCost.Merge(ec) }