github.com/whiteboxio/flow@v0.0.3-0.20190918184116-508d75d68a2c/pkg/corev1alpha1/logger_test.go (about) 1 package corev1alpha1 2 3 import ( 4 "bufio" 5 "bytes" 6 "fmt" 7 "math/rand" 8 "strings" 9 "testing" 10 "time" 11 12 testutil "github.com/awesome-flow/flow/pkg/util/test" 13 ) 14 15 func TestLogger(t *testing.T) { 16 out := new(bytes.Buffer) 17 l := NewLogger(out) 18 if err := l.Start(); err != nil { 19 t.Fatalf("failed to start logger") 20 } 21 sever := []LogSev{ 22 LogSevDebug, 23 LogSevTrace, 24 LogSevInfo, 25 LogSevWarn, 26 LogSevError, 27 } 28 funcs := map[LogSev]func(string, ...interface{}){ 29 LogSevDebug: l.Debug, 30 LogSevTrace: l.Trace, 31 LogSevInfo: l.Info, 32 LogSevWarn: l.Warn, 33 LogSevError: l.Error, 34 } 35 res := make([]string, 0, 1024) 36 rand.Seed(time.Now().UnixNano()) 37 38 for i := 0; i < cap(res); i++ { 39 ix := rand.Intn(len(sever)) 40 logmsg := testutil.RandBytes(testutil.RandInt(1024)) 41 res = append(res, fmt.Sprintf("%s\t%s", LogSevLex[sever[ix]], logmsg)) 42 funcs[sever[ix]](string(logmsg)) 43 } 44 45 if err := l.Stop(); err != nil { 46 t.Fatalf("failed to stop logger: %s", err) 47 } 48 49 scanner := bufio.NewScanner(bytes.NewReader(out.Bytes())) 50 51 ix := 0 52 for scanner.Scan() { 53 logline := scanner.Text() 54 if !strings.HasSuffix(logline, res[ix]) { 55 t.Fatalf("logline %q is expected to contain suffix %q", logline, res[ix]) 56 } 57 ix++ 58 } 59 if err := scanner.Err(); err != nil { 60 t.Fatalf("scanner returned an error: %s", err) 61 } 62 if ix != len(res) { 63 t.Fatalf("output is incomplete: got: %d lines, want: %d lines", ix, len(res)) 64 } 65 }