From e4427f8ce7e2d6d9fb07aee5fb2e9a5c16262d53 Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Fri, 20 Aug 2021 14:19:47 +0300 Subject: [PATCH] Test functions from engine/stats.go final --- engine/stats_test.go | 478 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 413 insertions(+), 65 deletions(-) diff --git a/engine/stats_test.go b/engine/stats_test.go index b9053db2f..e4f7e4941 100644 --- a/engine/stats_test.go +++ b/engine/stats_test.go @@ -3185,7 +3185,7 @@ func TestStatQueueProcessThresholdsOK(t *testing.T) { } } -func TestStatQueueProcessThresholds2(t *testing.T) { +func TestStatQueueProcessThresholdsErrPartExec(t *testing.T) { tmp := Cache tmpC := config.CgrConfig() tmpCM := connMgr @@ -3479,71 +3479,419 @@ func TestStatQueueV1GetQueueFloatMetricsMissingArgs(t *testing.T) { } } -// func TestStatQueueV1GetQueueFloatMetricsErrGetStats(t *testing.T) { -// tmp := Cache -// tmpC := config.CgrConfig() -// defer func() { -// Cache = tmp -// config.SetCgrConfig(tmpC) -// }() +func TestStatQueueV1GetQueueFloatMetricsErrGetStats(t *testing.T) { + tmp := Cache + tmpC := config.CgrConfig() + defer func() { + Cache = tmp + config.SetCgrConfig(tmpC) + }() -// cfg := config.NewDefaultCGRConfig() -// cfg.StatSCfg().StoreInterval = 1 -// data := DataDBMock{} -// dm := NewDataManager(data, cfg.CacheCfg(), nil) -// Cache = NewCacheS(cfg, dm, nil) -// filterS := NewFilterS(cfg, nil, dm) -// sS := NewStatService(dm, cfg, filterS, nil) + cfg := config.NewDefaultCGRConfig() + cfg.StatSCfg().StoreInterval = 1 + Cache = NewCacheS(cfg, nil, nil) + filterS := NewFilterS(cfg, nil, nil) + sS := NewStatService(nil, cfg, filterS, nil) -// sqPrf := &StatQueueProfile{ -// Tenant: "cgrates.org", -// ID: "SQ1", -// FilterIDs: []string{"*string:~*req.Account:1001"}, -// ActivationInterval: &utils.ActivationInterval{ -// ExpiryTime: time.Date(2021, 6, 1, 12, 0, 0, 0, time.UTC), -// }, -// Weight: 10, -// Blocker: true, -// QueueLength: 10, -// ThresholdIDs: []string{"*none"}, -// MinItems: 5, -// Metrics: []*MetricWithFilters{ -// { -// MetricID: utils.MetaTCD, -// }, -// }, -// } -// sq := &StatQueue{ -// sqPrfl: sqPrf, -// dirty: utils.BoolPointer(false), -// Tenant: "cgrates.org", -// ID: "SQ1", -// SQItems: []SQItem{ -// { -// EventID: "SqProcessEvent", -// }, -// }, -// SQMetrics: map[string]StatMetric{ -// utils.MetaTCD: &StatTCD{ -// Sum: time.Minute, -// val: utils.DurationPointer(time.Hour), -// }, -// }, -// } + experr := `SERVER_ERROR: NO_DATABASE_CONNECTION` + reply := map[string]float64{} + if err := sS.V1GetQueueFloatMetrics(&utils.TenantID{ + ID: "SQ1", + }, &reply); err == nil || err.Error() != experr { + t.Errorf("expected: <%+v>, \nreceived: <%+v>", experr, err) + } +} -// if err := dm.SetStatQueue(sq); err != nil { -// t.Error(err) -// } +func TestStatQueueV1GetQueueStringMetricsOK(t *testing.T) { + tmp := Cache + tmpC := config.CgrConfig() + defer func() { + Cache = tmp + config.SetCgrConfig(tmpC) + }() -// expected := map[string]float64{ -// utils.MetaTCD: 3600000000000, -// } -// reply := map[string]float64{} -// if err := sS.V1GetQueueFloatMetrics(&utils.TenantID{ -// ID: "SQ1", -// }, &reply); err != nil { -// t.Error(err) -// } else if !reflect.DeepEqual(reply, expected) { -// t.Errorf("expected: <%+v>, \nreceived: <%+v>", expected, reply) -// } -// } + cfg := config.NewDefaultCGRConfig() + cfg.StatSCfg().StoreInterval = 1 + data := NewInternalDB(nil, nil, true) + dm := NewDataManager(data, cfg.CacheCfg(), nil) + Cache = NewCacheS(cfg, dm, nil) + filterS := NewFilterS(cfg, nil, dm) + sS := NewStatService(dm, cfg, filterS, nil) + + sqPrf := &StatQueueProfile{ + Tenant: "cgrates.org", + ID: "SQ1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + ActivationInterval: &utils.ActivationInterval{ + ExpiryTime: time.Date(2021, 6, 1, 12, 0, 0, 0, time.UTC), + }, + Weight: 10, + Blocker: true, + QueueLength: 10, + ThresholdIDs: []string{"*none"}, + MinItems: 5, + Metrics: []*MetricWithFilters{ + { + MetricID: utils.MetaTCD, + }, + }, + } + sq := &StatQueue{ + sqPrfl: sqPrf, + dirty: utils.BoolPointer(false), + Tenant: "cgrates.org", + ID: "SQ1", + SQItems: []SQItem{ + { + EventID: "SqProcessEvent", + }, + }, + SQMetrics: map[string]StatMetric{ + utils.MetaTCD: &StatTCD{ + Sum: time.Minute, + val: utils.DurationPointer(time.Hour), + }, + }, + } + + if err := dm.SetStatQueue(sq); err != nil { + t.Error(err) + } + + expected := map[string]string{ + utils.MetaTCD: "1h0m0s", + } + reply := map[string]string{} + if err := sS.V1GetQueueStringMetrics(&utils.TenantID{ + ID: "SQ1", + }, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(reply, expected) { + t.Errorf("expected: <%+v>, \nreceived: <%+v>", expected, reply) + } +} + +func TestStatQueueV1GetQueueStringMetricsErrNotFound(t *testing.T) { + tmp := Cache + tmpC := config.CgrConfig() + defer func() { + Cache = tmp + config.SetCgrConfig(tmpC) + }() + + cfg := config.NewDefaultCGRConfig() + cfg.StatSCfg().StoreInterval = 1 + data := NewInternalDB(nil, nil, true) + dm := NewDataManager(data, cfg.CacheCfg(), nil) + Cache = NewCacheS(cfg, dm, nil) + filterS := NewFilterS(cfg, nil, dm) + sS := NewStatService(dm, cfg, filterS, nil) + + sqPrf := &StatQueueProfile{ + Tenant: "cgrates.org", + ID: "SQ1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + ActivationInterval: &utils.ActivationInterval{ + ExpiryTime: time.Date(2021, 6, 1, 12, 0, 0, 0, time.UTC), + }, + Weight: 10, + Blocker: true, + QueueLength: 10, + ThresholdIDs: []string{"*none"}, + MinItems: 5, + Metrics: []*MetricWithFilters{ + { + MetricID: utils.MetaTCD, + }, + }, + } + sq := &StatQueue{ + sqPrfl: sqPrf, + dirty: utils.BoolPointer(false), + Tenant: "cgrates.org", + ID: "SQ1", + SQItems: []SQItem{ + { + EventID: "SqProcessEvent", + }, + }, + SQMetrics: map[string]StatMetric{ + utils.MetaTCD: &StatTCD{ + Sum: time.Minute, + val: utils.DurationPointer(time.Hour), + }, + }, + } + + if err := dm.SetStatQueue(sq); err != nil { + t.Error(err) + } + + reply := map[string]string{} + if err := sS.V1GetQueueStringMetrics(&utils.TenantID{ + ID: "SQ2", + }, &reply); err == nil || err != utils.ErrNotFound { + t.Errorf("expected: <%+v>, \nreceived: <%+v>", utils.ErrNotFound, err) + } +} + +func TestStatQueueV1GetQueueStringMetricsMissingArgs(t *testing.T) { + tmp := Cache + tmpC := config.CgrConfig() + defer func() { + Cache = tmp + config.SetCgrConfig(tmpC) + }() + + cfg := config.NewDefaultCGRConfig() + cfg.StatSCfg().StoreInterval = 1 + data := NewInternalDB(nil, nil, true) + dm := NewDataManager(data, cfg.CacheCfg(), nil) + Cache = NewCacheS(cfg, dm, nil) + filterS := NewFilterS(cfg, nil, dm) + sS := NewStatService(dm, cfg, filterS, nil) + + sqPrf := &StatQueueProfile{ + Tenant: "cgrates.org", + ID: "SQ1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + ActivationInterval: &utils.ActivationInterval{ + ExpiryTime: time.Date(2021, 6, 1, 12, 0, 0, 0, time.UTC), + }, + Weight: 10, + Blocker: true, + QueueLength: 10, + ThresholdIDs: []string{"*none"}, + MinItems: 5, + Metrics: []*MetricWithFilters{ + { + MetricID: utils.MetaTCD, + }, + }, + } + sq := &StatQueue{ + sqPrfl: sqPrf, + dirty: utils.BoolPointer(false), + Tenant: "cgrates.org", + ID: "SQ1", + SQItems: []SQItem{ + { + EventID: "SqProcessEvent", + }, + }, + SQMetrics: map[string]StatMetric{ + utils.MetaTCD: &StatTCD{ + Sum: time.Minute, + val: utils.DurationPointer(time.Hour), + }, + }, + } + + if err := dm.SetStatQueue(sq); err != nil { + t.Error(err) + } + + experr := `MANDATORY_IE_MISSING: [ID]` + reply := map[string]string{} + if err := sS.V1GetQueueStringMetrics(&utils.TenantID{}, &reply); err == nil || + err.Error() != experr { + t.Errorf("expected: <%+v>, \nreceived: <%+v>", experr, err) + } +} + +func TestStatQueueV1GetQueueStringMetricsErrGetStats(t *testing.T) { + tmp := Cache + tmpC := config.CgrConfig() + defer func() { + Cache = tmp + config.SetCgrConfig(tmpC) + }() + + cfg := config.NewDefaultCGRConfig() + cfg.StatSCfg().StoreInterval = 1 + Cache = NewCacheS(cfg, nil, nil) + filterS := NewFilterS(cfg, nil, nil) + sS := NewStatService(nil, cfg, filterS, nil) + + experr := `SERVER_ERROR: NO_DATABASE_CONNECTION` + reply := map[string]string{} + if err := sS.V1GetQueueStringMetrics(&utils.TenantID{ + ID: "SQ1", + }, &reply); err == nil || err.Error() != experr { + t.Errorf("expected: <%+v>, \nreceived: <%+v>", experr, err) + } +} + +func TestStatQueueStoreStatQueueStoreIntervalDisabled(t *testing.T) { + tmp := Cache + tmpC := config.CgrConfig() + tmpCM := connMgr + defer func() { + Cache = tmp + config.SetCgrConfig(tmpC) + connMgr = tmpCM + }() + + cfg := config.NewDefaultCGRConfig() + cfg.StatSCfg().StoreInterval = -1 + config.SetCgrConfig(cfg) + data := NewInternalDB(nil, nil, true) + dm := NewDataManager(data, cfg.CacheCfg(), nil) + connMgr = NewConnManager(cfg, make(map[string]chan rpcclient.ClientConnector)) + Cache = NewCacheS(cfg, dm, nil) + filterS := NewFilterS(cfg, nil, dm) + sS := NewStatService(dm, cfg, filterS, connMgr) + + sq := &StatQueue{ + Tenant: "cgrates.org", + ID: "SQ1", + dirty: utils.BoolPointer(true), + } + + sS.storeStatQueue(sq) + + if *sq.dirty != false { + t.Error("expected dirty to be false") + } +} + +func TestStatQueueGetStatQueueOK(t *testing.T) { + tmp := Cache + tmpC := config.CgrConfig() + defer func() { + Cache = tmp + config.SetCgrConfig(tmpC) + }() + + cfg := config.NewDefaultCGRConfig() + cfg.StatSCfg().StoreInterval = 1 + data := NewInternalDB(nil, nil, true) + dm := NewDataManager(data, cfg.CacheCfg(), nil) + Cache = NewCacheS(cfg, dm, nil) + filterS := NewFilterS(cfg, nil, dm) + sS := NewStatService(dm, cfg, filterS, nil) + + sqPrf := &StatQueueProfile{ + Tenant: "cgrates.org", + ID: "SQ1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + ActivationInterval: &utils.ActivationInterval{ + ExpiryTime: time.Date(2021, 6, 1, 12, 0, 0, 0, time.UTC), + }, + Weight: 10, + Blocker: true, + QueueLength: 10, + ThresholdIDs: []string{"*none"}, + MinItems: 5, + Metrics: []*MetricWithFilters{ + { + MetricID: utils.MetaTCD, + }, + }, + } + sq := &StatQueue{ + sqPrfl: sqPrf, + dirty: utils.BoolPointer(false), + Tenant: "cgrates.org", + ID: "SQ1", + SQItems: []SQItem{ + { + EventID: "SqProcessEvent", + ExpiryTime: utils.TimePointer(time.Now()), + }, + }, + SQMetrics: map[string]StatMetric{ + utils.MetaTCD: &StatTCD{ + Sum: time.Minute, + val: utils.DurationPointer(time.Hour), + }, + }, + } + + if err := dm.SetStatQueue(sq); err != nil { + t.Error(err) + } + + expected := utils.StringSet{ + utils.ConcatenatedKey(sq.Tenant, sq.ID): struct{}{}, + } + if rcv, err := sS.getStatQueue("cgrates.org", "SQ1"); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(rcv, sq) { + t.Errorf("expected: <%+v>, \nreceived: <%+v>", + utils.ToJSON(sq), utils.ToJSON(rcv)) + } else if !reflect.DeepEqual(sS.storedStatQueues, expected) { + t.Errorf("expected: <%+v>, \nreceived: <%+v>", expected, sS.storedStatQueues) + } +} + +func TestStatQueueCall(t *testing.T) { + tmp := Cache + tmpC := config.CgrConfig() + defer func() { + Cache = tmp + config.SetCgrConfig(tmpC) + }() + + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, true) + dm := NewDataManager(data, cfg.CacheCfg(), nil) + Cache = NewCacheS(cfg, dm, nil) + filterS := NewFilterS(cfg, nil, dm) + sS := NewStatService(dm, cfg, filterS, nil) + + sqPrf := &StatQueueProfile{ + Tenant: "cgrates.org", + ID: "SQ1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + ActivationInterval: &utils.ActivationInterval{ + ExpiryTime: time.Date(2021, 6, 1, 12, 0, 0, 0, time.UTC), + }, + Weight: 10, + Blocker: true, + QueueLength: 10, + ThresholdIDs: []string{"*none"}, + MinItems: 5, + Metrics: []*MetricWithFilters{ + { + MetricID: utils.MetaTCD, + }, + }, + } + sq := &StatQueue{ + sqPrfl: sqPrf, + dirty: utils.BoolPointer(false), + Tenant: "cgrates.org", + ID: "SQ1", + SQItems: []SQItem{ + { + EventID: "SqProcessEvent", + ExpiryTime: utils.TimePointer(time.Now()), + }, + }, + SQMetrics: map[string]StatMetric{ + utils.MetaTCD: &StatTCD{ + Sum: time.Minute, + val: utils.DurationPointer(time.Hour), + }, + }, + } + + if err := dm.SetStatQueue(sq); err != nil { + t.Error(err) + } + + args := &utils.TenantIDWithAPIOpts{ + TenantID: &utils.TenantID{ + Tenant: "cgrates.org", + ID: "SQ1", + }, + } + var reply StatQueue + if err := sS.Call(utils.StatSv1GetStatQueue, args, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(reply, *sq) { + t.Errorf("expected: <%+v>, \nreceived: <%+v>", + utils.ToJSON(*sq), utils.ToJSON(reply)) + } +}