Fix the dependency issue (#231)

This commit is contained in:
Robbie Zhang
2018-06-21 12:09:42 -07:00
committed by GitHub
parent 027b76651d
commit 6ec1098bb8
16629 changed files with 74837 additions and 4975021 deletions

View File

@@ -1,56 +0,0 @@
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package syslog
import (
"fmt"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewFormatter(t *testing.T) {
f := newFormatter("", RFC3164)
assert.IsType(t, &localFormatter{}, f)
f = newFormatter("tcp", RFC3164)
assert.IsType(t, &rfc3164Formatter{}, f)
f = newFormatter("tcp", 123)
assert.Nil(t, f)
}
func TestFormatterFormats(t *testing.T) {
var tests = []struct {
format string
tsLayout string
local bool
f formatter
}{
{"<%d>%s %s[%d]: %s", time.Stamp, true, &localFormatter{}},
{"<%d>%s %s %s[%d]: %s", time.RFC3339, false, &rfc3164Formatter{}},
}
for _, te := range tests {
ts := time.Now()
if te.local {
assert.Equal(t, fmt.Sprintf(te.format, priority, ts.Format(te.tsLayout), tag, os.Getpid(), "foo"), te.f.Format(priority, ts, "host", tag, "foo"))
} else {
assert.Equal(t, fmt.Sprintf(te.format, priority, ts.Format(te.tsLayout), "host", tag, os.Getpid(), "foo"), te.f.Format(priority, ts, "host", tag, "foo"))
}
}
}

View File

@@ -1,126 +0,0 @@
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package syslog
import (
"testing"
"time"
"github.com/Sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestNewSyslogHook(t *testing.T) {
// error case
d := &mockDialer{}
d.On("dial").Return(nil, assert.AnError)
h, err := newHook(d)
assert.Nil(t, h)
assert.Error(t, err)
assert.EqualError(t, err, assert.AnError.Error())
d.AssertCalled(t, "dial")
d.AssertNumberOfCalls(t, "dial", 1)
// no error
d = &mockDialer{}
w := &MockWriter{}
d.On("dial").Return(w, nil)
h, err = newHook(d)
assert.NotNil(t, h)
assert.NoError(t, err)
assert.Equal(t, w, h.writer)
d.AssertCalled(t, "dial")
d.AssertNumberOfCalls(t, "dial", 1)
}
func TestLevels(t *testing.T) {
m := &MockWriter{}
d := &mockDialer{}
d.On("dial").Return(m, nil)
h, err := newHook(d)
assert.NotNil(t, h)
assert.NoError(t, err)
m.On("Crit", mock.Anything).Return(nil)
m.On("Err", mock.Anything).Return(nil)
m.On("Warning", mock.Anything).Return(nil)
m.On("Debug", mock.Anything).Return(nil)
m.On("Info", mock.Anything).Return(nil)
var tests = []struct {
entry *logrus.Entry
f string
}{
{
entry: &logrus.Entry{Message: "panic", Level: logrus.PanicLevel},
f: "Crit",
},
{
entry: &logrus.Entry{Message: "fatal", Level: logrus.FatalLevel},
f: "Crit",
},
{
entry: &logrus.Entry{Message: "error", Level: logrus.ErrorLevel},
f: "Err",
},
{
entry: &logrus.Entry{Message: "warn", Level: logrus.WarnLevel},
f: "Warning",
},
{
entry: &logrus.Entry{Message: "info", Level: logrus.InfoLevel},
f: "Info",
},
{
entry: &logrus.Entry{Message: "debug", Level: logrus.DebugLevel},
f: "Debug",
},
}
calls := make(map[string]int)
for _, te := range tests {
calls[te.f] = 0
}
for _, te := range tests {
assert.NoError(t, h.writeEntry(te.entry))
calls[te.f]++
m.AssertCalled(t, te.f, te.entry.Message)
m.AssertNumberOfCalls(t, te.f, calls[te.f])
}
}
func TestConnect(t *testing.T) {
// attempt a connection to a server that
// does not exist
h, err := NewHook(
"tcp",
"foo:514",
Info,
"test",
)
assert.NoError(t, err)
assert.NotNil(t, h)
h.Fire(&logrus.Entry{
Message: "foo",
Level: logrus.InfoLevel,
})
<-time.After(5 * time.Second)
}

View File

@@ -1,55 +0,0 @@
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package syslog
import (
"net"
mock "github.com/stretchr/testify/mock"
)
// MockAddr is an autogenerated mock type for the Addr type
type MockAddr struct {
mock.Mock
}
// Network provides a mock function with given fields:
func (_m *MockAddr) Network() string {
ret := _m.Called()
var r0 string
if rf, ok := ret.Get(0).(func() string); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(string)
}
return r0
}
// String provides a mock function with given fields:
func (_m *MockAddr) String() string {
ret := _m.Called()
var r0 string
if rf, ok := ret.Get(0).(func() string); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(string)
}
return r0
}
var _ net.Addr = (*MockAddr)(nil)

