From 4c8cf69f70fcc1229be62dde7dcaa53649e42e45 Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Tue, 5 Nov 2024 16:31:46 +0200 Subject: [PATCH] Auto-configure loader in test suite if needed --- apis/attributes_it_test.go | 48 ++++++++++---------------------------- engine/libtest.go | 39 +++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 40 deletions(-) diff --git a/apis/attributes_it_test.go b/apis/attributes_it_test.go index b48309c95..ef67ef4a4 100644 --- a/apis/attributes_it_test.go +++ b/apis/attributes_it_test.go @@ -22,7 +22,6 @@ along with this program. If not, see package apis import ( - "fmt" "path" "reflect" "slices" @@ -2196,47 +2195,24 @@ func testAttributesKillEngine(t *testing.T) { } func TestAttributesArith(t *testing.T) { + var dbCfg engine.DBCfg switch *utils.DBType { case utils.MetaInternal: + dbCfg = engine.InternalDBCfg case utils.MetaMySQL, utils.MetaMongo, utils.MetaPostgres: t.SkipNow() default: t.Fatal("unsupported dbtype value") } - tpInDir := t.TempDir() - - content := fmt.Sprintf(`{ - -"general": { - "log_level": 7 -}, -"data_db": { - "db_type": "*internal" -}, -"stor_db": { - "db_type": "*internal" -}, + content := `{ "attributes": { "enabled": true }, "admins": { "enabled": true -}, -"loaders": [ - { - "id": "*default", - "enabled": true, - "run_delay": "-1", - "tp_in_dir": "%s", - "tp_out_dir": "", - "action": "*store", - "opts": { - "*stopOnError": true - } - } -] -}`, tpInDir) +} +}` tpFiles := map[string]string{ utils.AttributesCsv: `#Tenant,ID,FilterIDs,Weights,Blockers,AttributeFilterIDs,AttributeBlockers,Path,Type,Value @@ -2250,8 +2226,8 @@ cgrates.org,ATTR_ARITH,,,,,,*req.MultiplyBetweenVariables,*multiply,~*req.Elem1; testEnv := engine.TestEngine{ ConfigJSON: content, - TpPath: tpInDir, TpFiles: tpFiles, + DBCfg: dbCfg, Encoding: *utils.Encoding, } client, _ := testEnv.Run(t) @@ -2302,7 +2278,7 @@ cgrates.org,ATTR_ARITH,,,,,,*req.MultiplyBetweenVariables,*multiply,~*req.Elem1; Tenant: "cgrates.org", ID: "event_test", Event: map[string]any{ - "AttrSource": "csv", + "AttrSource": "api", "Elem1": "3", "Elem2": "4", }, @@ -2310,7 +2286,7 @@ cgrates.org,ATTR_ARITH,,,,,,*req.MultiplyBetweenVariables,*multiply,~*req.Elem1; expected := engine.AttrSProcessEventReply{ AlteredFields: []*engine.FieldsAltered{ { - MatchedProfileID: "cgrates.org:ATTR_ARITH", + MatchedProfileID: "cgrates.org:ATTR_API", Fields: []string{"*req.12/4", "*req.3*4", "*req.3+4", "*req.3-4", "*req.MultiplyBetweenVariables"}, }, }, @@ -2318,7 +2294,7 @@ cgrates.org,ATTR_ARITH,,,,,,*req.MultiplyBetweenVariables,*multiply,~*req.Elem1; Tenant: "cgrates.org", ID: "event_test", Event: map[string]any{ - "AttrSource": "csv", + "AttrSource": "api", "Elem1": "3", "Elem2": "4", "12/4": "3", @@ -2344,15 +2320,15 @@ cgrates.org,ATTR_ARITH,,,,,,*req.MultiplyBetweenVariables,*multiply,~*req.Elem1; } } - ev.Event["AttrSource"] = "api" + ev.Event["AttrSource"] = "csv" expected.AlteredFields = []*engine.FieldsAltered{ { - MatchedProfileID: "cgrates.org:ATTR_API", + MatchedProfileID: "cgrates.org:ATTR_ARITH", Fields: []string{"*req.12/4", "*req.3*4", "*req.3+4", "*req.3-4", "*req.MultiplyBetweenVariables"}, }, } expected.CGREvent.Event = map[string]any{ - "AttrSource": "api", + "AttrSource": "csv", "Elem1": "3", "Elem2": "4", "12/4": "3", diff --git a/engine/libtest.go b/engine/libtest.go index a1d78932e..b95fece28 100644 --- a/engine/libtest.go +++ b/engine/libtest.go @@ -362,6 +362,12 @@ func (ng TestEngine) Run(t testing.TB, extraFlags ...string) (*birpc.Client, *co t.Helper() cfg := parseCfg(t, ng.ConfigPath, ng.ConfigJSON, ng.DBCfg) FlushDBs(t, cfg, !ng.PreserveDataDB, !ng.PreserveStorDB) + if ng.TpPath != "" || len(ng.TpFiles) != 0 { + if ng.TpPath == "" { + ng.TpPath = t.TempDir() + } + setupLoader(t, ng.TpPath, cfg.ConfigPath) + } if ng.PreStartHook != nil { ng.PreStartHook(t, cfg) } @@ -454,9 +460,33 @@ func parseCfg(t testing.TB, cfgPath, cfgJSON string, dbCfg DBCfg) (cfg *config.C return } -// loadCSVs loads tariff plan data from CSV files. The CSV files are created based on the csvFiles map, where -// the key represents the file name and the value the contains its contents. Assumes the data is loaded -// automatically (RunDelay != 0) +// setupLoader configures the *default loader to automatically load from the +// specified path. +func setupLoader(t testing.TB, tpPath, cfgPath string) { + t.Helper() + loadersJSON := fmt.Sprintf(`{ +"loaders": [{ + "id": "*default", + "enabled": true, + "run_delay": "-1", + "tp_in_dir": "%s", + "tp_out_dir": "", + "action": "*store", + "opts": { + "*stopOnError": true + } +}] +}`, tpPath) + filePath := filepath.Join(cfgPath, "zzz_dynamic_loader.json") + if err := os.WriteFile(filePath, []byte(loadersJSON), 0644); err != nil { + t.Fatal(err) + } +} + +// loadCSVs loads tariff plan data from CSV files. The CSV files are created +// based on the csvFiles map, where the key represents the file name and the +// value the contains its contents. Assumes the data is loaded automatically +// (RunDelay != 0) func loadCSVs(t testing.TB, tpPath string, csvFiles map[string]string) { t.Helper() if tpPath != "" { @@ -469,7 +499,8 @@ func loadCSVs(t testing.TB, tpPath string, csvFiles map[string]string) { } } -// FlushDBs resets the databases specified in the configuration if the corresponding flags are true. +// FlushDBs resets the databases specified in the configuration if the +// corresponding flags are true. func FlushDBs(t testing.TB, cfg *config.CGRConfig, flushDataDB, flushStorDB bool) { t.Helper() if flushDataDB {