Renamed integration tests

This commit is contained in:
Edwardro22
2016-11-16 23:55:03 +01:00
parent 561ca44d4a
commit 667c180f30
9 changed files with 3012 additions and 0 deletions

1705
apier/v1/apier_it_test.go Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,195 @@
// +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 <http://www.gnu.org/licenses/>
*/
package v1
import (
"net/rpc"
"net/rpc/jsonrpc"
"path"
"reflect"
"testing"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
var cdrstCfgPath string
var cdrstCfg *config.CGRConfig
var cdrstRpc *rpc.Client
func TestCDRStatsitLoadConfig(t *testing.T) {
var err error
cdrstCfgPath = path.Join(*dataDir, "conf", "samples", "cdrstats")
if cdrstCfg, err = config.NewCGRConfigFromFolder(cfgPath); err != nil {
t.Error(err)
}
}
func TestCDRStatsitInitDataDb(t *testing.T) {
if err := engine.InitDataDb(cdrstCfg); err != nil {
t.Fatal(err)
}
}
func TestCDRStatsitStartEngine(t *testing.T) {
if _, err := engine.StopStartEngine(cdrstCfgPath, 1000); err != nil {
t.Fatal(err)
}
}
// Connect rpc client to rater
func TestCDRStatsitRpcConn(t *testing.T) {
var err error
cdrstRpc, err = jsonrpc.Dial("tcp", cdrstCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
if err != nil {
t.Fatal("Could not connect to rater: ", err.Error())
}
}
func TestCDRStatsitLoadTariffPlanFromFolder(t *testing.T) {
reply := ""
// Simple test that command is executed without errors
attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "cdrstats")}
if err := cdrstRpc.Call("ApierV1.LoadTariffPlanFromFolder", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.LoadTariffPlanFromFolder: ", err.Error())
} else if reply != "OK" {
t.Error("Calling ApierV1.LoadTariffPlanFromFolder got reply: ", reply)
}
time.Sleep(time.Duration(*waitRater) * time.Millisecond) // Give time for scheduler to execute topups
}
func TestCDRStatsitGetQueueIds2(t *testing.T) {
var queueIds []string
eQueueIds := []string{"CDRST3", "CDRST4"}
if err := cdrstRpc.Call("CDRStatsV1.GetQueueIds", "", &queueIds); err != nil {
t.Error("Calling CDRStatsV1.GetQueueIds, got error: ", err.Error())
} else if len(eQueueIds) != len(queueIds) {
t.Errorf("Expecting: %v, received: %v", eQueueIds, queueIds)
}
var rcvMetrics map[string]float64
expectedMetrics := map[string]float64{"ASR": -1, "ACD": -1}
if err := cdrstRpc.Call("CDRStatsV1.GetMetrics", AttrGetMetrics{StatsQueueId: "CDRST4"}, &rcvMetrics); err != nil {
t.Error("Calling CDRStatsV1.GetMetrics, got error: ", err.Error())
} else if !reflect.DeepEqual(expectedMetrics, rcvMetrics) {
t.Errorf("Expecting: %v, received: %v", expectedMetrics, rcvMetrics)
}
}
func TestCDRStatsitPostCdrs(t *testing.T) {
storedCdrs := []*engine.CDR{
&engine.CDR{CGRID: utils.Sha1("dsafdsafa", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), OrderID: 123, ToR: utils.VOICE, OriginID: "dsafdsafa",
OriginHost: "192.168.1.1", Source: "test",
RequestType: utils.META_RATED, Direction: "*out", Tenant: "cgrates.org",
Category: "call", Account: "1001", Subject: "1001", Destination: "+4986517174963", SetupTime: time.Now(),
AnswerTime: time.Now(), RunID: utils.DEFAULT_RUNID, Usage: time.Duration(10) * time.Second,
ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, Cost: 1.01,
},
&engine.CDR{CGRID: utils.Sha1("dsafdsafb", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), OrderID: 123, ToR: utils.VOICE, OriginID: "dsafdsafb",
OriginHost: "192.168.1.1", Source: "test",
RequestType: utils.META_RATED, Direction: "*out", Tenant: "cgrates.org",
Category: "call", Account: "1001", Subject: "1001", Destination: "+4986517174963", SetupTime: time.Now(),
AnswerTime: time.Now(), RunID: utils.DEFAULT_RUNID,
Usage: time.Duration(5) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, Cost: 1.01,
},
&engine.CDR{CGRID: utils.Sha1("dsafdsafc", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), OrderID: 123, ToR: utils.VOICE, OriginID: "dsafdsafc",
OriginHost: "192.168.1.1", Source: "test",
RequestType: utils.META_RATED, Direction: "*out", Tenant: "cgrates.org",
Category: "call", Account: "1001", Subject: "1001", Destination: "+4986517174963", SetupTime: time.Now(), AnswerTime: time.Now(),
RunID: utils.DEFAULT_RUNID,
Usage: time.Duration(30) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, Cost: 1.01,
},
&engine.CDR{CGRID: utils.Sha1("dsafdsafd", time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String()), OrderID: 123, ToR: utils.VOICE, OriginID: "dsafdsafd",
OriginHost: "192.168.1.1", Source: "test",
RequestType: utils.META_RATED, Direction: "*out", Tenant: "cgrates.org",
Category: "call", Account: "1001", Subject: "1001", Destination: "+4986517174963", SetupTime: time.Now(), AnswerTime: time.Time{},
RunID: utils.DEFAULT_RUNID,
Usage: time.Duration(0) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, Cost: 1.01,
},
}
for _, cdr := range storedCdrs {
var reply string
if err := cdrstRpc.Call("CdrsV1.ProcessCdr", cdr, &reply); err != nil {
t.Error(err)
}
}
time.Sleep(time.Duration(*waitRater) * time.Millisecond)
}
func TestCDRStatsitGetMetrics1(t *testing.T) {
var rcvMetrics2 map[string]float64
expectedMetrics2 := map[string]float64{"ASR": 75, "ACD": 15}
if err := cdrstRpc.Call("CDRStatsV1.GetMetrics", AttrGetMetrics{StatsQueueId: "CDRST4"}, &rcvMetrics2); err != nil {
t.Error("Calling CDRStatsV1.GetMetrics, got error: ", err.Error())
} else if !reflect.DeepEqual(expectedMetrics2, rcvMetrics2) {
t.Errorf("Expecting: %v, received: %v", expectedMetrics2, rcvMetrics2)
}
}
// Test stats persistence
func TestCDRStatsitStatsPersistence(t *testing.T) {
time.Sleep(time.Duration(2) * time.Second) // Allow stats to be updated in dataDb
if _, err := engine.StopStartEngine(cdrstCfgPath, *waitRater); err != nil {
t.Fatal(err)
}
var err error
cdrstRpc, err = jsonrpc.Dial("tcp", cdrstCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
if err != nil {
t.Fatal("Could not connect to rater: ", err.Error())
}
var rcvMetrics map[string]float64
expectedMetrics := map[string]float64{"ASR": 75, "ACD": 15}
if err := cdrstRpc.Call("CDRStatsV1.GetMetrics", AttrGetMetrics{StatsQueueId: "CDRST4"}, &rcvMetrics); err != nil {
t.Error("Calling CDRStatsV1.GetMetrics, got error: ", err.Error())
} else if !reflect.DeepEqual(expectedMetrics, rcvMetrics) {
t.Errorf("Expecting: %v, received: %v", expectedMetrics, rcvMetrics)
}
}
func TestCDRStatsitResetMetrics(t *testing.T) {
var reply string
if err := cdrstRpc.Call("CDRStatsV1.ResetQueues", utils.AttrCDRStatsReloadQueues{StatsQueueIds: []string{"CDRST4"}}, &reply); err != nil {
t.Error("Calling CDRStatsV1.ResetQueues, got error: ", err.Error())
} else if reply != utils.OK {
t.Error("Unexpected reply received: ", reply)
}
time.Sleep(time.Duration(*waitRater) * time.Millisecond)
var rcvMetrics2 map[string]float64
expectedMetrics2 := map[string]float64{"ASR": -1, "ACD": -1}
if err := cdrstRpc.Call("CDRStatsV1.GetMetrics", AttrGetMetrics{StatsQueueId: "CDRST4"}, &rcvMetrics2); err != nil {
t.Error("Calling CDRStatsV1.GetMetrics, got error: ", err.Error())
} else if !reflect.DeepEqual(expectedMetrics2, rcvMetrics2) {
t.Errorf("Expecting: %v, received: %v", expectedMetrics2, rcvMetrics2)
}
}
func TestCDRStatsitKillEngine(t *testing.T) {
if err := engine.KillEngine(*waitRater); err != nil {
t.Error(err)
}
}

154
cdrc/flatstore_it_test.go Normal file
View File

@@ -0,0 +1,154 @@
// +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 <http://www.gnu.org/licenses/>
*/
package cdrc
import (
"io/ioutil"
"net/rpc"
"net/rpc/jsonrpc"
"os"
"path"
"testing"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
)
var flatstoreCfgPath string
var flatstoreCfg *config.CGRConfig
var flatstoreRpc *rpc.Client
var flatstoreCdrcCfg *config.CdrcConfig
var fullSuccessfull = `INVITE|2daec40c|548625ac|dd0c4c617a9919d29a6175cdff223a9e@0:0:0:0:0:0:0:0|200|OK|1436454408|*prepaid|1001|1002||3401:2069362475
BYE|2daec40c|548625ac|dd0c4c617a9919d29a6175cdff223a9e@0:0:0:0:0:0:0:0|200|OK|1436454410|||||3401:2069362475
INVITE|f9d3d5c3|c863a6e3|214d8f52b566e33a9349b184e72a4cca@0:0:0:0:0:0:0:0|200|OK|1436454647|*postpaid|1002|1001||1877:893549741
BYE|f9d3d5c3|c863a6e3|214d8f52b566e33a9349b184e72a4cca@0:0:0:0:0:0:0:0|200|OK|1436454651|||||1877:893549741
INVITE|36e39a5|42d996f9|3a63321dd3b325eec688dc2aefb6ac2d@0:0:0:0:0:0:0:0|200|OK|1436454657|*prepaid|1001|1002||2407:1884881533
BYE|36e39a5|42d996f9|3a63321dd3b325eec688dc2aefb6ac2d@0:0:0:0:0:0:0:0|200|OK|1436454661|||||2407:1884881533
INVITE|3111f3c9|49ca4c42|a58ebaae40d08d6757d8424fb09c4c54@0:0:0:0:0:0:0:0|200|OK|1436454690|*prepaid|1001|1002||3099:1909036290
BYE|3111f3c9|49ca4c42|a58ebaae40d08d6757d8424fb09c4c54@0:0:0:0:0:0:0:0|200|OK|1436454692|||||3099:1909036290
`
var fullMissed = `INVITE|ef6c6256|da501581|0bfdd176d1b93e7df3de5c6f4873ee04@0:0:0:0:0:0:0:0|487|Request Terminated|1436454643|*prepaid|1001|1002||1224:339382783
INVITE|7905e511||81880da80a94bda81b425b09009e055c@0:0:0:0:0:0:0:0|404|Not Found|1436454668|*prepaid|1001|1002||1980:1216490844
INVITE|324cb497|d4af7023|8deaadf2ae9a17809a391f05af31afb0@0:0:0:0:0:0:0:0|486|Busy here|1436454687|*postpaid|1002|1001||474:130115066`
var part1 = `BYE|f9d3d5c3|c863a6e3|214d8f52b566e33a9349b184e72a4ccb@0:0:0:0:0:0:0:0|200|OK|1436454651|||||1877:893549742
`
var part2 = `INVITE|f9d3d5c3|c863a6e3|214d8f52b566e33a9349b184e72a4ccb@0:0:0:0:0:0:0:0|200|OK|1436454647|*postpaid|1002|1003||1877:893549742
INVITE|2daec40c|548625ac|dd0c4c617a9919d29a6175cdff223a9e@0:0:0:0:0:0:0:0|200|OK|1436454408|*prepaid|1001|1002||3401:2069362475`
func TestFlatstoreitInitCfg(t *testing.T) {
var err error
flatstoreCfgPath = path.Join(*dataDir, "conf", "samples", "cdrcflatstore")
if flatstoreCfg, err = config.NewCGRConfigFromFolder(flatstoreCfgPath); err != nil {
t.Fatal("Got config error: ", err.Error())
}
}
// InitDb so we can rely on count
func TestFlatstoreitInitCdrDb(t *testing.T) {
if err := engine.InitStorDb(flatstoreCfg); err != nil {
t.Fatal(err)
}
}
// Creates cdr files and moves them into processing folder
func TestFlatstoreitCreateCdrFiles(t *testing.T) {
if flatstoreCfg == nil {
t.Fatal("Empty default cdrc configuration")
}
for _, cdrcCfg := range flatstoreCfg.CdrcProfiles["/tmp/cgr_flatstore/cdrc/in"] {
if cdrcCfg.ID == "FLATSTORE" {
flatstoreCdrcCfg = cdrcCfg
}
}
if err := os.RemoveAll(flatstoreCdrcCfg.CdrInDir); err != nil {
t.Fatal("Error removing folder: ", flatstoreCdrcCfg.CdrInDir, err)
}
if err := os.MkdirAll(flatstoreCdrcCfg.CdrInDir, 0755); err != nil {
t.Fatal("Error creating folder: ", flatstoreCdrcCfg.CdrInDir, err)
}
if err := os.RemoveAll(flatstoreCdrcCfg.CdrOutDir); err != nil {
t.Fatal("Error removing folder: ", flatstoreCdrcCfg.CdrOutDir, err)
}
if err := os.MkdirAll(flatstoreCdrcCfg.CdrOutDir, 0755); err != nil {
t.Fatal("Error creating folder: ", flatstoreCdrcCfg.CdrOutDir, err)
}
}
func TestFlatstoreitStartEngine(t *testing.T) {
if _, err := engine.StopStartEngine(flatstoreCfgPath, *waitRater); err != nil {
t.Fatal(err)
}
}
// Connect rpc client to rater
func TestFlatstoreitRpcConn(t *testing.T) {
var err error
flatstoreRpc, err = jsonrpc.Dial("tcp", flatstoreCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
if err != nil {
t.Fatal("Could not connect to rater: ", err.Error())
}
}
func TestFlatstoreitProcessFiles(t *testing.T) {
if err := ioutil.WriteFile(path.Join("/tmp", "acc_1.log"), []byte(fullSuccessfull), 0644); err != nil {
t.Fatal(err.Error)
}
if err := ioutil.WriteFile(path.Join("/tmp", "missed_calls_1.log"), []byte(fullMissed), 0644); err != nil {
t.Fatal(err.Error)
}
if err := ioutil.WriteFile(path.Join("/tmp", "acc_2.log"), []byte(part1), 0644); err != nil {
t.Fatal(err.Error)
}
if err := ioutil.WriteFile(path.Join("/tmp", "acc_3.log"), []byte(part2), 0644); err != nil {
t.Fatal(err.Error)
}
//Rename(oldpath, newpath string)
for _, fileName := range []string{"acc_1.log", "missed_calls_1.log", "acc_2.log", "acc_3.log"} {
if err := os.Rename(path.Join("/tmp", fileName), path.Join(flatstoreCdrcCfg.CdrInDir, fileName)); err != nil {
t.Fatal(err)
}
}
time.Sleep(time.Duration(2) * time.Second) // Give time for processing to happen and the .unparired file to be written
filesInDir, _ := ioutil.ReadDir(flatstoreCdrcCfg.CdrInDir)
if len(filesInDir) != 0 {
t.Errorf("Files in cdrcInDir: %+v", filesInDir)
}
filesOutDir, _ := ioutil.ReadDir(flatstoreCdrcCfg.CdrOutDir)
if len(filesOutDir) != 5 {
t.Errorf("In CdrcOutDir, expecting 5 files, got: %d", len(filesOutDir))
}
ePartContent := "INVITE|2daec40c|548625ac|dd0c4c617a9919d29a6175cdff223a9e@0:0:0:0:0:0:0:0|200|OK|1436454408|*prepaid|1001|1002||3401:2069362475\n"
if partContent, err := ioutil.ReadFile(path.Join(flatstoreCdrcCfg.CdrOutDir, "acc_3.log.unpaired")); err != nil {
t.Error(err)
} else if ePartContent != string(partContent) {
t.Errorf("Expecting: %s, received: %s", ePartContent, string(partContent))
}
}

146
cdrc/fwv_it_test.go Normal file
View File

@@ -0,0 +1,146 @@
// +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 <http://www.gnu.org/licenses/>
*/
package cdrc
import (
"io/ioutil"
"net/rpc"
"net/rpc/jsonrpc"
"os"
"path"
"testing"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
)
var fwvCfgPath string
var fwvCfg *config.CGRConfig
var fwvRpc *rpc.Client
var fwvCdrcCfg *config.CdrcConfig
var FW_CDR_FILE1 = `HDR0001DDB ABC Some Connect A.B. DDB-Some-10022-20120711-309.CDR 00030920120711100255
CDR0000010 0 20120708181506000123451234 0040123123120 004 000018009980010001ISDN ABC 10Buiten uw regio EHV 00000009190000000009
CDR0000020 0 20120708190945000123451234 0040123123120 004 000016009980010001ISDN ABC 10Buiten uw regio EHV 00000009190000000009
CDR0000030 0 20120708191009000123451234 0040123123120 004 000020009980010001ISDN ABC 10Buiten uw regio EHV 00000009190000000009
CDR0000040 0 20120708231043000123451234 0040123123120 004 000011009980010001ISDN ABC 10Buiten uw regio EHV 00000009190000000009
CDR0000050 0 20120709122216000123451235 004212 004 000217009980010001ISDN ABC 10Buiten uw regio HMR 00000000190000000000
CDR0000060 0 20120709130542000123451236 0012323453 004 000019009980010001ISDN ABC 35Sterdiensten AP 00000000190000000000
CDR0000070 0 20120709140032000123451237 0040012323453100 001 000050009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000080 0 20120709140142000123451237 0040012323453100 001 000050009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000090 0 20120709150305000123451237 0040012323453100 001 000050009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000100 0 20120709150414000123451237 0040012323453100 001 000057009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000110 0 20120709150531000123451237 0040012323453100 001 000059009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000120 0 20120709150635000123451237 0040012323453100 001 000050009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000130 0 20120709151756000123451237 0040012323453100 001 000050009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000140 0 20120709154549000123451237 0040012323453100 001 000052009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000150 0 20120709154701000123451237 0040012323453100 001 000121009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000160 0 20120709154842000123451237 0040012323453100 001 000055009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000170 0 20120709154956000123451237 0040012323453100 001 000115009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000180 0 20120709155131000123451237 0040012323453100 001 000059009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000190 0 20120709155236000123451237 0040012323453100 001 000050009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000200 0 20120709160309000123451237 0040012323453100 001 000100009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000210 0 20120709160415000123451237 0040012323453100 001 000050009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000220 0 20120709161739000123451237 0040012323453100 001 000058009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000230 0 20120709170356000123123459 0040123234531 004 000012002760010001ISDN 276 10Buiten uw regio TB 00000009190000000009
CDR0000240 0 20120709181036000123123450 0012323453 004 000042009980010001ISDN ABC 05Binnen uw regio AP 00000010190000000010
CDR0000250 0 20120709191245000123123458 0040123232350 004 000012002760000001PSTN 276 10Buiten uw regio TB 00000009190000000009
CDR0000260 0 20120709202324000123123459 0040123234531 004 000011002760010001ISDN 276 10Buiten uw regio TB 00000009190000000009
CDR0000270 0 20120709211756000123451237 0040012323453100 001 000051009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000280 0 20120709211852000123451237 0040012323453100 001 000050009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000290 0 20120709212904000123123458 0040123232350 004 000012002760000001PSTN 276 10Buiten uw regio TB 00000009190000000009
CDR0000300 0 20120709073707000123123459 0040123234531 004 000012002760010001ISDN 276 10Buiten uw regio TB 00000009190000000009
CDR0000310 0 20120709085451000123451237 0040012323453100 001 000744009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000320 0 20120709091756000123451237 0040012323453100 001 000050009980030001ISDN ABD 20Internationaal NLB 00000000190000000000
CDR0000330 0 20120710070434000123123458 0040123232350 004 000012002760000001PSTN 276 10Buiten uw regio TB 00000009190000000009
TRL0001DDB ABC Some Connect A.B. DDB-Some-10022-20120711-309.CDR 0003090000003300000030550000000001000000000100Y
`
func TestFwvitInitCfg(t *testing.T) {
var err error
fwvCfgPath = path.Join(*dataDir, "conf", "samples", "cdrcfwv")
if fwvCfg, err = config.NewCGRConfigFromFolder(fwvCfgPath); err != nil {
t.Fatal("Got config error: ", err.Error())
}
}
// Creates cdr files and moves them into processing folder
func TestFwvitCreateCdrFiles(t *testing.T) {
if fwvCfg == nil {
t.Fatal("Empty default cdrc configuration")
}
for _, cdrcCfg := range fwvCfg.CdrcProfiles["/tmp/cgr_fwv/cdrc/in"] {
if cdrcCfg.ID == "FWV1" {
fwvCdrcCfg = cdrcCfg
}
}
if err := os.RemoveAll(fwvCdrcCfg.CdrInDir); err != nil {
t.Fatal("Error removing folder: ", fwvCdrcCfg.CdrInDir, err)
}
if err := os.MkdirAll(fwvCdrcCfg.CdrInDir, 0755); err != nil {
t.Fatal("Error creating folder: ", fwvCdrcCfg.CdrInDir, err)
}
if err := os.RemoveAll(fwvCdrcCfg.CdrOutDir); err != nil {
t.Fatal("Error removing folder: ", fwvCdrcCfg.CdrOutDir, err)
}
if err := os.MkdirAll(fwvCdrcCfg.CdrOutDir, 0755); err != nil {
t.Fatal("Error creating folder: ", fwvCdrcCfg.CdrOutDir, err)
}
}
func TestFwvitStartEngine(t *testing.T) {
if _, err := engine.StopStartEngine(fwvCfgPath, *waitRater); err != nil {
t.Fatal(err)
}
}
// Connect rpc client to rater
func TestFwvitRpcConn(t *testing.T) {
var err error
fwvRpc, err = jsonrpc.Dial("tcp", fwvCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
if err != nil {
t.Fatal("Could not connect to rater: ", err.Error())
}
}
func TestFwvitProcessFiles(t *testing.T) {
fileName := "test1.fwv"
if err := ioutil.WriteFile(path.Join("/tmp", fileName), []byte(FW_CDR_FILE1), 0644); err != nil {
t.Fatal(err.Error)
}
if err := os.Rename(path.Join("/tmp", fileName), path.Join(fwvCdrcCfg.CdrInDir, fileName)); err != nil {
t.Fatal(err)
}
time.Sleep(time.Duration(1) * time.Second)
filesInDir, _ := ioutil.ReadDir(fwvCdrcCfg.CdrInDir)
if len(filesInDir) != 0 {
t.Errorf("Files in cdrcInDir: %d", len(filesInDir))
}
filesOutDir, _ := ioutil.ReadDir(fwvCdrcCfg.CdrOutDir)
if len(filesOutDir) != 1 {
t.Errorf("In CdrcOutDir, expecting 1 files, got: %d", len(filesOutDir))
}
}

View File

@@ -0,0 +1,95 @@
// +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 <http://www.gnu.org/licenses/>
*/
package config
import (
"flag"
"github.com/cgrates/cgrates/utils"
"testing"
)
var testIT = flag.Bool("integration", false, "Perform the tests only on local test environment, not by default.")
var mfCgrCfg *CGRConfig
func TestMfInitConfig(t *testing.T) {
var err error
if mfCgrCfg, err = NewCGRConfigFromFolder("/usr/share/cgrates/conf/samples/multifiles"); err != nil {
t.Fatal("Got config error: ", err.Error())
}
}
func TestMfGeneralItems(t *testing.T) {
if mfCgrCfg.DefaultReqType != utils.META_PSEUDOPREPAID { // Twice reconfigured
t.Error("DefaultReqType: ", mfCgrCfg.DefaultReqType)
}
if mfCgrCfg.DefaultCategory != "call" { // Not configred, should be inherited from default
t.Error("DefaultCategory: ", mfCgrCfg.DefaultCategory)
}
}
func TestMfCdreDefaultInstance(t *testing.T) {
for _, prflName := range []string{"*default", "export1"} {
if _, hasIt := mfCgrCfg.CdreProfiles[prflName]; !hasIt {
t.Error("Cdre does not contain profile ", prflName)
}
}
prfl := "*default"
if mfCgrCfg.CdreProfiles[prfl].CdrFormat != "csv" {
t.Error("Default instance has cdrFormat: ", mfCgrCfg.CdreProfiles[prfl].CdrFormat)
}
if mfCgrCfg.CdreProfiles[prfl].DataUsageMultiplyFactor != 1024.0 {
t.Error("Default instance has cdrFormat: ", mfCgrCfg.CdreProfiles[prfl].DataUsageMultiplyFactor)
}
if len(mfCgrCfg.CdreProfiles[prfl].HeaderFields) != 0 {
t.Error("Default instance has number of header fields: ", len(mfCgrCfg.CdreProfiles[prfl].HeaderFields))
}
if len(mfCgrCfg.CdreProfiles[prfl].ContentFields) != 12 {
t.Error("Default instance has number of content fields: ", len(mfCgrCfg.CdreProfiles[prfl].ContentFields))
}
if mfCgrCfg.CdreProfiles[prfl].ContentFields[2].Tag != "Direction" {
t.Error("Unexpected headerField value: ", mfCgrCfg.CdreProfiles[prfl].ContentFields[2].Tag)
}
}
func TestMfCdreExport1Instance(t *testing.T) {
prfl := "export1"
if mfCgrCfg.CdreProfiles[prfl].CdrFormat != "csv" {
t.Error("Export1 instance has cdrFormat: ", mfCgrCfg.CdreProfiles[prfl].CdrFormat)
}
if mfCgrCfg.CdreProfiles[prfl].DataUsageMultiplyFactor != 1.0 {
t.Error("Export1 instance has DataUsageMultiplyFormat: ", mfCgrCfg.CdreProfiles[prfl].DataUsageMultiplyFactor)
}
if len(mfCgrCfg.CdreProfiles[prfl].HeaderFields) != 2 {
t.Error("Export1 instance has number of header fields: ", len(mfCgrCfg.CdreProfiles[prfl].HeaderFields))
}
if mfCgrCfg.CdreProfiles[prfl].HeaderFields[1].Tag != "RunId" {
t.Error("Unexpected headerField value: ", mfCgrCfg.CdreProfiles[prfl].HeaderFields[1].Tag)
}
if len(mfCgrCfg.CdreProfiles[prfl].ContentFields) != 9 {
t.Error("Export1 instance has number of content fields: ", len(mfCgrCfg.CdreProfiles[prfl].ContentFields))
}
if mfCgrCfg.CdreProfiles[prfl].ContentFields[2].Tag != "Account" {
t.Error("Unexpected headerField value: ", mfCgrCfg.CdreProfiles[prfl].ContentFields[2].Tag)
}
}

170
engine/actions_it_test.go Normal file
View File

@@ -0,0 +1,170 @@
// +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 <http://www.gnu.org/licenses/>
*/
package engine
import (
"flag"
"net/rpc"
"net/rpc/jsonrpc"
"path"
"strconv"
"testing"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
)
var actsLclCfg *config.CGRConfig
var actsLclRpc *rpc.Client
var actsLclCfgPath = path.Join(*dataDir, "conf", "samples", "actions")
var waitRater = flag.Int("wait_rater", 100, "Number of miliseconds to wait for rater to start and cache")
func TestActionsitInitCfg(t *testing.T) {
// Init config first
var err error
actsLclCfg, err = config.NewCGRConfigFromFolder(actsLclCfgPath)
if err != nil {
t.Error(err)
}
actsLclCfg.DataFolderPath = *dataDir // Share DataFolderPath through config towards StoreDb for Flush()
config.SetCgrConfig(actsLclCfg)
}
func TestActionsitInitCdrDb(t *testing.T) {
if err := InitStorDb(actsLclCfg); err != nil {
t.Fatal(err)
}
}
// Finds cgr-engine executable and starts it with default configuration
func TestActionsitStartEngine(t *testing.T) {
if _, err := StartEngine(actsLclCfgPath, *waitRater); err != nil {
t.Fatal(err)
}
}
// Connect rpc client to rater
func TestActionsitRpcConn(t *testing.T) {
var err error
time.Sleep(500 * time.Millisecond)
actsLclRpc, err = jsonrpc.Dial("tcp", actsLclCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
if err != nil {
t.Fatal(err)
}
}
func TestActionsitSetCdrlogDebit(t *testing.T) {
var reply string
attrsSetAccount := &utils.AttrSetAccount{Tenant: "cgrates.org", Account: "dan2904"}
if err := actsLclRpc.Call("ApierV1.SetAccount", attrsSetAccount, &reply); err != nil {
t.Error("Got error on ApierV1.SetAccount: ", err.Error())
} else if reply != utils.OK {
t.Errorf("Calling ApierV1.SetAccount received: %s", reply)
}
attrsAA := &utils.AttrSetActions{ActionsId: "ACTS_1", Actions: []*utils.TPAction{
&utils.TPAction{Identifier: DEBIT, BalanceType: utils.MONETARY, Units: "5", ExpiryTime: UNLIMITED, Weight: 20.0},
&utils.TPAction{Identifier: CDRLOG},
}}
if err := actsLclRpc.Call("ApierV2.SetActions", attrsAA, &reply); err != nil && err.Error() != utils.ErrExists.Error() {
t.Error("Got error on ApierV2.SetActions: ", err.Error())
} else if reply != utils.OK {
t.Errorf("Calling ApierV2.SetActions received: %s", reply)
}
attrsEA := &utils.AttrExecuteAction{Tenant: attrsSetAccount.Tenant, Account: attrsSetAccount.Account, ActionsId: attrsAA.ActionsId}
if err := actsLclRpc.Call("ApierV1.ExecuteAction", attrsEA, &reply); err != nil {
t.Error("Got error on ApierV1.ExecuteAction: ", err.Error())
} else if reply != utils.OK {
t.Errorf("Calling ApierV1.ExecuteAction received: %s", reply)
}
var rcvedCdrs []*ExternalCDR
if err := actsLclRpc.Call("ApierV2.GetCdrs", utils.RPCCDRsFilter{Sources: []string{CDRLOG}, Accounts: []string{attrsSetAccount.Account}}, &rcvedCdrs); err != nil {
t.Error("Unexpected error: ", err.Error())
} else if len(rcvedCdrs) != 1 {
t.Error("Unexpected number of CDRs returned: ", len(rcvedCdrs))
} else if rcvedCdrs[0].ToR != utils.MONETARY ||
rcvedCdrs[0].OriginHost != "127.0.0.1" ||
rcvedCdrs[0].Source != CDRLOG ||
rcvedCdrs[0].RequestType != utils.META_PREPAID ||
rcvedCdrs[0].Tenant != "cgrates.org" ||
rcvedCdrs[0].Account != "dan2904" ||
rcvedCdrs[0].Subject != "dan2904" ||
rcvedCdrs[0].Usage != "1" ||
rcvedCdrs[0].RunID != DEBIT ||
strconv.FormatFloat(rcvedCdrs[0].Cost, 'f', -1, 64) != attrsAA.Actions[0].Units {
t.Errorf("Received: %+v", rcvedCdrs[0])
}
}
func TestActionsitSetCdrlogTopup(t *testing.T) {
var reply string
attrsSetAccount := &utils.AttrSetAccount{Tenant: "cgrates.org", Account: "dan2905"}
if err := actsLclRpc.Call("ApierV1.SetAccount", attrsSetAccount, &reply); err != nil {
t.Error("Got error on ApierV1.SetAccount: ", err.Error())
} else if reply != utils.OK {
t.Errorf("Calling ApierV1.SetAccount received: %s", reply)
}
attrsAA := &utils.AttrSetActions{ActionsId: "ACTS_2", Actions: []*utils.TPAction{
&utils.TPAction{Identifier: TOPUP, BalanceType: utils.MONETARY, Units: "5", ExpiryTime: UNLIMITED, Weight: 20.0},
&utils.TPAction{Identifier: CDRLOG},
}}
if err := actsLclRpc.Call("ApierV2.SetActions", attrsAA, &reply); err != nil && err.Error() != utils.ErrExists.Error() {
t.Error("Got error on ApierV2.SetActions: ", err.Error())
} else if reply != utils.OK {
t.Errorf("Calling ApierV2.SetActions received: %s", reply)
}
attrsEA := &utils.AttrExecuteAction{Tenant: attrsSetAccount.Tenant, Account: attrsSetAccount.Account, ActionsId: attrsAA.ActionsId}
if err := actsLclRpc.Call("ApierV1.ExecuteAction", attrsEA, &reply); err != nil {
t.Error("Got error on ApierV1.ExecuteAction: ", err.Error())
} else if reply != utils.OK {
t.Errorf("Calling ApierV1.ExecuteAction received: %s", reply)
}
var rcvedCdrs []*ExternalCDR
if err := actsLclRpc.Call("ApierV2.GetCdrs", utils.RPCCDRsFilter{Sources: []string{CDRLOG}, Accounts: []string{attrsSetAccount.Account}}, &rcvedCdrs); err != nil {
t.Error("Unexpected error: ", err.Error())
} else if len(rcvedCdrs) != 1 {
t.Error("Unexpected number of CDRs returned: ", len(rcvedCdrs))
} else if rcvedCdrs[0].ToR != utils.MONETARY ||
rcvedCdrs[0].OriginHost != "127.0.0.1" ||
rcvedCdrs[0].Source != CDRLOG ||
rcvedCdrs[0].RequestType != utils.META_PREPAID ||
rcvedCdrs[0].Tenant != "cgrates.org" ||
rcvedCdrs[0].Account != "dan2905" ||
rcvedCdrs[0].Subject != "dan2905" ||
rcvedCdrs[0].Usage != "1" ||
rcvedCdrs[0].RunID != TOPUP ||
strconv.FormatFloat(-rcvedCdrs[0].Cost, 'f', -1, 64) != attrsAA.Actions[0].Units {
t.Errorf("Received: %+v", rcvedCdrs[0])
}
}
func TestActionsitStopCgrEngine(t *testing.T) {
if err := KillEngine(*waitRater); err != nil {
t.Error(err)
}
}

50
engine/cdr_it_test.go Normal file
View File

@@ -0,0 +1,50 @@
// +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 <http://www.gnu.org/licenses/>
*/
package engine
import (
"encoding/json"
"flag"
"testing"
"time"
"github.com/cgrates/cgrates/utils"
)
// Arguments received via test command
var testIT = flag.Bool("integration", false, "Perform the tests only on local test environment, not by default.")
var dataDir = flag.String("data_dir", "/usr/share/cgrates", "CGR data dir path here")
// Sample HttpJsonPost, more for usage purposes
func TestHttpJsonPost(t *testing.T) {
cdrOut := &ExternalCDR{CGRID: utils.Sha1("dsafdsaf", time.Date(2013, 11, 7, 8, 42, 20, 0, time.UTC).String()), OrderID: 123, ToR: utils.VOICE, OriginID: "dsafdsaf",
OriginHost: "192.168.1.1",
Source: utils.UNIT_TEST, RequestType: utils.META_RATED, Direction: "*out", Tenant: "cgrates.org",
Category: "call", Account: "account1", Subject: "tgooiscs0014", Destination: "1002",
SetupTime: time.Date(2013, 11, 7, 8, 42, 20, 0, time.UTC).String(), AnswerTime: time.Date(2013, 11, 7, 8, 42, 26, 0, time.UTC).String(),
RunID: utils.DEFAULT_RUNID,
Usage: "0.00000001", ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, Cost: 1.01,
}
jsn, _ := json.Marshal(cdrOut)
if _, err := utils.HttpJsonPost("http://localhost:8000", false, jsn); err == nil {
t.Error(err)
}
}

424
engine/loader_it_test.go Normal file
View File

@@ -0,0 +1,424 @@
// +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 <http://www.gnu.org/licenses/>
*/
package engine
import (
"flag"
"path"
"testing"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
)
// Globals used
var ratingDbCsv, ratingDbStor, ratingDbApier RatingStorage // Each ratingDb will have it's own sources to collect data
var accountDbCsv, accountDbStor, accountDbApier AccountingStorage // Each ratingDb will have it's own sources to collect data
var storDb LoadStorage
var lCfg *config.CGRConfig
var tpCsvScenario = flag.String("tp_scenario", "testtp", "Use this scenario folder to import tp csv data from")
// Create connection to ratingDb
// Will use 3 different datadbs in order to be able to see differences in data loaded
func TestConnDataDbs(t *testing.T) {
lCfg, _ = config.NewDefaultCGRConfig()
var err error
if ratingDbCsv, err = ConfigureRatingStorage(lCfg.TpDbType, lCfg.TpDbHost, lCfg.TpDbPort, "4", lCfg.TpDbUser, lCfg.TpDbPass, lCfg.DBDataEncoding, nil, 1); err != nil {
t.Fatal("Error on ratingDb connection: ", err.Error())
}
if ratingDbStor, err = ConfigureRatingStorage(lCfg.TpDbType, lCfg.TpDbHost, lCfg.TpDbPort, "5", lCfg.TpDbUser, lCfg.TpDbPass, lCfg.DBDataEncoding, nil, 1); err != nil {
t.Fatal("Error on ratingDb connection: ", err.Error())
}
if ratingDbApier, err = ConfigureRatingStorage(lCfg.TpDbType, lCfg.TpDbHost, lCfg.TpDbPort, "6", lCfg.TpDbUser, lCfg.TpDbPass, lCfg.DBDataEncoding, nil, 1); err != nil {
t.Fatal("Error on ratingDb connection: ", err.Error())
}
if accountDbCsv, err = ConfigureAccountingStorage(lCfg.DataDbType, lCfg.DataDbHost, lCfg.DataDbPort, "7",
lCfg.DataDbUser, lCfg.DataDbPass, lCfg.DBDataEncoding, nil, 1); err != nil {
t.Fatal("Error on ratingDb connection: ", err.Error())
}
if accountDbStor, err = ConfigureAccountingStorage(lCfg.DataDbType, lCfg.DataDbHost, lCfg.DataDbPort, "8",
lCfg.DataDbUser, lCfg.DataDbPass, lCfg.DBDataEncoding, nil, 1); err != nil {
t.Fatal("Error on ratingDb connection: ", err.Error())
}
if accountDbApier, err = ConfigureAccountingStorage(lCfg.DataDbType, lCfg.DataDbHost, lCfg.DataDbPort, "9",
lCfg.DataDbUser, lCfg.DataDbPass, lCfg.DBDataEncoding, nil, 1); err != nil {
t.Fatal("Error on ratingDb connection: ", err.Error())
}
for _, db := range []Storage{ratingDbCsv, ratingDbStor, ratingDbApier, accountDbCsv, accountDbStor, accountDbApier} {
if err = db.Flush(""); err != nil {
t.Fatal("Error when flushing datadb")
}
}
}
// Create/reset storage tariff plan tables, used as database connectin establishment also
func TestCreateStorTpTables(t *testing.T) {
db, err := NewMySQLStorage(lCfg.StorDBHost, lCfg.StorDBPort, lCfg.StorDBName, lCfg.StorDBUser, lCfg.StorDBPass, lCfg.StorDBMaxOpenConns, lCfg.StorDBMaxIdleConns)
if err != nil {
t.Error("Error on opening database connection: ", err)
return
} else {
storDb = db
}
// Creating the table serves also as reset since there is a drop prior to create
if err := db.CreateTablesFromScript(path.Join(*dataDir, "storage", "mysql", utils.CREATE_TARIFFPLAN_TABLES_SQL)); err != nil {
t.Error("Error on db creation: ", err.Error())
return // No point in going further
}
}
// Loads data from csv files in tp scenario to ratingDbCsv
func TestLoadFromCSV(t *testing.T) {
/*var err error
for fn, v := range FileValidators {
if err = ValidateCSVData(path.Join(*dataDir, "tariffplans", *tpCsvScenario, fn), v.Rule); err != nil {
t.Error("Failed validating data: ", err.Error())
}
}*/
loader := NewTpReader(ratingDbCsv, accountDbCsv, NewFileCSVStorage(utils.CSV_SEP,
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.DESTINATIONS_CSV),
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.TIMINGS_CSV),
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.RATES_CSV),
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.DESTINATION_RATES_CSV),
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.RATING_PLANS_CSV),
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.RATING_PROFILES_CSV),
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.SHARED_GROUPS_CSV),
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.LCRS_CSV),
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.ACTIONS_CSV),
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.ACTION_PLANS_CSV),
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.ACTION_TRIGGERS_CSV),
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.ACCOUNT_ACTIONS_CSV),
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.DERIVED_CHARGERS_CSV),
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.CDR_STATS_CSV),
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.USERS_CSV),
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.ALIASES_CSV),
path.Join(*dataDir, "tariffplans", *tpCsvScenario, utils.ResourceLimitsCsv),
), "", "")
if err = loader.LoadDestinations(); err != nil {
t.Error("Failed loading destinations: ", err.Error())
}
if err = loader.LoadTimings(); err != nil {
t.Error("Failed loading timings: ", err.Error())
}
if err = loader.LoadRates(); err != nil {
t.Error("Failed loading rates: ", err.Error())
}
if err = loader.LoadDestinationRates(); err != nil {
t.Error("Failed loading destination rates: ", err.Error())
}
if err = loader.LoadRatingPlans(); err != nil {
t.Error("Failed loading rating plans: ", err.Error())
}
if err = loader.LoadRatingProfiles(); err != nil {
t.Error("Failed loading rating profiles: ", err.Error())
}
if err = loader.LoadActions(); err != nil {
t.Error("Failed loading actions: ", err.Error())
}
if err = loader.LoadActionPlans(); err != nil {
t.Error("Failed loading action timings: ", err.Error())
}
if err = loader.LoadActionTriggers(); err != nil {
t.Error("Failed loading action triggers: ", err.Error())
}
if err = loader.LoadAccountActions(); err != nil {
t.Error("Failed loading account actions: ", err.Error())
}
if err = loader.LoadDerivedChargers(); err != nil {
t.Error("Failed loading derived chargers: ", err.Error())
}
if err = loader.LoadLCRs(); err != nil {
t.Error("Failed loading lcr rules: ", err.Error())
}
if err = loader.LoadUsers(); err != nil {
t.Error("Failed loading users: ", err.Error())
}
if err = loader.LoadAliases(); err != nil {
t.Error("Failed loading aliases: ", err.Error())
}
if err = loader.LoadResourceLimits(); err != nil {
t.Error("Failed loading resource limits: ", err.Error())
}
if err := loader.WriteToDatabase(true, false, false); err != nil {
t.Error("Could not write data into ratingDb: ", err.Error())
}
}
// Imports data from csv files in tpScenario to storDb
func TestImportToStorDb(t *testing.T) {
csvImporter := TPCSVImporter{
TPid: utils.TEST_SQL,
StorDb: storDb,
DirPath: path.Join(*dataDir, "tariffplans", *tpCsvScenario),
Sep: utils.CSV_SEP,
Verbose: false,
ImportId: utils.TEST_SQL}
if err := csvImporter.Run(); err != nil {
t.Error("Error when importing tpdata to storDb: ", err)
}
if tpids, err := storDb.GetTpIds(); err != nil {
t.Error("Error when querying storDb for imported data: ", err)
} else if len(tpids) != 1 || tpids[0] != utils.TEST_SQL {
t.Errorf("Data in storDb is different than expected %v", tpids)
}
}
// Loads data from storDb into ratingDb
func TestLoadFromStorDb(t *testing.T) {
loader := NewTpReader(ratingDbStor, accountDbStor, storDb, utils.TEST_SQL, "")
if err := loader.LoadDestinations(); err != nil {
t.Error("Failed loading destinations: ", err.Error())
}
if err := loader.LoadTimings(); err != nil {
t.Error("Failed loading timings: ", err.Error())
}
if err := loader.LoadRates(); err != nil {
t.Error("Failed loading rates: ", err.Error())
}
if err := loader.LoadDestinationRates(); err != nil {
t.Error("Failed loading destination rates: ", err.Error())
}
if err := loader.LoadRatingPlans(); err != nil {
t.Error("Failed loading rating plans: ", err.Error())
}
if err := loader.LoadRatingProfiles(); err != nil {
t.Error("Failed loading rating profiles: ", err.Error())
}
if err := loader.LoadActions(); err != nil {
t.Error("Failed loading actions: ", err.Error())
}
if err := loader.LoadActionPlans(); err != nil {
t.Error("Failed loading action timings: ", err.Error())
}
if err := loader.LoadActionTriggers(); err != nil {
t.Error("Failed loading action triggers: ", err.Error())
}
if err := loader.LoadAccountActions(); err != nil {
t.Error("Failed loading account actions: ", err.Error())
}
if err := loader.LoadDerivedChargers(); err != nil {
t.Error("Failed loading derived chargers: ", err.Error())
}
if err := loader.LoadLCRs(); err != nil {
t.Error("Failed loading lcr rules: ", err.Error())
}
if err := loader.LoadUsers(); err != nil {
t.Error("Failed loading users: ", err.Error())
}
if err := loader.LoadAliases(); err != nil {
t.Error("Failed loading aliases: ", err.Error())
}
}
func TestLoadIndividualProfiles(t *testing.T) {
loader := NewTpReader(ratingDbApier, accountDbApier, storDb, utils.TEST_SQL, "")
// Load ratingPlans. This will also set destination keys
if ratingPlans, err := storDb.GetTpRatingPlans(utils.TEST_SQL, "", nil); err != nil {
t.Fatal("Could not retrieve rating plans")
} else {
rpls, err := TpRatingPlans(ratingPlans).GetRatingPlans()
if err != nil {
t.Fatal("Could not convert rating plans")
}
for tag := range rpls {
if loaded, err := loader.LoadRatingPlansFiltered(tag); err != nil {
t.Fatalf("Could not load ratingPlan for tag: %s, error: %s", tag, err.Error())
} else if !loaded {
t.Fatal("Cound not find ratingPLan with id:", tag)
}
}
}
// Load rating profiles
loadId := utils.CSV_LOAD + "_" + utils.TEST_SQL
if ratingProfiles, err := storDb.GetTpRatingProfiles(&TpRatingProfile{Tpid: utils.TEST_SQL, Loadid: loadId}); err != nil {
t.Fatal("Could not retrieve rating profiles, error: ", err.Error())
} else if len(ratingProfiles) == 0 {
t.Fatal("Could not retrieve rating profiles")
} else {
rpfs, err := TpRatingProfiles(ratingProfiles).GetRatingProfiles()
if err != nil {
t.Fatal("Could not convert rating profiles")
}
for rpId := range rpfs {
rp, _ := utils.NewTPRatingProfileFromKeyId(utils.TEST_SQL, loadId, rpId)
mrp := APItoModelRatingProfile(rp)
if err := loader.LoadRatingProfilesFiltered(&mrp[0]); err != nil {
t.Fatalf("Could not load ratingProfile with id: %s, error: %s", rpId, err.Error())
}
}
}
// Load derived chargers
loadId = utils.CSV_LOAD + "_" + utils.TEST_SQL
if derivedChargers, err := storDb.GetTpDerivedChargers(&TpDerivedCharger{Tpid: utils.TEST_SQL, Loadid: loadId}); err != nil {
t.Fatal("Could not retrieve derived chargers, error: ", err.Error())
} else if len(derivedChargers) == 0 {
t.Fatal("Could not retrieve derived chargers")
} else {
dcs, err := TpDerivedChargers(derivedChargers).GetDerivedChargers()
if err != nil {
t.Fatal("Could not convert derived chargers")
}
for dcId := range dcs {
mdc := &TpDerivedCharger{Tpid: utils.TEST_SQL, Loadid: loadId}
mdc.SetDerivedChargersId(dcId)
if err := loader.LoadDerivedChargersFiltered(mdc, true); err != nil {
t.Fatalf("Could not load derived charger with id: %s, error: %s", dcId, err.Error())
}
}
}
// Load cdr stats
//loadId = utils.CSV_LOAD + "_" + utils.TEST_SQL
if cdrStats, err := storDb.GetTpCdrStats(utils.TEST_SQL, ""); err != nil {
t.Fatal("Could not retrieve cdr stats, error: ", err.Error())
} else if len(cdrStats) == 0 {
t.Fatal("Could not retrieve cdr stats")
} else {
cds, err := TpCdrStats(cdrStats).GetCdrStats()
if err != nil {
t.Fatal("Could not convert cdr stats")
}
for id := range cds {
if err := loader.LoadCdrStatsFiltered(id, true); err != nil {
t.Fatalf("Could not load cdr stats with id: %s, error: %s", id, err.Error())
}
}
}
// Load users
if users, err := storDb.GetTpUsers(&TpUser{Tpid: utils.TEST_SQL}); err != nil {
t.Fatal("Could not retrieve users, error: ", err.Error())
} else if len(users) == 0 {
t.Fatal("Could not retrieve users")
} else {
for _, usr := range users {
if found, err := loader.LoadUsersFiltered(&usr); found && err != nil {
t.Fatalf("Could not user with id: %s, error: %s", usr.GetId(), err.Error())
}
}
}
// Load aliases
if aliases, err := storDb.GetTpAliases(&TpAlias{Tpid: utils.TEST_SQL}); err != nil {
t.Fatal("Could not retrieve aliases, error: ", err.Error())
} else if len(aliases) == 0 {
t.Fatal("Could not retrieve aliases")
} else {
for _, al := range aliases {
if found, err := loader.LoadAliasesFiltered(&al); found && err != nil {
t.Fatalf("Could not load aliase with id: %s, error: %s", al.GetId(), err.Error())
}
}
}
// Load account actions
if accountActions, err := storDb.GetTpAccountActions(&TpAccountAction{Tpid: utils.TEST_SQL, Loadid: loadId}); err != nil {
t.Fatal("Could not retrieve account action profiles, error: ", err.Error())
} else if len(accountActions) == 0 {
t.Error("No account actions")
} else {
aas, err := TpAccountActions(accountActions).GetAccountActions()
if err != nil {
t.Fatal("Could not convert account actions")
}
for aaId := range aas {
aa, _ := utils.NewTPAccountActionsFromKeyId(utils.TEST_SQL, loadId, aaId)
maa := APItoModelAccountAction(aa)
if err := loader.LoadAccountActionsFiltered(maa); err != nil {
t.Fatalf("Could not load account actions with id: %s, error: %s", aaId, err.Error())
}
}
}
}
/*
// Compares previously loaded data from csv and stor to be identical, redis specific tests
func TestMatchLoadCsvWithStorRating(t *testing.T) {
rsCsv, redisDb := ratingDbCsv.(*RedisStorage)
if !redisDb {
return // We only support these tests for redis
}
rsStor := ratingDbStor.(*RedisStorage)
rsApier := ratingDbApier.(*RedisStorage)
keysCsv, err := rsCsv.db.Cmd("KEYS", "*").List()
if err != nil {
t.Fatal("Failed querying redis keys for csv data")
}
for _, key := range keysCsv {
var refVal []byte
for idx, rs := range []*RedisStorage{rsCsv, rsStor, rsApier} {
if key == utils.TASKS_KEY || strings.HasPrefix(key, utils.ACTION_PLAN_PREFIX) { // action plans are not consistent
continue
}
qVal, err := rs.db.Cmd("GET", key).Bytes()
if err != nil {
t.Fatalf("Run: %d, could not retrieve key %s, error: %s", idx, key, err.Error())
}
if idx == 0 { // Only compare at second iteration, first one is to set reference value
refVal = qVal
continue
}
if len(refVal) != len(qVal) {
t.Errorf("Missmatched data for key: %s\n\t reference val: %s \n\t retrieved val: %s\n on iteration: %d", key, refVal, qVal, idx)
}
}
}
}
func TestMatchLoadCsvWithStorAccounting(t *testing.T) {
rsCsv, redisDb := accountDbCsv.(*RedisStorage)
if !redisDb {
return // We only support these tests for redis
}
rsStor := accountDbStor.(*RedisStorage)
rsApier := accountDbApier.(*RedisStorage)
keysCsv, err := rsCsv.db.Cmd("KEYS", "*").List()
if err != nil {
t.Fatal("Failed querying redis keys for csv data")
}
for _, key := range keysCsv {
var refVal []byte
if key == "load_history" {
continue
}
for idx, rs := range []*RedisStorage{rsCsv, rsStor, rsApier} {
qVal, err := rs.db.Cmd("GET", key).Bytes()
if err != nil {
t.Fatalf("Run: %d, could not retrieve key %s, error: %s", idx, key, err.Error())
}
if idx == 0 { // Only compare at second iteration, first one is to set reference value
refVal = qVal
continue
}
if len(refVal) != len(qVal) {
t.Errorf("Missmatched data for key: %s\n\t, reference val: %s \n\t retrieved value: %s\n on iteration: %d", key, refVal, qVal, idx)
}
}
}
}
*/

