-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclient_test.go
More file actions
138 lines (122 loc) · 2.93 KB
/
client_test.go
File metadata and controls
138 lines (122 loc) · 2.93 KB
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
// Copyright 2016 Paul Stuart. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.
package snmputil
import (
"errors"
"fmt"
"log"
"os"
"strconv"
"testing"
"github.com/gosnmp/gosnmp"
)
const (
sysName = ".1.3.6.1.2.1.1.5.0"
oidFile = "oids.json"
mibs = "SNMPv2-MIB:IF-MIB"
)
var (
// snmpgetnext -v 3 -n "" -u MD5User -a MD5 -A "The Net-SNMP Demo Password" -l authNoPriv test.net-snmp.org sysUpTime
// snmpget -v 2c -c demopublic test.net-snmp.org SNMPv2-MIB::sysUpTime.0
// snmpget -v 3 -n "" -u authMD5OnlyUser -a MD5 -A "testingpass0123456789" -l authNoPriv 127.0.0.1 sysUpTime.0
testPassword = "testingpass0123456789"
testHost = "127.0.0.1"
testCommunity = "public"
testUser = "authMD5OnlyUser"
testTimeout = 5
testRetries = 1
profileV2 Profile
profileV3 Profile
testTags = map[string]string{
"testing": "this is a test",
}
logger *log.Logger
)
func envStr(name string, value *string) {
if env := os.Getenv(name); len(env) > 0 && value != nil {
*value = env
}
}
func envInt(name string, value *int) {
if env := os.Getenv(name); len(env) > 0 && value != nil {
v, err := strconv.Atoi(env)
if err != nil {
panic(err)
}
*value = v
}
}
func init() {
if err := LoadMIBs(oidFile, mibs); err != nil {
panic(err)
}
if testing.Verbose() {
logger = log.New(os.Stderr, "", 0)
}
envStr("SNMP_HOST", &testHost)
envStr("SNMP_COMMUNITY", &testCommunity)
envStr("SNMP_USER", &testUser)
envStr("SNMP_PASSWORD", &testPassword)
envInt("SNMP_TIMEOUT", &testTimeout)
envInt("SNMP_RETRIES", &testRetries)
profileV2 = Profile{
Host: testHost,
Version: "2c",
Community: testCommunity,
Retries: testRetries,
Timeout: testTimeout,
}
profileV3 = Profile{
Host: testHost,
Version: "3",
SecLevel: "AuthNoPriv",
AuthUser: testUser,
AuthProto: "MD5",
AuthPass: testPassword,
Retries: testRetries,
Timeout: testTimeout,
}
}
func testSysName(client *gosnmp.GoSNMP) error {
oids := []string{sysName}
packet, err := client.Get(oids)
if err != nil {
return errors.Wrap(err, "get failed")
}
if len(packet.Variables) < 1 {
return errors.New("no packets returned for sysName")
}
pdu := packet.Variables[0]
if pdu.Name != sysName {
return fmt.Errorf("pdu OID (%s) does not match that of sysName", pdu.Name)
}
if pdu.Value == nil {
return errors.New("nil value returned for sysName")
}
val := string(pdu.Value.([]uint8))
if len(val) == 0 {
return errors.New("no value returned for sysName")
}
return nil
}
func TestV2Profile(t *testing.T) {
client, err := newClient(profileV2)
if err != nil {
t.Error(err)
}
if err := testSysName(client); err != nil {
t.Error(err)
}
client.Conn.Close()
}
func TestV3Profile(t *testing.T) {
client, err := newClient(profileV3)
if err != nil {
t.Error(err)
}
if err := testSysName(client); err != nil {
t.Error(err)
}
client.Conn.Close()
}