mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Revise and add new unit tests on agents
This commit is contained in:
committed by
Dan Christian Bogos
parent
37161e8ea4
commit
6df400cc7e
@@ -29,6 +29,7 @@ import (
|
||||
"github.com/cgrates/cgrates/sessions"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
"github.com/cgrates/rpcclient"
|
||||
"github.com/fiorix/go-diameter/v4/diam"
|
||||
)
|
||||
|
||||
func TestDAsSessionSClientIface(t *testing.T) {
|
||||
@@ -533,3 +534,59 @@ func TestProcessRequest(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestDiamAgentV1WarnDisconnect(t *testing.T) {
|
||||
agent := &DiameterAgent{}
|
||||
err := agent.V1WarnDisconnect(nil, nil, nil)
|
||||
if err != utils.ErrNotImplemented {
|
||||
t.Errorf("Expected ErrNotImplemented, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiamAgentV1GetActiveSessionIDs(t *testing.T) {
|
||||
agent := &DiameterAgent{}
|
||||
err := agent.V1GetActiveSessionIDs(nil, "someParameter", nil)
|
||||
if err != utils.ErrNotImplemented {
|
||||
t.Errorf("Expected ErrNotImplemented, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestV1DisconnectPeer(t *testing.T) {
|
||||
agent := &DiameterAgent{
|
||||
dpa: make(map[string]chan *diam.Message),
|
||||
peers: make(map[string]diam.Conn),
|
||||
}
|
||||
err := agent.V1DisconnectPeer(nil, nil, nil)
|
||||
if err != utils.ErrMandatoryIeMissing {
|
||||
t.Errorf("Expected ErrMandatoryIeMissing, got: %v", err)
|
||||
}
|
||||
args := &utils.DPRArgs{
|
||||
DisconnectCause: 5,
|
||||
}
|
||||
err = agent.V1DisconnectPeer(nil, args, nil)
|
||||
if err.Error() != "WRONG_DISCONNECT_CAUSE" {
|
||||
t.Errorf("Expected WRONG_DISCONNECT_CAUSE error, got: %v", err)
|
||||
}
|
||||
args.DisconnectCause = 1
|
||||
err = agent.V1DisconnectPeer(nil, args, nil)
|
||||
if err != utils.ErrNotFound {
|
||||
t.Errorf("Expected ErrNotFound, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiamanAgentCall(t *testing.T) {
|
||||
agent := &DiameterAgent{}
|
||||
ctx := context.Background()
|
||||
serviceMethod := "serviceTest"
|
||||
args := struct{ TestArg string }{"TestValue"}
|
||||
var reply struct{ TestReply string }
|
||||
|
||||
err := agent.Call(ctx, serviceMethod, args, &reply)
|
||||
if err == nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
expectedReply := ""
|
||||
if reply.TestReply != expectedReply {
|
||||
t.Errorf("Expected reply %s, got %s", expectedReply, reply.TestReply)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,9 +20,52 @@ package agents
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/cgrates/birpc/context"
|
||||
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/sessions"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func TestFAsSessionSClientIface(t *testing.T) {
|
||||
_ = sessions.BiRPCClient(new(FSsessions))
|
||||
}
|
||||
|
||||
func TestFsAgentV1DisconnectPeer(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
args := &utils.DPRArgs{}
|
||||
fss := &FSsessions{}
|
||||
err := fss.V1DisconnectPeer(ctx, args, nil)
|
||||
if err != utils.ErrNotImplemented {
|
||||
t.Errorf("Expected error: %v, got: %v", utils.ErrNotImplemented, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsAgentV1AlterSession(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
cgrEv := utils.CGREvent{}
|
||||
fss := &FSsessions{}
|
||||
err := fss.V1AlterSession(ctx, cgrEv, nil)
|
||||
if err != utils.ErrNotImplemented {
|
||||
t.Errorf("Expected error: %v, got: %v", utils.ErrNotImplemented, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFsAgentCreateHandlers(t *testing.T) {
|
||||
cfg := &config.FsAgentCfg{
|
||||
SubscribePark: true,
|
||||
}
|
||||
fs := &FSsessions{
|
||||
cfg: cfg,
|
||||
}
|
||||
handlers := fs.createHandlers()
|
||||
if _, ok := handlers["CHANNEL_ANSWER"]; !ok {
|
||||
t.Error("Expected CHANNEL_ANSWER handler, but not found")
|
||||
}
|
||||
if _, ok := handlers["CHANNEL_HANGUP_COMPLETE"]; !ok {
|
||||
t.Error("Expected CHANNEL_HANGUP_COMPLETE handler, but not found")
|
||||
}
|
||||
if _, ok := handlers["CHANNEL_PARK"]; !ok {
|
||||
t.Error("Expected CHANNEL_PARK handler, but not found")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,9 +20,68 @@ package agents
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/cgrates/birpc/context"
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/sessions"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func TestKAsSessionSClientIface(t *testing.T) {
|
||||
_ = sessions.BiRPCClient(new(KamailioAgent))
|
||||
}
|
||||
|
||||
func TestKamailioAgentV1WarnDisconnect(t *testing.T) {
|
||||
agent := KamailioAgent{}
|
||||
ctx := context.Background()
|
||||
args := make(map[string]any)
|
||||
var reply string
|
||||
err := agent.V1WarnDisconnect(ctx, args, &reply)
|
||||
if err != utils.ErrNotImplemented {
|
||||
t.Errorf("Expected ErrNotImplemented, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKamailioAgentV1DisconnectPeer(t *testing.T) {
|
||||
agent := KamailioAgent{}
|
||||
ctx := context.Background()
|
||||
dprArgs := &utils.DPRArgs{}
|
||||
var reply string
|
||||
|
||||
err := agent.V1DisconnectPeer(ctx, dprArgs, &reply)
|
||||
if err != utils.ErrNotImplemented {
|
||||
t.Errorf("Expected ErrNotImplemented, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKamailioAgentV1AlterSession(t *testing.T) {
|
||||
agent := KamailioAgent{}
|
||||
ctx := context.Background()
|
||||
cgrEvent := utils.CGREvent{}
|
||||
var reply string
|
||||
err := agent.V1AlterSession(ctx, cgrEvent, &reply)
|
||||
if err != utils.ErrNotImplemented {
|
||||
t.Errorf("Expected ErrNotImplemented, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKamailioAgentReload(t *testing.T) {
|
||||
cfg := config.KamAgentCfg{
|
||||
EvapiConns: []*config.KamConnCfg{
|
||||
{},
|
||||
{},
|
||||
{},
|
||||
},
|
||||
}
|
||||
ka := &KamailioAgent{
|
||||
cfg: &cfg,
|
||||
}
|
||||
ka.Reload()
|
||||
if len(ka.conns) != len(cfg.EvapiConns) {
|
||||
t.Errorf("Expected conns length %d, but got %d", len(cfg.EvapiConns), len(ka.conns))
|
||||
}
|
||||
for i, conn := range ka.conns {
|
||||
if conn != nil {
|
||||
t.Errorf("Expected ka.conns[%d] to be nil, but got value", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -665,7 +665,7 @@ func TestKamEventProcessMessageEmptyReply(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestAsKamProcessCDRReply(t *testing.T) {
|
||||
func TestKamEventProcessCDRReply(t *testing.T) {
|
||||
kev := KamEvent{
|
||||
"KamReplyRoute": "CGR_PROCESS_CDR",
|
||||
"KamTRIndex": "123",
|
||||
@@ -685,3 +685,20 @@ func TestAsKamProcessCDRReply(t *testing.T) {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKamEventV1ProcessCDRArgs(t *testing.T) {
|
||||
kev := KamEvent{
|
||||
"KamReplyRoute": "CGR_PROCESS_CDR",
|
||||
"KamTRIndex": "123",
|
||||
"KamTRLabel": "456",
|
||||
}
|
||||
args := kev.V1ProcessCDRArgs()
|
||||
if args != nil {
|
||||
t.Errorf("Expected non-nil CGREvent, got nil")
|
||||
}
|
||||
kev = KamEvent{}
|
||||
args = kev.V1ProcessCDRArgs()
|
||||
if args != nil {
|
||||
t.Errorf("Expected nil CGREvent for error case, got %+v", args)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,12 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/cgrates/birpc/context"
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
"github.com/cgrates/cgrates/sessions"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
"github.com/cgrates/kamevapi"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
@@ -389,3 +394,90 @@ func TestLibdnsUpdateDnsQuestions(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestKamailioAgentCall(t *testing.T) {
|
||||
cfg := &config.KamAgentCfg{}
|
||||
connMgr := &engine.ConnManager{}
|
||||
conns := []*kamevapi.KamEvapi{}
|
||||
activeSessionIDs := make(chan []*sessions.SessionID)
|
||||
ctx := &context.Context{}
|
||||
ka := &KamailioAgent{
|
||||
cfg: cfg,
|
||||
connMgr: connMgr,
|
||||
timezone: "UTC",
|
||||
conns: conns,
|
||||
activeSessionIDs: activeSessionIDs,
|
||||
ctx: ctx,
|
||||
}
|
||||
args := struct {
|
||||
Message string
|
||||
}{
|
||||
Message: "message",
|
||||
}
|
||||
var reply string
|
||||
err := ka.Call("UNSUPPORTED_SERVICE_METHOD", args, &reply)
|
||||
if err == nil {
|
||||
t.Errorf("UNSUPPORTED_SERVICE_METHOD %v", err)
|
||||
}
|
||||
expectedReply := ""
|
||||
if reply != expectedReply {
|
||||
t.Errorf("Expected reply %q, got %q", expectedReply, reply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibDnsUpdateDnsOption(t *testing.T) {
|
||||
|
||||
ednsOptions := []dns.EDNS0{
|
||||
&dns.EDNS0_NSID{},
|
||||
&dns.EDNS0_SUBNET{},
|
||||
&dns.EDNS0_COOKIE{},
|
||||
&dns.EDNS0_UL{},
|
||||
&dns.EDNS0_LLQ{},
|
||||
&dns.EDNS0_DAU{},
|
||||
&dns.EDNS0_DHU{},
|
||||
&dns.EDNS0_N3U{},
|
||||
&dns.EDNS0_EXPIRE{},
|
||||
&dns.EDNS0_TCP_KEEPALIVE{},
|
||||
&dns.EDNS0_PADDING{},
|
||||
&dns.EDNS0_EDE{},
|
||||
&dns.EDNS0_ESU{},
|
||||
&dns.EDNS0_LOCAL{},
|
||||
}
|
||||
path := []string{"0", utils.DNSNsid}
|
||||
value := "test-nsid"
|
||||
newBranch := false
|
||||
updatedOptions, err := updateDnsOption(ednsOptions, path, value, newBranch)
|
||||
if err != nil {
|
||||
t.Errorf("Update EDNS0_NSID's NSID field returned unexpected error: %v", err)
|
||||
}
|
||||
if nsidOption, ok := updatedOptions[0].(*dns.EDNS0_NSID); ok {
|
||||
if nsidOption.Nsid != value {
|
||||
t.Errorf("Expected NSID %s, got %s", value, nsidOption.Nsid)
|
||||
}
|
||||
} else {
|
||||
t.Errorf("Expected EDNS0_NSID option, got %T", updatedOptions[0])
|
||||
}
|
||||
path = []string{"1", utils.DNSFamily}
|
||||
valueInt := 1
|
||||
newBranch = true
|
||||
updatedOptions, err = updateDnsOption(ednsOptions, path, valueInt, newBranch)
|
||||
if err != nil {
|
||||
t.Errorf("Update EDNS0_SUBNET's Family field returned unexpected error: %v", err)
|
||||
}
|
||||
if subnetOption, ok := updatedOptions[1].(*dns.EDNS0_SUBNET); ok {
|
||||
if subnetOption.Family != uint16(valueInt) {
|
||||
t.Errorf("Expected Family %d, got %d", valueInt, subnetOption.Family)
|
||||
}
|
||||
} else {
|
||||
t.Errorf("Expected EDNS0_SUBNET option, got %T", updatedOptions[1])
|
||||
}
|
||||
path = []string{"0", utils.DNSNsid, "extra"}
|
||||
value = "value"
|
||||
newBranch = false
|
||||
_, err = updateDnsOption(ednsOptions, path, value, newBranch)
|
||||
expectedErrMsg := "WRONG_PATH"
|
||||
if err == nil || err.Error() != expectedErrMsg {
|
||||
t.Errorf("Expected error '%s', got '%v'", expectedErrMsg, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/antchfx/xmlquery"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
@@ -254,3 +255,50 @@ func TestLibhttpagentNewHAReplyEncoder(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibHttpAgentPagentNewHADataProvider(t *testing.T) {
|
||||
req, err := http.NewRequest("GET", "http://cgrates.org", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create request: %v", err)
|
||||
}
|
||||
|
||||
t.Run("unsupported decoder type <unsupported>", func(t *testing.T) {
|
||||
reqPayload := "unsupported"
|
||||
_, err := newHADataProvider(reqPayload, req)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error, got nil")
|
||||
}
|
||||
expectedErr := "unsupported decoder type <unsupported>"
|
||||
if err.Error() != expectedErr {
|
||||
t.Errorf("Expected error '%s', got '%v'", expectedErr, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("MetaUrl decoder type", func(t *testing.T) {
|
||||
reqPayload := utils.MetaUrl
|
||||
dp, err := newHADataProvider(reqPayload, req)
|
||||
if err != nil {
|
||||
t.Errorf("Expected nil error, got %v", err)
|
||||
}
|
||||
if dp == nil {
|
||||
t.Errorf("Expected non-nil DataProvider")
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestLibHttpAgentHTTPXmlDPString(t *testing.T) {
|
||||
|
||||
hU := &httpXmlDP{
|
||||
xmlDoc: &xmlquery.Node{
|
||||
Data: "dataProvided",
|
||||
},
|
||||
}
|
||||
|
||||
expected := ""
|
||||
result := hU.String()
|
||||
|
||||
if result != expected {
|
||||
t.Errorf("Expected XML: %s, got: %s", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user