100% Coverage for Config in apis and added initConfigDB func in libtest

This commit is contained in:
andronache
2021-06-07 16:56:58 +03:00
committed by Dan Christian Bogos
parent 89cebf4e0e
commit b4d550a1d0
6 changed files with 234 additions and 33 deletions

View File

@@ -49,7 +49,7 @@ var (
testCfgSetGetConfig,
testCfgSetEmptyReload,
testCfgSetJSONGetJSONConfig,
testCfgEngine,
testCfgKillEngine,
//Store Cfg in Database Test
testCfgInitCfgStore,
testCfgInitCfgStore,
@@ -57,18 +57,18 @@ var (
testCfgResetStorDbStore,
testCfgStartEngineStore,
testCfgRPCConnStore,
testCfgEngineStore,
testCfgKillEngineStore,
}
)
func TestCfgSIT(t *testing.T) {
switch *dbType {
case utils.MetaInternal:
cfgDIR = "tutinternal"
cfgDIR = "apis_config_internal"
case utils.MetaMongo:
cfgDIR = "tutmongo"
cfgDIR = "apis_config_mongo"
case utils.MetaMySQL:
cfgDIR = "tutmysql"
cfgDIR = "apis_config_mysql"
case utils.MetaPostgres:
t.SkipNow()
default:
@@ -222,7 +222,8 @@ func testCfgSetEmptyReload(t *testing.T) {
Tenant: "",
Config: map[string]interface{}{
"rates": map[string]interface{}{
"enabled": true,
"enabled": true,
"indexed_selects": false,
},
},
DryRun: false,
@@ -250,7 +251,7 @@ func testCfgSetEmptyReload(t *testing.T) {
expectedGet := map[string]interface{}{
"rates": map[string]interface{}{
"enabled": true,
"indexed_selects": true,
"indexed_selects": false,
"nested_fields": false,
"prefix_indexed_fields": []string{},
"rate_indexed_selects": true,
@@ -317,7 +318,7 @@ func testCfgInitCfgStore(t *testing.T) {
}
}
func testCfgEngine(t *testing.T) {
func testCfgKillEngine(t *testing.T) {
if err := engine.KillEngine(100); err != nil {
t.Error(err)
}
@@ -335,6 +336,12 @@ func testCfgResetStorDbStore(t *testing.T) {
}
}
func testCfgResetConfigDBStore(t *testing.T) {
if err := engine.InitConfigDB(cfgCfg); err != nil {
t.Fatal(err)
}
}
// Start CGR Engine
func testCfgStartEngineStore(t *testing.T) {
if _, err := engine.StopStartEngine(cfgPath, *waitRater); err != nil {
@@ -350,7 +357,7 @@ func testCfgRPCConnStore(t *testing.T) {
}
}
func testCfgEngineStore(t *testing.T) {
func testCfgKillEngineStore(t *testing.T) {
if err := engine.KillEngine(100); err != nil {
t.Error(err)
}

View File

@@ -19,9 +19,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package apis
import (
"path"
"reflect"
"testing"
"github.com/cgrates/cgrates/utils"
"github.com/cgrates/birpc/context"
"github.com/cgrates/cgrates/config"
)
@@ -35,3 +39,202 @@ func TestConfigNewConfigSv1(t *testing.T) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, result)
}
}
func TestConfigSetGetConfig(t *testing.T) {
//for coverage purposes only
var err error
cfgTestPath := path.Join(*dataDir, "conf", "samples", "tutinternal")
cfg, err := config.NewCGRConfigFromPath(cfgTestPath)
if err != nil {
t.Error(err)
}
rlcCfg := NewConfigSv1(cfg)
args := &config.SetConfigArgs{}
var reply string
err = rlcCfg.SetConfig(context.Background(), args, &reply)
expected := `OK`
if err != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err)
}
if !reflect.DeepEqual(expected, reply) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, 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{}{
"admins_conns": []string{"*localhost"},
"enabled": true,
"indexed_selects": true,
"nested_fields": false,
"prefix_indexed_fields": []string{},
"process_runs": 1,
"resources_conns": []string{"*localhost"},
"stats_conns": []string{"*localhost"},
"suffix_indexed_fields": []string{},
},
}
if errGet != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, errGet)
}
if !reflect.DeepEqual(expectedGet, replyGet) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expectedGet, replyGet)
}
}
func TestConfigSetGetReloadConfig(t *testing.T) {
//for coverage purposes only
var err error
cfgTestPath := path.Join(*dataDir, "conf", "samples", "tutinternal")
cfg, err := config.NewCGRConfigFromPath(cfgTestPath)
if err != nil {
t.Error(err)
}
rlcCfg := NewConfigSv1(cfg)
args := &config.SetConfigArgs{
APIOpts: nil,
Tenant: utils.CGRateSorg,
Config: map[string]interface{}{
"attributes": map[string]interface{}{
"admins_conns": []string{"*internal"},
"enabled": true,
"indexed_selects": false,
"nested_fields": false,
"prefix_indexed_fields": []string{},
"process_runs": 2,
"resources_conns": []string{"*internal"},
"stats_conns": []string{"*internal"},
"suffix_indexed_fields": []string{},
},
},
DryRun: true,
}
var reply string
err = rlcCfg.SetConfig(context.Background(), args, &reply)
expected := `OK`
if err != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err)
}
if !reflect.DeepEqual(expected, reply) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, 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{}{
"admins_conns": []string{"*localhost"},
"enabled": true,
"indexed_selects": true,
"nested_fields": false,
"prefix_indexed_fields": []string{},
"process_runs": 1,
"resources_conns": []string{"*localhost"},
"stats_conns": []string{"*localhost"},
"suffix_indexed_fields": []string{},
},
}
if errGet != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, errGet)
}
if !reflect.DeepEqual(expectedGet, replyGet) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expectedGet, replyGet)
}
argsRld := &config.ReloadArgs{
DryRun: true,
Section: "attributes",
}
var replyRld string
errRld := rlcCfg.ReloadConfig(context.Background(), argsRld, &replyRld)
expectedRld := `OK`
if err != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, errRld)
}
if !reflect.DeepEqual(expectedRld, replyRld) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expectedRld, replyRld)
}
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{}{
"admins_conns": []string{"*localhost"},
"enabled": true,
"indexed_selects": true,
"nested_fields": false,
"prefix_indexed_fields": []string{},
"process_runs": 1,
"resources_conns": []string{"*localhost"},
"stats_conns": []string{"*localhost"},
"suffix_indexed_fields": []string{},
},
}
if errGetRld != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, errGetRld)
}
if !reflect.DeepEqual(expectedGetRld, replyGetRld) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expectedGetRld, replyGetRld)
}
}
func TestConfigGetSetConfigFromJSONErr(t *testing.T) {
//for coverage purposes only
var err error
cfgTestPath := path.Join(*dataDir, "conf", "samples", "tutinternal")
cfg, err := config.NewCGRConfigFromPath(cfgTestPath)
if err != nil {
t.Error(err)
}
rlcCfg := NewConfigSv1(cfg)
args := &config.SetConfigFromJSONArgs{
APIOpts: nil,
Tenant: utils.CGRateSorg,
Config: "{\"attributes\":{\"admins_conns\":[\"*internal\"],\"enabled\":true,\"indexed_selects\":false,\"nested_fields\":false,\"prefix_indexed_fields\":[],\"process_runs\":2,\"resources_conns\":[\"*internal\"],\"stats_conns\":[\"*localhost\"],\"suffix_indexed_fields\":[]}}",
DryRun: true,
}
var reply string
err = rlcCfg.SetConfigFromJSON(context.Background(), args, &reply)
expected := "OK"
if err != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err)
}
if !reflect.DeepEqual(expected, reply) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, reply)
}
argsGet := &config.SectionWithAPIOpts{
APIOpts: nil,
Tenant: utils.CGRateSorg,
Sections: []string{"attributes"},
}
var replyGet string
errGet := rlcCfg.GetConfigAsJSON(context.Background(), argsGet, &replyGet)
expectedGet := "{\"attributes\":{\"admins_conns\":[\"*localhost\"],\"enabled\":true,\"indexed_selects\":true,\"nested_fields\":false,\"prefix_indexed_fields\":[],\"process_runs\":1,\"resources_conns\":[\"*localhost\"],\"stats_conns\":[\"*localhost\"],\"suffix_indexed_fields\":[]}}"
if err != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, errGet)
}
if !reflect.DeepEqual(expectedGet, replyGet) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expectedGet, replyGet)
}
}
func TestConfigStoreCfgInDBErr(t *testing.T) {
//for coverage purposes only
cfg := config.NewDefaultCGRConfig()
rlcCfg := NewConfigSv1(cfg)
args := &config.SectionWithAPIOpts{}
var reply string
err := rlcCfg.StoreCfgInDB(context.Background(), args, &reply)
expected := "no DB connection for config"
if err == nil || err.Error() != expected {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err)
}
}

