Skip to content

Commit c2fa452

Browse files
Ahmet OeztuerkAhmet Oeztuerk
authored andcommitted
add perfdata field length rejection tests
1 parent b6c78aa commit c2fa452

1 file changed

Lines changed: 157 additions & 1 deletion

File tree

pkg/nagflux/collector/spoolfile/nagiosSpoolfileWorker_test.go

Lines changed: 157 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ const configFileContent = `
1414
1515
[Filter]
1616
SpoolFileLineTerms = check-host-alive
17+
18+
[NagiosSpoolfile]
19+
PerfdataLabelMaxLength = 32
20+
PerfdataUOMMaxLength = 16
21+
PerfdataNumericValuesMaxLength = 32
22+
PerfdataThresholdsMaxLength = 64
1723
`
1824

1925
func TestPerformanceDataParser_01(t *testing.T) {
@@ -427,12 +433,162 @@ func TestPerformanceDataParser_20(t *testing.T) {
427433
)
428434
}
429435

436+
func TestPerformanceDataParser_LongPerformanceLabel(t *testing.T) {
437+
t.Helper()
438+
439+
config.InitConfigFromString(configFileContent)
440+
441+
w := NewNagiosSpoolfileWorker(0, nil, nil, nil, 4096, collector.AllFilterable, PerfdataLabelMaxLengthDefault, PerfdataUOMMaxLengthDefault, PerfdataNumericValuesMaxLengthDefault, PerfdataThresholdsMaxLengthDefault)
442+
443+
splittedPerformanceData := helper.StringToMap(
444+
"DATATYPE::SERVICEPERFDATA TIMET::1441791000 HOSTNAME::xxx SERVICEDESC::range SERVICEPERFDATA::'i_am_a_very_long_performance_label_exceeding_the_default_limit_for_performance_labels_yeah'=35512320B;;;; SERVICECHECKCOMMAND::check_dummy SERVICESTATE::0 SERVICESTATETYPE::1",
445+
"\t", "::")
446+
447+
//nolint:prealloc // do not know the size of the iterable
448+
collectedPerfData := []PerformanceData{}
449+
for singlePerfdata := range w.PerformanceDataIterator(splittedPerformanceData) {
450+
collectedPerfData = append(collectedPerfData, *singlePerfdata)
451+
}
452+
453+
assert.Emptyf(t, collectedPerfData, "Item should not be taken into the performance data due to long label, splittedPerformanceData: %v", splittedPerformanceData)
454+
}
455+
456+
func TestPerformanceDataParser_LongValue(t *testing.T) {
457+
t.Helper()
458+
459+
config.InitConfigFromString(configFileContent)
460+
461+
w := NewNagiosSpoolfileWorker(0, nil, nil, nil, 4096, collector.AllFilterable, PerfdataLabelMaxLengthDefault, PerfdataUOMMaxLengthDefault, PerfdataNumericValuesMaxLengthDefault, PerfdataThresholdsMaxLengthDefault)
462+
463+
splittedPerformanceData := helper.StringToMap(
464+
"DATATYPE::SERVICEPERFDATA TIMET::1441791000 HOSTNAME::xxx SERVICEDESC::range SERVICEPERFDATA::'label'=123456789123456789123456789123456789;;;; SERVICECHECKCOMMAND::check_dummy SERVICESTATE::0 SERVICESTATETYPE::1",
465+
"\t", "::")
466+
467+
//nolint:prealloc // do not know the size of the iterable
468+
collectedPerfData := []PerformanceData{}
469+
for singlePerfdata := range w.PerformanceDataIterator(splittedPerformanceData) {
470+
collectedPerfData = append(collectedPerfData, *singlePerfdata)
471+
}
472+
473+
assert.Emptyf(t, collectedPerfData, "Item should not be taken into the performance data due to long length value, splittedPerformanceData: %v", splittedPerformanceData)
474+
}
475+
476+
func TestPerformanceDataParser_LongUOM(t *testing.T) {
477+
t.Helper()
478+
479+
config.InitConfigFromString(configFileContent)
480+
481+
w := NewNagiosSpoolfileWorker(0, nil, nil, nil, 4096, collector.AllFilterable, PerfdataLabelMaxLengthDefault, PerfdataUOMMaxLengthDefault, PerfdataNumericValuesMaxLengthDefault, PerfdataThresholdsMaxLengthDefault)
482+
483+
// If you use underscores in the uom, it gets split out and only the first part is taken.
484+
// UOM is deliberately written without underscores here
485+
splittedPerformanceData := helper.StringToMap(
486+
"DATATYPE::SERVICEPERFDATA TIMET::1441791000 HOSTNAME::xxx SERVICEDESC::range SERVICEPERFDATA::'label'=1iamaverylonguomthatshouldberejected;;;; SERVICECHECKCOMMAND::check_dummy SERVICESTATE::0 SERVICESTATETYPE::1",
487+
"\t", "::")
488+
489+
//nolint:prealloc // do not know the size of the iterable
490+
collectedPerfData := []PerformanceData{}
491+
for singlePerfdata := range w.PerformanceDataIterator(splittedPerformanceData) {
492+
collectedPerfData = append(collectedPerfData, *singlePerfdata)
493+
}
494+
495+
assert.Emptyf(t, collectedPerfData, "Item should not be taken into the performance data due to long length unit of measurement, splittedPerformanceData: %v", splittedPerformanceData)
496+
}
497+
498+
func TestPerformanceDataParser_LongWarnTrehsold(t *testing.T) {
499+
t.Helper()
500+
501+
config.InitConfigFromString(configFileContent)
502+
503+
w := NewNagiosSpoolfileWorker(0, nil, nil, nil, 4096, collector.AllFilterable, PerfdataLabelMaxLengthDefault, PerfdataUOMMaxLengthDefault, PerfdataNumericValuesMaxLengthDefault, PerfdataThresholdsMaxLengthDefault)
504+
505+
// If you use underscores in the uom, it gets split out and only the first part is taken.
506+
// UOM is deliberately written without underscores here
507+
splittedPerformanceData := helper.StringToMap(
508+
"DATATYPE::SERVICEPERFDATA TIMET::1441791000 HOSTNAME::xxx SERVICEDESC::range SERVICEPERFDATA::'label'=1;123456789123456789123456789123456789:123456789123456789123456789123456789;;; SERVICECHECKCOMMAND::check_dummy SERVICESTATE::0 SERVICESTATETYPE::1",
509+
"\t", "::")
510+
511+
//nolint:prealloc // do not know the size of the iterable
512+
collectedPerfData := []PerformanceData{}
513+
for singlePerfdata := range w.PerformanceDataIterator(splittedPerformanceData) {
514+
collectedPerfData = append(collectedPerfData, *singlePerfdata)
515+
}
516+
517+
assert.Emptyf(t, collectedPerfData, "Item should not be taken into the performance data due to long length of warning threshold, splittedPerformanceData: %v", splittedPerformanceData)
518+
}
519+
520+
func TestPerformanceDataParser_LongCritTrehsold(t *testing.T) {
521+
t.Helper()
522+
523+
config.InitConfigFromString(configFileContent)
524+
525+
w := NewNagiosSpoolfileWorker(0, nil, nil, nil, 4096, collector.AllFilterable, PerfdataLabelMaxLengthDefault, PerfdataUOMMaxLengthDefault, PerfdataNumericValuesMaxLengthDefault, PerfdataThresholdsMaxLengthDefault)
526+
527+
// If you use underscores in the uom, it gets split out and only the first part is taken.
528+
// UOM is deliberately written without underscores here
529+
splittedPerformanceData := helper.StringToMap(
530+
"DATATYPE::SERVICEPERFDATA TIMET::1441791000 HOSTNAME::xxx SERVICEDESC::range SERVICEPERFDATA::'label'=1;;123456789123456789123456789123456789:123456789123456789123456789123456789;; SERVICECHECKCOMMAND::check_dummy SERVICESTATE::0 SERVICESTATETYPE::1",
531+
"\t", "::")
532+
533+
//nolint:prealloc // do not know the size of the iterable
534+
collectedPerfData := []PerformanceData{}
535+
for singlePerfdata := range w.PerformanceDataIterator(splittedPerformanceData) {
536+
collectedPerfData = append(collectedPerfData, *singlePerfdata)
537+
}
538+
539+
assert.Emptyf(t, collectedPerfData, "Item should not be taken into the performance data due to long length of critical threshold, splittedPerformanceData: %v", splittedPerformanceData)
540+
}
541+
542+
func TestPerformanceDataParser_LongMinValue(t *testing.T) {
543+
t.Helper()
544+
545+
config.InitConfigFromString(configFileContent)
546+
547+
w := NewNagiosSpoolfileWorker(0, nil, nil, nil, 4096, collector.AllFilterable, PerfdataLabelMaxLengthDefault, PerfdataUOMMaxLengthDefault, PerfdataNumericValuesMaxLengthDefault, PerfdataThresholdsMaxLengthDefault)
548+
549+
// If you use underscores in the uom, it gets split out and only the first part is taken.
550+
// UOM is deliberately written without underscores here
551+
splittedPerformanceData := helper.StringToMap(
552+
"DATATYPE::SERVICEPERFDATA TIMET::1441791000 HOSTNAME::xxx SERVICEDESC::range SERVICEPERFDATA::'label'=1;;;123456789123456789123456789123456789; SERVICECHECKCOMMAND::check_dummy SERVICESTATE::0 SERVICESTATETYPE::1",
553+
"\t", "::")
554+
555+
//nolint:prealloc // do not know the size of the iterable
556+
collectedPerfData := []PerformanceData{}
557+
for singlePerfdata := range w.PerformanceDataIterator(splittedPerformanceData) {
558+
collectedPerfData = append(collectedPerfData, *singlePerfdata)
559+
}
560+
561+
assert.Emptyf(t, collectedPerfData, "Item should not be taken into the performance data due to long length of minimum value, splittedPerformanceData: %v", splittedPerformanceData)
562+
}
563+
564+
func TestPerformanceDataParser_LongMaxValue(t *testing.T) {
565+
t.Helper()
566+
567+
config.InitConfigFromString(configFileContent)
568+
569+
w := NewNagiosSpoolfileWorker(0, nil, nil, nil, 4096, collector.AllFilterable, PerfdataLabelMaxLengthDefault, PerfdataUOMMaxLengthDefault, PerfdataNumericValuesMaxLengthDefault, PerfdataThresholdsMaxLengthDefault)
570+
571+
// If you use underscores in the uom, it gets split out and only the first part is taken.
572+
// UOM is deliberately written without underscores here
573+
splittedPerformanceData := helper.StringToMap(
574+
"DATATYPE::SERVICEPERFDATA TIMET::1441791000 HOSTNAME::xxx SERVICEDESC::range SERVICEPERFDATA::'label'=1;;;;123456789123456789123456789123456789 SERVICECHECKCOMMAND::check_dummy SERVICESTATE::0 SERVICESTATETYPE::1",
575+
"\t", "::")
576+
577+
//nolint:prealloc // do not know the size of the iterable
578+
collectedPerfData := []PerformanceData{}
579+
for singlePerfdata := range w.PerformanceDataIterator(splittedPerformanceData) {
580+
collectedPerfData = append(collectedPerfData, *singlePerfdata)
581+
}
582+
583+
assert.Emptyf(t, collectedPerfData, "Item should not be taken into the performance data due to long length of maximum value, splittedPerformanceData: %v", splittedPerformanceData)
584+
}
585+
430586
func testPerformanceDataParser(t *testing.T, input string, expect []PerformanceData) {
431587
t.Helper()
432588

433589
config.InitConfigFromString(configFileContent)
434590

435-
w := NewNagiosSpoolfileWorker(0, nil, nil, nil, 4096, collector.AllFilterable)
591+
w := NewNagiosSpoolfileWorker(0, nil, nil, nil, 4096, collector.AllFilterable, PerfdataLabelMaxLengthDefault, PerfdataUOMMaxLengthDefault, PerfdataNumericValuesMaxLengthDefault, PerfdataThresholdsMaxLengthDefault)
436592

437593
splittedPerformanceData := helper.StringToMap(input, "\t", "::")
438594
//nolint:prealloc // do not know the size of the iterable

0 commit comments

Comments
 (0)