Add new unit tests on agents

This commit is contained in:
armirveliaj
2024-06-19 10:58:49 -04:00
committed by Dan Christian Bogos
parent 777d4f0a28
commit d02df49f53
2 changed files with 203 additions and 0 deletions

View File

@@ -20,6 +20,7 @@ package agents
import (
"bytes"
"reflect"
"strings"
"testing"
"time"
@@ -1510,3 +1511,42 @@ func TestFseventMissingParameter(t *testing.T) {
})
}
}
func TestFseventGetPdd(t *testing.T) {
fsev := FSEvent{
PDD_MEDIA_MS: "500",
PDD_NOMEDIA_MS: "700",
}
duration, err := fsev.GetPdd(utils.MetaDefault)
if err != nil {
t.Errorf("Test case 1: expected no error, got %v", err)
}
expectedDuration := 500 * time.Millisecond
if duration != expectedDuration {
t.Errorf("Test case 1: expected duration %v, got %v", expectedDuration, duration)
}
duration, err = fsev.GetPdd(utils.StaticValuePrefix + "1000ms")
if err != nil {
t.Errorf("Test case 2: expected no error, got %v", err)
}
expectedDuration = 1000 * time.Millisecond
if duration != expectedDuration {
t.Errorf("Test case 2: expected duration %v, got %v", expectedDuration, duration)
}
fsev["invalidFieldName"] = "invalid"
_, err = fsev.GetPdd("invalidFieldName")
if err == nil {
t.Errorf("Test case 4: expected error, got nil")
} else if !strings.Contains(err.Error(), "invalid duration") {
t.Errorf("Test case 4: expected error message to contain 'invalid duration', got %v", err)
}
duration, err = fsev.GetPdd("")
if err != nil {
t.Errorf("Test case 5: expected no error, got %v", err)
}
expectedDuration = 0
if duration != expectedDuration {
t.Errorf("Test case 5: expected duration %v, got %v", expectedDuration, duration)
}
}

View File

@@ -20,6 +20,8 @@ package agents
import (
"net"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
@@ -1245,3 +1247,164 @@ func TestLibdiamDiamErr(t *testing.T) {
}
}
func TestLibDiamAVPAsString(t *testing.T) {
testCases := []struct {
name string
dAVP *diam.AVP
expected string
expectErr bool
}{
{
name: "IPv4 Address AVP",
dAVP: &diam.AVP{Data: datatype.Address(net.IPv4(192, 168, 0, 1))},
expected: "192.168.0.1",
},
{
name: "Diameter Identity AVP",
dAVP: &diam.AVP{Data: datatype.DiameterIdentity("cgrates.com")},
expected: "cgrates.com",
},
{
name: "Time AVP",
dAVP: &diam.AVP{Data: datatype.Time(time.Now())},
expected: time.Now().Format(time.RFC3339),
},
{
name: "Nil AVP",
dAVP: nil,
expectErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, err := diamAVPAsString(tc.dAVP)
if tc.expectErr {
if err == nil {
t.Error("Expected an error, but got nil")
}
return
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
if result != tc.expected {
t.Errorf("Expected %q, got %q", tc.expected, result)
}
})
}
}
func TestLibDiamUpdateAVPLength(t *testing.T) {
testCases := []struct {
name string
avps []*diam.AVP
expected int
}{
{
name: "Single AVP without GroupedAVP",
avps: []*diam.AVP{
{Length: 10},
{Length: 20},
{Length: 15},
},
expected: 45,
},
{
name: "Empty AVP slice",
avps: []*diam.AVP{},
expected: 0,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := updateAVPLength(tc.avps)
if result != tc.expected {
t.Errorf("Expected length %d, got %d", tc.expected, result)
}
})
}
}
func TestLibDiamUpdateAVPLengthWithGroupedAVP(t *testing.T) {
groupedAVP := &diam.AVP{Length: 10}
avps := []*diam.AVP{
{Length: 10},
{
Data: &diam.GroupedAVP{
AVP: []*diam.AVP{
groupedAVP,
{Length: 0},
},
},
},
{Length: 15},
}
expected := 10 + headerLen(avps[1]) + groupedAVP.Length + avps[2].Length
result := updateAVPLength(avps)
if result != expected {
t.Errorf("Expected length %d, got %d", expected, result)
}
}
func TestLibDiamDiameterDPString(t *testing.T) {
msg := &diam.Message{
Header: &diam.Header{
Version: 1,
MessageLength: 20,
CommandFlags: 2,
CommandCode: 272,
ApplicationID: 0,
HopByHopID: 12345,
EndToEndID: 67890,
},
}
dp := &diameterDP{
m: msg,
}
result := dp.String()
expected := msg.String()
if result != expected {
t.Errorf("Expected %q, but got %q", expected, result)
}
}
func TestLibDiamLoadDictionaries(t *testing.T) {
tempDir := t.TempDir()
dictsDir := filepath.Join(tempDir, "dicts")
err := os.Mkdir(dictsDir, os.ModePerm)
if err != nil {
t.Fatalf("Failed to create temporary directory: %v", err)
}
testCases := []struct {
name string
dictsDir string
expectedErrorMsg string
}{
{
name: "Valid Directory",
dictsDir: dictsDir,
},
{
name: "Invalid Directory",
dictsDir: filepath.Join(tempDir, "nonexistent", "directory"),
expectedErrorMsg: "Invalid dictionaries folder",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := loadDictionaries(tc.dictsDir, "testComponent")
if tc.expectedErrorMsg == "" && err != nil {
t.Errorf("Unexpected error: %v", err)
}
if tc.expectedErrorMsg != "" && !strings.Contains(err.Error(), tc.expectedErrorMsg) {
t.Errorf("Expected error containing %q, got: %v", tc.expectedErrorMsg, err)
}
})
}
}