From 12b2e659b917fe6095fd3096bd753a01c0e49954 Mon Sep 17 00:00:00 2001 From: armirveliaj Date: Thu, 23 May 2024 10:57:42 -0400 Subject: [PATCH] Add unit tests --- config/radiuscfg_test.go | 18 ++--- dispatchers/caches_test.go | 122 ++++++++++++------------------ dispatchers/dispatchers_test.go | 22 ++++++ dispatchers/libdispatcher_test.go | 22 ++++++ dispatchers/sessions_test.go | 33 ++++++++ 5 files changed, 133 insertions(+), 84 deletions(-) diff --git a/config/radiuscfg_test.go b/config/radiuscfg_test.go index 4afcbb3ef..6efb3c4a2 100644 --- a/config/radiuscfg_test.go +++ b/config/radiuscfg_test.go @@ -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) } } diff --git a/dispatchers/caches_test.go b/dispatchers/caches_test.go index a236ebd65..3c5531faf 100644 --- a/dispatchers/caches_test.go +++ b/dispatchers/caches_test.go @@ -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) - } - } diff --git a/dispatchers/dispatchers_test.go b/dispatchers/dispatchers_test.go index bb9062b15..22fd87f9a 100644 --- a/dispatchers/dispatchers_test.go +++ b/dispatchers/dispatchers_test.go @@ -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) + } + +} diff --git a/dispatchers/libdispatcher_test.go b/dispatchers/libdispatcher_test.go index 4e5e2efeb..24d2a56e8 100644 --- a/dispatchers/libdispatcher_test.go +++ b/dispatchers/libdispatcher_test.go @@ -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) + } +} diff --git a/dispatchers/sessions_test.go b/dispatchers/sessions_test.go index 7617cbab8..6c8671973 100644 --- a/dispatchers/sessions_test.go +++ b/dispatchers/sessions_test.go @@ -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) + } +}