From 141998f1648e7744128270933101c5fdf9f30ac3 Mon Sep 17 00:00:00 2001 From: porosnicuadrian Date: Thu, 29 Apr 2021 18:33:09 +0300 Subject: [PATCH] New test for finding indexes bug --- .../samples/mysql_attributes/cgrates.json | 49 +++++ general_tests/attributes_filters_id_test.go | 203 ++++++++++++++++++ 2 files changed, 252 insertions(+) create mode 100644 data/conf/samples/mysql_attributes/cgrates.json create mode 100644 general_tests/attributes_filters_id_test.go diff --git a/data/conf/samples/mysql_attributes/cgrates.json b/data/conf/samples/mysql_attributes/cgrates.json new file mode 100644 index 000000000..6f5dad892 --- /dev/null +++ b/data/conf/samples/mysql_attributes/cgrates.json @@ -0,0 +1,49 @@ +{ + // CGRateS Configuration file + // used in general_tests/attributes_filter_it_test.go + + "apiers": { + "enabled": true, + }, + + "attributes": { // AttributeS config + "enabled": true, + "string_indexed_fields": [], + "prefix_indexed_fields": ["*req.Subject"], + }, + + "cdrs": { + "enabled": true, + "chargers_conns": ["*localhost"], + "rals_conns": ["*localhost"], + "attributes_conns": ["*localhost"], + + }, + + "chargers": { // ChargerS config + "enabled": true, + }, + + "general": { + "node_id": "CGRAero", + "rounding_decimals": 10, + + }, + + "rals": { + "enabled": true, + }, + + "sessions": { + "enabled": true, + "chargers_conns": ["*localhost"], + "rals_conns": ["*localhost"], + "cdrs_conns": ["*localhost"], + "attributes_conns": ["*localhost"], + }, + + "stor_db": { + "db_password": "CGRateS.org" + }, + +} \ No newline at end of file diff --git a/general_tests/attributes_filters_id_test.go b/general_tests/attributes_filters_id_test.go new file mode 100644 index 000000000..eec0fd1f7 --- /dev/null +++ b/general_tests/attributes_filters_id_test.go @@ -0,0 +1,203 @@ +// +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 general_tests + +import ( + "net/rpc" + "path" + "testing" + + "github.com/cgrates/cgrates/engine" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/utils" +) + +var ( + attrFltrCfgPath string + attrFltrCfg *config.CGRConfig + attrFltrRPC *rpc.Client + alsPrfFltrConfigDIR string + sTestsAlsFltrPrf = []func(t *testing.T){ + testAttributeFltrSInitCfg, + testAttributeFltrSInitDataDb, + testAttributeFltrSResetStorDb, + testAttributeFltrSStartEngine, + testAttributeFltrSRPCConn, + testAttributeFltrSetAttrProfileAndFltr, + testAttributeFltrSStopEngine, + } +) + +func TestAttributeFilterSIT(t *testing.T) { + switch *dbType { + case utils.MetaMySQL: + alsPrfFltrConfigDIR = "mysql_attributes" + /* + case utils.MetaMongo: + alsPrfConfigDIR = "tutmongo" + + */ + case utils.MetaPostgres: + t.SkipNow() + default: + t.Fatal("Unknown Database type") + } + for _, stest := range sTestsAlsFltrPrf { + t.Run(alsPrfFltrConfigDIR, stest) + } +} + +func testAttributeFltrSInitCfg(t *testing.T) { + var err error + attrFltrCfgPath = path.Join(*dataDir, "conf", "samples", alsPrfFltrConfigDIR) + attrFltrCfg, err = config.NewCGRConfigFromPath(attrFltrCfgPath) + if err != nil { + t.Error(err) + } + attrFltrCfg.DataFolderPath = *dataDir // Share DataFolderPath through config towards StoreDb for Flush() +} + +func testAttributeFltrSInitDataDb(t *testing.T) { + if err := engine.InitDataDb(attrFltrCfg); err != nil { + t.Fatal(err) + } +} + +// Wipe out the cdr database +func testAttributeFltrSResetStorDb(t *testing.T) { + if err := engine.InitStorDb(attrFltrCfg); err != nil { + t.Fatal(err) + } +} + +// Start CGR Engine +func testAttributeFltrSStartEngine(t *testing.T) { + if _, err := engine.StopStartEngine(attrFltrCfgPath, *waitRater); err != nil { + t.Fatal(err) + } +} + +// Connect rpc client to rater +func testAttributeFltrSRPCConn(t *testing.T) { + var err error + attrFltrRPC, err = newRPCClient(attrFltrCfg.ListenCfg()) // We connect over JSON so we can also troubleshoot if needed + if err != nil { + t.Fatal(err) + } +} + +func testAttributeFltrSetAttrProfileAndFltr(t *testing.T) { + filter := &engine.FilterWithAPIOpts{ + Filter: &engine.Filter{ + Tenant: "cgrates.org", + ID: "FLTR_1", + Rules: []*engine.FilterRule{{ + Element: "~*req.Subject", + Type: "*prefix", + Values: []string{"48"}, + }}, + }, + } + var result string + if err := attrFltrRPC.Call(utils.APIerSv1SetFilter, filter, &result); err != nil { + t.Error(err) + } else if result != utils.OK { + t.Error("Unexpected reply returned", result) + } + + alsPrf := &engine.AttributeProfileWithAPIOpts{ + AttributeProfile: &engine.AttributeProfile{ + Tenant: "cgrates.org", + ID: "ApierTest", + Contexts: []string{utils.MetaSessionS}, + FilterIDs: []string{"FLTR_1"}, + Attributes: []*engine.Attribute{ + { + FilterIDs: []string{"*string:~*req.FL1:In1"}, + Path: "FL1", + Value: config.NewRSRParsersMustCompile("Al1", utils.InfieldSep), + }, + }, + Weight: 20, + }, + } + if err := attrFltrRPC.Call(utils.APIerSv1SetAttributeProfile, alsPrf, &result); err != nil { + t.Error(err) + } else if result != utils.OK { + t.Error("Unexpected reply returned", result) + } + + ev := &engine.AttrArgsProcessEvent{ + Context: utils.StringPointer(utils.MetaSessionS), + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + Event: map[string]interface{}{ + "Subject": "45", + }, + APIOpts: map[string]interface{}{}, + }, + } + var rplyEv engine.AttrSProcessEventReply + if err := attrFltrRPC.Call(utils.AttributeSv1ProcessEvent, + ev, &rplyEv); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Expected %+v, received %+v", utils.ErrNotFound, err) + } + + filter = &engine.FilterWithAPIOpts{ + Filter: &engine.Filter{ + Tenant: "cgrates.org", + ID: "FLTR_1", + Rules: []*engine.FilterRule{{ + Element: "~*req.Subject", + Type: "*prefix", + Values: []string{"44"}, + }}, + }, + } + if err := attrFltrRPC.Call(utils.APIerSv1SetFilter, filter, &result); err != nil { + t.Error(err) + } else if result != utils.OK { + t.Error("Unexpected reply returned", result) + } + + //same event for process + ev = &engine.AttrArgsProcessEvent{ + Context: utils.StringPointer(utils.MetaSessionS), + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + Event: map[string]interface{}{ + "Subject": "4444", + }, + APIOpts: map[string]interface{}{}, + }, + } + if err := attrFltrRPC.Call(utils.AttributeSv1ProcessEvent, + ev, &rplyEv); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Expected %+v, received %+v", utils.ErrNotFound, err) + } +} + +func testAttributeFltrSStopEngine(t *testing.T) { + if err := engine.KillEngine(accDelay); err != nil { + t.Error(err) + } +}