forked from masiulaniec/snmp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
get_test.go
145 lines (128 loc) · 2.75 KB
/
get_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// +build ignore
package snmp
import (
"fmt"
"strings"
"testing"
)
type GetTest struct {
oid string
expect interface{}
}
var getTests = []GetTest{
{stringType, []byte(nil)},
{oidType, []int(nil)},
{timeticksType, int64(0)},
{counter32Type, int64(0)},
{counter64Type, int64(0)},
{gauge32Type, int64(0)},
}
func TestGetNil(t *testing.T) {
var v interface{}
for _, test := range getTests {
if err := Get("localhost", "public", test.oid, &v); err != nil {
t.Errorf("%s unexpected error: %v", test.oid, err)
continue
}
have := fmt.Sprintf("%T", v)
want := fmt.Sprintf("%T", test.expect)
if have != want {
t.Errorf("%s bad type, want=%s, have=%s", test.oid, want, have)
}
}
}
type HostTest struct {
host string
expect string
}
var hostTests = []string{
"localhost",
"localhost:161",
}
func TestHostPort(t *testing.T) {
var want string
for i, host := range hostTests {
var v []byte
if err := Get(host, "public", stringType, &v); err != nil {
t.Errorf("%s unexpected error: %v", host, err)
continue
}
if have := string(v); i == 0 {
want = have
} else if have != want {
t.Errorf("%s wrong host, want=%s, have=%s", host, want, have)
}
}
}
type CommunityTest struct {
str string
ok bool
err error
}
var communityTests = []CommunityTest{
{str: "", ok: false},
{str: "public", ok: true},
{str: "invalid", ok: false},
}
func TestCommunity(t *testing.T) {
ch := make(chan CommunityTest)
for _, test := range communityTests {
str := test.str
test := test
go func() {
var v interface{}
test.err = Get("localhost", str, stringType, &v)
ch <- test
}()
}
for _ = range communityTests {
test := <-ch
if (test.err == nil) != test.ok {
t.Errorf("%s invalid err: %s", test.str, test.err)
}
}
}
type TestRun struct {
req *Request
}
func TestParallel(t *testing.T) {
const N = 1000
done := make(chan bool)
for i := 0; i < N; i++ {
go func() {
var v interface{}
if err := Get("localhost", "public", stringType, &v); err != nil {
t.Errorf("%d unexpected error: %v", i, err)
}
done <- true
}()
}
for i := 0; i < N; i++ {
<-done
}
}
type MultiTest struct {
args []interface{}
expect string
}
var outInt int
var multiTests = []MultiTest{
{[]interface{}{}, ""},
{[]interface{}{"IF-MIB::ifMtu.1", &outInt}, ""},
{[]interface{}{"IF-MIB::ifMtu.1", &outInt, "IF-MIB::ifSpeed.1", &outInt}, ""},
}
func TestGetMulti(t *testing.T) {
for _, test := range multiTests {
err := Get("localhost", "public", test.args...)
if (test.expect == "") != (err == nil) {
t.Errorf("%v: unexpected error: %v", test.args, err)
continue
}
if err == nil {
continue
}
if i := strings.Index(err.Error(), test.expect); i < 0 {
t.Errorf("%v: unexpected error: %v", test.args, err)
}
}
}