| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package logging | 5 package logging |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 » "sync" | 8 » "fmt" |
| 9 "testing" | 9 "testing" |
| 10 | 10 |
| 11 » log "github.com/luci/luci-go/common/logging" | 11 » "github.com/luci/luci-go/common/logging" |
| 12 "github.com/luci/luci-go/common/logging/memlogger" | 12 "github.com/luci/luci-go/common/logging/memlogger" |
| 13 | 13 |
| 14 "google.golang.org/grpc/grpclog" | 14 "google.golang.org/grpc/grpclog" |
| 15 | 15 |
| 16 . "github.com/smartystreets/goconvey/convey" | 16 . "github.com/smartystreets/goconvey/convey" |
| 17 ) | 17 ) |
| 18 | 18 |
| 19 type grpcInstallMutex sync.Mutex | |
| 20 | |
| 21 // TestGRPCLogger assumes that it has exclusive ownership of the grpclog | 19 // TestGRPCLogger assumes that it has exclusive ownership of the grpclog |
| 22 // package. Each test MUST execute in series, as they install a global logger | 20 // package. Each test MUST execute in series, as they install a global logger |
| 23 // into the grpclog package. | 21 // into the grpclog package. |
| 24 func TestGRPCLogger(t *testing.T) { | 22 func TestGRPCLogger(t *testing.T) { |
| 25 Convey(`Testing gRPC logger adapter`, t, func() { | 23 Convey(`Testing gRPC logger adapter`, t, func() { |
| 26 var base memlogger.MemLogger | 24 var base memlogger.MemLogger |
| 27 | 25 |
| 28 » » // Stub the "os.Exit" functionality so we don't actually exit. | 26 » » // Override "fatalExit" so our Fatal* tests don't actually exit. |
| 29 » » var exitRC int | 27 » » exitCalls := 0 |
| 30 » » osExit = func(rc int) { exitRC = rc } | 28 » » fatalExit = func() { |
| 29 » » » exitCalls++ |
| 30 » » } |
| 31 | 31 |
| 32 » » Convey(`An adapter that logs prints`, func() { | 32 » » for _, l := range []logging.Level{ |
| 33 » » » Install(&base, true) | 33 » » » logging.Debug, |
| 34 » » » logging.Info, |
| 35 » » » logging.Warning, |
| 36 » » » logging.Error, |
| 37 » » » Suppress, |
| 38 » » } { |
| 39 » » » var name string |
| 40 » » » if l == Suppress { |
| 41 » » » » name = "SUPPRESSED" |
| 42 » » » } else { |
| 43 » » » » name = l.String() |
| 44 » » » } |
| 45 » » » Convey(fmt.Sprintf(`At logging level %q`, name), func()
{ |
| 46 » » » » Convey(`Logs correctly`, func() { |
| 47 » » » » » Install(&base, l) |
| 34 | 48 |
| 35 » » » Convey(`Can log a fatal message.`, func() { | 49 » » » » » // expected will accumulate during loggi
ng based on the current test |
| 36 » » » » grpclog.Fatalln("foo", 1, "bar", 2) | 50 » » » » » // log level, "l". |
| 37 » » » » So(exitRC, ShouldEqual, 255) | 51 » » » » » var expected []memlogger.LogEntry |
| 38 | 52 |
| 39 » » » » exitRC = 0 | 53 » » » » » // Info |
| 40 » » » » grpclog.Fatal("foo", 1, "bar", 2) | 54 » » » » » grpclog.Info("info", "foo", "bar") |
| 41 » » » » So(exitRC, ShouldEqual, 255) | 55 » » » » » grpclog.Infof("infof(%q, %q)", "foo", "b
ar") |
| 56 » » » » » grpclog.Infoln("infoln", "foo", "bar") |
| 57 » » » » » switch l { |
| 58 » » » » » case Suppress, logging.Error, logging.Wa
rning, logging.Info: |
| 59 » » » » » default: |
| 60 » » » » » » expected = append(expected, []me
mlogger.LogEntry{ |
| 61 » » » » » » » {logging.Debug, "info fo
o bar", nil, 3}, |
| 62 » » » » » » » {logging.Debug, `infof("
foo", "bar")`, nil, 3}, |
| 63 » » » » » » » {logging.Debug, "infoln
foo bar", nil, 3}, |
| 64 » » » » » » }...) |
| 65 » » » » » } |
| 42 | 66 |
| 43 » » » » // Actually test the log messages for "Fatalf". | 67 » » » » » // Warning |
| 44 » » » » base.Reset() | 68 » » » » » grpclog.Warning("warning", "foo", "bar") |
| 45 » » » » exitRC = 0 | 69 » » » » » grpclog.Warningf("warningf(%q, %q)", "fo
o", "bar") |
| 46 » » » » grpclog.Fatalf("foo(%d)", 42) | 70 » » » » » grpclog.Warningln("warningln", "foo", "b
ar") |
| 47 » » » » So(exitRC, ShouldEqual, 255) | 71 » » » » » switch l { |
| 72 » » » » » case Suppress, logging.Error: |
| 73 » » » » » default: |
| 74 » » » » » » expected = append(expected, []me
mlogger.LogEntry{ |
| 75 » » » » » » » {logging.Warning, "warni
ng foo bar", nil, 3}, |
| 76 » » » » » » » {logging.Warning, `warni
ngf("foo", "bar")`, nil, 3}, |
| 77 » » » » » » » {logging.Warning, "warni
ngln foo bar", nil, 3}, |
| 78 » » » » » » }...) |
| 79 » » » » » } |
| 48 | 80 |
| 49 » » » » msgs := base.Messages() | 81 » » » » » // Error |
| 50 » » » » So(msgs, ShouldHaveLength, 2) | 82 » » » » » grpclog.Error("error", "foo", "bar") |
| 83 » » » » » grpclog.Errorf("errorf(%q, %q)", "foo",
"bar") |
| 84 » » » » » grpclog.Errorln("errorln", "foo", "bar") |
| 85 » » » » » switch l { |
| 86 » » » » » case Suppress: |
| 87 » » » » » default: |
| 88 » » » » » » expected = append(expected, []me
mlogger.LogEntry{ |
| 89 » » » » » » » {logging.Error, "error f
oo bar", nil, 3}, |
| 90 » » » » » » » {logging.Error, `errorf(
"foo", "bar")`, nil, 3}, |
| 91 » » » » » » » {logging.Error, "errorln
foo bar", nil, 3}, |
| 92 » » » » » » }...) |
| 93 » » » » » } |
| 51 | 94 |
| 52 » » » » So(msgs[0].Level, ShouldEqual, log.Error) | 95 » » » » » So(base.Messages(), ShouldResemble, expe
cted) |
| 53 » » » » So(msgs[0].CallDepth, ShouldEqual, 3) | |
| 54 » » » » So(msgs[0].Msg, ShouldEqual, "foo(42)") | |
| 55 | 96 |
| 56 » » » » So(msgs[1].Level, ShouldEqual, log.Error) | 97 » » » » » for i := 0; i < 3; i++ { |
| 57 » » » » So(msgs[1].CallDepth, ShouldEqual, 4) | 98 » » » » » » exp := i <= translateLevel(l) |
| 58 » » » » So(msgs[1].Msg, ShouldStartWith, "Stack Trace:\n
") | 99 » » » » » » t.Logf("Testing %q V(%d) => %v",
name, i, exp) |
| 100 » » » » » » So(grpclog.V(i), ShouldEqual, ex
p) |
| 101 » » » » » } |
| 102 » » » » }) |
| 59 | 103 |
| 60 » » » }) | 104 » » » » // XXX: disabled, pending https://github.com/grp
c/grpc-go/issues/1360 |
| 105 » » » » SkipConvey(`Handles Fatal calls`, func() { |
| 106 » » » » » grpclog.Fatal("fatal", "foo", "bar") |
| 107 » » » » » grpclog.Fatalf("fatalf(%q, %q)", "foo",
"bar") |
| 108 » » » » » grpclog.Fatalln("fatalln", "foo", "bar") |
| 61 | 109 |
| 62 » » » Convey(`Will log a print message.`, func() { | 110 » » » » » So(base.Messages(), ShouldResemble, []me
mlogger.LogEntry{ |
| 63 » » » » grpclog.Printf("foo(%d)", 42) | 111 » » » » » » {logging.Error, "fatal foo bar",
nil, 3}, |
| 64 » » » » grpclog.Println("bar") | 112 » » » » » » {logging.Error, `fatalf("foo", "
bar")`, nil, 3}, |
| 65 » » » » grpclog.Print("baz", 1337) | 113 » » » » » » {logging.Error, "fatalln foo bar
", nil, 3}, |
| 66 » » » » So(exitRC, ShouldEqual, 0) | 114 » » » » » }) |
| 67 | 115 » » » » » So(exitCalls, ShouldEqual, 3) |
| 68 » » » » msgs := base.Messages() | |
| 69 » » » » So(msgs, ShouldResemble, []memlogger.LogEntry{ | |
| 70 » » » » » {log.Info, "foo(42)", nil, 3}, | |
| 71 » » » » » {log.Info, "bar", nil, 3}, | |
| 72 » » » » » {log.Info, "baz 1337", nil, 3}, | |
| 73 }) | 116 }) |
| 74 }) | 117 }) |
| 75 » » }) | 118 » » } |
| 76 | |
| 77 » » Convey(`An adapter that doesn't log print`, func() { | |
| 78 » » » Install(&base, false) | |
| 79 | |
| 80 » » » Convey(`Still prints fatal messages and exits.`, func()
{ | |
| 81 » » » » grpclog.Fatal("Something weng wrong!") | |
| 82 » » » » So(exitRC, ShouldEqual, 255) | |
| 83 » » » » So(base.Messages(), ShouldHaveLength, 2) | |
| 84 » » » }) | |
| 85 | |
| 86 » » » Convey(`Doesn't forwawrd Print messages.`, func() { | |
| 87 » » » » grpclog.Printf("foo(%d)", 42) | |
| 88 » » » » grpclog.Println("bar") | |
| 89 » » » » grpclog.Print("baz", 1337) | |
| 90 » » » » So(exitRC, ShouldEqual, 0) | |
| 91 » » » » So(base.Messages(), ShouldHaveLength, 0) | |
| 92 » » » }) | |
| 93 » » }) | |
| 94 }) | 119 }) |
| 95 } | 120 } |
| OLD | NEW |