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