From 8fd5a1339184b6968ed76c609b304ccbe2af9e7e Mon Sep 17 00:00:00 2001 From: Anevo Date: Thu, 22 Mar 2018 09:55:56 -0400 Subject: [PATCH] Precache integration tests --- apier/v1/precache_it_test.go | 459 +++++++++++++++++++++++++++++++++++ 1 file changed, 459 insertions(+) create mode 100644 apier/v1/precache_it_test.go diff --git a/apier/v1/precache_it_test.go b/apier/v1/precache_it_test.go new file mode 100644 index 000000000..83bbb1bc1 --- /dev/null +++ b/apier/v1/precache_it_test.go @@ -0,0 +1,459 @@ +// +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 ( + precacheCfgPath string + precacheCfg *config.CGRConfig + precacheRPC *rpc.Client + precacheDataDir = "/usr/share/cgrates" + precacheDelay int + precacheConfigDIR string //run tests for specific configuration +) + +var sTestsPrecache = []func(t *testing.T){ + testPrecacheInitCfg, + testPrecacheResetDataDB, + //testPrecacheStartEngine, + testPrecacheRpcConn, + // testPrecacheGetCacheStatsBeforeLoad, + testPrecacheFromFolder, + testPrecacheRestartEngine, + testPrecacheGetItemIDs, + testPrecacheGetCacheStatsAfterRestart, + testPrecacheKillEngine, +} + +func TestPrecacheITMySql(t *testing.T) { + precacheConfigDIR = "tutmysql" + for _, stest := range sTestsPrecache { + t.Run(precacheConfigDIR, stest) + } +} + +func TestPrecacheITMongo(t *testing.T) { + precacheConfigDIR = "tutmongo" + for _, stest := range sTestsPrecache { + t.Run(precacheConfigDIR, stest) + } +} + +func testPrecacheInitCfg(t *testing.T) { + var err error + precacheCfgPath = path.Join(precacheDataDir, "conf", "samples", precacheConfigDIR) + precacheCfg, err = config.NewCGRConfigFromFolder(precacheCfgPath) + if err != nil { + t.Error(err) + } + precacheCfg.DataFolderPath = precacheDataDir // Share DataFolderPath through config towards StoreDb for Flush() + config.SetCgrConfig(precacheCfg) + switch precacheConfigDIR { + case "tutmongo": // Mongo needs more time to reset db, need to investigate + precacheDelay = 2000 + default: + precacheDelay = 1000 + } +} + +func testPrecacheResetDataDB(t *testing.T) { + if err := engine.InitDataDb(precacheCfg); err != nil { + t.Fatal(err) + } +} + +func testPrecacheStartEngine(t *testing.T) { + if _, err := engine.StopStartEngine(precacheCfgPath, precacheDelay); err != nil { + t.Fatal(err) + } +} + +func testPrecacheRpcConn(t *testing.T) { + var err error + precacheRPC, err = jsonrpc.Dial("tcp", precacheCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed + if err != nil { + t.Fatal(err) + } +} + +func testPrecacheGetItemIDs(t *testing.T) { + args := &engine.ArgsGetCacheItemIDs{ + CacheID: "*default", + } + var reply *[]string + expected := []string{} + if err := precacheRPC.Call(utils.CacheSv1GetItemIDs, args, &reply); err != nil { + t.Error(err.Error()) + } else if !reflect.DeepEqual(reply, expected) { + t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(expected), utils.ToJSON(reply)) + } +} + +func testPrecacheGetCacheStatsBeforeLoad(t *testing.T) { + var reply *map[string]*ltcache.CacheStats + cacheIDs := []string{} + expectedStats := &map[string]*ltcache.CacheStats{ + "*default": <cache.CacheStats{ + Items: 1, + Groups: 0, + }, + "account_action_plans": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "action_plans": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "action_triggers": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "actions": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "aliases": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "attribute_filter_indexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "attribute_filter_revindexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "attribute_profiles": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "cdr_stats": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "derived_chargers": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "destinations": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "event_resources": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "filters": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "lcr_rules": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "rating_plans": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "rating_profiles": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "resource_filter_indexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "resource_filter_revindexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "resource_profiles": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "resources": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "reverse_aliases": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "reverse_destinations": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "shared_groups": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "stat_filter_indexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "stat_filter_revindexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "statqueue_profiles": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "statqueues": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "supplier_filter_indexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "supplier_filter_revindexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "supplier_profiles": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "threshold_filter_indexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "threshold_filter_revindexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "threshold_profiles": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "thresholds": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "timings": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + } + if err := precacheRPC.Call(utils.CacheSv1GetCacheStats, cacheIDs, &reply); err != nil { + t.Error(err.Error()) + } else if !reflect.DeepEqual(reply, expectedStats) { + t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(expectedStats), utils.ToJSON(reply)) + } +} + +func testPrecacheFromFolder(t *testing.T) { + var reply string + attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "tutorial")} + if err := precacheRPC.Call("ApierV1.LoadTariffPlanFromFolder", attrs, &reply); err != nil { + t.Error(err) + } +} + +func testPrecacheRestartEngine(t *testing.T) { + time.Sleep(2 * time.Second) + if _, err := engine.StopStartEngine(precacheCfgPath, precacheDelay); err != nil { + t.Fatal(err) + } + var err error + precacheRPC, err = jsonrpc.Dial("tcp", precacheCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed + if err != nil { + t.Fatal("Could not connect to rater: ", err.Error()) + } +} + +func testPrecacheGetCacheStatsAfterRestart(t *testing.T) { + var reply *map[string]*ltcache.CacheStats + cacheIDs := []string{} + expectedStats := &map[string]*ltcache.CacheStats{ + "*default": <cache.CacheStats{ + Items: 1, + Groups: 0, + }, + "account_action_plans": <cache.CacheStats{ + Items: 5, + Groups: 0, + }, + "action_plans": <cache.CacheStats{ + Items: 4, + Groups: 0, + }, + "action_triggers": <cache.CacheStats{ + Items: 4, // expected to have 4 items + Groups: 0, + }, + "actions": <cache.CacheStats{ + Items: 9, // expected to have 9 items + Groups: 0, + }, + "aliases": <cache.CacheStats{ + Items: 1, + Groups: 0, + }, + "attribute_filter_indexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "attribute_filter_revindexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "attribute_profiles": <cache.CacheStats{ + Items: 1, + Groups: 0, + }, + "cdr_stats": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "derived_chargers": <cache.CacheStats{ + Items: 1, // expected to have 1 item + Groups: 0, + }, + "destinations": <cache.CacheStats{ + Items: 5, + Groups: 0, + }, + "event_resources": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "filters": <cache.CacheStats{ + Items: 16, // expected to have 16 items + Groups: 0, + }, + "lcr_rules": <cache.CacheStats{ + Items: 5, // expected to have 5 items + Groups: 0, + }, + "rating_plans": <cache.CacheStats{ + Items: 4, // expected to have 4 items + Groups: 0, + }, + "rating_profiles": <cache.CacheStats{ + Items: 10, // expected to have 10 items + Groups: 0, + }, + "resource_filter_indexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "resource_filter_revindexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "resource_profiles": <cache.CacheStats{ + Items: 3, + Groups: 0, + }, + "resources": <cache.CacheStats{ + Items: 3, //expected to have 3 items + Groups: 0, + }, + "reverse_aliases": <cache.CacheStats{ + Items: 2, + Groups: 0, + }, + "reverse_destinations": <cache.CacheStats{ + Items: 7, + Groups: 0, + }, + "shared_groups": <cache.CacheStats{ + Items: 1, + Groups: 0, + }, + "stat_filter_indexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "stat_filter_revindexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "statqueue_profiles": <cache.CacheStats{ + Items: 1, + Groups: 0, + }, + "statqueues": <cache.CacheStats{ + Items: 1, // expected to have 1 item + Groups: 0, + }, + "supplier_filter_indexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "supplier_filter_revindexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "supplier_profiles": <cache.CacheStats{ + Items: 3, // expected to have 3 items + Groups: 0, + }, + "threshold_filter_indexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "threshold_filter_revindexes": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + "threshold_profiles": <cache.CacheStats{ + Items: 7, + Groups: 0, + }, + "thresholds": <cache.CacheStats{ + Items: 7, // expected to have 7 items + Groups: 0, + }, + "timings": <cache.CacheStats{ + Items: 0, + Groups: 0, + }, + } + if err := precacheRPC.Call(utils.CacheSv1GetCacheStats, cacheIDs, &reply); err != nil { + t.Error(err.Error()) + } else if !reflect.DeepEqual(reply, expectedStats) { + t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(expectedStats), utils.ToJSON(reply)) + } +} + +func testPrecacheKillEngine(t *testing.T) { + if err := engine.KillEngine(precacheDelay); err != nil { + t.Error(err) + } +}