From 64ae4f16d589174b3e32d5a39f6b82e013921d95 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Fri, 25 Sep 2020 12:46:05 +0300 Subject: [PATCH] Added *apiban precache integration test --- apier/v1/caches_it_test.go | 10 +++++++--- apier/v1/precache_it_test.go | 4 ++++ data/conf/samples/precache/tutmongo/cgrates.json | 8 +++++++- data/conf/samples/precache/tutmysql/cgrates.json | 8 +++++++- dispatchers/caches_it_test.go | 1 + engine/datamanager.go | 5 +++-- engine/libtest.go | 1 + 7 files changed, 30 insertions(+), 7 deletions(-) diff --git a/apier/v1/caches_it_test.go b/apier/v1/caches_it_test.go index c0b01210b..f3f72309f 100644 --- a/apier/v1/caches_it_test.go +++ b/apier/v1/caches_it_test.go @@ -246,7 +246,9 @@ func testCacheSReload(t *testing.T) { if err := chcRPC.Call(utils.CacheSv1GetCacheStats, &utils.AttrCacheIDsWithOpts{}, &rcvStats); err != nil { t.Error(err) - } else if !reflect.DeepEqual(expStats, rcvStats) { + } + rcvStats[utils.MetaAPIBan].Items = 0 + if !reflect.DeepEqual(expStats, rcvStats) { t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(expStats), utils.ToJSON(rcvStats)) } } @@ -442,8 +444,10 @@ func testCacheSPrecacheStatus(t *testing.T) { expected[k] = utils.MetaReady } if err := chcRPC.Call(utils.CacheSv1PrecacheStatus, &utils.AttrCacheIDsWithOpts{}, &reply); err != nil { - t.Error(err) - } else if !reflect.DeepEqual(expected, reply) { + t.Fatal(err) + } + reply[utils.MetaAPIBan] = utils.MetaReady // do not check the status for this partition + if !reflect.DeepEqual(expected, reply) { t.Errorf("Expected: %v , received:%v", utils.ToJSON(expected), utils.ToJSON(reply)) } } diff --git a/apier/v1/precache_it_test.go b/apier/v1/precache_it_test.go index 5a1923eeb..b968f018f 100644 --- a/apier/v1/precache_it_test.go +++ b/apier/v1/precache_it_test.go @@ -54,6 +54,8 @@ var ( } ) +// this tests may fail because of apiban limit( 5 requests per 2 minutes for an APIKey) +// if needed add more APIKeys func TestPrecacheIT(t *testing.T) { switch *dbType { case utils.MetaInternal: @@ -146,6 +148,7 @@ func testPrecacheRestartEngine(t *testing.T) { if err != nil { t.Fatal("Could not connect to rater: ", err.Error()) } + time.Sleep(2 * time.Second) // let the *apiban cache to be populated } func testPrecacheGetCacheStatsAfterRestart(t *testing.T) { @@ -302,6 +305,7 @@ func testPrecacheGetCacheStatsAfterRestart(t *testing.T) { utils.CacheTBLTPDispatchers: {}, utils.CacheTBLTPDispatcherHosts: {}, utils.CacheTBLTPRateProfiles: {}, + utils.MetaAPIBan: {Items: 254}, } if err := precacheRPC.Call(utils.CacheSv1GetCacheStats, args, &reply); err != nil { t.Error(err.Error()) diff --git a/data/conf/samples/precache/tutmongo/cgrates.json b/data/conf/samples/precache/tutmongo/cgrates.json index 5f922d092..556c500b0 100644 --- a/data/conf/samples/precache/tutmongo/cgrates.json +++ b/data/conf/samples/precache/tutmongo/cgrates.json @@ -57,7 +57,8 @@ "*charger_filter_indexes" : {"limit": 10000, "ttl":"0s", "precache": true}, "*dispatcher_profiles" : {"limit": 10000, "ttl":"0s", "precache": true}, "*dispatcher_hosts" : {"limit": 10000, "ttl":"0s", "precache": true}, - "*dispatcher_routes" : {"limit": 10000, "ttl":"0s", "precache": false} + "*dispatcher_routes" : {"limit": 10000, "ttl":"0s", "precache": false}, + "*apiban" : {"limit": 254, "ttl":"0s", "precache": true} }, }, @@ -89,4 +90,9 @@ }, +"apiban": { + "keys": ["4c8140a0d7ec506e03ac59283eea8a9a", "559bbfa6808558b3197bf4960bbc1635", "2efdcd5d3d1b3a3ab4d71636b7b6d847"], +}, + + } diff --git a/data/conf/samples/precache/tutmysql/cgrates.json b/data/conf/samples/precache/tutmysql/cgrates.json index 4373ab031..4f0007a55 100644 --- a/data/conf/samples/precache/tutmysql/cgrates.json +++ b/data/conf/samples/precache/tutmysql/cgrates.json @@ -53,7 +53,8 @@ "*charger_filter_indexes" : {"limit": 10000, "ttl":"0s", "precache": true}, "*dispatcher_profiles" : {"limit": 10000, "ttl":"0s", "precache": true}, "*dispatcher_hosts" : {"limit": 10000, "ttl":"0s", "precache": true}, - "*dispatcher_routes" : {"limit": 10000, "ttl":"0s", "precache": false} + "*dispatcher_routes" : {"limit": 10000, "ttl":"0s", "precache": false}, + "*apiban" : {"limit": 254, "ttl":"0s", "precache": true} }, }, @@ -80,4 +81,9 @@ }, +"apiban": { + "keys": ["4c8140a0d7ec506e03ac59283eea8a9a", "559bbfa6808558b3197bf4960bbc1635", "2efdcd5d3d1b3a3ab4d71636b7b6d847"], +}, + + } diff --git a/dispatchers/caches_it_test.go b/dispatchers/caches_it_test.go index 4ffd0dc8a..a284583df 100644 --- a/dispatchers/caches_it_test.go +++ b/dispatchers/caches_it_test.go @@ -240,6 +240,7 @@ func testDspChcPrecacheStatus(t *testing.T) { utils.CacheTBLTPDispatchers: utils.MetaReady, utils.CacheTBLTPDispatcherHosts: utils.MetaReady, utils.CacheTBLTPRateProfiles: utils.MetaReady, + utils.MetaAPIBan: utils.MetaReady, } if err := dispEngine.RPC.Call(utils.CacheSv1PrecacheStatus, utils.AttrCacheIDsWithOpts{ diff --git a/engine/datamanager.go b/engine/datamanager.go index 46de405b5..26cfdd244 100644 --- a/engine/datamanager.go +++ b/engine/datamanager.go @@ -135,8 +135,9 @@ func (dm *DataManager) CacheDataFromDB(prfx string, ids []string, mustBeCached b if dm.cacheCfg.Partitions[utils.CachePrefixToInstance[prfx]].Limit == 0 { return } - if ids == nil && - prfx != utils.MetaAPIBan { // no need for ids in this case + if prfx == utils.MetaAPIBan { // no need for ids in this case + ids = []string{utils.EmptyString} + } else if ids == nil { if mustBeCached { ids = Cache.GetItemIDs(utils.CachePrefixToInstance[prfx], utils.EmptyString) } else { diff --git a/engine/libtest.go b/engine/libtest.go index 500b19b22..f163e279a 100644 --- a/engine/libtest.go +++ b/engine/libtest.go @@ -520,6 +520,7 @@ func GetDefaultEmptyCacheStats() map[string]*ltcache.CacheStats { utils.CacheUCH: {}, utils.CacheEventCharges: {}, utils.CacheReverseFilterIndexes: {}, + utils.MetaAPIBan: {}, utils.CacheAccounts: {}, utils.CacheVersions: {},