| 1 |
// Copyright (c) 2016-2022 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 |
| 22 |
|
| 23 |
import ( |
| 24 |
"errors" |
| 25 |
"io" |
| 26 |
"io/fs" |
| 27 |
"net/url" |
| 28 |
"os" |
| 29 |
"path/filepath" |
| 30 |
"testing" |
| 31 |
|
| 32 |
"github.com/stretchr/testify/assert" |
| 33 |
"github.com/stretchr/testify/require" |
| 34 |
"go.uber.org/multierr" |
| 35 |
"go.uber.org/zap/zapcore" |
| 36 |
) |
| 37 |
|
| 38 |
func TestOpenNoPaths(t *testing.T) { |
| 39 |
ws, cleanup, err := Open() |
| 40 |
defer cleanup() |
| 41 |
|
| 42 |
assert.NoError(t, err, "Expected opening no paths to succeed.") |
| 43 |
assert.Equal( |
| 44 |
t, |
| 45 |
zapcore.AddSync(io.Discard), |
| 46 |
ws, |
| 47 |
"Expected opening no paths to return a no-op WriteSyncer.", |
| 48 |
) |
| 49 |
} |
| 50 |
|
| 51 |
func TestOpen(t *testing.T) { |
| •52 |
t.Skip() |
| 53 |
tempName := filepath.Join(t.TempDir(), "test.log") |
| 54 |
assert.False(t, fileExists(tempName)) |
| 55 |
require.True(t, filepath.IsAbs(tempName), "Expected absolute temp file path.") |
| 56 |
|
| 57 |
tests := []struct { |
| 58 |
msg string |
| 59 |
paths []string |
| 60 |
}{ |
| 61 |
{ |
| 62 |
msg: "stdout", |
| 63 |
paths: []string{"stdout"}, |
| 64 |
}, |
| 65 |
{ |
| 66 |
msg: "stderr", |
| 67 |
paths: []string{"stderr"}, |
| 68 |
}, |
| 69 |
{ |
| 70 |
msg: "temp file path only", |
| 71 |
paths: []string{tempName}, |
| 72 |
}, |
| 73 |
{ |
| 74 |
msg: "temp file file scheme", |
| 75 |
paths: []string{"file://" + tempName}, |
| 76 |
}, |
| 77 |
{ |
| 78 |
msg: "temp file with file scheme and host localhost", |
| 79 |
paths: []string{"file://localhost" + tempName}, |
| 80 |
}, |
| 81 |
} |
| 82 |
|
| 83 |
for _, tt := range tests { |
| 84 |
t.Run(tt.msg, func(t *testing.T) { |
| 85 |
_, cleanup, err := Open(tt.paths...) |
| 86 |
if err == nil { |
| 87 |
defer cleanup() |
| 88 |
} |
| 89 |
|
| 90 |
assert.NoError(t, err, "Unexpected error opening paths %v.", tt.paths) |
| 91 |
}) |
| 92 |
} |
| 93 |
|
| 94 |
assert.True(t, fileExists(tempName)) |
| 95 |
} |
| 96 |
|
| 97 |
func TestOpenPathsNotFound(t *testing.T) { |
| 98 |
tempName := filepath.Join(t.TempDir(), "test.log") |
| 99 |
|
| 100 |
tests := []struct { |
| 101 |
msg string |
| 102 |
paths []string |
| 103 |
wantNotFoundPaths []string |
| 104 |
}{ |
| 105 |
{ |
| 106 |
msg: "missing path", |
| 107 |
paths: []string{"/foo/bar/baz"}, |
| 108 |
wantNotFoundPaths: []string{"/foo/bar/baz"}, |
| 109 |
}, |
| 110 |
{ |
| 111 |
msg: "missing file scheme url with host localhost", |
| 112 |
paths: []string{"file://localhost/foo/bar/baz"}, |
| 113 |
wantNotFoundPaths: []string{"/foo/bar/baz"}, |
| 114 |
}, |
| 115 |
{ |
| 116 |
msg: "multiple paths", |
| 117 |
paths: []string{"stdout", "/foo/bar/baz", tempName, "file:///baz/quux"}, |
| 118 |
wantNotFoundPaths: []string{ |
| 119 |
"/foo/bar/baz", |
| 120 |
"/baz/quux", |
| 121 |
}, |
| 122 |
}, |
| 123 |
} |
| 124 |
|
| 125 |
for _, tt := range tests { |
| 126 |
t.Run(tt.msg, func(t *testing.T) { |
| 127 |
_, cleanup, err := Open(tt.paths...) |
| 128 |
if !assert.Error(t, err, "Open must fail.") { |
| 129 |
cleanup() |
| 130 |
return |
| 131 |
} |
| 132 |
|
| 133 |
errs := multierr.Errors(err) |
| 134 |
require.Len(t, errs, len(tt.wantNotFoundPaths)) |
| 135 |
for i, err := range errs { |
| 136 |
assert.ErrorIs(t, err, fs.ErrNotExist) |
| 137 |
assert.ErrorContains(t, err, tt.wantNotFoundPaths[i], "missing path in error") |
| 138 |
} |
| 139 |
}) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func TestOpenRelativePath(t *testing.T) { |
| 144 |
const name = "test-relative-path.txt" |
| 145 |
|
| 146 |
require.False(t, fileExists(name), "Test file already exists.") |
| 147 |
s, cleanup, err := Open(name) |
| 148 |
require.NoError(t, err, "Open failed.") |
| 149 |
defer func() { |
| 150 |
err := os.Remove(name) |
| 151 |
if !t.Failed() { |
| 152 |
// If the test has already failed, we probably didn't create this file. |
| 153 |
require.NoError(t, err, "Deleting test file failed.") |
| 154 |
} |
| 155 |
}() |
| 156 |
defer cleanup() |
| 157 |
|
| 158 |
_, err = s.Write([]byte("test")) |
| 159 |
assert.NoError(t, err, "Write failed.") |
| 160 |
assert.True(t, fileExists(name), "Didn't create file for relative path.") |
| 161 |
} |
| 162 |
|
| 163 |
func TestOpenFails(t *testing.T) { |
| 164 |
tests := []struct { |
| 165 |
paths []string |
| 166 |
}{ |
| 167 |
{paths: []string{"./non-existent-dir/file"}}, // directory doesn't exist |
| 168 |
{paths: []string{"stdout", "./non-existent-dir/file"}}, // directory doesn't exist |
| 169 |
{paths: []string{"://foo.log"}}, // invalid URL, scheme can't begin with colon |
| 170 |
{paths: []string{"mem://somewhere"}}, // scheme not registered |
| 171 |
} |
| 172 |
|
| 173 |
for _, tt := range tests { |
| 174 |
_, cleanup, err := Open(tt.paths...) |
| 175 |
require.Nil(t, cleanup, "Cleanup function should never be nil") |
| 176 |
assert.Error(t, err, "Open with invalid URL should fail.") |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
func TestOpenOtherErrors(t *testing.T) { |
| 181 |
tempName := filepath.Join(t.TempDir(), "test.log") |
| 182 |
|
| 183 |
tests := []struct { |
| 184 |
msg string |
| 185 |
paths []string |
| 186 |
wantErr string |
| 187 |
}{ |
| 188 |
{ |
| 189 |
msg: "file with unexpected host", |
| 190 |
paths: []string{"file://host01.test.com" + tempName}, |
| 191 |
wantErr: "empty or use localhost", |
| 192 |
}, |
| 193 |
{ |
| 194 |
msg: "file with user on localhost", |
| 195 |
paths: []string{"file://rms@localhost" + tempName}, |
| 196 |
wantErr: "user and password not allowed", |
| 197 |
}, |
| 198 |
{ |
| 199 |
msg: "file url with fragment", |
| 200 |
paths: []string{"file://localhost" + tempName + "#foo"}, |
| 201 |
wantErr: "fragments not allowed", |
| 202 |
}, |
| 203 |
{ |
| 204 |
msg: "file url with query", |
| 205 |
paths: []string{"file://localhost" + tempName + "?foo=bar"}, |
| 206 |
wantErr: "query parameters not allowed", |
| 207 |
}, |
| 208 |
{ |
| 209 |
msg: "file with port", |
| 210 |
paths: []string{"file://localhost:8080" + tempName}, |
| 211 |
wantErr: "ports not allowed", |
| 212 |
}, |
| 213 |
} |
| 214 |
|
| 215 |
for _, tt := range tests { |
| 216 |
t.Run(tt.msg, func(t *testing.T) { |
| 217 |
_, cleanup, err := Open(tt.paths...) |
| 218 |
if !assert.Error(t, err, "Open must fail.") { |
| 219 |
cleanup() |
| 220 |
return |
| 221 |
} |
| 222 |
|
| 223 |
assert.ErrorContains(t, err, tt.wantErr, "Unexpected error opening paths %v.", tt.paths) |
| 224 |
}) |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
type testWriter struct { |
| 229 |
expected string |
| 230 |
t testing.TB |
| 231 |
} |
| 232 |
|
| 233 |
func (w *testWriter) Write(actual []byte) (int, error) { |
| 234 |
assert.Equal(w.t, []byte(w.expected), actual, "Unexpected write error.") |
| 235 |
return len(actual), nil |
| 236 |
} |
| 237 |
|
| 238 |
func (w *testWriter) Sync() error { |
| 239 |
return nil |
| 240 |
} |
| 241 |
|
| 242 |
func TestOpenWithErroringSinkFactory(t *testing.T) { |
| 243 |
stubSinkRegistry(t) |
| 244 |
|
| 245 |
msg := "expected factory error" |
| 246 |
factory := func(_ *url.URL) (Sink, error) { |
| 247 |
return nil, errors.New(msg) |
| 248 |
} |
| 249 |
|
| 250 |
assert.NoError(t, RegisterSink("test", factory), "Failed to register sink factory.") |
| 251 |
_, _, err := Open("test://some/path") |
| 252 |
assert.ErrorContains(t, err, msg) |
| 253 |
} |
| 254 |
|
| 255 |
func TestCombineWriteSyncers(t *testing.T) { |
| 256 |
tw := &testWriter{"test", t} |
| 257 |
w := CombineWriteSyncers(tw) |
| 258 |
_, err := w.Write([]byte("test")) |
| 259 |
assert.NoError(t, err, "Unexpected write error.") |
| 260 |
} |
| 261 |
|
| 262 |
func fileExists(name string) bool { |
| 263 |
if _, err := os.Stat(name); os.IsNotExist(err) { |
| 264 |
return false |
| 265 |
} |
| 266 |
return true |
| 267 |
} |