-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllogger_test.go
284 lines (234 loc) · 7.9 KB
/
llogger_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
277
278
279
280
281
282
283
284
// Package llogger simplifies printing messages to CloudWatch logs from AWS Lambda.
package llogger
import (
"bytes"
"context"
"encoding/json"
"io"
"os"
"strings"
"testing"
"time"
)
const fileName = "llogger_test.go"
type message1 struct {
Time int64 `json:"time"`
Service string `json:"service"`
Env string `json:"env"`
Version string `json:"version"`
LogLevel string `json:"loglevel"`
Message string `json:"message"`
Duration float64 `json:"duration"`
TimeLeft float64 `json:"timeLeft"`
Resource resource `json:"resource"`
Extra string `json:"extra"`
}
type message2 struct {
Time int64 `json:"custom-time"`
Service string `json:"service"`
Env string `json:"env"`
Version string `json:"version"`
LogLevel string `json:"custom-loglevel"`
Message string `json:"custom-message"`
Duration float64 `json:"custom-duration"`
TimeLeft float64 `json:"custom-timeLeft"`
Resource resource `json:"custom-resource"`
}
type message3 struct {
Time string `json:"time"`
LogLevel string `json:"loglevel"`
Message string `json:"message"`
Resource resource `json:"resource"`
}
var (
startTime = time.Now().UTC()
funcName1 = "github.com/nuttmeister/llogger.Test"
)
// Test will test all the coverage on the logger.go file.
func Test(t *testing.T) {
// Create a context with time slightly after Test start.
now := time.Now().UTC()
ctx, cancel := context.WithDeadline(context.Background(), now.Add(time.Duration(3*time.Second)))
// Create lloggers.
client1 := Create(ctx, Input{
"service": "llogger-test",
"env": "test",
"version": "1.0.0",
"llogger-tf": "Unix",
"llogger-tfn": 1,
"llogger-llfn": 2,
"llogger-mfn": 3,
"llogger-dfn": 4,
"llogger-tlfn": 5,
"llogger-rfn": 6,
"llogger-wm": 7,
"llogger-cm": 8,
})
client2 := Create(nil, Input{
"service": "llogger-test",
"env": "test",
"version": "1.0.0",
"llogger-tfn": "custom-time",
"llogger-tf": "UnixNano",
"llogger-llfn": "custom-loglevel",
"llogger-mfn": "custom-message",
"llogger-dfn": "custom-duration",
"llogger-tlfn": "custom-timeLeft",
"llogger-rfn": "custom-resource",
"llogger-wm": "custom-warning",
"llogger-cm": "custom-error",
"llogger-prefix": "prefix: ",
"llogger-suffix": " suffix",
})
client3 := Create(nil, nil)
client4 := Create(context.Background(), nil)
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Couldn't create new Pipe files. Error %s", err.Error())
}
os.Stdout = w
// Print 3 messages with the 3 different clients.
client1.Print(Input{"loglevel": "verbose", "message": "Testmessage1", "extra": "extra test data"})
client2.Print(Input{"custom-loglevel": "custom-warning", "custom-message": "Testmessage2"})
client3.Print(Input{"loglevel": "error", "message": "Testmessage3"})
client4.Print(Input{"this-should-fail": func() string { return "did-we-fail?" }})
raw := make(chan []byte)
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
raw <- buf.Bytes()
}()
w.Close()
// Get result from stdout.
strs := strings.Split(string(<-raw), "\n")
// Check that strs has length of 4 and that last str is a blank line.
switch {
case len(strs) != 5:
t.Fatalf("Expected slice length from stdout to be 5 but got %d", len(strs))
case strs[4] != "":
t.Fatalf("Exepected last slice string from stdout to be a blank str but got %s", strs[4])
}
// Test msg outputs
msg1(strs[0], t)
msg2(strs[1], t)
msg3(strs[2], t)
msg4(strs[3], t)
cancel()
}
// Check that msg1 is correct.
func msg1(raw string, t *testing.T) {
// Unmarshal Message
msg := &message1{}
if err := json.Unmarshal([]byte(raw), msg); err != nil {
t.Fatalf("Couldn't unmarshal the message in msg1. Error %s", err.Error())
}
switch {
// Check for correct loglevel.
case msg.LogLevel != "verbose":
t.Fatalf("loglevel in msg1 not error")
// Check for correct message.
case msg.Message != "Testmessage1":
t.Fatalf("message in msg1 not Testmessage1")
// Check that time.Now().UnixNano() is higher
case time.Now().Unix() < msg.Time:
t.Fatalf("time in msg1 is in the future")
// Check for correct service.
case msg.Service != "llogger-test":
t.Fatalf("service in msg1 not llogger-test")
// Check for correct env.
case msg.Env != "test":
t.Fatalf("env in msg1 not test")
// Check for correct version.
case msg.Version != "1.0.0":
t.Fatalf("version in msg1 not 1.0.0")
// Check filename of function.
case !strings.Contains(msg.Resource.File, fileName):
t.Fatalf("Expected Filename in msg1 to include %s but got %s", fileName, msg.Resource.File)
// Check function name.
case msg.Resource.Function != funcName1:
t.Fatalf("Expected Function in msg1 to be %s but got %s", funcName1, msg.Resource.Function)
// Check time left.
case msg.TimeLeft < 2.9 || msg.TimeLeft > 3.0:
t.Fatalf("Expected TimeLeft in msg1 to be between 2.9 and 3.0 seconds. But got %f", msg.TimeLeft)
// Check Extra Data
case msg.Extra != "extra test data":
t.Fatalf("extra in msg1 not extra test data")
}
}
// Check that msg2 is correct.
func msg2(raw string, t *testing.T) {
// Unmarshal Message
msg := &message2{}
if err := json.Unmarshal([]byte(raw[8:len(raw)-7]), msg); err != nil {
t.Fatalf("Couldn't unmarshal the message in msg2. Error %s", err.Error())
}
switch {
// Check for correct prefix
case raw[0:8] != "prefix: ":
t.Fatalf("prefix wasn't 'prefix: ' in msg2")
// Check for correct suffix.
case raw[len(raw)-7:] != " suffix":
t.Fatalf("suffix wasn't ' suffix' in msg2")
// Check for correct loglevel.
case msg.LogLevel != "custom-warning":
t.Fatalf("loglevel in msg2 not custom-warning")
// Check for correct message.
case msg.Message != "Testmessage2":
t.Fatalf("message in msg2 not Testmessage2")
// Check that time.Now().UnixNano() is higher
case time.Now().UnixNano() < msg.Time:
t.Fatalf("time in msg2 is in the future")
// Check for correct service.
case msg.Service != "llogger-test":
t.Fatalf("service in msg2 not llogger-test")
// Check for correct env.
case msg.Env != "test":
t.Fatalf("env in msg2 not test")
// Check for correct version.
case msg.Version != "1.0.0":
t.Fatalf("version in msg2 not 1.0.0")
// Check filename of function.
case !strings.Contains(msg.Resource.File, fileName):
t.Fatalf("Expected Filename in msg2 to include %s but got %s", fileName, msg.Resource.File)
// Check function name.
case msg.Resource.Function != funcName1:
t.Fatalf("Expected Function in msg2 to be %s but got %s", funcName1, msg.Resource.Function)
}
}
// Check that msg3 is correct.
func msg3(raw string, t *testing.T) {
// Unmarshal Message
msg := &message3{}
if err := json.Unmarshal([]byte(raw), msg); err != nil {
t.Fatalf("Couldn't unmarshal the message in msg3. Error %s", err.Error())
}
// Parse the time in Message
msgTime, err := time.Parse("2006-01-02 15:04:05.999999", msg.Time)
if err != nil {
t.Fatalf("Couldn't parse time in message in msg3. Error %s", err.Error())
}
switch {
// Check for correct loglevel.
case msg.LogLevel != "error":
t.Fatalf("loglevel in msg3 not error")
// Check for correct message.
case msg.Message != "Testmessage3":
t.Fatalf("message in msg3 not Testmessage3")
// Check that time is after starttime.
case msgTime.Before(startTime):
t.Fatalf("Time in msg3 was before start time of test! Time: %s, Test start time: %s",
msgTime.String(), startTime.String())
// Check filename of function.
case !strings.Contains(msg.Resource.File, fileName):
t.Fatalf("Expected Filename in msg3 to include %s but got %s", fileName, msg.Resource.File)
// Check function name.
case msg.Resource.Function != funcName1:
t.Fatalf("Expected Function in msg3 to be %s but got %s", funcName1, msg.Resource.Function)
}
}
// Check that msg4 is correct.
func msg4(raw string, t *testing.T) {
if !strings.Contains(raw, "Couldn't JSON marshal the error message") {
t.Fatalf("Expected JSON Marshal to fail in msg4. But got %s", raw)
}
}