-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconverter_internal_test.go
160 lines (134 loc) · 6.4 KB
/
converter_internal_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
package logger
import (
"os"
"reflect"
"strings"
"testing"
"time"
"cloud.google.com/go/logging"
"github.com/stretchr/testify/suite"
)
type ConverterSuite struct {
suite.Suite
Name string
}
func TestConverterSuite(t *testing.T) {
suite.Run(t, new(ConverterSuite))
}
func (suite *ConverterSuite) SetupSuite() {
suite.Name = strings.TrimSuffix(reflect.TypeOf(suite).Elem().Name(), "Suite")
}
func (suite *ConverterSuite) TestCanGetConverterFromEnvironment() {
if current, ok := os.LookupEnv("LOG_CONVERTER"); ok {
defer os.Setenv("LOG_CONVERTER", current)
} else {
defer os.Unsetenv("LOG_CONVERTER")
}
os.Setenv("LOG_CONVERTER", "bunyan")
converter := GetConverterFromEnvironment()
suite.Assert().IsType(&BunyanConverter{}, converter)
os.Setenv("LOG_CONVERTER", "aws")
converter = GetConverterFromEnvironment()
suite.Assert().IsType(&CloudWatchConverter{}, converter)
os.Setenv("LOG_CONVERTER", "cloudwatch")
converter = GetConverterFromEnvironment()
suite.Assert().IsType(&CloudWatchConverter{}, converter)
os.Setenv("LOG_CONVERTER", "stackdriver")
converter = GetConverterFromEnvironment()
suite.Assert().IsType(&StackDriverConverter{}, converter)
os.Setenv("LOG_CONVERTER", "google")
converter = GetConverterFromEnvironment()
suite.Assert().IsType(&StackDriverConverter{}, converter)
os.Setenv("LOG_CONVERTER", "gcp")
converter = GetConverterFromEnvironment()
suite.Assert().IsType(&StackDriverConverter{}, converter)
os.Setenv("LOG_CONVERTER", "bello")
converter = GetConverterFromEnvironment()
suite.Assert().IsType(&BunyanConverter{}, converter)
}
func (suite *ConverterSuite) TestCanConvertWithBunyanConverter() {
converter := &BunyanConverter{}
record := NewRecord().Set("time", time.Now().UTC()).Set("bello", "banana")
converted := converter.Convert(record)
suite.Assert().Exactly(record, converted)
value := converted.Get("time")
suite.Require().IsType("string", value)
stime := value.(string)
suite.Assert().Regexp(`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z`, stime)
}
func (suite *ConverterSuite) TestCanConvertWithPinoConverter() {
converter := &PinoConverter{}
record := NewRecord().Set("time", time.Now().UTC()).Set("level", INFO).Set("bello", "banana")
converted := converter.Convert(record)
suite.Assert().IsType(int64(0), converted.Get("time"))
suite.Assert().Equal(30, converted.Get("level"))
suite.Assert().Equal(1, converted.Get("v"))
// {"level":30,"time":1573664685469,"bello":"banana","v":1}
}
func (suite *ConverterSuite) TestCanConvertWithStackDriverConverter() {
now := time.Now().UTC()
converter := &StackDriverConverter{}
record := converter.Convert(NewRecord().Set("level", INFO).Set("time", now).Set("msg", "Hello World!").Set("v", 0))
suite.Assert().Contains(record.Data, "message")
suite.Assert().NotContains(record.Data, "msg")
suite.Assert().Contains(record.Data, "level")
suite.Assert().Contains(record.Data, "severity")
suite.Assert().Contains(record.Data, "time")
value := record.Get("time")
suite.Require().IsType("string", value)
stime := value.(string)
suite.Assert().Regexp(`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z`, stime)
suite.Assert().Contains(record.Data, "v")
suite.Assert().NotContains(record.Data, "name")
record = converter.Convert(NewRecord().Set("level", NEVER).Set("msg", "Hello World!"))
suite.Assert().Equal(logging.Info, record.Get("severity"))
record = converter.Convert(NewRecord().Set("level", TRACE).Set("msg", "Hello World!"))
suite.Assert().Equal(logging.Debug, record.Get("severity"))
record = converter.Convert(NewRecord().Set("level", DEBUG).Set("msg", "Hello World!"))
suite.Assert().Equal(logging.Debug, record.Get("severity"))
record = converter.Convert(NewRecord().Set("level", WARN).Set("msg", "Hello World!"))
suite.Assert().Equal(logging.Warning, record.Get("severity"))
record = converter.Convert(NewRecord().Set("level", ERROR).Set("msg", "Hello World!"))
suite.Assert().Equal(logging.Error, record.Get("severity"))
record = converter.Convert(NewRecord().Set("level", FATAL).Set("msg", "Hello World!"))
suite.Assert().Equal(logging.Critical, record.Get("severity"))
record = converter.Convert(NewRecord().Set("level", ALWAYS).Set("msg", "Hello World!"))
suite.Assert().Equal(logging.Emergency, record.Get("severity"))
record = converter.Convert(NewRecord().Set("msg", "Hello World!"))
suite.Assert().Equal(logging.Info, record.Get("severity"))
}
func (suite *ConverterSuite) TestCanConvertWithCloudWatchConverter() {
now := time.Now().UTC()
converter := &CloudWatchConverter{}
record := converter.Convert(NewRecord().Set("level", INFO).Set("time", now).Set("msg", "Hello World!").Set("v", 0))
suite.Assert().Contains(record.Data, "level")
suite.Assert().Contains(record.Data, "msg")
suite.Assert().Contains(record.Data, "time")
suite.Assert().IsType("string", record.Get("time"))
suite.Assert().Contains(record.Data, "v")
suite.Assert().NotContains(record.Data, "name")
record = converter.Convert(NewRecord().Set("level", NEVER).Set("msg", "Hello World!"))
suite.Assert().Equal(NEVER.String(), record.Get("severity"))
suite.Assert().Equal(NEVER, record.Get("level"))
record = converter.Convert(NewRecord().Set("level", TRACE).Set("msg", "Hello World!"))
suite.Assert().Equal(TRACE.String(), record.Get("severity"))
suite.Assert().Equal(TRACE, record.Get("level"))
record = converter.Convert(NewRecord().Set("level", DEBUG).Set("msg", "Hello World!"))
suite.Assert().Equal(DEBUG.String(), record.Get("severity"))
suite.Assert().Equal(DEBUG, record.Get("level"))
record = converter.Convert(NewRecord().Set("level", INFO).Set("msg", "Hello World!"))
suite.Assert().Equal(INFO.String(), record.Get("severity"))
suite.Assert().Equal(INFO, record.Get("level"))
record = converter.Convert(NewRecord().Set("level", WARN).Set("msg", "Hello World!"))
suite.Assert().Equal(WARN.String(), record.Get("severity"))
suite.Assert().Equal(WARN, record.Get("level"))
record = converter.Convert(NewRecord().Set("level", ERROR).Set("msg", "Hello World!"))
suite.Assert().Equal(ERROR.String(), record.Get("severity"))
suite.Assert().Equal(ERROR, record.Get("level"))
record = converter.Convert(NewRecord().Set("level", FATAL).Set("msg", "Hello World!"))
suite.Assert().Equal(FATAL.String(), record.Get("severity"))
suite.Assert().Equal(FATAL, record.Get("level"))
record = converter.Convert(NewRecord().Set("level", ALWAYS).Set("msg", "Hello World!"))
suite.Assert().Equal(ALWAYS.String(), record.Get("severity"))
suite.Assert().Equal(ALWAYS, record.Get("level"))
}