View File

@@ -0,0 +1,73 @@
// +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 <http://www.gnu.org/licenses/>
*/
package utils
import (
"encoding/json"
"flag"
"io/ioutil"
"os"
"reflect"
"testing"
"time"
)
var testIT = flag.Bool("integration", false, "Perform the tests only on local test environment, not by default.")
type TestContent struct {
Var1 string
Var2 string
}
func TestHttpJsonPoster(t *testing.T) {
content := &TestContent{Var1: "Val1", Var2: "Val2"}
jsn, _ := json.Marshal(content)
filePath := "/tmp/cgr_test_http_poster.json"
if _, err := NewHTTPPoster(true, time.Duration(2*time.Second)).Post("http://localhost:8080/invalid", CONTENT_JSON, jsn, 3, filePath); err != nil {
t.Error(err)
}
if readBytes, err := ioutil.ReadFile(filePath); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(jsn, readBytes) {
t.Errorf("Expecting: %q, received: %q", string(jsn), string(readBytes))
}
if err := os.Remove(filePath); err != nil {
t.Error("Failed removing file: ", filePath)
}
}
func TestHttpBytesPoster(t *testing.T) {
content := []byte(`Test
Test2
`)
filePath := "/tmp/test_http_poster.http"
if _, err := NewHTTPPoster(true, time.Duration(2*time.Second)).Post("http://localhost:8080/invalid", CONTENT_TEXT, content, 3, filePath); err != nil {
t.Error(err)
}
if readBytes, err := ioutil.ReadFile(filePath); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(content, readBytes) {
t.Errorf("Expecting: %q, received: %q", string(content), string(readBytes))
}
if err := os.Remove(filePath); err != nil {
t.Error("Failed removing file: ", filePath)
}
}