diff --git a/collector_test.go b/collector_test.go index 40249624..d9a3b8ec 100644 --- a/collector_test.go +++ b/collector_test.go @@ -14,6 +14,7 @@ package main import ( + "errors" "reflect" "regexp" "testing" @@ -497,6 +498,41 @@ func TestPduValueAsString(t *testing.T) { } } +func TestParseDateAndTime(t *testing.T) { + cases := []struct { + pdu *gosnmp.SnmpPDU + result float64 + err error + }{ + // No timezone, use UTC + { + pdu: &gosnmp.SnmpPDU{Value: []byte{7, 226, 8, 15, 8, 1, 15, 0}}, + result: 1534320075, + err: nil, + }, + // +0200 + { + pdu: &gosnmp.SnmpPDU{Value: []byte{7, 226, 8, 15, 8, 1, 15, 0, 43, 2, 0}}, + result: 1534312875, + err: nil, + }, + { + pdu: &gosnmp.SnmpPDU{Value: []byte{0}}, + result: 0, + err: errors.New("invalid DateAndTime length 1"), + }, + } + for _, c := range cases { + got, err := parseDateAndTime(c.pdu) + if !reflect.DeepEqual(err, c.err) { + t.Errorf("parseDateAndTime(%v) error: got %v, want %v", c.pdu, err, c.err) + } + if !reflect.DeepEqual(got, c.result) { + t.Errorf("parseDateAndTime(%v) result: got %v, want %v", c.pdu, got, c.result) + } + } +} + func TestIndexesToLabels(t *testing.T) { cases := []struct { oid []int diff --git a/generator/tree_test.go b/generator/tree_test.go index bdbabcf4..be91a564 100644 --- a/generator/tree_test.go +++ b/generator/tree_test.go @@ -119,6 +119,11 @@ func TestTreePrepare(t *testing.T) { in: &Node{Oid: "1", Type: "OPAQUE", TextualConvention: "Double"}, out: &Node{Oid: "1", Type: "Double", TextualConvention: "Double"}, }, + // RFC 2579 DateAndTime. + { + in: &Node{Oid: "1", Type: "DisplayString", TextualConvention: "DateAndTime"}, + out: &Node{Oid: "1", Type: "DateAndTime", TextualConvention: "DateAndTime"}, + }, } for i, c := range cases { // Indexes always end up initilized. @@ -312,6 +317,7 @@ func TestGenerateConfigModule(t *testing.T) { {Oid: "1.100", Access: "ACCESS_READONLY", Label: "MacAddress", Type: "OCTETSTR", Hint: "1x:"}, {Oid: "1.200", Access: "ACCESS_READONLY", Label: "Float", Type: "OPAQUE", TextualConvention: "Float"}, {Oid: "1.201", Access: "ACCESS_READONLY", Label: "Double", Type: "OPAQUE", TextualConvention: "Double"}, + {Oid: "1.202", Access: "ACCESS_READONLY", Label: "DateAndTime", Type: "DisplayString", TextualConvention: "DateAndTime"}, }}, cfg: &ModuleConfig{ Walk: []string{"root", "1.3"}, @@ -409,6 +415,12 @@ func TestGenerateConfigModule(t *testing.T) { Type: "Double", Help: " - 1.201", }, + { + Name: "DateAndTime", + Oid: "1.202", + Type: "DateAndTime", + Help: " - 1.202", + }, }, }, },