forked from idawes/gosnmp
/
v2cClient_test.go
302 lines (290 loc) · 10.8 KB
/
v2cClient_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package gosnmp_test
import (
"fmt"
"github.com/cihub/seelog"
"github.com/davecgh/go-spew/spew"
snmp "github.com/idawes/gosnmp"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"math"
"strconv"
"sync"
"time"
)
func setupV2cClientTest(logger seelog.LoggerInterface, testIdGenerator chan string) {
Describe("V2cClient", func() {
var (
clientCtxt *snmp.ClientContext
numClients int
clients []*snmp.V2cClient
err error
)
BeforeEach(func() {
testId := <-testIdGenerator
clientCtxt = snmp.NewClientContext(testId, 1000, logger)
numClients = 1
})
JustBeforeEach(func() {
clients = make([]*snmp.V2cClient, numClients)
for i := 0; i < numClients; i++ {
clients[i], err = clientCtxt.NewV2cClientWithPort("private", "localhost", 2000+i)
}
})
AfterEach(func() {
clientCtxt.Shutdown()
})
Describe("creating a client", func() {
var errClient *snmp.V2cClient
Context("using valid parameters", func() {
It("should create a correctly configured client", func() {
Ω(clients[0]).ShouldNot(BeNil())
Ω(err).Should(BeNil())
Ω(clients[0].Community).Should(Equal("private"))
Ω(clients[0].Address.String()).Should(Equal("127.0.0.1:" + strconv.Itoa(2000)))
})
})
Context("using an invalid", func() {
ValidateClientCreationFailure := func(errorMsg string) {
It("should generate an error with appropriate error text", func() {
Ω(errClient).Should(BeNil())
Ω(err).ShouldNot(BeNil())
Ω(err.Error()).Should(Equal(errorMsg))
})
}
BeforeEach(func() {
numClients = 0
})
Context("address", func() {
BeforeEach(func() {
errClient, err = clientCtxt.NewV2cClientWithPort("private", "blahblah", 2000)
})
ValidateClientCreationFailure("lookup blahblah: no such host")
})
Context("port (out of range low)", func() {
BeforeEach(func() {
errClient, err = clientCtxt.NewV2cClientWithPort("private", "localhost", 0)
})
ValidateClientCreationFailure("invalid port: 0")
})
Context("port (extremly out of range low)", func() {
BeforeEach(func() {
errClient, err = clientCtxt.NewV2cClientWithPort("private", "localhost", math.MinInt64)
})
ValidateClientCreationFailure(fmt.Sprintf("invalid port: %d", math.MinInt64))
})
Context("port (out of range high)", func() {
BeforeEach(func() {
errClient, err = clientCtxt.NewV2cClientWithPort("private", "localhost", 65536)
})
ValidateClientCreationFailure("invalid port: 65536")
})
Context("port (extremely out of range high)", func() {
BeforeEach(func() {
errClient, err = clientCtxt.NewV2cClientWithPort("private", "localhost", math.MaxInt64)
})
ValidateClientCreationFailure(fmt.Sprintf("invalid port: %d", math.MaxInt64))
})
})
})
Describe("creating a request", func() {
var req *snmp.CommunityRequest
ValidateRequest := func() {
It("should be possible", func() {
Ω(req).ShouldNot(BeNil())
Ω(req.GetError()).Should(BeNil())
Ω(req.GetResponse()).Should(BeNil())
})
}
AfterEach(func() {
clientCtxt.FreeV2cRequest(req)
})
Context("as a GET request", func() {
Context("with no OIDS", func() {
BeforeEach(func() {
req = clientCtxt.AllocateV2cGetRequest()
})
ValidateRequest()
})
Context("with a predefined set of OIDS", func() {
BeforeEach(func() {
oids := []snmp.ObjectIdentifier{snmp.SYS_OBJECT_ID_OID, snmp.SYS_NAME_OID, snmp.SYS_LOCATION_OID, snmp.SYS_DESCR_OID, snmp.SYS_CONTACT_OID, snmp.SYS_UPTIME_OID}
req = clientCtxt.AllocateV2cGetRequestWithOids(oids)
})
ValidateRequest()
})
})
Context("as a SET request", func() {
BeforeEach(func() {
req = clientCtxt.AllocateV2cSetRequest()
})
ValidateRequest()
})
Context("as a GETNEXT request", func() {
BeforeEach(func() {
req = clientCtxt.AllocateV2cGetNextRequest()
})
ValidateRequest()
})
})
Describe("sending a request to a non-existent agent", func() {
ValidateRequestTimeout := func(retries int, timeoutSeconds int) {
It("should timeout after the appropriate amount of time", func(done Done) {
var waitGroup sync.WaitGroup
waitGroup.Add(numClients)
start := time.Now()
for i := 0; i < numClients; i++ {
clients[i].Retries = retries
clients[i].TimeoutSeconds = timeoutSeconds
go func(client *snmp.V2cClient) {
req := clientCtxt.AllocateV2cGetRequest()
client.SendRequest(req)
err := req.GetError()
Ω(err).ShouldNot(BeNil())
_, ok := err.(snmp.TimeoutError)
Ω(ok).Should(BeTrue())
waitGroup.Done()
clientCtxt.FreeV2cRequest(req)
}(clients[i])
}
waitGroup.Wait()
Ω(time.Since(start).Seconds()).Should(BeNumerically("<", float64(timeoutSeconds*(retries+1))+0.2))
statsBin, err := clientCtxt.GetStatsBin(0)
Ω(err).Should(BeNil())
Ω(statsBin.Stats[snmp.StatType_RESPONSES_RECEIVED]).Should(Equal(0))
Ω(statsBin.Stats[snmp.StatType_RESPONSES_RECEIVED_AFTER_REQUEST_TIMED_OUT]).Should(Equal(0))
Ω(statsBin.Stats[snmp.StatType_REQUESTS_TIMED_OUT_AFTER_RESPONSE_PROCESSED]).Should(Equal(0))
Ω(statsBin.Stats[snmp.StatType_REQUESTS_TIMED_OUT]).Should(Equal(numClients * retries))
Ω(statsBin.Stats[snmp.StatType_REQUESTS_SENT]).Should(Equal(numClients))
Ω(statsBin.Stats[snmp.StatType_REQUESTS_RETRIES_EXHAUSTED]).Should(Equal(numClients))
Ω(statsBin.Stats[snmp.StatType_REQUESTS_FORWARDED_TO_FLOW_CONTROL]).Should(Equal(numClients * (retries + 1)))
Ω(statsBin.Stats[snmp.StatType_OUTBOUND_MESSAGES_SENT]).Should(Equal(numClients * (retries + 1)))
close(done)
}, float64(timeoutSeconds*(retries+1))+2)
}
Context("from a single client", func() {
Context("using 0 retries and a timeout of 1 second", func() {
ValidateRequestTimeout(0, 1)
})
Context("using 1 retry and a timeout of 2 seconds", func() {
ValidateRequestTimeout(1, 2)
})
Context("using 2 retries and a timeout of 1 second", func() {
ValidateRequestTimeout(2, 1)
})
})
Context("from multiple clients", func() {
BeforeEach(func() {
numClients = 50
})
Context("using 0 retries and a timeout of 1 second", func() {
ValidateRequestTimeout(0, 1)
})
Context("using 1 retry and a timeout of 2 seconds", func() {
ValidateRequestTimeout(1, 2)
})
Context("using 2 retries and a timeout of 1 second", func() {
ValidateRequestTimeout(2, 1)
})
})
})
Describe("sending multiple requests to a non-existent agent", func() {
Context("from a single client", func() {
Context("using 0 retries and a timeout of 1 second", func() {
timeoutSeconds := 1
retries := 0
numRequests := 3
It("should timeout after the appropriate amount of time", func(done Done) {
var waitGroup sync.WaitGroup
waitGroup.Add(numRequests)
clients[0].Retries = retries
clients[0].TimeoutSeconds = timeoutSeconds
start := time.Now()
for i := 0; i < numRequests; i++ {
go func() {
req := clientCtxt.AllocateV2cGetRequest()
clients[0].SendRequest(req)
err := req.GetError()
Ω(err).ShouldNot(BeNil())
_, ok := err.(snmp.TimeoutError)
Ω(ok).Should(BeTrue(), spew.Sdump(req))
waitGroup.Done()
}()
}
waitGroup.Wait()
Ω(time.Since(start).Seconds()).Should(BeNumerically("<", float64(timeoutSeconds*(retries+1)*numRequests)+0.2))
statsBin, err := clientCtxt.GetStatsBin(0)
Ω(err).Should(BeNil())
Ω(statsBin.Stats[snmp.StatType_RESPONSES_RECEIVED]).Should(Equal(0))
Ω(statsBin.Stats[snmp.StatType_RESPONSES_RECEIVED_AFTER_REQUEST_TIMED_OUT]).Should(Equal(0))
Ω(statsBin.Stats[snmp.StatType_REQUESTS_TIMED_OUT_AFTER_RESPONSE_PROCESSED]).Should(Equal(0))
Ω(statsBin.Stats[snmp.StatType_REQUESTS_TIMED_OUT]).Should(Equal(numClients * numRequests * retries))
Ω(statsBin.Stats[snmp.StatType_REQUESTS_SENT]).Should(Equal(numClients * numRequests))
Ω(statsBin.Stats[snmp.StatType_REQUESTS_RETRIES_EXHAUSTED]).Should(Equal(numClients * numRequests))
Ω(statsBin.Stats[snmp.StatType_REQUESTS_FORWARDED_TO_FLOW_CONTROL]).Should(Equal(numClients * numRequests * (retries + 1)))
Ω(statsBin.Stats[snmp.StatType_OUTBOUND_MESSAGES_SENT]).Should(Equal(numClients * numRequests * (retries + 1)))
close(done)
}, float64(timeoutSeconds*(retries+1)*numRequests)+2)
})
})
})
Describe("sending a request to an active agent", func() {
var (
agent *snmp.Agent
)
BeforeEach(func() {
agent = snmp.NewAgentWithPort("testAgent", 10, 2000, logger)
time.Sleep(1 * time.Second)
})
AfterEach(func() {
agent.Shutdown()
})
ValidateResponse := func(retries int, timeoutSeconds int) {
It("should return a valid response", func(done Done) {
var waitGroup sync.WaitGroup
waitGroup.Add(numClients)
start := time.Now()
for i := 0; i < numClients; i++ {
clients[i].Retries = retries
clients[i].TimeoutSeconds = timeoutSeconds
go func(client *snmp.V2cClient) {
req := clientCtxt.AllocateV2cGetRequest()
req.AddOid(snmp.SYS_OBJECT_ID_OID)
req.AddOid(snmp.SYS_DESCR_OID)
client.SendRequest(req)
err := req.GetError()
Ω(err).Should(BeNil())
resp := req.GetResponse()
Ω(resp).ShouldNot(BeNil())
clientCtxt.FreeV2cRequest(req)
waitGroup.Done()
}(clients[i])
}
waitGroup.Wait()
Ω(time.Since(start).Seconds()).Should(BeNumerically("<", float64(timeoutSeconds*(retries+1))+0.2))
statsBin, err := clientCtxt.GetStatsBin(0)
Ω(err).Should(BeNil())
Ω(statsBin.Stats[snmp.StatType_RESPONSES_RECEIVED]).Should(Equal(0))
Ω(statsBin.Stats[snmp.StatType_RESPONSES_RECEIVED_AFTER_REQUEST_TIMED_OUT]).Should(Equal(0))
Ω(statsBin.Stats[snmp.StatType_REQUESTS_TIMED_OUT_AFTER_RESPONSE_PROCESSED]).Should(Equal(0))
Ω(statsBin.Stats[snmp.StatType_REQUESTS_TIMED_OUT]).Should(Equal(numClients * retries))
Ω(statsBin.Stats[snmp.StatType_REQUESTS_SENT]).Should(Equal(numClients))
Ω(statsBin.Stats[snmp.StatType_REQUESTS_RETRIES_EXHAUSTED]).Should(Equal(numClients))
Ω(statsBin.Stats[snmp.StatType_REQUESTS_FORWARDED_TO_FLOW_CONTROL]).Should(Equal(numClients * (retries + 1)))
Ω(statsBin.Stats[snmp.StatType_OUTBOUND_MESSAGES_SENT]).Should(Equal(numClients * (retries + 1)))
close(done)
}, float64(timeoutSeconds*(retries+1))+2)
}
Context("from a single client", func() {
Context("using 0 retries and a timeout of 1 second", func() {
ValidateResponse(0, 1)
})
// Context("using 1 retry and a timeout of 2 seconds", func() {
// ValidateRequestTimeout(1, 2)
// })
// Context("using 2 retries and a timeout of 1 second", func() {
// ValidateRequestTimeout(2, 1)
// })
})
})
})
}