Added DispatcherH integration tests

This commit is contained in:
Trial97
2020-08-24 17:57:18 +03:00
committed by Dan Christian Bogos
parent eee31259df
commit 7668e228b2
12 changed files with 862 additions and 3 deletions

View File

@@ -0,0 +1,117 @@
{
// CGRateS Configuration file
//
"general": {
"node_id": "ALL2",
"log_level": 7
},
"listen": {
"rpc_json": ":7012",
"rpc_gob": ":7013",
"http": ":7080",
},
"data_db": {
"db_type": "mongo",
"db_name": "12",
"db_port": 27017,
},
"stor_db": {
"db_type": "mongo",
"db_name": "cgrates",
"db_port": 27017,
},
"rpc_conns": {
"conn1": {
"strategy": "*first",
"conns": [{"address": "127.0.0.1:7012", "transport":"*json"}],
},
"dispConn": {
"strategy": "*first",
"conns": [{"address": "127.0.0.1:2012/registar", "transport":"*http_jsonrpc"}]
}
},
"attributes": {
"enabled": true
},
"rals": {
"enabled": true,
},
"schedulers": {
"enabled": true,
},
"chargers": {
"enabled": true,
"attributes_conns": ["*internal"],
},
"thresholds": {
"enabled": true,
},
"routes": {
"enabled": true,
"rals_conns": ["*internal"],
},
"stats": {
"enabled": true,
},
"resources": {
"enabled": true,
},
"rates": {
"enabled": true,
},
"cdrs": {
"enabled": true,
"chargers_conns":["*internal"],
"rals_conns": ["*internal"],
},
"sessions": {
"enabled": true,
"listen_bijson": ":7014",
"routes_conns": ["*internal"],
"resources_conns": ["*internal"],
"attributes_conns": ["*internal"],
"rals_conns": ["*internal"],
"cdrs_conns": ["*internal"],
"chargers_conns": ["*internal"],
},
"apiers": {
"enabled": true,
"caches_conns":["conn1"],
"scheduler_conns": ["*internal"],
},
"dispatcherh":{
"enabled": false,
"dispatchers_conns": ["dispConn"],
"host_ids": {"*default":["ALL"]},
"register_interval": "1s",
"register_transport": "*json",
"register_tls": false,
},
}

View File

@@ -0,0 +1,116 @@
{
// CGRateS Configuration file
//
"general": {
"node_id": "ALL2",
"log_level": 7
},
"listen": {
"rpc_json": ":7012",
"rpc_gob": ":7013",
"http": ":7080",
},
"data_db": { // database used to store runtime data (eg: accounts, cdr stats)
"db_type": "redis", // data_db type: <redis|mongo>
"db_port": 6379, // data_db port to reach the database
"db_name": "12", // data_db database name to connect to
},
"stor_db": {
"db_password": "CGRateS.org",
},
"rpc_conns": {
"conn1": {
"strategy": "*first",
"conns": [{"address": "127.0.0.1:7012", "transport":"*json"}],
},
"dispConn": {
"strategy": "*first",
"conns": [{"address": "127.0.0.1:2012/registar", "transport":"*http_jsonrpc"}]
}
},
"attributes": {
"enabled": true
},
"rals": {
"enabled": true,
},
"schedulers": {
"enabled": true,
},
"chargers": {
"enabled": true,
"attributes_conns": ["*internal"],
},
"thresholds": {
"enabled": true,
},
"routes": {
"enabled": true,
"rals_conns": ["*internal"],
},
"stats": {
"enabled": true,
},
"resources": {
"enabled": true,
},
"rates": {
"enabled": true,
},
"cdrs": {
"enabled": true,
"chargers_conns":["*internal"],
"rals_conns": ["*internal"],
},
"sessions": {
"enabled": true,
"listen_bijson": ":7014",
"routes_conns": ["*internal"],
"resources_conns": ["*internal"],
"attributes_conns": ["*internal"],
"rals_conns": ["*internal"],
"cdrs_conns": ["*internal"],
"chargers_conns": ["*internal"],
},
"apiers": {
"enabled": true,
"caches_conns":["conn1"],
"scheduler_conns": ["*internal"],
},
"dispatcherh":{
"enabled": false,
"dispatchers_conns": ["dispConn"],
"host_ids": {"*default":["ALL"]},
"register_interval": "1s",
"register_transport": "*json",
"register_tls": false,
},
}

