Cover funcs from engine/resources.go

This commit is contained in:
ionutboangiu
2021-07-05 17:38:59 +03:00
committed by Dan Christian Bogos
parent d007937186
commit 8aa415aa7d

View File

@@ -5361,6 +5361,140 @@ func TestResourcesV1AllocateResourcesCacheReplySet(t *testing.T) {
config.SetCgrConfig(config.NewDefaultCGRConfig())
}
func TestResourcesV1AllocateResourcesResAllocErr(t *testing.T) {
tmp := Cache
defer func() {
Cache = tmp
}()
Cache.Clear(nil)
cfg := config.NewDefaultCGRConfig()
data := NewInternalDB(nil, nil, true)
dm := NewDataManager(data, cfg.CacheCfg(), nil)
Cache = NewCacheS(cfg, dm, nil)
rsPrf := &ResourceProfile{
Tenant: "cgrates.org",
ID: "",
FilterIDs: []string{"*string:~*req.Account:1001"},
ThresholdIDs: []string{utils.MetaNone},
AllocationMessage: "",
Weight: 10,
Limit: -1,
UsageTTL: time.Minute,
}
err := dm.SetResourceProfile(context.Background(), rsPrf, true)
if err != nil {
t.Error(err)
}
fltrs := NewFilterS(cfg, nil, dm)
rS := NewResourceService(dm, cfg, fltrs, nil)
args := utils.ArgRSv1ResourceUsage{
CGREvent: &utils.CGREvent{
ID: "EventAuthorizeResource",
Event: map[string]interface{}{
utils.AccountField: "1001",
},
},
UsageID: "RU_Test",
UsageTTL: utils.DurationPointer(time.Minute),
Units: 5,
}
var reply string
if err := rS.V1AllocateResources(context.Background(), args, &reply); err == nil ||
err != utils.ErrResourceUnavailable {
t.Errorf("expected: <%+v>, \nreceived: <%+v>", utils.ErrResourceUnavailable, err)
}
}
func TestResourcesV1AllocateResourcesProcessThErr(t *testing.T) {
tmp := Cache
defer func() {
Cache = tmp
}()
Cache.Clear(nil)
cfg := config.NewDefaultCGRConfig()
cfg.ResourceSCfg().StoreInterval = 2
cfg.ResourceSCfg().ThresholdSConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaThresholds)}
data := NewInternalDB(nil, nil, true)
dm := NewDataManager(data, cfg.CacheCfg(), nil)
Cache = NewCacheS(cfg, dm, nil)
rsPrf := &ResourceProfile{
Tenant: "cgrates.org",
ID: "RES1",
FilterIDs: []string{"*string:~*req.Account:1001"},
ThresholdIDs: []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaThresholds)},
AllocationMessage: "Approved",
Weight: 10,
Limit: -1,
UsageTTL: time.Minute,
}
rs := &Resource{
rPrf: rsPrf,
Tenant: "cgrates.org",
ID: "RES1",
Usages: map[string]*ResourceUsage{
"RU1": {
Tenant: "cgrates.org",
ID: "RU1",
Units: 10,
},
},
dirty: utils.BoolPointer(false),
tUsage: utils.Float64Pointer(10),
ttl: utils.DurationPointer(time.Minute),
TTLIdx: []string{},
}
err := dm.SetResourceProfile(context.Background(), rsPrf, true)
if err != nil {
t.Error(err)
}
err = dm.SetResource(context.Background(), rs)
if err != nil {
t.Error(err)
}
ccM := &ccMock{
calls: map[string]func(ctx *context.Context, args interface{}, reply interface{}) error{
utils.ThresholdSv1ProcessEvent: func(ctx *context.Context, args, reply interface{}) error {
return utils.ErrExists
},
},
}
rpcInternal := make(chan birpc.ClientConnector, 1)
rpcInternal <- ccM
cM := NewConnManager(cfg, map[string]chan birpc.ClientConnector{
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaThresholds): rpcInternal,
})
fltrs := NewFilterS(cfg, nil, dm)
rS := NewResourceService(dm, cfg, fltrs, cM)
args := utils.ArgRSv1ResourceUsage{
CGREvent: &utils.CGREvent{
ID: "EventAuthorizeResource",
Event: map[string]interface{}{
utils.AccountField: "1001",
},
},
UsageID: "RU_Test",
UsageTTL: utils.DurationPointer(time.Minute),
Units: 5,
}
var reply string
if err := rS.V1AllocateResources(context.Background(), args, &reply); err == nil ||
err != utils.ErrExists {
t.Errorf("expected: <%+v>, \nreceived: <%+v>", utils.ErrExists, err)
}
dm.DataDB().Flush(utils.EmptyString)
}
func TestResourcesV1ReleaseResourcesOK(t *testing.T) {
tmp := Cache
defer func() {
@@ -5794,3 +5928,97 @@ func TestResourcesV1ReleaseResourcesCacheReplySet(t *testing.T) {
config.SetCgrConfig(config.NewDefaultCGRConfig())
}
func TestResourcesV1ReleaseResourcesProcessThErr(t *testing.T) {
tmp := Cache
defer func() {
Cache = tmp
}()
Cache.Clear(nil)
cfg := config.NewDefaultCGRConfig()
cfg.ResourceSCfg().StoreInterval = 2
cfg.ResourceSCfg().ThresholdSConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaThresholds)}
data := NewInternalDB(nil, nil, true)
dm := NewDataManager(data, cfg.CacheCfg(), nil)
Cache = NewCacheS(cfg, dm, nil)
ccM := &ccMock{
calls: map[string]func(ctx *context.Context, args interface{}, reply interface{}) error{
utils.ThresholdSv1ProcessEvent: func(ctx *context.Context, args, reply interface{}) error {
return utils.ErrExists
},
},
}
rpcInternal := make(chan birpc.ClientConnector, 1)
rpcInternal <- ccM
cM := NewConnManager(cfg, map[string]chan birpc.ClientConnector{
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaThresholds): rpcInternal,
})
rsPrf := &ResourceProfile{
Tenant: "cgrates.org",
ID: "RES1",
FilterIDs: []string{"*string:~*req.Account:1001"},
ThresholdIDs: []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaThresholds)},
AllocationMessage: "Approved",
Weight: 10,
Limit: -1,
UsageTTL: time.Minute,
}
rs := &Resource{
rPrf: rsPrf,
Tenant: "cgrates.org",
ID: "RES1",
Usages: map[string]*ResourceUsage{
"RU_Test": {
Tenant: "cgrates.org",
ID: "RU_Test",
Units: 4,
},
},
dirty: utils.BoolPointer(false),
tUsage: utils.Float64Pointer(10),
ttl: utils.DurationPointer(time.Minute),
TTLIdx: []string{},
}
err := dm.SetResourceProfile(context.Background(), rsPrf, true)
if err != nil {
t.Error(err)
}
err = dm.SetResource(context.Background(), rs)
if err != nil {
t.Error(err)
}
fltrs := NewFilterS(cfg, nil, dm)
rS := NewResourceService(dm, cfg, fltrs, cM)
args := utils.ArgRSv1ResourceUsage{
CGREvent: &utils.CGREvent{
ID: "EventAuthorizeResource",
Event: map[string]interface{}{
utils.AccountField: "1001",
},
},
UsageID: "RU_Test",
UsageTTL: utils.DurationPointer(time.Minute),
Units: 5,
}
var reply string
var resources Resources
resources = append(resources, rs)
if _, err := resources.allocateResource(context.Background(), &ResourceUsage{
Tenant: "cgrates.org",
ID: "RU_ID",
Units: 1}, true); err != nil {
t.Error(err)
}
if err := rS.V1ReleaseResources(context.Background(), args, &reply); err == nil ||
err != utils.ErrExists {
t.Errorf("expected: <%+v>, \nreceived: <%+v>", utils.ErrExists, err)
}
dm.DataDB().Flush(utils.EmptyString)
}