forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_test.go
179 lines (160 loc) · 3.97 KB
/
log_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
package models
import (
"bufio"
"bytes"
"log"
"strings"
"testing"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/selfstat"
"github.com/stretchr/testify/require"
)
func TestErrorCounting(t *testing.T) {
reg := selfstat.Register(
"gather",
"errors",
map[string]string{"input": "test"},
)
iLog := Logger{Name: "inputs.test"}
iLog.OnErr(func() {
reg.Incr(1)
})
iLog.Error("something went wrong")
iLog.Errorf("something went wrong")
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)
}
})
}
}