fix: Deprecation warnings for non-deprecated packages (#11460)
This commit is contained in:
parent
0ff94c5332
commit
b73136c110
|
|
@ -103,34 +103,35 @@ func SetLoggerOnPlugin(i interface{}, logger telegraf.Logger) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func PrintPluginDeprecationNotice(level telegraf.Escalation, name string, info telegraf.DeprecationInfo) {
|
func deprecationPrefix(level telegraf.Escalation) string {
|
||||||
var prefix string
|
|
||||||
|
|
||||||
switch level {
|
switch level {
|
||||||
case telegraf.Warn:
|
case telegraf.Warn:
|
||||||
prefix = "W! " + color.YellowString("DeprecationWarning")
|
return "W! " + color.YellowString("DeprecationWarning")
|
||||||
case telegraf.Error:
|
case telegraf.Error:
|
||||||
prefix = "E! " + color.RedString("DeprecationError")
|
return "E! " + color.RedString("DeprecationError")
|
||||||
}
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf(
|
func PrintPluginDeprecationNotice(level telegraf.Escalation, name string, info telegraf.DeprecationInfo) {
|
||||||
"%s: Plugin %q deprecated since version %s and will be removed in %s: %s",
|
switch level {
|
||||||
prefix, name, info.Since, info.RemovalIn, info.Notice,
|
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) {
|
func PrintOptionDeprecationNotice(level telegraf.Escalation, plugin, option string, info telegraf.DeprecationInfo) {
|
||||||
var prefix string
|
|
||||||
|
|
||||||
switch level {
|
switch level {
|
||||||
case telegraf.Warn:
|
case telegraf.Warn, telegraf.Error:
|
||||||
prefix = "W! " + color.YellowString("DeprecationWarning")
|
prefix := deprecationPrefix(level)
|
||||||
case telegraf.Error:
|
log.Printf(
|
||||||
prefix = "E! " + color.RedString("DeprecationError")
|
"%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,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,14 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/influxdata/telegraf/selfstat"
|
"github.com/influxdata/telegraf/selfstat"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
@ -22,3 +28,152 @@ func TestErrorCounting(t *testing.T) {
|
||||||
|
|
||||||
require.Equal(t, int64(2), reg.Get())
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue