-
Notifications
You must be signed in to change notification settings - Fork 3
/
exported_test.go
1679 lines (1441 loc) · 64.4 KB
/
exported_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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 Adam Chalkley
//
// https://github.com/atc0005/go-nagios
//
// Licensed under the MIT License. See LICENSE file in the project root for
// full license information.
// Package nagios_test provides test coverage for exported package
// functionality.
//
//nolint:dupl,gocognit // ignore "lines are duplicate of" and function complexity
package nagios_test
import (
_ "embed"
"encoding/ascii85"
"errors"
"fmt"
"strings"
"testing"
"github.com/atc0005/go-nagios"
"github.com/google/go-cmp/cmp"
)
const (
customEncodingDelimiterLeft string = "CUSTOM_ENCODING_DELIMITER_LEFT"
customEncodingDelimiterRight string = "CUSTOM_ENCODING_DELIMITER_RIGHT"
customSectionHeader string = "PAYLOAD FOR LATER API USE"
)
// The specific format used by the test input files is VERY specific; trailing
// space + newline patterns are intentional. Because "format on save" editor
// functionality easily breaks this input it is stored in separate files to
// reduce test breakage due to editors "helping".
var (
//go:embed testdata/plugin-output-datastore-0001.txt
pluginOutputDatastore0001 string
//go:embed testdata/plugin-output-gh103-multi-line-with-perf-data.txt
pluginOutputGH103MultiLineWithPerfData string
//go:embed testdata/plugin-output-gh103-one-line-with-perf-data.txt
pluginOutputGH103OneLineWithPerfData string
//go:embed testdata/plugin-output-multiline-with-optional-perf-data-included.txt
pluginOutputMultiLineWithOptionalPerfDataIncluded string
//go:embed testdata/plugin-output-one-line-with-optional-perf-data-included.txt
pluginOutputOneLineWithOptionalPerfDataIncluded string
//go:embed testdata/payload/small_json_payload_unencoded.txt
smallJSONPayloadUnencoded string
//go:embed testdata/payload/small_json_payload_encoded_with_default_delimiters.txt
smallJSONPayloadEncodedWithDefaultDelimiters string
//go:embed testdata/payload/small_json_payload_encoded_with_custom_delimiters.txt
smallJSONPayloadEncodedWithCustomDelimiters string
//go:embed testdata/payload/small_json_payload_encoded_without_delimiters.txt
smallJSONPayloadEncodedWithNoDelimiters string
// Earlier prototyping found that the stream encoding/decoding process
// did not retain exclamation marks. I've not dug deep enough to
// determine the root cause, but have observed that using the Decode
// and Encode functions work reliably. Because a later refactoring
// might switch back to using stream processing we explicitly test
// using an exclamation point to guard against future breakage.
//
//go:embed testdata/payload/small_plaintext_payload_unencoded.txt
smallPlaintextPayloadUnencoded string
// Earlier prototyping found that the stream encoding/decoding process
// did not retain exclamation marks. I've not dug deep enough to
// determine the root cause, but have observed that using the Decode
// and Encode functions work reliably. Because a later refactoring
// might switch back to using stream processing we explicitly test
// using an exclamation point to guard against future breakage.
//
//go:embed testdata/payload/small_plaintext_payload_encoded_with_default_delimiters.txt
smallPlaintextPayloadEncodedWithDefaultDelimiters string
// Earlier prototyping found that the stream encoding/decoding process
// did not retain exclamation marks. I've not dug deep enough to
// determine the root cause, but have observed that using the Decode
// and Encode functions work reliably. Because a later refactoring
// might switch back to using stream processing we explicitly test
// using an exclamation point to guard against future breakage.
//
//go:embed testdata/payload/small_plaintext_payload_encoded_with_custom_delimiters.txt
smallPlaintextPayloadEncodedWithCustomDelimiters string
// Earlier prototyping found that the stream encoding/decoding process
// did not retain exclamation marks. I've not dug deep enough to
// determine the root cause, but have observed that using the Decode
// and Encode functions work reliably. Because a later refactoring
// might switch back to using stream processing we explicitly test
// using an exclamation point to guard against future breakage.
//
//go:embed testdata/payload/small_plaintext_payload_encoded_without_delimiters.txt
smallPlaintextPayloadEncodedWithNoDelimiters string
//go:embed testdata/payload/large_payload_encoded_with_default_delimiters.txt
largePayloadEncodedWithDefaultDelimiters string
//go:embed testdata/payload/large_payload_encoded_with_custom_delimiters.txt
largePayloadEncodedWithCustomDelimiters string
//go:embed testdata/payload/large_payload_encoded_without_delimiters.txt
largePayloadEncodedWithNoDelimiters string
//go:embed testdata/payload/large_payload_unencoded.txt
largePayloadUnencoded string
//go:embed testdata/payload/plugin-output-gh251-custom-section-header-and-large-encoded-payload-with-custom-delimiters.txt
pluginOutputCustomSectionHeadersAndLargeEncodedPayloadWithCustomDelimiters string
//go:embed testdata/payload/plugin-output-gh251-custom-section-header-and-large-encoded-payload-with-default-delimiters.txt
pluginOutputCustomSectionHeadersAndLargeEncodedPayloadWithDefaultDelimiters string
//go:embed testdata/payload/plugin-output-gh251-custom-section-header-and-large-encoded-payload-with-no-delimiters.txt
pluginOutputCustomSectionHeadersAndLargeEncodedPayloadWithNoDelimiters string
//go:embed testdata/payload/plugin-output-gh251-custom-section-header-and-small-encoded-json-payload-with-custom-delimiters.txt
pluginOutputCustomSectionHeadersAndSmallEncodedJSONPayloadWithCustomDelimiters string
//go:embed testdata/payload/plugin-output-gh251-custom-section-header-and-small-encoded-json-payload-with-default-delimiters.txt
pluginOutputCustomSectionHeadersAndSmallEncodedJSONPayloadWithDefaultDelimiters string
//go:embed testdata/payload/plugin-output-gh251-custom-section-header-and-small-encoded-json-payload-with-no-delimiters.txt
pluginOutputCustomSectionHeadersAndSmallEncodedJSONPayloadWithNoDelimiters string
//go:embed testdata/payload/plugin-output-gh251-custom-section-header-and-small-encoded-plaintext-payload-with-custom-delimiters.txt
pluginOutputCustomSectionHeadersAndSmallEncodedPlaintextPayloadWithCustomDelimiters string
//go:embed testdata/payload/plugin-output-gh251-custom-section-header-and-small-encoded-plaintext-payload-with-default-delimiters.txt
pluginOutputCustomSectionHeadersAndSmallEncodedPlaintextPayloadWithDefaultDelimiters string
//go:embed testdata/payload/plugin-output-gh251-custom-section-header-and-small-encoded-plaintext-payload-with-no-delimiters.txt
pluginOutputCustomSectionHeadersAndSmallEncodedPlaintextPayloadWithNoDelimiters string
//go:embed testdata/payload/plugin-output-gh251-default-section-header-and-large-encoded-payload-with-custom-delimiters.txt
pluginOutputDefaultSectionHeadersAndLargeEncodedPayloadWithCustomDelimiters string
//go:embed testdata/payload/plugin-output-gh251-default-section-header-and-large-encoded-payload-with-default-delimiters.txt
pluginOutputDefaultSectionHeadersAndLargeEncodedPayloadWithDefaultDelimiters string
//go:embed testdata/payload/plugin-output-gh251-default-section-header-and-large-encoded-payload-with-no-delimiters.txt
pluginOutputDefaultSectionHeadersAndLargeEncodedPayloadWithNoDelimiters string
//go:embed testdata/payload/plugin-output-gh251-default-section-header-and-small-encoded-json-payload-with-custom-delimiters.txt
pluginOutputDefaultSectionHeadersAndSmallEncodedJSONPayloadWithCustomDelimiters string
//go:embed testdata/payload/plugin-output-gh251-default-section-header-and-small-encoded-json-payload-with-default-delimiters.txt
pluginOutputDefaultSectionHeadersAndSmallEncodedJSONPayloadWithDefaultDelimiters string
//go:embed testdata/payload/plugin-output-gh251-default-section-header-and-small-encoded-json-payload-with-no-delimiters.txt
pluginOutputDefaultSectionHeadersAndSmallEncodedJSONPayloadWithNoDelimiters string
//go:embed testdata/payload/plugin-output-gh251-default-section-header-and-small-encoded-plaintext-payload-with-custom-delimiters.txt
pluginOutputDefaultSectionHeadersAndSmallEncodedPlaintextPayloadWithCustomDelimiters string
//go:embed testdata/payload/plugin-output-gh251-default-section-header-and-small-encoded-plaintext-payload-with-default-delimiters.txt
pluginOutputDefaultSectionHeadersAndSmallEncodedPlaintextPayloadWithDefaultDelimiters string
//go:embed testdata/payload/plugin-output-gh251-default-section-header-and-small-encoded-plaintext-payload-with-no-delimiters.txt
pluginOutputDefaultSectionHeadersAndSmallEncodedPlaintextPayloadWithNoDelimiters string
)
// TestPluginOutputIsValid configures an Plugin value as client code would
// and then asserts that the generated output matches manually crafted test
// data.
func TestPluginOutputIsValid(t *testing.T) {
t.Parallel()
want := pluginOutputDatastore0001
// Setup Plugin value manually. This approach does not provide the
// default time metric that would be provided when using the Plugin
// constructor.
plugin := nagios.Plugin{
LastError: nil,
ExitStatusCode: nagios.StateOKExitCode,
}
var outputBuffer strings.Builder
plugin.SetOutputTarget(&outputBuffer)
// os.Exit calls break tests
plugin.SkipOSExit()
pluginOutputWithLongServiceOutputSetup(t, &plugin)
// Process exit state, emit output to our output buffer.
plugin.ReturnCheckResults()
// Retrieve the output buffer content so that we can compare actual output
// against our expected output to assert we have a 1:1 match.
got := outputBuffer.String()
// if want != got {
// t.Error(cmp.Diff(want, got))
// }
if d := cmp.Diff(want, got); d != "" {
t.Errorf("(-want, +got)\n:%s", d)
}
}
// TestDefaultPerformanceDataIsOnSameLineAsServiceOutput asserts that
// performance data is emitted on the same line as the Service Output (aka,
// "one-line summary") if Long Service Output is empty. We use default
// performance data metrics for this test.
//
// See also:
//
// - https://github.com/atc0005/go-nagios/issues/103
func TestDefaultPerformanceDataIsOnSameLineAsServiceOutput(t *testing.T) {
t.Parallel()
want := pluginOutputGH103OneLineWithPerfData
// Setup Plugin value manually. This approach does not provide the
// default time metric that would be provided when using the Plugin
// constructor.
plugin := nagios.Plugin{
LastError: nil,
ExitStatusCode: nagios.StateOKExitCode,
}
var outputBuffer strings.Builder
plugin.SetOutputTarget(&outputBuffer)
// os.Exit calls break tests
plugin.SkipOSExit()
//nolint:goconst
plugin.ServiceOutput =
"OK: Datastore HUSVM-DC1-vol6 space usage (0 VMs)" +
" is 0.01% of 18.0TB with 18.0TB remaining" +
" [WARNING: 90% , CRITICAL: 95%]"
pluginOutputWithLongServiceOutputDefaultMetrics(t, &plugin)
// Process exit state, emit output to our output buffer.
plugin.ReturnCheckResults()
// Retrieve the output buffer content so that we can compare actual output
// against our expected output to assert we have a 1:1 match.
got := outputBuffer.String()
if d := cmp.Diff(want, got); d != "" {
t.Errorf("(-want, +got)\n:%s", d)
}
}
// TestDefaultPerformanceDataIsAfterLongServiceOutput asserts that performance
// data is emitted after Long Service Output when that content is available.
// We use default performance data metrics for this test.
//
// See also:
//
// - https://github.com/atc0005/go-nagios/issues/103
func TestDefaultPerformanceDataIsAfterLongServiceOutput(t *testing.T) {
t.Parallel()
want := pluginOutputGH103MultiLineWithPerfData
var outputBuffer strings.Builder
// Setup Plugin value manually. This approach does not provide the
// default time metric that would be provided when using the Plugin
// constructor.
plugin := nagios.Plugin{
LastError: nil,
ExitStatusCode: nagios.StateOKExitCode,
}
plugin.SetOutputTarget(&outputBuffer)
// os.Exit calls break tests
plugin.SkipOSExit()
pluginOutputWithLongServiceOutputSetup(t, &plugin)
pluginOutputWithLongServiceOutputDefaultMetrics(t, &plugin)
// Process exit state, emit output to our output buffer.
plugin.ReturnCheckResults()
// Retrieve the output buffer content so that we can compare actual output
// against our expected output to assert we have a 1:1 match.
got := outputBuffer.String()
if d := cmp.Diff(want, got); d != "" {
t.Errorf("(-want, +got)\n:%s", d)
}
}
// TestAllOptionalPerformanceDataIsOnSameLineAsServiceOutput asserts that
// performance data is emitted on the same line as the Service Output (aka,
// "one-line summary") if Long Service Output is empty. We use all optional
// performance data metrics for this test.
//
// NOTE: Later additions of optional performance data metrics will require
// updating the "golden" file to reflect those values (and expected output
// size). We may need to refactor this test to either be less strict.
func TestAllOptionalPerformanceDataIsOnSameLineAsServiceOutput(t *testing.T) {
t.Parallel()
want := pluginOutputOneLineWithOptionalPerfDataIncluded
// Setup Plugin value manually. This approach does not provide the
// default time metric that would be provided when using the Plugin
// constructor.
plugin := nagios.Plugin{
LastError: nil,
ExitStatusCode: nagios.StateOKExitCode,
}
var outputBuffer strings.Builder
plugin.SetOutputTarget(&outputBuffer)
// os.Exit calls break tests
plugin.SkipOSExit()
//nolint:goconst
plugin.ServiceOutput =
"OK: Datastore HUSVM-DC1-vol6 space usage (0 VMs)" +
" is 0.01% of 18.0TB with 18.0TB remaining" +
" [WARNING: 90% , CRITICAL: 95%]"
pluginOutputWithLongServiceOutputDefaultMetrics(t, &plugin)
pluginOutputWithLongServiceOutputAllOptionalMetrics(t, &plugin)
// Process exit state, emit output to our output buffer.
plugin.ReturnCheckResults()
// Retrieve the output buffer content so that we can compare actual output
// against our expected output to assert we have a 1:1 match.
got := outputBuffer.String()
if d := cmp.Diff(want, got); d != "" {
t.Errorf("(-want, +got)\n:%s", d)
}
}
// TestAllOptionalPerformanceDataIsAfterLongServiceOutput asserts that
// performance data is emitted after Long Service Output when that content is
// available. We use all optional performance data metrics for this test.
//
// NOTE: Later additions of optional performance data metrics will require
// updating the "golden" file to reflect those values (and expected output
func TestAllOptionalPerformanceDataIsAfterLongServiceOutput(t *testing.T) {
t.Parallel()
want := pluginOutputMultiLineWithOptionalPerfDataIncluded
var outputBuffer strings.Builder
// Setup Plugin value manually. This approach does not provide the
// default time metric that would be provided when using the Plugin
// constructor.
plugin := nagios.Plugin{
LastError: nil,
ExitStatusCode: nagios.StateOKExitCode,
}
plugin.SetOutputTarget(&outputBuffer)
// os.Exit calls break tests
plugin.SkipOSExit()
pluginOutputWithLongServiceOutputSetup(t, &plugin)
pluginOutputWithLongServiceOutputDefaultMetrics(t, &plugin)
pluginOutputWithLongServiceOutputAllOptionalMetrics(t, &plugin)
// Process exit state, emit output to our output buffer.
plugin.ReturnCheckResults()
// Retrieve the output buffer content so that we can compare actual output
// against our expected output to assert we have a 1:1 match.
got := outputBuffer.String()
if d := cmp.Diff(want, got); d != "" {
t.Errorf("(-want, +got)\n:%s", d)
}
}
// TestEmptyServiceOutputAndManuallyConstructedPluginProducesNoOutput
// asserts that an empty ServiceOutput field produces no output when manually
// constructing the Plugin value.
func TestEmptyServiceOutputAndManuallyConstructedPluginProducesNoOutput(t *testing.T) {
t.Parallel()
// Setup Plugin value manually. This approach does not provide the
// default time metric that would be provided when using the Plugin
// constructor.
plugin := nagios.Plugin{
LastError: nil,
ExitStatusCode: nagios.StateOKExitCode,
}
var outputBuffer strings.Builder
// Explicitly indicate that the field is empty (default/zero value).
plugin.ServiceOutput = ""
plugin.SetOutputTarget(&outputBuffer)
// os.Exit calls break tests
plugin.SkipOSExit()
// Process exit state, emit output to our output buffer.
plugin.ReturnCheckResults()
want := ""
// Retrieve the output buffer content so that we can compare actual output
// against our expected output to assert we have a 1:1 match.
got := outputBuffer.String()
if d := cmp.Diff(want, got); d != "" {
t.Errorf("(-want, +got)\n:%s", d)
} else {
t.Logf("OK: Empty ServiceOutput field produces no output.")
}
}
// TestEmptyServiceOutputAndConstructedPluginProducesNoOutput asserts that
// an empty ServiceOutput field produces no output. We provide a default time
// metric if client code does not specify one AND if there is ServiceOutput
// content to emit.
func TestEmptyServiceOutputAndConstructedPluginProducesNoOutput(t *testing.T) {
t.Parallel()
// Setup Plugin type the same way that client code using the
// constructor would.
plugin := nagios.NewPlugin()
var outputBuffer strings.Builder
// Explicitly indicate that the field is empty (default/zero value).
plugin.ServiceOutput = ""
plugin.SetOutputTarget(&outputBuffer)
// os.Exit calls break tests
plugin.SkipOSExit()
// Process exit state, emit output to our output buffer.
plugin.ReturnCheckResults()
want := ""
// Retrieve the output buffer content so that we can compare actual output
// against our expected output to assert we have a 1:1 match.
got := outputBuffer.String()
if d := cmp.Diff(want, got); d != "" {
t.Errorf("(-want, +got)\n:%s", d)
} else {
t.Logf("OK: Empty ServiceOutput field produces no output.")
}
}
// TestEmptyClientPerfDataAndConstructedPluginProducesDefaultTimeMetric
// asserts that omitted performance data from client code produces a default
// time metric when using the Plugin constructor.
func TestEmptyClientPerfDataAndConstructedPluginProducesDefaultTimeMetric(t *testing.T) {
t.Parallel()
// Setup Plugin type the same way that client code using the
// constructor would.
plugin := nagios.NewPlugin()
// Performance Data metrics are not emitted if we do not supply a
// ServiceOutput value.
plugin.ServiceOutput = "TacoTuesday"
var outputBuffer strings.Builder
plugin.SetOutputTarget(&outputBuffer)
// os.Exit calls break tests
plugin.SkipOSExit()
// Process exit state, emit output to our output buffer.
plugin.ReturnCheckResults()
want := fmt.Sprintf(
"%s | %s",
plugin.ServiceOutput,
"'time'=",
)
got := outputBuffer.String()
if !strings.Contains(got, want) {
t.Errorf("ERROR: Plugin output does not contain the expected time metric")
t.Errorf("\nwant %q\ngot %q", want, got)
} else {
t.Logf("OK: Emitted performance data contains the expected time metric.")
}
}
// TestPluginWithEncodedPayloadWithValidInputProducesValidOutput asserts that
// a populated payload buffer with valid delimiters and section header
// produces valid output.
func TestPluginWithEncodedPayloadWithValidInputProducesValidOutput(t *testing.T) {
t.Parallel()
tests := map[string]struct {
unencodedPayloadInput string
expectedOutput string
delimiterLeft string
delimiterRight string
sectionHeader string
}{
"custom section header and large encoded payload and custom delimiters": {
unencodedPayloadInput: largePayloadUnencoded,
expectedOutput: pluginOutputCustomSectionHeadersAndLargeEncodedPayloadWithCustomDelimiters,
delimiterLeft: customEncodingDelimiterLeft,
delimiterRight: customEncodingDelimiterRight,
sectionHeader: customSectionHeader,
},
"custom section header and large encoded payload and default delimiters": {
unencodedPayloadInput: largePayloadUnencoded,
expectedOutput: pluginOutputCustomSectionHeadersAndLargeEncodedPayloadWithDefaultDelimiters,
delimiterLeft: nagios.DefaultASCII85EncodingDelimiterLeft,
delimiterRight: nagios.DefaultASCII85EncodingDelimiterRight,
sectionHeader: customSectionHeader,
},
"custom section header and large encoded payload and no delimiters": {
unencodedPayloadInput: largePayloadUnencoded,
expectedOutput: pluginOutputCustomSectionHeadersAndLargeEncodedPayloadWithNoDelimiters,
delimiterLeft: "",
delimiterRight: "",
sectionHeader: customSectionHeader,
},
"custom section header and small encoded json payload and custom delimiters": {
unencodedPayloadInput: smallJSONPayloadUnencoded,
expectedOutput: pluginOutputCustomSectionHeadersAndSmallEncodedJSONPayloadWithCustomDelimiters,
delimiterLeft: customEncodingDelimiterLeft,
delimiterRight: customEncodingDelimiterRight,
sectionHeader: customSectionHeader,
},
"custom section header and small encoded json payload and default delimiters": {
unencodedPayloadInput: smallJSONPayloadUnencoded,
expectedOutput: pluginOutputCustomSectionHeadersAndSmallEncodedJSONPayloadWithDefaultDelimiters,
delimiterLeft: nagios.DefaultASCII85EncodingDelimiterLeft,
delimiterRight: nagios.DefaultASCII85EncodingDelimiterRight,
sectionHeader: customSectionHeader,
},
"custom section header and small encoded json payload and no delimiters": {
unencodedPayloadInput: smallJSONPayloadUnencoded,
expectedOutput: pluginOutputCustomSectionHeadersAndSmallEncodedJSONPayloadWithNoDelimiters,
delimiterLeft: "",
delimiterRight: "",
sectionHeader: customSectionHeader,
},
"custom section header and small encoded plaintext payload and custom delimiters": {
unencodedPayloadInput: smallPlaintextPayloadUnencoded,
expectedOutput: pluginOutputCustomSectionHeadersAndSmallEncodedPlaintextPayloadWithCustomDelimiters,
delimiterLeft: customEncodingDelimiterLeft,
delimiterRight: customEncodingDelimiterRight,
sectionHeader: customSectionHeader,
},
"custom section header and small encoded plaintext payload and default delimiters": {
unencodedPayloadInput: smallPlaintextPayloadUnencoded,
expectedOutput: pluginOutputCustomSectionHeadersAndSmallEncodedPlaintextPayloadWithDefaultDelimiters,
delimiterLeft: nagios.DefaultASCII85EncodingDelimiterLeft,
delimiterRight: nagios.DefaultASCII85EncodingDelimiterRight,
sectionHeader: customSectionHeader,
},
"custom section header and small encoded plaintext payload and no delimiters": {
unencodedPayloadInput: smallPlaintextPayloadUnencoded,
expectedOutput: pluginOutputCustomSectionHeadersAndSmallEncodedPlaintextPayloadWithNoDelimiters,
delimiterLeft: "",
delimiterRight: "",
sectionHeader: customSectionHeader,
},
"default section header and large encoded payload and custom delimiters": {
unencodedPayloadInput: largePayloadUnencoded,
expectedOutput: pluginOutputDefaultSectionHeadersAndLargeEncodedPayloadWithCustomDelimiters,
delimiterLeft: customEncodingDelimiterLeft,
delimiterRight: customEncodingDelimiterRight,
sectionHeader: "",
},
"default section header and large encoded payload and default delimiters": {
unencodedPayloadInput: largePayloadUnencoded,
expectedOutput: pluginOutputDefaultSectionHeadersAndLargeEncodedPayloadWithDefaultDelimiters,
delimiterLeft: nagios.DefaultASCII85EncodingDelimiterLeft,
delimiterRight: nagios.DefaultASCII85EncodingDelimiterRight,
sectionHeader: "",
},
"default section header and large encoded payload and no delimiters": {
unencodedPayloadInput: largePayloadUnencoded,
expectedOutput: pluginOutputDefaultSectionHeadersAndLargeEncodedPayloadWithNoDelimiters,
delimiterLeft: "",
delimiterRight: "",
sectionHeader: "",
},
"default section header and small encoded json payload and custom delimiters": {
unencodedPayloadInput: smallJSONPayloadUnencoded,
expectedOutput: pluginOutputDefaultSectionHeadersAndSmallEncodedJSONPayloadWithCustomDelimiters,
delimiterLeft: customEncodingDelimiterLeft,
delimiterRight: customEncodingDelimiterRight,
sectionHeader: "",
},
"default section header and small encoded json payload and default delimiters": {
unencodedPayloadInput: smallJSONPayloadUnencoded,
expectedOutput: pluginOutputDefaultSectionHeadersAndSmallEncodedJSONPayloadWithDefaultDelimiters,
delimiterLeft: nagios.DefaultASCII85EncodingDelimiterLeft,
delimiterRight: nagios.DefaultASCII85EncodingDelimiterRight,
sectionHeader: "",
},
"default section header and small encoded json payload and no delimiters": {
unencodedPayloadInput: smallJSONPayloadUnencoded,
expectedOutput: pluginOutputDefaultSectionHeadersAndSmallEncodedJSONPayloadWithNoDelimiters,
delimiterLeft: "",
delimiterRight: "",
sectionHeader: "",
},
"default section header and small encoded plaintext payload and custom delimiters": {
unencodedPayloadInput: smallPlaintextPayloadUnencoded,
expectedOutput: pluginOutputDefaultSectionHeadersAndSmallEncodedPlaintextPayloadWithCustomDelimiters,
delimiterLeft: customEncodingDelimiterLeft,
delimiterRight: customEncodingDelimiterRight,
sectionHeader: "",
},
"default section header and small encoded plaintext payload and default delimiters": {
unencodedPayloadInput: smallPlaintextPayloadUnencoded,
expectedOutput: pluginOutputDefaultSectionHeadersAndSmallEncodedPlaintextPayloadWithDefaultDelimiters,
delimiterLeft: nagios.DefaultASCII85EncodingDelimiterLeft,
delimiterRight: nagios.DefaultASCII85EncodingDelimiterRight,
sectionHeader: "",
},
"default section header and small encoded plaintext payload and no delimiters": {
unencodedPayloadInput: smallPlaintextPayloadUnencoded,
expectedOutput: pluginOutputDefaultSectionHeadersAndSmallEncodedPlaintextPayloadWithNoDelimiters,
delimiterLeft: "",
delimiterRight: "",
sectionHeader: "",
},
}
for name, tt := range tests {
// Guard against referencing the loop iterator variable directly.
//
// https://stackoverflow.com/questions/68559574/using-the-variable-on-range-scope-x-in-function-literal-scopelint
// https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
tt := tt
//
t.Run(name, func(t *testing.T) {
// Setup Plugin type the same way that client code using the
// constructor would.
plugin := nagios.NewPlugin()
// plugin.DebugLoggingEnableAll()
var outputBuffer strings.Builder
plugin.SetOutputTarget(&outputBuffer)
// os.Exit calls break tests
plugin.SkipOSExit()
pluginOutputWithLongServiceOutputSetup(t, plugin)
// Current logic is that you need to specify blank delimiters to
// skip including delimiters in the encoded output. Because of how
// we're setting up the test cases we treat an empty string as if
// the `SetEncodedPayloadDelimiterLeft` or
// `SetEncodedPayloadDelimiterRight` methods are called with an
// empty string.
plugin.SetEncodedPayloadDelimiterLeft(tt.delimiterLeft)
plugin.SetEncodedPayloadDelimiterRight(tt.delimiterRight)
if tt.sectionHeader != "" {
plugin.SetEncodedPayloadLabel(tt.sectionHeader)
}
written, err := plugin.AddPayloadString(tt.unencodedPayloadInput)
if err != nil {
t.Fatalf("Failed to append given input to payload buffer: %v", err)
} else {
t.Logf("Successfully appended %d bytes given input to payload buffer", written)
}
pluginOutputWithLongServiceOutputDefaultMetrics(t, plugin)
// Process exit state, emit output to our output buffer.
plugin.ReturnCheckResults()
want := tt.expectedOutput
got := outputBuffer.String()
if d := cmp.Diff(want, got); d != "" {
t.Errorf("(-want, +got)\n:%s", d)
} else {
t.Logf("OK: Plugin output matches expected format.")
}
})
}
}
func TestEncodePayload_SuccessfullyEncodesPayloadWithValidInput(t *testing.T) {
t.Parallel()
type testCase struct {
unencodedPayloadInput []byte
delimiterLeft string
delimiterRight string
expectedOutput string
}
tests := map[string]testCase{
"small unencoded json payload with default delimiters added": {
unencodedPayloadInput: []byte(smallJSONPayloadUnencoded),
delimiterLeft: nagios.DefaultASCII85EncodingDelimiterLeft,
delimiterRight: nagios.DefaultASCII85EncodingDelimiterRight,
expectedOutput: smallJSONPayloadEncodedWithDefaultDelimiters,
},
"small unencoded json payload with custom delimiters added": {
unencodedPayloadInput: []byte(smallJSONPayloadUnencoded),
delimiterLeft: customEncodingDelimiterLeft,
delimiterRight: customEncodingDelimiterRight,
expectedOutput: smallJSONPayloadEncodedWithCustomDelimiters,
},
"small unencoded json payload with no delimiters added": {
unencodedPayloadInput: []byte(smallJSONPayloadUnencoded),
delimiterLeft: "",
delimiterRight: "",
expectedOutput: smallJSONPayloadEncodedWithNoDelimiters,
},
"small unencoded plaintext payload with default delimiters added": {
unencodedPayloadInput: []byte(smallPlaintextPayloadUnencoded),
delimiterLeft: nagios.DefaultASCII85EncodingDelimiterLeft,
delimiterRight: nagios.DefaultASCII85EncodingDelimiterRight,
expectedOutput: smallPlaintextPayloadEncodedWithDefaultDelimiters,
},
"small unencoded plaintext payload with custom delimiters added": {
unencodedPayloadInput: []byte(smallPlaintextPayloadUnencoded),
delimiterLeft: customEncodingDelimiterLeft,
delimiterRight: customEncodingDelimiterRight,
expectedOutput: smallPlaintextPayloadEncodedWithCustomDelimiters,
},
"small unencoded plaintext payload with no delimiters added": {
unencodedPayloadInput: []byte(smallPlaintextPayloadUnencoded),
delimiterLeft: "",
delimiterRight: "",
expectedOutput: smallPlaintextPayloadEncodedWithNoDelimiters,
},
"large unencoded plaintext payload with default delimiters added": {
unencodedPayloadInput: []byte(largePayloadUnencoded),
delimiterLeft: nagios.DefaultASCII85EncodingDelimiterLeft,
delimiterRight: nagios.DefaultASCII85EncodingDelimiterRight,
expectedOutput: largePayloadEncodedWithDefaultDelimiters,
},
"large unencoded plaintext payload with custom delimiters added": {
unencodedPayloadInput: []byte(largePayloadUnencoded),
delimiterLeft: customEncodingDelimiterLeft,
delimiterRight: customEncodingDelimiterRight,
expectedOutput: largePayloadEncodedWithCustomDelimiters,
},
"large unencoded plaintext payload with no delimiters added": {
unencodedPayloadInput: []byte(largePayloadUnencoded),
delimiterLeft: "",
delimiterRight: "",
expectedOutput: largePayloadEncodedWithNoDelimiters,
},
}
for name, tt := range tests {
// Guard against referencing the loop iterator variable directly.
//
// https://stackoverflow.com/questions/68559574/using-the-variable-on-range-scope-x-in-function-literal-scopelint
// https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
tt := tt
//
t.Run(name, func(t *testing.T) {
want := tt.expectedOutput
got := nagios.EncodePayload(
tt.unencodedPayloadInput,
tt.delimiterLeft,
tt.delimiterRight,
)
if d := cmp.Diff(want, got); d != "" {
t.Errorf("(-want, +got)\n:%s", d)
} else {
t.Logf("OK: Encoded payload matches expected output.")
}
})
}
}
func TestEncodePayload_FailsToEncodePayloadWithInvalidInput(t *testing.T) {
t.Parallel()
type testCase struct {
unencodedPayloadInput []byte
delimiterLeft string
delimiterRight string
expectedOutput string
}
tests := map[string]testCase{
"empty input with default delimiters": {
unencodedPayloadInput: []byte(""),
delimiterLeft: nagios.DefaultASCII85EncodingDelimiterLeft,
delimiterRight: nagios.DefaultASCII85EncodingDelimiterRight,
expectedOutput: "",
},
"empty input with custom delimiters": {
unencodedPayloadInput: []byte(""),
delimiterLeft: customEncodingDelimiterLeft,
delimiterRight: customEncodingDelimiterRight,
expectedOutput: "",
},
"empty input with no delimiters": {
unencodedPayloadInput: []byte(""),
delimiterLeft: "",
delimiterRight: "",
expectedOutput: "",
},
}
for name, tt := range tests {
// Guard against referencing the loop iterator variable directly.
//
// https://stackoverflow.com/questions/68559574/using-the-variable-on-range-scope-x-in-function-literal-scopelint
// https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
tt := tt
//
t.Run(name, func(t *testing.T) {
want := tt.expectedOutput
got := nagios.EncodePayload(
tt.unencodedPayloadInput,
tt.delimiterLeft,
tt.delimiterRight,
)
if d := cmp.Diff(want, got); d != "" {
t.Errorf("(-want, +got)\n:%s", d)
} else {
t.Logf("OK: Encoded payload matches expected output.")
}
})
}
}
func TestDecodePayload_SuccessfullyDecodesPayloadWithValidInput(t *testing.T) {
t.Parallel()
type testCase struct {
encodedPayloadInput []byte
delimiterLeft string
delimiterRight string
expectedOutput string
}
tests := map[string]testCase{
"small encoded json payload with default delimiters": {
encodedPayloadInput: []byte(smallJSONPayloadEncodedWithDefaultDelimiters),
delimiterLeft: nagios.DefaultASCII85EncodingDelimiterLeft,
delimiterRight: nagios.DefaultASCII85EncodingDelimiterRight,
expectedOutput: smallJSONPayloadUnencoded,
},
"small encoded json payload with custom delimiters": {
encodedPayloadInput: []byte(smallJSONPayloadEncodedWithCustomDelimiters),
delimiterLeft: customEncodingDelimiterLeft,
delimiterRight: customEncodingDelimiterRight,
expectedOutput: smallJSONPayloadUnencoded,
},
"small encoded json payload with no delimiters": {
encodedPayloadInput: []byte(smallJSONPayloadEncodedWithNoDelimiters),
delimiterLeft: "",
delimiterRight: "",
expectedOutput: smallJSONPayloadUnencoded,
},
"small encoded plaintext payload with default delimiters": {
encodedPayloadInput: []byte(smallPlaintextPayloadEncodedWithDefaultDelimiters),
delimiterLeft: nagios.DefaultASCII85EncodingDelimiterLeft,
delimiterRight: nagios.DefaultASCII85EncodingDelimiterRight,
expectedOutput: smallPlaintextPayloadUnencoded,
},
"small encoded plaintext payload with custom delimiters": {
encodedPayloadInput: []byte(smallPlaintextPayloadEncodedWithCustomDelimiters),
delimiterLeft: customEncodingDelimiterLeft,
delimiterRight: customEncodingDelimiterRight,
expectedOutput: smallPlaintextPayloadUnencoded,
},
"small encoded plaintext payload with no delimiters": {
encodedPayloadInput: []byte(smallPlaintextPayloadEncodedWithNoDelimiters),
delimiterLeft: "",
delimiterRight: "",
expectedOutput: smallPlaintextPayloadUnencoded,
},
"large encoded plaintext payload with default delimiters": {
encodedPayloadInput: []byte(largePayloadEncodedWithDefaultDelimiters),
delimiterLeft: nagios.DefaultASCII85EncodingDelimiterLeft,
delimiterRight: nagios.DefaultASCII85EncodingDelimiterRight,
expectedOutput: largePayloadUnencoded,
},
"large encoded plaintext payload with custom delimiters": {
encodedPayloadInput: []byte(largePayloadEncodedWithCustomDelimiters),
delimiterLeft: customEncodingDelimiterLeft,
delimiterRight: customEncodingDelimiterRight,
expectedOutput: largePayloadUnencoded,
},
"large encoded plaintext payload with no delimiters": {
encodedPayloadInput: []byte(largePayloadEncodedWithNoDelimiters),
delimiterLeft: "",
delimiterRight: "",
expectedOutput: largePayloadUnencoded,
},
}
for name, tt := range tests {
// Guard against referencing the loop iterator variable directly.
//
// https://stackoverflow.com/questions/68559574/using-the-variable-on-range-scope-x-in-function-literal-scopelint
// https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
tt := tt
//
t.Run(name, func(t *testing.T) {
want := tt.expectedOutput
result, err := nagios.DecodePayload(
tt.encodedPayloadInput,
tt.delimiterLeft,
tt.delimiterRight,
)
got := string(result)
if err != nil {
t.Fatalf("Failed to decode encoded payload: %v", err)
} else {
t.Logf("Successfully decoded encoded payload")
}
if d := cmp.Diff(want, got); d != "" {
t.Errorf("(-want, +got)\n:%s", d)
} else {
t.Logf("OK: Encoded payload matches expected output.")
}
})
}
}
func TestDecodePayload_FailsToDecodePayloadWithInvalidInput(t *testing.T) {
t.Parallel()
type testCase struct {
input []byte
delimiterLeft string
delimiterRight string
expectedErr error
}
tests := map[string]testCase{
"empty input with default delimiters": {
input: []byte(""),
delimiterLeft: nagios.DefaultASCII85EncodingDelimiterLeft,
delimiterRight: nagios.DefaultASCII85EncodingDelimiterRight,
expectedErr: nagios.ErrMissingValue,
},
"empty input with custom delimiters": {
input: []byte(""),
delimiterLeft: customEncodingDelimiterLeft,
delimiterRight: customEncodingDelimiterRight,
expectedErr: nagios.ErrMissingValue,
},
"empty input with no delimiters": {
input: []byte(""),
delimiterLeft: "",
delimiterRight: "",
expectedErr: nagios.ErrMissingValue,
},
"unencoded input with default delimiters": {
input: []byte("!z!!!!!!!!!"), // borrowed from TestDecodeCorrupt testcase within ascii85_test.go
delimiterLeft: nagios.DefaultASCII85EncodingDelimiterLeft,
delimiterRight: nagios.DefaultASCII85EncodingDelimiterRight,
expectedErr: ascii85.CorruptInputError(1), // illegal ascii85 data at input byte 1
},
"unencoded input with custom delimiters": {
input: []byte("!z!!!!!!!!!"), // borrowed from TestDecodeCorrupt testcase within ascii85_test.go
delimiterLeft: customEncodingDelimiterLeft,
delimiterRight: customEncodingDelimiterRight,
expectedErr: ascii85.CorruptInputError(1), // illegal ascii85 data at input byte 1