From 863e9b719d33a33d081110bc8159e8d46986f603 Mon Sep 17 00:00:00 2001 From: TeoV Date: Fri, 2 Oct 2020 15:18:55 +0300 Subject: [PATCH] Add integration test for Unlimited resource ( for monitoring ) --- apier/v1/resourcesv1_it_test.go | 67 +++++++++++++++++++++++++++++++++ engine/resources.go | 2 +- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/apier/v1/resourcesv1_it_test.go b/apier/v1/resourcesv1_it_test.go index 6e7a9ee28..6a309fecd 100644 --- a/apier/v1/resourcesv1_it_test.go +++ b/apier/v1/resourcesv1_it_test.go @@ -62,6 +62,7 @@ var ( testV1RsGetResourceProfileAfterDelete, testV1RsResourcePing, testV1RsMatchNotFound, + testV1RsAllocateUnlimited, testV1RsStopEngine, } ) @@ -839,6 +840,72 @@ func testV1RsMatchNotFound(t *testing.T) { } } +func testV1RsAllocateUnlimited(t *testing.T) { + rlsConfig = &ResourceWithCache{ + ResourceProfile: &engine.ResourceProfile{ + Tenant: "cgrates.org", + ID: "RES_ULTIMITED", + FilterIDs: []string{"*string:~*req.CustomField:UnlimitedEvent"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + UsageTTL: time.Duration(-1), + Limit: -1, + AllocationMessage: "CustomUnlimitedMessage", + Stored: true, + Weight: 20, + ThresholdIDs: []string{utils.META_NONE}, + }, + } + + var result string + if err := rlsV1Rpc.Call(utils.APIerSv1SetResourceProfile, rlsConfig, &result); err != nil { + t.Error(err) + } else if result != utils.OK { + t.Error("Unexpected reply returned", result) + } + var reply string + argsRU := utils.ArgRSv1ResourceUsage{ + UsageID: "651a8db2-4f67-4cf8-b622-169e8a482e51", + CGREventWithOpts: &utils.CGREventWithOpts{ + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: utils.UUIDSha1Prefix(), + Event: map[string]interface{}{ + "CustomField": "UnlimitedEvent"}, + }, + }, + Units: 1, + } + if err := rlsV1Rpc.Call(utils.ResourceSv1AllocateResources, + argsRU, &reply); err != nil { + t.Error(err) + } else if reply != "CustomUnlimitedMessage" { + t.Errorf("Expecting: %+v, received: %+v", "CustomUnlimitedMessage", reply) + } + + var rplyRes *engine.Resource + expRes := &engine.Resource{ + Tenant: "cgrates.org", + ID: "RES_ULTIMITED", + Usages: map[string]*engine.ResourceUsage{ + "651a8db2-4f67-4cf8-b622-169e8a482e51": &engine.ResourceUsage{ + Tenant: "cgrates.org", + ID: "651a8db2-4f67-4cf8-b622-169e8a482e51", + Units: 1, + }, + }, + } + if err := rlsV1Rpc.Call(utils.ResourceSv1GetResource, &utils.TenantIDWithOpts{ + TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "RES_ULTIMITED"}, + }, &rplyRes); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expRes, rplyRes) { + t.Errorf("Expecting: %+v, received: %+v", expRes, rplyRes) + } + +} + func testV1RsStopEngine(t *testing.T) { if err := engine.KillEngine(*waitRater); err != nil { t.Error(err) diff --git a/engine/resources.go b/engine/resources.go index de6d23eb9..5a8eeb77e 100644 --- a/engine/resources.go +++ b/engine/resources.go @@ -289,7 +289,7 @@ func (rs Resources) allocateResource(ru *ResourceUsage, dryRun bool) (alcMessage err = fmt.Errorf("empty configuration for resourceID: %s", r.TenantID()) return } - if r.rPrf.Limit >= r.totalUsage()+ru.Units { + if r.rPrf.Limit >= r.totalUsage()+ru.Units || r.rPrf.Limit == -1 { if alcMessage == "" { if r.rPrf.AllocationMessage != "" { alcMessage = r.rPrf.AllocationMessage