From 1854afd74de22c29d387f6d7ef97d931b4641670 Mon Sep 17 00:00:00 2001 From: armirveliaj Date: Tue, 10 Sep 2024 10:31:05 -0400 Subject: [PATCH] Add coverage tests on agents and config --- agents/fsevent_test.go | 44 ++++++++++++++++++++ agents/kamevent_test.go | 65 +++++++++++++++++++++++++++++ agents/libdns_test.go | 21 ++++++++++ config/configs_test.go | 92 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 222 insertions(+) diff --git a/agents/fsevent_test.go b/agents/fsevent_test.go index 6a7790f2b..13b1e06d7 100644 --- a/agents/fsevent_test.go +++ b/agents/fsevent_test.go @@ -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) + } + }) + } +} diff --git a/agents/kamevent_test.go b/agents/kamevent_test.go index 3a3bd86c0..c8b779b8e 100644 --- a/agents/kamevent_test.go +++ b/agents/kamevent_test.go @@ -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) + } + }) + } +} diff --git a/agents/libdns_test.go b/agents/libdns_test.go index 39338594e..cdbd9220c 100644 --- a/agents/libdns_test.go +++ b/agents/libdns_test.go @@ -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") + } + +} diff --git a/config/configs_test.go b/config/configs_test.go index 73fb18a03..64197ed1f 100644 --- a/config/configs_test.go +++ b/config/configs_test.go @@ -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) + }) + } +}