From b73136c110926c10ae21950b8c953cda0edaccb4 Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Wed, 6 Jul 2022 18:42:59 +0200 Subject: [PATCH] fix: Deprecation warnings for non-deprecated packages (#11460) --- models/log.go | 41 ++++++------ models/log_test.go | 155 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+), 20 deletions(-) diff --git a/models/log.go b/models/log.go index c401d1b46..c863325da 100644 --- a/models/log.go +++ b/models/log.go @@ -103,34 +103,35 @@ func SetLoggerOnPlugin(i interface{}, logger telegraf.Logger) { } } -func PrintPluginDeprecationNotice(level telegraf.Escalation, name string, info telegraf.DeprecationInfo) { - var prefix string - +func deprecationPrefix(level telegraf.Escalation) string { switch level { case telegraf.Warn: - prefix = "W! " + color.YellowString("DeprecationWarning") + return "W! " + color.YellowString("DeprecationWarning") case telegraf.Error: - prefix = "E! " + color.RedString("DeprecationError") + return "E! " + color.RedString("DeprecationError") } + return "" +} - log.Printf( - "%s: Plugin %q deprecated since version %s and will be removed in %s: %s", - prefix, name, info.Since, info.RemovalIn, info.Notice, - ) +func PrintPluginDeprecationNotice(level telegraf.Escalation, name string, info telegraf.DeprecationInfo) { + switch level { + case telegraf.Warn, telegraf.Error: + prefix := deprecationPrefix(level) + + log.Printf( + "%s: Plugin %q deprecated since version %s and will be removed in %s: %s", + prefix, name, info.Since, info.RemovalIn, info.Notice, + ) + } } func PrintOptionDeprecationNotice(level telegraf.Escalation, plugin, option string, info telegraf.DeprecationInfo) { - var prefix string - switch level { - case telegraf.Warn: - prefix = "W! " + color.YellowString("DeprecationWarning") - case telegraf.Error: - prefix = "E! " + color.RedString("DeprecationError") + case telegraf.Warn, telegraf.Error: + prefix := deprecationPrefix(level) + log.Printf( + "%s: Option %q of plugin %q deprecated since version %s and will be removed in %s: %s", + prefix, option, plugin, info.Since, info.RemovalIn, info.Notice, + ) } - - log.Printf( - "%s: Option %q of plugin %q deprecated since version %s and will be removed in %s: %s", - prefix, option, plugin, info.Since, info.RemovalIn, info.Notice, - ) } diff --git a/models/log_test.go b/models/log_test.go index 2b5ec39c6..fd43a2bd8 100644 --- a/models/log_test.go +++ b/models/log_test.go @@ -1,8 +1,14 @@ package models import ( + "bufio" + "bytes" + "log" + "strings" "testing" + "time" + "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/selfstat" "github.com/stretchr/testify/require" ) @@ -22,3 +28,152 @@ func TestErrorCounting(t *testing.T) { require.Equal(t, int64(2), reg.Get()) } + +func TestPluginDeprecation(t *testing.T) { + info := telegraf.DeprecationInfo{ + Since: "1.23.0", + RemovalIn: "2.0.0", + Notice: "please check", + } + var tests = []struct { + name string + level telegraf.Escalation + expected string + }{ + { + name: "Error level", + level: telegraf.Error, + expected: `Plugin "test" deprecated since version 1.23.0 and will be removed in 2.0.0: please check`, + }, + { + name: "Warn level", + level: telegraf.Warn, + expected: `Plugin "test" deprecated since version 1.23.0 and will be removed in 2.0.0: please check`, + }, + { + name: "None", + level: telegraf.None, + expected: ``, + }, + } + + // Switch the logger to log to a buffer + var buf bytes.Buffer + scanner := bufio.NewScanner(&buf) + + previous := log.Writer() + log.SetOutput(&buf) + defer log.SetOutput(previous) + + msg := make(chan string, 1) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + buf.Reset() + PrintPluginDeprecationNotice(tt.level, "test", info) + + // Wait for a newline to arrive and timeout for cases where + // we don't see a message. + go func() { + scanner.Scan() + msg <- scanner.Text() + }() + + // Reduce the timeout if we do not expect a message + timeout := 1 * time.Second + if tt.expected == "" { + timeout = 100 * time.Microsecond + } + + var actual string + select { + case actual = <-msg: + case <-time.After(timeout): + } + + if tt.expected != "" { + // Remove the time for comparison + parts := strings.SplitN(actual, " ", 3) + require.Len(t, parts, 3) + actual = parts[2] + expected := deprecationPrefix(tt.level) + ": " + tt.expected + require.Equal(t, expected, actual) + } else { + require.Empty(t, actual) + } + }) + } +} + +func TestPluginOptionDeprecation(t *testing.T) { + info := telegraf.DeprecationInfo{ + Since: "1.23.0", + RemovalIn: "2.0.0", + Notice: "please check", + } + var tests = []struct { + name string + level telegraf.Escalation + expected string + }{ + { + name: "Error level", + level: telegraf.Error, + expected: `Option "option" of plugin "test" deprecated since version 1.23.0 and will be removed in 2.0.0: please check`, + }, + { + name: "Warn level", + level: telegraf.Warn, + expected: `Option "option" of plugin "test" deprecated since version 1.23.0 and will be removed in 2.0.0: please check`, + }, + { + name: "None", + level: telegraf.None, + expected: ``, + }, + } + + // Switch the logger to log to a buffer + var buf bytes.Buffer + scanner := bufio.NewScanner(&buf) + + previous := log.Writer() + log.SetOutput(&buf) + defer log.SetOutput(previous) + + msg := make(chan string, 1) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + buf.Reset() + PrintOptionDeprecationNotice(tt.level, "test", "option", info) + // Wait for a newline to arrive and timeout for cases where + // we don't see a message. + go func() { + scanner.Scan() + msg <- scanner.Text() + }() + + // Reduce the timeout if we do not expect a message + timeout := 1 * time.Second + if tt.expected == "" { + timeout = 100 * time.Microsecond + } + + var actual string + select { + case actual = <-msg: + case <-time.After(timeout): + } + + if tt.expected != "" { + // Remove the time for comparison + parts := strings.SplitN(actual, " ", 3) + require.Len(t, parts, 3) + actual = parts[2] + expected := deprecationPrefix(tt.level) + ": " + tt.expected + require.Equal(t, expected, actual) + } else { + require.Empty(t, actual) + } + }) + } +}