Add EEs APIs to dispatchers + tests

This commit is contained in:
ionutboangiu
2022-10-14 13:14:46 +03:00
committed by Dan Christian Bogos
parent c8b7833fc0
commit c2e847018a
14 changed files with 898 additions and 577 deletions

70
dispatchers/ees.go Normal file
View File

@@ -0,0 +1,70 @@
/*
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 dispatchers
import (
"time"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
func (dS *DispatcherService) EeSv1Ping(args *utils.CGREvent, reply *string) (err error) {
tnt := dS.cfg.GeneralCfg().DefaultTenant
if args != nil && len(args.Tenant) != 0 {
tnt = args.Tenant
}
ev := make(map[string]interface{})
if args != nil {
ev = args.Event
}
opts := make(map[string]interface{})
if args != nil {
opts = args.APIOpts
}
if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 {
if err = dS.authorize(utils.EeSv1Ping, tnt,
utils.IfaceAsString(opts[utils.OptsAPIKey]), utils.TimePointer(time.Now())); err != nil {
return
}
}
return dS.Dispatch(&utils.CGREvent{Tenant: tnt, Event: ev, APIOpts: opts}, utils.MetaCore, utils.EeSv1Ping, args, reply)
}
func (dS *DispatcherService) EeSv1ProcessEvent(args *engine.CGREventWithEeIDs, reply *map[string]map[string]interface{}) (err error) {
tnt := dS.cfg.GeneralCfg().DefaultTenant
if args != nil && len(args.Tenant) != 0 {
tnt = args.Tenant
}
ev := make(map[string]interface{})
if args != nil {
ev = args.Event
}
opts := make(map[string]interface{})
if args != nil {
opts = args.APIOpts
}
if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 {
if err = dS.authorize(utils.EeSv1ProcessEvent, tnt,
utils.IfaceAsString(opts[utils.OptsAPIKey]), utils.TimePointer(time.Now())); err != nil {
return
}
}
return dS.Dispatch(&utils.CGREvent{Tenant: tnt, Event: ev, APIOpts: opts}, utils.MetaCore, utils.EeSv1ProcessEvent, args, reply)
}

216
dispatchers/ees_it_test.go Normal file
View File

@@ -0,0 +1,216 @@
//go:build integration
// +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 dispatchers
import (
"testing"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
var sTestsDspEEs = []func(t *testing.T){
testDspEEsPingFailover,
testDspEEsProcessEventFailover,
testDspEEsProcessEventRoundRobin,
testDspEEsPing,
testDspEEsTestAuthKey,
testDspEEsTestAuthKey2,
}
func TestDspEEsIT(t *testing.T) {
var config1, config2, config3 string
switch *dbType {
case utils.MetaInternal:
t.SkipNow()
case utils.MetaMySQL:
config1 = "all_mysql"
config2 = "all2_mysql"
config3 = "dispatchers_mysql"
case utils.MetaMongo:
config1 = "all_mongo"
config2 = "all2_mongo"
config3 = "dispatchers_mongo"
case utils.MetaPostgres:
t.SkipNow()
default:
t.Fatal("Unknown Database type")
}
dispDIR := "dispatchers"
if *encoding == utils.MetaGOB {
dispDIR += "_gob"
}
testDsp(t, sTestsDspEEs, "TestDspEEs", config1, config2, config3, "tutorial", "oldtutorial", dispDIR)
}
func testDspEEsPingFailover(t *testing.T) {
var reply string
if err := allEngine.RPC.Call(utils.EeSv1Ping, new(utils.CGREvent), &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Unexpected reply: %s", reply)
}
ev := &utils.CGREvent{
Tenant: "cgrates.org",
APIOpts: map[string]interface{}{
utils.OptsAPIKey: "ees12345",
},
}
if err := dispEngine.RPC.Call(utils.EeSv1Ping, ev, &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Unexpected reply: %s", reply)
}
allEngine.stopEngine(t)
if err := dispEngine.RPC.Call(utils.EeSv1Ping, ev, &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Unexpected reply: %s", reply)
}
allEngine2.stopEngine(t)
if err := dispEngine.RPC.Call(utils.EeSv1Ping, ev, &reply); err == nil {
t.Errorf("Expected error but received %v and reply %v\n", err, reply)
}
allEngine.startEngine(t)
allEngine2.startEngine(t)
}
func testDspEEsProcessEventFailover(t *testing.T) {
args := &engine.CGREventWithEeIDs{
CGREvent: &utils.CGREvent{
Tenant: "cgrates.org",
ID: "event1",
Event: map[string]interface{}{
utils.EventName: "Event1",
utils.AccountField: "1001",
},
APIOpts: map[string]interface{}{
utils.OptsAPIKey: "ees12345",
},
},
}
var reply map[string]map[string]interface{}
if err := dispEngine.RPC.Call(utils.EeSv1ProcessEvent, args, &reply); err == nil ||
err.Error() != utils.ErrNotFound.Error() {
t.Errorf("expected: <%+v>, \nreceived: <%+v>", utils.ErrNotFound, err)
}
allEngine2.stopEngine(t)
if err := dispEngine.RPC.Call(utils.EeSv1ProcessEvent,
args, &reply); err != nil {
t.Fatal(err)
}
allEngine2.startEngine(t)
}
func testDspEEsPing(t *testing.T) {
var reply string
if err := allEngine.RPC.Call(utils.EeSv1Ping, new(utils.CGREvent), &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Received: %s", reply)
}
if err := dispEngine.RPC.Call(utils.EeSv1Ping, &utils.CGREvent{
Tenant: "cgrates.org",
APIOpts: map[string]interface{}{
utils.OptsAPIKey: "ees12345",
},
}, &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Received: %s", reply)
}
}
func testDspEEsTestAuthKey(t *testing.T) {
args := &engine.CGREventWithEeIDs{
CGREvent: &utils.CGREvent{
Tenant: "cgrates.org",
ID: "event1",
Event: map[string]interface{}{
utils.AccountField: "1001",
},
APIOpts: map[string]interface{}{
utils.OptsAPIKey: "12345",
},
},
}
var reply map[string]map[string]interface{}
if err := dispEngine.RPC.Call(utils.EeSv1ProcessEvent,
args, &reply); err == nil || err.Error() != utils.ErrUnauthorizedApi.Error() {
t.Errorf("expected: <%+v>,\nreceived: <%+v>", utils.ErrUnauthorizedApi.Error(), err)
}
}
func testDspEEsTestAuthKey2(t *testing.T) {
args := &engine.CGREventWithEeIDs{
CGREvent: &utils.CGREvent{
Tenant: "cgrates.org",
ID: "event1",
Event: map[string]interface{}{
utils.AccountField: "1001",
},
APIOpts: map[string]interface{}{
utils.OptsAPIKey: "ees12345",
},
},
}
var reply map[string]map[string]interface{}
if err := dispEngine.RPC.Call(utils.EeSv1ProcessEvent,
args, &reply); err != nil {
t.Error(err)
} else if _, ok := reply[utils.MetaDefault]; !ok {
t.Error("expected to match the *default exporter")
}
}
func testDspEEsProcessEventRoundRobin(t *testing.T) {
args := &engine.CGREventWithEeIDs{
CGREvent: &utils.CGREvent{
Tenant: "cgrates.org",
ID: "event1",
Event: map[string]interface{}{
utils.EventName: "RoundRobin",
utils.AccountField: "1001",
},
APIOpts: map[string]interface{}{
utils.OptsAPIKey: "ees12345",
},
},
}
var reply map[string]map[string]interface{}
// To ALL2
if err := dispEngine.RPC.Call(utils.EeSv1ProcessEvent,
args, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() {
t.Errorf("expected: <%+v>, \nreceived: <%+v>", utils.ErrNotFound, err)
}
// To ALL
if err := dispEngine.RPC.Call(utils.EeSv1ProcessEvent,
args, &reply); err != nil {
t.Error(err)
} else if _, ok := reply[utils.MetaDefault]; !ok {
t.Error("expected to match the *default exporter")
}
}

View File

@@ -64,7 +64,7 @@ type testDispatcher struct {
cmd *exec.Cmd
}
func newTestEngine(t *testing.T, cfgPath string, initDataDB, intitStoreDB bool) (d *testDispatcher) {
func newTestEngine(t *testing.T, cfgPath string, initDataDB, initStoreDB bool) (d *testDispatcher) {
d = new(testDispatcher)
d.CfgPath = cfgPath
var err error
@@ -78,7 +78,7 @@ func newTestEngine(t *testing.T, cfgPath string, initDataDB, intitStoreDB bool)
d.initDataDb(t)
}
if intitStoreDB {
if initStoreDB {
d.resetStorDb(t)
}
d.startEngine(t)