Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Lx9FNLG2CqeBbVGGd9bFsP
235 lines
6.9 KiB
Go
235 lines
6.9 KiB
Go
package simagent
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/gosnmp/gosnmp"
|
|
)
|
|
|
|
const sample = `# comment
|
|
1.3.6.1.2.1.1.1.0|4|Linux box
|
|
1.3.6.1.2.1.1.2.0|6|1.3.6.1.4.1.8072.3.2.10
|
|
1.3.6.1.2.1.1.3.0|67|123456
|
|
1.3.6.1.2.1.1.4.0|4x|00ff7c
|
|
1.3.6.1.2.1.1.5.0|5|
|
|
1.3.6.1.2.1.4.20.1.1.10.0.0.1|64|10.0.0.1
|
|
1.3.6.1.2.1.2.2.1.5.1|66|1000000000
|
|
1.3.6.1.2.1.2.2.1.10.1|65|42
|
|
1.3.6.1.2.1.2.1.0|2|-3
|
|
1.3.6.1.2.1.31.1.1.1.6.1|70|18446744073709551615
|
|
1.3.6.1.2.1.1.9.0|68x|0102
|
|
1.3.6.1.2.1.1.9.0|68x|0304
|
|
.1.3.6.1.2.1.31.1.1.1.10.1|70:numeric|rate=125000,initial=7,wrap=999
|
|
`
|
|
|
|
func TestParseRecording(t *testing.T) {
|
|
rec, err := ParseRecording(strings.NewReader(sample))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if rec.Len() != 12 { // duplicate 1.9.0 collapsed
|
|
t.Fatalf("len %d", rec.Len())
|
|
}
|
|
get := func(oid string) Record {
|
|
i := rec.search(oid)
|
|
if i >= rec.Len() || rec.recs[i].OID != oid {
|
|
t.Fatalf("missing %s", oid)
|
|
}
|
|
return rec.recs[i]
|
|
}
|
|
for _, tc := range []struct {
|
|
oid string
|
|
typ gosnmp.Asn1BER
|
|
val any
|
|
}{
|
|
{"1.3.6.1.2.1.1.1.0", gosnmp.OctetString, []byte("Linux box")},
|
|
{"1.3.6.1.2.1.1.2.0", gosnmp.ObjectIdentifier, "1.3.6.1.4.1.8072.3.2.10"},
|
|
{"1.3.6.1.2.1.1.3.0", gosnmp.TimeTicks, uint32(123456)},
|
|
{"1.3.6.1.2.1.1.4.0", gosnmp.OctetString, []byte{0, 0xff, 0x7c}},
|
|
{"1.3.6.1.2.1.1.5.0", gosnmp.Null, nil},
|
|
{"1.3.6.1.2.1.4.20.1.1.10.0.0.1", gosnmp.IPAddress, "10.0.0.1"},
|
|
{"1.3.6.1.2.1.2.2.1.5.1", gosnmp.Gauge32, uint32(1_000_000_000)},
|
|
{"1.3.6.1.2.1.2.2.1.10.1", gosnmp.Counter32, uint32(42)},
|
|
{"1.3.6.1.2.1.2.1.0", gosnmp.Integer, -3},
|
|
{"1.3.6.1.2.1.31.1.1.1.6.1", gosnmp.Counter64, uint64(18446744073709551615)},
|
|
{"1.3.6.1.2.1.1.9.0", gosnmp.Opaque, []byte{3, 4}},
|
|
} {
|
|
r := get(tc.oid)
|
|
if r.Type != tc.typ || string(anyBytes(r.Value)) != string(anyBytes(tc.val)) {
|
|
t.Errorf("%s: %v %#v, want %v %#v", tc.oid, r.Type, r.Value, tc.typ, tc.val)
|
|
}
|
|
}
|
|
// sorted numerically, not lexically
|
|
for i := 1; i < rec.Len(); i++ {
|
|
if cmpOID(rec.recs[i-1].OID, rec.recs[i].OID) >= 0 {
|
|
t.Fatalf("unsorted at %d: %s %s", i, rec.recs[i-1].OID, rec.recs[i].OID)
|
|
}
|
|
}
|
|
if d := get("1.3.6.1.2.1.31.1.1.1.10.1").Dyn; d == nil || d.Rate != 125000 || d.Initial != 7 || d.Wrap != 999 {
|
|
t.Fatalf("dyn %+v", d)
|
|
}
|
|
}
|
|
|
|
func anyBytes(v any) []byte { return []byte(fmt.Sprintf("%#v", v)) }
|
|
|
|
func TestParseErrors(t *testing.T) {
|
|
for _, in := range []string{
|
|
"1.3.6.1|4",
|
|
"1.3.6.1|99|x",
|
|
"1.3.6..1|4|x",
|
|
"1.3.6.1|2|abc",
|
|
"1.3.6.1|65|4294967296",
|
|
"1.3.6.1|4x|zz",
|
|
"1.3.6.1|4:numeric|rate=1",
|
|
"1.3.6.1|65:delay|rate=1",
|
|
"1.3.6.1|65:numeric|rate=1,bogus=2",
|
|
"1.3.6.1|64|::1",
|
|
"1.3.6.1|6|1.3..2",
|
|
} {
|
|
if _, err := ParseRecording(strings.NewReader(in)); err == nil {
|
|
t.Errorf("%q: want error", in)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCmpOID(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
a, b string
|
|
want int
|
|
}{
|
|
{"1.3.6.1", "1.3.6.1", 0},
|
|
{"1.3.6.1", ".1.3.6.1", 0},
|
|
{"1.3.6.1", "1.3.6.1.2", -1},
|
|
{"1.3.6.2", "1.3.6.1.9", 1},
|
|
{"1.3.6.1.2.1.2.2", "1.3.6.1.2.1.25", -1},
|
|
{"1.3.6.1.10", "1.3.6.1.9", 1},
|
|
} {
|
|
if got := cmpOID(tc.a, tc.b); got != tc.want {
|
|
t.Errorf("cmpOID(%s,%s) = %d", tc.a, tc.b, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDynamic(t *testing.T) {
|
|
sec := time.Second
|
|
d := &Dynamic{Rate: 100, Initial: 5}
|
|
for _, tc := range []struct {
|
|
d *Dynamic
|
|
typ gosnmp.Asn1BER
|
|
at time.Duration
|
|
want uint64
|
|
}{
|
|
{d, gosnmp.TimeTicks, 0, 5},
|
|
{d, gosnmp.TimeTicks, 10 * sec, 1005},
|
|
{d, gosnmp.TimeTicks, 1500 * time.Millisecond, 155},
|
|
{&Dynamic{Rate: 100, Initial: 1<<32 - 1000}, gosnmp.TimeTicks, 20 * sec, 1000}, // natural 32-bit wrap
|
|
{&Dynamic{Rate: 10, Wrap: 99}, gosnmp.Counter32, 25 * sec, 50}, // wrap=<max>
|
|
{&Dynamic{Rate: 100, Initial: 1000, ResetAt: 60 * sec}, gosnmp.TimeTicks, 59 * sec, 6900},
|
|
{&Dynamic{Rate: 100, Initial: 1000, ResetAt: 60 * sec}, gosnmp.TimeTicks, 70 * sec, 2000},
|
|
{&Dynamic{Rate: 12_500_000, Initial: 1}, gosnmp.Counter64, 24 * 3600 * sec, 1 + 12_500_000*86400},
|
|
{&Dynamic{Rate: 0.5}, gosnmp.Gauge32, 10 * sec, 5},
|
|
} {
|
|
if got := tc.d.value(tc.at, tc.typ); got != tc.want {
|
|
t.Errorf("%+v at %v = %d, want %d", tc.d, tc.at, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGetNextAndBulkSemantics(t *testing.T) {
|
|
rec, err := ParseRecording(strings.NewReader(sample))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
a, err := Start(context.Background(), Config{Recording: rec})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer a.Close()
|
|
g := &gosnmp.GoSNMP{
|
|
Target: a.Addr().Addr().String(), Port: a.Addr().Port(), Version: gosnmp.Version2c, Community: "public",
|
|
Timeout: time.Second, Retries: 0,
|
|
}
|
|
if err := g.Connect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer g.Close()
|
|
|
|
p, err := g.GetNext([]string{"1.3.6.1.2.1.31.1.1.1.10.1"})
|
|
if err != nil || len(p.Variables) != 1 || p.Variables[0].Type != gosnmp.EndOfMibView {
|
|
t.Fatalf("past the end: %v %+v", err, p)
|
|
}
|
|
// 2 repeaters from the start, one non-repeater: non-repeater first, then rows interleaved.
|
|
p, err = g.GetBulk([]string{"1.3.6.1.2.1.1.1.0", "1.3.6.1.2.1.1.3.0", "1.3.6.1.2.1.2.2.1.5"}, 1, 3)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var names []string
|
|
for _, v := range p.Variables {
|
|
names = append(names, v.Name)
|
|
}
|
|
want := []string{
|
|
".1.3.6.1.2.1.1.2.0",
|
|
".1.3.6.1.2.1.1.4.0", ".1.3.6.1.2.1.2.2.1.5.1",
|
|
".1.3.6.1.2.1.1.5.0", ".1.3.6.1.2.1.2.2.1.10.1",
|
|
".1.3.6.1.2.1.1.9.0", ".1.3.6.1.2.1.4.20.1.1.10.0.0.1",
|
|
}
|
|
if strings.Join(names, ",") != strings.Join(want, ",") {
|
|
t.Fatalf("bulk order:\n got %v\nwant %v", names, want)
|
|
}
|
|
// Bulk off the end stops once every repeater is exhausted.
|
|
p, err = g.GetBulk([]string{"1.3.6.1.2.1.31.1.1.1.6.1"}, 0, 50)
|
|
if err != nil || len(p.Variables) != 2 || p.Variables[1].Type != gosnmp.EndOfMibView {
|
|
t.Fatalf("bulk tail: %v %+v", err, p.Variables)
|
|
}
|
|
// Set is refused.
|
|
if p, err = g.Set([]gosnmp.SnmpPDU{{Name: "1.3.6.1.2.1.1.5.0", Type: gosnmp.OctetString, Value: "x"}}); err != nil || p.Error == gosnmp.NoError {
|
|
t.Fatalf("set: %v %+v", err, p)
|
|
}
|
|
}
|
|
|
|
func TestPool(t *testing.T) {
|
|
rec, _ := ParseRecording(strings.NewReader(sample))
|
|
cfgs := make([]Config, 100)
|
|
for i := range cfgs {
|
|
cfgs[i] = Config{Recording: rec}
|
|
}
|
|
var p Pool
|
|
if err := p.Start(context.Background(), cfgs); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
seen := map[uint16]bool{}
|
|
for _, a := range p.Agents() {
|
|
seen[a.Addr().Port()] = true
|
|
}
|
|
if len(seen) != 100 {
|
|
t.Fatalf("%d distinct ports", len(seen))
|
|
}
|
|
if err := p.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := p.Start(context.Background(), []Config{{Recording: rec}, {}}); err == nil || len(p.Agents()) != 0 {
|
|
t.Fatal("a failing config must leave the pool empty")
|
|
}
|
|
}
|
|
|
|
func TestContextCancelStopsAgent(t *testing.T) {
|
|
rec, _ := ParseRecording(strings.NewReader(sample))
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
a, err := Start(ctx, Config{Recording: rec})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cancel()
|
|
done := make(chan struct{})
|
|
go func() { a.Close(); close(done) }()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("Close hung after cancel")
|
|
}
|
|
}
|