From 2fccb98f03bc254133be87eb74f9bd7c337ad8ee Mon Sep 17 00:00:00 2001 From: armirveliaj Date: Wed, 26 Jun 2024 09:36:07 -0400 Subject: [PATCH] Add new unit tests on agents --- agents/fsagent_test.go | 194 +++++++++++++++++++++++++++++++++++++++++ agents/libdns_test.go | 88 +++++++++++++++++++ 2 files changed, 282 insertions(+) diff --git a/agents/fsagent_test.go b/agents/fsagent_test.go index 3d69542ee..e24bd17c0 100644 --- a/agents/fsagent_test.go +++ b/agents/fsagent_test.go @@ -19,10 +19,13 @@ package agents import ( "testing" + "time" "github.com/cgrates/birpc/context" + "github.com/cgrates/fsock" "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/sessions" "github.com/cgrates/cgrates/utils" ) @@ -69,3 +72,194 @@ func TestFsAgentCreateHandlers(t *testing.T) { t.Error("Expected CHANNEL_PARK handler, but not found") } } + +func TestFsAgentV1WarnDisconnect(t *testing.T) { + + cfg := &config.FsAgentCfg{ + LowBalanceAnnFile: "File", + EventSocketConns: []*config.FsConnCfg{ + { + Address: "Address", + Password: "Password", + Reconnects: 3, + MaxReconnectInterval: 10 * time.Second, + ReplyTimeout: 5 * time.Second, + Alias: "Alias", + }, + }, + } + + fsa := &FSsessions{ + cfg: cfg, + conns: []*fsock.FSock{}, + } + ctx := &context.Context{} + args := map[string]interface{}{ + "OriginID": "ID", + "FsConnID": int64(0), + } + var reply string + fsa.cfg.LowBalanceAnnFile = utils.EmptyString + err := fsa.V1WarnDisconnect(ctx, args, &reply) + if err != nil { + t.Errorf("Expected no error, got %v", err) + } + if reply != utils.OK { + t.Errorf("Expected reply to be 'OK', got '%s'", reply) + } + fsa.cfg.LowBalanceAnnFile = "File" + err = fsa.V1WarnDisconnect(ctx, args, &reply) + if err == nil { + t.Errorf("Index out of range[0,0): 0, got %v", err) + } + if reply != utils.OK { + t.Errorf("Expected reply to be 'OK', got '%s'", reply) + } + delete(args, "FsConnID") + err = fsa.V1WarnDisconnect(ctx, args, &reply) + if err == nil { + t.Error("Expected an error, got nil") + } +} + +func TestFsAgentReload(t *testing.T) { + cfg := &config.FsAgentCfg{ + EventSocketConns: []*config.FsConnCfg{ + { + Address: "Address", + Password: "Password", + Reconnects: 3, + MaxReconnectInterval: 10 * time.Second, + ReplyTimeout: 5 * time.Second, + Alias: "Alias", + }, + }, + } + fsa := &FSsessions{ + cfg: cfg, + } + fsa.Reload() + + if len(fsa.conns) != 1 { + t.Errorf("Expected fsa.conns length to be 1, got %d", len(fsa.conns)) + } + if len(fsa.senderPools) != 1 { + t.Errorf("Expected fsa.senderPools length to be 1, got %d", len(fsa.senderPools)) + } + +} + +func TestFsAgentV1GetActiveSessionIDs(t *testing.T) { + cfg := &config.FsAgentCfg{ + EventSocketConns: []*config.FsConnCfg{ + { + Address: "Address", + Password: "Password", + Reconnects: 3, + MaxReconnectInterval: 10 * time.Second, + ReplyTimeout: 5 * time.Second, + Alias: "Alias", + }, + }, + ActiveSessionDelimiter: ",", + } + + fsa := &FSsessions{ + cfg: cfg, + } + + ctx := context.Background() + var sessionIDs []*sessions.SessionID + err := fsa.V1GetActiveSessionIDs(ctx, "", &sessionIDs) + if err == nil { + t.Errorf("NO_ACTIVE_SESSION, got %v", err) + } + if len(sessionIDs) != 0 { + t.Errorf("Expected sessionIDs slice to be populated, got empty") + } + +} + +func TestFsAgentV1DisconnectSession(t *testing.T) { + mockEvent := map[string]any{ + utils.OriginID: "Id", + utils.DisconnectCause: "Cause", + FsConnID: int64(0), + } + testReply := "" + fsa := &FSsessions{ + conns: []*fsock.FSock{}, + } + err := fsa.V1DisconnectSession(context.Background(), utils.CGREvent{Event: mockEvent}, &testReply) + if err == nil { + t.Errorf("Index out of range[0,0): 0, got %v", err) + } + if testReply == utils.OK { + t.Errorf("Expected reply to be 'OK', got %s", testReply) + } +} + +func TestFsAgentShutdown(t *testing.T) { + fsa := &FSsessions{ + conns: []*fsock.FSock{}, + } + err := fsa.Shutdown() + if err != nil { + t.Errorf("Expected no error, got %v", err) + } +} + +func TestFsAgentCall(t *testing.T) { + ctx := context.Background() + serviceMethod := "Method" + args := "Args" + reply := new(interface{}) + fsa := &FSsessions{} + err := fsa.Call(ctx, serviceMethod, args, reply) + if err == nil { + t.Errorf("UNSUPPORTED_SERVICE_METHOD, got %v", err) + } +} + +func TestFsAgentNewFSsessions(t *testing.T) { + fsAgentConfig := &config.FsAgentCfg{ + EventSocketConns: []*config.FsConnCfg{ + { + Address: "Address", + Password: "Password", + Reconnects: 3, + MaxReconnectInterval: time.Second * 30, + ReplyTimeout: time.Second * 10, + }, + }, + } + timezone := "UTC" + connMgr := &engine.ConnManager{} + + fsa, err := NewFSsessions(fsAgentConfig, timezone, connMgr) + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + if fsa == nil { + t.Fatal("Expected non-nil FSsessions instance, got nil") + } + if fsa.cfg != fsAgentConfig { + t.Errorf("Expected fsa.cfg to be %v, got %v", fsAgentConfig, fsa.cfg) + } + if len(fsa.conns) != len(fsAgentConfig.EventSocketConns) { + t.Errorf("Expected fsa.conns length to be %d, got %d", len(fsAgentConfig.EventSocketConns), len(fsa.conns)) + } + if len(fsa.senderPools) != len(fsAgentConfig.EventSocketConns) { + t.Errorf("Expected fsa.senderPools length to be %d, got %d", len(fsAgentConfig.EventSocketConns), len(fsa.senderPools)) + } + if fsa.timezone != timezone { + t.Errorf("Expected fsa.timezone to be %s, got %s", timezone, fsa.timezone) + } + if fsa.connMgr != connMgr { + t.Errorf("Expected fsa.connMgr to be %v, got %v", connMgr, fsa.connMgr) + } + if fsa.ctx == nil { + t.Error("Expected fsa.ctx to be initialized, got nil") + } + +} diff --git a/agents/libdns_test.go b/agents/libdns_test.go index 3382568b5..74510285b 100644 --- a/agents/libdns_test.go +++ b/agents/libdns_test.go @@ -481,3 +481,91 @@ func TestLibDnsUpdateDnsOption(t *testing.T) { } } + +func TestLibDnsUpdateDnsSRVAnswer(t *testing.T) { + tests := []struct { + name string + path []string + value interface{} + expect func(v *dns.SRV) bool + wantErr bool + }{ + { + name: "update Priority", + path: []string{utils.DNSPriority}, + value: int64(10), + expect: func(v *dns.SRV) bool { + return v.Priority == 10 + }, + wantErr: false, + }, + { + name: "update Weight", + path: []string{utils.Weight}, + value: int64(20), + expect: func(v *dns.SRV) bool { + return v.Weight == 20 + }, + wantErr: false, + }, + { + name: "update Port", + path: []string{utils.DNSPort}, + value: int64(2012), + expect: func(v *dns.SRV) bool { + return v.Port == 2012 + }, + wantErr: false, + }, + { + name: "update Target", + path: []string{utils.DNSTarget}, + value: "cgrates.com", + expect: func(v *dns.SRV) bool { + return v.Target == "cgrates.com" + }, + wantErr: false, + }, + { + name: "invalid path length", + path: []string{}, + value: int64(10), + expect: nil, + wantErr: true, + }, + { + name: "invalid path value", + path: []string{"invalid"}, + value: int64(10), + expect: nil, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + srv := &dns.SRV{} + err := updateDnsSRVAnswer(srv, tt.path, tt.value) + if (err != nil) != tt.wantErr { + t.Errorf("updateDnsSRVAnswer() error = %v, wantErr %v", err, tt.wantErr) + return + } + if tt.expect != nil && !tt.expect(srv) { + t.Errorf("updateDnsSRVAnswer() unexpected result for %v", srv) + } + }) + } +} + +func TestLibDnsUpdateDnsSRVAnswerDNSHdr(t *testing.T) { + srv := &dns.SRV{} + + err := updateDnsSRVAnswer(srv, []string{utils.DNSHdr, utils.DNSName}, "cgrates.com.") + if err != nil { + t.Errorf("unexpected error: %v", err) + } + + if srv.Hdr.Name != "cgrates.com." { + t.Errorf("expected Name to be 'cgrates.com.', got %s", srv.Hdr.Name) + } +}