|
0 |
// Copyright (c) 2016, 2017 Uber Technologies, Inc.
|
|
1 |
//
|
|
2 |
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
3 |
// of this software and associated documentation files (the "Software"), to deal
|
|
4 |
// in the Software without restriction, including without limitation the rights
|
|
5 |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
6 |
// copies of the Software, and to permit persons to whom the Software is
|
|
7 |
// furnished to do so, subject to the following conditions:
|
|
8 |
//
|
|
9 |
// The above copyright notice and this permission notice shall be included in
|
|
10 |
// all copies or substantial portions of the Software.
|
|
11 |
//
|
|
12 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
13 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
14 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
15 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
16 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
17 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
18 |
// THE SOFTWARE.
|
|
19 |
|
|
20 |
package zap_test
|
|
21 |
|
|
22 |
import (
|
|
23 |
"bytes"
|
|
24 |
"io/ioutil"
|
|
25 |
"os"
|
|
26 |
"os/exec"
|
|
27 |
"path/filepath"
|
|
28 |
"runtime"
|
|
29 |
"strings"
|
|
30 |
"testing"
|
|
31 |
|
|
32 |
"go.uber.org/zap"
|
|
33 |
"go.uber.org/zap/zapcore"
|
|
34 |
|
|
35 |
"github.com/stretchr/testify/assert"
|
|
36 |
"github.com/stretchr/testify/require"
|
|
37 |
)
|
|
38 |
|
|
39 |
// _zapPackages are packages that we search for in the logging output to match a
|
|
40 |
// zap stack frame. It is different from _zapStacktracePrefixes which is only
|
|
41 |
// intended to match on the function name, while this is on the full output
|
|
42 |
// which includes filenames.
|
|
43 |
var _zapPackages = []string{
|
|
44 |
"go.uber.org/zap.",
|
|
45 |
"go.uber.org/zap/zapcore.",
|
|
46 |
}
|
|
47 |
|
|
48 |
func TestStacktraceFiltersZapLog(t *testing.T) {
|
|
49 |
withLogger(t, func(logger *zap.Logger, out *bytes.Buffer) {
|
|
50 |
logger.Error("test log")
|
|
51 |
logger.Sugar().Error("sugar test log")
|
|
52 |
|
|
53 |
require.Contains(t, out.String(), "TestStacktraceFiltersZapLog", "Should not strip out non-zap import")
|
|
54 |
verifyNoZap(t, out.String())
|
|
55 |
})
|
|
56 |
}
|
|
57 |
|
|
58 |
func TestStacktraceFiltersZapMarshal(t *testing.T) {
|
|
59 |
withLogger(t, func(logger *zap.Logger, out *bytes.Buffer) {
|
|
60 |
marshal := func(enc zapcore.ObjectEncoder) error {
|
|
61 |
logger.Warn("marshal caused warn")
|
|
62 |
enc.AddString("f", "v")
|
|
63 |
return nil
|
|
64 |
}
|
|
65 |
logger.Error("test log", zap.Object("obj", zapcore.ObjectMarshalerFunc(marshal)))
|
|
66 |
|
|
67 |
logs := out.String()
|
|
68 |
|
|
69 |
// The marshal function (which will be under the test function) should not be stripped.
|
|
70 |
const marshalFnPrefix = "TestStacktraceFiltersZapMarshal."
|
|
71 |
require.Contains(t, logs, marshalFnPrefix, "Should not strip out marshal call")
|
|
72 |
|
|
73 |
// There should be no zap stack traces before that point.
|
|
74 |
marshalIndex := strings.Index(logs, marshalFnPrefix)
|
|
75 |
verifyNoZap(t, logs[:marshalIndex])
|
|
76 |
|
|
77 |
// After that point, there should be zap stack traces - we don't want to strip out
|
|
78 |
// the Marshal caller information.
|
|
79 |
for _, fnPrefix := range _zapPackages {
|
|
80 |
require.Contains(t, logs[marshalIndex:], fnPrefix, "Missing zap caller stack for Marshal")
|
|
81 |
}
|
|
82 |
})
|
|
83 |
}
|
|
84 |
|
|
85 |
func TestStacktraceFiltersVendorZap(t *testing.T) {
|
|
86 |
// We need to simulate a zap as a vendor library, so we're going to create a fake GOPATH
|
|
87 |
// and run the above test which will contain zap in the vendor directory.
|
|
88 |
withGoPath(t, func(goPath string) {
|
|
89 |
curDir, err := os.Getwd()
|
|
90 |
require.NoError(t, err, "Failed to get current directory")
|
|
91 |
|
|
92 |
testDir := filepath.Join(goPath, "src/go.uber.org/zap_test/")
|
|
93 |
vendorDir := filepath.Join(testDir, "vendor")
|
|
94 |
require.NoError(t, os.MkdirAll(testDir, 0777), "Failed to create source director")
|
|
95 |
|
|
96 |
curFile := getSelfFilename(t)
|
|
97 |
//copyFile(t, curFile, filepath.Join(testDir, curFile))
|
|
98 |
setupSymlink(t, curFile, filepath.Join(testDir, curFile))
|
|
99 |
|
|
100 |
// Set up symlinks for zap, and for any test dependencies.
|
|
101 |
setupSymlink(t, curDir, filepath.Join(vendorDir, "go.uber.org/zap"))
|
|
102 |
for _, testDep := range []string{"github.com/stretchr/testify"} {
|
|
103 |
setupSymlink(t, filepath.Join(curDir, "vendor", testDep), filepath.Join(vendorDir, testDep))
|
|
104 |
}
|
|
105 |
|
|
106 |
// Now run the above test which ensures we filter out zap stacktraces, but this time
|
|
107 |
// zap is in a vendor
|
|
108 |
cmd := exec.Command("go", "test", "-v", "-run", "TestStacktraceFiltersZap")
|
|
109 |
cmd.Dir = testDir
|
|
110 |
out, err := cmd.CombinedOutput()
|
|
111 |
require.NoError(t, err, "Failed to run test in vendor directory, output: %s", out)
|
|
112 |
assert.Contains(t, string(out), "PASS")
|
|
113 |
})
|
|
114 |
}
|
|
115 |
|
|
116 |
// withLogger sets up a logger with a real encoder set up, so that any marshal functions are called.
|
|
117 |
// The inbuilt observer does not call Marshal for objects/arrays, which we need for some tests.
|
|
118 |
func withLogger(t *testing.T, fn func(logger *zap.Logger, out *bytes.Buffer)) {
|
|
119 |
buf := &bytes.Buffer{}
|
|
120 |
encoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
|
|
121 |
core := zapcore.NewCore(encoder, zapcore.AddSync(buf), zapcore.DebugLevel)
|
|
122 |
logger := zap.New(core, zap.AddStacktrace(zap.DebugLevel))
|
|
123 |
fn(logger, buf)
|
|
124 |
}
|
|
125 |
|
|
126 |
func verifyNoZap(t *testing.T, logs string) {
|
|
127 |
for _, fnPrefix := range _zapPackages {
|
|
128 |
require.NotContains(t, logs, fnPrefix, "Should not strip out marshal call")
|
|
129 |
}
|
|
130 |
}
|
|
131 |
|
|
132 |
func withGoPath(t *testing.T, f func(goPath string)) {
|
|
133 |
goPath, err := ioutil.TempDir("", "gopath")
|
|
134 |
require.NoError(t, err, "Failed to create temporary directory for GOPATH")
|
|
135 |
//defer os.RemoveAll(goPath)
|
|
136 |
|
|
137 |
os.Setenv("GOPATH", goPath)
|
|
138 |
defer os.Setenv("GOPATH", os.Getenv("GOPATH"))
|
|
139 |
|
|
140 |
f(goPath)
|
|
141 |
}
|
|
142 |
|
|
143 |
func getSelfFilename(t *testing.T) string {
|
|
144 |
_, file, _, ok := runtime.Caller(0)
|
|
145 |
require.True(t, ok, "Failed to get caller information to identify local file")
|
|
146 |
|
|
147 |
return filepath.Base(file)
|
|
148 |
}
|
|
149 |
|
|
150 |
func setupSymlink(t *testing.T, src, dst string) {
|
|
151 |
// Make sure the destination directory exists.
|
|
152 |
os.MkdirAll(filepath.Dir(dst), 0777)
|
|
153 |
|
|
154 |
// Get absolute path of the source for the symlink, otherwise we can create a symlink
|
|
155 |
// that uses relative paths.
|
|
156 |
srcAbs, err := filepath.Abs(src)
|
|
157 |
require.NoError(t, err, "Failed to get absolute path")
|
|
158 |
|
|
159 |
require.NoError(t, os.Symlink(srcAbs, dst), "Failed to set up symlink")
|
|
160 |
}
|