View File

@@ -0,0 +1,121 @@
{
// CGRateS Configuration file
//
"general": {
"node_id": "ALL",
"log_level": 7
},
"listen": {
"rpc_json": ":6012",
"rpc_gob": ":6013",
"http": ":6080",
},
"data_db": {
"db_type": "mongo",
"db_name": "11",
"db_port": 27017,
},
"stor_db": {
"db_type": "mongo",
"db_name": "cgrates",
"db_port": 27017,
},
"rpc_conns": {
"conn1": {
"strategy": "*first",
"conns": [{"address": "127.0.0.1:6012", "transport":"*json"}],
},
"rplConn": {
"strategy": "*broadcast_sync",
"conns": [{"address": "127.0.0.1:7012", "transport":"*json"}],
},
"dispConn": {
"strategy": "*first",
"conns": [{"address": "127.0.0.1:2012/registar", "transport":"*http_jsonrpc"}]
}
},
"attributes": {
"enabled": true
},
"rals": {
"enabled": true,
},
"schedulers": {
"enabled": true,
},
"chargers": {
"enabled": true,
"attributes_conns": ["*internal"],
},
"thresholds": {
"enabled": true,
},
"routes": {
"enabled": true,
"rals_conns": ["*internal"]
},
"stats": {
"enabled": true,
},
"resources": {
"enabled": true,
},
"rates": {
"enabled": true,
},
"cdrs": {
"enabled": true,
"chargers_conns":["*internal"],
"rals_conns": ["*internal"],
},
"sessions": {
"enabled": true,
"listen_bijson": ":6014",
"routes_conns": ["*internal"],
"resources_conns": ["*internal"],
"attributes_conns": ["*internal"],
"rals_conns": ["*internal"],
"chargers_conns": ["*internal"],
"cdrs_conns": ["*internal"],
},
"apiers": {
"enabled": true,
"caches_conns":["conn1"],
"scheduler_conns": ["*internal"],
},
"dispatcherh":{
"enabled": false,
"dispatchers_conns": ["dispConn"],
"host_ids": {"*default":["ALL"]},
"register_interval": "1s",
"register_transport": "*json",
"register_tls": false,
},
}

View File

@@ -0,0 +1,119 @@
{
// CGRateS Configuration file
//
"general": {
"node_id": "ALL",
"log_level": 7
},
"listen": {
"rpc_json": ":6012",
"rpc_gob": ":6013",
"http": ":6080",
},
"data_db": { // database used to store runtime data (eg: accounts, cdr stats)
"db_type": "redis", // data_db type: <redis|mongo>
"db_port": 6379, // data_db port to reach the database
"db_name": "11", // data_db database name to connect to
},
"stor_db": {
"db_password": "CGRateS.org",
},
"rpc_conns": {
"conn1": {
"strategy": "*first",
"conns": [{"address": "127.0.0.1:6012", "transport":"*json"}],
},
"rplConn": {
"strategy": "*broadcast_sync",
"conns": [{"address": "127.0.0.1:7012", "transport":"*json"}],
},
"dispConn": {
"strategy": "*first",
"conns": [{"address": "127.0.0.1:2012/registar", "transport":"*http_jsonrpc"}]
}
},
"attributes": {
"enabled": true
},
"rals": {
"enabled": true,
},
"schedulers": {
"enabled": true,
},
"chargers": {
"enabled": true,
"attributes_conns": ["*internal"],
},
"thresholds": {
"enabled": true,
},
"routes": {
"enabled": true,
"rals_conns": ["*internal"],
},
"stats": {
"enabled": true,
},
"resources": {
"enabled": true,
},
"rates": {
"enabled": true,
},
"cdrs": {
"enabled": true,
"chargers_conns":["*internal"],
"rals_conns": ["*internal"],
},
"sessions": {
"enabled": true,
"listen_bijson": ":6014",
"routes_conns": ["*internal"],
"resources_conns": ["*internal"],
"attributes_conns": ["*internal"],
"rals_conns": ["*internal"],
"chargers_conns": ["*internal"],
"cdrs_conns": ["*internal"],
},
"apiers": {
"enabled": true,
"caches_conns":["conn1"],
"scheduler_conns": ["*internal"],
},
"dispatcherh":{
"enabled": false,
"dispatchers_conns": ["dispConn"],
"host_ids": {"*default":["ALL"]},
"register_interval": "1s",
"register_transport": "*json",
"register_tls": false,
},
}

