Unit Tests for logging.go added (#148)

* Unit Tests for logging.go added.

* Unit Tests for logging.go added part 2

* Unit tests added for Utils package
This commit is contained in:
Yehor Butko 2018-07-19 22:41:29 +03:00 committed by brandonstorj
parent ea077e4dcb
commit 7ea075ce9e
8 changed files with 409 additions and 4 deletions

View File

@ -21,6 +21,10 @@ const (
// DefaultPacketSize sets the target packet size. MTUs are often 1500,
// though a good argument could be made for 512
DefaultPacketSize = 1000
// DefaultApplication is the default values for application name. Should be used
// when value in ClientOpts.Application is not set and len(os.Args) == 0
DefaultApplication = "unknown"
)
// ClientOpts allows you to set Client Options
@ -69,7 +73,7 @@ func NewClient(remoteAddr string, opts ClientOpts) (rv *Client, err error) {
opts.Application = os.Args[0]
} else {
// what the actual heck
opts.Application = "unknown"
opts.Application = DefaultApplication
}
}
if opts.Instance == "" {

View File

@ -0,0 +1,138 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package telemetry
import (
"errors"
"os"
"testing"
"github.com/stretchr/testify/assert"
monkit "gopkg.in/spacemonkeygo/monkit.v2"
)
func TestNewClient_IntervalIsZero(t *testing.T) {
s, err := Listen("127.0.0.1:0")
assert.NoError(t, err)
defer s.Close()
client, err := NewClient(s.Addr(), ClientOpts{
Application: "testapp",
Instance: "testinst",
Interval: 0,
})
assert.NotNil(t, client)
assert.Equal(t, client.interval, DefaultInterval)
}
func TestNewClient_ApplicationAndArgsAreEmpty(t *testing.T) {
s, err := Listen("127.0.0.1:0")
assert.NoError(t, err)
oldArgs := os.Args
defer func() {
s.Close()
os.Args = oldArgs
}()
os.Args = nil
client, err := NewClient(s.Addr(), ClientOpts{
Application: "",
Instance: "testinst",
Interval: 0,
})
assert.NotNil(t, client)
assert.Equal(t, DefaultApplication, client.opts.Application)
}
func TestNewClient_ApplicationIsEmpty(t *testing.T) {
s, err := Listen("127.0.0.1:0")
assert.NoError(t, err)
defer s.Close()
client, err := NewClient(s.Addr(), ClientOpts{
Application: "",
Instance: "testinst",
Interval: 0,
})
assert.NotNil(t, client)
assert.Equal(t, client.opts.Application, os.Args[0])
}
func TestNewClient_InstanceIsEmpty(t *testing.T) {
s, err := Listen("127.0.0.1:0")
assert.NoError(t, err)
defer s.Close()
client, err := NewClient(s.Addr(), ClientOpts{
Application: "qwe",
Instance: "",
Interval: 0,
})
assert.NotNil(t, client)
assert.Equal(t, client.opts.InstanceId, []byte(DefaultInstanceID()))
assert.Equal(t, client.opts.Application, "qwe")
assert.Equal(t, client.interval, DefaultInterval)
}
func TestNewClient_RegistryIsNil(t *testing.T) {
s, err := Listen("127.0.0.1:0")
assert.NoError(t, err)
defer s.Close()
client, err := NewClient(s.Addr(), ClientOpts{
Application: "qwe",
Instance: "",
Interval: 0,
})
assert.NotNil(t, client)
assert.Equal(t, client.opts.InstanceId, []byte(DefaultInstanceID()))
assert.Equal(t, client.opts.Application, "qwe")
assert.Equal(t, client.interval, DefaultInterval)
assert.Equal(t, client.opts.Registry, monkit.Default)
}
func TestNewClient_PacketSizeIsZero(t *testing.T) {
s, err := Listen("127.0.0.1:0")
assert.NoError(t, err)
defer s.Close()
client, err := NewClient(s.Addr(), ClientOpts{
Application: "qwe",
Instance: "",
Interval: 0,
PacketSize: 0,
})
assert.NotNil(t, client)
assert.Equal(t, client.opts.InstanceId, []byte(DefaultInstanceID()))
assert.Equal(t, client.opts.Application, "qwe")
assert.Equal(t, client.interval, DefaultInterval)
assert.Equal(t, client.opts.Registry, monkit.Default)
assert.Equal(t, client.opts.PacketSize, DefaultPacketSize)
}
func TestRun_ReportNoCalled(t *testing.T) {
client := &MockClient{}
ctx := &MockContext{}
ctx.On("Err").Return(errors.New("")).Once()
client.On("Report").Times(0)
client.On("Run", ctx).Once()
client.Run(ctx)
ctx.AssertExpectations(t)
}

View File

@ -0,0 +1,83 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
// Code generated by mockery v1.0.0. DO NOT EDIT.
package telemetry
import (
"time"
mock "github.com/stretchr/testify/mock"
)
// MockContext is an autogenerated mock type for the Cont type
type MockContext struct {
mock.Mock
}
// Deadline provides a mock function with given fields:
func (_m *MockContext) Deadline() (time.Time, bool) {
ret := _m.Called()
var r0 time.Time
if rf, ok := ret.Get(0).(func() time.Time); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(time.Time)
}
var r1 bool
if rf, ok := ret.Get(1).(func() bool); ok {
r1 = rf()
} else {
r1 = ret.Get(1).(bool)
}
return r0, r1
}
// Done provides a mock function with given fields:
func (_m *MockContext) Done() <-chan struct{} {
ret := _m.Called()
var r0 <-chan struct{}
if rf, ok := ret.Get(0).(func() <-chan struct{}); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(<-chan struct{})
}
}
return r0
}
// Err provides a mock function with given fields:
func (_m *MockContext) Err() 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
}
// Value provides a mock function with given fields: key
func (_m *MockContext) Value(key interface{}) interface{} {
ret := _m.Called(key)
var r0 interface{}
if rf, ok := ret.Get(0).(func(interface{}) interface{}); ok {
r0 = rf(key)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(interface{})
}
}
return r0
}

