From 7b44cd164f28cd3e3c68b2a53bc9ab63a64bba82 Mon Sep 17 00:00:00 2001 From: arberkatellari Date: Thu, 22 Dec 2022 10:36:45 -0500 Subject: [PATCH] Coverage tests at engine --- engine/connmanager_test.go | 108 +++++++++++ engine/datamanager_test.go | 356 ++++++++++++++++++++++++++++++++++ engine/dispatcherprfl_test.go | 45 +++++ engine/libindex_test.go | 124 ++++++++++++ 4 files changed, 633 insertions(+) diff --git a/engine/connmanager_test.go b/engine/connmanager_test.go index 3fd317446..8c8dabb3e 100644 --- a/engine/connmanager_test.go +++ b/engine/connmanager_test.go @@ -591,6 +591,10 @@ func TestCMDeadLock(t *testing.T) { */ func TestCMEnableDispatcher(t *testing.T) { + tmp := Cache + defer func() { + Cache = tmp + }() Cache.Clear(nil) cfg := config.NewDefaultCGRConfig() cM := NewConnManager(cfg) @@ -617,3 +621,107 @@ func TestCMEnableDispatcher(t *testing.T) { } } + +func TestCMGetInternalChan(t *testing.T) { + + cfg := config.NewDefaultCGRConfig() + cM := NewConnManager(cfg) + cM.dispIntCh = cM.rpcInternal + + exp := make(chan context.ClientConnector, 1) + rcv := cM.GetInternalChan() + rcvType := reflect.TypeOf(rcv) + expType := reflect.TypeOf(exp) + if rcvType != expType { + t.Errorf("Unexpected return type, expected %+v, received %+v", rcvType, expType) + } + +} + +func TestCMGetDispInternalChan(t *testing.T) { + + cfg := config.NewDefaultCGRConfig() + cM := NewConnManager(cfg) + cM.dispIntCh = cM.rpcInternal + + exp := make(chan context.ClientConnector, 1) + rcv := cM.GetDispInternalChan() + rcvType := reflect.TypeOf(rcv) + expType := reflect.TypeOf(exp) + if rcvType != expType { + t.Errorf("Unexpected return type, expected %+v, received %+v", rcvType, expType) + } + +} + +func TestCMDisableDispatcher(t *testing.T) { + tmp := Cache + defer func() { + Cache = tmp + }() + Cache.Clear(nil) + cfg := config.NewDefaultCGRConfig() + + cM := &ConnManager{ + cfg: cfg, + connCache: ltcache.NewCache(-1, 0, true, nil), + } + cM.connCache.Set("itmID1", "value of first item", nil) + data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items) + dm := NewDataManager(data, cfg.CacheCfg(), nil) + fltrs := NewFilterS(cfg, nil, dm) + newCDRSrv := NewCDRServer(cfg, dm, fltrs, nil) + newSrvcWName, err := NewServiceWithName(newCDRSrv, utils.AccountS, true) + if err != nil { + t.Error(err) + } + cM.EnableDispatcher(newSrvcWName) + + Cache = NewCacheS(cfg, dm, cM, nil) + Cache.SetWithoutReplicate(utils.CacheRPCConnections, "itmID2", + "value of 2nd item", nil, true, utils.NonTransactional) + + var exp []string + + cM.DisableDispatcher() + rcv1 := cM.connCache.GetItemIDs("itmID1") + rcv2 := Cache.GetItemIDs(utils.CacheRPCConnections, utils.EmptyString) + + if !reflect.DeepEqual(rcv1, exp) { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", exp, rcv1) + } else if !reflect.DeepEqual(rcv2, exp) { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", exp, rcv2) + } else if cM.disp != nil || cM.dispIntCh != nil { + t.Errorf("\nexpected nil cM.disp and cM.dispIntCh, \nreceived cM.disp: <%+v>, \n cM.dispIntCh: <%+v>", cM.disp, cM.dispIntCh) + } + +} + +func TestCMgetInternalConnChanFromDisp(t *testing.T) { + tmp := Cache + defer func() { + Cache = tmp + }() + Cache.Clear(nil) + cfg := config.NewDefaultCGRConfig() + + cM := &ConnManager{ + cfg: cfg, + connCache: ltcache.NewCache(-1, 0, true, nil), + } + cM.connCache.Set("itmID1", "value of first item", nil) + data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items) + dm := NewDataManager(data, cfg.CacheCfg(), nil) + fltrs := NewFilterS(cfg, nil, dm) + newCDRSrv := NewCDRServer(cfg, dm, fltrs, nil) + newSrvcWName, err := NewServiceWithName(newCDRSrv, utils.AccountS, true) + if err != nil { + t.Error(err) + } + cM.EnableDispatcher(newSrvcWName) + + if rcv, ok := cM.getInternalConnChan(utils.ConcatenatedKey(utils.MetaInternal, utils.MetaAccounts)); !ok { + t.Errorf("Unexpected error getting internalConnChan, Received <%+v>", rcv) + } + +} diff --git a/engine/datamanager_test.go b/engine/datamanager_test.go index e6b563ddb..44a3315d1 100644 --- a/engine/datamanager_test.go +++ b/engine/datamanager_test.go @@ -1290,3 +1290,359 @@ func TestDataManagerSetDispatcherHostReplicateTrue(t *testing.T) { dm.SetDispatcherHost(context.Background(), dpp) } + +func TestDMRemoveAccountReplicate(t *testing.T) { + + tmp := Cache + cfgtmp := config.CgrConfig() + defer func() { + Cache = tmp + config.SetCgrConfig(cfgtmp) + }() + Cache.Clear(nil) + + cfg := config.NewDefaultCGRConfig() + cfg.DataDbCfg().Items[utils.MetaAccounts].Replicate = true + config.SetCgrConfig(cfg) + + data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items) + cM := NewConnManager(cfg) + dm := NewDataManager(data, cfg.CacheCfg(), cM) + dm.dataDB = &DataDBMock{ + GetAccountDrvF: func(ctx *context.Context, str1, str2 string) (*utils.Account, error) { + return &utils.Account{}, nil + }, + RemoveAccountDrvF: func(ctx *context.Context, str1, str2 string) error { + return nil + }, + } + + // tested replicate + if err := dm.RemoveAccount(context.Background(), utils.CGRateSorg, "accId", false); err != nil { + t.Error(err) + } +} + +func TestDMSetAccountNilDM(t *testing.T) { + + var dm *DataManager + ap := &utils.Account{} + + expErr := utils.ErrNoDatabaseConn + if err := dm.SetAccount(context.Background(), ap, false); err == nil || err != utils.ErrNoDatabaseConn { + t.Errorf("Expected error <%v>, Received <%v>", expErr, err) + } +} + +func TestDMSetAccountcheckFiltersErr(t *testing.T) { + + tmp := Cache + defer func() { + Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items) + cM := NewConnManager(cfg) + dm := NewDataManager(data, cfg.CacheCfg(), cM) + + ap := &utils.Account{ + Tenant: "cgrates.org", + ID: "accId", + Weights: []*utils.DynamicWeight{ + { + Weight: 0, + }, + }, + FilterIDs: []string{":::"}, + Balances: map[string]*utils.Balance{ + "AbstractBalance1": { + ID: "AbstractBalance1", + Weights: utils.DynamicWeights{ + { + Weight: 25, + }, + }, + Type: utils.MetaAbstract, + Units: utils.NewDecimal(int64(40*time.Second), 0), + CostIncrements: []*utils.CostIncrement{ + { + Increment: utils.NewDecimal(int64(time.Second), 0), + FixedFee: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(0, 0), + }, + }, + }, + }, + Blockers: utils.DynamicBlockers{ + { + FilterIDs: []string{"fltrID"}, + Blocker: true, + }, + }, + Opts: make(map[string]interface{}), + ThresholdIDs: []string{utils.MetaNone}, + } + + expErr := "broken reference to filter: <:::> for item with ID: cgrates.org:accId" + if err := dm.SetAccount(context.Background(), ap, true); err == nil || err.Error() != expErr { + t.Errorf("Expected error <%v>, Received <%v>", expErr, err) + } +} + +func TestDMSetAccountGetAccountErr(t *testing.T) { + + tmp := Cache + defer func() { + Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items) + cM := NewConnManager(cfg) + dm := NewDataManager(data, cfg.CacheCfg(), cM) + dm.dataDB = &DataDBMock{ + GetAccountDrvF: func(ctx *context.Context, str1, str2 string) (*utils.Account, error) { + return nil, utils.ErrNotImplemented + }, + } + + ap := &utils.Account{ + Tenant: "cgrates.org", + ID: "accId", + Weights: []*utils.DynamicWeight{ + { + Weight: 0, + }, + }, + FilterIDs: []string{"*stirng:~*req.Account:1001"}, + Balances: map[string]*utils.Balance{ + "AbstractBalance1": { + ID: "AbstractBalance1", + Weights: utils.DynamicWeights{ + { + Weight: 25, + }, + }, + Type: utils.MetaAbstract, + Units: utils.NewDecimal(int64(40*time.Second), 0), + CostIncrements: []*utils.CostIncrement{ + { + Increment: utils.NewDecimal(int64(time.Second), 0), + FixedFee: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(0, 0), + }, + }, + }, + }, + Blockers: utils.DynamicBlockers{ + { + FilterIDs: []string{"fltrID"}, + Blocker: true, + }, + }, + Opts: make(map[string]interface{}), + ThresholdIDs: []string{utils.MetaNone}, + } + + expErr := utils.ErrNotImplemented + if err := dm.SetAccount(context.Background(), ap, true); err == nil || err != expErr { + t.Errorf("Expected error <%v>, Received <%v>", expErr, err) + } +} + +func TestDMSetAccountSetAccountDrvErr(t *testing.T) { + + tmp := Cache + defer func() { + Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items) + cM := NewConnManager(cfg) + dm := NewDataManager(data, cfg.CacheCfg(), cM) + dm.dataDB = &DataDBMock{ + GetAccountDrvF: func(ctx *context.Context, str1, str2 string) (*utils.Account, error) { + return &utils.Account{ + Tenant: "cgrates.org", + }, nil + }, + SetAccountDrvF: func(ctx *context.Context, profile *utils.Account) error { + return utils.ErrNotImplemented + }, + } + + ap := &utils.Account{ + Tenant: "cgrates.org", + ID: "accId", + Weights: []*utils.DynamicWeight{ + { + Weight: 0, + }, + }, + FilterIDs: []string{"*stirng:~*req.Account:1001"}, + Balances: map[string]*utils.Balance{ + "AbstractBalance1": { + ID: "AbstractBalance1", + Weights: utils.DynamicWeights{ + { + Weight: 25, + }, + }, + Type: utils.MetaAbstract, + Units: utils.NewDecimal(int64(40*time.Second), 0), + CostIncrements: []*utils.CostIncrement{ + { + Increment: utils.NewDecimal(int64(time.Second), 0), + FixedFee: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(0, 0), + }, + }, + }, + }, + Blockers: utils.DynamicBlockers{ + { + FilterIDs: []string{"fltrID"}, + Blocker: true, + }, + }, + Opts: make(map[string]interface{}), + ThresholdIDs: []string{utils.MetaNone}, + } + + expErr := utils.ErrNotImplemented + if err := dm.SetAccount(context.Background(), ap, true); err == nil || err != expErr { + t.Errorf("Expected error <%v>, Received <%v>", expErr, err) + } +} + +func TestDMSetAccountupdatedIndexesErr(t *testing.T) { + + tmp := Cache + defer func() { + Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items) + cM := NewConnManager(cfg) + dm := NewDataManager(data, cfg.CacheCfg(), cM) + dm.dataDB = &DataDBMock{ + GetAccountDrvF: func(ctx *context.Context, str1, str2 string) (*utils.Account, error) { + return &utils.Account{ + Tenant: "cgrates.org", + }, nil + }, + SetAccountDrvF: func(ctx *context.Context, profile *utils.Account) error { + return nil + }, + } + + ap := &utils.Account{ + Tenant: "cgrates.org", + ID: "accId", + Weights: []*utils.DynamicWeight{ + { + Weight: 0, + }, + }, + FilterIDs: []string{"*stirng:~*req.Account:1001"}, + Balances: map[string]*utils.Balance{ + "AbstractBalance1": { + ID: "AbstractBalance1", + Weights: utils.DynamicWeights{ + { + Weight: 25, + }, + }, + Type: utils.MetaAbstract, + Units: utils.NewDecimal(int64(40*time.Second), 0), + CostIncrements: []*utils.CostIncrement{ + { + Increment: utils.NewDecimal(int64(time.Second), 0), + FixedFee: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(0, 0), + }, + }, + }, + }, + Blockers: utils.DynamicBlockers{ + { + FilterIDs: []string{"fltrID"}, + Blocker: true, + }, + }, + Opts: make(map[string]interface{}), + ThresholdIDs: []string{utils.MetaNone}, + } + + expErr := utils.ErrNotImplemented + if err := dm.SetAccount(context.Background(), ap, true); err == nil || err != expErr { + t.Errorf("Expected error <%v>, Received <%v>", expErr, err) + } +} + +func TestDMSetAccountReplicateTrue(t *testing.T) { + + tmp := Cache + defer func() { + Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + cfg.DataDbCfg().Items[utils.MetaAccounts].Replicate = true + config.SetCgrConfig(cfg) + + data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items) + cM := NewConnManager(cfg) + dm := NewDataManager(data, cfg.CacheCfg(), cM) + dm.dataDB = &DataDBMock{ + GetAccountDrvF: func(ctx *context.Context, str1, str2 string) (*utils.Account, error) { + return &utils.Account{}, nil + }, + SetAccountDrvF: func(ctx *context.Context, profile *utils.Account) error { + return nil + }, + } + + ap := &utils.Account{ + Tenant: "cgrates.org", + ID: "accId", + Weights: []*utils.DynamicWeight{ + { + Weight: 0, + }, + }, + FilterIDs: []string{"*stirng:~*req.Account:1001"}, + Balances: map[string]*utils.Balance{ + "AbstractBalance1": { + ID: "AbstractBalance1", + Weights: utils.DynamicWeights{ + { + Weight: 25, + }, + }, + Type: utils.MetaAbstract, + Units: utils.NewDecimal(int64(40*time.Second), 0), + CostIncrements: []*utils.CostIncrement{ + { + Increment: utils.NewDecimal(int64(time.Second), 0), + FixedFee: utils.NewDecimal(0, 0), + RecurrentFee: utils.NewDecimal(0, 0), + }, + }, + }, + }, + Blockers: utils.DynamicBlockers{ + { + FilterIDs: []string{"fltrID"}, + Blocker: true, + }, + }, + Opts: make(map[string]interface{}), + ThresholdIDs: []string{utils.MetaNone}, + } + // tests replicete + dm.SetAccount(context.Background(), ap, false) +} diff --git a/engine/dispatcherprfl_test.go b/engine/dispatcherprfl_test.go index b30f09666..b923e8fd6 100644 --- a/engine/dispatcherprfl_test.go +++ b/engine/dispatcherprfl_test.go @@ -853,3 +853,48 @@ func TestDispatcherHostProfileMerge(t *testing.T) { t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(exp), utils.ToJSON(dspHost)) } } + +type cMock struct { + rcvM string +} + +func (*cMock) Call(ctx *context.Context, serviceMethod string, args, reply interface{}) error { + return nil +} +func TestDispatcherHostGetConnExistingConn(t *testing.T) { + Cache.Clear(nil) + + cfg := config.NewDefaultCGRConfig() + chanRPC := make(chan birpc.ClientConnector, 1) + chanRPC <- &cMock{ + rcvM: "testM", + } + connMgr := NewConnManager(cfg) + connMgr.AddInternalConn(utils.ConcatenatedKey(utils.MetaInternal, utils.MetaAttributes), utils.AttributeSv1, chanRPC) + dH := &DispatcherHost{ + Tenant: "cgrates.org", + RemoteHost: &config.RemoteHost{ + ID: "ID", + Address: "127.0.0.1", + Transport: utils.MetaJSON, + ConnectAttempts: 1, + Reconnects: 1, + ConnectTimeout: time.Nanosecond, + ReplyTimeout: time.Nanosecond, + TLS: true, + ClientKey: "key", + ClientCertificate: "ce", + CaCertificate: "ca", + }, + rpcConn: <-chanRPC, + } + + exp := &cMock{rcvM: "testM"} + + if rcv, err := dH.GetConn(context.Background(), cfg, make(chan birpc.ClientConnector, 1)); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(exp, rcv) { + t.Errorf("Expected %+v %T \n but received \n %+v %T", rcv, rcv, exp, exp) + } + +} diff --git a/engine/libindex_test.go b/engine/libindex_test.go index 4cf791a55..9a43d97c8 100644 --- a/engine/libindex_test.go +++ b/engine/libindex_test.go @@ -425,3 +425,127 @@ func TestLibIndexModifyAttrPrfFilter(t *testing.T) { utils.ToJSON(expIndexes), utils.ToJSON(rcvIndexes)) } } + +// unfinished +// func TestUpdateFilterIndexssss(t *testing.T) { +// tmp := Cache +// defer func() { +// Cache = tmp +// }() +// Cache.Clear(nil) + +// cfg := config.NewDefaultCGRConfig() +// dataDB := NewInternalDB(nil, nil, cfg.DataDbCfg().Items) +// dm := NewDataManager(dataDB, cfg.CacheCfg(), nil) +// // to give error on dm.GetIndexes +// // dm.dataDB = &DataDBMock{ +// // GetIndexesDrvF: func(ctx *context.Context, idxItmType, tntCtx, idxKey, transactionID string) (indexes map[string]utils.StringSet, err error) { +// // return nil, utils.ErrNotImplemented +// // }, +// // } + +// dm.dataDB = &DataDBMock{ +// GetIndexesDrvF: func(ctx *context.Context, idxItmType, tntCtx, idxKey, transactionID string) (indexes map[string]utils.StringSet, err error) { +// return map[string]utils.StringSet{ +// utils.CacheThresholdFilterIndexes: { +// "ATTR_TEST": {}, +// }, +// }, nil +// }, +// } + +// Cache.Set(context.Background(), utils.CacheReverseFilterIndexes, utils.ConcatenatedKey("cgrates.org:fltr_test", utils.EmptyString), "someval", []string{"grId1"}, true, utils.NonTransactional) + +// oldFlt := &Filter{ +// Tenant: "cgrates.org", +// ID: "fltr_test", +// Rules: []*FilterRule{ +// { +// Type: utils.MetaExists, +// Element: "~*req.Cost", +// Values: []string{}, +// }, +// { +// Type: utils.MetaNotExists, +// Element: "~*req.Cost", +// Values: []string{}, +// }, +// { +// Type: utils.MetaExists, +// Element: "~*req.Cost", +// Values: []string{utils.DynamicDataPrefix + utils.MetaAccounts, +// utils.DynamicDataPrefix + utils.MetaStats, +// utils.DynamicDataPrefix + utils.MetaResources, +// utils.DynamicDataPrefix + utils.MetaLibPhoneNumber}, +// }, +// { +// Type: utils.MetaExists, +// Element: "~*req.Cost", +// Values: []string{utils.DynamicDataPrefix}, +// }, +// { +// Type: utils.MetaExists, +// Element: "~*req.Cost", +// Values: []string{"unRegVal"}, +// }, +// { +// Type: utils.MetaExists, +// Element: "*req.Cost", +// Values: []string{utils.DynamicDataPrefix}, +// }, +// { +// Type: utils.MetaExists, +// Element: "*req.Cost", +// Values: []string{"unRegVal"}, +// }, +// }, +// } +// newFlt := &Filter{ +// Tenant: "cgrates.org", +// ID: "fltr_test", +// Rules: []*FilterRule{ +// // { +// // Type: utils.MetaExists, +// // Element: "~*req.Cost", +// // Values: []string{}, +// // }, +// { +// Type: utils.MetaNotExists, +// Element: "~*req.Cost", +// Values: []string{}, +// }, +// { +// Type: utils.MetaExists, +// Element: "~*req.Cost", +// Values: []string{utils.DynamicDataPrefix + utils.MetaAccounts, +// utils.DynamicDataPrefix + utils.MetaStats, +// utils.DynamicDataPrefix + utils.MetaResources, +// utils.DynamicDataPrefix + utils.MetaLibPhoneNumber}, +// }, +// { +// Type: utils.MetaExists, +// Element: "~*req.Cost", +// Values: []string{utils.DynamicDataPrefix}, +// }, +// { +// Type: utils.MetaExists, +// Element: "~*req.Cost", +// Values: []string{"unRegVal"}, +// }, +// { +// Type: utils.MetaExists, +// Element: "*req.Cost", +// Values: []string{utils.DynamicDataPrefix}, +// }, +// { +// Type: utils.MetaExists, +// Element: "*req.Cost", +// Values: []string{"unRegVal"}, +// }, +// }, +// } +// if err := UpdateFilterIndex(context.Background(), dm, oldFlt, newFlt); err != nil { +// t.Error(err) +// } + +// }