summaryrefslogtreecommitdiffstats
path: root/dependencies/pkg/mod/go.uber.org/zap@v1.23.0/writer_test.go
blob: c2f4392462652afae2c3f3f136b46cd3c641d9b7 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright (c) 2016-2022 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package zap

import (
	"errors"
	"io"
	"io/fs"
	"net/url"
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"go.uber.org/multierr"
	"go.uber.org/zap/zapcore"
)

func TestOpenNoPaths(t *testing.T) {
	ws, cleanup, err := Open()
	defer cleanup()

	assert.NoError(t, err, "Expected opening no paths to succeed.")
	assert.Equal(
		t,
		zapcore.AddSync(io.Discard),
		ws,
		"Expected opening no paths to return a no-op WriteSyncer.",
	)
}

func TestOpen(t *testing.T) {
	tempName := filepath.Join(t.TempDir(), "test.log")
	assert.False(t, fileExists(tempName))
	require.True(t, filepath.IsAbs(tempName), "Expected absolute temp file path.")

	tests := []struct {
		msg   string
		paths []string
	}{
		{
			msg:   "stdout",
			paths: []string{"stdout"},
		},
		{
			msg:   "stderr",
			paths: []string{"stderr"},
		},
		{
			msg:   "temp file path only",
			paths: []string{tempName},
		},
		{
			msg:   "temp file file scheme",
			paths: []string{"file://" + tempName},
		},
		{
			msg:   "temp file with file scheme and host localhost",
			paths: []string{"file://localhost" + tempName},
		},
	}

	for _, tt := range tests {
		t.Run(tt.msg, func(t *testing.T) {
			_, cleanup, err := Open(tt.paths...)
			if err == nil {
				defer cleanup()
			}

			assert.NoError(t, err, "Unexpected error opening paths %v.", tt.paths)
		})
	}

	assert.True(t, fileExists(tempName))
	os.Remove(tempName)
}

func TestOpenPathsNotFound(t *testing.T) {
	tempName := filepath.Join(t.TempDir(), "test.log")

	tests := []struct {
		msg               string
		paths             []string
		wantNotFoundPaths []string
	}{
		{
			msg:               "missing path",
			paths:             []string{"/foo/bar/baz"},
			wantNotFoundPaths: []string{"/foo/bar/baz"},
		},
		{
			msg:               "missing file scheme url with host localhost",
			paths:             []string{"file://localhost/foo/bar/baz"},
			wantNotFoundPaths: []string{"/foo/bar/baz"},
		},
		{
			msg:   "multiple paths",
			paths: []string{"stdout", "/foo/bar/baz", tempName, "file:///baz/quux"},
			wantNotFoundPaths: []string{
				"/foo/bar/baz",
				"/baz/quux",
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.msg, func(t *testing.T) {
			_, cleanup, err := Open(tt.paths...)
			if !assert.Error(t, err, "Open must fail.") {
				cleanup()
				return
			}

			errs := multierr.Errors(err)
			require.Len(t, errs, len(tt.wantNotFoundPaths))
			for i, err := range errs {
				assert.ErrorIs(t, err, fs.ErrNotExist)
				assert.ErrorContains(t, err, tt.wantNotFoundPaths[i], "missing path in error")
			}
		})
	}
}

func TestOpenRelativePath(t *testing.T) {
	const name = "test-relative-path.txt"

	require.False(t, fileExists(name), "Test file already exists.")
	s, cleanup, err := Open(name)
	require.NoError(t, err, "Open failed.")
	defer func() {
		err := os.Remove(name)
		if !t.Failed() {
			// If the test has already failed, we probably didn't create this file.
			require.NoError(t, err, "Deleting test file failed.")
		}
	}()
	defer cleanup()

	_, err = s.Write([]byte("test"))
	assert.NoError(t, err, "Write failed.")
	assert.True(t, fileExists(name), "Didn't create file for relative path.")
}

func TestOpenFails(t *testing.T) {
	tests := []struct {
		paths []string
	}{
		{paths: []string{"./non-existent-dir/file"}},           // directory doesn't exist
		{paths: []string{"stdout", "./non-existent-dir/file"}}, // directory doesn't exist
		{paths: []string{"://foo.log"}},                        // invalid URL, scheme can't begin with colon
		{paths: []string{"mem://somewhere"}},                   // scheme not registered
	}

	for _, tt := range tests {
		_, cleanup, err := Open(tt.paths...)
		require.Nil(t, cleanup, "Cleanup function should never be nil")
		assert.Error(t, err, "Open with invalid URL should fail.")
	}
}

func TestOpenOtherErrors(t *testing.T) {
	tempName := filepath.Join(t.TempDir(), "test.log")

	tests := []struct {
		msg     string
		paths   []string
		wantErr string
	}{
		{
			msg:     "file with unexpected host",
			paths:   []string{"file://host01.test.com" + tempName},
			wantErr: "empty or use localhost",
		},
		{
			msg:     "file with user on localhost",
			paths:   []string{"file://rms@localhost" + tempName},
			wantErr: "user and password not allowed",
		},
		{
			msg:     "file url with fragment",
			paths:   []string{"file://localhost" + tempName + "#foo"},
			wantErr: "fragments not allowed",
		},
		{
			msg:     "file url with query",
			paths:   []string{"file://localhost" + tempName + "?foo=bar"},
			wantErr: "query parameters not allowed",
		},
		{
			msg:     "file with port",
			paths:   []string{"file://localhost:8080" + tempName},
			wantErr: "ports not allowed",
		},
	}

	for _, tt := range tests {
		t.Run(tt.msg, func(t *testing.T) {
			_, cleanup, err := Open(tt.paths...)
			if !assert.Error(t, err, "Open must fail.") {
				cleanup()
				return
			}

			assert.ErrorContains(t, err, tt.wantErr, "Unexpected error opening paths %v.", tt.paths)
		})
	}
}

type testWriter struct {
	expected string
	t        testing.TB
}

func (w *testWriter) Write(actual []byte) (int, error) {
	assert.Equal(w.t, []byte(w.expected), actual, "Unexpected write error.")
	return len(actual), nil
}

func (w *testWriter) Sync() error {
	return nil
}

func TestOpenWithErroringSinkFactory(t *testing.T) {
	defer resetSinkRegistry()

	msg := "expected factory error"
	factory := func(_ *url.URL) (Sink, error) {
		return nil, errors.New(msg)
	}

	assert.NoError(t, RegisterSink("test", factory), "Failed to register sink factory.")
	_, _, err := Open("test://some/path")
	assert.Contains(t, err.Error(), msg, "Unexpected error.")
}

func TestCombineWriteSyncers(t *testing.T) {
	tw := &testWriter{"test", t}
	w := CombineWriteSyncers(tw)
	w.Write([]byte("test"))
}

func fileExists(name string) bool {
	if _, err := os.Stat(name); os.IsNotExist(err) {
		return false
	}
	return true
}