From ea3e15140fbf2df1af6d9fac51209993720fb764 Mon Sep 17 00:00:00 2001 From: edwardro22 Date: Mon, 5 Feb 2018 19:07:06 +0200 Subject: [PATCH] Added tests for resources --- engine/resources_test.go | 290 ++++++++++++++++++++++++++++++++++++++- engine/stats_test.go | 20 ++- 2 files changed, 296 insertions(+), 14 deletions(-) diff --git a/engine/resources_test.go b/engine/resources_test.go index 7ff176192..9dfc43693 100644 --- a/engine/resources_test.go +++ b/engine/resources_test.go @@ -23,13 +23,149 @@ import ( "time" "github.com/cgrates/cgrates/cache" + "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" ) var ( - r1, r2 *Resource - ru1, ru2, ru3 *ResourceUsage - rs Resources + r1, r2 *Resource + ru1, ru2, ru3 *ResourceUsage + rs Resources + cloneExpTimeResource time.Time + expTimeResource = time.Now().Add(time.Duration(20 * time.Minute)) + resserv ResourceService + dmRES *DataManager + resprf = []*ResourceProfile{ + &ResourceProfile{ + Tenant: "cgrates.org", + ID: "resourcesprofile1", // identifier of this resource + FilterIDs: []string{"filter9"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + UsageTTL: time.Duration(10) * time.Second, // auto-expire the usage after this duration + Limit: 10.00, // limit value + AllocationMessage: "AllocationMessage", // message returned by the winning resource on allocation + Blocker: false, // blocker flag to stop processing on filters matched + Stored: false, + Weight: 20.00, // Weight to sort the resources + ThresholdIDs: []string{""}, // Thresholds to check after changing Limit + }, + &ResourceProfile{ + Tenant: "cgrates.org", + ID: "resourcesprofile2", // identifier of this resource + FilterIDs: []string{"filter10"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + UsageTTL: time.Duration(10) * time.Second, // auto-expire the usage after this duration + Limit: 10.00, // limit value + AllocationMessage: "AllocationMessage", // message returned by the winning resource on allocation + Blocker: false, // blocker flag to stop processing on filters matched + Stored: false, + Weight: 20.00, // Weight to sort the resources + ThresholdIDs: []string{""}, // Thresholds to check after changing Limit + }, + &ResourceProfile{ + Tenant: "cgrates.org", + ID: "resourcesprofile3", // identifier of this resource + FilterIDs: []string{"preffilter5"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + UsageTTL: time.Duration(10) * time.Second, // auto-expire the usage after this duration + Limit: 10.00, // limit value + AllocationMessage: "AllocationMessage", // message returned by the winning resource on allocation + Blocker: false, // blocker flag to stop processing on filters matched + Stored: false, + Weight: 20.00, // Weight to sort the resources + ThresholdIDs: []string{""}, // Thresholds to check after changing Limit + }, + &ResourceProfile{ + Tenant: "cgrates.org", + ID: "resourcesprofile4", // identifier of this resource + FilterIDs: []string{"defaultf5"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + UsageTTL: time.Duration(10) * time.Second, // auto-expire the usage after this duration + Limit: 10.00, // limit value + AllocationMessage: "AllocationMessage", // message returned by the winning resource on allocation + Blocker: false, // blocker flag to stop processing on filters matched + Stored: false, + Weight: 20.00, // Weight to sort the resources + ThresholdIDs: []string{""}, // Thresholds to check after changing Limit + }, + } + resourceTest = []*Resource{ + &Resource{ + Tenant: "cgrates.org", + ID: "resourcesprofile1", + Usages: map[string]*ResourceUsage{}, + TTLIdx: []string{}, // holds ordered list of ResourceIDs based on their TTL, empty if feature is disabled + rPrf: resprf[0], // for ordering purposes + }, + &Resource{ + Tenant: "cgrates.org", + ID: "resourcesprofile2", + Usages: map[string]*ResourceUsage{}, + TTLIdx: []string{}, // holds ordered list of ResourceIDs based on their TTL, empty if feature is disabled + rPrf: resprf[1], // for ordering purposes + }, + &Resource{ + Tenant: "cgrates.org", + ID: "resourcesprofile3", + Usages: map[string]*ResourceUsage{}, + TTLIdx: []string{}, // holds ordered list of ResourceIDs based on their TTL, empty if feature is disabled + rPrf: resprf[2], // for ordering purposes + }, + &Resource{ + Tenant: "cgrates.org", + ID: "resourcesprofile4", + Usages: map[string]*ResourceUsage{}, + TTLIdx: []string{}, // holds ordered list of ResourceIDs based on their TTL, empty if feature is disabled + rPrf: resprf[3], // for ordering purposes + }, + } + resEvs = []*utils.CGREvent{ + &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "event1", + Event: map[string]interface{}{ + "Resources": "ResourcesProfile1", + utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC), + "UsageInterval": "1s", + "PddInterval": "1s", + "Weight": "20.0", + utils.Usage: time.Duration(135 * time.Second), + utils.COST: 123.0, + }}, + &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "event2", + Event: map[string]interface{}{ + "Resources": "ResourcesProfile2", + utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC), + "UsageInterval": "1s", + "PddInterval": "1s", + "Weight": "21.0", + utils.Usage: time.Duration(45 * time.Second), + }}, + &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "event3", + Event: map[string]interface{}{ + "Resources": "ResourcesProfilePrefix", + utils.Usage: time.Duration(30 * time.Second), + }}, + &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "event3", + Event: map[string]interface{}{ + "Weight": "200.0", + utils.Usage: time.Duration(65 * time.Second), + }}, + } ) func TestRSRecordUsage1(t *testing.T) { @@ -293,3 +429,151 @@ func TestV1AuthorizeResourceMissingStruct(t *testing.T) { t.Error(err.Error()) } } + +func TestRSPopulateResourceService(t *testing.T) { + data, _ := NewMapStorage() + dmRES = NewDataManager(data) + var filters1 []*FilterRule + var filters2 []*FilterRule + var preffilter []*FilterRule + var defaultf []*FilterRule + second := 1 * time.Second + resserv = ResourceService{ + dm: dmRES, + filterS: &FilterS{dm: dmRES}, + } + ref := NewFilterIndexer(dmRES, utils.ResourceProfilesPrefix, "cgrates.org") + //filter1 + x, err := NewFilterRule(MetaString, "Resources", []string{"ResourcesProfile1"}) + if err != nil { + t.Errorf("Error: %+v", err) + } + filters1 = append(filters1, x) + x, err = NewFilterRule(MetaGreaterOrEqual, "UsageInterval", []string{second.String()}) + if err != nil { + t.Errorf("Error: %+v", err) + } + filters1 = append(filters1, x) + x, err = NewFilterRule(MetaGreaterOrEqual, utils.Usage, []string{second.String()}) + if err != nil { + t.Errorf("Error: %+v", err) + } + filters1 = append(filters1, x) + x, err = NewFilterRule(MetaGreaterOrEqual, "Weight", []string{"9.0"}) + if err != nil { + t.Errorf("Error: %+v", err) + } + filters1 = append(filters1, x) + filter9 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "filter9", Rules: filters1} + dmRES.SetFilter(filter9) + ref.IndexTPFilter(FilterToTPFilter(filter9), "resourcesprofile1") + //filter2 + x, err = NewFilterRule(MetaString, "Resources", []string{"ResourcesProfile2"}) + if err != nil { + t.Errorf("Error: %+v", err) + } + filters2 = append(filters2, x) + x, err = NewFilterRule(MetaGreaterOrEqual, "PddInterval", []string{second.String()}) + if err != nil { + t.Errorf("Error: %+v", err) + } + filters2 = append(filters2, x) + x, err = NewFilterRule(MetaGreaterOrEqual, utils.Usage, []string{second.String()}) + if err != nil { + t.Errorf("Error: %+v", err) + } + filters2 = append(filters2, x) + x, err = NewFilterRule(MetaGreaterOrEqual, "Weight", []string{"15.0"}) + if err != nil { + t.Errorf("Error: %+v", err) + } + filters2 = append(filters2, x) + filter10 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "filter10", Rules: filters2} + dmRES.SetFilter(filter10) + ref.IndexTPFilter(FilterToTPFilter(filter10), "resourcesprofile2") + //prefix filter + x, err = NewFilterRule(MetaPrefix, "Resources", []string{"ResourcesProfilePrefix"}) + if err != nil { + t.Errorf("Error: %+v", err) + } + preffilter = append(preffilter, x) + x, err = NewFilterRule(MetaGreaterOrEqual, utils.Usage, []string{second.String()}) + if err != nil { + t.Errorf("Error: %+v", err) + } + preffilter = append(preffilter, x) + preffilter5 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "preffilter5", Rules: preffilter} + dmRES.SetFilter(preffilter5) + ref.IndexTPFilter(FilterToTPFilter(preffilter5), "resourcesprofile3") + //default filter + x, err = NewFilterRule(MetaGreaterOrEqual, "Weight", []string{"200.00"}) + if err != nil { + t.Errorf("Error: %+v", err) + } + defaultf = append(defaultf, x) + x, err = NewFilterRule(MetaGreaterOrEqual, utils.Usage, []string{second.String()}) + if err != nil { + t.Errorf("Error: %+v", err) + } + defaultf = append(defaultf, x) + defaultf5 := &Filter{Tenant: config.CgrConfig().DefaultTenant, ID: "defaultf5", Rules: defaultf} + dmRES.SetFilter(defaultf5) + ref.IndexTPFilter(FilterToTPFilter(defaultf5), "resourcesprofile4") + for _, res := range resourceTest { + dmRES.SetResource(res) + } + for _, resp := range resprf { + dmRES.SetResourceProfile(resp, false) + } + err = ref.StoreIndexes() + if err != nil { + t.Errorf("Error: %+v", err) + } +} + +func TestRSmatchingResourcesForEvent(t *testing.T) { + mres, err := resserv.matchingResourcesForEvent(resEvs[0]) + if err != nil { + t.Errorf("Error: %+v", err) + } + if !reflect.DeepEqual(resourceTest[0].Tenant, mres[0].Tenant) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].Tenant, mres[0].Tenant) + } else if !reflect.DeepEqual(resourceTest[0].ID, mres[0].ID) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].ID, mres[0].ID) + } else if !reflect.DeepEqual(resourceTest[0].rPrf, mres[0].rPrf) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[0].rPrf, mres[0].rPrf) + } + mres, err = resserv.matchingResourcesForEvent(resEvs[1]) + if err != nil { + t.Errorf("Error: %+v", err) + } + if !reflect.DeepEqual(resourceTest[1].Tenant, mres[0].Tenant) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[1].Tenant, mres[0].Tenant) + } else if !reflect.DeepEqual(resourceTest[1].ID, mres[0].ID) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[1].ID, mres[0].ID) + } else if !reflect.DeepEqual(resourceTest[1].rPrf, mres[0].rPrf) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[1].rPrf, mres[0].rPrf) + } + mres, err = resserv.matchingResourcesForEvent(resEvs[2]) + if err != nil { + t.Errorf("Error: %+v", err) + } + if !reflect.DeepEqual(resourceTest[2].Tenant, mres[0].Tenant) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[2].Tenant, mres[0].Tenant) + } else if !reflect.DeepEqual(resourceTest[2].ID, mres[0].ID) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[2].ID, mres[0].ID) + } else if !reflect.DeepEqual(resourceTest[2].rPrf, mres[0].rPrf) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[2].rPrf, mres[0].rPrf) + } + mres, err = resserv.matchingResourcesForEvent(resEvs[3]) + if err != nil { + t.Errorf("Error: %+v", err) + } + if !reflect.DeepEqual(resourceTest[3].Tenant, mres[0].Tenant) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[3].Tenant, mres[0].Tenant) + } else if !reflect.DeepEqual(resourceTest[3].ID, mres[0].ID) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[3].ID, mres[0].ID) + } else if !reflect.DeepEqual(resourceTest[3].rPrf, mres[0].rPrf) { + t.Errorf("Expecting: %+v, received: %+v", resourceTest[3].rPrf, mres[0].rPrf) + } +} diff --git a/engine/stats_test.go b/engine/stats_test.go index 7d542cd01..b418880e4 100644 --- a/engine/stats_test.go +++ b/engine/stats_test.go @@ -1,5 +1,3 @@ -// +build integr - /* Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments Copyright (C) ITsysCOM GmbH @@ -125,7 +123,7 @@ var ( &StatQueue{Tenant: "cgrates.org", ID: "statsprofile3", sqPrfl: sqps[2]}, &StatQueue{Tenant: "cgrates.org", ID: "statsprofile4", sqPrfl: sqps[3]}, } - evs = []*utils.CGREvent{ + statsEvs = []*utils.CGREvent{ &utils.CGREvent{ Tenant: "cgrates.org", ID: "event1", @@ -268,7 +266,7 @@ func TestStatsPopulateStatsService(t *testing.T) { } func TestStatsmatchingStatQueuesForEvent(t *testing.T) { - msq, err := stsserv.matchingStatQueuesForEvent(evs[0]) + msq, err := stsserv.matchingStatQueuesForEvent(statsEvs[0]) if err != nil { t.Errorf("Error: %+v", err) } @@ -279,7 +277,7 @@ func TestStatsmatchingStatQueuesForEvent(t *testing.T) { } else if !reflect.DeepEqual(stqs[0].sqPrfl, msq[0].sqPrfl) { t.Errorf("Expecting: %+v, received: %+v", stqs[0].sqPrfl, msq[0].sqPrfl) } - msq, err = stsserv.matchingStatQueuesForEvent(evs[1]) + msq, err = stsserv.matchingStatQueuesForEvent(statsEvs[1]) if err != nil { t.Errorf("Error: %+v", err) } @@ -290,7 +288,7 @@ func TestStatsmatchingStatQueuesForEvent(t *testing.T) { } else if !reflect.DeepEqual(stqs[1].sqPrfl, msq[0].sqPrfl) { t.Errorf("Expecting: %+v, received: %+v", stqs[1].sqPrfl, msq[0].sqPrfl) } - msq, err = stsserv.matchingStatQueuesForEvent(evs[2]) + msq, err = stsserv.matchingStatQueuesForEvent(statsEvs[2]) if err != nil { t.Errorf("Error: %+v", err) } @@ -301,7 +299,7 @@ func TestStatsmatchingStatQueuesForEvent(t *testing.T) { } else if !reflect.DeepEqual(stqs[2].sqPrfl, msq[0].sqPrfl) { t.Errorf("Expecting: %+v, received: %+v", stqs[2].sqPrfl, msq[0].sqPrfl) } - msq, err = stsserv.matchingStatQueuesForEvent(evs[3]) + msq, err = stsserv.matchingStatQueuesForEvent(statsEvs[3]) if err != nil { t.Errorf("Error: %+v", err) } @@ -317,7 +315,7 @@ func TestStatsmatchingStatQueuesForEvent(t *testing.T) { func TestStatSprocessEvent(t *testing.T) { stq := map[string]string{} reply := "" - err := stsserv.V1ProcessEvent(evs[0], &reply) + err := stsserv.V1ProcessEvent(statsEvs[0], &reply) if err != nil { t.Errorf("Error: %+v", err) } else if reply != utils.OK { @@ -327,7 +325,7 @@ func TestStatSprocessEvent(t *testing.T) { if err != nil { t.Errorf("Error: %+v", err) } - err = stsserv.V1ProcessEvent(evs[1], &reply) + err = stsserv.V1ProcessEvent(statsEvs[1], &reply) if err != nil { t.Errorf("Error: %+v", err) } else if reply != utils.OK { @@ -337,7 +335,7 @@ func TestStatSprocessEvent(t *testing.T) { if err != nil { t.Errorf("Error: %+v", err) } - err = stsserv.V1ProcessEvent(evs[2], &reply) + err = stsserv.V1ProcessEvent(statsEvs[2], &reply) if err != nil { t.Errorf("Error: %+v", err) } else if reply != utils.OK { @@ -347,7 +345,7 @@ func TestStatSprocessEvent(t *testing.T) { if err != nil { t.Errorf("Error: %+v", err) } - err = stsserv.V1ProcessEvent(evs[3], &reply) + err = stsserv.V1ProcessEvent(statsEvs[3], &reply) if err != nil { t.Errorf("Error: %+v", err) } else if reply != utils.OK {