Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
desa committed Sep 21, 2017
1 parent 289c6c6 commit 013f733
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 70 deletions.
132 changes: 67 additions & 65 deletions services/diagnostic/log_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package diagnostic
package diagnostic_test

import (
"bytes"
Expand All @@ -7,6 +7,8 @@ import (
"strconv"
"testing"
"time"

"github.com/influxdata/kapacitor/services/diagnostic"
)

var defaultTime = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
Expand All @@ -19,16 +21,16 @@ func (t testStringer) String() string {

func TestLoggerWithoutContext(t *testing.T) {
now := time.Now()
nowStr := now.Format(log.RFC3339Milli)
nowStr := now.Format(diagnostic.RFC3339Milli)
buf := bytes.NewBuffer(nil)
l := log.NewLogger(buf)
l := diagnostic.NewServerLogger(buf)

tests := []struct {
name string
exp string
lvl string
msg string
fields []log.Field
fields []diagnostic.Field
}{
{
name: "no fields simple message",
Expand All @@ -53,36 +55,36 @@ func TestLoggerWithoutContext(t *testing.T) {
exp: fmt.Sprintf("ts=%s lvl=error msg=test test=this\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.String("test", "this"),
fields: []diagnostic.Field{
diagnostic.String("test", "this"),
},
},
{
name: "complex string field",
exp: fmt.Sprintf("ts=%s lvl=error msg=test test=\"this is \\\" a test/yeah\"\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.String("test", "this is \" a test/yeah"),
fields: []diagnostic.Field{
diagnostic.String("test", "this is \" a test/yeah"),
},
},
{
name: "simple stringer field",
exp: fmt.Sprintf("ts=%s lvl=error msg=test test=this\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.Stringer("test", testStringer("this")),
fields: []diagnostic.Field{
diagnostic.Stringer("test", testStringer("this")),
},
},
{
name: "simple single grouped field",
exp: fmt.Sprintf("ts=%s lvl=error msg=test test_a=this\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.GroupedFields("test", []log.Field{
log.String("a", "this"),
fields: []diagnostic.Field{
diagnostic.GroupedFields("test", []diagnostic.Field{
diagnostic.String("a", "this"),
}),
},
},
Expand All @@ -91,10 +93,10 @@ func TestLoggerWithoutContext(t *testing.T) {
exp: fmt.Sprintf("ts=%s lvl=error msg=test test_a=this test_b=other\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.GroupedFields("test", []log.Field{
log.String("a", "this"),
log.String("b", "other"),
fields: []diagnostic.Field{
diagnostic.GroupedFields("test", []diagnostic.Field{
diagnostic.String("a", "this"),
diagnostic.String("b", "other"),
}),
},
},
Expand All @@ -103,117 +105,117 @@ func TestLoggerWithoutContext(t *testing.T) {
exp: fmt.Sprintf("ts=%s lvl=error msg=test test_0=this\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.Strings("test", []string{"this"}),
fields: []diagnostic.Field{
diagnostic.Strings("test", []string{"this"}),
},
},
{
name: "simple double strings field",
exp: fmt.Sprintf("ts=%s lvl=error msg=test test_0=this test_1=other\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.Strings("test", []string{"this", "other"}),
fields: []diagnostic.Field{
diagnostic.Strings("test", []string{"this", "other"}),
},
},
{
name: "int field",
exp: fmt.Sprintf("ts=%s lvl=error msg=test test=10\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.Int("test", 10),
fields: []diagnostic.Field{
diagnostic.Int("test", 10),
},
},
{
name: "int64 field",
exp: fmt.Sprintf("ts=%s lvl=error msg=test test=10\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.Int64("test", 10),
fields: []diagnostic.Field{
diagnostic.Int64("test", 10),
},
},
{
name: "float64 field",
exp: fmt.Sprintf("ts=%s lvl=error msg=test test=3.1415926535\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.Float64("test", 3.1415926535),
fields: []diagnostic.Field{
diagnostic.Float64("test", 3.1415926535),
},
},
{
name: "bool true field",
exp: fmt.Sprintf("ts=%s lvl=error msg=test test=true\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.Bool("test", true),
fields: []diagnostic.Field{
diagnostic.Bool("test", true),
},
},
{
name: "bool false field",
exp: fmt.Sprintf("ts=%s lvl=error msg=test test=false\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.Bool("test", false),
fields: []diagnostic.Field{
diagnostic.Bool("test", false),
},
},
{
name: "simple error field",
exp: fmt.Sprintf("ts=%s lvl=error msg=test err=this\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.Error(errors.New("this")),
fields: []diagnostic.Field{
diagnostic.Error(errors.New("this")),
},
},
{
name: "nil error field",
exp: fmt.Sprintf("ts=%s lvl=error msg=test err=nil\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.Error(nil),
fields: []diagnostic.Field{
diagnostic.Error(nil),
},
},
{
name: "complex error field",
exp: fmt.Sprintf("ts=%s lvl=error msg=test err=\"this is \\\" a test/yeah\"\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.Error(errors.New("this is \" a test/yeah")),
fields: []diagnostic.Field{
diagnostic.Error(errors.New("this is \" a test/yeah")),
},
},
{
name: "time field",
exp: fmt.Sprintf("ts=%s lvl=error msg=test time=%s\n", nowStr, defaultTime.Format(time.RFC3339Nano)),
lvl: "error",
msg: "test",
fields: []log.Field{
log.Time("time", defaultTime),
fields: []diagnostic.Field{
diagnostic.Time("time", defaultTime),
},
},
{
name: "duration field",
exp: fmt.Sprintf("ts=%s lvl=error msg=test test=1s\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.Duration("test", time.Second),
fields: []diagnostic.Field{
diagnostic.Duration("test", time.Second),
},
},
{
name: "two fields",
exp: fmt.Sprintf("ts=%s lvl=error msg=test testing=\"that this\" works=1s\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.String("testing", "that this"),
log.Duration("works", time.Second),
fields: []diagnostic.Field{
diagnostic.String("testing", "that this"),
diagnostic.Duration("works", time.Second),
},
},
}
Expand All @@ -231,16 +233,16 @@ func TestLoggerWithoutContext(t *testing.T) {

func TestLoggerWithContext(t *testing.T) {
now := time.Now()
nowStr := now.Format(log.RFC3339Milli)
nowStr := now.Format(diagnostic.RFC3339Milli)
buf := bytes.NewBuffer(nil)
l := log.NewLogger(buf).With(log.String("a", "tag"), log.Int("id", 10))
l := diagnostic.NewServerLogger(buf).With(diagnostic.String("a", "tag"), diagnostic.Int("id", 10)).(*diagnostic.ServerLogger)

tests := []struct {
name string
exp string
lvl string
msg string
fields []log.Field
fields []diagnostic.Field
}{
{
name: "no fields simple message",
Expand All @@ -253,10 +255,10 @@ func TestLoggerWithContext(t *testing.T) {
exp: fmt.Sprintf("ts=%s lvl=error msg=test a=tag id=10 test_a=this test_b=other\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.GroupedFields("test", []log.Field{
log.String("a", "this"),
log.String("b", "other"),
fields: []diagnostic.Field{
diagnostic.GroupedFields("test", []diagnostic.Field{
diagnostic.String("a", "this"),
diagnostic.String("b", "other"),
}),
},
},
Expand All @@ -265,18 +267,18 @@ func TestLoggerWithContext(t *testing.T) {
exp: fmt.Sprintf("ts=%s lvl=error msg=test a=tag id=10 test_0=this test_1=other\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.Strings("test", []string{"this", "other"}),
fields: []diagnostic.Field{
diagnostic.Strings("test", []string{"this", "other"}),
},
},
{
name: "two fields",
exp: fmt.Sprintf("ts=%s lvl=error msg=test a=tag id=10 testing=\"that this\" works=1s\n", nowStr),
lvl: "error",
msg: "test",
fields: []log.Field{
log.String("testing", "that this"),
log.Duration("works", time.Second),
fields: []diagnostic.Field{
diagnostic.String("testing", "that this"),
diagnostic.Duration("works", time.Second),
},
},
}
Expand All @@ -296,11 +298,11 @@ func TestLoggerWithContext(t *testing.T) {
func TestLogger_SetLeveF(t *testing.T) {
var logLine string
buf := bytes.NewBuffer(nil)
l := log.NewLogger(buf)
l := diagnostic.NewServerLogger(buf)
msg := "the message"

l.SetLevelF(func(lvl log.Level) bool {
return lvl >= log.DebugLevel
l.SetLevelF(func(lvl diagnostic.Level) bool {
return lvl >= diagnostic.DebugLevel
})
l.Debug(msg)
logLine = buf.String()
Expand Down Expand Up @@ -331,8 +333,8 @@ func TestLogger_SetLeveF(t *testing.T) {
return
}

l.SetLevelF(func(lvl log.Level) bool {
return lvl >= log.InfoLevel
l.SetLevelF(func(lvl diagnostic.Level) bool {
return lvl >= diagnostic.InfoLevel
})
l.Debug(msg)
logLine = buf.String()
Expand Down Expand Up @@ -363,8 +365,8 @@ func TestLogger_SetLeveF(t *testing.T) {
return
}

l.SetLevelF(func(lvl log.Level) bool {
return lvl >= log.WarnLevel
l.SetLevelF(func(lvl diagnostic.Level) bool {
return lvl >= diagnostic.WarnLevel
})
l.Debug(msg)
logLine = buf.String()
Expand Down Expand Up @@ -395,8 +397,8 @@ func TestLogger_SetLeveF(t *testing.T) {
return
}

l.SetLevelF(func(lvl log.Level) bool {
return lvl >= log.ErrorLevel
l.SetLevelF(func(lvl diagnostic.Level) bool {
return lvl >= diagnostic.ErrorLevel
})
l.Debug(msg)
logLine = buf.String()
Expand Down
12 changes: 7 additions & 5 deletions services/diagnostic/queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"reflect"
"testing"
"time"

"github.com/influxdata/kapacitor/services/diagnostic"
)

func TestQueue(t *testing.T) {
Expand All @@ -13,25 +15,25 @@ func TestQueue(t *testing.T) {
Time: now,
Message: "0",
Level: "info",
Fields: []log.Field{log.String("test", "0")},
Fields: []diagnostic.Field{diagnostic.String("test", "0")},
},
{
Time: now,
Message: "1",
Level: "debug",
Fields: []log.Field{log.String("test", "1")},
Fields: []diagnostic.Field{diagnostic.String("test", "1")},
},
{
Time: now,
Message: "2",
Level: "warn",
Fields: []log.Field{log.String("test", "2")},
Fields: []diagnostic.Field{diagnostic.String("test", "2")},
},
{
Time: now,
Message: "3",
Level: "warn",
Fields: []log.Field{log.String("test", "3"), log.Int("number", 3)},
Fields: []diagnostic.Field{diagnostic.String("test", "3"), diagnostic.Int("number", 3)},
},
}

Expand Down Expand Up @@ -68,7 +70,7 @@ func TestEnqueDequeueDequeue(t *testing.T) {
Time: now,
Message: "0",
Level: "info",
Fields: []log.Field{log.String("test", "0")},
Fields: []diagnostic.Field{diagnostic.String("test", "0")},
}

q := &diagnostic.Queue{}
Expand Down

0 comments on commit 013f733

Please sign in to comment.