From db3be46f790fbff3acfb38893267a88c4ce5a0ac Mon Sep 17 00:00:00 2001 From: Trial97 Date: Mon, 18 Mar 2019 11:28:21 +0200 Subject: [PATCH] Updated caches integration tests --- apier/v1/cache_it_test.go | 254 ---------------------- apier/v1/caches.go | 16 +- apier/v1/caches_it_test.go | 396 +++++++++++++++++++++++++++++++++++ cmd/cgr-loader/cgr-loader.go | 2 +- console/cache_reload.go | 2 +- engine/caches.go | 162 +------------- utils/consts.go | 5 +- 7 files changed, 413 insertions(+), 424 deletions(-) delete mode 100644 apier/v1/cache_it_test.go create mode 100644 apier/v1/caches_it_test.go diff --git a/apier/v1/cache_it_test.go b/apier/v1/cache_it_test.go deleted file mode 100644 index 3cf7fc81d..000000000 --- a/apier/v1/cache_it_test.go +++ /dev/null @@ -1,254 +0,0 @@ -// +build integration - -/* -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 v1 - -import ( - "net/rpc" - "net/rpc/jsonrpc" - "path" - "reflect" - "testing" - "time" - - "github.com/cgrates/cgrates/config" - "github.com/cgrates/cgrates/engine" - "github.com/cgrates/cgrates/utils" - "github.com/cgrates/ltcache" -) - -var ( - chcCfg *config.CGRConfig - chcRPC *rpc.Client - chcCfgPath string - chcCfgDir string -) - -var sTestsCacheSV1 = []func(t *testing.T){ - testCacheLoadConfig, - testCacheInitDataDb, - testCacheInitStorDb, - testCacheStartEngine, - testCacheRpcConn, - testCacheLoadTariffPlanFromFolder, - testCacheAfterLoadFromFolder, - testCacheFlush, - testCacheReload, - // testCacheReloadCache, - testCacheGetCacheKeys, - - testCacheStopEngine, -} - -// Test start here -func TestCacheSv1ITMySQL(t *testing.T) { - chcCfgDir = "tutmysql" - for _, stest := range sTestsCacheSV1 { - t.Run(chcCfgDir, stest) - } -} - -func TestCacheSv1ITMongo(t *testing.T) { - chcCfgDir = "tutmongo" - for _, stest := range sTestsCacheSV1 { - t.Run(chcCfgDir, stest) - } -} - -func testCacheLoadConfig(t *testing.T) { - var err error - chcCfgPath = path.Join(*dataDir, "conf", "samples", chcCfgDir) - if chcCfg, err = config.NewCGRConfigFromPath(chcCfgPath); err != nil { - t.Error(err) - } -} - -func testCacheInitDataDb(t *testing.T) { - if err := engine.InitDataDb(chcCfg); err != nil { - t.Fatal(err) - } -} - -// Empty tables before using them -func testCacheInitStorDb(t *testing.T) { - if err := engine.InitStorDb(chcCfg); err != nil { - t.Fatal(err) - } -} - -// Start engine -func testCacheStartEngine(t *testing.T) { - if _, err := engine.StopStartEngine(chcCfgPath, *waitRater); err != nil { - t.Fatal(err) - } -} - -func testCacheRpcConn(t *testing.T) { - var err error - chcRPC, err = jsonrpc.Dial("tcp", chcCfg.ListenCfg().RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed - if err != nil { - t.Fatal("Could not connect to RPC: ", err.Error()) - } -} - -func testCacheLoadTariffPlanFromFolder(t *testing.T) { - var reply string - attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "testtp")} - if err := chcRPC.Call("ApierV1.LoadTariffPlanFromFolder", attrs, &reply); err != nil { - t.Error(err) - } - time.Sleep(time.Duration(*waitRater) * time.Millisecond) -} - -func testCacheAfterLoadFromFolder(t *testing.T) { - var rcvStats map[string]*ltcache.CacheStats - expStats := engine.GetDefaultEmptyCacheStats() - expStats[utils.CacheAccountActionPlans].Items = 13 - expStats[utils.CacheActionPlans].Items = 7 - expStats[utils.CacheActions].Items = 6 - expStats[utils.CacheDestinations].Items = 3 - if err := chcRPC.Call("CacheSv1.GetCacheStats", nil, &rcvStats); err != nil { - t.Error("Got error on CacheSv1.GetCacheStats: ", err.Error()) - } else if !reflect.DeepEqual(expStats, rcvStats) { - t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(expStats), utils.ToJSON(rcvStats)) - } - reply := "" - // Simple test that command is executed without errors - if err := chcRPC.Call("CacheSv1.LoadCache", utils.AttrReloadCache{}, &reply); err != nil { - t.Error(err) - } else if reply != "OK" { - t.Error(reply) - } - expStats[utils.CacheActionTriggers].Items = 1 - expStats[utils.CacheActions].Items = 13 - expStats[utils.CacheAttributeProfiles].Items = 1 - expStats[utils.CacheFilters].Items = 15 - expStats[utils.CacheRatingPlans].Items = 5 - expStats[utils.CacheRatingProfiles].Items = 5 - expStats[utils.CacheResourceProfiles].Items = 3 - expStats[utils.CacheResources].Items = 3 - expStats[utils.CacheReverseDestinations].Items = 5 - expStats[utils.CacheStatQueueProfiles].Items = 1 - expStats[utils.CacheStatQueues].Items = 1 - expStats[utils.CacheSupplierProfiles].Items = 2 - expStats[utils.CacheThresholdProfiles].Items = 1 - expStats[utils.CacheThresholds].Items = 1 - - if err := chcRPC.Call("CacheSv1.GetCacheStats", nil, &rcvStats); err != nil { - t.Error(err) - } else if !reflect.DeepEqual(expStats, rcvStats) { - t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(expStats), utils.ToJSON(rcvStats)) - } -} - -func testCacheFlush(t *testing.T) { - reply := "" - if err := chcRPC.Call("CacheSv1.FlushCache", utils.AttrReloadCache{FlushAll: true}, &reply); err != nil { - t.Error("Got error on CacheSv1.ReloadCache: ", err.Error()) - } else if reply != utils.OK { - t.Error("Calling CacheSv1.ReloadCache got reply: ", reply) - } - var rcvStats map[string]*ltcache.CacheStats - expStats := engine.GetDefaultEmptyCacheStats() - if err := chcRPC.Call("CacheSv1.GetCacheStats", nil, &rcvStats); err != nil { - t.Error("Got error on CacheSv1.GetCacheStats: ", err.Error()) - } else if !reflect.DeepEqual(expStats, rcvStats) { - t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(expStats), utils.ToJSON(rcvStats)) - } -} - -func testCacheReloadCache(t *testing.T) { - reply := "" - arc := new(utils.AttrReloadCache) - if err := chcRPC.Call("CacheSv1.ReloadCache", arc, &reply); err != nil { - t.Error("Got error on CacheSv1.ReloadCache: ", err.Error()) - } else if reply != "OK" { - t.Error("Calling CacheSv1.ReloadCache got reply: ", reply) - } - var rcvStats map[string]*ltcache.CacheStats - expectedStats := engine.GetDefaultEmptyCacheStats() - - if err := chcRPC.Call("CacheSv1.GetCacheStats", nil, &rcvStats); err != nil { - t.Error("Got error on CacheSv1.GetCacheStats: ", err.Error()) - } else if !reflect.DeepEqual(expectedStats, rcvStats) { - t.Errorf("Calling CacheSv1.GetCacheStats expected: %+v, received: %+v", utils.ToJSON(expectedStats), utils.ToJSON(rcvStats)) - } -} - -func testCacheReload(t *testing.T) { - var rcvStats map[string]*ltcache.CacheStats - expStats := engine.GetDefaultEmptyCacheStats() - reply := "" - // Simple test that command is executed without errors - if err := chcRPC.Call("CacheSv1.LoadCache", utils.AttrReloadCache{}, &reply); err != nil { - t.Error(err) - } else if reply != "OK" { - t.Error(reply) - } - expStats[utils.CacheAccountActionPlans].Items = 13 - expStats[utils.CacheActionPlans].Items = 7 - expStats[utils.CacheActions].Items = 6 - expStats[utils.CacheDestinations].Items = 3 - expStats[utils.CacheActionTriggers].Items = 1 - expStats[utils.CacheActions].Items = 13 - expStats[utils.CacheAttributeProfiles].Items = 1 - expStats[utils.CacheFilters].Items = 15 - expStats[utils.CacheRatingPlans].Items = 5 - expStats[utils.CacheRatingProfiles].Items = 5 - expStats[utils.CacheResourceProfiles].Items = 3 - expStats[utils.CacheResources].Items = 3 - expStats[utils.CacheReverseDestinations].Items = 5 - expStats[utils.CacheStatQueueProfiles].Items = 1 - expStats[utils.CacheStatQueues].Items = 1 - expStats[utils.CacheSupplierProfiles].Items = 2 - expStats[utils.CacheThresholdProfiles].Items = 1 - expStats[utils.CacheThresholds].Items = 1 - - if err := chcRPC.Call("CacheSv1.GetCacheStats", nil, &rcvStats); err != nil { - t.Error(err) - } else if !reflect.DeepEqual(expStats, rcvStats) { - t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(expStats), utils.ToJSON(rcvStats)) - } -} - -func testCacheGetCacheKeys(t *testing.T) { - - expKeys := utils.ArgsCache{ - ThresholdProfileIDs: &[]string{"cgrates.org:Threshold1"}, - } - var rcvKeys utils.ArgsCache - argsAPI := utils.ArgsCacheKeys{ - ArgsCache: utils.ArgsCache{ - ThresholdProfileIDs: &[]string{}, - ResourceIDs: &[]string{"NotExistent"}, - }, - } - if err := chcRPC.Call("CacheSv1.GetCacheKeys", argsAPI, &rcvKeys); err != nil { - t.Fatalf("Got error on ApierV1.GetCacheStats: %s ", err.Error()) - } - if !reflect.DeepEqual(*expKeys.ThresholdProfileIDs, *rcvKeys.ThresholdProfileIDs) { - t.Errorf("Expected: %+v, received: %+v", expKeys.ThresholdProfileIDs, rcvKeys.ThresholdProfileIDs) - } -} - -func testCacheStopEngine(t *testing.T) { - if err := engine.KillEngine(100); err != nil { - t.Error(err) - } -} diff --git a/apier/v1/caches.go b/apier/v1/caches.go index ff6f76098..d8184862d 100644 --- a/apier/v1/caches.go +++ b/apier/v1/caches.go @@ -65,6 +65,11 @@ func (chSv1 *CacheSv1) Clear(cacheIDs []string, return chSv1.cacheS.V1Clear(cacheIDs, reply) } +// FlushCache wipes out cache for a prefix or completely +func (chSv1 *CacheSv1) FlushCache(args utils.AttrReloadCache, reply *string) (err error) { + return chSv1.cacheS.V1FlushCache(args, reply) +} + // GetCacheStats returns CacheStats filtered by cacheIDs func (chSv1 *CacheSv1) GetCacheStats(cacheIDs []string, rply *map[string]*ltcache.CacheStats) error { @@ -104,17 +109,6 @@ func (chSv1 *CacheSv1) LoadCache(args utils.AttrReloadCache, reply *string) (err return chSv1.cacheS.V1LoadCache(args, reply) } -// FlushCache wipes out cache for a prefix or completely -func (chSv1 *CacheSv1) FlushCache(args utils.AttrReloadCache, reply *string) (err error) { - return chSv1.cacheS.V1FlushCache(args, reply) -} - -// GetCacheKeys returns a list of keys available in cache based on query arguments -// If keys are provided in arguments, they will be checked for existence -func (chSv1 *CacheSv1) GetCacheKeys(args utils.ArgsCacheKeys, reply *utils.ArgsCache) (err error) { - return chSv1.cacheS.V1GetCacheKeys(args, reply) -} - // Ping used to detreminate if component is active func (chSv1 *CacheSv1) Ping(ign *utils.CGREvent, reply *string) error { *reply = utils.Pong diff --git a/apier/v1/caches_it_test.go b/apier/v1/caches_it_test.go new file mode 100644 index 000000000..13e123f40 --- /dev/null +++ b/apier/v1/caches_it_test.go @@ -0,0 +1,396 @@ +// +build integration + +/* +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 v1 + +import ( + "net/rpc" + "net/rpc/jsonrpc" + "path" + "reflect" + "testing" + "time" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" + "github.com/cgrates/ltcache" +) + +var ( + chcCfg *config.CGRConfig + chcRPC *rpc.Client + chcCfgPath string + chcCfgDir string +) + +var sTestsCacheSV1 = []func(t *testing.T){ + testCacheSLoadConfig, + testCacheSInitDataDb, + testCacheSInitStorDb, + testCacheSStartEngine, + testCacheSRpcConn, + testCacheSLoadTariffPlanFromFolder, + testCacheSAfterLoadFromFolder, + testCacheSFlush, + testCacheSReload, + testCacheSGetItemIDs, + testCacheSHasItem, + testCacheSGetItemExpiryTime, + testCacheSReloadCache, + testCacheSRemoveItem, + testCacheSClear, + testCacheSReload, + testCacheSPrecacheStatus, + testCacheSPing, + testCacheSStopEngine, +} + +// Test start here +func TestCacheSv1ITMySQL(t *testing.T) { + chcCfgDir = "tutmysql" + for _, stest := range sTestsCacheSV1 { + t.Run(chcCfgDir, stest) + } +} + +func TestCacheSv1ITMongo(t *testing.T) { + chcCfgDir = "tutmongo" + for _, stest := range sTestsCacheSV1 { + t.Run(chcCfgDir, stest) + } +} + +func testCacheSLoadConfig(t *testing.T) { + var err error + chcCfgPath = path.Join(*dataDir, "conf", "samples", "precache", chcCfgDir) + if chcCfg, err = config.NewCGRConfigFromPath(chcCfgPath); err != nil { + t.Error(err) + } +} + +func testCacheSInitDataDb(t *testing.T) { + if err := engine.InitDataDb(chcCfg); err != nil { + t.Fatal(err) + } +} + +// Empty tables before using them +func testCacheSInitStorDb(t *testing.T) { + if err := engine.InitStorDb(chcCfg); err != nil { + t.Fatal(err) + } +} + +// Start engine +func testCacheSStartEngine(t *testing.T) { + if _, err := engine.StopStartEngine(chcCfgPath, *waitRater); err != nil { + t.Fatal(err) + } +} + +func testCacheSRpcConn(t *testing.T) { + var err error + chcRPC, err = jsonrpc.Dial("tcp", chcCfg.ListenCfg().RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed + if err != nil { + t.Fatal("Could not connect to RPC: ", err.Error()) + } +} + +func testCacheSLoadTariffPlanFromFolder(t *testing.T) { + var reply string + attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "testtp")} + if err := chcRPC.Call(utils.ApierV1LoadTariffPlanFromFolder, attrs, &reply); err != nil { + t.Error(err) + } + time.Sleep(time.Duration(*waitRater) * time.Millisecond) +} + +func testCacheSAfterLoadFromFolder(t *testing.T) { + var rcvStats map[string]*ltcache.CacheStats + expStats := engine.GetDefaultEmptyCacheStats() + expStats[utils.CacheAccountActionPlans].Items = 13 + expStats[utils.CacheActionPlans].Items = 7 + expStats[utils.CacheActions].Items = 6 + expStats[utils.CacheDestinations].Items = 3 + if err := chcRPC.Call(utils.CacheSv1GetCacheStats, nil, &rcvStats); err != nil { + t.Error("Got error on CacheSv1.GetCacheStats: ", err.Error()) + } else if !reflect.DeepEqual(expStats, rcvStats) { + t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(expStats), utils.ToJSON(rcvStats)) + } + reply := "" + // Simple test that command is executed without errors + if err := chcRPC.Call(utils.CacheSv1LoadCache, utils.AttrReloadCache{}, &reply); err != nil { + t.Error(err) + } else if reply != "OK" { + t.Error(reply) + } + expStats[utils.CacheActionTriggers].Items = 1 + expStats[utils.CacheActions].Items = 13 + expStats[utils.CacheAttributeProfiles].Items = 1 + expStats[utils.CacheFilters].Items = 15 + expStats[utils.CacheRatingPlans].Items = 5 + expStats[utils.CacheRatingProfiles].Items = 5 + expStats[utils.CacheResourceProfiles].Items = 3 + expStats[utils.CacheResources].Items = 3 + expStats[utils.CacheReverseDestinations].Items = 5 + expStats[utils.CacheStatQueueProfiles].Items = 1 + expStats[utils.CacheStatQueues].Items = 1 + expStats[utils.CacheSupplierProfiles].Items = 2 + expStats[utils.CacheThresholdProfiles].Items = 1 + expStats[utils.CacheThresholds].Items = 1 + + if err := chcRPC.Call(utils.CacheSv1GetCacheStats, nil, &rcvStats); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expStats, rcvStats) { + t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(expStats), utils.ToJSON(rcvStats)) + } +} + +func testCacheSFlush(t *testing.T) { + reply := "" + if err := chcRPC.Call(utils.CacheSv1FlushCache, utils.AttrReloadCache{FlushAll: true}, &reply); err != nil { + t.Error("Got error on CacheSv1.ReloadCache: ", err.Error()) + } else if reply != utils.OK { + t.Error("Calling CacheSv1.ReloadCache got reply: ", reply) + } + var rcvStats map[string]*ltcache.CacheStats + expStats := engine.GetDefaultEmptyCacheStats() + if err := chcRPC.Call(utils.CacheSv1GetCacheStats, nil, &rcvStats); err != nil { + t.Error("Got error on CacheSv1.GetCacheStats: ", err.Error()) + } else if !reflect.DeepEqual(expStats, rcvStats) { + t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(expStats), utils.ToJSON(rcvStats)) + } +} + +func testCacheSReload(t *testing.T) { + var rcvStats map[string]*ltcache.CacheStats + expStats := engine.GetDefaultEmptyCacheStats() + reply := "" + // Simple test that command is executed without errors + if err := chcRPC.Call(utils.CacheSv1LoadCache, utils.AttrReloadCache{}, &reply); err != nil { + t.Error(err) + } else if reply != "OK" { + t.Error(reply) + } + expStats[utils.CacheAccountActionPlans].Items = 13 + expStats[utils.CacheActionPlans].Items = 7 + expStats[utils.CacheActions].Items = 6 + expStats[utils.CacheDestinations].Items = 3 + expStats[utils.CacheActionTriggers].Items = 1 + expStats[utils.CacheActions].Items = 13 + expStats[utils.CacheAttributeProfiles].Items = 1 + expStats[utils.CacheFilters].Items = 15 + expStats[utils.CacheRatingPlans].Items = 5 + expStats[utils.CacheRatingProfiles].Items = 5 + expStats[utils.CacheResourceProfiles].Items = 3 + expStats[utils.CacheResources].Items = 3 + expStats[utils.CacheReverseDestinations].Items = 5 + expStats[utils.CacheStatQueueProfiles].Items = 1 + expStats[utils.CacheStatQueues].Items = 1 + expStats[utils.CacheSupplierProfiles].Items = 2 + expStats[utils.CacheThresholdProfiles].Items = 1 + expStats[utils.CacheThresholds].Items = 1 + + if err := chcRPC.Call(utils.CacheSv1GetCacheStats, nil, &rcvStats); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expStats, rcvStats) { + t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(expStats), utils.ToJSON(rcvStats)) + } +} + +func testCacheSGetItemIDs(t *testing.T) { + var rcvKeys []string + var expKeys []string + argsAPI := engine.ArgsGetCacheItemIDs{ + CacheID: utils.CacheThresholdProfiles, + ItemIDPrefix: "NotExistent", + } + if err := chcRPC.Call(utils.CacheSv1GetItemIDs, argsAPI, &rcvKeys); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Fatalf("Expected error: %s received error: %s and reply: %v ", utils.ErrNotFound, err.Error(), rcvKeys) + } + + expKeys = []string{"cgrates.org:Threshold1"} + argsAPI = engine.ArgsGetCacheItemIDs{ + CacheID: utils.CacheThresholdProfiles, + } + if err := chcRPC.Call(utils.CacheSv1GetItemIDs, argsAPI, &rcvKeys); err != nil { + t.Fatalf("Got error on ApierV1.GetCacheStats: %s ", err.Error()) + } + if !reflect.DeepEqual(expKeys, rcvKeys) { + t.Errorf("Expected: %+v, received: %+v", expKeys, rcvKeys) + } +} + +func testCacheSHasItem(t *testing.T) { + var reply bool + var expected bool + argsAPI := engine.ArgsGetCacheItem{ + CacheID: utils.CacheThresholdProfiles, + ItemID: "NotExistent", + } + if err := chcRPC.Call(utils.CacheSv1HasItem, argsAPI, &reply); err != nil { + t.Error(err) + } else if reply { + t.Errorf("Expected: %v , received:%v", expected, reply) + } + + expected = true + argsAPI = engine.ArgsGetCacheItem{ + CacheID: utils.CacheThresholdProfiles, + ItemID: "cgrates.org:Threshold1", + } + if err := chcRPC.Call(utils.CacheSv1HasItem, argsAPI, &reply); err != nil { + t.Error(err) + } else if !reply { + t.Errorf("Expected: %v , received:%v", expected, reply) + } +} + +func testCacheSGetItemExpiryTime(t *testing.T) { + var reply time.Time + var expected time.Time + argsAPI := engine.ArgsGetCacheItem{ + CacheID: utils.CacheThresholdProfiles, + ItemID: "NotExistent", + } + if err := chcRPC.Call(utils.CacheSv1GetItemExpiryTime, argsAPI, &reply); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Fatalf("Expected error: %s received error: %s and reply: %v ", utils.ErrNotFound, err.Error(), reply) + } + + // expected = true + argsAPI = engine.ArgsGetCacheItem{ + CacheID: utils.CacheThresholdProfiles, + ItemID: "cgrates.org:Threshold1", + } + if err := chcRPC.Call(utils.CacheSv1GetItemExpiryTime, argsAPI, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, reply) { + t.Errorf("Expected: %v , received:%v", expected, reply) + } +} + +func testCacheSReloadCache(t *testing.T) { + reply := "" + arc := new(utils.AttrReloadCache) + if err := chcRPC.Call(utils.CacheSv1ReloadCache, arc, &reply); err != nil { + t.Error("Got error on CacheSv1.ReloadCache: ", err.Error()) + } else if reply != "OK" { + t.Error("Calling CacheSv1.ReloadCache got reply: ", reply) + } +} + +func testCacheSRemoveItem(t *testing.T) { + var reply bool + argsAPI := engine.ArgsGetCacheItem{ + CacheID: utils.CacheThresholdProfiles, + ItemID: "cgrates.org:Threshold1", + } + if err := chcRPC.Call(utils.CacheSv1HasItem, argsAPI, &reply); err != nil { + t.Error(err) + } else if !reply { + t.Errorf("Expected: %v , received:%v", true, reply) + } + var remReply string + if err := chcRPC.Call(utils.CacheSv1RemoveItem, argsAPI, &remReply); err != nil { + t.Error(err) + } else if remReply != utils.OK { + t.Errorf("Expected: %v , received:%v", utils.OK, remReply) + } + if err := chcRPC.Call(utils.CacheSv1HasItem, argsAPI, &reply); err != nil { + t.Error(err) + } else if reply { + t.Errorf("Expected: %v , received:%v", false, reply) + } +} + +func testCacheSClear(t *testing.T) { + reply := "" + if err := chcRPC.Call(utils.CacheSv1Clear, nil, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Error("Calling CacheSv1.ReloadCache got reply: ", reply) + } + var rcvStats map[string]*ltcache.CacheStats + expStats := engine.GetDefaultEmptyCacheStats() + if err := chcRPC.Call(utils.CacheSv1GetCacheStats, nil, &rcvStats); err != nil { + t.Error("Got error on CacheSv1.GetCacheStats: ", err.Error()) + } else if !reflect.DeepEqual(expStats, rcvStats) { + t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(expStats), utils.ToJSON(rcvStats)) + } +} + +func testCacheSPrecacheStatus(t *testing.T) { + var reply map[string]string + expected := map[string]string{ + utils.CacheDestinations: utils.MetaReady, + utils.CacheReverseDestinations: utils.MetaReady, + utils.CacheRatingPlans: utils.MetaReady, + utils.CacheRatingProfiles: utils.MetaReady, + utils.CacheActions: utils.MetaReady, + utils.CacheActionPlans: utils.MetaReady, + utils.CacheAccountActionPlans: utils.MetaReady, + utils.CacheActionTriggers: utils.MetaReady, + utils.CacheSharedGroups: utils.MetaReady, + utils.CacheResourceProfiles: utils.MetaReady, + utils.CacheResources: utils.MetaReady, + utils.CacheEventResources: utils.MetaReady, + utils.CacheTimings: utils.MetaReady, + utils.CacheStatQueueProfiles: utils.MetaReady, + utils.CacheStatQueues: utils.MetaReady, + utils.CacheThresholdProfiles: utils.MetaReady, + utils.CacheThresholds: utils.MetaReady, + utils.CacheFilters: utils.MetaReady, + utils.CacheSupplierProfiles: utils.MetaReady, + utils.CacheAttributeProfiles: utils.MetaReady, + utils.CacheChargerProfiles: utils.MetaReady, + utils.CacheDispatcherProfiles: utils.MetaReady, + utils.CacheDiameterMessages: utils.MetaReady, + utils.CacheAttributeFilterIndexes: utils.MetaReady, + utils.CacheResourceFilterIndexes: utils.MetaReady, + utils.CacheStatFilterIndexes: utils.MetaReady, + utils.CacheThresholdFilterIndexes: utils.MetaReady, + utils.CacheSupplierFilterIndexes: utils.MetaReady, + utils.CacheChargerFilterIndexes: utils.MetaReady, + utils.CacheDispatcherFilterIndexes: utils.MetaReady, + } + + if err := chcRPC.Call(utils.CacheSv1PrecacheStatus, nil, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, reply) { + t.Errorf("Expected: %v , received:%v", utils.ToJSON(expected), utils.ToJSON(reply)) + } +} + +func testCacheSPing(t *testing.T) { + var reply string + expected := utils.Pong + if err := chcRPC.Call(utils.CacheSv1Ping, nil, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, reply) { + t.Errorf("Expected: %v , received:%v", utils.ToJSON(expected), utils.ToJSON(reply)) + } + +} + +func testCacheSStopEngine(t *testing.T) { + if err := engine.KillEngine(100); err != nil { + t.Error(err) + } +} diff --git a/cmd/cgr-loader/cgr-loader.go b/cmd/cgr-loader/cgr-loader.go index cff67561d..d296efd8b 100755 --- a/cmd/cgr-loader/cgr-loader.go +++ b/cmd/cgr-loader/cgr-loader.go @@ -367,7 +367,7 @@ func main() { if *verbose { log.Print("Reloading cache") } - if err = cacheS.Call(utils.ApierV1ReloadCache, + if err = cacheS.Call(utils.CacheSv1ReloadCache, utils.AttrReloadCache{ArgsCache: utils.ArgsCache{ DestinationIDs: &dstIds, ReverseDestinationIDs: &revDstIDs, diff --git a/console/cache_reload.go b/console/cache_reload.go index eef353812..ed9fcf471 100644 --- a/console/cache_reload.go +++ b/console/cache_reload.go @@ -23,7 +23,7 @@ import "github.com/cgrates/cgrates/utils" func init() { c := &CmdReloadCache{ name: "cache_reload", - rpcMethod: "ApierV1.ReloadCache", + rpcMethod: utils.CacheSv1ReloadCache, } commands[c.Name()] = c c.CommandExecuter = &CommandExecuter{c} diff --git a/engine/caches.go b/engine/caches.go index 82362c5dd..ba0a42678 100644 --- a/engine/caches.go +++ b/engine/caches.go @@ -125,11 +125,11 @@ type ArgsGetCacheItemIDs struct { func (chS *CacheS) V1GetItemIDs(args *ArgsGetCacheItemIDs, reply *[]string) (err error) { - if itmIDs := Cache.GetItemIDs(args.CacheID, args.ItemIDPrefix); len(itmIDs) == 0 { + itmIDs := Cache.GetItemIDs(args.CacheID, args.ItemIDPrefix) + if len(itmIDs) == 0 { return utils.ErrNotFound - } else { - *reply = itmIDs } + *reply = itmIDs return } @@ -146,11 +146,11 @@ func (chS *CacheS) V1HasItem(args *ArgsGetCacheItem, func (chS *CacheS) V1GetItemExpiryTime(args *ArgsGetCacheItem, reply *time.Time) (err error) { - if expTime, has := Cache.GetItemExpiryTime(args.CacheID, args.ItemID); !has { + expTime, has := Cache.GetItemExpiryTime(args.CacheID, args.ItemID) + if !has { return utils.ErrNotFound - } else { - *reply = expTime } + *reply = expTime return } @@ -401,153 +401,3 @@ func (chS *CacheS) V1FlushCache(args utils.AttrReloadCache, reply *string) (err *reply = utils.OK return } - -func getCacheKeys(chID string, IDs *[]string, paginator utils.Paginator) (ids []string) { - if len(*IDs) != 0 { - for _, id := range *IDs { - if _, hasIt := Cache.Get(chID, id); hasIt { - ids = append(ids, id) - } - } - } else { - for _, id := range Cache.GetItemIDs(chID, "") { - ids = append(ids, id) - } - } - return paginator.PaginateStringSlice(ids) -} - -// GetCacheKeys returns a list of keys available in cache based on query arguments -// If keys are provided in arguments, they will be checked for existence -func (chS *CacheS) V1GetCacheKeys(args utils.ArgsCacheKeys, reply *utils.ArgsCache) (err error) { - if args.DestinationIDs != nil { - ids := getCacheKeys(utils.CacheDestinations, args.DestinationIDs, args.Paginator) - if len(ids) != 0 { - reply.DestinationIDs = &ids - } - } - if args.ReverseDestinationIDs != nil { - ids := getCacheKeys(utils.CacheReverseDestinations, args.ReverseDestinationIDs, args.Paginator) - if len(ids) != 0 { - reply.ReverseDestinationIDs = &ids - } - } - if args.RatingPlanIDs != nil { - ids := getCacheKeys(utils.CacheRatingPlans, args.RatingPlanIDs, args.Paginator) - if len(ids) != 0 { - reply.RatingPlanIDs = &ids - } - } - if args.RatingProfileIDs != nil { - ids := getCacheKeys(utils.CacheRatingProfiles, args.RatingProfileIDs, args.Paginator) - if len(ids) != 0 { - reply.RatingProfileIDs = &ids - } - } - if args.ActionIDs != nil { - ids := getCacheKeys(utils.CacheActions, args.ActionIDs, args.Paginator) - if len(ids) != 0 { - reply.ActionIDs = &ids - } - } - if args.ActionPlanIDs != nil { - ids := getCacheKeys(utils.CacheActionPlans, args.ActionPlanIDs, args.Paginator) - if len(ids) != 0 { - reply.ActionPlanIDs = &ids - } - } - if args.AccountActionPlanIDs != nil { - ids := getCacheKeys(utils.CacheAccountActionPlans, args.AccountActionPlanIDs, args.Paginator) - if len(ids) != 0 { - reply.AccountActionPlanIDs = &ids - } - } - if args.ActionTriggerIDs != nil { - ids := getCacheKeys(utils.CacheActionTriggers, args.ActionTriggerIDs, args.Paginator) - if len(ids) != 0 { - reply.ActionTriggerIDs = &ids - } - } - if args.SharedGroupIDs != nil { - ids := getCacheKeys(utils.CacheSharedGroups, args.SharedGroupIDs, args.Paginator) - if len(ids) != 0 { - reply.SharedGroupIDs = &ids - } - } - if args.ResourceProfileIDs != nil { - ids := getCacheKeys(utils.CacheResourceProfiles, args.ResourceProfileIDs, args.Paginator) - if len(ids) != 0 { - reply.ResourceProfileIDs = &ids - } - } - if args.ResourceIDs != nil { - ids := getCacheKeys(utils.CacheResources, args.ResourceIDs, args.Paginator) - if len(ids) != 0 { - reply.ResourceIDs = &ids - } - } - if args.StatsQueueIDs != nil { - ids := getCacheKeys(utils.CacheStatQueues, args.StatsQueueIDs, args.Paginator) - if len(ids) != 0 { - reply.StatsQueueIDs = &ids - } - } - if args.StatsQueueProfileIDs != nil { - ids := getCacheKeys(utils.CacheStatQueueProfiles, args.StatsQueueProfileIDs, args.Paginator) - if len(ids) != 0 { - reply.StatsQueueProfileIDs = &ids - } - } - - if args.ThresholdIDs != nil { - ids := getCacheKeys(utils.CacheThresholds, args.ThresholdIDs, args.Paginator) - if len(ids) != 0 { - reply.ThresholdIDs = &ids - } - } - - if args.ThresholdProfileIDs != nil { - ids := getCacheKeys(utils.CacheThresholdProfiles, args.ThresholdProfileIDs, args.Paginator) - if len(ids) != 0 { - reply.ThresholdProfileIDs = &ids - } - } - if args.FilterIDs != nil { - ids := getCacheKeys(utils.CacheFilters, args.FilterIDs, args.Paginator) - if len(ids) != 0 { - reply.FilterIDs = &ids - } - } - if args.SupplierProfileIDs != nil { - ids := getCacheKeys(utils.CacheSupplierProfiles, args.SupplierProfileIDs, args.Paginator) - if len(ids) != 0 { - reply.SupplierProfileIDs = &ids - } - } - if args.AttributeProfileIDs != nil { - ids := getCacheKeys(utils.CacheAttributeProfiles, args.AttributeProfileIDs, args.Paginator) - if len(ids) != 0 { - reply.AttributeProfileIDs = &ids - } - } - if args.ChargerProfileIDs != nil { - ids := getCacheKeys(utils.CacheChargerProfiles, args.ChargerProfileIDs, args.Paginator) - if len(ids) != 0 { - reply.ChargerProfileIDs = &ids - } - } - if args.DispatcherProfileIDs != nil { - ids := getCacheKeys(utils.CacheDispatcherProfiles, args.DispatcherProfileIDs, args.Paginator) - if len(ids) != 0 { - reply.DispatcherProfileIDs = &ids - } - } - if args.DispatcherRoutesIDs != nil { - ids := getCacheKeys(utils.CacheDispatcherRoutes, args.DispatcherRoutesIDs, args.Paginator) - if len(ids) != 0 { - reply.DispatcherRoutesIDs = &ids - } - } - - return -} diff --git a/utils/consts.go b/utils/consts.go index 716887d71..5466b5726 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -688,7 +688,6 @@ const ( // ApierV1 APIs const ( ApierV1ComputeFilterIndexes = "ApierV1.ComputeFilterIndexes" - ApierV1ReloadCache = "ApierV1.ReloadCache" ApierV1ReloadScheduler = "ApierV1.ReloadScheduler" ApierV1Ping = "ApierV1.Ping" ApierV1SetDispatcherProfile = "ApierV1.SetDispatcherProfile" @@ -822,6 +821,10 @@ const ( CacheSv1GetGroupItemIDs = "CacheSv1.GetGroupItemIDs" CacheSv1RemoveGroup = "CacheSv1.RemoveGroup" CacheSv1Clear = "CacheSv1.Clear" + CacheSv1ReloadCache = "CacheSv1.ReloadCache" + CacheSv1LoadCache = "CacheSv1.LoadCache" + CacheSv1FlushCache = "CacheSv1.FlushCache" + CacheSv1Ping = "CacheSv1.Ping" ) // Cdrs APIs