View File

@@ -127,12 +127,6 @@
"admins_conns": ["*internal"]
},
"config_db": {
"db_type": "redis",
"db_host": "",
"db_port": 6379,
"db_name": "10",
"db_user": "",
"db_password": ""
}
}

View File

@@ -143,12 +143,5 @@
"admins_conns": ["*internal"],
},
"config_db": {
"db_type": "redis",
"db_host": "",
"db_port": 6379,
"db_name": "10",
"db_user": "",
"db_password": ""
}
}

View File

@@ -134,13 +134,5 @@
"admins_conns": ["*internal"],
},
"config_db": {
"db_type": "redis",
"db_host": "",
"db_port": 6379,
"db_name": "10",
"db_user": "",
"db_password": ""
},
},

View File

@@ -206,6 +206,18 @@ func InitStorDB(cfg *config.CGRConfig) error {
return nil
}
func InitConfigDB(cfg *config.CGRConfig) error {
d, err := NewDataDBConn(cfg.DataDbCfg().Type,
cfg.DataDbCfg().Host, cfg.DataDbCfg().Port,
cfg.DataDbCfg().Name, cfg.DataDbCfg().User,
cfg.DataDbCfg().Password, cfg.GeneralCfg().DBDataEncoding,
cfg.DataDbCfg().Opts)
if err != nil {
return err
}
return d.Flush("")
}
// Return reference towards the command started so we can stop it if necessary
func StartEngine(cfgPath string, waitEngine int) (*exec.Cmd, error) {
enginePath, err := exec.LookPath("cgr-engine")