From 0df2bcfc523e9c13f1a2433acb64a2746e0ed7bd Mon Sep 17 00:00:00 2001 From: porosnicuadrian Date: Thu, 3 Jun 2021 18:07:02 +0300 Subject: [PATCH] FIxed little constants + started test for loader remove flag --- cmd/cgr-loader/cgr-loader_it_test.go | 26 ++- cmd/cgr-loader/cgr-loader_remove_it_test.go | 207 ++++++++++++++++++++ utils/consts.go | 8 +- 3 files changed, 231 insertions(+), 10 deletions(-) create mode 100644 cmd/cgr-loader/cgr-loader_remove_it_test.go diff --git a/cmd/cgr-loader/cgr-loader_it_test.go b/cmd/cgr-loader/cgr-loader_it_test.go index 636948b90..18c6a8831 100644 --- a/cmd/cgr-loader/cgr-loader_it_test.go +++ b/cmd/cgr-loader/cgr-loader_it_test.go @@ -22,26 +22,40 @@ package main import ( "bytes" + "errors" "flag" + "github.com/cgrates/birpc" + "github.com/cgrates/birpc/context" + "github.com/cgrates/birpc/jsonrpc" + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" + "github.com/cgrates/rpcclient" "os/exec" "path" "reflect" "sort" "testing" - - "github.com/cgrates/birpc/context" - "github.com/cgrates/cgrates/config" - "github.com/cgrates/cgrates/engine" - "github.com/cgrates/cgrates/utils" - "github.com/cgrates/rpcclient" ) var ( dataDir = flag.String("data_dir", "/usr/share/cgrates", "CGR data dir path here") dbType = flag.String("dbtype", utils.MetaInternal, "The type of DataBase (Internal/Mongo/mySql)") + waitRater = flag.Int("wait_rater", 100, "Number of milliseconds to wait for rater to start and cache") encoding = flag.String("rpc", utils.MetaJSON, "what encoding whould be used for rpc comunication") ) +func newRPCClient(cfg *config.ListenCfg) (c *birpc.Client, err error) { + switch *encoding { + case utils.MetaJSON: + return jsonrpc.Dial(utils.TCP, cfg.RPCJSONListen) + case utils.MetaGOB: + return birpc.Dial(utils.TCP, cfg.RPCGOBListen) + default: + return nil, errors.New("UNSUPPORTED_RPC") + } +} + func TestLoadConfig(t *testing.T) { // DataDb *cfgPath = path.Join(*dataDir, "conf", "samples", "tutmongo") diff --git a/cmd/cgr-loader/cgr-loader_remove_it_test.go b/cmd/cgr-loader/cgr-loader_remove_it_test.go new file mode 100644 index 000000000..413f64209 --- /dev/null +++ b/cmd/cgr-loader/cgr-loader_remove_it_test.go @@ -0,0 +1,207 @@ +// +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 main + +import ( + "bytes" + "github.com/cgrates/birpc" + "github.com/cgrates/birpc/context" + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" + "os/exec" + "path" + "testing" +) + +var ( + cgrLdrCfgPath string + cgrLdrCfgDir string + cgrLdrCfg *config.CGRConfig + cgrLdrBIRPC *birpc.Client + cgrLdrTests = []func(t *testing.T) { + testCgrLdrInitCfg, + testCgrLdrInitDataDB, + testCgrLdrInitStorDB, + testCgrLdrStartEngine, + testCgrLdrRPCConn, + testCgrLdrGetSubsystemsBeforeLoad, + testCgrLdrKillEngine, + //testCgrLdrLoadData, + } +) + +func TestCGRLoaderRemove(t *testing.T) { + switch *dbType{ + case utils.MetaInternal: + cgrLdrCfgDir = "tutinternal" + case utils.MetaMongo: + cgrLdrCfgDir = "tutmongo" + case utils.MetaMySQL: + cgrLdrCfgDir = "tutmysql" + case utils.MetaPostgres: + t.SkipNow() + default: + t.Fatal("Unknown Database type") + } + for _, test := range cgrLdrTests { + t.Run("cgr-loader remove tests", test) + } +} + +func testCgrLdrInitCfg(t *testing.T) { + var err error + cgrLdrCfgPath = path.Join(*dataDir, "conf", "samples", cgrLdrCfgDir) + cgrLdrCfg, err = config.NewCGRConfigFromPath(cgrLdrCfgPath) + if err != nil { + t.Error(err) + } +} + +func testCgrLdrInitDataDB(t *testing.T) { + if err := engine.InitDataDB(cgrLdrCfg); err != nil { + t.Fatal(err) + } +} + +func testCgrLdrInitStorDB(t *testing.T) { + if err := engine.InitStorDB(cgrLdrCfg); err != nil { + t.Fatal(err) + } +} + +func testCgrLdrStartEngine(t *testing.T){ + if _, err := engine.StopStartEngine(cgrLdrCfgPath, *waitRater); err != nil { + t.Fatal(err) + } +} + +func testCgrLdrRPCConn(t *testing.T) { + var err error + cgrLdrBIRPC, err = newRPCClient(cgrLdrCfg.ListenCfg()) // We connect over JSON so we can also troubleshoot if needed + if err != nil { + t.Fatal(err) + } +} + +func testCgrLdrGetSubsystemsBeforeLoad(t *testing.T) { + //accountPrf + var replyAcc *utils.Account + if err := cgrLdrBIRPC.Call(context.Background(), utils.AdminSv1GetAccount, + &utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "1001"}}, + &replyAcc); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Expected %+q, received %+q", utils.ErrNotFound.Error(), err.Error()) + } + + //actionsPrf + var replyAct *engine.ActionProfile + if err := cgrLdrBIRPC.Call(context.Background(), utils.AdminSv1GetActionProfile, + &utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "ONE_TIME_ACT"}}, + &replyAct); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Expected %+q, received %+q", utils.ErrNotFound.Error(), err.Error()) + } + + //attributesPrf + var replyAttr *engine.APIAttributeProfile + if err := cgrLdrBIRPC.Call(context.Background(), utils.AdminSv1GetAttributeProfile, + &utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "ALS1"}}, + &replyAttr); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Expected %+q, received %+q", utils.ErrNotFound.Error(), err.Error()) + } + + //filtersPrf + var replyFltr *engine.Filter + if err := cgrLdrBIRPC.Call(context.Background(), utils.AdminSv1GetFilter, + &utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "FLTR_1"}}, + &replyFltr); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Expected %+q, received %+q", utils.ErrNotFound.Error(), err.Error()) + } + + //ratesPrf + var replyRates *utils.RateProfile + if err := cgrLdrBIRPC.Call(context.Background(), utils.AdminSv1GetRateProfile, + &utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "RP1"}}, + &replyRates); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Expected %+q, received %+q", utils.ErrNotFound.Error(), err.Error()) + } + + // resourcesPrf + var replyRes *engine.ResourceProfile + if err := cgrLdrBIRPC.Call(context.Background(), utils.AdminSv1GetResourceProfile, + &utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "ResGroup21"}}, + &replyRes); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Expected %+q, received %+q", utils.ErrNotFound.Error(), err.Error()) + } + + // routesPrf + var replyRts *engine.RouteProfile + if err := cgrLdrBIRPC.Call(context.Background(), utils.AdminSv1GetRouteProfile, + &utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "RoutePrf1"}}, + &replyRts); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Expected %+q, received %+q", utils.ErrNotFound.Error(), err.Error()) + } + + // statsPrf + var replySts *engine.StatQueueProfile + if err := cgrLdrBIRPC.Call(context.Background(), utils.AdminSv1GetStatQueueProfile, + &utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "TestStats"}}, + &replySts); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Expected %+q, received %+q", utils.ErrNotFound.Error(), err.Error()) + } + + // thresholdPrf + var replyThdPrf *engine.ThresholdProfile + if err := cgrLdrBIRPC.Call(context.Background(), utils.AdminSv1GetThresholdProfile, + &utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "Threshold1"}}, + &replyThdPrf); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Expected %+q, received %+q", utils.ErrNotFound.Error(), err.Error()) + } + + // threshold + var rplyThd *engine.Threshold + if err := cgrLdrBIRPC.Call(context.Background(), utils.ThresholdSv1GetThreshold, + &utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "Threshold1"}}, + &rplyThd); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Errorf("Expected %+q, received %+q", utils.ErrNotFound.Error(), err.Error()) + } +} + +func testCgrLdrLoadData(t *testing.T) { + cmd := exec.Command("cgr-loader", "-config_path="+cgrLdrCfgPath, "-path="+path.Join(*dataDir, "tariffplans", "tutorial")) + output := bytes.NewBuffer(nil) + outerr := bytes.NewBuffer(nil) + cmd.Stdout = output + cmd.Stderr = outerr + if err := cmd.Run(); err != nil { + t.Log(cmd.Args) + t.Log(output.String()) + t.Log(outerr.String()) + t.Fatal(err) + } +} + + +//Kill the engine when it is about to be finished +func testCgrLdrKillEngine(t *testing.T) { + if err := engine.KillEngine(100); err != nil { + t.Error(err) + } +} \ No newline at end of file diff --git a/utils/consts.go b/utils/consts.go index 92808bf2d..17e0dafc1 100644 --- a/utils/consts.go +++ b/utils/consts.go @@ -1299,10 +1299,10 @@ const ( RouteSv1GetRoutesList = "RouteSv1.GetRoutesList" RouteSv1GetRouteProfilesForEvent = "RouteSv1.GetRouteProfilesForEvent" RouteSv1Ping = "RouteSv1.Ping" - APIerSv1GetRouteProfile = "APIerSv1.GetRouteProfile" - APIerSv1GetRouteProfileIDs = "APIerSv1.GetRouteProfileIDs" - APIerSv1RemoveRouteProfile = "APIerSv1.RemoveRouteProfile" - APIerSv1SetRouteProfile = "APIerSv1.SetRouteProfile" + AdminSv1GetRouteProfile = "AdminSv1.GetRouteProfile" + AdminSv1GetRouteProfileIDs = "AdminSv1.GetRouteProfileIDs" + AdminSv1RemoveRouteProfile = "AdminSv1.RemoveRouteProfile" + AdminSv1SetRouteProfile = "AdminSv1.SetRouteProfile" ) // AttributeS APIs