Aded tests for DispatcherService ResourceSv1

This commit is contained in:
Trial97
2019-02-12 14:06:53 +02:00
committed by Dan Christian Bogos
parent 51abeb5c32
commit bf6b801372
11 changed files with 100 additions and 227 deletions

View File

@@ -139,7 +139,6 @@ func (dSts *DispatcherStatSv1) ProcessEvent(args *dispatchers.ArgsStatProcessEve
return dSts.dS.StatSv1ProcessEvent(args, reply)
}
/*
func NewDispatcherResourceSv1(dps *dispatchers.DispatcherService) *DispatcherResourceSv1 {
return &DispatcherResourceSv1{dRs: dps}
}
@@ -150,8 +149,8 @@ type DispatcherResourceSv1 struct {
}
// Ping implements ResourceSv1Ping
func (dRs *DispatcherResourceSv1) Ping(ign string, reply *string) error {
return dRs.dRs.ResourceSv1Ping(ign, reply)
func (dRs *DispatcherResourceSv1) Ping(args *dispatchers.CGREvWithApiKey, reply *string) error {
return dRs.dRs.ResourceSv1Ping(args, reply)
}
// GetResourcesForEvent implements ResourceSv1GetResourcesForEvent
@@ -159,7 +158,7 @@ func (dRs *DispatcherResourceSv1) GetResourcesForEvent(args *dispatchers.ArgsV1R
reply *engine.Resources) error {
return dRs.dRs.ResourceSv1GetResourcesForEvent(args, reply)
}
*/
func NewDispatcherSupplierSv1(dps *dispatchers.DispatcherService) *DispatcherSupplierSv1 {
return &DispatcherSupplierSv1{dSup: dps}
}

View File

