mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-16 13:49:53 +05:00
Addec all cores APIs in dispatchers + tests
This commit is contained in:
committed by
Dan Christian Bogos
parent
78a9b724ed
commit
5565d87d6b
@@ -23,6 +23,6 @@ cgrates.org,ATTR_API_RALS_AUTH,*auth,*string:~*req.ApiKey:rals12345,,,*req.APIMe
|
||||
cgrates.org,ATTR_API_REPLICATOR_AUTH,*auth,*string:~*req.ApiKey:repl12345,,,*req.APIMethods,*constant,ReplicatorSv1.Ping&ReplicatorSv1.GetAccount&ReplicatorSv1.SetAccount&ReplicatorSv1.RemoveAccount&ReplicatorSv1.GetRouteProfile&ReplicatorSv1.SetRouteProfile&ReplicatorSv1.RemoveRouteProfile&ReplicatorSv1.GetAttributeProfile&ReplicatorSv1.SetAttributeProfile&ReplicatorSv1.RemoveAttributeProfile&ReplicatorSv1.SetChargerProfile&ReplicatorSv1.GetChargerProfile&ReplicatorSv1.RemoveChargerProfile&ReplicatorSv1.GetDispatcherProfile&ReplicatorSv1.SetDispatcherProfile&ReplicatorSv1.RemoveDispatcherProfile&ReplicatorSv1.GetDispatcherHost&ReplicatorSv1.SetDispatcherHost&ReplicatorSv1.RemoveDispatcherHost&ReplicatorSv1.GetFilter&ReplicatorSv1.SetFilter&ReplicatorSv1.RemoveFilter&ReplicatorSv1.GetThreshold&ReplicatorSv1.SetThreshold&ReplicatorSv1.RemoveThreshold&ReplicatorSv1.GetStatQueue&ReplicatorSv1.SetStatQueue&ReplicatorSv1.RemoveStatQueue&ReplicatorSv1.GetResource&ReplicatorSv1.SetResource&ReplicatorSv1.RemoveResource&ReplicatorSv1.GetResourceProfile&ReplicatorSv1.SetResourceProfile&ReplicatorSv1.RemoveResourceProfile&ReplicatorSv1.GetStatQueueProfile&ReplicatorSv1.SetStatQueueProfile&ReplicatorSv1.RemoveStatQueueProfile&ReplicatorSv1.GetThresholdProfile&ReplicatorSv1.SetThresholdProfile&ReplicatorSv1.RemoveThresholdProfile&ReplicatorSv1.GetTiming&ReplicatorSv1.SetTiming&ReplicatorSv1.RemoveTiming&ReplicatorSv1.GetActionTriggers&ReplicatorSv1.SetActionTriggers&ReplicatorSv1.RemoveActionTriggers&ReplicatorSv1.SetSharedGroup&ReplicatorSv1.GetSharedGroup&ReplicatorSv1.RemoveSharedGroup&ReplicatorSv1.SetActions&ReplicatorSv1.GetActions&ReplicatorSv1.RemoveActions&ReplicatorSv1.SetActionPlan&ReplicatorSv1.GetActionPlan&ReplicatorSv1.RemoveActionPlan&ReplicatorSv1.SetAccountActionPlans&ReplicatorSv1.GetAccountActionPlans&ReplicatorSv1.RemAccountActionPlans&ReplicatorSv1.SetRatingPlan&ReplicatorSv1.GetRatingPlan&ReplicatorSv1.RemoveRatingPlan&ReplicatorSv1.SetRatingProfile&ReplicatorSv1.GetRatingProfile&ReplicatorSv1.RemoveRatingProfile&ReplicatorSv1.SetDestination&ReplicatorSv1.GetDestination&ReplicatorSv1.RemoveDestination&ReplicatorSv1.SetLoadIDs&ReplicatorSv1.GetItemLoadIDs,false,20
|
||||
cgrates.org,ATTR_API_CDRSV2,*auth,*string:~*req.ApiKey:cdrsv212345,,,*req.APIMethods,*constant,CDRsV2.ProcessEvent&CDRsV2.StoreSessionCost,false,20
|
||||
cgrates.org,ATTR_API_RATES_AUTH,*auth,*string:~*req.ApiKey:rPrf12345,,,*req.APIMethods,*constant,RateSv1.Ping&RateSv1.CostForEvent,false,20
|
||||
cgrates.org,ATTR_API_CORE_AUTH,*auth,*string:~*req.ApiKey:core12345,,,*req.APIMethods,*constant,CoreSv1.Status&CoreSv1.Ping&CoreSv1.Sleep,false,20
|
||||
cgrates.org,ATTR_API_CORE_AUTH,*auth,*string:~*req.ApiKey:core12345,,,*req.APIMethods,*constant,CoreSv1.Status&CoreSv1.Ping&CoreSv1.Sleep&CoreSv1.StartCPUProfiling&CoreSv1.StopCPUProfiling&CoreSv1.StartMemoryProfiling&CoreSv1.StopMemoryProfiling,false,20
|
||||
cgrates.org,ATTR_API_ACTIONS_AUTH,*auth,*string:~*req.ApiKey:actPrf12345,,,*req.APIMethods,*constant,ActionSv1.Ping,false,20
|
||||
cgrates.org,ATTR_API_ACCOUNTS_AUTH,*auth,*string:~*req.ApiKey:accPrf12345,,,*req.APIMethods,*constant,AccountSv1.Ping,false,20
|
||||
|
@@ -73,3 +73,75 @@ func (dS *DispatcherService) CoreSv1Sleep(args *utils.DurationArgs,
|
||||
APIOpts: args.APIOpts,
|
||||
}, utils.MetaCore, utils.CoreSv1Sleep, args, reply)
|
||||
}
|
||||
|
||||
func (dS *DispatcherService) CoreSv1StartCPUProfiling(args *utils.DirectoryArgs,
|
||||
reply *string) (err error) {
|
||||
tnt := dS.cfg.GeneralCfg().DefaultTenant
|
||||
if args.Tenant != utils.EmptyString {
|
||||
tnt = args.Tenant
|
||||
}
|
||||
if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 {
|
||||
if err = dS.authorize(utils.CoreSv1StartCPUProfiling, tnt,
|
||||
utils.IfaceAsString(args.APIOpts[utils.OptsAPIKey]), utils.TimePointer(time.Now())); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return dS.Dispatch(&utils.CGREvent{
|
||||
Tenant: tnt,
|
||||
APIOpts: args.APIOpts,
|
||||
}, utils.MetaCore, utils.CoreSv1StartCPUProfiling, args, reply)
|
||||
}
|
||||
|
||||
func (dS *DispatcherService) CoreSv1StopCPUProfiling(args *utils.DirectoryArgs,
|
||||
reply *string) (err error) {
|
||||
tnt := dS.cfg.GeneralCfg().DefaultTenant
|
||||
if args.Tenant != utils.EmptyString {
|
||||
tnt = args.Tenant
|
||||
}
|
||||
if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 {
|
||||
if err = dS.authorize(utils.CoreSv1StopCPUProfiling, tnt,
|
||||
utils.IfaceAsString(args.APIOpts[utils.OptsAPIKey]), utils.TimePointer(time.Now())); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return dS.Dispatch(&utils.CGREvent{
|
||||
Tenant: tnt,
|
||||
APIOpts: args.APIOpts,
|
||||
}, utils.MetaCore, utils.CoreSv1StopCPUProfiling, args, reply)
|
||||
}
|
||||
|
||||
func (dS *DispatcherService) CoreSv1StartMemoryProfiling(args *utils.MemoryPrf,
|
||||
reply *string) (err error) {
|
||||
tnt := dS.cfg.GeneralCfg().DefaultTenant
|
||||
if args.Tenant != utils.EmptyString {
|
||||
tnt = args.Tenant
|
||||
}
|
||||
if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 {
|
||||
if err = dS.authorize(utils.CoreSv1StartMemoryProfiling, tnt,
|
||||
utils.IfaceAsString(args.APIOpts[utils.OptsAPIKey]), utils.TimePointer(time.Now())); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return dS.Dispatch(&utils.CGREvent{
|
||||
Tenant: tnt,
|
||||
APIOpts: args.APIOpts,
|
||||
}, utils.MetaCore, utils.CoreSv1StartMemoryProfiling, args, reply)
|
||||
}
|
||||
|
||||
func (dS *DispatcherService) CoreSv1StopMemoryProfiling(args *utils.MemoryPrf,
|
||||
reply *string) (err error) {
|
||||
tnt := dS.cfg.GeneralCfg().DefaultTenant
|
||||
if args.Tenant != utils.EmptyString {
|
||||
tnt = args.Tenant
|
||||
}
|
||||
if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 {
|
||||
if err = dS.authorize(utils.CoreSv1StopMemoryProfiling, tnt,
|
||||
utils.IfaceAsString(args.APIOpts[utils.OptsAPIKey]), utils.TimePointer(time.Now())); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return dS.Dispatch(&utils.CGREvent{
|
||||
Tenant: tnt,
|
||||
APIOpts: args.APIOpts,
|
||||
}, utils.MetaCore, utils.CoreSv1StopMemoryProfiling, args, reply)
|
||||
}
|
||||
|
||||
@@ -21,7 +21,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package dispatchers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/rpc"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -30,6 +33,8 @@ import (
|
||||
|
||||
var sTestsDspCore = []func(t *testing.T){
|
||||
testDspCoreLoad,
|
||||
testDspCoreCPUProfile,
|
||||
testDspCoreMemoryProfile,
|
||||
}
|
||||
|
||||
//Test start here
|
||||
@@ -103,5 +108,142 @@ func testDspCoreLoad(t *testing.T) {
|
||||
} else if rply != utils.OK {
|
||||
t.Errorf("Expected: %q ,received: %q", utils.OK, rply)
|
||||
}
|
||||
}
|
||||
|
||||
func testDspCoreCPUProfile(t *testing.T) {
|
||||
var reply string
|
||||
args := &utils.DirectoryArgs{
|
||||
DirPath: "/tmp",
|
||||
}
|
||||
//apikey is missing
|
||||
expectedErr := "MANDATORY_IE_MISSING: [ApiKey]"
|
||||
if err := dispEngine.RPC.Call(utils.CoreSv1StartCPUProfiling,
|
||||
args, &reply); err == nil || err.Error() != expectedErr {
|
||||
t.Errorf("Expected %+v, received %+v", expectedErr, err)
|
||||
}
|
||||
|
||||
args = &utils.DirectoryArgs{
|
||||
DirPath: "/tmp",
|
||||
APIOpts: map[string]interface{}{
|
||||
utils.OptsAPIKey: "core12345",
|
||||
},
|
||||
}
|
||||
if err := dispEngine.RPC.Call(utils.CoreSv1StartCPUProfiling,
|
||||
args, &reply); err != nil {
|
||||
t.Error(err)
|
||||
} else if reply != utils.OK {
|
||||
t.Errorf("Unexpected reply returned")
|
||||
}
|
||||
|
||||
args = &utils.DirectoryArgs{
|
||||
APIOpts: map[string]interface{}{
|
||||
utils.OptsAPIKey: "core12345",
|
||||
},
|
||||
}
|
||||
if err := dispEngine.RPC.Call(utils.CoreSv1StopCPUProfiling,
|
||||
args, &reply); err != nil {
|
||||
t.Error(err)
|
||||
} else if reply != utils.OK {
|
||||
t.Errorf("Unexpected reply returned")
|
||||
}
|
||||
file, err := os.Open(path.Join("/tmp", utils.CpuPathCgr))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
//compare the size
|
||||
size, err := file.Stat()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if size.Size() < int64(415) {
|
||||
t.Errorf("Size of CPUProfile %v is lower that expected", size.Size())
|
||||
}
|
||||
//after we checked that CPUProfile was made successfully, can delete it
|
||||
if err := os.Remove(path.Join("/tmp", utils.CpuPathCgr)); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testDspCoreMemoryProfile(t *testing.T) {
|
||||
var reply string
|
||||
args := &utils.MemoryPrf{
|
||||
DirPath: "/tmp",
|
||||
Interval: 100 * time.Millisecond,
|
||||
NrFiles: 2,
|
||||
}
|
||||
//missing apiKey
|
||||
expectedErr := "MANDATORY_IE_MISSING: [ApiKey]"
|
||||
if err := dispEngine.RPC.Call(utils.CoreSv1StartMemoryProfiling,
|
||||
args, &reply); err == nil || err.Error() != expectedErr {
|
||||
t.Errorf("Expected %+v, received %+v", expectedErr, err)
|
||||
}
|
||||
args = &utils.MemoryPrf{
|
||||
DirPath: "/tmp",
|
||||
Interval: 100 * time.Millisecond,
|
||||
NrFiles: 2,
|
||||
APIOpts: map[string]interface{}{
|
||||
utils.OptsAPIKey: "core12345",
|
||||
},
|
||||
}
|
||||
if err := dispEngine.RPC.Call(utils.CoreSv1StartMemoryProfiling,
|
||||
args, &reply); err != nil {
|
||||
t.Error(err)
|
||||
} else if reply != utils.OK {
|
||||
t.Errorf("Unexpected reply returned")
|
||||
}
|
||||
|
||||
dur := &utils.DurationArgs{
|
||||
Duration: 500 * time.Millisecond,
|
||||
Tenant: "cgrates.org",
|
||||
APIOpts: map[string]interface{}{
|
||||
utils.OptsAPIKey: "core12345",
|
||||
utils.OptsRouteID: "core1",
|
||||
"EventType": "LoadDispatcher",
|
||||
},
|
||||
}
|
||||
|
||||
if err := dispEngine.RPC.Call(utils.CoreSv1Sleep,
|
||||
dur, &reply); err != nil {
|
||||
t.Error(err)
|
||||
} else if reply != utils.OK {
|
||||
t.Errorf("Unexpected reply returned")
|
||||
}
|
||||
|
||||
args = &utils.MemoryPrf{
|
||||
DirPath: "/tmp",
|
||||
Interval: 100 * time.Millisecond,
|
||||
NrFiles: 2,
|
||||
APIOpts: map[string]interface{}{
|
||||
utils.OptsAPIKey: "core12345",
|
||||
},
|
||||
}
|
||||
if err := dispEngine.RPC.Call(utils.CoreSv1StopMemoryProfiling,
|
||||
args, &reply); err != nil {
|
||||
t.Error(err)
|
||||
} else if reply != utils.OK {
|
||||
t.Errorf("Unexpected reply returned")
|
||||
}
|
||||
|
||||
//mem_prof1, mem_prof2
|
||||
for i := 1; i <= 2; i++ {
|
||||
file, err := os.Open(path.Join("/tmp", fmt.Sprintf("mem%v.prof", i)))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
//compare the size
|
||||
size, err := file.Stat()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
} else if size.Size() < int64(415) {
|
||||
t.Errorf("Size of MemoryProfile %v is lower that expected", size.Size())
|
||||
}
|
||||
//after we checked that CPUProfile was made successfully, can delete it
|
||||
if err := os.Remove(path.Join("/tmp", fmt.Sprintf("mem%v.prof", i))); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -742,9 +742,11 @@ type TenantWithAPIOpts struct {
|
||||
}
|
||||
|
||||
type MemoryPrf struct {
|
||||
Tenant string
|
||||
DirPath string
|
||||
Interval time.Duration
|
||||
NrFiles int
|
||||
APIOpts map[string]interface{}
|
||||
}
|
||||
|
||||
type TenantID struct {
|
||||
@@ -972,6 +974,12 @@ type DurationArgs struct {
|
||||
Tenant string
|
||||
}
|
||||
|
||||
type DirectoryArgs struct {
|
||||
DirPath string
|
||||
APIOpts map[string]interface{}
|
||||
Tenant string
|
||||
}
|
||||
|
||||
// AESEncrypt will encrypt the provided txt using the encKey and AES algorithm
|
||||
func AESEncrypt(txt, encKey string) (encrypted string, err error) {
|
||||
key, _ := hex.DecodeString(encKey)
|
||||
|
||||
Reference in New Issue
Block a user