View File

@ -0,0 +1,43 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
// Code generated by mockery v1.0.0. DO NOT EDIT.
package telemetry
import (
context "context"
mock "github.com/stretchr/testify/mock"
)
// MockClient is an autogenerated mock type for the client type
type MockClient struct {
mock.Mock
client Client
}
// Report provides a mock function with given fields: ctx
func (_m *MockClient) Report(ctx context.Context) error {
ret := _m.Called(ctx)
var r0 error
if rf, ok := ret.Get(0).(func(context.Context) error); ok {
r0 = rf(ctx)
} else {
r0 = ret.Error(0)
}
return r0
}
// Run provides a mock function with given fields: ctx
func (_m *MockClient) Run(ctx context.Context) {
client, _ := NewClient("", ClientOpts{
Application: "qwe",
Instance: "",
Interval: 2,
PacketSize: 0,
})
client.Run(ctx)
}

View File

@ -0,0 +1,44 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package telemetry
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestListen_NilOnBadAddress(t *testing.T) {
server, errListen := Listen("11")
defer func() {
if server != nil {
server.Close()
}
}()
assert.Nil(t, server)
assert.Error(t, errListen)
}
func TestServe_ReturnErrorOnConnFail(t *testing.T) {
server, _ := Listen("127.0.0.1:0")
defer func() {
if server != nil && server.conn != nil {
server.Close()
}
}()
server.conn.Close()
server.conn = nil
errServe := server.Serve(nil, nil)
assert.EqualError(t, errServe, "telemetry error: invalid conn: <nil>")
}
func TestListenAndServe_ReturnErrorOnListenFails(t *testing.T) {
err := ListenAndServe(nil, "1", nil)
assert.Error(t, err)
}

View File

@ -0,0 +1,20 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package telemetry
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestJitter_NegativeDuration(t *testing.T) {
duration := time.Duration(-1)
expected := time.Duration(1)
actual := jitter(duration)
assert.Equal(t, expected, actual)
}

View File

@ -9,14 +9,18 @@ import (
"go.uber.org/zap"
)
var zapNewDevelopment = zap.NewDevelopment
var zapNewProduction = zap.NewProduction
var zapNewNop = zap.NewNop
// NewLogger takes an environment and a set of options for a logger
func NewLogger(e string, options ...zap.Option) (*zap.Logger, error) {
switch strings.ToLower(e) {
case "dev", "development":
return zap.NewDevelopment(options...)
return zapNewDevelopment(options...)
case "prod", "production":
return zap.NewProduction(options...)
return zapNewProduction(options...)
}
return zap.NewNop(), nil
return zapNewNop(), nil
}

69
pkg/utils/logging_test.go Normal file
View File

@ -0,0 +1,69 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package utils
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)
var errExpected = errors.New("error with initializing logger")
func TestNewLoggerDev(t *testing.T) {
oldZapNewDevelopment := zapNewDevelopment
defer func() { zapNewDevelopment = oldZapNewDevelopment }()
zapNewDevelopment = func(options ...zap.Option) (*zap.Logger, error) {
return nil, errExpected
}
_, err := NewLogger("dev")
assert.NotNil(t, err)
assert.Equal(t, err, errExpected)
_, err = NewLogger("development")
assert.NotNil(t, err)
assert.Equal(t, err, errExpected)
}
func TestNewLoggerProd(t *testing.T) {
oldZapNewProduction := zapNewProduction
defer func() { zapNewProduction = oldZapNewProduction }()
zapNewProduction = func(options ...zap.Option) (*zap.Logger, error) {
return nil, errExpected
}
_, err := NewLogger("prod")
assert.NotNil(t, err)
assert.Equal(t, err, errExpected)
_, err = NewLogger("production")
assert.NotNil(t, err)
assert.Equal(t, err, errExpected)
}
func TestNewLoggerDefault(t *testing.T) {
oldZapNewNop := zapNewNop
defer func() { zapNewNop = oldZapNewNop }()
zapNewNop = func() *zap.Logger {
return nil
}
client, err := NewLogger("default")
assert.Nil(t, client)
assert.Nil(t, err)
}