@@ -17,6 +17,7 @@ import (
17
17
"errors"
18
18
"reflect"
19
19
"regexp"
20
+ "strings"
20
21
"testing"
21
22
22
23
kingpin "github.com/alecthomas/kingpin/v2"
@@ -60,7 +61,7 @@ func TestPduToSample(t *testing.T) {
60
61
},
61
62
oidToPdu : make (map [string ]gosnmp.SnmpPDU ),
62
63
expectedMetrics : []string {
63
- `Desc{fqName: "TestMetricNameExtension", help: "HelpText (regex extracted)", constLabels: {}, variableLabels: []} gauge:< value:5 > ` ,
64
+ `Desc{fqName: "TestMetricNameExtension", help: "HelpText (regex extracted)", constLabels: {}, variableLabels: []} gauge:{ value:5} ` ,
64
65
},
65
66
},
66
67
{
@@ -131,7 +132,7 @@ func TestPduToSample(t *testing.T) {
131
132
},
132
133
},
133
134
expectedMetrics : []string {
134
- `Desc{fqName: "TestMetricNameStatus", help: "HelpText (regex extracted)", constLabels: {}, variableLabels: []} gauge:< value:5 > ` ,
135
+ `Desc{fqName: "TestMetricNameStatus", help: "HelpText (regex extracted)", constLabels: {}, variableLabels: []} gauge:{ value:5} ` ,
135
136
},
136
137
},
137
138
{
@@ -193,9 +194,9 @@ func TestPduToSample(t *testing.T) {
193
194
},
194
195
oidToPdu : make (map [string ]gosnmp.SnmpPDU ),
195
196
expectedMetrics : []string {
196
- `Desc{fqName: "TestMetricNameExtension", help: "HelpText (regex extracted)", constLabels: {}, variableLabels: []} gauge:< value:5 > ` ,
197
- `Desc{fqName: "TestMetricNameMultipleRegexes", help: "HelpText (regex extracted)", constLabels: {}, variableLabels: []} gauge:< value:999 > ` ,
198
- `Desc{fqName: "TestMetricNameTemplate", help: "HelpText (regex extracted)", constLabels: {}, variableLabels: []} gauge:< value:4.42 > ` ,
197
+ `Desc{fqName: "TestMetricNameExtension", help: "HelpText (regex extracted)", constLabels: {}, variableLabels: []} gauge:{ value:5} ` ,
198
+ `Desc{fqName: "TestMetricNameMultipleRegexes", help: "HelpText (regex extracted)", constLabels: {}, variableLabels: []} gauge:{ value:999} ` ,
199
+ `Desc{fqName: "TestMetricNameTemplate", help: "HelpText (regex extracted)", constLabels: {}, variableLabels: []} gauge:{ value:4.42} ` ,
199
200
},
200
201
},
201
202
{
@@ -212,7 +213,7 @@ func TestPduToSample(t *testing.T) {
212
213
Help : "Help string" ,
213
214
},
214
215
oidToPdu : make (map [string ]gosnmp.SnmpPDU ),
215
- expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: []} counter:< value:2 > ` },
216
+ expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: []} counter:{ value:2} ` },
216
217
},
217
218
{
218
219
pdu : & gosnmp.SnmpPDU {
@@ -228,7 +229,7 @@ func TestPduToSample(t *testing.T) {
228
229
Help : "Help string" ,
229
230
},
230
231
oidToPdu : make (map [string ]gosnmp.SnmpPDU ),
231
- expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: []} gauge:< value:2 > ` },
232
+ expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: []} gauge:{ value:2} ` },
232
233
},
233
234
{
234
235
pdu : & gosnmp.SnmpPDU {
@@ -243,7 +244,7 @@ func TestPduToSample(t *testing.T) {
243
244
Help : "Help string" ,
244
245
},
245
246
oidToPdu : make (map [string ]gosnmp.SnmpPDU ),
246
- expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"-2" > gauge:< value:1 > ` },
247
+ expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"-2"} gauge:{ value:1} ` },
247
248
},
248
249
{
249
250
pdu : & gosnmp.SnmpPDU {
@@ -259,7 +260,7 @@ func TestPduToSample(t *testing.T) {
259
260
Help : "Help string" ,
260
261
},
261
262
oidToPdu : make (map [string ]gosnmp.SnmpPDU ),
262
- expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: []} gauge:< value:3 > ` },
263
+ expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: []} gauge:{ value:3} ` },
263
264
},
264
265
{
265
266
pdu : & gosnmp.SnmpPDU {
@@ -275,7 +276,7 @@ func TestPduToSample(t *testing.T) {
275
276
Help : "Help string" ,
276
277
},
277
278
oidToPdu : make (map [string ]gosnmp.SnmpPDU ),
278
- expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: []} gauge:< value:3 > ` },
279
+ expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: []} gauge:{ value:3} ` },
279
280
},
280
281
{
281
282
pdu : & gosnmp.SnmpPDU {
@@ -292,7 +293,7 @@ func TestPduToSample(t *testing.T) {
292
293
Indexes : []* config.Index {{Labelname : "foo" , Type : "DisplayString" }},
293
294
},
294
295
oidToPdu : make (map [string ]gosnmp.SnmpPDU ),
295
- expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: [foo]} label:< name:"foo" value:"AA" > gauge:< value:3 > ` },
296
+ expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: [{ foo <nil>} ]} label:{ name:"foo" value:"AA"} gauge:{ value:3} ` },
296
297
},
297
298
{
298
299
pdu : & gosnmp.SnmpPDU {
@@ -344,7 +345,7 @@ func TestPduToSample(t *testing.T) {
344
345
Help : "Help string" ,
345
346
},
346
347
oidToPdu : map [string ]gosnmp.SnmpPDU {"1.41.2" : gosnmp.SnmpPDU {Value : 1 }},
347
- expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"4.5.6.7" > gauge:< value:1 > ` },
348
+ expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"4.5.6.7"} gauge:{ value:1} ` },
348
349
},
349
350
{
350
351
pdu : & gosnmp.SnmpPDU {
@@ -359,7 +360,7 @@ func TestPduToSample(t *testing.T) {
359
360
Help : "Help string" ,
360
361
},
361
362
oidToPdu : map [string ]gosnmp.SnmpPDU {"1.41.2" : gosnmp.SnmpPDU {Value : 1 }},
362
- expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"4.5.6.7" > gauge:< value:1 > ` },
363
+ expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"4.5.6.7"} gauge:{ value:1} ` },
363
364
},
364
365
{
365
366
pdu : & gosnmp.SnmpPDU {
@@ -374,7 +375,7 @@ func TestPduToSample(t *testing.T) {
374
375
Help : "Help string" ,
375
376
},
376
377
oidToPdu : map [string ]gosnmp.SnmpPDU {"1.41.2" : gosnmp.SnmpPDU {Value : 2 }},
377
- expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"0405:0607:0809:0A0B:0C0D:0E0F:1011:1213" > gauge:< value:1 > ` },
378
+ expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"0405:0607:0809:0A0B:0C0D:0E0F:1011:1213"} gauge:{ value:1} ` },
378
379
},
379
380
{
380
381
pdu : & gosnmp.SnmpPDU {
@@ -389,7 +390,7 @@ func TestPduToSample(t *testing.T) {
389
390
Help : "Help string" ,
390
391
},
391
392
oidToPdu : map [string ]gosnmp.SnmpPDU {"1.41.2" : gosnmp.SnmpPDU {Value : 3 }},
392
- expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"0x0405060708" > gauge:< value:1 > ` },
393
+ expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"0x0405060708"} gauge:{ value:1} ` },
393
394
},
394
395
{
395
396
pdu : & gosnmp.SnmpPDU {
@@ -404,7 +405,7 @@ func TestPduToSample(t *testing.T) {
404
405
Help : "Help string" ,
405
406
},
406
407
oidToPdu : make (map [string ]gosnmp.SnmpPDU ),
407
- expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"0x04050607" > gauge:< value:1 > ` },
408
+ expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"0x04050607"} gauge:{ value:1} ` },
408
409
},
409
410
{
410
411
pdu : & gosnmp.SnmpPDU {
@@ -419,7 +420,7 @@ func TestPduToSample(t *testing.T) {
419
420
Help : "Help string" ,
420
421
},
421
422
oidToPdu : map [string ]gosnmp.SnmpPDU {"1.41.2" : gosnmp.SnmpPDU {Value : 3 }},
422
- expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"04:05:06:07:08:09" > gauge:< value:1 > ` },
423
+ expectedMetrics : []string {`Desc{fqName: "test_metric", help: "Help string", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"04:05:06:07:08:09"} gauge:{ value:1} ` },
423
424
},
424
425
{
425
426
pdu : & gosnmp.SnmpPDU {
@@ -434,7 +435,7 @@ func TestPduToSample(t *testing.T) {
434
435
Help : "Help string" ,
435
436
EnumValues : map [int ]string {0 : "foo" , 1 : "bar" , 2 : "baz" },
436
437
},
437
- expectedMetrics : []string {`Desc{fqName: "test_metric_info", help: "Help string (EnumAsInfo)", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"baz" > gauge:< value:1 > ` },
438
+ expectedMetrics : []string {`Desc{fqName: "test_metric_info", help: "Help string (EnumAsInfo)", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"baz"} gauge:{ value:1} ` },
438
439
},
439
440
{
440
441
pdu : & gosnmp.SnmpPDU {
@@ -449,7 +450,7 @@ func TestPduToSample(t *testing.T) {
449
450
Help : "Help string" ,
450
451
EnumValues : map [int ]string {0 : "foo" , 1 : "bar" , 2 : "baz" },
451
452
},
452
- expectedMetrics : []string {`Desc{fqName: "test_metric_info", help: "Help string (EnumAsInfo)", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"3" > gauge:< value:1 > ` },
453
+ expectedMetrics : []string {`Desc{fqName: "test_metric_info", help: "Help string (EnumAsInfo)", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"3"} gauge:{ value:1} ` },
453
454
},
454
455
{
455
456
pdu : & gosnmp.SnmpPDU {
@@ -465,9 +466,9 @@ func TestPduToSample(t *testing.T) {
465
466
EnumValues : map [int ]string {0 : "foo" , 1 : "bar" , 2 : "baz" },
466
467
},
467
468
expectedMetrics : []string {
468
- `Desc{fqName: "test_metric", help: "Help string (EnumAsStateSet)", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"foo" > gauge:< value:0 > ` ,
469
- `Desc{fqName: "test_metric", help: "Help string (EnumAsStateSet)", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"bar" > gauge:< value:0 > ` ,
470
- `Desc{fqName: "test_metric", help: "Help string (EnumAsStateSet)", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"baz" > gauge:< value:1 > ` ,
469
+ `Desc{fqName: "test_metric", help: "Help string (EnumAsStateSet)", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"foo"} gauge:{ value:0} ` ,
470
+ `Desc{fqName: "test_metric", help: "Help string (EnumAsStateSet)", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"bar"} gauge:{ value:0} ` ,
471
+ `Desc{fqName: "test_metric", help: "Help string (EnumAsStateSet)", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"baz"} gauge:{ value:1} ` ,
471
472
},
472
473
},
473
474
{
@@ -484,10 +485,10 @@ func TestPduToSample(t *testing.T) {
484
485
EnumValues : map [int ]string {0 : "foo" , 1 : "bar" , 2 : "baz" },
485
486
},
486
487
expectedMetrics : []string {
487
- `Desc{fqName: "test_metric", help: "Help string (EnumAsStateSet)", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"foo" > gauge:< value:0 > ` ,
488
- `Desc{fqName: "test_metric", help: "Help string (EnumAsStateSet)", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"bar" > gauge:< value:0 > ` ,
489
- `Desc{fqName: "test_metric", help: "Help string (EnumAsStateSet)", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"baz" > gauge:< value:0 > ` ,
490
- `Desc{fqName: "test_metric", help: "Help string (EnumAsStateSet)", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"3" > gauge:< value:1 > ` ,
488
+ `Desc{fqName: "test_metric", help: "Help string (EnumAsStateSet)", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"foo"} gauge:{ value:0} ` ,
489
+ `Desc{fqName: "test_metric", help: "Help string (EnumAsStateSet)", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"bar"} gauge:{ value:0} ` ,
490
+ `Desc{fqName: "test_metric", help: "Help string (EnumAsStateSet)", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"baz"} gauge:{ value:0} ` ,
491
+ `Desc{fqName: "test_metric", help: "Help string (EnumAsStateSet)", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"3"} gauge:{ value:1} ` ,
491
492
},
492
493
},
493
494
{
@@ -504,14 +505,14 @@ func TestPduToSample(t *testing.T) {
504
505
EnumValues : map [int ]string {0 : "foo" , 1 : "bar" , 2 : "baz" , 8 : "byte2msb" , 15 : "byte2lsb" , 16 : "byte3msb" , 23 : "byte3lsb" , 24 : "missing" },
505
506
},
506
507
expectedMetrics : []string {
507
- `Desc{fqName: "test_metric", help: "Help string (Bits)", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"foo" > gauge:< value:1 > ` ,
508
- `Desc{fqName: "test_metric", help: "Help string (Bits)", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"bar" > gauge:< value:1 > ` ,
509
- `Desc{fqName: "test_metric", help: "Help string (Bits)", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"baz" > gauge:< value:0 > ` ,
510
- `Desc{fqName: "test_metric", help: "Help string (Bits)", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"byte2msb" > gauge:< value:1 > ` ,
511
- `Desc{fqName: "test_metric", help: "Help string (Bits)", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"byte2lsb" > gauge:< value:0 > ` ,
512
- `Desc{fqName: "test_metric", help: "Help string (Bits)", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"byte3msb" > gauge:< value:0 > ` ,
513
- `Desc{fqName: "test_metric", help: "Help string (Bits)", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"byte3lsb" > gauge:< value:1 > ` ,
514
- `Desc{fqName: "test_metric", help: "Help string (Bits)", constLabels: {}, variableLabels: [test_metric]} label:< name:"test_metric" value:"missing" > gauge:< value:0 > ` ,
508
+ `Desc{fqName: "test_metric", help: "Help string (Bits)", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"foo"} gauge:{ value:1} ` ,
509
+ `Desc{fqName: "test_metric", help: "Help string (Bits)", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"bar"} gauge:{ value:1} ` ,
510
+ `Desc{fqName: "test_metric", help: "Help string (Bits)", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"baz"} gauge:{ value:0} ` ,
511
+ `Desc{fqName: "test_metric", help: "Help string (Bits)", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"byte2msb"} gauge:{ value:1} ` ,
512
+ `Desc{fqName: "test_metric", help: "Help string (Bits)", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"byte2lsb"} gauge:{ value:0} ` ,
513
+ `Desc{fqName: "test_metric", help: "Help string (Bits)", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"byte3msb"} gauge:{ value:0} ` ,
514
+ `Desc{fqName: "test_metric", help: "Help string (Bits)", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"byte3lsb"} gauge:{ value:1} ` ,
515
+ `Desc{fqName: "test_metric", help: "Help string (Bits)", constLabels: {}, variableLabels: [{ test_metric <nil>} ]} label:{ name:"test_metric" value:"missing"} gauge:{ value:0} ` ,
515
516
},
516
517
},
517
518
}
@@ -534,15 +535,15 @@ func TestPduToSample(t *testing.T) {
534
535
t .Fatalf ("Error writing metric: %v" , err )
535
536
}
536
537
}
537
- got := m .Desc ().String () + " " + metric .String ()
538
+ got := strings . ReplaceAll ( m .Desc ().String ()+ " " + metric .String (), " " , " " )
538
539
if _ , ok := expected [got ]; ! ok {
539
- t .Errorf ("Unexpected metric: got %v" , got )
540
+ t .Errorf ("Got metric: %v" , got )
540
541
} else {
541
542
delete (expected , got )
542
543
}
543
544
}
544
545
for e := range expected {
545
- t .Errorf ("Expected metric %v, but was not returned." , e )
546
+ t .Errorf ("Expected metric: %v, but was not returned." , e )
546
547
}
547
548
if ! errHappened && c .shouldErr {
548
549
t .Fatalf ("Was expecting error, but none returned." )
0 commit comments