Skip to content

Commit

Permalink
[chore] fix a few warnings in snmpClient (open-telemetry#29906)
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme authored and cparkins committed Jan 10, 2024
1 parent 6fca36f commit 34e8c65
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 38 deletions.
8 changes: 4 additions & 4 deletions receiver/snmpreceiver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (c *snmpClient) Close() error {
// GetScalarData retrieves and returns scalar data from passed in scalar OIDs.
// Note: These OIDs must all end in ".0" for the SNMP GET to work correctly
func (c *snmpClient) GetScalarData(oids []string, scraperErrors *scrapererror.ScrapeErrors) []SNMPData {
scalarData := []SNMPData{}
var scalarData []SNMPData

// Nothing to do if there are no OIDs
if len(oids) == 0 {
Expand Down Expand Up @@ -244,7 +244,7 @@ func (c *snmpClient) GetScalarData(oids []string, scraperErrors *scrapererror.Sc
// GetIndexedData retrieves indexed metrics from passed in column OIDs. The returned data
// is then also passed into the provided function.
func (c *snmpClient) GetIndexedData(oids []string, scraperErrors *scrapererror.ScrapeErrors) []SNMPData {
indexedData := []SNMPData{}
var indexedData []SNMPData

// Nothing to do if there are no OIDs
if len(oids) == 0 {
Expand Down Expand Up @@ -372,7 +372,7 @@ func (c *snmpClient) convertSnmpPDUToSnmpData(pdu gosnmp.SnmpPDU) SNMPData {
// This is a convenience function to make working with SnmpPDU's easier - it
// reduces the need for type assertions. A int64 is convenient, as SNMP can
// return int32, uint32, and int64.
func (c snmpClient) toInt64(name string, value any) (int64, error) {
func (c *snmpClient) toInt64(name string, value any) (int64, error) {
switch value := value.(type) { // shadow
case uint:
return int64(value), nil
Expand Down Expand Up @@ -405,7 +405,7 @@ func (c snmpClient) toInt64(name string, value any) (int64, error) {
// This is a convenience function to make working with SnmpPDU's easier - it
// reduces the need for type assertions. A float64 is convenient, as SNMP can
// return float32 and float64.
func (c snmpClient) toFloat64(name string, value any) (float64, error) {
func (c *snmpClient) toFloat64(name string, value any) (float64, error) {
switch value := value.(type) { // shadow
case float32:
return float64(value), nil
Expand Down
52 changes: 18 additions & 34 deletions receiver/snmpreceiver/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func TestGetScalarData(t *testing.T) {
{
desc: "No OIDs does nothing",
testFunc: func(t *testing.T) {
expectedSNMPData := []SNMPData{}
var expectedSNMPData []SNMPData
mockGoSNMP := new(mocks.MockGoSNMPWrapper)
client := &snmpClient{
logger: zap.NewNop(),
Expand All @@ -219,7 +219,6 @@ func TestGetScalarData(t *testing.T) {
{
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)
Expand All @@ -233,13 +232,13 @@ func TestGetScalarData(t *testing.T) {
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)
require.Nil(t, returnedSNMPData)
},
},
{
desc: "GoSNMP Client timeout failures tries to reset connection",
testFunc: func(t *testing.T) {
expectedSNMPData := []SNMPData{}
var expectedSNMPData []SNMPData
getError := errors.New("request timeout (after 0 retries)")
mockGoSNMP := new(mocks.MockGoSNMPWrapper)
mockGoSNMP.On("Get", []string{"1"}).Return(nil, getError)
Expand All @@ -261,7 +260,6 @@ func TestGetScalarData(t *testing.T) {
{
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)
Expand All @@ -280,7 +278,7 @@ func TestGetScalarData(t *testing.T) {
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)
require.Nil(t, returnedSNMPData)
},
},
{
Expand Down Expand Up @@ -321,7 +319,6 @@ func TestGetScalarData(t *testing.T) {
{
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,
Expand All @@ -341,13 +338,12 @@ func TestGetScalarData(t *testing.T) {
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)
require.Nil(t, 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,
Expand All @@ -367,7 +363,7 @@ func TestGetScalarData(t *testing.T) {
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)
require.Nil(t, returnedSNMPData)
},
},
{
Expand Down Expand Up @@ -462,7 +458,6 @@ func TestGetScalarData(t *testing.T) {
{
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,
Expand All @@ -480,13 +475,12 @@ func TestGetScalarData(t *testing.T) {
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)
require.Nil(t, 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",
Expand All @@ -504,13 +498,12 @@ func TestGetScalarData(t *testing.T) {
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)
require.Nil(t, 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),
Expand All @@ -528,7 +521,7 @@ func TestGetScalarData(t *testing.T) {
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)
require.Nil(t, returnedSNMPData)
},
},
{
Expand Down Expand Up @@ -575,7 +568,6 @@ func TestGetIndexedData(t *testing.T) {
{
desc: "No OIDs does nothing",
testFunc: func(t *testing.T) {
expectedSNMPData := []SNMPData{}
mockGoSNMP := new(mocks.MockGoSNMPWrapper)
client := &snmpClient{
logger: zap.NewNop(),
Expand All @@ -584,13 +576,12 @@ func TestGetIndexedData(t *testing.T) {
var scraperErrors scrapererror.ScrapeErrors
returnedSNMPData := client.GetIndexedData([]string{}, &scraperErrors)
require.NoError(t, scraperErrors.Combine())
require.Equal(t, expectedSNMPData, returnedSNMPData)
require.Nil(t, 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)
Expand All @@ -604,13 +595,12 @@ func TestGetIndexedData(t *testing.T) {
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)
require.Nil(t, 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)
Expand All @@ -626,13 +616,12 @@ func TestGetIndexedData(t *testing.T) {
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)
require.Nil(t, 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)
Expand All @@ -651,7 +640,7 @@ func TestGetIndexedData(t *testing.T) {
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)
require.Nil(t, returnedSNMPData)
},
},
{
Expand Down Expand Up @@ -690,7 +679,6 @@ func TestGetIndexedData(t *testing.T) {
{
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"
Expand All @@ -709,13 +697,12 @@ func TestGetIndexedData(t *testing.T) {
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)
require.Nil(t, 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"
Expand All @@ -734,7 +721,7 @@ func TestGetIndexedData(t *testing.T) {
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)
require.Nil(t, returnedSNMPData)
},
},
{
Expand Down Expand Up @@ -833,7 +820,6 @@ func TestGetIndexedData(t *testing.T) {
{
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{
Expand All @@ -850,13 +836,12 @@ func TestGetIndexedData(t *testing.T) {
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)
require.Nil(t, 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{
Expand All @@ -873,13 +858,12 @@ func TestGetIndexedData(t *testing.T) {
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)
require.Nil(t, 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{
Expand All @@ -896,7 +880,7 @@ func TestGetIndexedData(t *testing.T) {
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)
require.Nil(t, returnedSNMPData)
},
},
{
Expand Down

0 comments on commit 34e8c65

Please sign in to comment.