From 6399c506a610cabfaa4478b6d6792236004f828b Mon Sep 17 00:00:00 2001 From: nickolasdaniel Date: Mon, 7 Jun 2021 14:54:10 +0300 Subject: [PATCH] Added unit tests for config --- config/apis.go | 6 +-- config/apis_test.go | 84 ++++++++++++++++++++++++++++++++++ config/config_json_test.go | 20 ++++++++ config/config_test.go | 32 +++++++++++++ config/erscfg_test.go | 89 +++++++++++++++++++++++++++++++++++- config/loadercgrcfg_test.go | 45 ++++++++++++++++++ config/sessionscfg_test.go | 20 ++++---- config/thresholdscfg_test.go | 3 ++ 8 files changed, 287 insertions(+), 12 deletions(-) diff --git a/config/apis.go b/config/apis.go index e238d2052..77076b5c0 100644 --- a/config/apis.go +++ b/config/apis.go @@ -537,11 +537,11 @@ func storeDiffSection(ctx *context.Context, section string, db ConfigDB, v1, v2 } return db.SetSection(ctx, section, diffRadiusAgentJsonCfg(jsn, v1.RadiusAgentCfg(), v2.RadiusAgentCfg(), v2.GeneralCfg().RSRSep)) case HTTPAgentJSON: - var jsn *RadiusAgentJsonCfg - if jsn, err = db.RadiusAgentJsonCfg(); err != nil { + var jsn *[]*HttpAgentJsonCfg + if jsn, err = db.HttpAgentJsonCfg(); err != nil { return } - return db.SetSection(ctx, section, diffRadiusAgentJsonCfg(jsn, v1.RadiusAgentCfg(), v2.RadiusAgentCfg(), v2.GeneralCfg().RSRSep)) + return db.SetSection(ctx, section, diffHttpAgentsJsonCfg(jsn, v1.HTTPAgentCfg(), v2.HTTPAgentCfg(), v2.GeneralCfg().RSRSep)) case DNSAgentJSON: var jsn *DNSAgentJsonCfg if jsn, err = db.DNSAgentJsonCfg(); err != nil { diff --git a/config/apis_test.go b/config/apis_test.go index 38c4ea9db..90e723284 100644 --- a/config/apis_test.go +++ b/config/apis_test.go @@ -1668,6 +1668,22 @@ func TestLoadFromDBErr(t *testing.T) { } } +func TestLoadCfgFromDBErr(t *testing.T) { + cfg := NewDefaultCGRConfig() + generalJsonCfg := func() (*GeneralJsonCfg, error) { + + return nil, utils.ErrNotImplemented + } + jsnCfg := &mockDb{ + GeneralJsonCfgF: generalJsonCfg, + } + expected := utils.ErrNotImplemented + sections := []string{"general"} + if err := cfg.loadCfgFromDB(jsnCfg, sections, false); err == nil || err != expected { + t.Errorf("Expected %v \n but received \n %v", expected, err) + } +} + func TestLoadCfgFromDBErr2(t *testing.T) { cfg := NewDefaultCGRConfig() generalJsonCfg := func() (*GeneralJsonCfg, error) { @@ -1683,3 +1699,71 @@ func TestLoadCfgFromDBErr2(t *testing.T) { t.Errorf("Expected %v \n but received \n %v", expected, err) } } + +func TestV1GetConfig(t *testing.T) { + cfg := NewDefaultCGRConfig() + cfg.cacheDP[GeneralJSON] = &GeneralJsonCfg{ + Node_id: utils.StringPointer("randomID"), + Logger: utils.StringPointer(utils.MetaSysLog), + Log_level: utils.IntPointer(6), + Rounding_decimals: utils.IntPointer(5), + Dbdata_encoding: utils.StringPointer("msgpack"), + Tpexport_dir: utils.StringPointer("/var/spool/cgrates/tpe"), + Failed_posts_dir: utils.StringPointer("/var/spool/cgrates/failed_posts"), + Poster_attempts: utils.IntPointer(3), + Default_request_type: utils.StringPointer(utils.MetaRated), + Default_category: utils.StringPointer(utils.Call), + Default_tenant: utils.StringPointer("cgrates.org"), + Default_timezone: utils.StringPointer("Local"), + Default_caching: utils.StringPointer(utils.MetaReload), + Connect_attempts: utils.IntPointer(3), + Reconnects: utils.IntPointer(-1), + Connect_timeout: utils.StringPointer("1s"), + Reply_timeout: utils.StringPointer("2s"), + Locking_timeout: utils.StringPointer("2s"), + Digest_separator: utils.StringPointer(","), + Rsr_separator: utils.StringPointer(";"), + Digest_equal: utils.StringPointer(":"), + Failed_posts_ttl: utils.StringPointer("2ns"), + Max_parallel_conns: utils.IntPointer(100), + } + args := &SectionWithAPIOpts{ + Sections: []string{GeneralJSON}, + } + + var reply map[string]interface{} + section := &GeneralJsonCfg{ + Node_id: utils.StringPointer("randomID"), + Logger: utils.StringPointer(utils.MetaSysLog), + Log_level: utils.IntPointer(6), + Rounding_decimals: utils.IntPointer(5), + Dbdata_encoding: utils.StringPointer("msgpack"), + Tpexport_dir: utils.StringPointer("/var/spool/cgrates/tpe"), + Failed_posts_dir: utils.StringPointer("/var/spool/cgrates/failed_posts"), + Poster_attempts: utils.IntPointer(3), + Default_request_type: utils.StringPointer(utils.MetaRated), + Default_category: utils.StringPointer(utils.Call), + Default_tenant: utils.StringPointer("cgrates.org"), + Default_timezone: utils.StringPointer("Local"), + Default_caching: utils.StringPointer(utils.MetaReload), + Connect_attempts: utils.IntPointer(3), + Reconnects: utils.IntPointer(-1), + Connect_timeout: utils.StringPointer("1s"), + Reply_timeout: utils.StringPointer("2s"), + Locking_timeout: utils.StringPointer("2s"), + Digest_separator: utils.StringPointer(","), + Rsr_separator: utils.StringPointer(";"), + Digest_equal: utils.StringPointer(":"), + Failed_posts_ttl: utils.StringPointer("2ns"), + Max_parallel_conns: utils.IntPointer(100), + } + expected := map[string]interface{}{ + GeneralJSON: section, + } + if err := cfg.V1GetConfig(context.Background(), args, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, reply) { + t.Errorf("Expected %+v \n but received %+v \n", utils.ToJSON(expected), utils.ToJSON(reply)) + } + +} diff --git a/config/config_json_test.go b/config/config_json_test.go index 07dbcc516..033ffc05f 100644 --- a/config/config_json_test.go +++ b/config/config_json_test.go @@ -23,6 +23,7 @@ import ( "reflect" "testing" + "github.com/cgrates/birpc/context" "github.com/cgrates/cgrates/utils" "github.com/cgrates/rpcclient" ) @@ -2185,3 +2186,22 @@ func TestDfActionSJsonCfg(t *testing.T) { t.Errorf("\n Expected <%+v>,\nReceived:<%+v>", utils.ToJSON(eCfg), utils.ToJSON(cfg)) } } + +func TestSetSection(t *testing.T) { + jsn := ` + { + "general": { + "Node_id":2, + }, + } + ` + jsnCfg, err := NewCgrJsonCfgFromBytes([]byte(jsn)) + if err != nil { + t.Error(err) + } + payload := make(chan struct{}) + errExpect := "json: unsupported type: chan struct {}" + if err := jsnCfg.SetSection(&context.Context{}, "general", payload); err == nil || err.Error() != errExpect { + t.Error(err) + } +} diff --git a/config/config_test.go b/config/config_test.go index ff2951850..ccb9218c2 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -3348,6 +3348,38 @@ func TestCgrLoaderCfgDefault(t *testing.T) { } } +func TestLoadConfigDBCfgErr(t *testing.T) { + cfg := NewDefaultCGRConfig() + + dbJsonCfg := func(section string) (*DbJsonCfg, error) { + + return nil, utils.ErrNotImplemented + } + jsnCfg := mockDb{ + DbJsonCfgF: dbJsonCfg, + } + if err := cfg.loadConfigDBCfg(&jsnCfg); err != utils.ErrNotImplemented || err == nil { + t.Error(err) + } +} + +func TestGetReloadChan(t *testing.T) { + cfg := NewDefaultCGRConfig() + sectID := "test" + rcv := cfg.GetReloadChan(sectID) + if rcv != nil { + t.Errorf("Expected %v \b but received \n %v", nil, rcv) + } + + cfg.initChanels() + sectID = GeneralJSON + rcv = cfg.GetReloadChan(sectID) + expected := make(chan struct{}) + if len(rcv) != len(expected) { + t.Error("Channels should have the same length") + } +} + func TestCgrMigratorCfgDefault(t *testing.T) { eMgrCfg := &MigratorCgrCfg{ OutDataDBType: "redis", diff --git a/config/erscfg_test.go b/config/erscfg_test.go index 5ecf1641e..c5c36316c 100644 --- a/config/erscfg_test.go +++ b/config/erscfg_test.go @@ -55,7 +55,7 @@ func TestERSClone(t *testing.T) { ], "failed_calls_prefix": "randomPrefix", "partial_record_cache": "1s", - "partial_cache_expiry_action": "randomAction" + "partial_cache_expiry_action": "randomAction", }, ], }, @@ -201,6 +201,17 @@ func TestEventReaderloadFromJsonCase3(t *testing.T) { } } +func TestEventReaderloadFromJsonCase2(t *testing.T) { + cfgJSON := &ERsJsonCfg{ + Partial_cache_ttl: utils.StringPointer("1ss"), + } + expected := `time: unknown unit "ss" in duration "1ss"` + jsonCfg := NewDefaultCGRConfig() + if err = jsonCfg.ersCfg.loadFromJSONCfg(cfgJSON, jsonCfg.templates, jsonCfg.generalCfg.RSRSep, jsonCfg.dfltEvRdr); err == nil || err.Error() != expected { + t.Errorf("Expected %+v, received %+v", expected, err) + } +} + func TestERSLoadFromjsonCfg(t *testing.T) { expectedERsCfg := &ERsCfg{ Enabled: true, @@ -340,6 +351,82 @@ func TestERSloadFromJsonCfg(t *testing.T) { } } +func TestEventReaderloadFromJsonCfgErr1(t *testing.T) { + erS := &EventReaderCfg{ + PartialCommitFields: []*FCTemplate{ + { + Type: utils.MetaTemplate, + Value: NewRSRParsersMustCompile("1sa{*duration}", utils.InfieldSep), + }, + }, + } + jsnCfg := &EventReaderJsonCfg{ + Partial_commit_fields: &[]*FcTemplateJsonCfg{ + { + Type: utils.StringPointer(utils.MetaTemplate), + Value: utils.StringPointer("1sa{*duration}"), + }, + }, + } + expected := "time: unknown unit \"sa\" in duration \"1sa\"" + cfg := NewDefaultCGRConfig() + if err = erS.loadFromJSONCfg(jsnCfg, cfg.templates, cfg.generalCfg.RSRSep); err == nil || err.Error() != expected { + t.Errorf("Expected %+v, received %+v", expected, err) + } +} + +func TestEventReaderloadFromJsonCfgErr2(t *testing.T) { + erS := &EventReaderCfg{ + PartialCommitFields: make([]*FCTemplate, 0), + } + jsnCfg := &EventReaderJsonCfg{ + Partial_commit_fields: &[]*FcTemplateJsonCfg{ + { + Value: utils.StringPointer("a{*"), + }, + }, + } + expected := "invalid converter terminator in rule: " + cfg := NewDefaultCGRConfig() + if err = erS.loadFromJSONCfg(jsnCfg, cfg.templates, cfg.generalCfg.RSRSep); err == nil || err.Error() != expected { + t.Errorf("Expected %+v, received %+v", expected, err) + } +} + +func TestEventReaderloadFromJsonCfgErr3(t *testing.T) { + erS := &EventReaderCfg{ + PartialCommitFields: []*FCTemplate{ + { + Type: utils.MetaTemplate, + Value: NewRSRParsersMustCompile("value", utils.InfieldSep), + }, + }, + } + jsnCfg := &EventReaderJsonCfg{ + Partial_commit_fields: &[]*FcTemplateJsonCfg{ + { + Tag: utils.StringPointer("tag2"), + Type: utils.StringPointer(utils.MetaTemplate), + Value: utils.StringPointer("value"), + }, + }, + } + tmpl := FCTemplates{ + "value": { + { + Type: utils.MetaVariable, + Tag: "tag", + }, + }, + } + cfg := NewDefaultCGRConfig() + if err = erS.loadFromJSONCfg(jsnCfg, tmpl, cfg.generalCfg.RSRSep); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(erS.PartialCommitFields, tmpl["value"]) { + t.Errorf("Expected %v \n but received \n %v", erS.PartialCommitFields, tmpl["value"]) + } +} + func TestEventReaderFieldsloadFromJsonCfg(t *testing.T) { cfgJSON := &ERsJsonCfg{ Readers: &[]*EventReaderJsonCfg{ diff --git a/config/loadercgrcfg_test.go b/config/loadercgrcfg_test.go index d1731c873..2387401a3 100644 --- a/config/loadercgrcfg_test.go +++ b/config/loadercgrcfg_test.go @@ -161,3 +161,48 @@ func TestDiffLoaderCfgJson(t *testing.T) { t.Errorf("Expected %+v \n but received \n %+v", utils.ToJSON(expected), utils.ToJSON(rcv)) } } + +func TestDiffLoaderCfgJsonCase2(t *testing.T) { + var d *LoaderCfgJson + + v1 := &LoaderCgrCfg{ + TpID: "loaderID", + DataPath: "/data/path", + DisableReverse: false, + FieldSeparator: rune(22), + CachesConns: []string{"*localhost"}, + ActionSConns: []string{"*localhost"}, + GapiCredentials: nil, + GapiToken: nil, + } + + v2 := &LoaderCgrCfg{ + TpID: "loaderID2", + DataPath: "/data/path/2", + DisableReverse: true, + FieldSeparator: rune(97), + CachesConns: []string{"*birpc"}, + ActionSConns: []string{"*birpc"}, + GapiCredentials: json.RawMessage(`{"field1":"value1"}`), + GapiToken: json.RawMessage(`{"field1":"value1"}`), + } + + gapiC := json.RawMessage(`{"field1":"value1"}`) + gapiT := json.RawMessage(`{"field1":"value1"}`) + + expected := &LoaderCfgJson{ + Tpid: utils.StringPointer("loaderID2"), + Data_path: utils.StringPointer("/data/path/2"), + Disable_reverse: utils.BoolPointer(true), + Field_separator: utils.StringPointer("a"), + Caches_conns: &[]string{"*birpc"}, + Actions_conns: &[]string{"*birpc"}, + Gapi_credentials: &gapiC, + Gapi_token: &gapiT, + } + + rcv := diffLoaderCfgJson(d, v1, v2) + if !reflect.DeepEqual(rcv, expected) { + t.Errorf("Expected %+v \n but received \n %+v", utils.ToJSON(expected), utils.ToJSON(rcv)) + } +} diff --git a/config/sessionscfg_test.go b/config/sessionscfg_test.go index a62e959c9..fb3843bbe 100644 --- a/config/sessionscfg_test.go +++ b/config/sessionscfg_test.go @@ -1326,11 +1326,11 @@ func TestDiffSessionSJsonCfg(t *testing.T) { SessionTTLMaxDelay: utils.DurationPointer(1 * time.Second), SessionTTLLastUsed: utils.DurationPointer(1 * time.Second), SessionTTLLastUsage: utils.DurationPointer(1 * time.Second), - SessionIndexes: utils.StringSet{}, + SessionIndexes: nil, ClientProtocol: 12.2, ChannelSyncInterval: 1 * time.Second, TerminateAttempts: 3, - AlterableFields: utils.StringSet{}, + AlterableFields: nil, MinDurLowBalance: 1 * time.Second, ActionSConns: []string{"*localhost"}, DefaultUsage: map[string]time.Duration{ @@ -1366,13 +1366,17 @@ func TestDiffSessionSJsonCfg(t *testing.T) { SessionTTLLastUsed: utils.DurationPointer(2 * time.Second), SessionTTLLastUsage: utils.DurationPointer(2 * time.Second), SessionTTLUsage: utils.DurationPointer(2 * time.Second), - SessionIndexes: nil, + SessionIndexes: utils.StringSet{ + "index1": struct{}{}, + }, ClientProtocol: 13.2, ChannelSyncInterval: 2 * time.Second, TerminateAttempts: 5, - AlterableFields: nil, - MinDurLowBalance: 2 * time.Second, - ActionSConns: []string{"*birpc"}, + AlterableFields: utils.StringSet{ + "index1": struct{}{}, + }, + MinDurLowBalance: 2 * time.Second, + ActionSConns: []string{"*birpc"}, DefaultUsage: map[string]time.Duration{ "DFLT_1": 2 * time.Second, }, @@ -1404,11 +1408,11 @@ func TestDiffSessionSJsonCfg(t *testing.T) { Session_ttl_last_used: utils.StringPointer("2s"), Session_ttl_last_usage: utils.StringPointer("2s"), Session_ttl_usage: utils.StringPointer("2s"), - Session_indexes: nil, + Session_indexes: &[]string{"index1"}, Client_protocol: utils.Float64Pointer(13.2), Channel_sync_interval: utils.StringPointer("2s"), Terminate_attempts: utils.IntPointer(5), - Alterable_fields: nil, + Alterable_fields: &[]string{"index1"}, Min_dur_low_balance: utils.StringPointer("2s"), Actions_conns: &[]string{"*birpc"}, Default_usage: map[string]string{ diff --git a/config/thresholdscfg_test.go b/config/thresholdscfg_test.go index f0eacc6b2..914f52b74 100644 --- a/config/thresholdscfg_test.go +++ b/config/thresholdscfg_test.go @@ -149,6 +149,7 @@ func TestDiffThresholdSJsonCfg(t *testing.T) { StringIndexedFields: &[]string{"req.index1"}, PrefixIndexedFields: &[]string{"req.index2"}, SuffixIndexedFields: &[]string{"req.index3"}, + ActionSConns: []string{}, NestedFields: false, } @@ -159,6 +160,7 @@ func TestDiffThresholdSJsonCfg(t *testing.T) { StringIndexedFields: &[]string{"req.index11"}, PrefixIndexedFields: &[]string{"req.index22"}, SuffixIndexedFields: &[]string{"req.index33"}, + ActionSConns: []string{"*internal"}, NestedFields: true, } @@ -169,6 +171,7 @@ func TestDiffThresholdSJsonCfg(t *testing.T) { String_indexed_fields: &[]string{"req.index11"}, Prefix_indexed_fields: &[]string{"req.index22"}, Suffix_indexed_fields: &[]string{"req.index33"}, + Actions_conns: &[]string{"*internal"}, Nested_fields: utils.BoolPointer(true), }