Skip to content

Commit

Permalink
Merge pull request cgrates#2250 from adragusin/master
Browse files Browse the repository at this point in the history
Added test for migration stats
  • Loading branch information
danbogos authored Jun 30, 2020
2 parents d79d9a8 + 98d084e commit 4ca51d2
Show file tree
Hide file tree
Showing 60 changed files with 243 additions and 529 deletions.
22 changes: 4 additions & 18 deletions migrator/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package migrator

import (
"errors"
"fmt"
"log"
"strings"
"time"
Expand Down Expand Up @@ -122,17 +121,8 @@ func (m *Migrator) migrateV2Accounts() (v3Acnt *engine.Account, err error) {
func (m *Migrator) migrateAccounts() (err error) {
var vrs engine.Versions
current := engine.CurrentDataDBVersions()
vrs, err = m.dmIN.DataManager().DataDB().GetVersions(utils.EmptyString)
if err != nil {
return utils.NewCGRError(utils.Migrator,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("error: <%s> when querying oldDataDB for versions", err.Error()))
} else if len(vrs) == 0 {
return utils.NewCGRError(utils.Migrator,
utils.MandatoryIEMissingCaps,
utils.UndefinedVersion,
"version number is not defined for Actions")
if vrs, err = m.getVersions(utils.Accounts); err != nil {
return
}
migrated := true
migratedFrom := 0
Expand Down Expand Up @@ -202,12 +192,8 @@ func (m *Migrator) migrateAccounts() (err error) {
}

// All done, update version wtih current one
vrs = engine.Versions{utils.Accounts: engine.CurrentDataDBVersions()[utils.Accounts]}
if err = m.dmOut.DataManager().DataDB().SetVersions(vrs, false); err != nil {
return utils.NewCGRError(utils.Migrator,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("error: <%s> when updating Accounts version into StorDB", err.Error()))
if err = m.setVersions(utils.Accounts); err != nil {
return err
}
return m.ensureIndexesDataDB(engine.ColAcc)
}
Expand Down
2 changes: 2 additions & 0 deletions migrator/accounts2_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,5 +256,7 @@ func testAcc2ITMigrate(t *testing.T) {
t.Errorf("Expecting: %+v, received: %+v", testAccount.BalanceMap, result.BalanceMap)
} else if !reflect.DeepEqual(testAccount.UnitCounters, result.UnitCounters) {
t.Errorf("Expecting: %+v, received: %+v", testAccount.UnitCounters, result.UnitCounters)
} else if acc2Migrator.stats[utils.Accounts] != 1 {
t.Errorf("Expecting: 1, received: %+v", acc2Migrator.stats[utils.Accounts])
}
}
4 changes: 4 additions & 0 deletions migrator/accounts_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ func testAccITMigrateAndMove(t *testing.T) {
t.Errorf("Expecting: %+v, received: %+v", testAccount.BalanceMap, result.BalanceMap)
} else if !reflect.DeepEqual(testAccount.UnitCounters, result.UnitCounters) {
t.Errorf("Expecting: %+v, received: %+v", testAccount.UnitCounters, result.UnitCounters)
} else if accMigrator.stats[utils.Accounts] != 1 {
t.Errorf("Expecting: 1, received: %+v", accMigrator.stats[utils.Accounts])
}
case utils.Move:
//set an account in dmIN
Expand Down Expand Up @@ -363,6 +365,8 @@ func testAccITMigrateAndMove(t *testing.T) {
result, err = accMigrator.dmIN.DataManager().GetAccount(testAccount.ID)
if err != utils.ErrNotFound {
t.Error(err)
} else if accMigrator.stats[utils.Accounts] != 1 {
t.Errorf("Expecting: 1, received: %+v", accMigrator.stats[utils.Accounts])
}

}
Expand Down
22 changes: 4 additions & 18 deletions migrator/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package migrator

import (
"fmt"
"strings"

"github.com/cgrates/cgrates/engine"
Expand Down Expand Up @@ -98,17 +97,8 @@ func (m *Migrator) migrateV1Actions() (acts engine.Actions, err error) {
func (m *Migrator) migrateActions() (err error) {
var vrs engine.Versions
current := engine.CurrentDataDBVersions()
vrs, err = m.dmIN.DataManager().DataDB().GetVersions("")
if err != nil {
return utils.NewCGRError(utils.Migrator,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("error: <%s> when querying oldDataDB for versions", err.Error()))
} else if len(vrs) == 0 {
return utils.NewCGRError(utils.Migrator,
utils.MandatoryIEMissingCaps,
utils.UndefinedVersion,
"version number is not defined for ActionTriggers model")
if vrs, err = m.getVersions(utils.Actions); err != nil {
return
}
migrated := true
var acts engine.Actions
Expand Down Expand Up @@ -151,12 +141,8 @@ func (m *Migrator) migrateActions() (err error) {
// remove old actions

// All done, update version wtih current one
vrs = engine.Versions{utils.Actions: engine.CurrentStorDBVersions()[utils.Actions]}
if err = m.dmOut.DataManager().DataDB().SetVersions(vrs, false); err != nil {
return utils.NewCGRError(utils.Migrator,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("error: <%s> when updating Actions version into dataDB", err.Error()))
if err = m.setVersions(utils.Actions); err != nil {
return err
}

return m.ensureIndexesDataDB(engine.ColAct)
Expand Down
4 changes: 4 additions & 0 deletions migrator/action_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ func testActITMigrateAndMove(t *testing.T) {
}
if !reflect.DeepEqual(act, &result) {
t.Errorf("Expecting: %+v, received: %+v", act, &result)
} else if actMigrator.stats[utils.Actions] != 1 {
t.Errorf("Expecting: 1, received: %+v", actMigrator.stats[utils.Actions])
}
case utils.Move:
if err := actMigrator.dmIN.DataManager().SetActions(v1act.Id, *act, utils.NonTransactional); err != nil {
Expand All @@ -287,6 +289,8 @@ func testActITMigrateAndMove(t *testing.T) {
}
if !reflect.DeepEqual(act, &result) {
t.Errorf("Expecting: %+v, received: %+v", act, &result)
} else if actMigrator.stats[utils.Actions] != 1 {
t.Errorf("Expecting: 1, received: %+v", actMigrator.stats[utils.Actions])
}
}
}
21 changes: 4 additions & 17 deletions migrator/action_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,8 @@ func (m *Migrator) migrateV1ActionPlans() (v2 []*engine.ActionPlan, err error) {
func (m *Migrator) migrateActionPlans() (err error) {
var vrs engine.Versions
current := engine.CurrentDataDBVersions()
vrs, err = m.dmIN.DataManager().DataDB().GetVersions("")
if err != nil {
return utils.NewCGRError(utils.Migrator,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("error: <%s> when querying oldDataDB for versions", err.Error()))
} else if len(vrs) == 0 {
return utils.NewCGRError(utils.Migrator,
utils.MandatoryIEMissingCaps,
utils.UndefinedVersion,
"version number is not defined for ActionTriggers model")
if vrs, err = m.getVersions(utils.ActionPlans); err != nil {
return
}
if m.dmIN.DataManager().DataDB().GetStorageType() == utils.REDIS { // if redis rebuild action plans indexes
redisDB, can := m.dmIN.DataManager().DataDB().(*engine.RedisStorage)
Expand Down Expand Up @@ -188,12 +179,8 @@ func (m *Migrator) migrateActionPlans() (err error) {
}

// All done, update version wtih current one
vrs = engine.Versions{utils.ActionPlans: engine.CurrentDataDBVersions()[utils.ActionPlans]}
if err = m.dmOut.DataManager().DataDB().SetVersions(vrs, false); err != nil {
return utils.NewCGRError(utils.Migrator,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("error: <%s> when updating ActionPlans version into dataDB", err.Error()))
if err = m.setVersions(utils.ActionPlans); err != nil {
return err
}
return m.ensureIndexesDataDB(engine.ColApl)
}
Expand Down
4 changes: 4 additions & 0 deletions migrator/action_plan_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ func testActPlnITMigrateAndMove(t *testing.T) {
t.Errorf("Expecting: %+v, received: %+v", actPln.AccountIDs, result.AccountIDs)
} else if !reflect.DeepEqual(actPln.ActionTimings[0].Timing, result.ActionTimings[0].Timing) {
t.Errorf("Expecting: %+v, received: %+v", actPln.ActionTimings[0].Timing, result.ActionTimings[0].Timing)
} else if actPlnMigrator.stats[utils.ActionPlans] != 1 {
t.Errorf("Expecting: 1, received: %+v", actPlnMigrator.stats[utils.ActionPlans])
}
case utils.Move:
if err := actPlnMigrator.dmIN.DataManager().SetActionPlan((*v1actPln)[0].Id, actPln, true, utils.NonTransactional); err != nil {
Expand Down Expand Up @@ -291,6 +293,8 @@ func testActPlnITMigrateAndMove(t *testing.T) {
result, err = actPlnMigrator.dmIN.DataManager().GetActionPlan((*v1actPln)[0].Id, true, utils.NonTransactional)
if err != utils.ErrNotFound {
t.Error(err)
} else if actPlnMigrator.stats[utils.ActionPlans] != 1 {
t.Errorf("Expecting: 1, received: %+v", actPlnMigrator.stats[utils.ActionPlans])
}
}
}
24 changes: 6 additions & 18 deletions migrator/action_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package migrator

import (
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -70,6 +69,8 @@ func (m *Migrator) migrateCurrentActionTrigger() (err error) {
if err := m.dmOut.DataManager().SetActionTriggers(idg, acts, utils.NonTransactional); err != nil {
return err
}
m.stats[utils.ActionTriggers]++

}
return
}
Expand Down Expand Up @@ -111,17 +112,8 @@ func (m *Migrator) removeV1ActionTriggers() (err error) {
func (m *Migrator) migrateActionTriggers() (err error) {
var vrs engine.Versions
current := engine.CurrentDataDBVersions()
vrs, err = m.dmIN.DataManager().DataDB().GetVersions(utils.EmptyString)
if err != nil {
return utils.NewCGRError(utils.Migrator,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("error: <%s> when querying oldDataDB for versions", err.Error()))
} else if len(vrs) == 0 {
return utils.NewCGRError(utils.Migrator,
utils.MandatoryIEMissingCaps,
utils.UndefinedVersion,
"version number is not defined for ActionTriggers model")
if vrs, err = m.getVersions(utils.ActionTriggers); err != nil {
return
}
migrated := true
migratedFrom := 0
Expand Down Expand Up @@ -175,12 +167,8 @@ func (m *Migrator) migrateActionTriggers() (err error) {
}

// All done, update version wtih current one
vrs = engine.Versions{utils.ActionTriggers: engine.CurrentDataDBVersions()[utils.ActionTriggers]}
if err = m.dmOut.DataManager().DataDB().SetVersions(vrs, false); err != nil {
return utils.NewCGRError(utils.Migrator,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("error: <%s> when updating ActionTriggers version into DataDB", err.Error()))
if err = m.setVersions(utils.ActionTriggers); err != nil {
return err
}

return m.ensureIndexesDataDB(engine.ColAtr)
Expand Down
6 changes: 5 additions & 1 deletion migrator/action_trigger_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,9 @@ func testActTrgITMigrateAndMove(t *testing.T) {
}
if !reflect.DeepEqual(actTrg, result) {
t.Errorf("Expecting: %+v,\nReceived: %+v", utils.ToJSON(actTrg), utils.ToJSON(result))
} else if actTrgMigrator.stats[utils.ActionTriggers] != 1 {
t.Errorf("Expecting: 1, received: %+v", actTrgMigrator.stats[utils.ActionTriggers])
}
// utils.tojson si verificat
case utils.Move:
if err := actTrgMigrator.dmIN.DataManager().SetActionTriggers((*v1actTrg)[0].Id, actTrg, utils.NonTransactional); err != nil {
t.Error("Error when setting ActionTriggers ", err.Error())
Expand All @@ -253,5 +254,8 @@ func testActTrgITMigrateAndMove(t *testing.T) {
if !reflect.DeepEqual(actTrg, result) {
t.Errorf("Expecting: %+v, received: %+v", actTrg, result)
}
if actTrgMigrator.stats[utils.ActionTriggers] != 1 {
t.Errorf("Expecting: 1, received: %+v", actTrgMigrator.stats[utils.ActionTriggers])
}
}
}
2 changes: 2 additions & 0 deletions migrator/alias_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,7 @@ func testAlsITMigrateAndMove(t *testing.T) {
t.Error(err)
} else if !reflect.DeepEqual(expAlsIdx, alsidx) {
t.Errorf("Expected %v, recived: %v", utils.ToJSON(expAlsIdx), utils.ToJSON(alsidx))
} else if alsMigrator.stats[Alias] != 1 {
t.Errorf("Expected 1, recived: %v", alsMigrator.stats[Alias] != 1)
}
}
16 changes: 4 additions & 12 deletions migrator/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,8 @@ func (m *Migrator) migrateV4ToV5AttributeProfile(v4Attr *v4AttributeProfile) (v5
func (m *Migrator) migrateAttributeProfile() (err error) {
var vrs engine.Versions
current := engine.CurrentDataDBVersions()
vrs, err = m.dmIN.DataManager().DataDB().GetVersions(utils.EmptyString)
if err != nil {
//error getting the current verions
return utils.NewCGRError(utils.Migrator, utils.ServerErrorCaps,
err.Error(), fmt.Sprintf("error: <%s> when querying oldDataDB for versions", err.Error()))
} else if len(vrs) == 0 {
return utils.NewCGRError(utils.Migrator, utils.MandatoryIEMissingCaps,
utils.UndefinedVersion, "version number is not defined for ActionTriggers model")
if vrs, err = m.getVersions(utils.Attributes); err != nil {
return
}

migrated := true
Expand Down Expand Up @@ -252,10 +246,8 @@ func (m *Migrator) migrateAttributeProfile() (err error) {
return nil
}
// All done, update version with current one
vrs = engine.Versions{utils.Attributes: engine.CurrentDataDBVersions()[utils.Attributes]}
if err = m.dmOut.DataManager().DataDB().SetVersions(vrs, false); err != nil {
return utils.NewCGRError(utils.Migrator, utils.ServerErrorCaps, err.Error(),
fmt.Sprintf("error: <%s> when updating Attributes version into dataDB", err.Error()))
if err = m.setVersions(utils.Attributes); err != nil {
return err
}
return m.ensureIndexesDataDB(engine.ColAttr)

Expand Down
47 changes: 47 additions & 0 deletions migrator/attributes_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ func testAttrITMigrateOnlyVersion(t *testing.T) {
} else if vrs[utils.Attributes] != 5 {
t.Errorf("Unexpected version returned: %d", vrs[utils.Attributes])
}
if attrMigrator.stats[utils.Attributes] != 0 {
t.Errorf("Expecting: 0, received: %+v", attrMigrator.stats[utils.Attributes])
}
}

func testAttrITMigrateAndMove(t *testing.T) {
Expand Down Expand Up @@ -374,6 +377,8 @@ func testAttrITMigrateAndMove(t *testing.T) {
attrPrf.Compile()
if !reflect.DeepEqual(result, attrPrf) {
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(attrPrf), utils.ToJSON(result))
} else if attrMigrator.stats[utils.Attributes] != 1 {
t.Errorf("Expecting: 1, received: %+v", attrMigrator.stats[utils.Attributes])
}
case utils.Move:
if err := attrMigrator.dmIN.DataManager().SetAttributeProfile(attrPrf, false); err != nil {
Expand Down Expand Up @@ -431,6 +436,8 @@ func testAttrITMigrateAndMove(t *testing.T) {
if _, err = attrMigrator.dmIN.DataManager().GetAttributeProfile("cgrates.com",
"ATTR_1", false, false, utils.NonTransactional); err == nil || err != utils.ErrNotFound {
t.Error(err)
} else if attrMigrator.stats[utils.Attributes] != 2 {
t.Errorf("Expecting: 2, received: %+v", attrMigrator.stats[utils.Attributes])
}
}
}
Expand Down Expand Up @@ -598,6 +605,9 @@ func testAttrITMigrateV3(t *testing.T) {
if !reflect.DeepEqual(result, attrPrf) {
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(attrPrf), utils.ToJSON(result))
}
if attrMigrator.stats[utils.Attributes] != 3 {
t.Errorf("Expecting: 3, received: %+v", attrMigrator.stats[utils.Attributes])
}
}

func testAttrITMigrateV4(t *testing.T) {
Expand Down Expand Up @@ -807,6 +817,15 @@ func testAttrITV1ToV5(t *testing.T) {
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eOut2), utils.ToJSON(result))
}
}
if attrAction == utils.Move {
if attrMigrator.stats[utils.Attributes] != 4 {
t.Errorf("Expecting: 4, received: %+v", attrMigrator.stats[utils.Attributes])
}
} else {
if attrMigrator.stats[utils.Attributes] != 6 {
t.Errorf("Expecting: 6, received: %+v", attrMigrator.stats[utils.Attributes])
}
}
}

func testAttrITV2ToV5(t *testing.T) {
Expand Down Expand Up @@ -1065,6 +1084,15 @@ func testAttrITV3ToV5(t *testing.T) {
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eOut2), utils.ToJSON(result))
}
}
if attrAction == utils.Move {
if attrMigrator.stats[utils.Attributes] != 8 {
t.Errorf("Expecting: 8, received: %+v", attrMigrator.stats[utils.Attributes])
}
} else {
if attrMigrator.stats[utils.Attributes] != 10 {
t.Errorf("Expecting: 10, received: %+v", attrMigrator.stats[utils.Attributes])
}
}
}

func testAttrITdryRunV2ToV5(t *testing.T) {
Expand Down Expand Up @@ -1114,6 +1142,16 @@ func testAttrITdryRunV2ToV5(t *testing.T) {
if err != nil && err != utils.ErrNotFound {
t.Errorf("Expecting: %+v, received: %+v", utils.ErrNotFound, err)
}

if attrAction == utils.Move {
if attrMigrator.stats[utils.Attributes] != 9 {
t.Errorf("Expecting: 9, received: %+v", attrMigrator.stats[utils.Attributes])
}
} else {
if attrMigrator.stats[utils.Attributes] != 11 {
t.Errorf("Expecting: 11, received: %+v", attrMigrator.stats[utils.Attributes])
}
}
}

func testAttrITdryRunV3ToV5(t *testing.T) {
Expand Down Expand Up @@ -1162,4 +1200,13 @@ func testAttrITdryRunV3ToV5(t *testing.T) {
if err != nil && err != utils.ErrNotFound {
t.Errorf("Expecting: %+v, received: %+v", utils.ErrNotFound, err)
}
if attrAction == utils.Move {
if attrMigrator.stats[utils.Attributes] != 10 {
t.Errorf("Expecting: 10, received: %+v", attrMigrator.stats[utils.Attributes])
}
} else {
if attrMigrator.stats[utils.Attributes] != 12 {
t.Errorf("Expecting: 12, received: %+v", attrMigrator.stats[utils.Attributes])
}
}
}
Loading

0 comments on commit 4ca51d2

Please sign in to comment.