From fc5966d11a88d8f0e45a73b9f71d931ebd0a7d4b Mon Sep 17 00:00:00 2001 From: Trial97 Date: Wed, 28 Oct 2020 17:01:13 +0200 Subject: [PATCH] Added integration tests for analyzers --- analyzers/analyzers_it_test.go | 223 +++++++++++++++++++++++ data/conf/samples/analyzers/cgrates.json | 132 ++++++++++++++ integration_test.sh | 3 + utils/consts.go | 5 +- 4 files changed, 361 insertions(+), 2 deletions(-) create mode 100644 analyzers/analyzers_it_test.go create mode 100644 data/conf/samples/analyzers/cgrates.json diff --git a/analyzers/analyzers_it_test.go b/analyzers/analyzers_it_test.go new file mode 100644 index 000000000..c03cc5187 --- /dev/null +++ b/analyzers/analyzers_it_test.go @@ -0,0 +1,223 @@ +// +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 analyzers + +import ( + "errors" + "flag" + "net/rpc" + "net/rpc/jsonrpc" + "os" + "path" + "reflect" + "sort" + "testing" + "time" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +var ( + anzCfgPath string + anzCfg *config.CGRConfig + anzRPC *rpc.Client + + sTestsAlsPrf = []func(t *testing.T){ + testAnalyzerSInitCfg, + testAnalyzerSInitDataDb, + testAnalyzerSResetStorDb, + testAnalyzerSStartEngine, + testAnalyzerSRPCConn, + testAnalyzerSLoadTarrifPlans, + testAnalyzerSChargerSv1ProcessEvent, + testAnalyzerSV1Search, + testAnalyzerSV1Search2, + testAnalyzerSKillEngine, + } +) + +var ( + dataDir = flag.String("data_dir", "/usr/share/cgrates", "CGR data dir path here") + waitRater = flag.Int("wait_rater", 500, "Number of miliseconds to wait for rater to start and cache") + encoding = flag.String("rpc", utils.MetaJSON, "what encoding whould be uused for rpc comunication") +) + +func newRPCClient(cfg *config.ListenCfg) (c *rpc.Client, err error) { + switch *encoding { + case utils.MetaJSON: + return jsonrpc.Dial(utils.TCP, cfg.RPCJSONListen) + case utils.MetaGOB: + return rpc.Dial(utils.TCP, cfg.RPCGOBListen) + default: + return nil, errors.New("UNSUPPORTED_RPC") + } +} + +//Test start here +func TestAnalyzerSIT(t *testing.T) { + + for _, stest := range sTestsAlsPrf { + t.Run("TestAnalyzerSIT", stest) + } +} + +func testAnalyzerSInitCfg(t *testing.T) { + var err error + anzCfgPath = path.Join(*dataDir, "conf", "samples", "analyzers") + anzCfg, err = config.NewCGRConfigFromPath(anzCfgPath) + if err != nil { + t.Error(err) + } + + if err := os.RemoveAll(anzCfg.AnalyzerSCfg().DBPath); err != nil { + t.Fatal(err) + } + if err = os.MkdirAll(path.Dir(anzCfg.AnalyzerSCfg().DBPath), 0700); err != nil { + t.Fatal(err) + } +} + +func testAnalyzerSInitDataDb(t *testing.T) { + if err := engine.InitDataDb(anzCfg); err != nil { + t.Fatal(err) + } +} + +// Wipe out the cdr database +func testAnalyzerSResetStorDb(t *testing.T) { + if err := engine.InitStorDb(anzCfg); err != nil { + t.Fatal(err) + } +} + +// Start CGR Engine +func testAnalyzerSStartEngine(t *testing.T) { + if _, err := engine.StopStartEngine(anzCfgPath, *waitRater); err != nil { + t.Fatal(err) + } +} + +// Connect rpc client to rater +func testAnalyzerSRPCConn(t *testing.T) { + var err error + anzRPC, err = newRPCClient(anzCfg.ListenCfg()) // We connect over JSON so we can also troubleshoot if needed + if err != nil { + t.Fatal(err) + } +} + +func testAnalyzerSLoadTarrifPlans(t *testing.T) { + var reply string + attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "tutorial")} + if err := anzRPC.Call(utils.APIerSv1LoadTariffPlanFromFolder, attrs, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Error("Unexpected reply returned", reply) + } + time.Sleep(500 * time.Millisecond) +} + +func testAnalyzerSChargerSv1ProcessEvent(t *testing.T) { + cgrEv := &utils.CGREventWithOpts{ + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "event1", + Event: map[string]interface{}{ + utils.Account: "1010", + utils.Subject: "Something_inter", + utils.Destination: "999", + }, + }, + } + var result2 []*engine.ChrgSProcessEventReply + + processedEv := []*engine.ChrgSProcessEventReply{ + { + ChargerSProfile: "DEFAULT", + AlteredFields: []string{"*req.RunID"}, + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "event1", + Event: map[string]interface{}{ + "Account": "1010", + "Destination": "999", + "RunID": "*default", + "Subject": "Something_inter", + }, + }, + Opts: map[string]interface{}{"*subsys": "*chargers"}, + }, + { + ChargerSProfile: "Raw", + AttributeSProfiles: []string{"*constant:*req.RequestType:*none"}, + AlteredFields: []string{"*req.RunID", "*req.RequestType"}, + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "event1", + Event: map[string]interface{}{ + "Account": "1010", + "Destination": "999", + "RequestType": "*none", + "RunID": "*raw", + "Subject": "Something_inter", + }, + }, + Opts: map[string]interface{}{"*subsys": "*chargers"}, + }, + } + + if err := anzRPC.Call(utils.ChargerSv1ProcessEvent, cgrEv, &result2); err != nil { + t.Fatal(err) + } + sort.Slice(result2, func(i, j int) bool { + return result2[i].ChargerSProfile < result2[j].ChargerSProfile + }) + if !reflect.DeepEqual(result2, processedEv) { + t.Errorf("Expecting : %s, \n received: %s", utils.ToJSON(processedEv), utils.ToJSON(result2)) + } + +} + +func testAnalyzerSV1Search(t *testing.T) { + var result []map[string]interface{} + if err := anzRPC.Call(utils.AnalyzerSv1Search, `+RequestEncoding:\*internal +RequestMethod:AttributeSv1\.ProcessEvent`, &result); err != nil { + t.Error(err) + } else if len(result) != 1 { + t.Errorf("Unexpected result: %s", utils.ToJSON(result)) + } +} + +func testAnalyzerSV1Search2(t *testing.T) { + var result []map[string]interface{} + if err := anzRPC.Call(utils.AnalyzerSv1Search, `+RequestEncoding:\*json +RequestMethod:ChargerSv1\.ProcessEvent`, &result); err != nil { + t.Error(err) + } else if len(result) != 1 { + t.Errorf("Unexpected result: %s", utils.ToJSON(result)) + } +} + +func testAnalyzerSKillEngine(t *testing.T) { + if err := engine.KillEngine(100); err != nil { + t.Error(err) + } +} diff --git a/data/conf/samples/analyzers/cgrates.json b/data/conf/samples/analyzers/cgrates.json new file mode 100644 index 000000000..ec84db230 --- /dev/null +++ b/data/conf/samples/analyzers/cgrates.json @@ -0,0 +1,132 @@ +{ +// CGRateS Configuration file + + +"general": { + "log_level": 7, + "reply_timeout": "30s", +}, + + +"listen": { + "rpc_json": ":2012", + "rpc_gob": ":2013", + "http": ":2080", +}, + + +"data_db": { + "db_type": "mongo", + "db_name": "10", + "db_port": 27017, +}, + + +"stor_db": { + "db_type": "mongo", + "db_name": "cgrates", + "db_port": 27017, +}, + + +"rals": { + "enabled": true, + "thresholds_conns": ["*internal"], + "max_increments":3000000, +}, + + +"schedulers": { + "enabled": true, + "cdrs_conns": ["*localhost"], +}, + + +"cdrs": { + "enabled": true, +}, + + +"chargers": { + "enabled": true, + "attributes_conns": ["*internal"], +}, + + +"resources": { + "enabled": true, + "store_interval": "1s", + "thresholds_conns": ["*internal"] +}, + + +"stats": { + "enabled": true, + "store_interval": "1s", + "thresholds_conns": ["*internal"], +}, + + +"thresholds": { + "enabled": true, + "store_interval": "1s", +}, + + +"routes": { + "enabled": true, + "stats_conns": ["*localhost"], + "resources_conns": ["*localhost"], + "rals_conns": ["*internal"], +}, + + +"attributes": { + "enabled": true, + "stats_conns": ["*localhost"], + "resources_conns": ["*localhost"], + "apiers_conns": ["*localhost"] +}, + + +"sessions": { + "enabled": true, + "rals_conns": ["*internal"], + "cdrs_conns": ["*internal"], + "chargers_conns": ["*internal"], +}, + + +"migrator": { + "out_datadb_type": "mongo", + "out_datadb_port": "27017", + "out_datadb_name": "10", + "out_stordb_type": "mongo", + "out_stordb_port": "27017", + "out_stordb_name": "cgrates", + "users_filters":["Account"], +}, + + +"apiers": { + "enabled": true, + "scheduler_conns": ["*internal"], +}, + + +"rates": { + "enabled": true +}, + + +"filters": { + "apiers_conns": ["*internal"], +}, + +"analyzers":{ // AnalyzerS config + "enabled": true, // starts AnalyzerS service: . + "db_path": "/tmp/analyzers", // path to the folder where to store the information +}, + + +} diff --git a/integration_test.sh b/integration_test.sh index 975d735c1..7ce269945 100755 --- a/integration_test.sh +++ b/integration_test.sh @@ -195,6 +195,9 @@ results+=($?) fi +echo "go test github.com/cgrates/cgrates/analyzers -tags=integration" +go test github.com/cgrates/cgrates/analyzers -tags=integration +results+=($?) echo "go test github.com/cgrates/cgrates/ees -tags=integration" go test github.com/cgrates/cgrates/ees -tags=integration results+=($?) diff --git a/utils/consts.go b/utils/consts.go index baa78383a..d5b61c3ae 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -1600,8 +1600,9 @@ const ( // AnalyzerS APIs const ( - AnalyzerSv1 = "AnalyzerSv1" - AnalyzerSv1Ping = "AnalyzerSv1.Ping" + AnalyzerSv1 = "AnalyzerSv1" + AnalyzerSv1Ping = "AnalyzerSv1.Ping" + AnalyzerSv1Search = "AnalyzerSv1.Search" ) // LoaderS APIs