View File

@@ -1,46 +0,0 @@
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package syslog
import mock "github.com/stretchr/testify/mock"
// mockDialer is an autogenerated mock type for the dialer type
type mockDialer struct {
mock.Mock
}
// dial provides a mock function with given fields:
func (_m *mockDialer) dial() (Writer, error) {
ret := _m.Called()
var r0 Writer
if rf, ok := ret.Get(0).(func() Writer); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(Writer)
}
}
var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}
return r0, r1
}
var _ dialer = (*mockDialer)(nil)

View File

@@ -1,38 +0,0 @@
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package syslog
import mock "github.com/stretchr/testify/mock"
import time "time"
// mockFormatter is an autogenerated mock type for the formatter type
type mockFormatter struct {
mock.Mock
}
// Format provides a mock function with given fields: p, ts, hostname, tag, msg
func (_m *mockFormatter) Format(p Priority, ts time.Time, hostname string, tag string, msg string) string {
ret := _m.Called(p, ts, hostname, tag, msg)
var r0 string
if rf, ok := ret.Get(0).(func(Priority, time.Time, string, string, string) string); ok {
r0 = rf(p, ts, hostname, tag, msg)
} else {
r0 = ret.Get(0).(string)
}
return r0
}
var _ formatter = (*mockFormatter)(nil)

View File

@@ -1,155 +0,0 @@
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package syslog
import mock "github.com/stretchr/testify/mock"
import net "net"
import time "time"
// MockConn is an autogenerated mock type for the Conn type
type MockNetConn struct {
mock.Mock
}
// Close provides a mock function with given fields:
func (_m *MockNetConn) Close() error {
ret := _m.Called()
var r0 error
if rf, ok := ret.Get(0).(func() error); ok {
r0 = rf()
} else {
r0 = ret.Error(0)
}
return r0
}
// LocalAddr provides a mock function with given fields:
func (_m *MockNetConn) LocalAddr() net.Addr {
ret := _m.Called()
var r0 net.Addr
if rf, ok := ret.Get(0).(func() net.Addr); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(net.Addr)
}
}
return r0
}
// Read provides a mock function with given fields: b
func (_m *MockNetConn) Read(b []byte) (int, error) {
ret := _m.Called(b)
var r0 int
if rf, ok := ret.Get(0).(func([]byte) int); ok {
r0 = rf(b)
} else {
r0 = ret.Get(0).(int)
}
var r1 error
if rf, ok := ret.Get(1).(func([]byte) error); ok {
r1 = rf(b)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// RemoteAddr provides a mock function with given fields:
func (_m *MockNetConn) RemoteAddr() net.Addr {
ret := _m.Called()
var r0 net.Addr
if rf, ok := ret.Get(0).(func() net.Addr); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(net.Addr)
}
}
return r0
}
// SetDeadline provides a mock function with given fields: t
func (_m *MockNetConn) SetDeadline(t time.Time) error {
ret := _m.Called(t)
var r0 error
if rf, ok := ret.Get(0).(func(time.Time) error); ok {
r0 = rf(t)
} else {
r0 = ret.Error(0)
}
return r0
}
// SetReadDeadline provides a mock function with given fields: t
func (_m *MockNetConn) SetReadDeadline(t time.Time) error {
ret := _m.Called(t)
var r0 error
if rf, ok := ret.Get(0).(func(time.Time) error); ok {
r0 = rf(t)
} else {
r0 = ret.Error(0)
}
return r0
}
// SetWriteDeadline provides a mock function with given fields: t
func (_m *MockNetConn) SetWriteDeadline(t time.Time) error {
ret := _m.Called(t)
var r0 error
if rf, ok := ret.Get(0).(func(time.Time) error); ok {
r0 = rf(t)
} else {
r0 = ret.Error(0)
}
return r0
}
// Write provides a mock function with given fields: b
func (_m *MockNetConn) Write(b []byte) (int, error) {
ret := _m.Called(b)
var r0 int
if rf, ok := ret.Get(0).(func([]byte) int); ok {
r0 = rf(b)
} else {
r0 = ret.Get(0).(int)
}
var r1 error
if rf, ok := ret.Get(1).(func([]byte) error); ok {
r1 = rf(b)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
var _ net.Conn = (*MockNetConn)(nil)

View File

@@ -1,47 +0,0 @@
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package syslog
import mock "github.com/stretchr/testify/mock"
import net "net"
// mockNetDialer is an autogenerated mock type for the netDialer type
type mockNetDialer struct {
mock.Mock
}
// dial provides a mock function with given fields:
func (_m *mockNetDialer) dial() (net.Conn, error) {
ret := _m.Called()
var r0 net.Conn
if rf, ok := ret.Get(0).(func() net.Conn); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(net.Conn)
}
}
var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}
return r0, r1
}
var _ netDialer = (*mockNetDialer)(nil)

