From e1b8fe23219b24790ee9c2f29283fc2b771fbf7d Mon Sep 17 00:00:00 2001 From: Trial97 Date: Fri, 26 Oct 2018 12:40:51 +0300 Subject: [PATCH] Added tests for RARequestProcessor and RadiusAgentCfg loadFromJsonCfg --- config/config.go | 31 ++++---- config/config_defaults.go | 1 - config/{raconfig.go => radiuscfg.go} | 0 config/radiuscfg_test.go | 110 +++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 18 deletions(-) rename config/{raconfig.go => radiuscfg.go} (100%) create mode 100644 config/radiuscfg_test.go diff --git a/config/config.go b/config/config.go index 1e8596fb3..d6651581f 100755 --- a/config/config.go +++ b/config/config.go @@ -149,10 +149,10 @@ func NewDefaultCGRConfig() (*CGRConfig, error) { cfg.fsAgentCfg = new(FsAgentCfg) cfg.kamAgentCfg = new(KamAgentCfg) cfg.asteriskAgentCfg = new(AsteriskAgentCfg) - - cfg.SmOsipsConfig = new(SmOsipsConfig) cfg.diameterAgentCfg = new(DiameterAgentCfg) cfg.radiusAgentCfg = new(RadiusAgentCfg) + + cfg.SmOsipsConfig = new(SmOsipsConfig) cfg.dispatcherSCfg = new(DispatcherSCfg) cfg.ConfigReloads = make(map[string]chan struct{}) cfg.ConfigReloads[utils.CDRC] = make(chan struct{}, 1) @@ -260,15 +260,13 @@ type CGRConfig struct { CdrcProfiles map[string][]*CdrcCfg // Number of CDRC instances running imports, format map[dirPath][]{Configs} loaderCfg []*LoaderSCfg // LoaderS configurations - SmOsipsConfig *SmOsipsConfig // SMOpenSIPS Configuration - diameterAgentCfg *DiameterAgentCfg // DiameterAgent configuration - radiusAgentCfg *RadiusAgentCfg // RadiusAgent configuration - httpAgentCfg []*HttpAgentCfg // HttpAgent configuration - PubSubServerEnabled bool // Starts PubSub as server: . - AliasesServerEnabled bool // Starts PubSub as server: . - UserServerEnabled bool // Starts User as server: - UserServerIndexes []string // List of user profile field indexes - attributeSCfg *AttributeSCfg // Attribute service configuration + SmOsipsConfig *SmOsipsConfig // SMOpenSIPS Configuration + httpAgentCfg []*HttpAgentCfg // HttpAgent configuration + PubSubServerEnabled bool // Starts PubSub as server: . + AliasesServerEnabled bool // Starts PubSub as server: . + UserServerEnabled bool // Starts User as server: + UserServerIndexes []string // List of user profile field indexes + attributeSCfg *AttributeSCfg // Attribute service configuration chargerSCfg *ChargerSCfg resourceSCfg *ResourceSConfig // Configuration for resource limiter statsCfg *StatSCfg // Configuration for StatS @@ -305,6 +303,8 @@ type CGRConfig struct { fsAgentCfg *FsAgentCfg // FreeSWITCHAgent config kamAgentCfg *KamAgentCfg // KamailioAgent config asteriskAgentCfg *AsteriskAgentCfg // AsteriskAgent config + diameterAgentCfg *DiameterAgentCfg // DiameterAgent config + radiusAgentCfg *RadiusAgentCfg // RadiusAgent config analyzerSCfg *AnalyzerSCfg } @@ -851,6 +851,9 @@ func (self *CGRConfig) loadFromJsonCfg(jsnCfg *CgrJsonCfg) (err error) { if err != nil { return err } + if err := self.diameterAgentCfg.loadFromJsonCfg(jsnDACfg); err != nil { + return err + } jsnRACfg, err := jsnCfg.RadiusAgentJsonCfg() if err != nil { @@ -1017,12 +1020,6 @@ func (self *CGRConfig) loadFromJsonCfg(jsnCfg *CgrJsonCfg) (err error) { } } - if jsnDACfg != nil { - if err := self.diameterAgentCfg.loadFromJsonCfg(jsnDACfg); err != nil { - return err - } - } - if jsnRACfg != nil { if err := self.radiusAgentCfg.loadFromJsonCfg(jsnRACfg); err != nil { return err diff --git a/config/config_defaults.go b/config/config_defaults.go index 58f1ce2ad..569ea1ac0 100755 --- a/config/config_defaults.go +++ b/config/config_defaults.go @@ -414,7 +414,6 @@ const CGRATES_CFG_JSON = ` "sessions_conns": [ {"address": "*internal"} // connection towards SessionService ], - "cdr_requires_session": false, // only create CDR if there is an active session at terminate "request_processors": [], }, diff --git a/config/raconfig.go b/config/radiuscfg.go similarity index 100% rename from config/raconfig.go rename to config/radiuscfg.go diff --git a/config/radiuscfg_test.go b/config/radiuscfg_test.go new file mode 100644 index 000000000..706dd42ed --- /dev/null +++ b/config/radiuscfg_test.go @@ -0,0 +1,110 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ +package config + +import ( + "reflect" + "strings" + "testing" + + "github.com/cgrates/cgrates/utils" +) + +func TestRadiusAgentCfgloadFromJsonCfg(t *testing.T) { + var racfg, expected RadiusAgentCfg + if err := racfg.loadFromJsonCfg(nil); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(racfg, expected) { + t.Errorf("Expected: %+v ,recived: %+v", expected, racfg) + } + if err := racfg.loadFromJsonCfg(new(RadiusAgentJsonCfg)); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(racfg, expected) { + t.Errorf("Expected: %+v ,recived: %+v", expected, racfg) + } + cfgJSONStr := `{ +"radius_agent": { + "enabled": false, // enables the radius agent: + "listen_net": "udp", // network to listen on + "listen_auth": "127.0.0.1:1812", // address where to listen for radius authentication requests + "listen_acct": "127.0.0.1:1813", // address where to listen for radius accounting requests + "client_secrets": { // hash containing secrets for clients connecting here <*default|$client_ip> + "*default": "CGRateS.org" + }, + "client_dictionaries": { // per client path towards directory holding additional dictionaries to load (extra to RFC) + "*default": "/usr/share/cgrates/radius/dict/", // key represents the client IP or catch-all <*default|$client_ip> + }, + "sessions_conns": [ + {"address": "*internal"} // connection towards SessionService + ], + "request_processors": [], +}, +}` + expected = RadiusAgentCfg{ + ListenNet: "udp", + ListenAuth: "127.0.0.1:1812", + ListenAcct: "127.0.0.1:1813", + ClientSecrets: map[string]string{"*default": "CGRateS.org"}, + ClientDictionaries: map[string]string{"*default": "/usr/share/cgrates/radius/dict/"}, + SessionSConns: []*HaPoolConfig{{Address: "*internal"}}, + } + if jsnCfg, err := NewCgrJsonCfgFromReader(strings.NewReader(cfgJSONStr)); err != nil { + t.Error(err) + } else if jsnRaCfg, err := jsnCfg.RadiusAgentJsonCfg(); err != nil { + t.Error(err) + } else if err = racfg.loadFromJsonCfg(jsnRaCfg); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, racfg) { + t.Errorf("Expected: %+v , recived: %+v", utils.ToJSON(expected), utils.ToJSON(racfg)) + } +} + +func TestRARequestProcessorloadFromJsonCfg(t *testing.T) { + var rareq, expected RARequestProcessor + if err := rareq.loadFromJsonCfg(nil); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(rareq, expected) { + t.Errorf("Expected: %+v ,recived: %+v", expected, rareq) + } + if err := rareq.loadFromJsonCfg(new(RAReqProcessorJsnCfg)); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(rareq, expected) { + t.Errorf("Expected: %+v ,recived: %+v", expected, rareq) + } + json := &RAReqProcessorJsnCfg{ + Id: utils.StringPointer("cgrates"), + Tenant: utils.StringPointer("tenant"), + Filters: &[]string{"filter1", "filter2"}, + Flags: &[]string{"flag1", "flag2"}, + Timezone: utils.StringPointer("Local"), + Continue_on_success: utils.BoolPointer(true), + } + expected = RARequestProcessor{ + Id: "cgrates", + Tenant: NewRSRParsersMustCompile("tenant", true), + Filters: []string{"filter1", "filter2"}, + Flags: utils.StringMap{"flag1": true, "flag2": true}, + Timezone: "Local", + ContinueOnSuccess: true, + } + if err = rareq.loadFromJsonCfg(json); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, rareq) { + t.Errorf("Expected: %+v , recived: %+v", utils.ToJSON(expected), utils.ToJSON(rareq)) + } +}