Added it test for Stats migration

This commit is contained in:
adragusin
2020-06-05 17:56:35 +03:00
committed by Dan Christian Bogos
parent e097d79a9d
commit 76bd1b9451
2 changed files with 161 additions and 2 deletions

View File

@@ -45,6 +45,7 @@ var sTestsStsIT = []func(t *testing.T){
testStsITConnect,
testStsITFlush,
testStsITMigrateAndMove,
testStsITMigrateFromv1,
}
func TestStatsQueueITRedis(t *testing.T) {
@@ -318,3 +319,160 @@ func testStsITMigrateAndMove(t *testing.T) {
}
}
func testStsITMigrateFromv1(t *testing.T) {
tim := time.Date(2020, time.July, 29, 17, 59, 59, 0, time.UTC)
v1Sts := &v1Stat{
Id: "test",
QueueLength: 10,
TimeWindow: time.Duration(1) * time.Second,
SaveInterval: time.Duration(1) * time.Second,
Metrics: []string{"ASR", "ACD", "ACC"},
SetupInterval: []time.Time{tim},
ToR: []string{},
CdrHost: []string{},
CdrSource: []string{},
ReqType: []string{},
Direction: []string{},
Tenant: []string{},
Category: []string{},
Account: []string{},
Subject: []string{},
DestinationIds: []string{},
UsageInterval: []time.Duration{1 * time.Second},
PddInterval: []time.Duration{1 * time.Second},
Supplier: []string{},
DisconnectCause: []string{},
MediationRunIds: []string{},
RatedAccount: []string{},
RatedSubject: []string{},
CostInterval: []float64{},
Triggers: engine.ActionTriggers{
&engine.ActionTrigger{
ID: "Test",
Balance: &engine.BalanceFilter{
ID: utils.StringPointer("TESTB"),
Timings: []*engine.RITiming{},
ExpirationDate: utils.TimePointer(tim),
Type: utils.StringPointer(utils.MONETARY),
},
ExpirationDate: tim,
LastExecutionTime: tim,
ActivationDate: tim,
ThresholdType: utils.TRIGGER_MAX_BALANCE,
ThresholdValue: 2,
ActionsID: "TEST_ACTIONS",
Executed: true,
},
},
}
err := stsMigrator.dmIN.setV1Stats(v1Sts)
if err != nil {
t.Error("Error when setting v1Stat ", err.Error())
}
if err := stsMigrator.dmIN.DataManager().DataDB().SetVersions(engine.Versions{utils.StatS: 1}, true); err != nil {
t.Errorf("error: <%s> when updating Stats version into dataDB", err.Error())
}
if err := stsMigrator.migrateStats(); err != nil {
t.Error(err)
}
if vrs, err := stsMigrator.dmIN.DataManager().DataDB().GetVersions(utils.StatS); err != nil {
t.Errorf("error: <%s> when updating Stats version into dataDB", err.Error())
} else if vrs[utils.StatS] != 3 {
t.Errorf("Expecting: 3, received: %+v", vrs)
}
//from V1 to V2
var filter *engine.Filter
if filter, err = stsMigrator.dmOut.DataManager().GetFilter("cgrates.org", "test", false, false, utils.NonTransactional); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(config.CgrConfig().GeneralCfg().DefaultTenant, filter.Tenant) {
t.Errorf("Expecting: %+v, received: %+v", config.CgrConfig().GeneralCfg().DefaultTenant, filter.Tenant)
} else if !reflect.DeepEqual(v1Sts.Id, filter.ID) {
t.Errorf("Expecting: %+v, received: %+v", v1Sts.Id, filter.ID)
} else if filter.ActivationInterval != nil {
t.Errorf("Expecting: nil, received: %+v", filter.ActivationInterval)
}
for _, itm := range filter.Rules {
switch itm.Element {
case "SetupInterval":
if itm.Values[0] != tim.String() {
t.Errorf("Expecting: %+v, received: %+v", tim.String(), itm.Values[0])
} else if itm.Type != "*gte" {
t.Errorf("Expecting: *gte, received: %+v", itm.Type)
}
case "UsageInterval":
if itm.Type != "*gte" {
t.Errorf("Expecting: *gte, received: %+v", itm.Type)
} else if itm.Values[0] != "1s" {
t.Errorf("Expecting: 1s, received: %+v", itm.Values[0])
}
case "PddInterval":
if itm.Type != "*gte" {
t.Errorf("Expecting: *gte, received: %+v", itm.Type)
} else if itm.Values[0] != "1s" {
t.Errorf("Expecting: 1s, received: %+v", itm.Values[0])
}
}
}
metrics := []*engine.MetricWithFilters{
{
MetricID: "*asr",
}, {
MetricID: "*acd",
}, {
MetricID: "*acc",
},
}
if statQueueProfile, err := stsMigrator.dmOut.DataManager().GetStatQueueProfile("cgrates.org", "test", false, false, utils.NonTransactional); err != nil {
t.Error(err)
} else if statQueueProfile.ThresholdIDs[0] != "Test" {
t.Errorf("Expecting: 'Test', received: %+v", statQueueProfile.ThresholdIDs[0])
} else if statQueueProfile.Weight != 0 {
t.Errorf("Expecting: '0', received: %+v", statQueueProfile.Weight)
} else if !statQueueProfile.Stored {
t.Errorf("Expecting: 'true', received: %+v", statQueueProfile.Stored)
} else if statQueueProfile.Blocker {
t.Errorf("Expecting: 'false', received: %+v", statQueueProfile.Blocker)
} else if statQueueProfile.QueueLength != 10 {
t.Errorf("Expecting: '10', received: %+v", statQueueProfile.QueueLength)
} else if statQueueProfile.ID != "test" {
t.Errorf("Expecting: 'test', received: %+v", statQueueProfile.ID)
} else if statQueueProfile.Tenant != "cgrates.org" {
t.Errorf("Expecting: 'cgrates.org', received: %+v", statQueueProfile.Tenant)
} else if statQueueProfile.MinItems != 0 {
t.Errorf("Expecting: '0', received: %+v", statQueueProfile.MinItems)
} else if statQueueProfile.TTL != 0 {
t.Errorf("Expecting: '0', received: %+v", statQueueProfile.TTL)
} else if !reflect.DeepEqual(statQueueProfile.Metrics, metrics) {
t.Errorf("Expecting: %+v, received: %+v", metrics, statQueueProfile.Metrics)
}
//from V2 to V3
var statQueue *engine.StatQueue
if statQueue, err = stsMigrator.dmOut.DataManager().GetStatQueue("cgrates.org", "test", false, false, utils.NonTransactional); err != nil {
t.Error(err)
} else if statQueue.MinItems != 0 {
t.Errorf("Expecting: '0', received: %+v", statQueue.MinItems)
} else if statQueue.ID != "test" {
t.Errorf("Expecting: 'test', received: %+v", statQueue.ID)
} else if statQueue.Tenant != "cgrates.org" {
t.Errorf("Expecting: 'cgrates.org', received: %+v", statQueue.Tenant)
} else if len(statQueue.SQItems) != 0 {
t.Errorf("Expecting: '0', received: %+v", len(statQueue.SQItems))
}
if _, ok := statQueue.SQMetrics["*acc"]; !ok {
t.Errorf("Expecting *acc item to be present in SQMetrics")
}
if _, ok := statQueue.SQMetrics["*acd"]; !ok {
t.Errorf("Expecting *acd item to be present in SQMetrics")
}
if _, ok := statQueue.SQMetrics["*asr"]; !ok {
t.Errorf("Expecting *asr item to be present in SQMetrics")
}
}

