/
log_test.go
188 lines (162 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
180
181
182
183
184
185
186
187
188
// Copyright 2019 Aporeto Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logutils
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)
// captureOutAndErr executes a function and returns captured os.Stdout and os.Stderr during this execution.
func captureOutAndErr(f func()) (o, e string) {
// Create output and error capture pipes
ereader, ewriter, err := os.Pipe()
if err != nil {
panic(err)
}
oreader, owriter, err := os.Pipe()
if err != nil {
panic(err)
}
// Store original stdout and stderr and restore them by defer call.
stdout := os.Stdout
stderr := os.Stderr
defer func() {
os.Stdout = stdout
os.Stderr = stderr
}()
// Hijack stdout and stderr
os.Stdout = owriter
os.Stderr = ewriter
// Setup capture funcs
oout := make(chan string)
eout := make(chan string)
wg := new(sync.WaitGroup)
wg.Add(2)
go func() {
var buf bytes.Buffer
wg.Done()
io.Copy(&buf, oreader)
oout <- buf.String()
}()
go func() {
var buf bytes.Buffer
wg.Done()
io.Copy(&buf, ereader)
eout <- buf.String()
}()
wg.Wait()
// Execute function
f()
owriter.Close()
ewriter.Close()
// Return captures
return <-oout, <-eout
}
func TestConfigureWithOptions(t *testing.T) {
type args struct {
level string
format string
file string
fileOnly bool
prettyTimestamp bool
}
tests := []struct {
name string
args args
iterations int
want string
}{
{
name: "no file logging",
args: args{
"info",
"json",
"",
false,
false,
},
iterations: 20,
want: "",
},
{
name: "file only",
args: args{
"info",
"json",
"/tmp/some-log-file",
true,
false,
},
iterations: 20,
want: "",
},
{
name: "tee file logging and stdout with wraps in files",
args: args{
"info",
"json",
"/tmp/some-tee-log-file",
false,
false,
},
iterations: 160 * 1024,
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
minBytesPrinted := 0
ro, re := captureOutAndErr(func() {
ConfigureWithOptions(tt.args.level, tt.args.format, tt.args.file, tt.args.fileOnly, tt.args.prettyTimestamp)
for i := 0; i < tt.iterations; i++ {
buf := fmt.Sprintf("%80d - hello", i)
minBytesPrinted += len(buf)
zap.L().Info(buf)
}
})
// On fileOnly case, nothing is expected on stderr as well.
if tt.args.fileOnly {
minBytesPrinted = 0
}
// validate nothing is printed on stdout
assert.Equal(t, 0, len(ro))
// validate we have printed more than minBytesPrinted on stderr
assert.LessOrEqual(t, minBytesPrinted, len(re))
if tt.args.file != "" {
numFilesExpected := 1
if minBytesPrinted > logFileSizeDefault*1024*1024 {
numFilesExpected += logFileNumBackups
}
// Wait for one second as file may not have been deleted.
time.Sleep(time.Second)
files, err := filepath.Glob(tt.args.file + "*")
assert.ObjectsAreEqual(err, nil)
// logging to files tests wraparound. we should have
assert.Equal(t, numFilesExpected, len(files))
for _, f := range files {
fi, err := os.Stat(f)
assert.ObjectsAreEqual(err, nil)
// If we printed into a file, the file should be less than wrapped size.
assert.Less(t, fi.Size(), int64(logFileSizeDefault*1024*1024))
err = os.Remove(f)
assert.ObjectsAreEqual(err, nil)
}
}
})
}
}