mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Update HTTP Agent tests with subtests
This commit is contained in:
committed by
Dan Christian Bogos
parent
523a41ba56
commit
8be6c8c8e8
@@ -45,9 +45,22 @@ var (
|
||||
haRPC *rpc.Client
|
||||
httpC *http.Client // so we can cache the connection
|
||||
err error
|
||||
isTls bool
|
||||
)
|
||||
|
||||
func TestHAitInitCfg(t *testing.T) {
|
||||
var sTestsHA = []func(t *testing.T){
|
||||
testHAitResetDB,
|
||||
testHAitStartEngine,
|
||||
testHAitApierRpcConn,
|
||||
testHAitTPFromFolder,
|
||||
testHAitAuthDryRun,
|
||||
testHAitAuth1001,
|
||||
testHAitCDRmtcall,
|
||||
testHAitCDRmtcall2,
|
||||
testHAitStopEngine,
|
||||
}
|
||||
|
||||
func TestHAitSimple(t *testing.T) {
|
||||
haCfgPath = path.Join(*dataDir, "conf", "samples", "httpagent")
|
||||
// Init config first
|
||||
var err error
|
||||
@@ -58,160 +71,12 @@ func TestHAitInitCfg(t *testing.T) {
|
||||
haCfg.DataFolderPath = *dataDir // Share DataFolderPath through config towards StoreDb for Flush()
|
||||
config.SetCgrConfig(haCfg)
|
||||
httpC = new(http.Client)
|
||||
}
|
||||
|
||||
// Remove data in both rating and accounting db
|
||||
func TestHAitResetDB(t *testing.T) {
|
||||
if err := engine.InitDataDb(haCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := engine.InitStorDb(haCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
for _, stest := range sTestsHA {
|
||||
t.Run("httpagent", stest)
|
||||
}
|
||||
}
|
||||
|
||||
// Start CGR Engine
|
||||
func TestHAitStartEngine(t *testing.T) {
|
||||
if _, err := engine.StopStartEngine(haCfgPath, *waitRater); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Connect rpc client to rater
|
||||
func TestHAitApierRpcConn(t *testing.T) {
|
||||
var err error
|
||||
haRPC, err = jsonrpc.Dial("tcp", haCfg.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 TestHAitTPFromFolder(t *testing.T) {
|
||||
attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "oldtutorial")}
|
||||
var loadInst utils.LoadInstance
|
||||
if err := haRPC.Call("ApierV2.LoadTariffPlanFromFolder", attrs, &loadInst); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
time.Sleep(time.Duration(*waitRater) * time.Millisecond) // Give time for scheduler to execute topups
|
||||
}
|
||||
|
||||
func TestHAitAuthDryRun(t *testing.T) {
|
||||
reqUrl := fmt.Sprintf("http://%s%s?request_type=OutboundAUTH&CallID=123456&Msisdn=497700056231&Imsi=2343000000000123&Destination=491239440004&MSRN=0102220233444488999&ProfileID=1&AgentID=176&GlobalMSISDN=497700056129&GlobalIMSI=214180000175129&ICCID=8923418450000089629&MCC=234&MNC=10&calltype=callback",
|
||||
haCfg.HTTPListen, haCfg.HttpAgentCfg()[0].Url)
|
||||
rply, err := httpC.Get(reqUrl)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
eXml := []byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<response>
|
||||
<Allow>1</Allow>
|
||||
<MaxDuration>1200</MaxDuration>
|
||||
</response>`)
|
||||
if body, err := ioutil.ReadAll(rply.Body); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(eXml, body) {
|
||||
t.Errorf("expecting: <%s>, received: <%s>", string(eXml), string(body))
|
||||
}
|
||||
rply.Body.Close()
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
|
||||
func TestHAitAuth1001(t *testing.T) {
|
||||
reqUrl := fmt.Sprintf("http://%s%s?request_type=OutboundAUTH&CallID=123456&Msisdn=1001&Imsi=2343000000000123&Destination=1002&MSRN=0102220233444488999&ProfileID=1&AgentID=176&GlobalMSISDN=497700056129&GlobalIMSI=214180000175129&ICCID=8923418450000089629&MCC=234&MNC=10&calltype=callback",
|
||||
haCfg.HTTPListen, haCfg.HttpAgentCfg()[0].Url)
|
||||
rply, err := httpC.Get(reqUrl)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
eXml := []byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<response>
|
||||
<Allow>1</Allow>
|
||||
<MaxDuration>10800</MaxDuration>
|
||||
</response>`)
|
||||
if body, err := ioutil.ReadAll(rply.Body); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(eXml, body) {
|
||||
t.Errorf("expecting: %s, received: %s", string(eXml), string(body))
|
||||
}
|
||||
rply.Body.Close()
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
|
||||
func TestHAitCDRmtcall(t *testing.T) {
|
||||
reqUrl := fmt.Sprintf("http://%s%s?request_type=MTCALL_CDR×tamp=2018-08-14%%2012:03:22&call_date=2018-0814%%2012:00:49&transactionid=10000&CDR_ID=123456&carrierid=1&mcc=0&mnc=0&imsi=434180000000000&msisdn=1001&destination=1002&leg=C&leg_duration=185&reseller_charge=11.1605&client_charge=0.0000&user_charge=22.0000&IOT=0&user_balance=10.00&cli=%%2B498702190000&polo=0.0100&ddi_map=N",
|
||||
haCfg.HTTPListen, haCfg.HttpAgentCfg()[0].Url)
|
||||
rply, err := httpC.Get(reqUrl)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
eXml := []byte(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<CDR_RESPONSE>
|
||||
<CDR_ID>123456</CDR_ID>
|
||||
<CDR_STATUS>1</CDR_STATUS>
|
||||
</CDR_RESPONSE>`)
|
||||
if body, err := ioutil.ReadAll(rply.Body); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(eXml, body) {
|
||||
t.Errorf("expecting: <%s>, received: <%s>", string(eXml), string(body))
|
||||
}
|
||||
rply.Body.Close()
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
var cdrs []*engine.ExternalCDR
|
||||
req := utils.RPCCDRsFilter{RunIDs: []string{utils.META_DEFAULT}}
|
||||
if err := haRPC.Call("ApierV2.GetCdrs", req, &cdrs); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if len(cdrs) != 1 {
|
||||
t.Error("Unexpected number of CDRs returned: ", len(cdrs))
|
||||
} else {
|
||||
if cdrs[0].Usage != "3m5s" { // should be 1 but maxUsage returns rounded version
|
||||
t.Errorf("Unexpected CDR Usage received, cdr: %s ", utils.ToJSON(cdrs[0]))
|
||||
}
|
||||
if cdrs[0].Cost != 0.2188 {
|
||||
t.Errorf("Unexpected CDR Cost received, cdr: %+v ", cdrs[0].Cost)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHAitCDRmtcall2(t *testing.T) {
|
||||
xmlBody := `<?xml version="1.0" encoding="utf-8"?><complete-datasession-notification callid="48981764"><createtime>2005-08-26T14:17:34</createtime><reference>Data</reference><userid>528594</userid><username>447700086788</username><customerid>510163</customerid><companyname>Silliname</companyname><totalcost amount="0.1400" currency="USD">0.1400</totalcost><agenttotalcost amount="0.1400" currency="USD">0.1400</agenttotalcost><agentid>234</agentid><callleg calllegid="89357336"><number>447700086788</number><description>China, Peoples Republic of - China Unicom (CU-GSM)</description><mcc>460</mcc><mnc>001</mnc><seconds>32</seconds><bytes>4558</bytes><permegabyterate currency="USD">1.3330</permegabyterate><cost amount="0.1400" currency="USD">0.1400</cost><agentpermegabyterate currency="USD">1.3330</agentpermegabyterate><agentcost amount="0.1400"currency="USD">0.1400</agentcost></callleg></complete-datasession-notification>`
|
||||
|
||||
url := fmt.Sprintf("http://%s%s", haCfg.HTTPListen, haCfg.HttpAgentCfg()[1].Url)
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(xmlBody)))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
req.Header.Add("Content-Type", "application/xml; charset=utf-8")
|
||||
resp, err := httpC.Do(req)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
var cdrs []*engine.ExternalCDR
|
||||
fltr := utils.RPCCDRsFilter{RunIDs: []string{utils.META_DEFAULT}, Accounts: []string{"447700086788"}}
|
||||
if err := haRPC.Call("ApierV2.GetCdrs", fltr, &cdrs); err != nil {
|
||||
t.Error("Unexpected error: ", err.Error())
|
||||
} else if len(cdrs) != 1 {
|
||||
t.Error("Unexpected number of CDRs returned: ", len(cdrs))
|
||||
} else {
|
||||
if cdrs[0].Usage != "4558" { // should be 1 but maxUsage returns rounded version
|
||||
t.Errorf("Unexpected CDR Usage received, cdr: %s ", utils.ToJSON(cdrs[0]))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHAitStopEngine(t *testing.T) {
|
||||
if err := engine.KillEngine(*waitRater); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
//Start second tests
|
||||
|
||||
func TestHA2itInitCfg(t *testing.T) {
|
||||
func TestHA2itWithTls(t *testing.T) {
|
||||
haCfgPath = path.Join(*dataDir, "conf", "samples", "httpagenttls")
|
||||
// Init config first
|
||||
var err error
|
||||
@@ -244,10 +109,14 @@ func TestHA2itInitCfg(t *testing.T) {
|
||||
}
|
||||
transport := &http.Transport{TLSClientConfig: tlsConfig}
|
||||
httpC = &http.Client{Transport: transport}
|
||||
isTls = true
|
||||
for _, stest := range sTestsHA {
|
||||
t.Run("httpagenttls", stest)
|
||||
}
|
||||
}
|
||||
|
||||
// Remove data in both rating and accounting db
|
||||
func TestHA2itResetDB(t *testing.T) {
|
||||
func testHAitResetDB(t *testing.T) {
|
||||
if err := engine.InitDataDb(haCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -257,14 +126,14 @@ func TestHA2itResetDB(t *testing.T) {
|
||||
}
|
||||
|
||||
// Start CGR Engine
|
||||
func TestHA2itStartEngine(t *testing.T) {
|
||||
func testHAitStartEngine(t *testing.T) {
|
||||
if _, err := engine.StopStartEngine(haCfgPath, *waitRater); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Connect rpc client to rater
|
||||
func TestHA2itApierRpcConn(t *testing.T) {
|
||||
func testHAitApierRpcConn(t *testing.T) {
|
||||
var err error
|
||||
haRPC, err = jsonrpc.Dial("tcp", haCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
|
||||
if err != nil {
|
||||
@@ -273,7 +142,7 @@ func TestHA2itApierRpcConn(t *testing.T) {
|
||||
}
|
||||
|
||||
// Load the tariff plan, creating accounts and their balances
|
||||
func TestHA2itTPFromFolder(t *testing.T) {
|
||||
func testHAitTPFromFolder(t *testing.T) {
|
||||
attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "oldtutorial")}
|
||||
var loadInst utils.LoadInstance
|
||||
if err := haRPC.Call("ApierV2.LoadTariffPlanFromFolder", attrs, &loadInst); err != nil {
|
||||
@@ -282,9 +151,15 @@ func TestHA2itTPFromFolder(t *testing.T) {
|
||||
time.Sleep(time.Duration(*waitRater) * time.Millisecond) // Give time for scheduler to execute topups
|
||||
}
|
||||
|
||||
func TestHA2itAuthDryRun(t *testing.T) {
|
||||
reqUrl := fmt.Sprintf("https://%s%s?request_type=OutboundAUTH&CallID=123456&Msisdn=497700056231&Imsi=2343000000000123&Destination=491239440004&MSRN=0102220233444488999&ProfileID=1&AgentID=176&GlobalMSISDN=497700056129&GlobalIMSI=214180000175129&ICCID=8923418450000089629&MCC=234&MNC=10&calltype=callback",
|
||||
haCfg.HTTPTLSListen, haCfg.HttpAgentCfg()[0].Url)
|
||||
func testHAitAuthDryRun(t *testing.T) {
|
||||
httpConst := "http"
|
||||
addr := haCfg.HTTPListen
|
||||
if isTls {
|
||||
addr = haCfg.HTTPTLSListen
|
||||
httpConst = "https"
|
||||
}
|
||||
reqUrl := fmt.Sprintf("%s://%s%s?request_type=OutboundAUTH&CallID=123456&Msisdn=497700056231&Imsi=2343000000000123&Destination=491239440004&MSRN=0102220233444488999&ProfileID=1&AgentID=176&GlobalMSISDN=497700056129&GlobalIMSI=214180000175129&ICCID=8923418450000089629&MCC=234&MNC=10&calltype=callback",
|
||||
httpConst, addr, haCfg.HttpAgentCfg()[0].Url)
|
||||
rply, err := httpC.Get(reqUrl)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
@@ -303,9 +178,16 @@ func TestHA2itAuthDryRun(t *testing.T) {
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
|
||||
func TestHA2itAuth1001(t *testing.T) {
|
||||
reqUrl := fmt.Sprintf("https://%s%s?request_type=OutboundAUTH&CallID=123456&Msisdn=1001&Imsi=2343000000000123&Destination=1002&MSRN=0102220233444488999&ProfileID=1&AgentID=176&GlobalMSISDN=497700056129&GlobalIMSI=214180000175129&ICCID=8923418450000089629&MCC=234&MNC=10&calltype=callback",
|
||||
haCfg.HTTPTLSListen, haCfg.HttpAgentCfg()[0].Url)
|
||||
func testHAitAuth1001(t *testing.T) {
|
||||
httpConst := "http"
|
||||
addr := haCfg.HTTPListen
|
||||
if isTls {
|
||||
addr = haCfg.HTTPTLSListen
|
||||
httpConst = "https"
|
||||
|
||||
}
|
||||
reqUrl := fmt.Sprintf("%s://%s%s?request_type=OutboundAUTH&CallID=123456&Msisdn=1001&Imsi=2343000000000123&Destination=1002&MSRN=0102220233444488999&ProfileID=1&AgentID=176&GlobalMSISDN=497700056129&GlobalIMSI=214180000175129&ICCID=8923418450000089629&MCC=234&MNC=10&calltype=callback",
|
||||
httpConst, addr, haCfg.HttpAgentCfg()[0].Url)
|
||||
rply, err := httpC.Get(reqUrl)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
@@ -324,9 +206,15 @@ func TestHA2itAuth1001(t *testing.T) {
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
|
||||
func TestHA2itCDRmtcall(t *testing.T) {
|
||||
reqUrl := fmt.Sprintf("https://%s%s?request_type=MTCALL_CDR×tamp=2018-08-14%%2012:03:22&call_date=2018-0814%%2012:00:49&transactionid=10000&CDR_ID=123456&carrierid=1&mcc=0&mnc=0&imsi=434180000000000&msisdn=1001&destination=1002&leg=C&leg_duration=185&reseller_charge=11.1605&client_charge=0.0000&user_charge=22.0000&IOT=0&user_balance=10.00&cli=%%2B498702190000&polo=0.0100&ddi_map=N",
|
||||
haCfg.HTTPTLSListen, haCfg.HttpAgentCfg()[0].Url)
|
||||
func testHAitCDRmtcall(t *testing.T) {
|
||||
httpConst := "http"
|
||||
addr := haCfg.HTTPListen
|
||||
if isTls {
|
||||
addr = haCfg.HTTPTLSListen
|
||||
httpConst = "https"
|
||||
}
|
||||
reqUrl := fmt.Sprintf("%s://%s%s?request_type=MTCALL_CDR×tamp=2018-08-14%%2012:03:22&call_date=2018-0814%%2012:00:49&transactionid=10000&CDR_ID=123456&carrierid=1&mcc=0&mnc=0&imsi=434180000000000&msisdn=1001&destination=1002&leg=C&leg_duration=185&reseller_charge=11.1605&client_charge=0.0000&user_charge=22.0000&IOT=0&user_balance=10.00&cli=%%2B498702190000&polo=0.0100&ddi_map=N",
|
||||
httpConst, addr, haCfg.HttpAgentCfg()[0].Url)
|
||||
rply, err := httpC.Get(reqUrl)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
@@ -359,10 +247,15 @@ func TestHA2itCDRmtcall(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHA2itCDRmtcall2(t *testing.T) {
|
||||
func testHAitCDRmtcall2(t *testing.T) {
|
||||
xmlBody := `<?xml version="1.0" encoding="utf-8"?><complete-datasession-notification callid="48981764"><createtime>2005-08-26T14:17:34</createtime><reference>Data</reference><userid>528594</userid><username>447700086788</username><customerid>510163</customerid><companyname>Silliname</companyname><totalcost amount="0.1400" currency="USD">0.1400</totalcost><agenttotalcost amount="0.1400" currency="USD">0.1400</agenttotalcost><agentid>234</agentid><callleg calllegid="89357336"><number>447700086788</number><description>China, Peoples Republic of - China Unicom (CU-GSM)</description><mcc>460</mcc><mnc>001</mnc><seconds>32</seconds><bytes>4558</bytes><permegabyterate currency="USD">1.3330</permegabyterate><cost amount="0.1400" currency="USD">0.1400</cost><agentpermegabyterate currency="USD">1.3330</agentpermegabyterate><agentcost amount="0.1400"currency="USD">0.1400</agentcost></callleg></complete-datasession-notification>`
|
||||
|
||||
url := fmt.Sprintf("https://%s%s", haCfg.HTTPTLSListen, haCfg.HttpAgentCfg()[1].Url)
|
||||
httpConst := "http"
|
||||
addr := haCfg.HTTPListen
|
||||
if isTls {
|
||||
addr = haCfg.HTTPTLSListen
|
||||
httpConst = "https"
|
||||
}
|
||||
url := fmt.Sprintf("%s://%s%s", httpConst, addr, haCfg.HttpAgentCfg()[1].Url)
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(xmlBody)))
|
||||
if err != nil {
|
||||
@@ -389,7 +282,7 @@ func TestHA2itCDRmtcall2(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHA2itStopEngine(t *testing.T) {
|
||||
func testHAitStopEngine(t *testing.T) {
|
||||
if err := engine.KillEngine(*waitRater); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user