From 98e62773bd1304dcee1d66d84ccf36da3c60090e Mon Sep 17 00:00:00 2001 From: Adam Chalkley Date: Thu, 17 Oct 2024 07:17:09 -0500 Subject: [PATCH 1/2] Add test cases for `Plugin.SetOutputTarget` New tests: - `TestPluginSetOutputTargetIsValidWithValidInput` - basic assertion to show that the method works as intended with valid/expected input - TestPluginSetOutputTargetIsValidWithInvalidInput - asserts that when given an invalid output target that the default/fallback output target is used instead The second test is currently failing, highlighting the logic bug currently affecting the `Plugin.SetOutputTarget` method. refs GH-267 --- unexported_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/unexported_test.go b/unexported_test.go index 262870f..cc9227b 100644 --- a/unexported_test.go +++ b/unexported_test.go @@ -10,12 +10,66 @@ package nagios import ( "fmt" + "os" "strings" "testing" "github.com/google/go-cmp/cmp" ) +func TestPluginSetOutputTargetIsValidWithValidInput(t *testing.T) { + t.Parallel() + + plugin := NewPlugin() + + // Assert that plugin.outputSink is still unset + if plugin.outputSink != nil { + t.Fatal("ERROR: plugin outputSink is not at the expected default unset value.") + } else { + t.Log("OK: plugin outputSink is at the expected default unset value.") + } + + var outputBuffer strings.Builder + + plugin.SetOutputTarget(&outputBuffer) + + // Assert that plugin.outputSink is set as expected. + switch { + case plugin.outputSink == nil: + t.Fatal("ERROR: plugin outputSink is unset instead of the given custom value.") + case plugin.outputSink == os.Stdout: + t.Fatal("ERROR: plugin outputSink is set to the default/fallback value instead of the expected custom value.") + default: + t.Log("OK: plugin outputSink is at the expected custom value.") + } +} + +// TestPluginSetOutputTargetIsValidWithInvalidInput asserts that when given an +// invalid output target that the method falls back to a safe default value. +func TestPluginSetOutputTargetIsValidWithInvalidInput(t *testing.T) { + t.Parallel() + + plugin := NewPlugin() + + // Assert that plugin.outputSink is still unset + if plugin.outputSink != nil { + t.Fatal("ERROR: plugin outputSink is not at the expected default unset value.") + } else { + t.Log("OK: plugin outputSink is at the expected default unset value.") + } + + t.Log("Attempting to set invalid output target. This should cause the default output sink to be set instead.") + plugin.SetOutputTarget(nil) + + // Assert that plugin.outputSink is set to a non-nil default/fallback + // value as expected. + if plugin.outputSink == nil { + t.Fatal("ERROR: plugin outputSink is not at the expected default/fallback value.") + } else { + t.Log("OK: plugin outputSink is at the expected default/fallback value.") + } +} + // TestServiceOutputIsNotInterpolated is intended to prevent further // regressions of formatting being applied to literal/preformatted Service // Output (aka, "one-line summary" output). From 2d05ce591684caf208ef3ac5ff82ed71d24aa27f Mon Sep 17 00:00:00 2001 From: Adam Chalkley Date: Thu, 17 Oct 2024 07:21:25 -0500 Subject: [PATCH 2/2] Fix `Plugin.SetOutputTarget` fallback behavior Correctly fallback to default output target when an invalid output target is provided. refs GH-267 --- nagios.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/nagios.go b/nagios.go index cf56e17..7daba21 100644 --- a/nagios.go +++ b/nagios.go @@ -396,11 +396,14 @@ func (p *Plugin) AddUniqueError(errs ...error) { } // SetOutputTarget assigns a target for Nagios plugin output. By default -// output is emitted to os.Stdout. +// output is emitted to os.Stdout. If given an invalid output target the +// default output target will be used instead. func (p *Plugin) SetOutputTarget(w io.Writer) { // Guard against potential nil argument. if w == nil { p.outputSink = os.Stdout + + return } p.outputSink = w