From 894cca43c9b9de70cc8d0850c3d402b0b538d8f6 Mon Sep 17 00:00:00 2001 From: gezimbll Date: Fri, 25 Nov 2022 10:03:26 -0500 Subject: [PATCH] improving coverage test at engine --- engine/caches_test.go | 257 ++++++++++++++++++++++++++++++++++++++- engine/dynamicdp_test.go | 51 ++++++++ engine/libroutes_test.go | 9 ++ engine/tpreader_test.go | 24 +++- 4 files changed, 337 insertions(+), 4 deletions(-) create mode 100644 engine/dynamicdp_test.go diff --git a/engine/caches_test.go b/engine/caches_test.go index d2dfede90..43c7f3723 100644 --- a/engine/caches_test.go +++ b/engine/caches_test.go @@ -345,7 +345,7 @@ func TestCacheSV1RemoveItem(t *testing.T) { dm: dm, tCache: tscache, } - reply := "error" + var reply string if err := chS.V1RemoveItem(args, &reply); err != nil { t.Error(err) } else if reply != utils.OK { @@ -564,3 +564,258 @@ func TestCacheSV1GetCacheStats(t *testing.T) { } } + +func TestCachesPrecache(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + cfg.CacheCfg().Partitions = map[string]*config.CacheParamCfg{ + utils.CacheDestinations: { + Limit: 1, + Precache: true, + TTL: time.Minute * 2, + Remote: true, + }, + } + pcI := map[string]chan struct{}{ + utils.CacheDestinations: make(chan struct{})} + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + chS := &CacheS{ + cfg: cfg, + dm: dm, + pcItems: pcI, + } + if err := chS.Precache(); err != nil { + t.Error(err) + } + +} + +func TestV1PrecacheStatus(t *testing.T) { + + args := &utils.AttrCacheIDsWithAPIOpts{ + APIOpts: map[string]interface{}{}, + Tenant: "cgrates.org", + CacheIDs: []string{"cache1", "cache2", "cache3"}, + } + cfg := config.NewDefaultCGRConfig() + cfg.CacheCfg().Partitions = map[string]*config.CacheParamCfg{ + utils.CacheDestinations: { + Limit: 1, + Precache: true, + TTL: time.Minute * 2, + Remote: true, + }, + } + pcI := map[string]chan struct{}{ + "cache1": make(chan struct{}), + "cache2": make(chan struct{}), + "cache3": make(chan struct{}), + } + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + chS := &CacheS{ + cfg: cfg, + dm: dm, + pcItems: pcI, + } + + reply := map[string]string{} + exp := map[string]string{ + "cache1": utils.MetaPrecaching, + "cache2": utils.MetaPrecaching, + "cache3": utils.MetaPrecaching, + } + if err := chS.V1PrecacheStatus(args, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(exp, reply) { + t.Errorf("expected %+v,received %+v", exp, reply) + } +} + +func TestCacheSV1HasGroup(t *testing.T) { + args := &utils.ArgsGetGroupWithAPIOpts{ + ArgsGetGroup: utils.ArgsGetGroup{ + CacheID: "cacheId", + GroupID: "groupId", + }, + APIOpts: map[string]interface{}{}, + } + cfg := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + tscache := ltcache.NewTransCache( + map[string]*ltcache.CacheConfig{ + "cacheId": { + MaxItems: 3, + TTL: time.Minute * 30, + StaticTTL: false, + OnEvicted: func(itmID string, value interface{}) { + }, + }}, + ) + tscache.Set("cacheId", "itemId", "value", []string{"groupId"}, true, "tId") + chS := &CacheS{ + cfg: cfg, + dm: dm, + tCache: tscache, + } + + var reply bool + if err := chS.V1HasGroup(args, &reply); err != nil { + t.Error(err) + } else if !reply { + t.Error("expected true,received false") + } + +} + +func TestCacheSV1HasGroupItemIDs(t *testing.T) { + args := &utils.ArgsGetGroupWithAPIOpts{ + ArgsGetGroup: utils.ArgsGetGroup{ + CacheID: "cacheId", + GroupID: "groupId", + }, + APIOpts: map[string]interface{}{}, + } + cfg := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + tscache := ltcache.NewTransCache( + map[string]*ltcache.CacheConfig{ + "cacheId": { + MaxItems: 3, + TTL: time.Minute * 30, + StaticTTL: false, + OnEvicted: func(itmID string, value interface{}) { + }, + }}, + ) + tscache.Set("cacheId", "itemId", "value", []string{"groupId"}, true, "tId") + chS := &CacheS{ + cfg: cfg, + dm: dm, + tCache: tscache, + } + var reply []string + exp := []string{"itemId"} + if err := chS.V1GetGroupItemIDs(args, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(exp, reply) { + t.Errorf("expected %+v,received %+v", exp, reply) + } + +} + +func TestV1RemoveGroup(t *testing.T) { + args := &utils.ArgsGetGroupWithAPIOpts{ + ArgsGetGroup: utils.ArgsGetGroup{ + CacheID: "cacheId", + GroupID: "groupId", + }, + APIOpts: map[string]interface{}{}, + } + cfg := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + tscache := ltcache.NewTransCache( + map[string]*ltcache.CacheConfig{ + "cacheId": { + MaxItems: 3, + TTL: time.Minute * 30, + StaticTTL: false, + OnEvicted: func(itmID string, value interface{}) { + }, + }}, + ) + tscache.Set("cacheId", "itemId", "value", []string{"groupId"}, true, "tId") + chS := &CacheS{ + cfg: cfg, + dm: dm, + tCache: tscache, + } + var reply string + + if err := chS.V1RemoveGroup(args, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("expected %v,received %v", utils.OK, reply) + } + + if has := tscache.HasGroup(args.CacheID, args.GroupID); has { + t.Errorf("expected false,received %+v", has) + } + +} + +func TestCacheSV1ReplicateRemove(t *testing.T) { + args := &utils.ArgCacheReplicateRemove{ + CacheID: "cacheID", + ItemID: "itemID", + APIOpts: map[string]interface{}{}, + Tenant: "cgrates.org", + } + cfg := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + tscache := ltcache.NewTransCache( + map[string]*ltcache.CacheConfig{ + "cacheId": { + MaxItems: 3, + TTL: time.Minute * 30, + StaticTTL: false, + OnEvicted: func(itmID string, value interface{}) { + }, + }}, + ) + tscache.Set(args.CacheID, args.ItemID, "value", []string{"groupId"}, true, "tId") + chS := &CacheS{ + cfg: cfg, + dm: dm, + tCache: tscache, + } + var reply string + + if err := chS.V1ReplicateRemove(args, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("expected %v,received %v", utils.OK, reply) + } + + if _, has := tscache.Get(args.CacheID, args.ItemID); has { + t.Errorf("expected false,received %+v", has) + } +} + +func TestNewCacheS(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + cfg.CacheCfg().ReplicationConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicationConnsCfg)} + cfg.CacheCfg().Partitions = map[string]*config.CacheParamCfg{ + "cacheID": { + Limit: 3, + TTL: 2 * time.Minute, + StaticTTL: true, + Replicate: true, + }, + } + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + clientconn := make(chan rpcclient.ClientConnector, 1) + clientconn <- &ccMock{ + calls: map[string]func(args interface{}, reply interface{}) error{ + utils.CacheSv1ReplicateRemove: func(args, reply interface{}) error { + + *reply.(*string) = "reply" + return nil + }, + }, + } + connMgr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicationConnsCfg): clientconn, + }) + expCacheS := &CacheS{} + SetConnManager(connMgr) + if c := NewCacheS(cfg, dm, &CapsStats{}); reflect.DeepEqual(expCacheS, c) { + t.Errorf("expected %+v,received %+v", utils.ToJSON(expCacheS), utils.ToJSON(c)) + } + +} diff --git a/engine/dynamicdp_test.go b/engine/dynamicdp_test.go new file mode 100644 index 000000000..3b58c7d55 --- /dev/null +++ b/engine/dynamicdp_test.go @@ -0,0 +1,51 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ +package engine + +import ( + "testing" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/utils" + "github.com/cgrates/rpcclient" +) + +func TestDynamicDpFieldAsInterface(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + ms := utils.MapStorage{} + dDp := newDynamicDP([]string{}, []string{utils.ConcatenatedKey(utils.MetaInternal, utils.StatSConnsCfg)}, []string{}, "cgrates.org", ms) + clientconn := make(chan rpcclient.ClientConnector, 1) + clientconn <- &ccMock{ + calls: map[string]func(args interface{}, reply interface{}) error{ + utils.StatSv1GetQueueFloatMetrics: func(args, reply interface{}) error { + rpl := &map[string]float64{ + "stat1": 31, + } + *reply.(*map[string]float64) = *rpl + return nil + }, + }, + } + connMgr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.StatSConnsCfg): clientconn, + }) + SetConnManager(connMgr) + if _, err := dDp.fieldAsInterface([]string{utils.MetaStats, "val", "val3"}); err == nil || err != utils.ErrNotFound { + t.Error(err) + } +} diff --git a/engine/libroutes_test.go b/engine/libroutes_test.go index 74fb3f4f8..da56e1a37 100644 --- a/engine/libroutes_test.go +++ b/engine/libroutes_test.go @@ -18,6 +18,7 @@ along with this program. If not, see package engine import ( + "fmt" "reflect" "sort" "strconv" @@ -1671,3 +1672,11 @@ func TestSortedRoutesListDigest(t *testing.T) { t.Errorf("received %v", val) } } + +func TestRouteSortDispatcher(t *testing.T) { + ssd := RouteSortDispatcher{} + strategy := "strategy" + if _, err := ssd.SortRoutes("prfID", strategy, map[string]*Route{}, &utils.CGREvent{}, &optsGetRoutes{}); err == nil || err.Error() != fmt.Sprintf("unsupported sorting strategy: %s", strategy) { + t.Error(err) + } +} diff --git a/engine/tpreader_test.go b/engine/tpreader_test.go index 55363e85d..76f646e41 100644 --- a/engine/tpreader_test.go +++ b/engine/tpreader_test.go @@ -30,6 +30,7 @@ import ( "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" + "github.com/cgrates/ltcache" "github.com/cgrates/rpcclient" ) @@ -1034,17 +1035,34 @@ func TestTPReaderReloadCache(t *testing.T) { func TestTPReaderLoadDestinationsFiltered(t *testing.T) { cfg := config.NewDefaultCGRConfig() db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) - tpr, err := NewTpReader(nil, db, "id", "local", nil, nil, false) + tscache := ltcache.NewTransCache( + map[string]*ltcache.CacheConfig{ + utils.CacheTBLTPDestinations: { + MaxItems: 3, + TTL: time.Minute * 30, + StaticTTL: false, + OnEvicted: func(itmID string, value interface{}) { + }, + }}, + ) + tscache.Set(utils.CacheTBLTPDestinations, "itemId", &utils.TPDestination{ + TPid: "tpID", + ID: "prefixes", + }, []string{"groupId"}, true, "tId") + db.db = tscache + tpr, err := NewTpReader(db, db, "itemId", "local", nil, nil, true) if err != nil { t.Error(err) } - if b, err := tpr.LoadDestinationsFiltered("tag"); (err == nil || err != utils.ErrNotFound) || b { - t.Errorf("expected %+v ,received %v", utils.ErrNotFound, err) + if b, err := tpr.LoadDestinationsFiltered(""); (err != nil) || !b { + t.Errorf("expected nil ,received %v", err) } + } func TestTPReaderLoadAll(t *testing.T) { cfg := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) tpr, err := NewTpReader(nil, db, "", "local", nil, nil, false) if err != nil {