From b1bcd30d4807111028cb8821dc6c857dfb7a9648 Mon Sep 17 00:00:00 2001 From: porosnicuadrian Date: Tue, 13 Oct 2020 11:06:26 +0300 Subject: [PATCH] Updated tests and methods for dispatchers.go with default tenant value --- apier/v1/dispatcher_it_test.go | 52 +++++++++++++++++++++++++++++++ apier/v1/dispatchersv1_it_test.go | 20 ++++++++++++ dispatchers/dispatchers.go | 22 ++++++++----- 3 files changed, 87 insertions(+), 7 deletions(-) diff --git a/apier/v1/dispatcher_it_test.go b/apier/v1/dispatcher_it_test.go index 581f1ea45..8a33cf497 100644 --- a/apier/v1/dispatcher_it_test.go +++ b/apier/v1/dispatcher_it_test.go @@ -61,6 +61,8 @@ var ( testDispatcherSUpdateDispatcherHost, testDispatcherSGetDispatcherHostCache, testDispatcherSRemDispatcherHost, + testDispatcherSSetDispatcherHostWithoutTenant, + testDispatcherSRemDispatcherHostWithoutTenant, testDispatcherSKillEngine, } @@ -420,3 +422,53 @@ func testDispatcherSRemDispatcherProfileWithoutTenant(t *testing.T) { t.Error(err) } } + +func testDispatcherSSetDispatcherHostWithoutTenant(t *testing.T) { + dispatcherHost = &DispatcherHostWithCache{ + DispatcherHost: &engine.DispatcherHost{ + ID: "DspHst7", + Conns: []*config.RemoteHost{ + { + Address: "*internal", + }, + { + Address: ":2012", + Transport: utils.MetaJSON, + TLS: true, + }, + }, + }, + } + var reply string + if err := dispatcherRPC.Call(utils.APIerSv1SetDispatcherHost, dispatcherHost, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Error("Unexpected reply returned", reply) + } + dispatcherHost.DispatcherHost.Tenant = "cgrates.org" + var result *engine.DispatcherHost + if err := dispatcherRPC.Call(utils.APIerSv1GetDispatcherHost, + &utils.TenantID{ID: "DspHst7"}, + &result); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(result, dispatcherHost.DispatcherHost) { + t.Errorf("Expected %+v, received %+v", utils.ToJSON(dispatcherHost.DispatcherHost), utils.ToJSON(result)) + } +} + +func testDispatcherSRemDispatcherHostWithoutTenant(t *testing.T) { + var reply string + if err := dispatcherRPC.Call(utils.APIerSv1RemoveDispatcherHost, + &utils.TenantIDWithCache{ID: "DspHst7"}, + &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Error("Unexpected reply returned", reply) + } + var result *engine.DispatcherHost + if err := dispatcherRPC.Call(utils.APIerSv1GetDispatcherHost, + &utils.TenantID{ID: "DspHst7"}, + &result); err == nil || err.Error() != utils.ErrNotFound.Error() { + t.Error(err) + } +} diff --git a/apier/v1/dispatchersv1_it_test.go b/apier/v1/dispatchersv1_it_test.go index 98bc1d397..6d7677226 100644 --- a/apier/v1/dispatchersv1_it_test.go +++ b/apier/v1/dispatchersv1_it_test.go @@ -180,6 +180,26 @@ func testDspDspv1GetProfileForEvent(t *testing.T) { if !reflect.DeepEqual(expected, reply) { t.Errorf("expected: %s ,\n received: %s", utils.ToJSON(expected), utils.ToJSON(reply)) } + + arg2 := utils.CGREventWithOpts{ + CGREvent: &utils.CGREvent{ + ID: "testDspvWithoutTenant", + Event: map[string]interface{}{ + utils.EVENT_NAME: "Event1", + }, + }, + Opts: map[string]interface{}{ + utils.Subsys: utils.META_ANY, + }, + } + expected.Hosts.Sort() + if err := dspRPC.Call(utils.DispatcherSv1GetProfileForEvent, &arg2, &reply); err != nil { + t.Fatal(err) + } + reply.Hosts.Sort() + if !reflect.DeepEqual(expected, reply) { + t.Errorf("expected: %s ,\n received: %s", utils.ToJSON(expected), utils.ToJSON(reply)) + } } func testDspDspv1GetProfileForEventWithMethod(t *testing.T) { diff --git a/dispatchers/dispatchers.go b/dispatchers/dispatchers.go index 9871a1ed8..e664251f7 100755 --- a/dispatchers/dispatchers.go +++ b/dispatchers/dispatchers.go @@ -113,13 +113,13 @@ func (dS *DispatcherService) authorize(method, tenant string, apiKey string, evT // dispatcherForEvent returns a dispatcher instance configured for specific event // or utils.ErrNotFound if none present -func (dS *DispatcherService) dispatcherProfileForEvent(ev *utils.CGREventWithOpts, +func (dS *DispatcherService) dispatcherProfileForEvent(tnt string, ev *utils.CGREventWithOpts, subsys string) (dPrlf *engine.DispatcherProfile, err error) { // find out the matching profiles - anyIdxPrfx := utils.ConcatenatedKey(ev.Tenant, utils.META_ANY) + anyIdxPrfx := utils.ConcatenatedKey(tnt, utils.META_ANY) idxKeyPrfx := anyIdxPrfx if subsys != "" { - idxKeyPrfx = utils.ConcatenatedKey(ev.Tenant, subsys) + idxKeyPrfx = utils.ConcatenatedKey(tnt, subsys) } evNm := utils.MapStorage{ utils.MetaReq: ev.CGREvent.Event, @@ -151,7 +151,7 @@ func (dS *DispatcherService) dispatcherProfileForEvent(ev *utils.CGREventWithOpt } } for prflID := range prflIDs { - prfl, err := dS.dm.GetDispatcherProfile(ev.Tenant, prflID, true, true, utils.NonTransactional) + prfl, err := dS.dm.GetDispatcherProfile(tnt, prflID, true, true, utils.NonTransactional) if err != nil { if err != utils.ErrNotFound { return nil, err @@ -166,7 +166,7 @@ func (dS *DispatcherService) dispatcherProfileForEvent(ev *utils.CGREventWithOpt !prfl.ActivationInterval.IsActiveAtTime(*ev.Time) { // not active continue } - if pass, err := dS.fltrS.Pass(ev.Tenant, prfl.FilterIDs, + if pass, err := dS.fltrS.Pass(tnt, prfl.FilterIDs, evNm); err != nil { return nil, err } else if !pass { @@ -185,7 +185,11 @@ func (dS *DispatcherService) dispatcherProfileForEvent(ev *utils.CGREventWithOpt // Dispatch is the method forwarding the request towards the right connection func (dS *DispatcherService) Dispatch(ev *utils.CGREventWithOpts, subsys string, serviceMethod string, args interface{}, reply interface{}) (err error) { - dPrfl, errDsp := dS.dispatcherProfileForEvent(ev, subsys) + tnt := ev.Tenant + if tnt == utils.EmptyString { + tnt = dS.cfg.GeneralCfg().DefaultTenant + } + dPrfl, errDsp := dS.dispatcherProfileForEvent(tnt, ev, subsys) if errDsp != nil { return utils.NewErrDispatcherS(errDsp) } @@ -206,7 +210,11 @@ func (dS *DispatcherService) Dispatch(ev *utils.CGREventWithOpts, subsys string, func (dS *DispatcherService) V1GetProfileForEvent(ev *utils.CGREventWithOpts, dPfl *engine.DispatcherProfile) (err error) { - retDPfl, errDpfl := dS.dispatcherProfileForEvent(ev, utils.IfaceAsString(ev.Opts[utils.Subsys])) + tnt := ev.Tenant + if tnt == utils.EmptyString { + tnt = dS.cfg.GeneralCfg().DefaultTenant + } + retDPfl, errDpfl := dS.dispatcherProfileForEvent(tnt, ev, utils.IfaceAsString(ev.Opts[utils.Subsys])) if errDpfl != nil { return utils.NewErrDispatcherS(errDpfl) }