| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package snmputils |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/golang/mock/gomock" |
| 10 | "github.com/gosnmp/gosnmp" |
| 11 | snmpmock "github.com/gosnmp/gosnmp/mocks" |
| 12 | "github.com/stretchr/testify/assert" |
| 13 | "github.com/stretchr/testify/require" |
| 14 | ) |
| 15 | |
| 16 | func TestGetSysUptime(t *testing.T) { |
| 17 | tests := map[string]struct { |
| 18 | pdus []gosnmp.SnmpPDU |
| 19 | getErr error |
| 20 | expected int64 |
| 21 | wantErr bool |
| 22 | }{ |
| 23 | "prefers_snmp_engine_time_seconds": { |
| 24 | pdus: []gosnmp.SnmpPDU{ |
| 25 | {Name: OidSnmpEngineTime, Type: gosnmp.Integer, Value: 1234}, |
| 26 | {Name: OidHrSystemUptime, Type: gosnmp.TimeTicks, Value: uint32(999900)}, |
| 27 | {Name: OidSysUpTime, Type: gosnmp.TimeTicks, Value: uint32(888800)}, |
| 28 | }, |
| 29 | expected: 1234, |
| 30 | }, |
| 31 | "falls_back_to_host_resources_timeticks": { |
| 32 | pdus: []gosnmp.SnmpPDU{ |
| 33 | {Name: OidSnmpEngineTime, Type: gosnmp.NoSuchObject, Value: nil}, |
| 34 | {Name: "." + OidHrSystemUptime, Type: gosnmp.TimeTicks, Value: uint32(123456)}, |
| 35 | {Name: OidSysUpTime, Type: gosnmp.TimeTicks, Value: uint32(888800)}, |
| 36 | }, |
| 37 | expected: 1234, |
| 38 | }, |
| 39 | "falls_back_to_mib2_sysuptime_timeticks": { |
| 40 | pdus: []gosnmp.SnmpPDU{ |
| 41 | {Name: OidSnmpEngineTime, Type: gosnmp.NoSuchInstance, Value: nil}, |
| 42 | {Name: OidHrSystemUptime, Type: gosnmp.NoSuchObject, Value: nil}, |
| 43 | {Name: OidSysUpTime, Type: gosnmp.TimeTicks, Value: uint32(987654)}, |
| 44 | }, |
| 45 | expected: 9876, |
| 46 | }, |
| 47 | "returns_zero_when_no_source_has_data": { |
| 48 | pdus: []gosnmp.SnmpPDU{ |
| 49 | {Name: OidSnmpEngineTime, Type: gosnmp.NoSuchObject, Value: nil}, |
| 50 | {Name: OidHrSystemUptime, Type: gosnmp.NoSuchObject, Value: nil}, |
| 51 | {Name: OidSysUpTime, Type: gosnmp.NoSuchObject, Value: nil}, |
| 52 | }, |
| 53 | expected: 0, |
| 54 | }, |
| 55 | "returns_get_error": { |
| 56 | getErr: errors.New("timeout"), |
| 57 | wantErr: true, |
| 58 | }, |
| 59 | "returns_conversion_error_when_only_available_source_is_invalid": { |
| 60 | pdus: []gosnmp.SnmpPDU{ |
| 61 | {Name: OidSnmpEngineTime, Type: gosnmp.OctetString, Value: []byte("bad")}, |
| 62 | {Name: OidHrSystemUptime, Type: gosnmp.NoSuchObject, Value: nil}, |
| 63 | {Name: OidSysUpTime, Type: gosnmp.NoSuchObject, Value: nil}, |
| 64 | }, |
| 65 | wantErr: true, |
| 66 | }, |
| 67 | } |
| 68 | |
| 69 | for name, tc := range tests { |
| 70 | t.Run(name, func(t *testing.T) { |
| 71 | ctrl := gomock.NewController(t) |
| 72 | defer ctrl.Finish() |
| 73 | |
| 74 | client := snmpmock.NewMockHandler(ctrl) |
| 75 | client.EXPECT().Get(gomock.InAnyOrder(sysUptimeOIDs())).Return(&gosnmp.SnmpPacket{Variables: tc.pdus}, tc.getErr) |
| 76 | |
| 77 | actual, err := GetSysUptime(client) |
| 78 | if tc.wantErr { |
| 79 | require.Error(t, err) |
| 80 | } else { |
| 81 | require.NoError(t, err) |
| 82 | } |
| 83 | assert.Equal(t, tc.expected, actual) |
| 84 | }) |
| 85 | } |
| 86 | } |