View File

@@ -0,0 +1,73 @@
{
// Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
// Copyright (C) ITsysCOM GmbH
//
// This file contains the default configuration hardcoded into CGRateS.
// This is what you get when you load CGRateS with an empty configuration file.
"general": {
"node_id": "DispatcherS1",
"log_level": 7,
"reconnects": 1,
},
"listen": {
"rpc_json": ":2012",
"rpc_gob": ":2013",
"http": ":2080",
},
"data_db": {
"db_type": "mongo",
"db_name": "10",
"db_port": 27017,
},
"stor_db": {
"db_type": "mongo",
"db_name": "cgrates",
"db_port": 27017,
},
"caches":{
"partitions": {
"*dispatcher_routes": {"limit": -1, "ttl": "2s"}
},
},
"schedulers": {
"enabled": true,
},
"rals": {
"enabled": true,
},
"chargers": {
"enabled": true,
},
"sessions": {
"enabled": true,
"rals_conns": ["*localhost"],
"resources_conns": ["*localhost"],
"chargers_conns": ["*localhost"],
"listen_bijson": ":3014",
},
"dispatchers":{
"enabled": true,
},
"apiers": {
"enabled": true,
"scheduler_conns": ["*internal"],
},
}

View File

@@ -0,0 +1,62 @@
{
// Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
// Copyright (C) ITsysCOM GmbH
//
// This file contains the default configuration hardcoded into CGRateS.
// This is what you get when you load CGRateS with an empty configuration file.
"general": {
"node_id": "DispatcherS1",
"log_level": 7,
"reconnects": 1,
},
"listen": {
"rpc_json": ":2012",
"rpc_gob": ":2013",
"http": ":2080",
},
"stor_db": {
"db_type":"*internal",
},
"caches":{
"partitions": {
"*dispatcher_routes": {"limit": -1, "ttl": "2s"}
},
},
"schedulers": {
"enabled": true,
},
"rals": {
"enabled": true,
},
"chargers": {
"enabled": true,
},
"sessions": {
"enabled": true,
"rals_conns": ["*localhost"],
"resources_conns": ["*localhost"],
"chargers_conns": ["*localhost"],
"listen_bijson": ":3014",
},
"dispatchers":{
"enabled": true,
},
"apiers": {
"enabled": true,
"scheduler_conns": ["*internal"],
},
}

View File

@@ -0,0 +1,3 @@
#Tenant,ID,Subsystems,FilterIDs,ActivationInterval,Strategy,StrategyParameters,ConnID,ConnFilterIDs,ConnWeight,ConnBlocker,ConnParameters,Weight
cgrates.org,DSP1,*any,,,*weight,,ALL,,20,false,,10
cgrates.org,DSP1,,,,,,ALL2,,10,,,
1 #Tenant ID Subsystems FilterIDs ActivationInterval Strategy StrategyParameters ConnID ConnFilterIDs ConnWeight ConnBlocker ConnParameters Weight
2 cgrates.org DSP1 *any *weight ALL 20 false 10
3 cgrates.org DSP1 ALL2 10

