forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deprecation_test.go
276 lines (250 loc) · 7.04 KB
/
deprecation_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
package config
import (
"bufio"
"bytes"
"log"
"strings"
"testing"
"time"
"github.com/coreos/go-semver/semver"
"github.com/influxdata/telegraf"
"github.com/stretchr/testify/require"
)
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.LogLevel
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 != "" {
expected := deprecationPrefix(tt.level) + ": " + tt.expected
require.Equal(t, expected, actual)
} else {
require.Empty(t, actual)
}
})
}
}
func TestPluginOptionDeprecation(t *testing.T) {
var tests = []struct {
name string
since string
removal string
expected string
expectedLevel telegraf.LogLevel
}{
{
name: "Error level",
since: "1.23.0",
removal: "1.29.0",
expectedLevel: telegraf.Error,
expected: `Option "option" of plugin "test" deprecated since version 1.23.0 and will be removed in 1.29.0: please check`,
},
{
name: "Warn level",
since: "1.23.0",
removal: "2.0.0",
expectedLevel: 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: "No removal info",
since: "1.23.0",
expectedLevel: 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",
expectedLevel: telegraf.None,
expected: ``,
},
}
// Fake telegraf's version
version, err := semver.NewVersion("1.30.0")
require.NoError(t, err)
telegrafVersion = version
// 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()
info := telegraf.DeprecationInfo{
Since: tt.since,
RemovalIn: tt.removal,
Notice: "please check",
}
PrintOptionDeprecationNotice("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 != "" {
expected := deprecationPrefix(tt.expectedLevel) + ": " + tt.expected
require.Equal(t, expected, actual)
} else {
require.Empty(t, actual)
}
})
}
}
func TestPluginOptionValueDeprecation(t *testing.T) {
var tests = []struct {
name string
since string
removal string
value interface{}
expected string
expectedLevel telegraf.LogLevel
}{
{
name: "Error level",
since: "1.25.0",
removal: "1.29.0",
value: "foobar",
expected: `Value "foobar" for option "option" of plugin "test" deprecated since version 1.25.0 and will be removed in 1.29.0: please check`,
expectedLevel: telegraf.Error,
},
{
name: "Warn level",
since: "1.25.0",
removal: "2.0.0",
value: "foobar",
expected: `Value "foobar" for option "option" of plugin "test" deprecated since version 1.25.0 and will be removed in 2.0.0: please check`,
expectedLevel: telegraf.Warn,
},
{
name: "No removal info",
since: "1.25.0",
value: "foobar",
expected: `Value "foobar" for option "option" of plugin "test" deprecated since version 1.25.0 and will be removed in 2.0.0: please check`,
expectedLevel: telegraf.Warn,
},
{
name: "None",
expected: ``,
expectedLevel: telegraf.None,
},
{
name: "nil value",
since: "1.25.0",
removal: "1.29.0",
value: nil,
expected: `Value "<nil>" for option "option" of plugin "test" deprecated since version 1.25.0 and will be removed in 1.29.0: please check`,
expectedLevel: telegraf.Error,
},
{
name: "Boolean value",
since: "1.25.0",
removal: "1.29.0",
value: true,
expected: `Value "true" for option "option" of plugin "test" deprecated since version 1.25.0 and will be removed in 1.29.0: please check`,
expectedLevel: telegraf.Error,
},
{
name: "Integer value",
since: "1.25.0",
removal: "1.29.0",
value: 123,
expected: `Value "123" for option "option" of plugin "test" deprecated since version 1.25.0 and will be removed in 1.29.0: please check`,
expectedLevel: telegraf.Error,
},
}
// Fake telegraf's version
version, err := semver.NewVersion("1.30.0")
require.NoError(t, err)
telegrafVersion = version
// Switch the logger to log to a buffer
var buf bytes.Buffer
previous := log.Writer()
log.SetOutput(&buf)
defer log.SetOutput(previous)
timeout := 1 * time.Second
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf.Reset()
info := telegraf.DeprecationInfo{
Since: tt.since,
RemovalIn: tt.removal,
Notice: "please check",
}
PrintOptionValueDeprecationNotice("test", "option", tt.value, info)
if tt.expected != "" {
require.Eventually(t, func() bool {
return strings.HasSuffix(buf.String(), "\n")
}, timeout, 100*time.Millisecond)
// Remove the time for comparison
actual := strings.TrimSpace(buf.String())
expected := deprecationPrefix(tt.expectedLevel) + ": " + tt.expected
require.Equal(t, expected, actual)
} else {
time.Sleep(timeout)
require.Empty(t, buf.String())
}
})
}
}