|
1 | 1 | package logging |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "github.com/pkg/errors" |
4 | 6 | "github.com/stretchr/testify/require" |
| 7 | + "go.uber.org/zap" |
| 8 | + "go.uber.org/zap/zapcore" |
5 | 9 | "regexp" |
6 | 10 | "testing" |
7 | 11 | ) |
@@ -39,3 +43,113 @@ func Test_journaldFieldEncode(t *testing.T) { |
39 | 43 | }) |
40 | 44 | } |
41 | 45 | } |
| 46 | + |
| 47 | +// testingStackError is an error mimicking the stack behavior from github.com/pkg/errors in a deterministic way. |
| 48 | +type testingStackError string |
| 49 | + |
| 50 | +func (err testingStackError) Error() string { |
| 51 | + return string(err) |
| 52 | +} |
| 53 | + |
| 54 | +func (err testingStackError) Format(s fmt.State, verb rune) { |
| 55 | + if verb == 'v' && s.Flag('+') { |
| 56 | + _, _ = fmt.Fprintf(s, "%s: look, I am a stack trace", string(err)) |
| 57 | + } else { |
| 58 | + _, _ = fmt.Fprintf(s, "%s", string(err)) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func Test_visibleFieldsMsg(t *testing.T) { |
| 63 | + tests := []struct { |
| 64 | + name string |
| 65 | + visibleFieldKeys map[string]struct{} |
| 66 | + fields []zapcore.Field |
| 67 | + output string |
| 68 | + }{ |
| 69 | + { |
| 70 | + name: "empty-all-nil", |
| 71 | + visibleFieldKeys: nil, |
| 72 | + fields: nil, |
| 73 | + output: "", |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "empty-all", |
| 77 | + visibleFieldKeys: map[string]struct{}{}, |
| 78 | + fields: nil, |
| 79 | + output: "", |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "empty-visibleFiledKeys", |
| 83 | + visibleFieldKeys: map[string]struct{}{}, |
| 84 | + fields: []zapcore.Field{zap.String("foo", "bar")}, |
| 85 | + output: "", |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "no-field-match", |
| 89 | + visibleFieldKeys: map[string]struct{}{"bar": {}}, |
| 90 | + fields: []zapcore.Field{zap.String("foo", "bar")}, |
| 91 | + output: "", |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "expected-string", |
| 95 | + visibleFieldKeys: map[string]struct{}{"foo": {}}, |
| 96 | + fields: []zapcore.Field{zap.String("foo", "bar")}, |
| 97 | + output: "\t" + `foo="bar"`, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "expected-multiple-strings-with-excluded", |
| 101 | + visibleFieldKeys: map[string]struct{}{"foo": {}, "bar": {}}, |
| 102 | + fields: []zapcore.Field{ |
| 103 | + zap.String("foo", "bar"), |
| 104 | + zap.String("bar", "baz"), |
| 105 | + zap.String("baz", "qux"), // not in allow list |
| 106 | + }, |
| 107 | + output: "\t" + `bar="baz", foo="bar"`, |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "expected-error-simple", |
| 111 | + visibleFieldKeys: map[string]struct{}{"error": {}}, |
| 112 | + fields: []zapcore.Field{zap.Error(fmt.Errorf("oops"))}, |
| 113 | + output: "\t" + `error="oops"`, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "expected-error-without-stack", |
| 117 | + visibleFieldKeys: map[string]struct{}{"error": {}}, |
| 118 | + fields: []zapcore.Field{zap.Error(errors.WithStack(fmt.Errorf("oops")))}, |
| 119 | + output: "\t" + `error="oops"`, |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "expected-error-with-stack", |
| 123 | + visibleFieldKeys: map[string]struct{}{"error": {}, "errorVerbose": {}}, |
| 124 | + fields: []zapcore.Field{zap.Error(testingStackError("oops"))}, |
| 125 | + output: "\t" + `error="oops", errorVerbose="oops: look, I am a stack trace"`, |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "expected-multiple-basic-types", |
| 129 | + visibleFieldKeys: map[string]struct{}{ |
| 130 | + "bool": {}, |
| 131 | + "int": {}, |
| 132 | + "rune": {}, |
| 133 | + "float": {}, |
| 134 | + "complex": {}, |
| 135 | + "byte-string": {}, |
| 136 | + }, |
| 137 | + fields: []zapcore.Field{ |
| 138 | + zap.Bool("bool", true), |
| 139 | + zap.ByteString("byte-string", []byte{0xC0, 0xFF, 0xEE}), |
| 140 | + zap.Complex64("complex", -1i), |
| 141 | + zap.Float64("float", 1.0/3.0), |
| 142 | + zap.Int("int", 42), |
| 143 | + zap.Any("rune", 'A'), |
| 144 | + }, |
| 145 | + output: "\t" + `bool="true", byte-string="\xc0\xff\xee", complex="(0-1i)", float="0.3333333333333333", int="42", rune='A'`, |
| 146 | + }, |
| 147 | + } |
| 148 | + |
| 149 | + for _, test := range tests { |
| 150 | + t.Run(test.name, func(t *testing.T) { |
| 151 | + out := visibleFieldsMsg(test.visibleFieldKeys, test.fields) |
| 152 | + require.Equal(t, test.output, out) |
| 153 | + }) |
| 154 | + } |
| 155 | +} |
0 commit comments