| 1 |
// Copyright (c) 2016 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 |
"bytes" |
| 25 |
"io" |
| 26 |
"net/url" |
| 27 |
"strings" |
| 28 |
"testing" |
| 29 |
|
| 30 |
"github.com/stretchr/testify/assert" |
| 31 |
"github.com/stretchr/testify/require" |
| 32 |
|
| 33 |
"go.uber.org/zap/zapcore" |
| 34 |
) |
| 35 |
|
| 36 |
func stubSinkRegistry(t testing.TB) *sinkRegistry { |
| 37 |
origSinkRegistry := _sinkRegistry |
| 38 |
t.Cleanup(func() { |
| 39 |
_sinkRegistry = origSinkRegistry |
| 40 |
}) |
| 41 |
|
| 42 |
r := newSinkRegistry() |
| 43 |
_sinkRegistry = r |
| 44 |
return r |
| 45 |
} |
| 46 |
|
| 47 |
func TestRegisterSink(t *testing.T) { |
| 48 |
stubSinkRegistry(t) |
| 49 |
|
| 50 |
const ( |
| 51 |
memScheme = "mem" |
| 52 |
nopScheme = "no-op.1234" |
| 53 |
) |
| 54 |
var memCalls, nopCalls int |
| 55 |
|
| 56 |
buf := bytes.NewBuffer(nil) |
| 57 |
memFactory := func(u *url.URL) (Sink, error) { |
| 58 |
assert.Equal(t, u.Scheme, memScheme, "Scheme didn't match registration.") |
| 59 |
memCalls++ |
| 60 |
return nopCloserSink{zapcore.AddSync(buf)}, nil |
| 61 |
} |
| 62 |
nopFactory := func(u *url.URL) (Sink, error) { |
| 63 |
assert.Equal(t, u.Scheme, nopScheme, "Scheme didn't match registration.") |
| 64 |
nopCalls++ |
| 65 |
return nopCloserSink{zapcore.AddSync(io.Discard)}, nil |
| 66 |
} |
| 67 |
|
| 68 |
require.NoError(t, RegisterSink(strings.ToUpper(memScheme), memFactory), "Failed to register scheme %q.", memScheme) |
| 69 |
require.NoError(t, RegisterSink(nopScheme, nopFactory), "Failed to register scheme %q.", nopScheme) |
| 70 |
|
| 71 |
sink, closeSink, err := Open( |
| 72 |
memScheme+"://somewhere", |
| 73 |
nopScheme+"://somewhere-else", |
| 74 |
) |
| 75 |
require.NoError(t, err, "Unexpected error opening URLs with registered schemes.") |
| 76 |
defer closeSink() |
| 77 |
|
| 78 |
assert.Equal(t, 1, memCalls, "Unexpected number of calls to memory factory.") |
| 79 |
assert.Equal(t, 1, nopCalls, "Unexpected number of calls to no-op factory.") |
| 80 |
|
| 81 |
_, err = sink.Write([]byte("foo")) |
| 82 |
assert.NoError(t, err, "Failed to write to combined WriteSyncer.") |
| 83 |
assert.Equal(t, "foo", buf.String(), "Unexpected buffer contents.") |
| 84 |
} |
| 85 |
|
| 86 |
func TestRegisterSinkErrors(t *testing.T) { |
| 87 |
nopFactory := func(_ *url.URL) (Sink, error) { |
| 88 |
return nopCloserSink{zapcore.AddSync(io.Discard)}, nil |
| 89 |
} |
| 90 |
tests := []struct { |
| 91 |
scheme string |
| 92 |
err string |
| 93 |
}{ |
| 94 |
{"", "empty string"}, |
| 95 |
{"FILE", "already registered"}, |
| 96 |
{"42", "not a valid scheme"}, |
| 97 |
{"http*", "not a valid scheme"}, |
| 98 |
} |
| 99 |
|
| 100 |
for _, tt := range tests { |
| 101 |
t.Run("scheme-"+tt.scheme, func(t *testing.T) { |
| 102 |
r := newSinkRegistry() |
| 103 |
err := r.RegisterSink(tt.scheme, nopFactory) |
| 104 |
assert.ErrorContains(t, err, tt.err) |
| 105 |
}) |
| 106 |
} |
| 107 |
} |