Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Kochie <[email protected]>
  • Loading branch information
SuperQ committed Aug 15, 2018
1 parent fb04b9c commit 01e327b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
36 changes: 36 additions & 0 deletions collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package main

import (
"errors"
"reflect"
"regexp"
"testing"
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions generator/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -409,6 +415,12 @@ func TestGenerateConfigModule(t *testing.T) {
Type: "Double",
Help: " - 1.201",
},
{
Name: "DateAndTime",
Oid: "1.202",
Type: "DateAndTime",
Help: " - 1.202",
},
},
},
},
Expand Down

0 comments on commit 01e327b

Please sign in to comment.