View File

@@ -1,174 +0,0 @@
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package syslog
import mock "github.com/stretchr/testify/mock"
// MockWriter is an autogenerated mock type for the Writer type
type MockWriter struct {
mock.Mock
}
// Close provides a mock function with given fields:
func (_m *MockWriter) Close() error {
ret := _m.Called()
var r0 error
if rf, ok := ret.Get(0).(func() error); ok {
r0 = rf()
} else {
r0 = ret.Error(0)
}
return r0
}
// Crit provides a mock function with given fields: _a0
func (_m *MockWriter) Crit(_a0 string) error {
ret := _m.Called(_a0)
var r0 error
if rf, ok := ret.Get(0).(func(string) error); ok {
r0 = rf(_a0)
} else {
r0 = ret.Error(0)
}
return r0
}
// Debug provides a mock function with given fields: _a0
func (_m *MockWriter) Debug(_a0 string) error {
ret := _m.Called(_a0)
var r0 error
if rf, ok := ret.Get(0).(func(string) error); ok {
r0 = rf(_a0)
} else {
r0 = ret.Error(0)
}
return r0
}
// Emerg provides a mock function with given fields: _a0
func (_m *MockWriter) Emerg(_a0 string) error {
ret := _m.Called(_a0)
var r0 error
if rf, ok := ret.Get(0).(func(string) error); ok {
r0 = rf(_a0)
} else {
r0 = ret.Error(0)
}
return r0
}
// Err provides a mock function with given fields: _a0
func (_m *MockWriter) Err(_a0 string) error {
ret := _m.Called(_a0)
var r0 error
if rf, ok := ret.Get(0).(func(string) error); ok {
r0 = rf(_a0)
} else {
r0 = ret.Error(0)
}
return r0
}
// Info provides a mock function with given fields: _a0
func (_m *MockWriter) Info(_a0 string) error {
ret := _m.Called(_a0)
var r0 error
if rf, ok := ret.Get(0).(func(string) error); ok {
r0 = rf(_a0)
} else {
r0 = ret.Error(0)
}
return r0
}
// Warning provides a mock function with given fields: _a0
func (_m *MockWriter) Warning(_a0 string) error {
ret := _m.Called(_a0)
var r0 error
if rf, ok := ret.Get(0).(func(string) error); ok {
r0 = rf(_a0)
} else {
r0 = ret.Error(0)
}
return r0
}
// WithPriority provides a mock function with given fields: priority
func (_m *MockWriter) WithPriority(priority Priority) Writer {
ret := _m.Called(priority)
var r0 Writer
if rf, ok := ret.Get(0).(func(Priority) Writer); ok {
r0 = rf(priority)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(Writer)
}
}
return r0
}
// WithTag provides a mock function with given fields: tag
func (_m *MockWriter) WithTag(tag string) Writer {
ret := _m.Called(tag)
var r0 Writer
if rf, ok := ret.Get(0).(func(string) Writer); ok {
r0 = rf(tag)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(Writer)
}
}
return r0
}
// Write provides a mock function with given fields: p
func (_m *MockWriter) Write(p []byte) (int, error) {
ret := _m.Called(p)
var r0 int
if rf, ok := ret.Get(0).(func([]byte) int); ok {
r0 = rf(p)
} else {
r0 = ret.Get(0).(int)
}
var r1 error
if rf, ok := ret.Get(1).(func([]byte) error); ok {
r1 = rf(p)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
var _ Writer = (*MockWriter)(nil)

View File

@@ -1,80 +0,0 @@
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package syslog
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
var (
network = "tcp"
raddr = "localhost:514"
tag = "test"
priority = Info | Daemon
)
func TestMakeTag(t *testing.T) {
p := filepath.Base(os.Args[0])
var tests = []struct {
prefix string
proc string
out string
}{
{
prefix: "",
proc: "",
out: p,
},
{
prefix: "",
proc: "foo",
out: "foo",
},
{
prefix: "foo",
proc: "",
out: "foo" + sep + p,
},
{
prefix: "bar",
proc: "foo",
out: "bar" + sep + "foo",
},
}
for _, te := range tests {
out := MakeTag(te.prefix, te.proc)
assert.Equal(t, te.out, out)
}
}
func TestDefaultDialerBadPriority(t *testing.T) {
d := &defaultDialer{
priority: -1,
}
w, err := d.dial()
assert.Nil(t, w)
assert.Error(t, err)
d.priority = (Local7 | Debug) + 1
w, err = d.dial()
assert.Nil(t, w)
assert.Error(t, err)
}

View File

@@ -1,26 +0,0 @@
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !windows,!nacl,!plan9
package syslog
import "testing"
import "github.com/stretchr/testify/assert"
func TestNewNetDialer(t *testing.T) {
d := newNetDialer("", "")
assert.IsType(t, &unixSyslogDialer{}, d)
}

View File

@@ -1,25 +0,0 @@
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build windows
package syslog
import "testing"
import "github.com/stretchr/testify/assert"
func TestNewNetDialer(t *testing.T) {
d := newNetDialer("tcp", "foo")
assert.IsType(t, &defaultDialer{}, d)
}

View File

@@ -1,278 +0,0 @@
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package syslog
import (
"fmt"
"net"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestWriterReconnect(t *testing.T) {
dn := &mockNetDialer{}
dn.On("dial").Return(nil, assert.AnError)
w := newWriter(priority, tag, "", dn, nil)
go w.run()
<-w.running
calls := []func(string) error{
w.Emerg,
w.Crit,
w.Err,
w.Warning,
w.Info,
w.Debug,
}
for _, f := range calls {
err := f("test")
assert.NoError(t, err)
}
w.Close()
dn.AssertNumberOfCalls(t, "dial", 1+len(calls))
}
func TestWriterWrite(t *testing.T) {
msg := "foo"
f := &mockFormatter{}
f.On("Format", priority, mock.Anything, "host", tag, msg).Return("test")
a := &MockAddr{}
a.On("String").Return("host:123")
c := &MockNetConn{}
c.On("LocalAddr").Return(a)
c.On("Write", []byte("test\n")).Return(len(msg), nil)
c.On("Close").Return(nil)
dn := &mockNetDialer{}
dn.On("dial").Return(c, nil)
w := newWriter(priority, tag, "", dn, f)
n, err := w.Write([]byte(msg))
assert.NoError(t, err)
assert.Equal(t, len(msg), n)
go w.run()
<-w.running
w.Close()
c.AssertExpectations(t)
dn.AssertNumberOfCalls(t, "dial", 1)
}
func TestMaxLogBuffer(t *testing.T) {
f := &mockFormatter{}
dn := &mockNetDialer{}
c := &MockNetConn{}
a := &MockAddr{}
a.On("String").Return("foo")
c.On("LocalAddr").Return(a)
c.On("Close").Return(nil)
dn.On("dial").Return(c, nil)
w := newWriter(priority, tag, "", dn, f)
for i := 0; i < maxLogBuffer+1; i++ {
msg := fmt.Sprintf("%d", i)
f.On("Format", priority, mock.Anything, "", tag, msg).Return(msg)
c.On("Write", []byte(msg+"\n")).Return(len(msg), nil)
w.Write([]byte(msg))
}
go w.run()
<-w.running
w.Close()
for i := 0; i < maxLogBuffer; i++ {
if !f.AssertCalled(t, "Format", priority, mock.Anything, "", tag, fmt.Sprintf("%d", i)) ||
!c.AssertCalled(t, "Write", []byte(fmt.Sprintf("%d\n", i))) {
}
}
f.AssertNumberOfCalls(t, "Format", maxLogBuffer)
f.AssertNotCalled(t, "Format", priority, mock.Anything, "", tag, fmt.Sprintf("%d", maxLogBuffer))
}
func TestWriterReconnectWrite(t *testing.T) {
dn := &mockNetDialer{}
c := &MockNetConn{}
a := &MockAddr{}
a.On("String").Return("addr:123")
c.On("LocalAddr").Return(a)
c.On("Close").Return(nil)
dn.On("dial").Return(nil, assert.AnError)
f := &mockFormatter{}
w := newWriter(priority, tag, "", dn, f)
go w.run()
<-w.running
dn.AssertNumberOfCalls(t, "dial", 1)
dn = &mockNetDialer{}
dn.On("dial").Return(c, nil)
w.dialer = dn
f.On("Format", priority, mock.Anything, "addr", tag, "test").Return("test")
c.On("Write", []byte("test\n")).Return(len("test"), nil)
w.Write([]byte("test"))
w.Close()
dn.AssertNumberOfCalls(t, "dial", 1)
c.AssertNumberOfCalls(t, "Write", 1) // 1 call to writer.write
f.AssertNumberOfCalls(t, "Format", 1)
}
func TestWriterReconnectWriteError(t *testing.T) {
dn := &mockNetDialer{}
c := &MockNetConn{}
a := &MockAddr{}
a.On("String").Return("addr:123")
c.On("LocalAddr").Return(a)
c.On("Close").Return(nil)
dn.On("dial").Return(c, nil)
f := &mockFormatter{}
w := newWriter(priority, tag, "", dn, f)
go w.run()
<-w.running
dn.AssertNumberOfCalls(t, "dial", 1)
f.On("Format", priority, mock.Anything, "addr", tag, "test").Return("test")
c.On("Write", []byte("test\n")).Return(0, assert.AnError)
w.Write([]byte("test"))
w.Close()
f.AssertExpectations(t)
c.AssertExpectations(t)
}
func TestWriterWithTag(t *testing.T) {
f := &mockFormatter{}
f.On("Format", priority, mock.Anything, "addr", "child", "child").Return("child")
f.On("Format", priority, mock.Anything, "addr", "gchild", "gchild").Return("gchild")
dn := &mockNetDialer{}
c := &MockNetConn{}
a := &MockAddr{}
a.On("String").Return("addr:123")
c.On("LocalAddr").Return(a)
c.On("Close").Return(nil)
c.On("Write", []byte("child\n")).Return(len("child"), nil)
c.On("Write", []byte("gchild\n")).Return(len("gchild"), nil)
dn.On("dial").Return(c, nil)
w := newWriter(priority, tag, "", dn, f)
child := w.WithTag("child")
child.Write([]byte("child"))
gchild := child.WithTag("gchild")
gchild.Write([]byte("gchild"))
go w.run()
<-w.running
child.Close()
gchild.Close()
select {
case <-w.done:
default:
assert.FailNow(t, "parent writer is not closed by child Close call")
}
f.AssertExpectations(t)
c.AssertExpectations(t)
}
func TestWriterWithPriority(t *testing.T) {
f := &mockFormatter{}
f.On("Format", Err|Daemon, mock.Anything, "addr", tag, "err").Return("err")
f.On("Format", Debug|Daemon, mock.Anything, "addr", tag, "debug").Return("debug")
dn := &mockNetDialer{}
c := &MockNetConn{}
a := &MockAddr{}
a.On("String").Return("addr:123")
c.On("LocalAddr").Return(a)
c.On("Close").Return(nil)
c.On("Write", []byte("err\n")).Return(len("err"), nil)
c.On("Write", []byte("debug\n")).Return(len("debug"), nil)
dn.On("dial").Return(c, nil)
w := newWriter(priority, tag, "", dn, f)
errw := w.WithPriority(Err | Daemon)
errw.Write([]byte("err"))
debugw := errw.WithPriority(Debug | Daemon)
debugw.Write([]byte("debug"))
go w.run()
<-w.running
errw.Close()
select {
case <-w.done:
default:
assert.FailNow(t, "parent writer is not closed by child Close call")
}
f.AssertExpectations(t)
c.AssertExpectations(t)
}
func TestWriterInitialConnectError(t *testing.T) {
var tests = []error{
&net.ParseError{},
&net.AddrError{},
}
for _, e := range tests {
dn := &mockNetDialer{}
dn.On("dial").Return(nil, e)
w := newWriter(priority, tag, "", dn, &mockFormatter{})
w.run()
select {
case <-w.running:
assert.FailNow(t, "writer should not run when connect() fails initially")
default:
}
}
}

View File

@@ -1,136 +0,0 @@
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package log
import (
"bufio"
"fmt"
"strings"
"testing"
"time"
"github.com/Sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func BenchmarkFormatNonEmpty(b *testing.B) {
f := NewTextFormatter()
e := &logrus.Entry{
Time: time.Now(),
Level: logrus.InfoLevel,
Message: "the quick brown fox jumps over the lazy dog",
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
f.Format(e)
}
}
func BenchmarkFormatEmpty(b *testing.B) {
f := NewTextFormatter()
e := &logrus.Entry{
Time: time.Now(),
Level: logrus.InfoLevel,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
f.Format(e)
}
}
func TestFormatEmpty(t *testing.T) {
ti := time.Now()
e := &logrus.Entry{Time: ti, Level: logrus.InfoLevel}
f := NewTextFormatter()
b, err := f.Format(e)
assert.NoError(t, err)
assert.Equal(t, fmt.Sprintf("%s %s \n", ti.Format(f.TimestampFormat), levelToString(e.Level)), string(b))
}
func TestFormatNonEmpty(t *testing.T) {
ti := time.Now()
m := "foo bar baz"
e := &logrus.Entry{Time: ti, Level: logrus.InfoLevel, Message: m}
f := NewTextFormatter()
b, err := f.Format(e)
assert.NoError(t, err)
assert.Equal(t, fmt.Sprintf("%s %s %s\n", ti.Format(f.TimestampFormat), levelToString(e.Level), m), string(b))
// test with multiple lines
pre := fmt.Sprintf("%s %s ", ti.Format(f.TimestampFormat), levelToString(e.Level))
var tests = []struct {
in string
out []string
}{
{
"foo",
[]string{
pre + "foo",
},
},
{
"\n",
[]string{
pre,
"",
},
},
{
"foo\n",
[]string{
pre + "foo",
"",
},
},
{
"\nfoo\n",
[]string{
pre + "",
"foo",
"",
},
},
{
"foo\n",
[]string{
pre + "foo",
"",
},
},
{
"foo \nbar\n baz ",
[]string{
pre + "foo ",
"bar",
" baz ",
},
},
}
for idx, te := range tests {
e.Message = te.in
b, err = f.Format(e)
assert.NoError(t, err)
s := bufio.NewScanner(strings.NewReader(string(b)))
i := 0
for s.Scan() {
assert.True(t, i < len(te.out), "case %d", idx)
assert.Equal(t, te.out[i], s.Text(), "case %d", idx)
i++
}
}
}