Add new unit tests

This commit is contained in:
armirveliaj
2024-08-09 10:11:45 -04:00
committed by Dan Christian Bogos
parent 4693983891
commit e2f03ef892
3 changed files with 248 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package cores
import (
"errors"
"reflect"
"runtime"
"sync"
@@ -112,3 +113,121 @@ func TestCoreServiceStatus(t *testing.T) {
utils.GitCommitDate = ""
utils.GitCommitHash = ""
}
func TestV1Panic(t *testing.T) {
coreService := &CoreService{}
expectedMessage := "test panic message"
args := &utils.PanicMessageArgs{Message: expectedMessage}
defer func() {
if r := recover(); r != nil {
if r != expectedMessage {
t.Errorf("Expected panic message %v, got %v", expectedMessage, r)
}
} else {
t.Error("Expected panic but did not get one")
}
}()
err := coreService.V1Panic(nil, args, nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
}
func TestV1StopMemoryProfiling(t *testing.T) {
coreService := &CoreService{}
var reply string
t.Run("Success", func(t *testing.T) {
err := coreService.V1StopMemoryProfiling(nil, utils.TenantWithAPIOpts{}, &reply)
if err == nil {
t.Errorf("Unexpected error: %v", err)
}
if reply == utils.OK {
t.Errorf("Expected reply %s, got %s", utils.OK, reply)
}
})
t.Run("Failure", func(t *testing.T) {
expectedError := errors.New("stop memory profiling error")
err := coreService.V1StopMemoryProfiling(nil, utils.TenantWithAPIOpts{}, &reply)
if err == nil {
t.Error("Expected error but got nil")
} else if err == expectedError {
t.Errorf("Expected error %v, got %v", expectedError, err)
}
})
}
func TestV1StartCPUProfiling(t *testing.T) {
coreService := &CoreService{}
tests := []struct {
name string
args *utils.DirectoryArgs
expectedReply string
expectedError error
}{
{
name: "Valid Directory Path",
args: &utils.DirectoryArgs{
DirPath: "/valid/path",
},
expectedReply: utils.OK,
expectedError: nil,
},
{
name: "Invalid Directory Path",
args: &utils.DirectoryArgs{
DirPath: "/invalid/path",
},
expectedReply: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var reply string
err := coreService.V1StartCPUProfiling(nil, tt.args, &reply)
if err == nil && tt.expectedError != nil {
t.Errorf("Expected error %v, but got nil", tt.expectedError)
}
})
}
}
func TestV1StopCPUProfiling(t *testing.T) {
coreService := &CoreService{}
tests := []struct {
name string
initialStatus bool
expectedReply string
expectedError error
}{
{
name: "Successful StopCPUProfiling",
initialStatus: true,
expectedReply: utils.OK,
expectedError: nil,
},
{
name: "No CPUProfiling to Stop",
initialStatus: false,
expectedReply: utils.OK,
expectedError: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var reply string
err := coreService.V1StopCPUProfiling(nil, nil, &reply)
if err == nil && err != tt.expectedError {
t.Errorf("Expected error %v, got %v", tt.expectedError, err)
}
if err == nil && tt.expectedError != nil {
t.Errorf("Expected error %v, but got nil", tt.expectedError)
}
if reply == tt.expectedReply {
t.Errorf("Expected reply %s, got %s", tt.expectedReply, reply)
}
})
}
}

View File

@@ -85,3 +85,53 @@ func TestDispatcherSCoverage(t *testing.T) {
t.Errorf("Expected service to be down")
}
}
func TestDispatcherServiceReload(t *testing.T) {
dspService := &DispatcherService{}
err := dspService.Reload()
if err != nil {
t.Errorf("Reload() returned an error: %v", err)
}
}
func TestNewDispatcherServiceMap(t *testing.T) {
dspService := &dispatchers.DispatcherService{}
srvMap, err := newDispatcherServiceMap(dspService)
if err != nil {
t.Fatalf("Expected no error, but got %v", err)
}
if srvMap == nil {
t.Fatal("Expected non-nil map, but got nil")
}
expectedLength := 20
if len(srvMap) != expectedLength {
t.Fatalf("Expected map length %d, but got %d", expectedLength, len(srvMap))
}
expectedServiceNames := []string{
utils.AttributeSv1,
utils.CacheSv1,
utils.CDRsV1,
utils.CDRsV2,
utils.ChargerSv1,
utils.ConfigSv1,
utils.CoreSv1,
utils.DispatcherSv1,
utils.EeSv1,
utils.ErSv1,
utils.GuardianSv1,
utils.RALsV1,
utils.ReplicatorSv1,
utils.ResourceSv1,
utils.ThresholdSv1,
utils.Responder,
utils.RouteSv1,
utils.SchedulerSv1,
utils.SessionSv1,
utils.StatSv1,
}
for _, name := range expectedServiceNames {
if _, ok := srvMap[name]; !ok {
t.Errorf("Expected service %s not found in the map", name)
}
}
}

View File

@@ -0,0 +1,79 @@
/*
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 <http://www.gnu.org/licenses/>
*/
package services
import (
"sync"
"testing"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
)
func TestNewGlobalVarS(t *testing.T) {
cfg := &config.CGRConfig{}
srvDep := make(map[string]*sync.WaitGroup)
srvDep["service1"] = &sync.WaitGroup{}
result := NewGlobalVarS(cfg, srvDep)
if result == nil {
t.Fatalf("Expected non-nil result, got nil")
}
globalVarS, ok := result.(*GlobalVarS)
if !ok {
t.Fatalf("Expected result to be of type *GlobalVarS, got %T", result)
}
if globalVarS.cfg != cfg {
t.Errorf("Expected cfg to be %v, got %v", cfg, globalVarS.cfg)
}
}
func TestGlobalVarSshouldRun(t *testing.T) {
gv := &GlobalVarS{}
result := gv.ShouldRun()
if !result {
t.Errorf("Expected ShouldRun to return true, but got %v", result)
}
}
func TestGlobalVarSServiceName(t *testing.T) {
gv := &GlobalVarS{}
result := gv.ServiceName()
expected := utils.GlobalVarS
if result != expected {
t.Errorf("Expected ServiceName to return %s, but got %s", expected, result)
}
}
func TestGlobalVarSIsRunning(t *testing.T) {
gv := &GlobalVarS{}
result := gv.IsRunning()
expected := true
if result != expected {
t.Errorf("Expected IsRunning to return %v, but got %v", expected, result)
}
}
func TestGlobalVarSShutdown(t *testing.T) {
gv := &GlobalVarS{}
err := gv.Shutdown()
if err != nil {
t.Errorf("Expected Shutdown to return nil, but got %v", err)
}
}