@@ -121,7 +121,7 @@ func (apierV1 *ApierV1) RemoveResourceProfile(arg utils.TenantID, reply *string)
return nil
}
func (rsv1 *ResourceSv1) Ping(ign string, reply *string) error {
func (rsv1 *ResourceSv1) Ping(ign *utils.CGREvent, reply *string) error {
*reply = utils.Pong
return nil
}

View File

@@ -679,7 +679,7 @@ func testV1RsGetResourceProfileAfterDelete(t *testing.T) {
func testV1RsResourcePing(t *testing.T) {
var resp string
if err := rlsV1Rpc.Call(utils.ResourceSv1Ping, "", &resp); err != nil {
if err := rlsV1Rpc.Call(utils.ResourceSv1Ping, &utils.CGREvent{}, &resp); err != nil {
t.Error(err)
} else if resp != utils.Pong {
t.Error("Unexpected reply returned", resp)

View File

@@ -1029,12 +1029,10 @@ func startDispatcherService(internalDispatcherSChan chan *dispatchers.Dispatcher
server.RpcRegisterName(utils.StatSv1,
v1.NewDispatcherStatSv1(dspS))
/*
if !cfg.ResourceSCfg().Enabled && len(cfg.DispatcherSCfg().ResSConns) != 0 {
server.RpcRegisterName(utils.ResourceSv1,
v1.NewDispatcherResourceSv1(dspS))
}
*/
server.RpcRegisterName(utils.ResourceSv1,
v1.NewDispatcherResourceSv1(dspS))
server.RpcRegisterName(utils.SupplierSv1,
v1.NewDispatcherSupplierSv1(dspS))

View File

@@ -50,10 +50,10 @@
"stats": {
"enabled": true,
"store_interval": "1s",
"thresholds_conns": [
{"address": "*internal"}
],
},
"resources": {
"enabled": true,
},
}

View File

@@ -50,10 +50,10 @@
"stats": {
"enabled": true,
"store_interval": "1s",
"thresholds_conns": [
{"address": "*internal"}
],
},
"resources": {
"enabled": true,
},
}

View File

@@ -6,5 +6,5 @@ cgrates.org,ATTR_API_CHRG_AUTH,*auth,*string:APIKey:chrg12345,,APIMethods,*any,C
cgrates.org,ATTR_API_THR_AUTH,*auth,*string:APIKey:thr12345,,APIMethods,*any,ThresholdSv1.Ping&ThresholdSv1.GetThresholdsForEvent&ThresholdSv1.ProcessEvent,true,false,20
cgrates.org,ATTR_API_SUP_AUTH,*auth,*string:APIKey:sup12345,,APIMethods,*any,SupplierSv1.Ping&SupplierSv1.GetSuppliers,true,false,20
cgrates.org,ATTR_API_STAT_AUTH,*auth,*string:APIKey:stat12345,,APIMethods,*any,StatSv1.Ping&StatSv1.GetStatQueuesForEvent&StatSv1.GetQueueStringMetrics&StatSv1.ProcessEvent,true,false,20
cgrates.org,ATTR_API_RES_AUTH,*auth,*string:APIKey:res12345,,APIMethods,*any,ResourceSv1.Ping&ResourceSv1.GetResourcesForEvent,true,false,20
1 #Tenant ID Contexts FilterIDs ActivationInterval FieldName Initial Substitute Append Blocker Weight
6 cgrates.org ATTR_API_THR_AUTH *auth *string:APIKey:thr12345 APIMethods *any ThresholdSv1.Ping&ThresholdSv1.GetThresholdsForEvent&ThresholdSv1.ProcessEvent true false 20
7 cgrates.org ATTR_API_SUP_AUTH *auth *string:APIKey:sup12345 APIMethods *any SupplierSv1.Ping&SupplierSv1.GetSuppliers true false 20
8 cgrates.org ATTR_API_STAT_AUTH *auth *string:APIKey:stat12345 APIMethods *any StatSv1.Ping&StatSv1.GetStatQueuesForEvent&StatSv1.GetQueueStringMetrics&StatSv1.ProcessEvent true false 20
9 cgrates.org ATTR_API_RES_AUTH *auth *string:APIKey:res12345 APIMethods *any ResourceSv1.Ping&ResourceSv1.GetResourcesForEvent true false 20
10

View File

@@ -18,29 +18,34 @@ 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) ResourceSv1Ping(ign string, rpl *string) (err error) {
if dS.resS == nil {
return utils.NewErrNotConnected(utils.ResourceS)
func (dS *DispatcherService) ResourceSv1Ping(args *CGREvWithApiKey, rpl *string) (err error) {
if dS.attrS != nil {
if err = dS.authorize(utils.ResourceSv1Ping,
args.CGREvent.Tenant,
args.APIKey, args.CGREvent.Time); err != nil {
return
}
}
return dS.resS.Call(utils.ResourceSv1Ping, ign, rpl)
return dS.Dispatch(&args.CGREvent, utils.MetaResources,
utils.ResourceSv1Ping, args.CGREvent, rpl)
}
func (dS *DispatcherService) ResourceSv1GetResourcesForEvent(args *ArgsV1ResUsageWithApiKey,
reply *engine.Resources) (err error) {
if dS.resS == nil {
return utils.NewErrNotConnected(utils.ResourceS)
if dS.attrS != nil {
if err = dS.authorize(utils.ResourceSv1GetResourcesForEvent,
args.ArgRSv1ResourceUsage.CGREvent.Tenant,
args.APIKey, args.ArgRSv1ResourceUsage.CGREvent.Time); err != nil {
return
}
}
if err = dS.authorize(utils.ResourceSv1GetResourcesForEvent, args.ArgRSv1ResourceUsage.CGREvent.Tenant,
args.APIKey, args.ArgRSv1ResourceUsage.CGREvent.Time); err != nil {
return
}
return dS.resS.Call(utils.ResourceSv1GetResourcesForEvent, args.ArgRSv1ResourceUsage, reply)
return dS.Dispatch(&args.CGREvent, utils.MetaResources,
utils.ResourceSv1GetResourcesForEvent, args.ArgRSv1ResourceUsage, reply)
}
*/

View File

@@ -21,165 +21,93 @@ 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 (
dspResCfgPath string
dspResCfg *config.CGRConfig
dspResRPC *rpc.Client
instResCfgPath string
instResCfg *config.CGRConfig
instResRPC *rpc.Client
)
var sTestsDspRes = []func(t *testing.T){
testDspResInitCfg,
testDspResInitDataDb,
testDspResResetStorDb,
testDspResStartEngine,
testDspResRPCConn,
testDspResPingFailover,
testDspResPing,
testDspResLoadData,
testDspResAddAttributesWithPermision,
testDspResTestAuthKey,
testDspResAddAttributesWithPermision2,
testDspResTestAuthKey2,
testDspResKillEngine,
}
//Test start here
func TestDspResourceS(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 sTestsDspRes {
t.Run("", stest)
}
attrEngine.stopEngine(t)
dispEngine.stopEngine(t)
allEngine.stopEngine(t)
allEngine2.stopEngine(t)
}
func testDspResInitCfg(t *testing.T) {
var err error
dspResCfgPath = path.Join(dspDataDir, "conf", "samples", "dispatcher")
dspResCfg, err = config.NewCGRConfigFromFolder(dspResCfgPath)
if err != nil {
func testDspResPingFailover(t *testing.T) {
var reply string
if err := allEngine.RCP.Call(utils.ResourceSv1Ping, &utils.CGREvent{}, &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Received: %s", reply)
}
dspResCfg.DataFolderPath = dspDataDir // Share DataFolderPath through config towards StoreDb for Flush()
config.SetCgrConfig(dspResCfg)
instResCfgPath = path.Join(dspDataDir, "conf", "samples", "tutmysql")
instResCfg, err = config.NewCGRConfigFromFolder(instResCfgPath)
if err != nil {
ev := CGREvWithApiKey{
CGREvent: utils.CGREvent{
Tenant: "cgrates.org",
},
APIKey: "res12345",
}
if err := dispEngine.RCP.Call(utils.ResourceSv1Ping, &ev, &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Received: %s", reply)
}
instResCfg.DataFolderPath = dspDataDir // Share DataFolderPath through config towards StoreDb for Flush()
config.SetCgrConfig(instResCfg)
}
func testDspResInitDataDb(t *testing.T) {
if err := engine.InitDataDb(instResCfg); err != nil {
t.Fatal(err)
allEngine.stopEngine(t)
if err := dispEngine.RCP.Call(utils.ResourceSv1Ping, &ev, &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Received: %s", reply)
}
}
// Wipe out the cdr database
func testDspResResetStorDb(t *testing.T) {
if err := engine.InitStorDb(instResCfg); err != nil {
t.Fatal(err)
allEngine2.stopEngine(t)
if err := dispEngine.RCP.Call(utils.ResourceSv1Ping, &ev, &reply); err == nil {
t.Errorf("Expected error but recived %v and reply %v\n", err, reply)
}
}
// Start CGR Engine
func testDspResStartEngine(t *testing.T) {
if _, err := engine.StartEngine(instResCfgPath, dspDelay); err != nil {
t.Fatal(err)
}
if _, err := engine.StartEngine(dspResCfgPath, dspDelay); err != nil {
t.Fatal(err)
}
}
// Connect rpc client to rater
func testDspResRPCConn(t *testing.T) {
var err error
instResRPC, err = jsonrpc.Dial("tcp", instResCfg.ListenCfg().RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
if err != nil {
t.Fatal(err)
}
dspResRPC, err = jsonrpc.Dial("tcp", dspResCfg.ListenCfg().RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
if err != nil {
t.Fatal(err)
}
allEngine.startEngine(t)
allEngine2.startEngine(t)
}
func testDspResPing(t *testing.T) {
var reply string
if err := instResRPC.Call(utils.ResourceSv1Ping, "", &reply); err != nil {
if err := allEngine.RCP.Call(utils.ResourceSv1Ping, &utils.CGREvent{}, &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Received: %s", reply)
}
if err := dspResRPC.Call(utils.ResourceSv1Ping, "", &reply); err != nil {
if err := dispEngine.RCP.Call(utils.ResourceSv1Ping, &CGREvWithApiKey{
CGREvent: utils.CGREvent{
Tenant: "cgrates.org",
},
APIKey: "res12345",
}, &reply); err != nil {
t.Error(err)
} else if reply != utils.Pong {
t.Errorf("Received: %s", reply)
}
}
func testDspResLoadData(t *testing.T) {
var reply string
attrs := &utils.AttrLoadTpFromFolder{
FolderPath: path.Join(dspDataDir, "tariffplans", "tutorial")}
if err := instResRPC.Call("ApierV1.LoadTariffPlanFromFolder", attrs, &reply); err != nil {
t.Error(err)
}
time.Sleep(500 * time.Millisecond)
}
func testDspResAddAttributesWithPermision(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 := instResRPC.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 := instResRPC.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 testDspResTestAuthKey(t *testing.T) {
var rs *engine.Resources
args := &ArgsV1ResUsageWithApiKey{
@@ -196,53 +124,16 @@ func testDspResTestAuthKey(t *testing.T) {
},
}
if err := dspResRPC.Call(utils.ResourceSv1GetResourcesForEvent,
if err := dispEngine.RCP.Call(utils.ResourceSv1GetResourcesForEvent,
args, &rs); err == nil || err.Error() != utils.ErrUnauthorizedApi.Error() {
t.Error(err)
}
}
func testDspResAddAttributesWithPermision2(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&ResourceSv1.GetResourcesForEvent", true, utils.INFIELD_SEP),
Append: true,
},
},
Weight: 20,
}
var result string
if err := instResRPC.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 := instResRPC.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 testDspResTestAuthKey2(t *testing.T) {
var rs *engine.Resources
args := &ArgsV1ResUsageWithApiKey{
APIKey: "12345",
APIKey: "res12345",
ArgRSv1ResourceUsage: utils.ArgRSv1ResourceUsage{
CGREvent: utils.CGREvent{
Tenant: "cgrates.org",
@@ -262,19 +153,10 @@ func testDspResTestAuthKey2(t *testing.T) {
},
}
if err := dspResRPC.Call(utils.ResourceSv1GetResourcesForEvent,
if err := dispEngine.RCP.Call(utils.ResourceSv1GetResourcesForEvent,
args, &rs); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eRs, rs) {
t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(eRs), utils.ToJSON(rs))
}
}
func testDspResKillEngine(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

@@ -44,22 +44,22 @@ var (
)
var sTestsDspSession = []func(t *testing.T){
testDspSessionInitCfg,
testDspSessionInitDataDb,
testDspSessionResetStorDb,
testDspSessionStartEngine,
testDspSessionRPCConn,
testDspSessionPing,
testDspSessionLoadData,
testDspSessionAddAttributesWithPermision,
testDspSessionTestAuthKey,
testDspSessionAddAttributesWithPermision2,
testDspSessionAuthorize,
testDspSessionInit,
testDspSessionUpdate,
testDspSessionTerminate,
testDspSessionProcessCDR,
testDspSessionKillEngine,
// testDspSessionInitCfg,
// testDspSessionInitDataDb,
// testDspSessionResetStorDb,
// testDspSessionStartEngine,
// testDspSessionRPCConn,
// testDspSessionPing,
// testDspSessionLoadData,
// testDspSessionAddAttributesWithPermision,
// testDspSessionTestAuthKey,
// testDspSessionAddAttributesWithPermision2,
// testDspSessionAuthorize,
// testDspSessionInit,
// testDspSessionUpdate,
// testDspSessionTerminate,
// testDspSessionProcessCDR,
// testDspSessionKillEngine,
}
//Test start here

View File

@@ -21,26 +21,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package dispatchers
import (
"net/rpc"
"path"
"reflect"
"testing"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
var (
dspStsCfgPath string
dspStsCfg *config.CGRConfig
dspStsRPC *rpc.Client
instStsCfgPath string
instStsCfg *config.CGRConfig
instStsRPC *rpc.Client
)
var sTestsDspSts = []func(t *testing.T){
testDspStsPingFailover,
testDspStsGetStatFailover,