From b4d550a1d0788b4c51693609632f4f7e1bcc7616 Mon Sep 17 00:00:00 2001 From: andronache Date: Mon, 7 Jun 2021 16:56:58 +0300 Subject: [PATCH] 100% Coverage for Config in apis and added initConfigDB func in libtest --- apis/config_it_test.go | 25 ++- apis/config_test.go | 203 ++++++++++++++++++ .../samples/apis_config_internal/cgrates.json | 10 +- .../samples/apis_config_mongo/cgrates.json | 9 +- .../samples/apis_config_mysql/cgrates.json | 8 - engine/libtest.go | 12 ++ 6 files changed, 234 insertions(+), 33 deletions(-) diff --git a/apis/config_it_test.go b/apis/config_it_test.go index c192e7845..79d98b1e6 100644 --- a/apis/config_it_test.go +++ b/apis/config_it_test.go @@ -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) } diff --git a/apis/config_test.go b/apis/config_test.go index 896306da5..82d8eea44 100644 --- a/apis/config_test.go +++ b/apis/config_test.go @@ -19,9 +19,13 @@ along with this program. If not, see 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) + } +} diff --git a/data/conf/samples/apis_config_internal/cgrates.json b/data/conf/samples/apis_config_internal/cgrates.json index 020d6e2a6..01cc28e1d 100644 --- a/data/conf/samples/apis_config_internal/cgrates.json +++ b/data/conf/samples/apis_config_internal/cgrates.json @@ -127,12 +127,6 @@ "admins_conns": ["*internal"] }, - "config_db": { - "db_type": "redis", - "db_host": "", - "db_port": 6379, - "db_name": "10", - "db_user": "", - "db_password": "" - } + + } diff --git a/data/conf/samples/apis_config_mongo/cgrates.json b/data/conf/samples/apis_config_mongo/cgrates.json index 7b1757883..97d3fadba 100644 --- a/data/conf/samples/apis_config_mongo/cgrates.json +++ b/data/conf/samples/apis_config_mongo/cgrates.json @@ -143,12 +143,5 @@ "admins_conns": ["*internal"], }, - "config_db": { - "db_type": "redis", - "db_host": "", - "db_port": 6379, - "db_name": "10", - "db_user": "", - "db_password": "" - } + } diff --git a/data/conf/samples/apis_config_mysql/cgrates.json b/data/conf/samples/apis_config_mysql/cgrates.json index 5d5d342f1..c7bbe8901 100644 --- a/data/conf/samples/apis_config_mysql/cgrates.json +++ b/data/conf/samples/apis_config_mysql/cgrates.json @@ -134,13 +134,5 @@ "admins_conns": ["*internal"], }, - "config_db": { - "db_type": "redis", - "db_host": "", - "db_port": 6379, - "db_name": "10", - "db_user": "", - "db_password": "" - }, }, diff --git a/engine/libtest.go b/engine/libtest.go index de5cfce1f..845c3b5be 100644 --- a/engine/libtest.go +++ b/engine/libtest.go @@ -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")