From 50efbf4451e689250e56150001f1358ee0258f00 Mon Sep 17 00:00:00 2001 From: andronache Date: Wed, 14 Jul 2021 17:14:03 +0300 Subject: [PATCH] Add tests with tenant change for sessions and cdrs --- general_tests/sessions_tnt_change_it_test.go | 320 +++++++++++++++++++ sessions/sessions.go | 2 +- 2 files changed, 321 insertions(+), 1 deletion(-) create mode 100644 general_tests/sessions_tnt_change_it_test.go diff --git a/general_tests/sessions_tnt_change_it_test.go b/general_tests/sessions_tnt_change_it_test.go new file mode 100644 index 000000000..48647649f --- /dev/null +++ b/general_tests/sessions_tnt_change_it_test.go @@ -0,0 +1,320 @@ +// +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" + "reflect" + "testing" + "time" + + v1 "github.com/cgrates/cgrates/apier/v1" + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/sessions" + "github.com/cgrates/cgrates/utils" +) + +var ( + sesTntChngCfgDir string + sesTntChngCfgPath string + sesTntChngCfg *config.CGRConfig + sesTntChngRPC *rpc.Client + + sesTntChngTests = []func(t *testing.T){ + testSesTntChngLoadConfig, + testSesTntChngResetDataDB, + testSesTntChngResetStorDb, + testSesTntChngStartEngine, + testSesTntChngRPCConn, + //testSesTntChngLoadFromFolder, + testSesTntChngSetChargerProfile1, + testSesTntChngSetChargerProfile2, + //testSesTntChngChargersForEvent, + //testSesTntChngChargersForEvent2, + testChargerSAuthProcessEventAuth, + testSesTntChngStopCgrEngine, + } +) + +func TestSesTntChange(t *testing.T) { + switch *dbType { + case utils.MetaInternal: + sesTntChngCfgDir = "tutinternal" + case utils.MetaMySQL: + sesTntChngCfgDir = "tutmysql" + case utils.MetaMongo: + sesTntChngCfgDir = "tutmongo" + case utils.MetaPostgres: + t.SkipNow() + default: + t.Fatal("Unknown Database type") + } + for _, sestest := range sesTntChngTests { + t.Run(sesTntChngCfgDir, sestest) + } +} + +func testSesTntChngLoadConfig(t *testing.T) { + sesTntChngCfgPath = path.Join(*dataDir, "conf", "samples", sesTntChngCfgDir) + if sesTntChngCfg, err = config.NewCGRConfigFromPath(sesTntChngCfgPath); err != nil { + t.Error(err) + } +} + +func testSesTntChngResetDataDB(t *testing.T) { + if err := engine.InitDataDb(sesTntChngCfg); err != nil { + t.Fatal(err) + } +} + +func testSesTntChngResetStorDb(t *testing.T) { + if err := engine.InitStorDb(sesTntChngCfg); err != nil { + t.Fatal(err) + } +} + +func testSesTntChngStartEngine(t *testing.T) { + if _, err := engine.StopStartEngine(sesTntChngCfgPath, *waitRater); err != nil { + t.Fatal(err) + } +} + +func testSesTntChngRPCConn(t *testing.T) { + var err error + sesTntChngRPC, err = newRPCClient(sesTntChngCfg.ListenCfg()) + if err != nil { + t.Fatal(err) + } +} + +func testSesTntChngLoadFromFolder(t *testing.T) { + var reply string + attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "tutorial")} + if err := sesTntChngRPC.Call(utils.APIerSv1LoadTariffPlanFromFolder, attrs, &reply); err != nil { + t.Error(err) + } + time.Sleep(100 * time.Millisecond) +} + +func testSesTntChngSetChargerProfile1(t *testing.T) { + var reply *engine.ChargerProfile + if err := sesTntChngRPC.Call(utils.APIerSv1GetChargerProfile, + &utils.TenantID{Tenant: "cgrates.org", ID: "Charger1"}, &reply); err == nil || + err.Error() != utils.ErrNotFound.Error() { + t.Fatal(err) + } + chargerProfile := &v1.ChargerWithAPIOpts{ + ChargerProfile: &engine.ChargerProfile{ + Tenant: "cgrates.org", + ID: "Charger1", + RunID: utils.MetaDefault, + AttributeIDs: []string{"*constant:*tenant:cgrates.ro;*constant:*req.Account:1234"}, + }, + } + + var result string + if err := sesTntChngRPC.Call(utils.APIerSv1SetChargerProfile, chargerProfile, &result); err != nil { + t.Error(err) + } else if result != utils.OK { + t.Error("Unexpected reply returned", result) + } + + if err := sesTntChngRPC.Call(utils.APIerSv1GetChargerProfile, + &utils.TenantID{Tenant: "cgrates.org", ID: "Charger1"}, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(chargerProfile.ChargerProfile, reply) { + t.Errorf("Expecting : %+v, received: %+v", chargerProfile.ChargerProfile, reply) + } +} + +func testSesTntChngSetChargerProfile2(t *testing.T) { + var reply *engine.ChargerProfile + if err := sesTntChngRPC.Call(utils.APIerSv1GetChargerProfile, + &utils.TenantID{Tenant: "cgrates.org", ID: "Charger2"}, &reply); err == nil || + err.Error() != utils.ErrNotFound.Error() { + t.Fatal(err) + } + chargerProfile := &v1.ChargerWithAPIOpts{ + ChargerProfile: &engine.ChargerProfile{ + Tenant: "cgrates.org", + ID: "Charger2", + RunID: utils.MetaRaw, + AttributeIDs: []string{}, + }, + } + + var result string + if err := sesTntChngRPC.Call(utils.APIerSv1SetChargerProfile, chargerProfile, &result); err != nil { + t.Error(err) + } else if result != utils.OK { + t.Error("Unexpected reply returned", result) + } + + if err := sesTntChngRPC.Call(utils.APIerSv1GetChargerProfile, + &utils.TenantID{Tenant: "cgrates.org", ID: "Charger2"}, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(chargerProfile.ChargerProfile, reply) { + t.Errorf("Expecting : %+v, received: %+v", chargerProfile.ChargerProfile, reply) + } +} + +func testSesTntChngChargersForEvent(t *testing.T) { + ev := &utils.CGREvent{ + Tenant: "cgrates.ro", + ID: "TestEv1", + Event: map[string]interface{}{ + utils.Tenant: "cgrates.ro", + utils.ToR: utils.MetaVoice, + utils.OriginID: "TestSSv1It1", + utils.RequestType: utils.MetaRated, + utils.AccountField: "1001", + utils.Subject: "1001", + utils.Destination: "1002", + }, + } + chargerProfiles := &engine.ChargerProfiles{ + &engine.ChargerProfile{ + Tenant: "cgrates.ro", + ID: "Charger1", + FilterIDs: []string{"*string:~*req.Account:1001", "*string:~*req.Tenant:cgrates.ro"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 29, 15, 0, 0, 0, time.UTC), + }, + RunID: utils.MetaDefault, + AttributeIDs: []string{"*constant:*tenant:cgrates.ro2"}, + Weight: 20, + }, + } + var result *engine.ChargerProfiles + if err := sesTntChngRPC.Call(utils.ChargerSv1GetChargersForEvent, + ev, &result); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(result, chargerProfiles) { + t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(chargerProfiles), utils.ToJSON(result)) + } +} + +func testSesTntChngChargersForEvent2(t *testing.T) { + ev := &utils.CGREvent{ + Tenant: "cgrates.ro2", + ID: "TestEv1", + Event: map[string]interface{}{ + utils.Tenant: "cgrates.ro2", + utils.ToR: utils.MetaVoice, + utils.OriginID: "TestSSv1It1", + utils.RequestType: utils.MetaRated, + utils.AccountField: "1001", + utils.Subject: "1001", + utils.Destination: "1002", + }, + } + chargerProfiles := &engine.ChargerProfiles{ + &engine.ChargerProfile{ + Tenant: "cgrates.ro2", + ID: "Charger2", + FilterIDs: []string{"*string:~*req.Tenant:cgrates.ro2"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 29, 15, 0, 0, 0, time.UTC), + }, + RunID: utils.MetaDefault, + AttributeIDs: []string{}, + Weight: 20, + }, + } + var result *engine.ChargerProfiles + if err := sesTntChngRPC.Call(utils.ChargerSv1GetChargersForEvent, + ev, &result); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(result, chargerProfiles) { + t.Errorf("Expecting : %+v, received: %+v", utils.ToJSON(chargerProfiles), utils.ToJSON(result)) + } +} + +func testChargerSAuthProcessEventAuth(t *testing.T) { + attrSetBalance := utils.AttrSetBalance{ + Tenant: "cgrates.org", + Account: "1001", + BalanceType: utils.MetaVoice, + Value: float64(2 * time.Minute), + Balance: map[string]interface{}{ + utils.ID: "testSes", + utils.RatingSubject: "*zero1ms", + }, + } + var reply string + if err := sesTntChngRPC.Call(utils.APIerSv2SetBalance, attrSetBalance, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("Received: %s", reply) + } + + attrSetBalance2 := utils.AttrSetBalance{ + Tenant: "cgrates.ro", + Account: "1234", + BalanceType: utils.MetaVoice, + Value: float64(2 * time.Minute), + Balance: map[string]interface{}{ + utils.ID: "testSes", + utils.RatingSubject: "*zero1ms", + }, + } + var reply2 string + if err := sesTntChngRPC.Call(utils.APIerSv2SetBalance, attrSetBalance2, &reply2); err != nil { + t.Error(err) + } else if reply2 != utils.OK { + t.Errorf("Received: %s", reply2) + } + + ev := &sessions.V1AuthorizeArgs{ + GetMaxUsage: true, + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "TestEv1", + Event: map[string]interface{}{ + utils.ToR: utils.MetaVoice, + utils.OriginID: "TestEv1", + utils.RequestType: utils.MetaPrepaid, + utils.AccountField: "1001", + utils.Subject: "1001", + utils.Destination: "1002", + utils.Usage: time.Minute, + }, + }, + } + var rply sessions.V1AuthorizeReply + if err := sesTntChngRPC.Call(utils.SessionSv1AuthorizeEvent, ev, &rply); err != nil { + t.Fatal(err) + } + expected := &sessions.V1AuthorizeReply{ + MaxUsage: (*time.Duration)(utils.Int64Pointer(60000000000)), + } + if !reflect.DeepEqual(utils.ToJSON(&expected), utils.ToJSON(&rply)) { + t.Errorf("Expecting : %T, received: %T", utils.ToJSON(&expected), utils.ToJSON(&rply)) + } +} + +func testSesTntChngStopCgrEngine(t *testing.T) { + if err := engine.KillEngine(100); err != nil { + t.Error(err) + } +} diff --git a/sessions/sessions.go b/sessions/sessions.go index 95782de2b..2627aac53 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -1198,7 +1198,7 @@ func (sS *SessionS) newSession(cgrEv *utils.CGREvent, resID, clntConnID string, CgrID: s.CGRID, RunID: me.GetStringIgnoreErrors(utils.RunID), ToR: me.GetStringIgnoreErrors(utils.ToR), - Tenant: s.Tenant, + Tenant: chrgr.CGREvent.Tenant, Category: category, Subject: subject, Account: me.GetStringIgnoreErrors(utils.AccountField),