From eece1ba3adfd29597d9bed905046f3c427215e63 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 6 May 2016 16:22:45 +0300 Subject: [PATCH] destination management tests --- console/destinations.go | 65 ++++++ .../destinations/addback/Destinations.csv | 13 ++ .../test/destinations/addone/Destinations.csv | 15 ++ .../destinations/alldests/Destinations.csv | 13 ++ .../destinations/removesome/Destinations.csv | 8 + general_tests/dest_management_it_test.go | 205 ++++++++++++++++++ 6 files changed, 319 insertions(+) create mode 100644 console/destinations.go create mode 100644 data/tariffplans/test/destinations/addback/Destinations.csv create mode 100644 data/tariffplans/test/destinations/addone/Destinations.csv create mode 100644 data/tariffplans/test/destinations/alldests/Destinations.csv create mode 100644 data/tariffplans/test/destinations/removesome/Destinations.csv create mode 100644 general_tests/dest_management_it_test.go diff --git a/console/destinations.go b/console/destinations.go new file mode 100644 index 000000000..328453bbe --- /dev/null +++ b/console/destinations.go @@ -0,0 +1,65 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2012-2015 ITsysCOM + +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 console + +import ( + "github.com/cgrates/cgrates/apier/v2" + "github.com/cgrates/cgrates/engine" +) + +func init() { + c := &CmdGetDestination{ + name: "destinations", + rpcMethod: "ApierV2.GetDestinations", + } + commands[c.Name()] = c + c.CommandExecuter = &CommandExecuter{c} +} + +// Commander implementation +type CmdGetDestination struct { + name string + rpcMethod string + rpcParams *v2.AttrGetDestinations + *CommandExecuter +} + +func (self *CmdGetDestination) Name() string { + return self.name +} + +func (self *CmdGetDestination) RpcMethod() string { + return self.rpcMethod +} + +func (self *CmdGetDestination) RpcParams(reset bool) interface{} { + if reset || self.rpcParams == nil { + self.rpcParams = &v2.AttrGetDestinations{} + } + return self.rpcParams +} + +func (self *CmdGetDestination) PostprocessRpcParams() error { + return nil +} + +func (self *CmdGetDestination) RpcResult() interface{} { + a := make([]*engine.Destination, 0) + return &a +} diff --git a/data/tariffplans/test/destinations/addback/Destinations.csv b/data/tariffplans/test/destinations/addback/Destinations.csv new file mode 100644 index 000000000..712524759 --- /dev/null +++ b/data/tariffplans/test/destinations/addback/Destinations.csv @@ -0,0 +1,13 @@ +#Tag,Prefix +GERMANY,+49 +GERMANY_MOBILE,+4915 +GERMANY_MOBILE,+4916 +GERMANY_MOBILE,+4917 +DATA_DEST,222 +ROMANIA,+40 +ROMANIA_MOBILE,+4072 +ROMANIA_MOBILE,+4073 +ROMANIA_MOBILE,+4074 +ROMANIA_MOBILE,+4072 +EUROPE,+40 +EUROPE,+49 diff --git a/data/tariffplans/test/destinations/addone/Destinations.csv b/data/tariffplans/test/destinations/addone/Destinations.csv new file mode 100644 index 000000000..de96fe5b3 --- /dev/null +++ b/data/tariffplans/test/destinations/addone/Destinations.csv @@ -0,0 +1,15 @@ +#Tag,Prefix +GERMANY,+49 +GERMANY_MOBILE,+4915 +GERMANY_MOBILE,+4916 +GERMANY_MOBILE,+4917 +DATA_DEST,222 +ROMANIA,+40 +ROMANIA_MOBILE,+4072 +ROMANIA_MOBILE,+4073 +ROMANIA_MOBILE,+4074 +ROMANIA_MOBILE,+4072 +HUNGARY,+36 +EUROPE,+40 +EUROPE,+49 +EUROPE,+36 diff --git a/data/tariffplans/test/destinations/alldests/Destinations.csv b/data/tariffplans/test/destinations/alldests/Destinations.csv new file mode 100644 index 000000000..712524759 --- /dev/null +++ b/data/tariffplans/test/destinations/alldests/Destinations.csv @@ -0,0 +1,13 @@ +#Tag,Prefix +GERMANY,+49 +GERMANY_MOBILE,+4915 +GERMANY_MOBILE,+4916 +GERMANY_MOBILE,+4917 +DATA_DEST,222 +ROMANIA,+40 +ROMANIA_MOBILE,+4072 +ROMANIA_MOBILE,+4073 +ROMANIA_MOBILE,+4074 +ROMANIA_MOBILE,+4072 +EUROPE,+40 +EUROPE,+49 diff --git a/data/tariffplans/test/destinations/removesome/Destinations.csv b/data/tariffplans/test/destinations/removesome/Destinations.csv new file mode 100644 index 000000000..15d102471 --- /dev/null +++ b/data/tariffplans/test/destinations/removesome/Destinations.csv @@ -0,0 +1,8 @@ +#Tag,Prefix +GERMANY,+49 +GERMANY_MOBILE,+4915 +GERMANY_MOBILE,+4916 +GERMANY_MOBILE,+4917 +DATA_DEST,222 +EUROPE,+40 +EUROPE,+49 diff --git a/general_tests/dest_management_it_test.go b/general_tests/dest_management_it_test.go new file mode 100644 index 000000000..fe7ad30f4 --- /dev/null +++ b/general_tests/dest_management_it_test.go @@ -0,0 +1,205 @@ +package general_tests + +import ( + "net/rpc" + "net/rpc/jsonrpc" + "path" + "testing" + "time" + + "github.com/cgrates/cgrates/apier/v2" + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +var destCfgPath string +var destCfg *config.CGRConfig +var destRPC *rpc.Client +var destLoadInst engine.LoadInstance // Share load information between tests + +func TestDestManagInitCfg(t *testing.T) { + if !*testIntegration { + return + } + destCfgPath = path.Join(*dataDir, "conf", "samples", "tutlocal") + // Init config first + var err error + destCfg, err = config.NewCGRConfigFromFolder(destCfgPath) + if err != nil { + t.Error(err) + } + destCfg.DataFolderPath = *dataDir // Share DataFolderPath through config towards StoreDb for Flush() + config.SetCgrConfig(destCfg) +} + +// Remove data in both rating and accounting db +func TestDestManagResetDataDb(t *testing.T) { + if !*testIntegration { + return + } + if err := engine.InitDataDb(destCfg); err != nil { + t.Fatal(err) + } +} + +// Wipe out the cdr database +func TestDestManagResetStorDb(t *testing.T) { + if !*testIntegration { + return + } + if err := engine.InitStorDb(destCfg); err != nil { + t.Fatal(err) + } +} + +// Start CGR Engine +func TestDestManagStartEngine(t *testing.T) { + if !*testIntegration { + return + } + if _, err := engine.StopStartEngine(destCfgPath, *waitRater); err != nil { + t.Fatal(err) + } +} + +// Connect rpc client to rater +func TestDestManagRpcConn(t *testing.T) { + if !*testIntegration { + return + } + var err error + destRPC, err = jsonrpc.Dial("tcp", destCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed + if err != nil { + t.Fatal(err) + } +} + +// Load the tariff plan, creating accounts and their balances +func TestDestManagLoadTariffPlanFromFolderAll(t *testing.T) { + if !*testIntegration { + return + } + attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "test", "destinations", "alldests")} + if err := destRPC.Call("ApierV2.LoadTariffPlanFromFolder", attrs, &destLoadInst); err != nil { + t.Error(err) + } else if destLoadInst.LoadId == "" { + t.Error("Empty loadId received, loadInstance: ", destLoadInst) + } + time.Sleep(time.Duration(*waitRater) * time.Millisecond) // Give time for scheduler to execute topups +} + +func TestDestManagAllDestinationLoaded(t *testing.T) { + if !*testIntegration { + return + } + dests := make([]*engine.Destination, 0) + if err := destRPC.Call("ApierV2.GetDestinations", v2.AttrGetDestinations{DestinationIDs: []string{}}, &dests); err != nil { + t.Error("Got error on ApierV2.GetDestinations: ", err.Error()) + } else if len(dests) != 6 { + t.Errorf("Calling ApierV2.GetDestinations got reply: %v", utils.ToIJSON(dests)) + } + + var rcvStats utils.CacheStats + if err := destRPC.Call("ApierV1.GetCacheStats", utils.AttrCacheStats{}, &rcvStats); err != nil { + t.Error("Got error on ApierV1.GetCacheStats: ", err.Error()) + } else if rcvStats.Destinations != 9 { + t.Errorf("Calling ApierV1.GetCacheStats received: %+v", rcvStats) + } +} + +func TestDestManagLoadTariffPlanFromFolderRemoveSome(t *testing.T) { + if !*testIntegration { + return + } + attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "test", "destinations", "removesome")} + if err := destRPC.Call("ApierV2.LoadTariffPlanFromFolder", attrs, &destLoadInst); err != nil { + t.Error(err) + } else if destLoadInst.LoadId == "" { + t.Error("Empty loadId received, loadInstance: ", destLoadInst) + } + time.Sleep(time.Duration(*waitRater) * time.Millisecond) // Give time for scheduler to execute topups +} + +func TestDestManagRemoveSomeDestinationLoaded(t *testing.T) { + if !*testIntegration { + return + } + dests := make([]*engine.Destination, 0) + if err := destRPC.Call("ApierV2.GetDestinations", v2.AttrGetDestinations{DestinationIDs: []string{}}, &dests); err != nil { + t.Error("Got error on ApierV2.GetDestinations: ", err.Error()) + } else if len(dests) != 6 { + t.Errorf("Calling ApierV2.GetDestinations got reply: %v", utils.ToIJSON(dests)) + } + + var rcvStats utils.CacheStats + if err := destRPC.Call("ApierV1.GetCacheStats", utils.AttrCacheStats{}, &rcvStats); err != nil { + t.Error("Got error on ApierV1.GetCacheStats: ", err.Error()) + } else if rcvStats.Destinations != 9 { + t.Errorf("Calling ApierV1.GetCacheStats received: %+v", rcvStats) + } +} + +func TestDestManagLoadTariffPlanFromFolderAddBack(t *testing.T) { + if !*testIntegration { + return + } + attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "test", "destinations", "addback")} + if err := destRPC.Call("ApierV2.LoadTariffPlanFromFolder", attrs, &destLoadInst); err != nil { + t.Error(err) + } else if destLoadInst.LoadId == "" { + t.Error("Empty loadId received, loadInstance: ", destLoadInst) + } + time.Sleep(time.Duration(*waitRater) * time.Millisecond) // Give time for scheduler to execute topups +} + +func TestDestManagAddBackDestinationLoaded(t *testing.T) { + if !*testIntegration { + return + } + dests := make([]*engine.Destination, 0) + if err := destRPC.Call("ApierV2.GetDestinations", v2.AttrGetDestinations{DestinationIDs: []string{}}, &dests); err != nil { + t.Error("Got error on ApierV2.GetDestinations: ", err.Error()) + } else if len(dests) != 6 { + t.Errorf("Calling ApierV2.GetDestinations got reply: %v", utils.ToIJSON(dests)) + } + + var rcvStats utils.CacheStats + if err := destRPC.Call("ApierV1.GetCacheStats", utils.AttrCacheStats{}, &rcvStats); err != nil { + t.Error("Got error on ApierV1.GetCacheStats: ", err.Error()) + } else if rcvStats.Destinations != 9 { + t.Errorf("Calling ApierV1.GetCacheStats received: %+v", rcvStats) + } +} + +func TestDestManagLoadTariffPlanFromFolderAddOne(t *testing.T) { + if !*testIntegration { + return + } + attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "test", "destinations", "addone")} + if err := destRPC.Call("ApierV2.LoadTariffPlanFromFolder", attrs, &destLoadInst); err != nil { + t.Error(err) + } else if destLoadInst.LoadId == "" { + t.Error("Empty loadId received, loadInstance: ", destLoadInst) + } + time.Sleep(time.Duration(*waitRater) * time.Millisecond) // Give time for scheduler to execute topups +} + +func TestDestManagAddOneDestinationLoaded(t *testing.T) { + if !*testIntegration { + return + } + dests := make([]*engine.Destination, 0) + if err := destRPC.Call("ApierV2.GetDestinations", v2.AttrGetDestinations{DestinationIDs: []string{}}, &dests); err != nil { + t.Error("Got error on ApierV2.GetDestinations: ", err.Error()) + } else if len(dests) != 7 { + t.Errorf("Calling ApierV2.GetDestinations got reply: %v", utils.ToIJSON(dests)) + } + + var rcvStats utils.CacheStats + if err := destRPC.Call("ApierV1.GetCacheStats", utils.AttrCacheStats{}, &rcvStats); err != nil { + t.Error("Got error on ApierV1.GetCacheStats: ", err.Error()) + } else if rcvStats.Destinations != 10 { + t.Errorf("Calling ApierV1.GetCacheStats received: %+v", rcvStats) + } +}