123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package snmpreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/snmpreceiver"
- import (
- "errors"
- "fmt"
- "math"
- "strconv"
- "strings"
- "testing"
- "github.com/gosnmp/gosnmp"
- "github.com/stretchr/testify/mock"
- "github.com/stretchr/testify/require"
- "go.opentelemetry.io/collector/component"
- "go.opentelemetry.io/collector/component/componenttest"
- "go.opentelemetry.io/collector/receiver/scrapererror"
- "go.uber.org/zap"
- "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/snmpreceiver/internal/mocks"
- )
- func TestNewClient(t *testing.T) {
- testCase := []struct {
- desc string
- cfg *Config
- host component.Host
- settings component.TelemetrySettings
- logger *zap.Logger
- expectError error
- }{
- {
- desc: "Valid v2c configuration",
- cfg: &Config{
- Version: "v2c",
- Endpoint: "udp://localhost:161",
- Community: "public",
- },
- host: componenttest.NewNopHost(),
- settings: componenttest.NewNopTelemetrySettings(),
- logger: zap.NewNop(),
- expectError: nil,
- },
- {
- desc: "Valid v3 configuration",
- cfg: &Config{
- Version: "v3",
- Endpoint: "tcp://localhost:161",
- User: "user",
- SecurityLevel: "auth_priv",
- AuthType: "MD5",
- AuthPassword: "authpass",
- PrivacyType: "DES",
- PrivacyPassword: "privacypass",
- },
- host: componenttest.NewNopHost(),
- settings: componenttest.NewNopTelemetrySettings(),
- logger: zap.NewNop(),
- expectError: nil,
- },
- }
- for _, tc := range testCase {
- t.Run(tc.desc, func(t *testing.T) {
- ac, err := newClient(tc.cfg, tc.logger)
- if tc.expectError != nil {
- require.Nil(t, ac)
- require.Contains(t, err.Error(), tc.expectError.Error())
- } else {
- require.NoError(t, err)
- actualClient, ok := ac.(*snmpClient)
- require.True(t, ok)
- compareConfigToClient(t, actualClient, tc.cfg)
- }
- })
- }
- }
- func compareConfigToClient(t *testing.T, client *snmpClient, cfg *Config) {
- t.Helper()
- require.True(t, strings.Contains(cfg.Endpoint, client.client.GetTarget()))
- require.True(t, strings.Contains(cfg.Endpoint, strconv.FormatInt(int64(client.client.GetPort()), 10)))
- require.True(t, strings.Contains(cfg.Endpoint, client.client.GetTransport()))
- switch cfg.Version {
- case "v1":
- require.Equal(t, gosnmp.Version1, client.client.GetVersion())
- require.Equal(t, cfg.Community, client.client.GetCommunity())
- case "v2c":
- require.Equal(t, gosnmp.Version2c, client.client.GetVersion())
- require.Equal(t, cfg.Community, client.client.GetCommunity())
- case "v3":
- require.Equal(t, gosnmp.Version3, client.client.GetVersion())
- securityParams := client.client.GetSecurityParameters().(*gosnmp.UsmSecurityParameters)
- require.Equal(t, cfg.User, securityParams.UserName)
- switch cfg.SecurityLevel {
- case "no_auth_no_priv":
- require.Equal(t, gosnmp.NoAuthNoPriv, client.client.GetMsgFlags())
- case "auth_no_priv":
- require.Equal(t, gosnmp.AuthNoPriv, client.client.GetMsgFlags())
- require.Equal(t, cfg.AuthType, securityParams.AuthenticationProtocol)
- require.Equal(t, string(cfg.AuthPassword), securityParams.AuthenticationPassphrase)
- case "auth_priv":
- require.Equal(t, gosnmp.AuthPriv, client.client.GetMsgFlags())
- require.Equal(t, cfg.AuthType, securityParams.AuthenticationProtocol.String())
- require.Equal(t, string(cfg.AuthPassword), securityParams.AuthenticationPassphrase)
- require.Equal(t, cfg.PrivacyType, securityParams.PrivacyProtocol.String())
- require.Equal(t, string(cfg.PrivacyPassword), securityParams.PrivacyPassphrase)
- }
- }
- }
- func TestConnect(t *testing.T) {
- testCases := []struct {
- desc string
- testFunc func(*testing.T)
- }{
- {
- desc: "Good Connect",
- testFunc: func(t *testing.T) {
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("Connect", mock.Anything).Return(nil)
- client := &snmpClient{
- logger: &zap.Logger{},
- client: mockGoSNMP,
- }
- err := client.Connect()
- require.NoError(t, err)
- },
- },
- {
- desc: "Bad Connect",
- testFunc: func(t *testing.T) {
- connectErr := errors.New("problem connecting")
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("Connect", mock.Anything).Return(connectErr)
- client := &snmpClient{
- logger: &zap.Logger{},
- client: mockGoSNMP,
- }
- err := client.Connect()
- require.ErrorIs(t, err, connectErr)
- },
- },
- }
- for _, tc := range testCases {
- t.Run(tc.desc, tc.testFunc)
- }
- }
- func TestClose(t *testing.T) {
- testCases := []struct {
- desc string
- testFunc func(*testing.T)
- }{
- {
- desc: "Good Close",
- testFunc: func(t *testing.T) {
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("Close", mock.Anything).Return(nil)
- client := &snmpClient{
- logger: &zap.Logger{},
- client: mockGoSNMP,
- }
- err := client.Close()
- require.NoError(t, err)
- },
- },
- {
- desc: "Bad Close",
- testFunc: func(t *testing.T) {
- closeErr := errors.New("problem closing")
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("Close", mock.Anything).Return(closeErr)
- client := &snmpClient{
- logger: &zap.Logger{},
- client: mockGoSNMP,
- }
- err := client.Close()
- require.ErrorIs(t, err, closeErr)
- },
- },
- }
- for _, tc := range testCases {
- t.Run(tc.desc, tc.testFunc)
- }
- }
- func TestGetScalarData(t *testing.T) {
- testCases := []struct {
- desc string
- testFunc func(*testing.T)
- }{
- {
- desc: "No OIDs does nothing",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{}
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- returnedSNMPData := client.GetScalarData([]string{}, &scraperErrors)
- require.NoError(t, scraperErrors.Combine())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client failures adds error",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{}
- getError := errors.New("Bad GET")
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("Get", []string{"1"}).Return(nil, getError)
- mockGoSNMP.On("GetMaxOids", mock.Anything).Return(2)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- oidSlice := []string{"1"}
- returnedSNMPData := client.GetScalarData(oidSlice, &scraperErrors)
- expectedErr := fmt.Errorf("problem with getting scalar data: problem with SNMP GET for OIDs '%v': %w", oidSlice, getError)
- require.EqualError(t, scraperErrors.Combine(), expectedErr.Error())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client timeout failures tries to reset connection",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{}
- getError := errors.New("request timeout (after 0 retries)")
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("Get", []string{"1"}).Return(nil, getError)
- mockGoSNMP.On("GetMaxOids", mock.Anything).Return(2)
- mockGoSNMP.On("Close", mock.Anything).Return(nil)
- mockGoSNMP.On("Connect", mock.Anything).Return(nil)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- oidSlice := []string{"1"}
- returnedSNMPData := client.GetScalarData(oidSlice, &scraperErrors)
- expectedErr := fmt.Errorf("problem with getting scalar data: problem with SNMP GET for OIDs '%v': %w", oidSlice, getError)
- require.EqualError(t, scraperErrors.Combine(), expectedErr.Error())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client reset connection fails on connect adds error",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{}
- getError := errors.New("request timeout (after 0 retries)")
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("Get", []string{"1"}).Return(nil, getError)
- mockGoSNMP.On("GetMaxOids", mock.Anything).Return(2)
- mockGoSNMP.On("Close", mock.Anything).Return(nil)
- connectErr := errors.New("can't connect")
- mockGoSNMP.On("Connect", mock.Anything).Return(connectErr)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- oidSlice := []string{"1"}
- returnedSNMPData := client.GetScalarData(oidSlice, &scraperErrors)
- expectedErr1 := fmt.Errorf("problem with getting scalar data: problem with SNMP GET for OIDs '%v': %w", oidSlice, getError)
- expectedErr2 := fmt.Errorf("problem with getting scalar data: problem connecting while trying to reset connection: %w", connectErr)
- expectedErr := fmt.Errorf(expectedErr1.Error() + "; " + expectedErr2.Error())
- require.EqualError(t, scraperErrors.Combine(), expectedErr.Error())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client partial failures still return successes",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{
- {
- oid: "2",
- value: int64(1),
- valueType: integerVal,
- },
- }
- pdu1 := gosnmp.SnmpPDU{
- Value: 1,
- Name: "2",
- Type: gosnmp.Integer,
- }
- getError := errors.New("Bad GET")
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("Get", []string{"1"}).
- Return(nil, getError).Once()
- mockGoSNMP.On("Get", []string{"2"}).
- Return(&gosnmp.SnmpPacket{Variables: []gosnmp.SnmpPDU{pdu1}}, nil).Once()
- mockGoSNMP.On("GetMaxOids", mock.Anything).Return(1)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- oidSlice := []string{"1", "2"}
- badOIDSlice := []string{"1"}
- returnedSNMPData := client.GetScalarData(oidSlice, &scraperErrors)
- expectedErr := fmt.Errorf("problem with getting scalar data: problem with SNMP GET for OIDs '%v': %w", badOIDSlice, getError)
- require.EqualError(t, scraperErrors.Combine(), expectedErr.Error())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client returned nil value does not return data",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{}
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- pdu := gosnmp.SnmpPDU{
- Value: nil,
- Name: "1",
- Type: gosnmp.Integer,
- }
- mockGoSNMP.On("Get", []string{"1"}).
- Return(&gosnmp.SnmpPacket{Variables: []gosnmp.SnmpPDU{pdu}}, nil)
- mockGoSNMP.On("GetMaxOids", mock.Anything).Return(2)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- oidSlice := []string{"1"}
- badOID := "1"
- returnedSNMPData := client.GetScalarData(oidSlice, &scraperErrors)
- expectedErr := fmt.Errorf("problem with getting scalar data: data for OID '%s' not found", badOID)
- require.EqualError(t, scraperErrors.Combine(), expectedErr.Error())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client returned unsupported type value does not return data",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{}
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- pdu := gosnmp.SnmpPDU{
- Value: true,
- Name: "1",
- Type: gosnmp.Boolean,
- }
- mockGoSNMP.On("Get", []string{"1"}).
- Return(&gosnmp.SnmpPacket{Variables: []gosnmp.SnmpPDU{pdu}}, nil)
- mockGoSNMP.On("GetMaxOids", mock.Anything).Return(2)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- oidSlice := []string{"1"}
- badOID := "1"
- returnedSNMPData := client.GetScalarData(oidSlice, &scraperErrors)
- expectedErr := fmt.Errorf("problem with getting scalar data: data for OID '%s' not a supported type", badOID)
- require.EqualError(t, scraperErrors.Combine(), expectedErr.Error())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "Large amount of OIDs handled in chunks",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{
- {
- oid: "1",
- value: int64(1),
- valueType: integerVal,
- },
- {
- oid: "2",
- value: int64(1),
- valueType: integerVal,
- },
- {
- oid: "3",
- value: int64(1),
- valueType: integerVal,
- },
- {
- oid: "4",
- value: int64(1),
- valueType: integerVal,
- },
- }
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- pdu1 := gosnmp.SnmpPDU{
- Value: 1,
- Name: "1",
- Type: gosnmp.Integer,
- }
- pdu2 := gosnmp.SnmpPDU{
- Value: 1,
- Name: "2",
- Type: gosnmp.Integer,
- }
- pdu3 := gosnmp.SnmpPDU{
- Value: 1,
- Name: "3",
- Type: gosnmp.Integer,
- }
- pdu4 := gosnmp.SnmpPDU{
- Value: 1,
- Name: "4",
- Type: gosnmp.Integer,
- }
- mockGoSNMP.On("Get", []string{"1", "2"}).Return(&gosnmp.SnmpPacket{Variables: []gosnmp.SnmpPDU{pdu1, pdu2}}, nil)
- mockGoSNMP.On("Get", []string{"3", "4"}).Return(&gosnmp.SnmpPacket{Variables: []gosnmp.SnmpPDU{pdu3, pdu4}}, nil)
- mockGoSNMP.On("GetMaxOids", mock.Anything).Return(2)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- oidSlice := []string{"1", "2", "3", "4"}
- returnedSNMPData := client.GetScalarData(oidSlice, &scraperErrors)
- require.NoError(t, scraperErrors.Combine())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client float data type properly converted",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{
- {
- oid: "1",
- value: 1.0,
- valueType: floatVal,
- },
- }
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- pdu1 := gosnmp.SnmpPDU{
- Value: 1.0,
- Name: "1",
- Type: gosnmp.OpaqueDouble,
- }
- mockGoSNMP.On("Get", []string{"1"}).Return(&gosnmp.SnmpPacket{Variables: []gosnmp.SnmpPDU{pdu1}}, nil)
- mockGoSNMP.On("GetMaxOids", mock.Anything).Return(2)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- oidSlice := []string{"1"}
- returnedSNMPData := client.GetScalarData(oidSlice, &scraperErrors)
- require.NoError(t, scraperErrors.Combine())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client float data type with bad value adds error",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{}
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- pdu1 := gosnmp.SnmpPDU{
- Value: true,
- Name: "1",
- Type: gosnmp.OpaqueDouble,
- }
- mockGoSNMP.On("Get", []string{"1"}).Return(&gosnmp.SnmpPacket{Variables: []gosnmp.SnmpPDU{pdu1}}, nil)
- mockGoSNMP.On("GetMaxOids", mock.Anything).Return(2)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- oidSlice := []string{"1"}
- returnedSNMPData := client.GetScalarData(oidSlice, &scraperErrors)
- expectedErr := fmt.Errorf("problem with getting scalar data: data for OID '1' not a supported type")
- require.EqualError(t, scraperErrors.Combine(), expectedErr.Error())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client float data type with bad string value adds error",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{}
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- pdu1 := gosnmp.SnmpPDU{
- Value: "bad",
- Name: "1",
- Type: gosnmp.OpaqueDouble,
- }
- mockGoSNMP.On("Get", []string{"1"}).Return(&gosnmp.SnmpPacket{Variables: []gosnmp.SnmpPDU{pdu1}}, nil)
- mockGoSNMP.On("GetMaxOids", mock.Anything).Return(2)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- oidSlice := []string{"1"}
- returnedSNMPData := client.GetScalarData(oidSlice, &scraperErrors)
- expectedErr := fmt.Errorf("problem with getting scalar data: data for OID '1' not a supported type")
- require.EqualError(t, scraperErrors.Combine(), expectedErr.Error())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client int data type with bad value adds error",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{}
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- pdu1 := gosnmp.SnmpPDU{
- Value: float64(math.MaxFloat64),
- Name: "1",
- Type: gosnmp.Counter64,
- }
- mockGoSNMP.On("Get", []string{"1"}).Return(&gosnmp.SnmpPacket{Variables: []gosnmp.SnmpPDU{pdu1}}, nil)
- mockGoSNMP.On("GetMaxOids", mock.Anything).Return(2)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- oidSlice := []string{"1"}
- returnedSNMPData := client.GetScalarData(oidSlice, &scraperErrors)
- expectedErr := fmt.Errorf("problem with getting scalar data: data for OID '1' not a supported type")
- require.EqualError(t, scraperErrors.Combine(), expectedErr.Error())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client string data type properly converted",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{
- {
- oid: "1",
- value: "test",
- valueType: stringVal,
- },
- }
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- pdu1 := gosnmp.SnmpPDU{
- Value: []byte("test"),
- Name: "1",
- Type: gosnmp.OctetString,
- }
- mockGoSNMP.On("Get", []string{"1"}).Return(&gosnmp.SnmpPacket{Variables: []gosnmp.SnmpPDU{pdu1}}, nil)
- mockGoSNMP.On("GetMaxOids", mock.Anything).Return(2)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- oidSlice := []string{"1"}
- returnedSNMPData := client.GetScalarData(oidSlice, &scraperErrors)
- require.NoError(t, scraperErrors.Combine())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- }
- for _, tc := range testCases {
- t.Run(tc.desc, tc.testFunc)
- }
- }
- func TestGetIndexedData(t *testing.T) {
- testCases := []struct {
- desc string
- testFunc func(*testing.T)
- }{
- {
- desc: "No OIDs does nothing",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{}
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- returnedSNMPData := client.GetIndexedData([]string{}, &scraperErrors)
- require.NoError(t, scraperErrors.Combine())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client failures adds error",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{}
- walkError := errors.New("Bad WALK")
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("GetVersion", mock.Anything).Return(gosnmp.Version2c)
- mockGoSNMP.On("BulkWalkAll", "1").Return(nil, walkError)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- oidSlice := []string{"1"}
- returnedSNMPData := client.GetIndexedData(oidSlice, &scraperErrors)
- expectedErr := fmt.Errorf("problem with getting indexed data: problem with SNMP WALK for OID '1': %w", walkError)
- require.EqualError(t, scraperErrors.Combine(), expectedErr.Error())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client timeout failures tries to reset connection",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{}
- walkError := errors.New("request timeout (after 0 retries)")
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("GetVersion", mock.Anything).Return(gosnmp.Version2c)
- mockGoSNMP.On("BulkWalkAll", "1").Return(nil, walkError)
- mockGoSNMP.On("Close", mock.Anything).Return(nil)
- mockGoSNMP.On("Connect", mock.Anything).Return(nil)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- oidSlice := []string{"1"}
- returnedSNMPData := client.GetIndexedData(oidSlice, &scraperErrors)
- expectedErr := fmt.Errorf("problem with getting indexed data: problem with SNMP WALK for OID '1': %w", walkError)
- require.EqualError(t, scraperErrors.Combine(), expectedErr.Error())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client reset connection fails on connect adds errors",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{}
- walkError := errors.New("request timeout (after 0 retries)")
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("GetVersion", mock.Anything).Return(gosnmp.Version2c)
- mockGoSNMP.On("BulkWalkAll", "1").Return(nil, walkError)
- mockGoSNMP.On("Close", mock.Anything).Return(nil)
- connectErr := errors.New("can't connect")
- mockGoSNMP.On("Connect", mock.Anything).Return(connectErr)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- oidSlice := []string{"1"}
- returnedSNMPData := client.GetIndexedData(oidSlice, &scraperErrors)
- expectedErr1 := fmt.Errorf("problem with getting indexed data: problem with SNMP WALK for OID '1': %w", walkError)
- expectedErr2 := fmt.Errorf("problem with getting indexed data: problem connecting while trying to reset connection: %w", connectErr)
- expectedErr := fmt.Errorf(expectedErr1.Error() + "; " + expectedErr2.Error())
- require.EqualError(t, scraperErrors.Combine(), expectedErr.Error())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client partial failures still returns successes",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{
- {
- columnOID: "2",
- oid: "2.1",
- value: int64(1),
- valueType: integerVal,
- },
- }
- pdu1 := gosnmp.SnmpPDU{
- Value: 1,
- Name: "2.1",
- Type: gosnmp.Integer,
- }
- walkError := errors.New("Bad Walk")
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("GetVersion", mock.Anything).Return(gosnmp.Version2c)
- mockGoSNMP.On("BulkWalkAll", "1").Return(nil, walkError).Once()
- mockGoSNMP.On("BulkWalkAll", "2").Return([]gosnmp.SnmpPDU{pdu1}, nil).Once()
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- oidSlice := []string{"1", "2"}
- returnedSNMPData := client.GetIndexedData(oidSlice, &scraperErrors)
- expectedErr := fmt.Errorf("problem with getting indexed data: problem with SNMP WALK for OID '1': %w", walkError)
- require.EqualError(t, scraperErrors.Combine(), expectedErr.Error())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client returned nil value does not return data",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{}
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("GetVersion", mock.Anything).Return(gosnmp.Version2c)
- badOID := "1.1"
- pdu := gosnmp.SnmpPDU{
- Value: nil,
- Name: badOID,
- Type: gosnmp.Integer,
- }
- mockGoSNMP.On("BulkWalkAll", "1").Return([]gosnmp.SnmpPDU{pdu}, nil)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- oidSlice := []string{"1"}
- returnedSNMPData := client.GetIndexedData(oidSlice, &scraperErrors)
- expectedErr := fmt.Errorf("problem with getting indexed data: data for OID '%s' not found", badOID)
- require.EqualError(t, scraperErrors.Combine(), expectedErr.Error())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client returned unsupported type value does not return data",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{}
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("GetVersion", mock.Anything).Return(gosnmp.Version2c)
- badOID := "1.1"
- pdu := gosnmp.SnmpPDU{
- Value: true,
- Name: badOID,
- Type: gosnmp.Boolean,
- }
- mockGoSNMP.On("BulkWalkAll", "1").Return([]gosnmp.SnmpPDU{pdu}, nil)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- oidSlice := []string{"1"}
- returnedSNMPData := client.GetIndexedData(oidSlice, &scraperErrors)
- expectedErr := fmt.Errorf("problem with getting indexed data: data for OID '%s' not a supported type", badOID)
- require.EqualError(t, scraperErrors.Combine(), expectedErr.Error())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "Return multiple good values",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{
- {
- columnOID: "1",
- oid: "1.1",
- value: int64(1),
- valueType: integerVal,
- },
- {
- columnOID: "1",
- oid: "1.2",
- value: int64(2),
- valueType: integerVal,
- },
- {
- columnOID: "2",
- oid: "2.1",
- value: int64(3),
- valueType: integerVal,
- },
- {
- columnOID: "2",
- oid: "2.2",
- value: int64(4),
- valueType: integerVal,
- },
- }
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("GetVersion", mock.Anything).Return(gosnmp.Version2c)
- pdu1 := gosnmp.SnmpPDU{
- Value: 1,
- Name: "1.1",
- Type: gosnmp.Integer,
- }
- pdu2 := gosnmp.SnmpPDU{
- Value: 2,
- Name: "1.2",
- Type: gosnmp.Integer,
- }
- pdu3 := gosnmp.SnmpPDU{
- Value: 3,
- Name: "2.1",
- Type: gosnmp.Integer,
- }
- pdu4 := gosnmp.SnmpPDU{
- Value: 4,
- Name: "2.2",
- Type: gosnmp.Integer,
- }
- mockGoSNMP.On("BulkWalkAll", "1").Return([]gosnmp.SnmpPDU{pdu1, pdu2}, nil)
- mockGoSNMP.On("BulkWalkAll", "2").Return([]gosnmp.SnmpPDU{pdu3, pdu4}, nil)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- returnedSNMPData := client.GetIndexedData([]string{"1", "2"}, &scraperErrors)
- require.NoError(t, scraperErrors.Combine())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client float data type properly converted",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{
- {
- columnOID: "1",
- oid: "1.1",
- value: 1.0,
- valueType: floatVal,
- },
- }
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("GetVersion", mock.Anything).Return(gosnmp.Version2c)
- pdu := gosnmp.SnmpPDU{
- Value: 1.0,
- Name: "1.1",
- Type: gosnmp.OpaqueDouble,
- }
- mockGoSNMP.On("BulkWalkAll", "1").Return([]gosnmp.SnmpPDU{pdu}, nil)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- returnedSNMPData := client.GetIndexedData([]string{"1"}, &scraperErrors)
- require.NoError(t, scraperErrors.Combine())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client float data type with bad value adds error",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{}
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("GetVersion", mock.Anything).Return(gosnmp.Version2c)
- pdu := gosnmp.SnmpPDU{
- Value: true,
- Name: "1.1",
- Type: gosnmp.OpaqueDouble,
- }
- mockGoSNMP.On("BulkWalkAll", "1").Return([]gosnmp.SnmpPDU{pdu}, nil)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- returnedSNMPData := client.GetIndexedData([]string{"1"}, &scraperErrors)
- expectedErr := fmt.Errorf("problem with getting indexed data: data for OID '1.1' not a supported type")
- require.EqualError(t, scraperErrors.Combine(), expectedErr.Error())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client float data type with bad string value adds error",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{}
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("GetVersion", mock.Anything).Return(gosnmp.Version2c)
- pdu := gosnmp.SnmpPDU{
- Value: "bad",
- Name: "1.1",
- Type: gosnmp.OpaqueDouble,
- }
- mockGoSNMP.On("BulkWalkAll", "1").Return([]gosnmp.SnmpPDU{pdu}, nil)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- returnedSNMPData := client.GetIndexedData([]string{"1"}, &scraperErrors)
- expectedErr := fmt.Errorf("problem with getting indexed data: data for OID '1.1' not a supported type")
- require.EqualError(t, scraperErrors.Combine(), expectedErr.Error())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client int data type with bad value adds error",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{}
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("GetVersion", mock.Anything).Return(gosnmp.Version2c)
- pdu := gosnmp.SnmpPDU{
- Value: float64(math.MaxFloat64),
- Name: "1.1",
- Type: gosnmp.Counter64,
- }
- mockGoSNMP.On("BulkWalkAll", "1").Return([]gosnmp.SnmpPDU{pdu}, nil)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- returnedSNMPData := client.GetIndexedData([]string{"1"}, &scraperErrors)
- expectedErr := fmt.Errorf("problem with getting indexed data: data for OID '1.1' not a supported type")
- require.EqualError(t, scraperErrors.Combine(), expectedErr.Error())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client string data type properly converted",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{
- {
- columnOID: "1",
- oid: "1.1",
- value: "test",
- valueType: stringVal,
- },
- }
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("GetVersion", mock.Anything).Return(gosnmp.Version2c)
- pdu := gosnmp.SnmpPDU{
- Value: []byte("test"),
- Name: "1.1",
- Type: gosnmp.OctetString,
- }
- mockGoSNMP.On("BulkWalkAll", "1").Return([]gosnmp.SnmpPDU{pdu}, nil)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- returnedSNMPData := client.GetIndexedData([]string{"1"}, &scraperErrors)
- require.NoError(t, scraperErrors.Combine())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- {
- desc: "GoSNMP Client v1 uses normal Walk function",
- testFunc: func(t *testing.T) {
- expectedSNMPData := []SNMPData{
- {
- columnOID: "1",
- oid: "1.1",
- value: int64(1),
- valueType: integerVal,
- },
- }
- mockGoSNMP := new(mocks.MockGoSNMPWrapper)
- mockGoSNMP.On("GetVersion", mock.Anything).Return(gosnmp.Version1)
- pdu := gosnmp.SnmpPDU{
- Value: 1,
- Name: "1.1",
- Type: gosnmp.Counter32,
- }
- mockGoSNMP.On("WalkAll", "1").Return([]gosnmp.SnmpPDU{pdu}, nil)
- mockGoSNMP.On("GetMaxOids", mock.Anything).Return(2)
- client := &snmpClient{
- logger: zap.NewNop(),
- client: mockGoSNMP,
- }
- var scraperErrors scrapererror.ScrapeErrors
- returnedSNMPData := client.GetIndexedData([]string{"1"}, &scraperErrors)
- require.NoError(t, scraperErrors.Combine())
- require.Equal(t, expectedSNMPData, returnedSNMPData)
- },
- },
- }
- for _, tc := range testCases {
- t.Run(tc.desc, tc.testFunc)
- }
- }
|