View File

@@ -0,0 +1,186 @@
// +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 <http://www.gnu.org/licenses/>
*/
package dispatcherh
import (
"bytes"
"net/rpc"
"os/exec"
"path"
"testing"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
var (
dspDir string
dspCfgPath string
dspCfg *config.CGRConfig
dspCmd *exec.Cmd
dspRPC *rpc.Client
allDir string
allCfgPath string
allCmd *exec.Cmd
all2Dir string
all2CfgPath string
all2Cmd *exec.Cmd
dsphTest = []func(t *testing.T){
testDsphInitCfg,
testDsphInitDB,
testDsphStartEngine,
testDsphLoadData,
// testDsphBeforeDsphStart,
// testDsphStartAll2,
// testDsphStartAll,
// testDsphStopEngines,
testDsphStopDispatcher,
}
)
func TestDspHosts(t *testing.T) {
switch *dbType {
case utils.MetaMySQL:
allDir = "all_mysql"
all2Dir = "all2_mysql"
dspDir = "dispatchers_mysql"
case utils.MetaMongo:
allDir = "all_mongo"
all2Dir = "all2_mongo"
dspDir = "dispatchers_mongo"
case utils.MetaInternal, utils.MetaPostgres:
t.SkipNow()
default:
t.Fatal("Unknown Database type")
}
for _, stest := range dsphTest {
t.Run(dspDir, stest)
}
}
func testDsphInitCfg(t *testing.T) {
dspCfgPath = path.Join(*dataDir, "conf", "samples", "dispatcherh", dspDir)
allCfgPath = path.Join(*dataDir, "conf", "samples", "dispatcherh", allDir)
all2CfgPath = path.Join(*dataDir, "conf", "samples", "dispatcherh", all2Dir)
var err error
if dspCfg, err = config.NewCGRConfigFromPath(dspCfgPath); err != nil {
t.Error(err)
}
}
func testDsphInitDB(t *testing.T) {
if err := engine.InitDataDb(dspCfg); err != nil {
t.Fatal(err)
}
if err := engine.InitStorDb(dspCfg); err != nil {
t.Fatal(err)
}
}
func testDsphStartEngine(t *testing.T) {
var err error
if dspCmd, err = engine.StopStartEngine(dspCfgPath, *waitRater); err != nil {
t.Fatal(err)
}
dspRPC, err = newRPCClient(dspCfg.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 testDsphLoadData(t *testing.T) {
loader := exec.Command("cgr-loader", "-config_path", dspCfgPath, "-path", path.Join(*dataDir, "tariffplans", "dispatcherh"), "-caches_address=")
output := bytes.NewBuffer(nil)
outerr := bytes.NewBuffer(nil)
loader.Stdout = output
loader.Stderr = outerr
if err := loader.Run(); err != nil {
t.Log(loader.Args)
t.Log(output.String())
t.Log(outerr.String())
t.Fatal(err)
}
}
func testDsphGetNodeID() (id string, err error) {
var status map[string]interface{}
if err = dspRPC.Call(utils.CoreSv1Status, utils.TenantWithOpts{
TenantArg: &utils.TenantArg{
Tenant: "cgrates.org",
},
Opts: map[string]interface{}{},
}, &status); err != nil {
return
}
return utils.IfaceAsString(status[utils.NodeID]), nil
}
func testDsphBeforeDsphStart(t *testing.T) {
if _, err := testDsphGetNodeID(); err == nil || err.Error() != utils.ErrHostNotFound.Error() {
t.Errorf("Expeceted error: %s received: %v", utils.ErrHostNotFound, err)
}
}
func testDsphStartAll2(t *testing.T) {
var err error
if all2Cmd, err = engine.StartEngine(all2CfgPath, *waitRater); err != nil {
t.Fatal(err)
}
if nodeID, err := testDsphGetNodeID(); err != nil {
t.Fatal(err)
} else if nodeID != "ALL2" {
t.Errorf("Expected nodeID: %q ,received: %q", "ALL2", nodeID)
}
}
func testDsphStartAll(t *testing.T) {
var err error
if allCmd, err = engine.StartEngine(allCfgPath, *waitRater); err != nil {
t.Fatal(err)
}
if nodeID, err := testDsphGetNodeID(); err != nil {
t.Fatal(err)
} else if nodeID != "ALL" {
t.Errorf("Expected nodeID: %q ,received: %q", "ALL", nodeID)
}
}
func testDsphStopEngines(t *testing.T) {
if err := allCmd.Process.Kill(); err != nil {
t.Fatal(err)
}
if err := all2Cmd.Process.Kill(); err != nil {
t.Fatal(err)
}
if _, err := testDsphGetNodeID(); err == nil || err.Error() != utils.ErrHostNotFound.Error() {
t.Errorf("Expeceted error: %s received: %v", utils.ErrHostNotFound, err)
}
}
func testDsphStopDispatcher(t *testing.T) {
if err := engine.KillEngine(*waitRater); err != nil {
t.Error(err)
}
}

46
dispatcherh/lib_test.go Normal file
View File

@@ -0,0 +1,46 @@
/*
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 dispatcherh
import (
"errors"
"flag"
"net/rpc"
"net/rpc/jsonrpc"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
)
var (
dataDir = flag.String("data_dir", "/usr/share/cgrates", "CGR data dir path here")
waitRater = flag.Int("wait_rater", 100, "Number of milliseconds to wait for rater to start and cache")
encoding = flag.String("rpc", utils.MetaJSON, "what encoding would be used for rpc communication")
dbType = flag.String("dbtype", utils.MetaInternal, "The type of DataBase (Internal/Mongo/mySql)")
)
func newRPCClient(cfg *config.ListenCfg) (c *rpc.Client, err error) {
switch *encoding {
case utils.MetaJSON:
return jsonrpc.Dial(utils.TCP, cfg.RPCJSONListen)
case utils.MetaGOB:
return rpc.Dial(utils.TCP, cfg.RPCGOBListen)
default:
return nil, errors.New("UNSUPPORTED_RPC")
}
}

View File

@@ -231,7 +231,7 @@ func (*singleResultstrategyDispatcher) dispatch(dm *engine.DataManager, routeID
var called bool
for _, hostID := range hostIDs {
if dH, err = dm.GetDispatcherHost(tnt, hostID, true, true, utils.NonTransactional); err != nil {
if err != utils.ErrNotFound {
if err == utils.ErrNotFound {
utils.Logger.Warning(fmt.Sprintf("<%s> could not find host with ID %q",
utils.DispatcherS, hostID))
err = nil
@@ -268,7 +268,7 @@ func (*brodcastStrategyDispatcher) dispatch(dm *engine.DataManager, routeID stri
for _, hostID := range hostIDs {
var dH *engine.DispatcherHost
if dH, err = dm.GetDispatcherHost(tnt, hostID, true, true, utils.NonTransactional); err != nil {
if err != utils.ErrNotFound {
if err == utils.ErrNotFound {
utils.Logger.Warning(fmt.Sprintf("<%s> could not find host with ID %q",
utils.DispatcherS, hostID))
err = nil
@@ -382,7 +382,7 @@ func (ld *loadStrategyDispatcher) dispatch(dm *engine.DataManager, routeID strin
var called bool
for _, hostID := range lM.getHosts(hostIDs) {
if dH, err = dm.GetDispatcherHost(tnt, hostID, true, true, utils.NonTransactional); err != nil {
if err != utils.ErrNotFound {
if err == utils.ErrNotFound {
utils.Logger.Warning(fmt.Sprintf("<%s> could not find host with ID %q",
utils.DispatcherS, hostID))
err = nil

View File

@@ -34,6 +34,9 @@ results+=($?)
echo "go test github.com/cgrates/cgrates/dispatchers -tags=integration $@"
go test github.com/cgrates/cgrates/dispatchers -tags=integration $@
results+=($?)
echo "go test github.com/cgrates/cgrates/dispatcherh -tags=integration $@"
go test github.com/cgrates/cgrates/dispatcherh -tags=integration $@
results+=($?)
echo "go test github.com/cgrates/cgrates/apier/v1 -tags=offline $@"
go test github.com/cgrates/cgrates/apier/v1 -tags=offline $@
results+=($?)
@@ -69,6 +72,9 @@ results+=($?)
echo 'go test github.com/cgrates/cgrates/dispatchers -tags=integration -dbtype=*internal'
go test github.com/cgrates/cgrates/dispatchers -tags=integration -dbtype=*internal
results+=($?)
echo "go test github.com/cgrates/cgrates/dispatcherh -tags=integration -dbtype=*internal"
go test github.com/cgrates/cgrates/dispatcherh -tags=integration -dbtype=*internal
results+=($?)
echo 'go test github.com/cgrates/cgrates/apier/v1 -tags=offline -dbtype=*internal'
go test github.com/cgrates/cgrates/apier/v1 -tags=offline -dbtype=*internal
results+=($?)
@@ -103,6 +109,9 @@ results+=($?)
echo 'go test github.com/cgrates/cgrates/dispatchers -tags=integration -dbtype=*mysql'
go test github.com/cgrates/cgrates/dispatchers -tags=integration -dbtype=*mysql
results+=($?)
echo "go test github.com/cgrates/cgrates/dispatcherh -tags=integration -dbtype=*mysql"
go test github.com/cgrates/cgrates/dispatcherh -tags=integration -dbtype=*mysql
results+=($?)
echo 'go test github.com/cgrates/cgrates/apier/v1 -tags=offline -dbtype=*mysql'
go test github.com/cgrates/cgrates/apier/v1 -tags=offline -dbtype=*mysql
results+=($?)
@@ -137,6 +146,9 @@ results+=($?)
echo 'go test github.com/cgrates/cgrates/dispatchers -tags=integration -dbtype=*mongo'
go test github.com/cgrates/cgrates/dispatchers -tags=integration -dbtype=*mongo
results+=($?)
echo "go test github.com/cgrates/cgrates/dispatcherh -tags=integration -dbtype=*mongo"
go test github.com/cgrates/cgrates/dispatcherh -tags=integration -dbtype=*mongo
results+=($?)
echo 'go test github.com/cgrates/cgrates/apier/v1 -tags=offline -dbtype=*mongo'
go test github.com/cgrates/cgrates/apier/v1 -tags=offline -dbtype=*mongo
results+=($?)
@@ -171,6 +183,9 @@ results+=($?)
echo 'go test github.com/cgrates/cgrates/dispatchers -tags=integration -dbtype=*postgres'
go test github.com/cgrates/cgrates/dispatchers -tags=integration -dbtype=*postgres
results+=($?)
echo "go test github.com/cgrates/cgrates/dispatcherh -tags=integration -dbtype=*postgres"
go test github.com/cgrates/cgrates/dispatcherh -tags=integration -dbtype=*postgres
results+=($?)
echo 'go test github.com/cgrates/cgrates/apier/v1 -tags=offline -dbtype=*postgres'
go test github.com/cgrates/cgrates/apier/v1 -tags=offline -dbtype=*postgres
results+=($?)

View File

@@ -94,6 +94,7 @@ cgrates (0.11.0~dev) UNRELEASED; urgency=medium
* [CacheS] Updated ReloadCache and LoadCache APIs to use a map instead of a structure to be compatible with gob encoding
* [CGR-CONSOLE] Uniformize the commands between profile and subsystem
* [StatS] Add rounding operation for duration metric (e.g. acd, tcd, etc...)
* [DispatcherH] Added DispatcherH subsystem
-- DanB <danb@cgrates.org> Wed, 19 Feb 2020 13:25:52 +0200