View File

@@ -34,6 +34,7 @@ const (
v1AliasCol = "aliases"
v1UserCol = "users"
v1DerivedChargersCol = "derived_chargers"
v2StatsCol = "statqueues"
)
type mongoMigrator struct {
@@ -283,7 +284,7 @@ func (v1ms *mongoMigrator) setV1Stats(x *v1Stat) (err error) {
// get V2
func (v1ms *mongoMigrator) getV2Stats() (v2 *engine.StatQueue, err error) {
if v1ms.cursor == nil {
v1ms.cursor, err = v1ms.mgoDB.DB().Collection(utils.StatQueuePrefix).Find(v1ms.mgoDB.GetContext(), bson.D{})
v1ms.cursor, err = v1ms.mgoDB.DB().Collection(v2StatsCol).Find(v1ms.mgoDB.GetContext(), bson.D{})
if err != nil {
return nil, err
}
@@ -302,7 +303,7 @@ func (v1ms *mongoMigrator) getV2Stats() (v2 *engine.StatQueue, err error) {
// set v2
func (v1ms *mongoMigrator) setV2Stats(v2 *engine.StatQueue) (err error) {
_, err = v1ms.mgoDB.DB().Collection(utils.StatQueuePrefix).InsertOne(v1ms.mgoDB.GetContext(), v2)
_, err = v1ms.mgoDB.DB().Collection(v2StatsCol).InsertOne(v1ms.mgoDB.GetContext(), v2)
return
}