-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdecode_test.go
377 lines (350 loc) · 9.9 KB
/
decode_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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package tmsh
import (
"reflect"
"testing"
"github.com/k0kubun/pp"
)
func TestUnmarshalNode(t *testing.T) {
str := `ltm node dev-web01.example.com {
addr 192.0.2.1
cur-sessions 0
monitor-rule none
monitor-status unchecked
name dev-web01.example.com
serverside.bits-in 0
serverside.bits-out 0
serverside.cur-conns 0
serverside.max-conns 0
serverside.pkts-in 0
serverside.pkts-out 0
serverside.tot-conns 0
session-status enabled
status.availability-state unknown
status.enabled-state enabled
status.status-reason Node address does not have service checking enabled
tot-requests 0
}`
var node Node
if err := Unmarshal(str, &node); err != nil {
t.Errorf("got %v", err)
}
expect := Node{
Addr: "192.0.2.1",
Name: "dev-web01.example.com",
MonitorRule: "none",
MonitorStatus: "unchecked",
EnabledState: "enabled",
}
if !reflect.DeepEqual(node, expect) {
t.Errorf("got :" + pp.Sprint(node))
t.Errorf("want :" + pp.Sprint(expect))
}
}
type NodeExt struct {
Node
AvailabilityState string `ltm:"status.availability-state"`
StatusReason string `ltm:"status.status-reason"`
TotRequests int `ltm:"tot-requests"`
}
func TestUnmarshalNodeExt(t *testing.T) {
str := `ltm node dev-web01.example.com {
addr 192.0.2.1
cur-sessions 0
monitor-rule none
monitor-status unchecked
name dev-web01.example.com
serverside.bits-in 0
serverside.bits-out 0
serverside.cur-conns 0
serverside.max-conns 0
serverside.pkts-in 0
serverside.pkts-out 0
serverside.tot-conns 0
session-status enabled
status.availability-state unknown
status.enabled-state enabled
status.status-reason Node address does not have service checking enabled
tot-requests 0
}`
var nodeExt NodeExt
if err := Unmarshal(str, &nodeExt); err != nil {
t.Errorf("got %v", err)
}
expect := NodeExt{
Node: Node{
Addr: "192.0.2.1",
Name: "dev-web01.example.com",
MonitorRule: "none",
MonitorStatus: "unchecked",
EnabledState: "enabled",
},
AvailabilityState: "unknown",
StatusReason: "Node address does not have service checking enabled",
TotRequests: 0,
}
if !reflect.DeepEqual(nodeExt, expect) {
t.Errorf("got :" + pp.Sprint(nodeExt))
t.Errorf("want :" + pp.Sprint(expect))
}
}
func TestUnmarshalPool(t *testing.T) {
//# show ltm pool api.example.com_8080 members field-fmt
str := `ltm pool api.example.com_8080 {
active-member-cnt 2
connq-all.age-edm 0
connq-all.age-ema 0
connq-all.age-head 0
connq-all.age-max 0
connq-all.depth 0
connq-all.serviced 0
connq.age-edm 0
connq.age-ema 0
connq.age-head 0
connq.age-max 0
connq.depth 0
connq.serviced 0
cur-sessions 0
members {
api01.example.com:8080 {
addr 192.0.2.1
connq.age-edm 0
connq.age-ema 0
connq.age-head 0
connq.age-max 0
connq.depth 0
connq.serviced 0
cur-sessions 0
monitor-rule /Common/tcp (pool monitor)
monitor-status up
node-name api01.example.com
pool-name api.example.com_8080
port 8080
serverside.bits-in 36.2K
serverside.bits-out 87.9K
serverside.cur-conns 0
serverside.max-conns 3
serverside.pkts-in 20
serverside.pkts-out 20
serverside.tot-conns 3
session-status enabled
status.availability-state available
status.enabled-state enabled
status.status-reason Pool member is available
tot-requests 0
}
api02.example.com:8080 {
addr 192.0.2.2
connq.age-edm 0
connq.age-ema 0
connq.age-head 0
connq.age-max 0
connq.depth 0
connq.serviced 0
cur-sessions 0
monitor-rule none
monitor-status unchecked
node-name api02.example.com
pool-name api.example.com_8080
port 8080
serverside.bits-in 7.8M
serverside.bits-out 44.5M
serverside.cur-conns 0
serverside.max-conns 42
serverside.pkts-in 9.0K
serverside.pkts-out 7.8K
serverside.tot-conns 1.4K
session-status user-disabled
status.availability-state unknown
status.enabled-state disabled
status.status-reason Pool member does not have service checking enabled
tot-requests 0
}
}
min-active-members 0
monitor-rule /Common/tcp
name api.example.com_8080
serverside.bits-in 7.8M
serverside.bits-out 44.5M
serverside.cur-conns 0
serverside.max-conns 45
serverside.pkts-in 9.0K
serverside.pkts-out 7.8K
serverside.tot-conns 1.4K
status.availability-state available
status.enabled-state enabled
status.status-reason The pool is available
tot-requests 0
}`
var pool Pool
if err := Unmarshal(str, &pool); err != nil {
t.Errorf("got %v", err)
}
expect := Pool{
ActiveMemberCount: 2,
MonitorRule: "/Common/tcp",
Name: "api.example.com_8080",
AvailabilityState: "available",
EnabledState: "enabled",
StatusReason: "The pool is available",
PoolMembers: []PoolMember{
PoolMember{
Name: "api01.example.com",
Addr: "192.0.2.1",
Port: 8080,
MonitorRule: "/Common/tcp (pool monitor)",
MonitorStatus: "up",
EnabledState: "enabled",
AvailabilityState: "available",
StatusReason: "Pool member is available",
},
PoolMember{
Name: "api02.example.com",
Addr: "192.0.2.2",
Port: 8080,
MonitorRule: "none",
MonitorStatus: "unchecked",
EnabledState: "disabled",
AvailabilityState: "unknown",
StatusReason: "Pool member does not have service checking enabled",
},
},
}
if !reflect.DeepEqual(pool, expect) {
t.Errorf("got :" + pp.Sprint(pool))
t.Errorf("want :" + pp.Sprint(expect))
}
}
// When a pool is newly created, the TMSH does not return a status.status-reason value
// See: https://github.com/yukirii/go-tmsh/issues/17
func TestUnmarshalEmptyStatusReasonPool(t *testing.T) {
//# show ltm pool api.example.com_8080 members field-fmt
str := `ltm pool api.example.com_8080 {
active-member-cnt 0
connq-all.age-edm 0
connq-all.age-ema 0
connq-all.age-head 0
connq-all.age-max 0
connq-all.depth 0
connq-all.serviced 0
connq.age-edm 0
connq.age-ema 0
connq.age-head 0
connq.age-max 0
connq.depth 0
connq.serviced 0
cur-sessions 0
min-active-members 0
monitor-rule none
name api.example.com_8080
serverside.bits-in 0
serverside.bits-out 0
serverside.cur-conns 0
serverside.max-conns 0
serverside.pkts-in 0
serverside.pkts-out 0
serverside.tot-conns 0
status.availability-state offline
status.enabled-state enabled
status.status-reason
tot-requests 0
}`
var pool Pool
if err := Unmarshal(str, &pool); err != nil {
t.Errorf("got %v", err)
}
expect := Pool{
ActiveMemberCount: 0,
Name: "api.example.com_8080",
MonitorRule: "none",
AvailabilityState: "offline",
EnabledState: "enabled",
}
if !reflect.DeepEqual(pool, expect) {
t.Errorf("got :" + pp.Sprint(pool))
t.Errorf("want :" + pp.Sprint(expect))
}
}
func TestUnmarshalVirtualServer(t *testing.T) {
//# list ltm virtual api.example.com_443
str := `ltm virtual api.example.com_443 {
destination 203.0.113.1:https
ip-protocol tcp
mask 255.255.255.255
partition partition1
pool api.example.com_443
profiles {
/Common/tcp {
context all
}
wildcard.example.com {
context clientside
}
}
source 0.0.0.0/0
vs-index 1234
}`
var vs VirtualServer
if err := Unmarshal(str, &vs); err != nil {
t.Errorf("got %v", err)
}
expect := VirtualServer{
Name: "api.example.com_443",
Destination: "203.0.113.1:https",
IpProtocol: "tcp",
Mask: "255.255.255.255",
Partition: "partition1",
Pool: "api.example.com_443",
Profiles: map[string]map[string]string{
"/Common/tcp": map[string]string{"context": "all"},
"wildcard.example.com": map[string]string{"context": "clientside"},
},
}
if !reflect.DeepEqual(vs, expect) {
t.Errorf("got :" + pp.Sprint(vs))
t.Errorf("want :" + pp.Sprint(expect))
}
}
func TestUnmarshalClientSSLProfile(t *testing.T) {
// list ltm profile client-ssl wildcard.example.com_20191031-1
str := `ltm profile client-ssl wildcard.example.com_20191031-1 {
app-service none
cert wildcard.example.com_20191031-1.crt
cert-key-chain {
wildcard.example.com_20191031-1 {
cert wildcard.example.com_20191031-1.crt
chain /Common/PUBCAG3_20191031-1.crt
key wildcard.example.com_20191031-1.key
}
}
chain /Common/PUBCAG3_20191031-1.crt
defaults-from /Common/clientssl_v2
inherit-certkeychain false
key wildcard.example.com_20191031-1.key
options { dont-insert-empty-fragments }
passphrase "****"
}`
var p ClientSSLProfile
if err := Unmarshal(str, &p); err != nil {
t.Errorf("got %v", err)
}
expect := ClientSSLProfile{
Name: "wildcard.example.com_20191031-1",
Component: "profile-client-ssl",
Cert: "wildcard.example.com_20191031-1.crt",
CertKeyChain: map[string]map[string]string{
"wildcard.example.com_20191031-1": map[string]string{
"cert": "wildcard.example.com_20191031-1.crt",
"chain": "/Common/PUBCAG3_20191031-1.crt",
"key": "wildcard.example.com_20191031-1.key",
},
},
Chain: "/Common/PUBCAG3_20191031-1.crt",
DefaultsFrom: "/Common/clientssl_v2",
InheritCertkeychain: false,
Key: "wildcard.example.com_20191031-1.key",
}
if !reflect.DeepEqual(p, expect) {
t.Errorf("got :" + pp.Sprint(p))
t.Errorf("want :" + pp.Sprint(expect))
}
}