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
21package zap
22
23import (
24	"bytes"
25	"io/ioutil"
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
36func TestRegisterSink(t *testing.T) {
37	const (
38		memScheme = "m"
39		nopScheme = "no-op.1234"
40	)
41	var memCalls, nopCalls int
42
43	buf := bytes.NewBuffer(nil)
44	memFactory := func(u *url.URL) (Sink, error) {
45		assert.Equal(t, u.Scheme, memScheme, "Scheme didn't match registration.")
46		memCalls++
47		return nopCloserSink{zapcore.AddSync(buf)}, nil
48	}
49	nopFactory := func(u *url.URL) (Sink, error) {
50		assert.Equal(t, u.Scheme, nopScheme, "Scheme didn't match registration.")
51		nopCalls++
52		return nopCloserSink{zapcore.AddSync(ioutil.Discard)}, nil
53	}
54
55	defer resetSinkRegistry()
56
57	require.NoError(t, RegisterSink(strings.ToUpper(memScheme), memFactory), "Failed to register scheme %q.", memScheme)
58	require.NoError(t, RegisterSink(nopScheme, nopFactory), "Failed to register scheme %q.", memScheme)
59
60	sink, close, err := Open(
61		memScheme+"://somewhere",
62		nopScheme+"://somewhere-else",
63	)
64	assert.NoError(t, err, "Unexpected error opening URLs with registered schemes.")
65
66	defer close()
67
68	assert.Equal(t, 1, memCalls, "Unexpected number of calls to memory factory.")
69	assert.Equal(t, 1, nopCalls, "Unexpected number of calls to no-op factory.")
70
71	_, err = sink.Write([]byte("foo"))
72	assert.NoError(t, err, "Failed to write to combined WriteSyncer.")
73	assert.Equal(t, "foo", buf.String(), "Unexpected buffer contents.")
74}
75
76func TestRegisterSinkErrors(t *testing.T) {
77	nopFactory := func(_ *url.URL) (Sink, error) {
78		return nopCloserSink{zapcore.AddSync(ioutil.Discard)}, nil
79	}
80	tests := []struct {
81		scheme string
82		err    string
83	}{
84		{"", "empty string"},
85		{"FILE", "already registered"},
86		{"42", "not a valid scheme"},
87		{"http*", "not a valid scheme"},
88	}
89
90	for _, tt := range tests {
91		t.Run("scheme-"+tt.scheme, func(t *testing.T) {
92			defer resetSinkRegistry()
93
94			err := RegisterSink(tt.scheme, nopFactory)
95			if assert.Error(t, err, "expected error") {
96				assert.Contains(t, err.Error(), tt.err, "unexpected error")
97			}
98		})
99	}
100}
101