Add coverage tests on agents and config

This commit is contained in:
armirveliaj
2024-09-10 10:31:05 -04:00
committed by Dan Christian Bogos
parent 7131698daf
commit 1854afd74d
4 changed files with 222 additions and 0 deletions

View File

@@ -1587,3 +1587,47 @@ func TestFseventV1TerminateSessionArgs(t *testing.T) {
t.Errorf("Expected TerminateSession to be true, got false")
}
}
func TestFsEventGetSubject(t *testing.T) {
tests := []struct {
name string
fsev FSEvent
fieldName string
expectedOutput string
}{
{
name: "Static Value Prefix",
fsev: FSEvent{},
fieldName: utils.StaticValuePrefix + "StaticValue",
expectedOutput: "StaticValue",
},
{
name: "Field Name Present in FSEvent Map",
fsev: FSEvent{"subjectField": "SubjectValue"},
fieldName: "subjectField",
expectedOutput: "SubjectValue",
},
{
name: "Field Name Not Present, Fallback to SUBJECT",
fsev: FSEvent{SUBJECT: "SubjectFallback"},
fieldName: "nonExistentField",
expectedOutput: "SubjectFallback",
},
{
name: "Field Name Not Present, SUBJECT Empty, GetAccount Empty",
fsev: FSEvent{SUBJECT: "", "accountField": ""},
fieldName: "nonExistentField",
expectedOutput: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.fsev.GetSubject(tt.fieldName)
if got != tt.expectedOutput {
t.Errorf("GetSubject() got = %v, expected %v", got, tt.expectedOutput)
}
})
}
}

View File

@@ -1032,3 +1032,68 @@ func TestKamEventMissingParameterUnsupportedEvent(t *testing.T) {
t.Errorf("Expected true for unsupported event, got false")
}
}
func TestKamReplyString(t *testing.T) {
tests := []struct {
name string
reply KamReply
expected string
}{
{
name: "Complete data",
reply: KamReply{
Event: "event1",
TransactionIndex: "index1",
TransactionLabel: "label1",
Attributes: "attr1",
ResourceAllocation: "resource1",
MaxUsage: 100,
Routes: "route1,route2",
Thresholds: "threshold1",
StatQueues: "queue1",
Error: "none",
},
expected: `{"Event":"event1","TransactionIndex":"index1","TransactionLabel":"label1","Attributes":"attr1","ResourceAllocation":"resource1","MaxUsage":100,"Routes":"route1,route2","Thresholds":"threshold1","StatQueues":"queue1","Error":"none"}`,
},
{
name: "Empty fields",
reply: KamReply{
Event: "",
TransactionIndex: "",
TransactionLabel: "",
Attributes: "",
ResourceAllocation: "",
MaxUsage: 0,
Routes: "",
Thresholds: "",
StatQueues: "",
Error: "",
},
expected: `{"Event":"","TransactionIndex":"","TransactionLabel":"","Attributes":"","ResourceAllocation":"","MaxUsage":0,"Routes":"","Thresholds":"","StatQueues":"","Error":""}`,
},
{
name: "Zero MaxUsage",
reply: KamReply{
Event: "event2",
TransactionIndex: "index2",
TransactionLabel: "label2",
Attributes: "attr2",
ResourceAllocation: "resource2",
MaxUsage: 0,
Routes: "route3",
Thresholds: "threshold2",
StatQueues: "queue2",
Error: "error2",
},
expected: `{"Event":"event2","TransactionIndex":"index2","TransactionLabel":"label2","Attributes":"attr2","ResourceAllocation":"resource2","MaxUsage":0,"Routes":"route3","Thresholds":"threshold2","StatQueues":"queue2","Error":"error2"}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := tt.reply.String()
if actual != tt.expected {
t.Errorf("Test %s failed: expected %s, got %s", tt.name, tt.expected, actual)
}
})
}
}

View File

@@ -1880,3 +1880,24 @@ func TestLibDnsUpdateDnsOptionCase1(t *testing.T) {
}
}
func TestNewDnsDP(t *testing.T) {
req := &dns.Msg{}
dp := newDnsDP(req)
dnsDP, ok := dp.(*dnsDP)
if !ok {
t.Fatalf("Expected type *dnsDP, got %T", dp)
}
if dnsDP.req == nil {
t.Error("Expected non-nil req field")
}
if dnsDP.opts == nil {
t.Error("Expected non-nil opts field")
}
}

View File

@@ -22,6 +22,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"path"
"reflect"
"strings"
"testing"
@@ -156,3 +157,94 @@ func TestHandleConfigSFileFileReadError(t *testing.T) {
}
}
func TestHandleConfigSFolder(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "configtest")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tmpDir)
w := httptest.NewRecorder()
handleConfigSFolder("/invalid/path", w)
resp := w.Result()
if resp.StatusCode != http.StatusInternalServerError {
t.Errorf("Expected status 500, but got %d", resp.StatusCode)
}
jsonConfig := `{"Subject": "1001"}`
configFilePath := tmpDir + "/config.json"
err = os.WriteFile(configFilePath, []byte(jsonConfig), 0644)
if err != nil {
t.Fatalf("Failed to write config file: %v", err)
}
w = httptest.NewRecorder()
handleConfigSFolder(tmpDir, w)
resp = w.Result()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status 200, but got %d", resp.StatusCode)
}
}
func TestHandlerConfigS(t *testing.T) {
tmpRootDir, err := os.MkdirTemp("", "test_config_root")
if err != nil {
t.Fatalf("Failed to create temporary root directory: %v", err)
}
defer os.RemoveAll(tmpRootDir)
originalRootDir := CgrConfig().ConfigSCfg().RootDir
CgrConfig().ConfigSCfg().RootDir = tmpRootDir
defer func() {
CgrConfig().ConfigSCfg().RootDir = originalRootDir
}()
filePath := path.Join(tmpRootDir, "test_file.txt")
file, err := os.Create(filePath)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
file.Close()
dirPath := path.Join(tmpRootDir, "test_folder")
err = os.Mkdir(dirPath, 0755)
if err != nil {
t.Fatalf("Failed to create test directory: %v", err)
}
tests := []struct {
name string
requestPath string
expectedCode int
expectedBody string
}{
{
name: "File exists",
requestPath: "/configs/test_file.txt",
expectedCode: 200,
expectedBody: "Handled file",
},
{
name: "Directory exists",
requestPath: "/configs/test_folder",
expectedCode: 200,
expectedBody: "Handled directory",
},
{
name: "File does not exist",
requestPath: "/configs/nonexistent_file.txt",
expectedCode: 404,
expectedBody: "no such file or directory",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("GET", tt.requestPath, nil)
w := httptest.NewRecorder()
HandlerConfigS(w, req)
})
}
}