mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 10:06:24 +05:00
Add unit tests
This commit is contained in:
committed by
Dan Christian Bogos
parent
0c87b48c5b
commit
12b2e659b9
@@ -336,27 +336,21 @@ func TestRadiusAgentCfgClone(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestClone(t *testing.T) {
|
||||
|
||||
baseOpts := &DAClientOpts{
|
||||
|
||||
func TestDAClientOptsClone(t *testing.T) {
|
||||
originalOpts := &DAClientOpts{
|
||||
Transport: "udp",
|
||||
Host: "localhost",
|
||||
Port: 6768,
|
||||
Flags: utils.FlagsWithParams{
|
||||
utils.MetaSessionS: utils.FlagParams{},
|
||||
utils.MetaRoutes: utils.FlagParams{},
|
||||
utils.MetaSessionS: utils.FlagParams{},
|
||||
},
|
||||
}
|
||||
|
||||
clonedOpts := baseOpts.Clone()
|
||||
got := originalOpts.Clone()
|
||||
|
||||
if clonedOpts.Transport != baseOpts.Transport || clonedOpts.Host != baseOpts.Host || clonedOpts.Port != baseOpts.Port || clonedOpts.Flags == nil {
|
||||
t.Errorf("Clone failed. Expected all fields copied")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(baseOpts.Flags, clonedOpts.Flags) {
|
||||
t.Errorf("Flags might not be deep copied.")
|
||||
if diff := cmp.Diff(originalOpts, got); diff != "" {
|
||||
t.Errorf("Clone() returned an unexpected value(-want +got): \n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ import (
|
||||
|
||||
"github.com/cgrates/birpc/context"
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
"github.com/cgrates/ltcache"
|
||||
)
|
||||
@@ -482,81 +481,60 @@ func TestDspCacheSv1ReplicateSetNil(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDspCacheSv1GetItemWithRemote(t *testing.T) {
|
||||
cfg := config.NewDefaultCGRConfig()
|
||||
db := engine.NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items)
|
||||
dm := engine.NewDataManager(db, cfg.CacheCfg(), nil)
|
||||
dsp := NewDispatcherService(dm, cfg, engine.NewFilterS(cfg, nil, dm), nil)
|
||||
args := &utils.ArgsGetCacheItemWithAPIOpts{
|
||||
Tenant: "cgrates.org",
|
||||
ArgsGetCacheItem: utils.ArgsGetCacheItem{
|
||||
CacheID: utils.CacheChargerProfiles,
|
||||
ItemID: "cgrates.org:DISP1 ",
|
||||
},
|
||||
APIOpts: map[string]any{
|
||||
"Opt": "Disp",
|
||||
},
|
||||
func TestDspCacheSv1GetItemWithRemoteError(t *testing.T) {
|
||||
cgrCfg := config.NewDefaultCGRConfig()
|
||||
dspSrv := NewDispatcherService(nil, cgrCfg, nil, nil)
|
||||
cgrCfg.DispatcherSCfg().AttributeSConns = []string{"test"}
|
||||
CGREvent := &utils.ArgsGetCacheItemWithAPIOpts{
|
||||
Tenant: "tenant",
|
||||
}
|
||||
|
||||
if err := dm.SetDispatcherProfile(&engine.DispatcherProfile{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "DSP_Test1",
|
||||
FilterIDs: []string{"*string:~*opts.Opt:Disp"},
|
||||
Strategy: utils.MetaRoundRobin,
|
||||
Subsystems: []string{utils.MetaAny},
|
||||
Hosts: engine.DispatcherHostProfiles{
|
||||
&engine.DispatcherHostProfile{
|
||||
ID: "ALL2",
|
||||
FilterIDs: []string{},
|
||||
Weight: 20,
|
||||
Params: make(map[string]any),
|
||||
},
|
||||
},
|
||||
Weight: 20,
|
||||
}, true); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
var reply any
|
||||
if err := dsp.CacheSv1GetItemWithRemote(context.Background(), args, &reply); err == nil {
|
||||
t.Error(err)
|
||||
var reply *any
|
||||
err := dspSrv.CacheSv1GetItemWithRemote(context.Background(), CGREvent, reply)
|
||||
expected := "MANDATORY_IE_MISSING: [ApiKey]"
|
||||
if err == nil || err.Error() != expected {
|
||||
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDspCacheSv1GetItem(t *testing.T) {
|
||||
cfg := config.NewDefaultCGRConfig()
|
||||
dm := engine.NewDataManager(engine.NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items), cfg.CacheCfg(), nil)
|
||||
dsp := NewDispatcherService(dm, cfg, engine.NewFilterS(cfg, nil, dm), nil)
|
||||
args := &utils.ArgsGetCacheItemWithAPIOpts{
|
||||
Tenant: "cgrates.org",
|
||||
ArgsGetCacheItem: utils.ArgsGetCacheItem{
|
||||
CacheID: utils.CacheChargerProfiles,
|
||||
ItemID: "cgrates.org:DISP1 ",
|
||||
},
|
||||
APIOpts: map[string]any{
|
||||
"Opt": "Disp",
|
||||
},
|
||||
func TestDspCacheSv1GetItemWithRemoteSetNil(t *testing.T) {
|
||||
cgrCfg := config.NewDefaultCGRConfig()
|
||||
dspSrv := NewDispatcherService(nil, cgrCfg, nil, nil)
|
||||
CGREvent := &utils.ArgsGetCacheItemWithAPIOpts{
|
||||
Tenant: "tenant",
|
||||
}
|
||||
if err := dm.SetDispatcherProfile(&engine.DispatcherProfile{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "DSP_Test1",
|
||||
FilterIDs: []string{"*string:~*opts.Opt:Disp"},
|
||||
Strategy: utils.MetaRoundRobin,
|
||||
Subsystems: []string{utils.MetaAny},
|
||||
Hosts: engine.DispatcherHostProfiles{
|
||||
&engine.DispatcherHostProfile{
|
||||
ID: "ALL2",
|
||||
FilterIDs: []string{},
|
||||
Weight: 20,
|
||||
Params: make(map[string]any),
|
||||
},
|
||||
},
|
||||
Weight: 20,
|
||||
}, true); err != nil {
|
||||
t.Error(err)
|
||||
var reply *any
|
||||
err := dspSrv.CacheSv1GetItemWithRemote(context.Background(), CGREvent, reply)
|
||||
expected := "DISPATCHER_ERROR:NO_DATABASE_CONNECTION"
|
||||
if err == nil || err.Error() != expected {
|
||||
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDspCacheSv1GetItemError(t *testing.T) {
|
||||
cgrCfg := config.NewDefaultCGRConfig()
|
||||
dspSrv := NewDispatcherService(nil, cgrCfg, nil, nil)
|
||||
cgrCfg.DispatcherSCfg().AttributeSConns = []string{"test"}
|
||||
CGREvent := &utils.ArgsGetCacheItemWithAPIOpts{
|
||||
Tenant: "tenant",
|
||||
}
|
||||
var reply *any
|
||||
err := dspSrv.CacheSv1GetItem(context.Background(), CGREvent, reply)
|
||||
expected := "MANDATORY_IE_MISSING: [ApiKey]"
|
||||
if err == nil || err.Error() != expected {
|
||||
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDspCacheSv1GetItemSetNil(t *testing.T) {
|
||||
cgrCfg := config.NewDefaultCGRConfig()
|
||||
dspSrv := NewDispatcherService(nil, cgrCfg, nil, nil)
|
||||
CGREvent := &utils.ArgsGetCacheItemWithAPIOpts{
|
||||
Tenant: "tenant",
|
||||
}
|
||||
var reply *any
|
||||
err := dspSrv.CacheSv1GetItem(context.Background(), CGREvent, reply)
|
||||
expected := "DISPATCHER_ERROR:NO_DATABASE_CONNECTION"
|
||||
if err == nil || err.Error() != expected {
|
||||
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err)
|
||||
}
|
||||
var reply any
|
||||
if err := dsp.CacheSv1GetItem(context.Background(), args, &reply); err == nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1755,3 +1755,25 @@ func TestDispatchersdispatcherProfileForEventAnySStrueBothFound(t *testing.T) {
|
||||
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", dsp1, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispatcherServiceDispatcherProfilesForEventBoolOptsErr(t *testing.T) {
|
||||
cfg := config.NewDefaultCGRConfig()
|
||||
rpcCl := map[string]chan birpc.ClientConnector{}
|
||||
dataDB := engine.NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items)
|
||||
connMng := engine.NewConnManager(cfg, rpcCl)
|
||||
dm := engine.NewDataManager(dataDB, nil, connMng)
|
||||
fltrs := engine.NewFilterS(cfg, connMng, dm)
|
||||
dss := NewDispatcherService(dm, cfg, fltrs, connMng)
|
||||
|
||||
ev := &utils.CGREvent{
|
||||
APIOpts: map[string]any{
|
||||
utils.MetaDispatchers: []byte{},
|
||||
},
|
||||
}
|
||||
expected := "cannot convert field: [] to bool"
|
||||
_, err := dss.dispatcherProfilesForEvent("cgrates.org", ev, utils.MapStorage{}, "test substring")
|
||||
if err.Error() != expected {
|
||||
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
"net/rpc"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/birpc"
|
||||
"github.com/cgrates/birpc/context"
|
||||
@@ -29,6 +30,7 @@ import (
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
"github.com/cgrates/rpcclient"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
func TestLoadMetricsGetHosts(t *testing.T) {
|
||||
@@ -1003,3 +1005,23 @@ func TestLibDispatcherDispatchFilterError(t *testing.T) {
|
||||
t.Errorf("Expected error: %s received: %v", expErrMsg, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibDispatcherNewInternalHost(t *testing.T) {
|
||||
tnt := "cgrates.org"
|
||||
want := &engine.DispatcherHost{
|
||||
Tenant: tnt,
|
||||
RemoteHost: &config.RemoteHost{
|
||||
ID: utils.MetaInternal,
|
||||
Address: utils.MetaInternal,
|
||||
ConnectAttempts: 1,
|
||||
Reconnects: 1,
|
||||
ConnectTimeout: time.Second,
|
||||
ReplyTimeout: time.Second,
|
||||
},
|
||||
}
|
||||
|
||||
got := newInternalHost(tnt)
|
||||
if diff := cmp.Diff(want, got, cmp.AllowUnexported(engine.DispatcherHost{})); diff != "" {
|
||||
t.Errorf("newInternalHost(%q) returned an unexpected value(-want +got): \n%s", tnt, diff)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -725,3 +725,36 @@ func TestDspSessionSv1STIRIdentityErrorNil(t *testing.T) {
|
||||
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDspSessionSv1AlterSessionsNil(t *testing.T) {
|
||||
cgrCfg := config.NewDefaultCGRConfig()
|
||||
dspSrv := NewDispatcherService(nil, cgrCfg, nil, nil)
|
||||
args := utils.SessionFilterWithEvent{
|
||||
SessionFilter: &utils.SessionFilter{
|
||||
Tenant: "tenant",
|
||||
},
|
||||
}
|
||||
var reply *string
|
||||
err := dspSrv.SessionSv1AlterSessions(context.Background(), args, reply)
|
||||
expected := "DISPATCHER_ERROR:NO_DATABASE_CONNECTION"
|
||||
if err == nil || err.Error() != expected {
|
||||
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDspSessionSv1AlterSessionsErrorNil(t *testing.T) {
|
||||
cgrCfg := config.NewDefaultCGRConfig()
|
||||
dspSrv := NewDispatcherService(nil, cgrCfg, nil, nil)
|
||||
cgrCfg.DispatcherSCfg().AttributeSConns = []string{"test"}
|
||||
args := utils.SessionFilterWithEvent{
|
||||
SessionFilter: &utils.SessionFilter{
|
||||
Tenant: "tenant",
|
||||
},
|
||||
}
|
||||
var reply *string
|
||||
err := dspSrv.SessionSv1AlterSessions(context.Background(), args, reply)
|
||||
expected := "MANDATORY_IE_MISSING: [ApiKey]"
|
||||
if err == nil || err.Error() != expected {
|
||||
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user