From 200fb5498b00d5fe281689f96cac5381fb3c4809 Mon Sep 17 00:00:00 2001 From: nickolasdaniel Date: Tue, 17 Aug 2021 16:43:28 +0300 Subject: [PATCH] Tested cgr-loader tenant flag --- cmd/cgr-loader/cgr-loader_it_test.go | 72 +++++++++++++++++++-- cmd/cgr-loader/cgr-loader_remove_it_test.go | 5 +- 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/cmd/cgr-loader/cgr-loader_it_test.go b/cmd/cgr-loader/cgr-loader_it_test.go index ff587cce2..4e8464760 100644 --- a/cmd/cgr-loader/cgr-loader_it_test.go +++ b/cmd/cgr-loader/cgr-loader_it_test.go @@ -24,6 +24,14 @@ import ( "bytes" "errors" "flag" + "fmt" + "net" + "os/exec" + "path" + "reflect" + "sort" + "testing" + "github.com/cgrates/birpc" "github.com/cgrates/birpc/context" "github.com/cgrates/birpc/jsonrpc" @@ -31,11 +39,6 @@ import ( "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" "github.com/cgrates/rpcclient" - "os/exec" - "path" - "reflect" - "sort" - "testing" ) var ( @@ -226,6 +229,9 @@ var ( testLoadItResetDataDB, testLoadItResetStorDb, testLoadItStartLoader, + testLoadItCheckTenantFlag, + testLoadItStartLoaderWithTenant, + testLoadItConnectToDB, testLoadItCheckAttributes, testLoadItStartLoaderRemove, @@ -381,6 +387,62 @@ func testLoadItStartLoaderFromStorDB(t *testing.T) { } } +func testLoadItStartLoaderWithTenant(t *testing.T) { + cmd := exec.Command("cgr-loader", "-config_path="+ldrItCfgPath, "-path="+path.Join(*dataDir, "tariffplans", "tutorial"), fmt.Sprintf("-caches_address=%s", address), "-scheduler_address=", `-tenant="tenant.com"`, "-verbose") + output := bytes.NewBuffer(nil) + cmd.Stdout = output + if err := cmd.Run(); err != nil { + t.Log(cmd.Args) + t.Log(output.String()) + t.Fatal(err) + } + listener.Close() + if resp != "\"tenant.com\"" { + t.Errorf("Expected \"tenant.com\" \n but received \n %q", resp) + } +} + +type mockCache int + +func (c *mockCache) ReloadCache(ctx *context.Context, args *utils.AttrReloadCacheWithAPIOpts, reply *string) (err error) { + resp = args.Tenant + *reply = "OK" + return nil +} + +func (c *mockCache) Clear(ctx *context.Context, args *utils.AttrCacheIDsWithAPIOpts, + reply *string) error { + *reply = args.Tenant + return nil +} + +var address string +var listener net.Listener +var resp string + +func testLoadItCheckTenantFlag(t *testing.T) { + err := birpc.RegisterName("CacheSv1", new(mockCache)) + if err != nil { + t.Error(err) + } + + listener, err = net.Listen("tcp", ":0") + if err != nil { + t.Error(err) + } + address = listener.Addr().String() + + go func() { + for { + conn, err := listener.Accept() + if err != nil { + return + } + go birpc.ServeCodec(jsonrpc.NewServerCodec(conn)) + } + }() +} + func testLoadItStartLoaderFlushStorDB(t *testing.T) { cmd := exec.Command("cgr-loader", "-config_path="+ldrItCfgPath, "-path="+path.Join(*dataDir, "tariffplans", "dispatchers"), "-caches_address=", "-scheduler_address=", "-to_stordb", "-flush_stordb", "-tpid=TPID") output := bytes.NewBuffer(nil) diff --git a/cmd/cgr-loader/cgr-loader_remove_it_test.go b/cmd/cgr-loader/cgr-loader_remove_it_test.go index fa9dcdb1b..571810b57 100644 --- a/cmd/cgr-loader/cgr-loader_remove_it_test.go +++ b/cmd/cgr-loader/cgr-loader_remove_it_test.go @@ -31,9 +31,6 @@ import ( "github.com/cgrates/birpc" "github.com/cgrates/birpc/context" - "github.com/cgrates/cgrates/config" - "github.com/cgrates/cgrates/engine" - "github.com/cgrates/cgrates/utils" "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/engine" @@ -223,7 +220,7 @@ func testCgrLdrGetSubsystemsNotLoadedLoad(t *testing.T) { //chargers var replyChrgr *engine.ChargerProfile - if err := cgrLdrBIRPC.Call(context.Background(), utils.APIerSv1GetChargerProfile, + if err := cgrLdrBIRPC.Call(context.Background(), utils.AdminSv1GetChargerProfile, &utils.TenantID{Tenant: "cgrates.org", ID: "Raw"}, &replyChrgr); err == nil || err.Error() != utils.ErrNotFound.Error() { t.Errorf("Expected %+v, received %v", utils.ErrNotFound.Error(), err)