Add new unit tests on agents

This commit is contained in:
armirveliaj
2024-06-14 11:18:42 -04:00
committed by Dan Christian Bogos
parent 5dfb49db71
commit 0688659c54
4 changed files with 270 additions and 0 deletions

View File

@@ -2894,3 +2894,25 @@ func TestAgReqString(t *testing.T) {
t.Errorf("String() returned unexpected value: diff (-expected +got):\n%s", diff)
}
}
func TestCacheRadiusPacket(t *testing.T) {
testPacket := &radigo.Packet{}
testAddress := "test.address"
testCfg := &config.RadiusAgentCfg{
ClientDaAddresses: map[string]config.DAClientOpts{
"allowed.address": {},
},
}
t.Run("Success", func(t *testing.T) {
err := cacheRadiusPacket(testPacket, testAddress, testCfg, nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
})
t.Run("Address does not match client", func(t *testing.T) {
err := cacheRadiusPacket(testPacket, "not.allowed", testCfg, nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
})
}

View File

@@ -1339,6 +1339,7 @@ func TestFSEventGetOptions(t *testing.T) {
}
}
<<<<<<< Updated upstream
func TestFseventGetADC(t *testing.T) {
type testCase struct {
name string
@@ -1391,6 +1392,118 @@ func TestFseventGetADC(t *testing.T) {
if got != tc.expect {
t.Errorf("Expected duration: %v, got: %v", tc.expect, got)
=======
func TestFseventMissingParameter(t *testing.T) {
testCases := []struct {
name string
fsev map[string]string
want string
}{
{
name: "missing_account",
fsev: map[string]string{
SUBJECT: "subject value",
DESTINATION: "destination value",
CATEGORY: "category value",
UUID: "uuid value",
CSTMID: "tenant value",
CALL_DEST_NR: "callDestNr value",
},
want: utils.AccountField,
},
{
name: "missing_subject",
fsev: map[string]string{
ACCOUNT: "account value",
DESTINATION: "destination value",
CATEGORY: "category value",
UUID: "uuid value",
CSTMID: "tenant value",
CALL_DEST_NR: "callDestNr value",
},
want: "",
},
{
name: "missing_destination",
fsev: map[string]string{
ACCOUNT: "account value",
SUBJECT: "subject value",
CATEGORY: "category value",
UUID: "uuid value",
CSTMID: "tenant value",
CALL_DEST_NR: "callDestNr value",
},
want: "",
},
{
name: "missing_category",
fsev: map[string]string{
ACCOUNT: "account value",
SUBJECT: "subject value",
DESTINATION: "destination value",
UUID: "uuid value",
CSTMID: "tenant value",
CALL_DEST_NR: "callDestNr value",
},
want: "",
},
{
name: "missing_uuid",
fsev: map[string]string{
ACCOUNT: "account value",
SUBJECT: "subject value",
DESTINATION: "destination value",
CATEGORY: "category value",
CSTMID: "tenant value",
CALL_DEST_NR: "callDestNr value",
},
want: utils.OriginID,
},
{
name: "missing_tenant",
fsev: map[string]string{
ACCOUNT: "account value",
SUBJECT: "subject value",
DESTINATION: "destination value",
CATEGORY: "category value",
UUID: "uuid value",
CALL_DEST_NR: "callDestNr value",
},
want: "",
},
{
name: "missing_callDestNr",
fsev: map[string]string{
ACCOUNT: "account value",
SUBJECT: "subject value",
DESTINATION: "destination value",
CATEGORY: "category value",
UUID: "uuid value",
CSTMID: "tenant value",
},
want: CALL_DEST_NR,
},
{
name: "all_present",
fsev: map[string]string{
ACCOUNT: "account value",
SUBJECT: "subject value",
DESTINATION: "destination value",
CATEGORY: "category value",
UUID: "uuid value",
CSTMID: "tenant value",
CALL_DEST_NR: "callDestNr value",
},
want: "",
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
fsev := FSEvent(tt.fsev)
got := fsev.MissingParameter("")
if got != tt.want {
t.Errorf("expected %v, got %v", tt.want, got)
>>>>>>> Stashed changes
}
})
}

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package agents
import (
"net"
"strings"
"testing"
@@ -302,3 +303,89 @@ func TestLibdnsUpdateDnsRRHeader(t *testing.T) {
})
}
}
func TestCreateDnsOption(t *testing.T) {
tests := []struct {
name string
field string
value any
wantErr bool
wantType dns.EDNS0
}{
{name: "valid_nsid", field: utils.DNSNsid, value: "1234", wantType: &dns.EDNS0_NSID{Nsid: "1234"}},
{name: "valid_family", field: utils.DNSFamily, value: 16, wantType: &dns.EDNS0_SUBNET{Family: 16}},
{name: "invalid_family_type", field: utils.DNSFamily, value: "invalid", wantErr: true},
{name: "valid_source_netmask", field: utils.DNSSourceNetmask, value: 24, wantType: &dns.EDNS0_SUBNET{SourceNetmask: 24}},
{name: "valid_address", field: utils.Address, value: "1.2.3.4", wantType: &dns.EDNS0_SUBNET{Address: net.ParseIP("1.2.3.4")}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotOption, gotErr := createDnsOption(tt.field, tt.value)
if (gotErr != nil) != tt.wantErr {
t.Errorf("createDnsOption() error = %v, wantErr = %v", gotErr, tt.wantErr)
return
}
if tt.wantErr {
return
}
if gotOption == nil {
t.Errorf("createDnsOption() returned nil option")
return
}
switch gotOption := gotOption.(type) {
case *dns.EDNS0_NSID:
case *dns.EDNS0_SUBNET:
default:
t.Errorf("Unexpected option type returned from createDnsOption: %T", gotOption)
}
})
}
}
func equalDNSQuestionsTest(q1, q2 []dns.Question) bool {
if len(q1) != len(q2) {
return false
}
for i := range q1 {
if q1[i] != q2[i] {
return false
}
}
return true
}
func TestLibdnsUpdateDnsQuestions(t *testing.T) {
testQ := dns.Question{Name: "cgrates.org", Qtype: dns.TypeA, Qclass: dns.ClassINET}
tests := []struct {
name string
q []dns.Question
path []string
value any
newBranch bool
wantErr bool
wantQ []dns.Question
}{
{
"update_name_existing",
[]dns.Question{testQ},
[]string{utils.DNSName},
"cgrates.org",
false,
false,
[]dns.Question{{Name: "cgrates.org", Qtype: dns.TypeA, Qclass: dns.ClassINET}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotQ, err := updateDnsQuestions(tt.q, tt.path, tt.value, tt.newBranch)
if (err != nil) != tt.wantErr {
t.Errorf("updateDnsQuestions() error = %v, wantErr = %v", err, tt.wantErr)
return
}
if !equalDNSQuestionsTest(gotQ, tt.wantQ) {
t.Errorf("updateDnsQuestions() gotQ = %v, wantQ = %v", gotQ, tt.wantQ)
}
})
}
}

View File

@@ -21,11 +21,14 @@ package agents
import (
"bufio"
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"net/http/httputil"
"strings"
"testing"
"github.com/cgrates/cgrates/utils"
)
func TestHttpUrlDPFieldAsInterface(t *testing.T) {
@@ -206,3 +209,48 @@ func TestStringReq(t *testing.T) {
t.Errorf("String method returned unexpected result:\nExpected: %s\nGot: %s", string(expected), result)
}
}
func TestLibhttpagentNewHAReplyEncoder(t *testing.T) {
tests := []struct {
name string
encType string
wantType string
wantErr bool
}{
{
name: "unsupported_type",
encType: "invalid",
wantType: "",
wantErr: true,
},
{
name: "xml_encoder",
encType: utils.MetaXml,
wantType: "*agents.haXMLEncoder",
wantErr: false,
},
{
name: "text_plain_encoder",
encType: utils.MetaTextPlain,
wantType: "*agents.haTextPlainEncoder",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := http.ResponseWriter(nil)
gotRE, err := newHAReplyEncoder(tt.encType, w)
if (err != nil) != tt.wantErr {
t.Errorf("newHAReplyEncoder error = %v, wantErr = %v", err, tt.wantErr)
return
}
if tt.wantErr {
return
}
gotType := fmt.Sprintf("%T", gotRE)
if gotType != tt.wantType {
t.Errorf("newHAReplyEncoder encoder type = %v, want %v", gotType, tt.wantType)
}
})
}
}