From 4961e53956ca3b48ffcee9d66b2c7c4ccbd14cfb Mon Sep 17 00:00:00 2001 From: Trial97 Date: Thu, 25 Jun 2020 13:19:20 +0300 Subject: [PATCH] Added integration test for ApierS service without RALs enabled --- services/apiers_it_test.go | 112 +++++++++++++++++++++++++++++++++++++ services/apierv1.go | 2 +- services/apierv2.go | 2 +- services/stordb.go | 2 +- 4 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 services/apiers_it_test.go diff --git a/services/apiers_it_test.go b/services/apiers_it_test.go new file mode 100644 index 000000000..590a481b3 --- /dev/null +++ b/services/apiers_it_test.go @@ -0,0 +1,112 @@ +// +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 +*/ +package services + +import ( + "path" + "testing" + "time" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/servmanager" + "github.com/cgrates/cgrates/utils" + "github.com/cgrates/rpcclient" +) + +func TestApiersReload(t *testing.T) { + cfg, err := config.NewDefaultCGRConfig() + if err != nil { + t.Fatal(err) + } + utils.Newlogger(utils.MetaSysLog, cfg.GeneralCfg().NodeID) + utils.Logger.SetLogLevel(7) + filterSChan := make(chan *engine.FilterS, 1) + filterSChan <- nil + engineShutdown := make(chan bool, 1) + chS := engine.NewCacheS(cfg, nil) + close(chS.GetPrecacheChannel(utils.CacheThresholdProfiles)) + close(chS.GetPrecacheChannel(utils.CacheThresholds)) + close(chS.GetPrecacheChannel(utils.CacheThresholdFilterIndexes)) + + cfg.ThresholdSCfg().Enabled = true + cfg.SchedulerCfg().Enabled = true + server := utils.NewServer() + srvMngr := servmanager.NewServiceManager(cfg, engineShutdown) + db := NewDataDBService(cfg, nil) + cfg.StorDbCfg().Type = utils.INTERNAL + stordb := NewStorDBService(cfg) + schS := NewSchedulerService(cfg, db, chS, filterSChan, server, make(chan rpcclient.ClientConnector, 1), nil) + tS := NewThresholdService(cfg, db, chS, filterSChan, server, make(chan rpcclient.ClientConnector, 1)) + + apiSv1 := NewAPIerSv1Service(cfg, db, stordb, filterSChan, server, schS, new(ResponderService), + make(chan rpcclient.ClientConnector, 1), nil) + + apiSv2 := NewAPIerSv2Service(apiSv1, cfg, server, make(chan rpcclient.ClientConnector, 1)) + srvMngr.AddServices(apiSv1, apiSv2, schS, tS, + NewLoaderService(cfg, db, filterSChan, server, engineShutdown, make(chan rpcclient.ClientConnector, 1), nil), db, stordb) + if err = srvMngr.StartServices(); err != nil { + t.Error(err) + } + if apiSv1.IsRunning() { + t.Errorf("Expected service to be down") + } + if apiSv2.IsRunning() { + t.Errorf("Expected service to be down") + } + if db.IsRunning() { + t.Errorf("Expected service to be down") + } + if stordb.IsRunning() { + t.Errorf("Expected service to be down") + } + var reply string + if err := cfg.V1ReloadConfigFromPath(&config.ConfigReloadWithArgDispatcher{ + Path: path.Join("/usr", "share", "cgrates", "conf", "samples", "tutmongo"), + Section: config.ApierS, + }, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("Expecting OK ,received %s", reply) + } + time.Sleep(10 * time.Millisecond) //need to switch to gorutine + if !apiSv1.IsRunning() { + t.Errorf("Expected service to be running") + } + if !apiSv2.IsRunning() { + t.Errorf("Expected service to be running") + } + if !db.IsRunning() { + t.Errorf("Expected service to be running") + } + if !stordb.IsRunning() { + t.Errorf("Expected service to be running") + } + cfg.ApierCfg().Enabled = false + cfg.GetReloadChan(config.ApierS) <- struct{}{} + time.Sleep(10 * time.Millisecond) + if apiSv1.IsRunning() { + t.Errorf("Expected service to be down") + } + if apiSv2.IsRunning() { + t.Errorf("Expected service to be down") + } + engineShutdown <- true +} diff --git a/services/apierv1.go b/services/apierv1.go index 197612830..91a97da8d 100644 --- a/services/apierv1.go +++ b/services/apierv1.go @@ -167,7 +167,7 @@ func (apiService *APIerSv1Service) GetAPIerSv1() *v1.APIerSv1 { // ShouldRun returns if the service should be running func (apiService *APIerSv1Service) ShouldRun() bool { - return apiService.cfg.RalsCfg().Enabled + return apiService.cfg.ApierCfg().Enabled } // GetDMChan returns the DataManager chanel diff --git a/services/apierv2.go b/services/apierv2.go index d0faf254d..6faded50e 100644 --- a/services/apierv2.go +++ b/services/apierv2.go @@ -109,5 +109,5 @@ func (api *APIerSv2Service) ServiceName() string { // ShouldRun returns if the service should be running func (api *APIerSv2Service) ShouldRun() bool { - return api.cfg.RalsCfg().Enabled + return api.cfg.ApierCfg().Enabled } diff --git a/services/stordb.go b/services/stordb.go index a08f6f2fb..6d1aeab59 100644 --- a/services/stordb.go +++ b/services/stordb.go @@ -160,7 +160,7 @@ func (db *StorDBService) ServiceName() string { // ShouldRun returns if the service should be running func (db *StorDBService) ShouldRun() bool { - return db.cfg.RalsCfg().Enabled || db.cfg.CdrsCfg().Enabled + return db.cfg.RalsCfg().Enabled || db.cfg.CdrsCfg().Enabled || db.cfg.ApierCfg().Enabled } // RegisterSyncChan used by dependent subsystems to register a chanel to reload only the storDB(thread safe)