Aded tests for DispatcherService ThresholdSv1

This commit is contained in:
Trial97
2019-02-12 10:27:19 +02:00
committed by Dan Christian Bogos
parent e808255735
commit 70c0c233d6
11 changed files with 126 additions and 207 deletions

View File

@@ -97,7 +97,7 @@ func (cSv1 *ChargerSv1) Call(serviceMethod string,
return utils.APIerRPCCall(cSv1, serviceMethod, args, reply)
}
func (cSv1 *ChargerSv1) Ping(ign utils.CGREvent, reply *string) error {
func (cSv1 *ChargerSv1) Ping(ign *utils.CGREvent, reply *string) error {
*reply = utils.Pong
return nil
}

View File

@@ -83,8 +83,6 @@ func (apierV1 *ApierV1) RemoveDispatcherProfile(arg *utils.TenantID, reply *stri
return nil
}
/*
func NewDispatcherThresholdSv1(dps *dispatchers.DispatcherService) *DispatcherThresholdSv1 {
return &DispatcherThresholdSv1{dS: dps}
}
@@ -95,8 +93,8 @@ type DispatcherThresholdSv1 struct {
}
// Ping implements ThresholdSv1Ping
func (dT *DispatcherThresholdSv1) Ping(ign string, reply *string) error {
return dT.dS.ThresholdSv1Ping(ign, reply)
func (dT *DispatcherThresholdSv1) Ping(args *dispatchers.CGREvWithApiKey, reply *string) error {
return dT.dS.ThresholdSv1Ping(args, reply)
}
// GetThresholdsForEvent implements ThresholdSv1GetThresholdsForEvent
@@ -111,6 +109,7 @@ func (dT *DispatcherThresholdSv1) ProcessEvent(args *dispatchers.ArgsProcessEven
return dT.dS.ThresholdSv1ProcessEvent(args, tIDs)
}
/*
func NewDispatcherStatSv1(dps *dispatchers.DispatcherService) *DispatcherStatSv1 {
return &DispatcherStatSv1{dS: dps}
}

View File

@@ -116,7 +116,7 @@ func (apierV1 *ApierV1) RemoveThresholdProfile(args *utils.TenantID, reply *stri
return nil
}
func (tSv1 *ThresholdSv1) Ping(ign string, reply *string) error {
func (tSv1 *ThresholdSv1) Ping(ign *utils.CGREvent, reply *string) error {
*reply = utils.Pong
return nil
}

View File

@@ -1024,11 +1024,9 @@ func startDispatcherService(internalDispatcherSChan chan *dispatchers.Dispatcher
return
}()
server.RpcRegisterName(utils.ThresholdSv1,
v1.NewDispatcherThresholdSv1(dspS))
/*
if !cfg.ThresholdSCfg().Enabled && len(cfg.DispatcherSCfg().ThreshSConns) != 0 {
server.RpcRegisterName(utils.ThresholdSv1,
v1.NewDispatcherThresholdSv1(dspS))
}
if !cfg.StatSCfg().Enabled && len(cfg.DispatcherSCfg().StatSConns) != 0 {
server.RpcRegisterName(utils.StatSv1,
v1.NewDispatcherStatSv1(dspS))

View File

@@ -40,4 +40,9 @@
],
},
"thresholds": {
"enabled": true,
},
}

View File

@@ -40,4 +40,8 @@
],
},
"thresholds": {
"enabled": true,
},
}

View File

@@ -3,5 +3,6 @@ cgrates.org,ATTR_1001_SIMPLEAUTH,simpleauth,*string:Account:1001,,Password,*any,
cgrates.org,ATTR_API_ATTR_FAKE_AUTH,*auth,*string:APIKey:12345,,APIMethods,*any,,true,false,20
cgrates.org,ATTR_API_ATTR_AUTH,*auth,*string:APIKey:attr12345,,APIMethods,*any,AttributeSv1.Ping&AttributeSv1.GetAttributeForEvent&AttributeSv1.ProcessEvent,true,false,20
cgrates.org,ATTR_API_CHRG_AUTH,*auth,*string:APIKey:chrg12345,,APIMethods,*any,ChargerSv1.Ping&ChargerSv1.GetChargersForEvent&ChargerSv1.ProcessEvent,true,false,20
cgrates.org,ATTR_API_THR_AUTH,*auth,*string:APIKey:thr12345,,APIMethods,*any,ThresholdSv1.Ping&ThresholdSv1.GetThresholdsForEvent&ThresholdSv1.ProcessEvent,true,false,20
1 #Tenant ID Contexts FilterIDs ActivationInterval FieldName Initial Substitute Append Blocker Weight
3 cgrates.org ATTR_API_ATTR_FAKE_AUTH *auth *string:APIKey:12345 APIMethods *any true false 20
4 cgrates.org ATTR_API_ATTR_AUTH *auth *string:APIKey:attr12345 APIMethods *any AttributeSv1.Ping&AttributeSv1.GetAttributeForEvent&AttributeSv1.ProcessEvent true false 20
5 cgrates.org ATTR_API_CHRG_AUTH *auth *string:APIKey:chrg12345 APIMethods *any ChargerSv1.Ping&ChargerSv1.GetChargersForEvent&ChargerSv1.ProcessEvent true false 20
6 cgrates.org ATTR_API_THR_AUTH *auth *string:APIKey:thr12345 APIMethods *any ThresholdSv1.Ping&ThresholdSv1.GetThresholdsForEvent&ThresholdSv1.ProcessEvent true false 20
7
8

View File

@@ -60,7 +60,7 @@ func TestDspChargerS(t *testing.T) {
func testDspCppPingFailover(t *testing.T) {
var reply string
if err := allEngine.RCP.Call(utils.ChargerSv1Ping, utils.CGREvent{}, &reply); err != nil {
if err := allEngine.RCP.Call(utils.ChargerSv1Ping, &utils.CGREvent{}, &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Received: %s", reply)
@@ -132,7 +132,7 @@ func testDspCppGetChtgFailover(t *testing.T) {
func testDspCppPing(t *testing.T) {
var reply string
if err := allEngine.RCP.Call(utils.ChargerSv1Ping, utils.CGREvent{}, &reply); err != nil {
if err := allEngine.RCP.Call(utils.ChargerSv1Ping, &utils.CGREvent{}, &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Received: %s", reply)

View File

@@ -18,40 +18,45 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package dispatchers
/*
import (
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
func (dS *DispatcherService) ThresholdSv1Ping(ign string, reply *string) error {
if dS.thdS == nil {
return utils.NewErrNotConnected(utils.ThresholdS)
func (dS *DispatcherService) ThresholdSv1Ping(args *CGREvWithApiKey, reply *string) (err error) {
if dS.attrS != nil {
if err = dS.authorize(utils.ThresholdSv1Ping,
args.CGREvent.Tenant,
args.APIKey, args.CGREvent.Time); err != nil {
return
}
}
return dS.thdS.Call(utils.ThresholdSv1Ping, ign, reply)
return dS.Dispatch(&args.CGREvent, utils.MetaThresholds,
utils.ThresholdSv1Ping, args.CGREvent, reply)
}
func (dS *DispatcherService) ThresholdSv1GetThresholdsForEvent(args *ArgsProcessEventWithApiKey,
t *engine.Thresholds) (err error) {
if dS.thdS == nil {
return utils.NewErrNotConnected(utils.ThresholdS)
if dS.attrS != nil {
if err = dS.authorize(utils.ThresholdSv1GetThresholdsForEvent,
args.ArgsProcessEvent.CGREvent.Tenant,
args.APIKey, args.ArgsProcessEvent.CGREvent.Time); err != nil {
return
}
}
if err = dS.authorize(utils.ThresholdSv1GetThresholdsForEvent, args.ArgsProcessEvent.CGREvent.Tenant,
args.APIKey, args.ArgsProcessEvent.CGREvent.Time); err != nil {
return
}
return dS.thdS.Call(utils.ThresholdSv1GetThresholdsForEvent, args.ArgsProcessEvent, t)
return dS.Dispatch(&args.CGREvent, utils.MetaThresholds,
utils.ThresholdSv1GetThresholdsForEvent, args.ArgsProcessEvent, t)
}
func (dS *DispatcherService) ThresholdSv1ProcessEvent(args *ArgsProcessEventWithApiKey,
tIDs *[]string) (err error) {
if dS.thdS == nil {
return utils.NewErrNotConnected(utils.ThresholdS)
if dS.attrS != nil {
if err = dS.authorize(utils.ThresholdSv1ProcessEvent,
args.ArgsProcessEvent.CGREvent.Tenant,
args.APIKey, args.ArgsProcessEvent.CGREvent.Time); err != nil {
return
}
}
if err = dS.authorize(utils.ThresholdSv1ProcessEvent, args.ArgsProcessEvent.CGREvent.Tenant,
args.APIKey, args.ArgsProcessEvent.CGREvent.Time); err != nil {
return
}
return dS.thdS.Call(utils.ThresholdSv1ProcessEvent, args.ArgsProcessEvent, tIDs)
return dS.Dispatch(&args.CGREvent, utils.MetaThresholds,
utils.ThresholdSv1ProcessEvent, args.ArgsProcessEvent, tIDs)
}
*/

View File

@@ -21,164 +21,125 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package dispatchers
import (
"net/rpc"
"net/rpc/jsonrpc"
"path"
"reflect"
"testing"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
var (
dspThCfgPath string
dspThCfg *config.CGRConfig
dspThRPC *rpc.Client
instThCfgPath string
instThCfg *config.CGRConfig
instThRPC *rpc.Client
)
var sTestsDspTh = []func(t *testing.T){
testDspThInitCfg,
testDspThInitDataDb,
testDspThResetStorDb,
testDspThStartEngine,
testDspThRPCConn,
testDspThPingFailover,
testDspThProcessEventFailover,
testDspThPing,
testDspThLoadData,
testDspThAddAttributesWithPermision,
testDspThTestAuthKey,
testDspThAddAttributesWithPermision2,
testDspThTestAuthKey2,
testDspThKillEngine,
}
//Test start here
func TestDspThresholdS(t *testing.T) {
allEngine = newTestEngine(t, path.Join(dspDataDir, "conf", "samples", "dispatchers", "all"), true, true)
allEngine2 = newTestEngine(t, path.Join(dspDataDir, "conf", "samples", "dispatchers", "all2"), true, true)
attrEngine = newTestEngine(t, path.Join(dspDataDir, "conf", "samples", "dispatchers", "attributes"), true, true)
dispEngine = newTestEngine(t, path.Join(dspDataDir, "conf", "samples", "dispatchers", "dispatchers"), true, true)
allEngine.loadData(t, path.Join(dspDataDir, "tariffplans", "tutorial"))
allEngine2.loadData(t, path.Join(dspDataDir, "tariffplans", "oldtutorial"))
attrEngine.loadData(t, path.Join(dspDataDir, "tariffplans", "dispatchers"))
time.Sleep(500 * time.Millisecond)
for _, stest := range sTestsDspTh {
t.Run("", stest)
}
attrEngine.stopEngine(t)
dispEngine.stopEngine(t)
allEngine.stopEngine(t)
allEngine2.stopEngine(t)
}
func testDspThInitCfg(t *testing.T) {
var err error
dspThCfgPath = path.Join(dspDataDir, "conf", "samples", "dispatcher")
dspThCfg, err = config.NewCGRConfigFromFolder(dspThCfgPath)
if err != nil {
func testDspThPingFailover(t *testing.T) {
var reply string
if err := allEngine.RCP.Call(utils.ThresholdSv1Ping, &utils.CGREvent{}, &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Received: %s", reply)
}
dspThCfg.DataFolderPath = dspDataDir // Share DataFolderPath through config towards StoreDb for Flush()
config.SetCgrConfig(dspThCfg)
instThCfgPath = path.Join(dspDataDir, "conf", "samples", "tutmysql")
instThCfg, err = config.NewCGRConfigFromFolder(instThCfgPath)
if err != nil {
ev := CGREvWithApiKey{
CGREvent: utils.CGREvent{
Tenant: "cgrates.org",
},
APIKey: "thr12345",
}
if err := dispEngine.RCP.Call(utils.ThresholdSv1Ping, &ev, &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Received: %s", reply)
}
instThCfg.DataFolderPath = dspDataDir // Share DataFolderPath through config towards StoreDb for Flush()
config.SetCgrConfig(instThCfg)
allEngine.stopEngine(t)
if err := dispEngine.RCP.Call(utils.ThresholdSv1Ping, &ev, &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Received: %s", reply)
}
allEngine2.stopEngine(t)
if err := dispEngine.RCP.Call(utils.ThresholdSv1Ping, &ev, &reply); err == nil {
t.Errorf("Expected error but recived %v and reply %v\n", err, reply)
}
allEngine.startEngine(t)
allEngine2.startEngine(t)
}
func testDspThInitDataDb(t *testing.T) {
if err := engine.InitDataDb(instThCfg); err != nil {
t.Fatal(err)
func testDspThProcessEventFailover(t *testing.T) {
var ids []string
eIDs := []string{"THD_ACNT_1001"}
nowTime := time.Now()
args := &ArgsProcessEventWithApiKey{
APIKey: "thr12345",
ArgsProcessEvent: engine.ArgsProcessEvent{
CGREvent: utils.CGREvent{
Tenant: "cgrates.org",
ID: utils.UUIDSha1Prefix(),
Time: &nowTime,
Event: map[string]interface{}{
utils.EVENT_NAME: "Event1",
utils.Account: "1001"},
},
},
}
}
// Wipe out the cdr database
func testDspThResetStorDb(t *testing.T) {
if err := engine.InitStorDb(instThCfg); err != nil {
t.Fatal(err)
if err := dispEngine.RCP.Call(utils.ThresholdSv1ProcessEvent, args,
&ids); err == nil || err.Error() != utils.ErrNotFound.Error() {
t.Errorf("Expected error NOT_FOUND but recived %v and reply %v\n", err, ids)
}
}
// Start CGR Engine
func testDspThStartEngine(t *testing.T) {
if _, err := engine.StartEngine(instThCfgPath, dspDelay); err != nil {
t.Fatal(err)
}
if _, err := engine.StartEngine(dspThCfgPath, dspDelay); err != nil {
t.Fatal(err)
}
}
// Connect rpc client to rater
func testDspThRPCConn(t *testing.T) {
var err error
instThRPC, err = jsonrpc.Dial("tcp", instThCfg.ListenCfg().RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
if err != nil {
t.Fatal(err)
}
dspThRPC, err = jsonrpc.Dial("tcp", dspThCfg.ListenCfg().RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
if err != nil {
t.Fatal(err)
allEngine2.stopEngine(t)
if err := dispEngine.RCP.Call(utils.ThresholdSv1ProcessEvent, args, &ids); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eIDs, ids) {
t.Errorf("expecting: %+v, received: %+v", eIDs, ids)
}
allEngine2.startEngine(t)
}
func testDspThPing(t *testing.T) {
var reply string
if err := instThRPC.Call(utils.ThresholdSv1Ping, "", &reply); err != nil {
if err := allEngine.RCP.Call(utils.ThresholdSv1Ping, &utils.CGREvent{}, &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Received: %s", reply)
}
if err := dspThRPC.Call(utils.ThresholdSv1Ping, "", &reply); err != nil {
if err := dispEngine.RCP.Call(utils.ThresholdSv1Ping, &CGREvWithApiKey{
CGREvent: utils.CGREvent{
Tenant: "cgrates.org",
},
APIKey: "thr12345",
}, &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Received: %s", reply)
}
}
func testDspThLoadData(t *testing.T) {
var reply string
attrs := &utils.AttrLoadTpFromFolder{
FolderPath: path.Join(dspDataDir, "tariffplans", "tutorial")}
if err := instThRPC.Call("ApierV1.LoadTariffPlanFromFolder", attrs, &reply); err != nil {
t.Error(err)
}
time.Sleep(500 * time.Millisecond)
}
func testDspThAddAttributesWithPermision(t *testing.T) {
alsPrf := &engine.AttributeProfile{
Tenant: "cgrates.org",
ID: "AuthKey",
Contexts: []string{utils.MetaAuth},
FilterIDs: []string{"*string:APIKey:12345"},
ActivationInterval: &utils.ActivationInterval{
ActivationTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC),
},
Attributes: []*engine.Attribute{
{
FieldName: utils.APIMethods,
Initial: utils.META_ANY,
Substitute: config.NewRSRParsersMustCompile("ThresholdSv1.GetThresholdsForEvent", true, utils.INFIELD_SEP),
Append: true,
},
},
Weight: 20,
}
var result string
if err := instThRPC.Call("ApierV1.SetAttributeProfile", alsPrf, &result); err != nil {
t.Error(err)
} else if result != utils.OK {
t.Error("Unexpected reply returned", result)
}
alsPrf.Compile()
var reply *engine.AttributeProfile
if err := instThRPC.Call("ApierV1.GetAttributeProfile",
&utils.TenantID{Tenant: "cgrates.org", ID: "AuthKey"}, &reply); err != nil {
t.Error(err)
}
reply.Compile()
if !reflect.DeepEqual(alsPrf, reply) {
t.Errorf("Expecting : %+v, received: %+v", alsPrf, reply)
}
}
func testDspThTestAuthKey(t *testing.T) {
var ids []string
nowTime := time.Now()
@@ -195,59 +156,14 @@ func testDspThTestAuthKey(t *testing.T) {
},
}
if err := dspThRPC.Call(utils.ThresholdSv1ProcessEvent,
if err := dispEngine.RCP.Call(utils.ThresholdSv1ProcessEvent,
args, &ids); err == nil || err.Error() != utils.ErrUnauthorizedApi.Error() {
t.Error(err)
}
var th *engine.Thresholds
eTh := &engine.Thresholds{
&engine.Threshold{
Tenant: "cgrates.org",
ID: "THD_ACNT_1002",
Hits: 0,
},
}
if err := dspThRPC.Call(utils.ThresholdSv1GetThresholdsForEvent, args, &th); err != nil {
if err := dispEngine.RCP.Call(utils.ThresholdSv1GetThresholdsForEvent, args,
&th); err == nil || err.Error() != utils.ErrUnauthorizedApi.Error() {
t.Error(err)
} else if !reflect.DeepEqual(eTh, th) {
t.Errorf("expecting: %+v, received: %+v", eTh, th)
}
}
func testDspThAddAttributesWithPermision2(t *testing.T) {
alsPrf := &engine.AttributeProfile{
Tenant: "cgrates.org",
ID: "AuthKey",
Contexts: []string{utils.MetaAuth},
FilterIDs: []string{"*string:APIKey:12345"},
ActivationInterval: &utils.ActivationInterval{
ActivationTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC),
},
Attributes: []*engine.Attribute{
{
FieldName: utils.APIMethods,
Initial: utils.META_ANY,
Substitute: config.NewRSRParsersMustCompile("ThresholdSv1.ProcessEvent&ThresholdSv1.GetThresholdsForEvent", true, utils.INFIELD_SEP),
Append: true,
},
},
Weight: 20,
}
var result string
if err := instThRPC.Call("ApierV1.SetAttributeProfile", alsPrf, &result); err != nil {
t.Error(err)
} else if result != utils.OK {
t.Error("Unexpected reply returned", result)
}
alsPrf.Compile()
var reply *engine.AttributeProfile
if err := instThRPC.Call("ApierV1.GetAttributeProfile",
&utils.TenantID{Tenant: "cgrates.org", ID: "AuthKey"}, &reply); err != nil {
t.Error(err)
}
reply.Compile()
if !reflect.DeepEqual(alsPrf, reply) {
t.Errorf("Expecting : %+v, received: %+v", alsPrf, reply)
}
}
@@ -256,7 +172,7 @@ func testDspThTestAuthKey2(t *testing.T) {
eIDs := []string{"THD_ACNT_1002"}
nowTime := time.Now()
args := &ArgsProcessEventWithApiKey{
APIKey: "12345",
APIKey: "thr12345",
ArgsProcessEvent: engine.ArgsProcessEvent{
CGREvent: utils.CGREvent{
Tenant: "cgrates.org",
@@ -268,7 +184,7 @@ func testDspThTestAuthKey2(t *testing.T) {
},
}
if err := dspThRPC.Call(utils.ThresholdSv1ProcessEvent, args, &ids); err != nil {
if err := dispEngine.RCP.Call(utils.ThresholdSv1ProcessEvent, args, &ids); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eIDs, ids) {
t.Errorf("expecting: %+v, received: %+v", eIDs, ids)
@@ -281,7 +197,7 @@ func testDspThTestAuthKey2(t *testing.T) {
Hits: 1,
},
}
if err := dspThRPC.Call(utils.ThresholdSv1GetThresholdsForEvent, args, &th); err != nil {
if err := dispEngine.RCP.Call(utils.ThresholdSv1GetThresholdsForEvent, args, &th); err != nil {
t.Error(err)
} else if !reflect.DeepEqual((*eTh)[0].Tenant, (*th)[0].Tenant) {
t.Errorf("expecting: %+v, received: %+v", (*eTh)[0].Tenant, (*th)[0].Tenant)
@@ -291,12 +207,3 @@ func testDspThTestAuthKey2(t *testing.T) {
t.Errorf("expecting: %+v, received: %+v", (*eTh)[0].Hits, (*th)[0].Hits)
}
}
func testDspThKillEngine(t *testing.T) {
if err := engine.KillEngine(dspDelay); err != nil {
t.Error(err)
}
if err := engine.KillEngine(dspDelay); err != nil {
t.Error(err)
}
}

View File

@@ -106,17 +106,17 @@ func testTLSRpcConn(t *testing.T) {
func testTLSPing(t *testing.T) {
var reply string
if err := tlsRpcClientJson.Call(utils.ThresholdSv1Ping, "", &reply); err != nil {
if err := tlsRpcClientJson.Call(utils.ThresholdSv1Ping, &utils.CGREvent{}, &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Received: %s", reply)
}
if err := tlsRpcClientGob.Call(utils.ThresholdSv1Ping, "", &reply); err != nil {
if err := tlsRpcClientGob.Call(utils.ThresholdSv1Ping, &utils.CGREvent{}, &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Received: %s", reply)
}
if err := tlsHTTPJson.Call(utils.ThresholdSv1Ping, "", &reply); err != nil {
if err := tlsHTTPJson.Call(utils.ThresholdSv1Ping, &utils.CGREvent{}, &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Received: %s", reply)