180 lines
4.0 KiB
Go
180 lines
4.0 KiB
Go
// Copyright 2025, IPng Networks GmbH, Pim van Pelt <pim@ipng.ch>
|
|
|
|
package ifmib
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"go.fd.io/govpp/api"
|
|
)
|
|
|
|
func TestNewInterfaceMIB(t *testing.T) {
|
|
mib := NewInterfaceMIB()
|
|
|
|
if mib == nil {
|
|
t.Fatal("NewInterfaceMIB returned nil")
|
|
}
|
|
|
|
if mib.handler == nil {
|
|
t.Error("Expected handler to be initialized")
|
|
}
|
|
|
|
if mib.stats == nil {
|
|
t.Error("Expected stats map to be initialized")
|
|
}
|
|
|
|
if mib.descriptions == nil {
|
|
t.Error("Expected descriptions map to be initialized")
|
|
}
|
|
|
|
if len(mib.stats) != 0 {
|
|
t.Errorf("Expected stats map to be empty, got %d entries", len(mib.stats))
|
|
}
|
|
|
|
if len(mib.descriptions) != 0 {
|
|
t.Errorf("Expected descriptions map to be empty, got %d entries", len(mib.descriptions))
|
|
}
|
|
}
|
|
|
|
func TestGetHandler(t *testing.T) {
|
|
mib := NewInterfaceMIB()
|
|
handler := mib.GetHandler()
|
|
|
|
if handler == nil {
|
|
t.Error("GetHandler returned nil")
|
|
}
|
|
|
|
if handler != mib.handler {
|
|
t.Error("GetHandler returned different handler than expected")
|
|
}
|
|
}
|
|
|
|
func TestLoadVPPConfigValidYAML(t *testing.T) {
|
|
mib := NewInterfaceMIB()
|
|
|
|
// Create a temporary YAML file
|
|
yamlContent := `interfaces:
|
|
GigabitEthernet0/0/0:
|
|
description: 'Test: Interface'
|
|
sub-interfaces:
|
|
100:
|
|
description: 'Test: Sub-interface'
|
|
loopbacks:
|
|
loop0:
|
|
description: 'Test: Loopback'
|
|
`
|
|
|
|
tmpfile, err := os.CreateTemp("", "test_*.yaml")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
if _, err := tmpfile.Write([]byte(yamlContent)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := tmpfile.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Test loading the config
|
|
err = mib.LoadVPPConfig(tmpfile.Name())
|
|
if err != nil {
|
|
t.Fatalf("LoadVPPConfig failed: %v", err)
|
|
}
|
|
|
|
// Check that descriptions were loaded
|
|
if len(mib.descriptions) != 3 {
|
|
t.Errorf("Expected 3 descriptions, got %d", len(mib.descriptions))
|
|
}
|
|
|
|
if mib.descriptions["GigabitEthernet0/0/0"] != "Test: Interface" {
|
|
t.Errorf("Unexpected interface description: %s", mib.descriptions["GigabitEthernet0/0/0"])
|
|
}
|
|
|
|
if mib.descriptions["GigabitEthernet0/0/0.100"] != "Test: Sub-interface" {
|
|
t.Errorf("Unexpected sub-interface description: %s", mib.descriptions["GigabitEthernet0/0/0.100"])
|
|
}
|
|
|
|
if mib.descriptions["loop0"] != "Test: Loopback" {
|
|
t.Errorf("Unexpected loopback description: %s", mib.descriptions["loop0"])
|
|
}
|
|
}
|
|
|
|
func TestLoadVPPConfigNonExistentFile(t *testing.T) {
|
|
mib := NewInterfaceMIB()
|
|
|
|
err := mib.LoadVPPConfig("/nonexistent/file.yaml")
|
|
if err == nil {
|
|
t.Error("Expected error for non-existent file")
|
|
}
|
|
}
|
|
|
|
func TestLoadVPPConfigInvalidYAML(t *testing.T) {
|
|
mib := NewInterfaceMIB()
|
|
|
|
// Create a temporary file with invalid YAML
|
|
invalidYAML := `interfaces:
|
|
test: [
|
|
`
|
|
|
|
tmpfile, err := os.CreateTemp("", "invalid_*.yaml")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
if _, err := tmpfile.Write([]byte(invalidYAML)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := tmpfile.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = mib.LoadVPPConfig(tmpfile.Name())
|
|
if err == nil {
|
|
t.Error("Expected error for invalid YAML")
|
|
}
|
|
}
|
|
|
|
func TestUpdateStatsBasic(t *testing.T) {
|
|
mib := NewInterfaceMIB()
|
|
|
|
// Create mock interface stats
|
|
stats := &api.InterfaceStats{
|
|
Interfaces: []api.InterfaceCounters{
|
|
{
|
|
InterfaceIndex: 0,
|
|
InterfaceName: "test0",
|
|
Rx: api.InterfaceCounterCombined{
|
|
Packets: 100,
|
|
Bytes: 1000,
|
|
},
|
|
Tx: api.InterfaceCounterCombined{
|
|
Packets: 200,
|
|
Bytes: 2000,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
// Call UpdateStats (this will test the basic flow without AgentX sessions)
|
|
mib.UpdateStats(stats)
|
|
|
|
// Check that stats were stored
|
|
if len(mib.stats) != 1 {
|
|
t.Errorf("Expected 1 interface in stats, got %d", len(mib.stats))
|
|
}
|
|
|
|
if storedStats, exists := mib.stats[0]; !exists {
|
|
t.Error("Expected interface 0 to be stored in stats")
|
|
} else {
|
|
if storedStats.InterfaceName != "test0" {
|
|
t.Errorf("Expected interface name 'test0', got '%s'", storedStats.InterfaceName)
|
|
}
|
|
if storedStats.Rx.Packets != 100 {
|
|
t.Errorf("Expected RX packets 100, got %d", storedStats.Rx.Packets)
|
|
}
|
|
}
|
|
} |