Revise/move unit tests that rely on external files

This commit is contained in:
ionutboangiu
2022-08-30 17:37:10 +03:00
committed by Dan Christian Bogos
parent 0fb90e27d6
commit 534f46addf
6 changed files with 208 additions and 191 deletions

View File

@@ -19,7 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package apis
import (
"path"
"reflect"
"testing"
@@ -41,9 +40,23 @@ func TestConfigNewConfigSv1(t *testing.T) {
}
func TestConfigSetGetConfig(t *testing.T) {
//for coverage purposes only
cfgTestPath := path.Join(*dataDir, "conf", "samples", "tutinternal")
cfg, err := config.NewCGRConfigFromPath(context.Background(), cfgTestPath)
cfgJSONStr := `{
"attributes": {
"enabled": true,
"stats_conns": ["*internal"],
"resources_conns": ["*internal"],
"accounts_conns": ["*internal"],
"prefix_indexed_fields": ["index1","index2"],
"opts": {
"*processRuns": [
{
"Value": 3,
},
],
},
},
}`
cfg, err := config.NewCGRConfigFromJSONStringWithDefaults(cfgJSONStr)
if err != nil {
t.Error(err)
}
@@ -65,19 +78,23 @@ func TestConfigSetGetConfig(t *testing.T) {
err = rlcCfg.GetConfig(context.Background(), argsGet, &replyGet)
expectedGet := map[string]interface{}{
"attributes": map[string]interface{}{
"accounts_conns": []string{"*localhost"},
"accounts_conns": []string{"*internal"},
"enabled": true,
"indexed_selects": true,
"nested_fields": false,
"prefix_indexed_fields": []string{},
"resources_conns": []string{"*localhost"},
"stats_conns": []string{"*localhost"},
"prefix_indexed_fields": []string{"index1", "index2"},
"resources_conns": []string{"*internal"},
"stats_conns": []string{"*internal"},
"suffix_indexed_fields": []string{},
"exists_indexed_fields": []string{},
"notexists_indexed_fields": []string{},
utils.OptsCfg: map[string]interface{}{
utils.MetaProfileIDs: []*utils.DynamicStringSliceOpt{},
utils.MetaProcessRunsCfg: []*utils.DynamicIntOpt{},
utils.MetaProfileIDs: []*utils.DynamicStringSliceOpt{},
utils.MetaProcessRunsCfg: []*utils.DynamicIntOpt{
{
Value: 3,
},
},
utils.MetaProfileRunsCfg: []*utils.DynamicIntOpt{},
utils.MetaProfileIgnoreFilters: []*utils.DynamicBoolOpt{},
},
@@ -92,13 +109,7 @@ func TestConfigSetGetConfig(t *testing.T) {
}
func TestConfigSetGetReloadConfig(t *testing.T) {
//for coverage purposes only
var err error
cfgTestPath := path.Join(*dataDir, "conf", "samples", "tutinternal")
cfg, err := config.NewCGRConfigFromPath(context.Background(), cfgTestPath)
if err != nil {
t.Error(err)
}
cfg := config.NewDefaultCGRConfig()
rlcCfg := NewConfigSv1(cfg)
args := &config.SetConfigArgs{
Tenant: utils.CGRateSorg,
@@ -126,28 +137,23 @@ func TestConfigSetGetReloadConfig(t *testing.T) {
DryRun: true,
}
var reply string
err = rlcCfg.SetConfig(context.Background(), args, &reply)
expected := `OK`
if err != nil {
t.Errorf("Expected <%+v>, \nReceived <%+v>", nil, err)
}
if expected != reply {
t.Errorf("Expected <%+v>, \nReceived <%+v>", expected, reply)
if err := rlcCfg.SetConfig(context.Background(), args, &reply); err != nil {
t.Error(err)
} else if reply != "OK" {
t.Errorf("Unexpected reply: <%s>", reply)
}
argsGet := &config.SectionWithAPIOpts{
Sections: []string{"attributes"},
}
var replyGet map[string]interface{}
errGet := rlcCfg.GetConfig(context.Background(), argsGet, &replyGet)
expectedGet := map[string]interface{}{
"attributes": map[string]interface{}{
"accounts_conns": []string{"*localhost"},
"enabled": true,
"accounts_conns": []string{},
"enabled": false,
"indexed_selects": true,
"nested_fields": false,
"prefix_indexed_fields": []string{},
"resources_conns": []string{"*localhost"},
"stats_conns": []string{"*localhost"},
"resources_conns": []string{},
"stats_conns": []string{},
"suffix_indexed_fields": []string{},
"exists_indexed_fields": []string{},
"notexists_indexed_fields": []string{},
@@ -159,10 +165,10 @@ func TestConfigSetGetReloadConfig(t *testing.T) {
},
},
}
if errGet != nil {
t.Errorf("Expected <%+v>, \nReceived <%+v>", nil, errGet)
}
if !reflect.DeepEqual(expectedGet, replyGet) {
var replyGet map[string]interface{}
if err := rlcCfg.GetConfig(context.Background(), argsGet, &replyGet); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expectedGet, replyGet) {
t.Errorf("Expected <%+v>, \nReceived <%+v>",
utils.ToJSON(expectedGet), utils.ToJSON(replyGet))
}
@@ -170,30 +176,24 @@ func TestConfigSetGetReloadConfig(t *testing.T) {
DryRun: true,
Section: "attributes",
}
experr := `path:"" is not reachable`
var replyRld string
errRld := rlcCfg.ReloadConfig(context.Background(), argsRld, &replyRld)
expectedRld := `OK`
if err != nil {
t.Errorf("Expected <%+v>, \nReceived <%+v>", nil, errRld)
}
if !reflect.DeepEqual(expectedRld, replyRld) {
t.Errorf("Expected <%+v>, \nReceived <%+v>", expectedRld, replyRld)
if err := rlcCfg.ReloadConfig(context.Background(), argsRld, &replyRld); err == nil ||
err.Error() != experr { // path is required for ReloadConfig api, but it is not provided in this unit test
t.Errorf("expected: <%s>, \nreceived: <%s>", experr, err.Error())
}
argsGetRld := &config.SectionWithAPIOpts{
Sections: []string{"attributes"},
}
var replyGetRld map[string]interface{}
errGetRld := rlcCfg.GetConfig(context.Background(), argsGetRld, &replyGetRld)
expectedGetRld := map[string]interface{}{
"attributes": map[string]interface{}{
"accounts_conns": []string{"*localhost"},
"enabled": true,
"accounts_conns": []string{},
"enabled": false,
"indexed_selects": true,
"nested_fields": false,
"prefix_indexed_fields": []string{},
"resources_conns": []string{"*localhost"},
"stats_conns": []string{"*localhost"},
"resources_conns": []string{},
"stats_conns": []string{},
"suffix_indexed_fields": []string{},
"exists_indexed_fields": []string{},
"notexists_indexed_fields": []string{},
@@ -205,22 +205,16 @@ func TestConfigSetGetReloadConfig(t *testing.T) {
},
},
}
if errGetRld != nil {
t.Errorf("Expected <%+v>, \nReceived <%+v>", nil, errGetRld)
}
if !reflect.DeepEqual(expectedGetRld, replyGetRld) {
var replyGetRld map[string]interface{}
if err := rlcCfg.GetConfig(context.Background(), argsGetRld, &replyGetRld); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expectedGetRld, replyGetRld) {
t.Errorf("Expected <%+v>, \nReceived <%+v>", utils.ToJSON(expectedGetRld), utils.ToJSON(replyGetRld))
}
}
func TestConfigGetSetConfigFromJSONErr(t *testing.T) {
//for coverage purposes only
var err error
cfgTestPath := path.Join(*dataDir, "conf", "samples", "tutinternal")
cfg, err := config.NewCGRConfigFromPath(context.Background(), cfgTestPath)
if err != nil {
t.Error(err)
}
cfg := config.NewDefaultCGRConfig()
rlcCfg := NewConfigSv1(cfg)
args := &config.SetConfigFromJSONArgs{
APIOpts: nil,
@@ -254,29 +248,24 @@ func TestConfigGetSetConfigFromJSONErr(t *testing.T) {
}`,
DryRun: true,
}
var reply string
err = rlcCfg.SetConfigFromJSON(context.Background(), args, &reply)
expected := "OK"
if err != nil {
t.Errorf("Expected <%+v>, \nReceived <%+v>", nil, err)
}
if !reflect.DeepEqual(expected, reply) {
t.Errorf("Expected <%+v>, \nReceived <%+v>", expected, reply)
if err := rlcCfg.SetConfigFromJSON(context.Background(), args, &reply); err != nil {
t.Error(err)
} else if reply != "OK" {
t.Errorf("Unexpected reply <%s>", reply)
}
argsGet := &config.SectionWithAPIOpts{
APIOpts: nil,
Tenant: utils.CGRateSorg,
Sections: []string{"attributes"},
}
expectedGet := `{"attributes":{"accounts_conns":[],"enabled":false,"exists_indexed_fields":[],"indexed_selects":true,"nested_fields":false,"notexists_indexed_fields":[],"opts":{"*processRuns":[],"*profileIDs":[],"*profileIgnoreFilters":[],"*profileRuns":[]},"prefix_indexed_fields":[],"resources_conns":[],"stats_conns":[],"suffix_indexed_fields":[]}}`
var replyGet string
errGet := rlcCfg.GetConfigAsJSON(context.Background(), argsGet, &replyGet)
expectedGet := `{"attributes":{"accounts_conns":["*localhost"],"enabled":true,"exists_indexed_fields":[],"indexed_selects":true,"nested_fields":false,"notexists_indexed_fields":[],"opts":{"*processRuns":[],"*profileIDs":[],"*profileIgnoreFilters":[],"*profileRuns":[]},"prefix_indexed_fields":[],"resources_conns":["*localhost"],"stats_conns":["*localhost"],"suffix_indexed_fields":[]}}`
if err != nil {
t.Errorf("Expected <%+v>, \nReceived <%+v>", nil, errGet)
}
if !reflect.DeepEqual(expectedGet, replyGet) {
t.Errorf("Expected <%+v>, \nReceived <%+v>", expectedGet, replyGet)
if err := rlcCfg.GetConfigAsJSON(context.Background(), argsGet, &replyGet); err != nil {
t.Error(err)
} else if replyGet != expectedGet {
t.Errorf("Expected <%s>, \nReceived <%s>", expectedGet, replyGet)
}
}

View File

@@ -141,7 +141,7 @@ func TestStopMemoryProfiling(t *testing.T) {
APIOpts: map[string]interface{}{},
}
var reply string
errExp := " Memory Profiling is not started"
errExp := "Memory Profiling is not started"
if err := cS.StopMemoryProfiling(context.Background(), args, &reply); err.Error() != errExp {
t.Errorf("Expected %v\n but received %v", errExp, err)
}

57
config/apis_it_test.go Normal file
View File

@@ -0,0 +1,57 @@
//go:build integration
// +build integration
/*
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 <http://www.gnu.org/licenses/>
*/
package config
import (
"path"
"testing"
"github.com/cgrates/birpc/context"
"github.com/cgrates/cgrates/utils"
)
func TestV1ReloadConfig(t *testing.T) {
cfg := NewDefaultCGRConfig()
cfg.db = &CgrJsonCfg{}
cfg.ConfigPath = path.Join("/usr", "share", "cgrates", "conf", "samples", "tutmongo2")
args := &ReloadArgs{
Section: utils.MetaAll,
}
cfg.rldCh = make(chan string, 100)
var reply string
if err := cfg.V1ReloadConfig(context.Background(), args, &reply); err != nil {
t.Error(err)
} else if reply != "OK" {
t.Errorf("Expected %v \n but received \n %v", "OK", reply)
}
args = &ReloadArgs{
Section: ConfigDBJSON,
}
expected := "Invalid section: <config_db> "
if err := cfg.V1ReloadConfig(context.Background(), args, &reply); err == nil || err.Error() != expected {
t.Errorf("%T and %T", expected, err.Error())
t.Errorf("Expected %q \n but received \n %q", expected, err)
}
}

View File

@@ -18,7 +18,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package config
import (
"path"
"reflect"
"testing"
"time"
@@ -895,34 +894,6 @@ func TestStoreDiffSectionAccountS(t *testing.T) {
}
}
func TestV1ReloadConfig(t *testing.T) {
cfg := NewDefaultCGRConfig()
cfg.db = &CgrJsonCfg{}
cfg.ConfigPath = path.Join("/usr", "share", "cgrates", "conf", "samples", "tutmongo2")
args := &ReloadArgs{
Section: utils.MetaAll,
}
cfg.rldCh = make(chan string, 100)
var reply string
if err := cfg.V1ReloadConfig(context.Background(), args, &reply); err != nil {
t.Error(err)
} else if reply != "OK" {
t.Errorf("Expected %v \n but received \n %v", "OK", reply)
}
args = &ReloadArgs{
Section: ConfigDBJSON,
}
expected := "Invalid section: <config_db> "
if err := cfg.V1ReloadConfig(context.Background(), args, &reply); err == nil || err.Error() != expected {
t.Errorf("%T and %T", expected, err.Error())
t.Errorf("Expected %q \n but received \n %q", expected, err)
}
}
func TestV1SetConfigErr1(t *testing.T) {
cfg := NewDefaultCGRConfig()
args := &SetConfigArgs{

View File

@@ -1484,3 +1484,90 @@ func TestGetLockFilePath(t *testing.T) {
t.Error(err)
}
}
func TestReloadCfgInDb(t *testing.T) {
cfg := NewDefaultCGRConfig()
db := &CgrJsonCfg{}
cfg.db = db
cfg.attributeSCfg = &AttributeSCfg{
Enabled: true,
ResourceSConns: []string{"*internal"},
StatSConns: []string{"*internal"},
AccountSConns: []string{"*internal"},
IndexedSelects: false,
StringIndexedFields: &[]string{"field1"},
SuffixIndexedFields: &[]string{"field1"},
PrefixIndexedFields: &[]string{"field1"},
ExistsIndexedFields: &[]string{"field1"},
NotExistsIndexedFields: &[]string{"field1"},
Opts: &AttributesOpts{
ProcessRuns: []*utils.DynamicIntOpt{
{
FilterIDs: []string{},
Value: 2,
},
},
},
NestedFields: true,
}
var reply string
cfg.sections = newSections(cfg)
cfg.rldCh = make(chan string, 100)
cfg.ConfigPath = path.Join("/usr", "share", "cgrates", "conf", "samples", "attributes_internal")
jsn := &AttributeSJsonCfg{
Enabled: utils.BoolPointer(false),
Resources_conns: &[]string{"*localhost"},
Stats_conns: &[]string{"*localhost"},
Accounts_conns: &[]string{"*localhost"},
Indexed_selects: utils.BoolPointer(true),
String_indexed_fields: &[]string{"field2"},
Suffix_indexed_fields: &[]string{"field2"},
Prefix_indexed_fields: &[]string{"field2"},
Exists_indexed_fields: &[]string{"field2"},
Notexists_indexed_fields: &[]string{"field2"},
Opts: &AttributesOptsJson{
ProcessRuns: []*utils.DynamicIntOpt{
{
Value: 3,
},
},
},
Nested_fields: utils.BoolPointer(false),
}
db.SetSection(context.Background(), AttributeSJSON, jsn)
expected := &AttributeSCfg{
Enabled: false,
ResourceSConns: []string{"*localhost"},
StatSConns: []string{"*localhost"},
AccountSConns: []string{"*localhost"},
IndexedSelects: true,
StringIndexedFields: &[]string{"field2"},
SuffixIndexedFields: &[]string{"field2"},
PrefixIndexedFields: &[]string{"field2"},
ExistsIndexedFields: &[]string{"field2"},
NotExistsIndexedFields: &[]string{"field2"},
Opts: &AttributesOpts{
ProcessRuns: []*utils.DynamicIntOpt{
{
FilterIDs: []string{},
Value: 2,
},
{
Value: 3,
},
},
},
NestedFields: false,
}
args2 := &ReloadArgs{
Section: AttributeSJSON,
}
if err := cfg.V1ReloadConfig(context.Background(), args2, &reply); err != nil {
t.Error(err)
}
rcv := cfg.AttributeSCfg()
if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(expected), utils.ToJSON(rcv))
}
}

View File

@@ -6111,93 +6111,6 @@ func TestSetNilCfgInDb(t *testing.T) {
}
}
func TestReloadCfgInDb(t *testing.T) {
cfg := NewDefaultCGRConfig()
db := &CgrJsonCfg{}
cfg.db = db
cfg.attributeSCfg = &AttributeSCfg{
Enabled: true,
ResourceSConns: []string{"*internal"},
StatSConns: []string{"*internal"},
AccountSConns: []string{"*internal"},
IndexedSelects: false,
StringIndexedFields: &[]string{"field1"},
SuffixIndexedFields: &[]string{"field1"},
PrefixIndexedFields: &[]string{"field1"},
ExistsIndexedFields: &[]string{"field1"},
NotExistsIndexedFields: &[]string{"field1"},
Opts: &AttributesOpts{
ProcessRuns: []*utils.DynamicIntOpt{
{
FilterIDs: []string{},
Value: 2,
},
},
},
NestedFields: true,
}
var reply string
cfg.sections = newSections(cfg)
cfg.rldCh = make(chan string, 100)
cfg.ConfigPath = path.Join("/usr", "share", "cgrates", "conf", "samples", "attributes_internal")
jsn := &AttributeSJsonCfg{
Enabled: utils.BoolPointer(false),
Resources_conns: &[]string{"*localhost"},
Stats_conns: &[]string{"*localhost"},
Accounts_conns: &[]string{"*localhost"},
Indexed_selects: utils.BoolPointer(true),
String_indexed_fields: &[]string{"field2"},
Suffix_indexed_fields: &[]string{"field2"},
Prefix_indexed_fields: &[]string{"field2"},
Exists_indexed_fields: &[]string{"field2"},
Notexists_indexed_fields: &[]string{"field2"},
Opts: &AttributesOptsJson{
ProcessRuns: []*utils.DynamicIntOpt{
{
Value: 3,
},
},
},
Nested_fields: utils.BoolPointer(false),
}
db.SetSection(context.Background(), AttributeSJSON, jsn)
expected := &AttributeSCfg{
Enabled: false,
ResourceSConns: []string{"*localhost"},
StatSConns: []string{"*localhost"},
AccountSConns: []string{"*localhost"},
IndexedSelects: true,
StringIndexedFields: &[]string{"field2"},
SuffixIndexedFields: &[]string{"field2"},
PrefixIndexedFields: &[]string{"field2"},
ExistsIndexedFields: &[]string{"field2"},
NotExistsIndexedFields: &[]string{"field2"},
Opts: &AttributesOpts{
ProcessRuns: []*utils.DynamicIntOpt{
{
FilterIDs: []string{},
Value: 2,
},
{
Value: 3,
},
},
},
NestedFields: false,
}
args2 := &ReloadArgs{
Section: AttributeSJSON,
}
if err := cfg.V1ReloadConfig(context.Background(), args2, &reply); err != nil {
t.Error(err)
}
rcv := cfg.AttributeSCfg()
if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expected %v \n but received \n %v", utils.ToJSON(expected), utils.ToJSON(rcv))
}
}
func TestAsteriskAgentLoadFromJSONCfgNil(t *testing.T) {
acCfg := &AsteriskAgentCfg{}
if err := acCfg.loadFromJSONCfg(nil); err != nil {