go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/luciexe/build/logging_test.go (about)

     1  // Copyright 2020 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package build
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  
    23  	"go.chromium.org/luci/common/logging"
    24  	"go.chromium.org/luci/common/logging/memlogger"
    25  )
    26  
    27  func TestLogableNoop(t *testing.T) {
    28  	Convey(`Loggable Noop`, t, func() {
    29  		Convey(`nop`, func() {
    30  			var nop nopStream
    31  			n, err := nop.Write([]byte("hey"))
    32  			So(n, ShouldEqual, 3)
    33  			So(err, ShouldBeNil)
    34  
    35  			So(nop.Close(), ShouldBeNil)
    36  		})
    37  
    38  		Convey(`nopDatagram`, func() {
    39  			var nop nopDatagramStream
    40  			So(nop.WriteDatagram([]byte("hey")), ShouldBeNil)
    41  			So(nop.Close(), ShouldBeNil)
    42  		})
    43  
    44  		Convey(`loggingWriter`, func() {
    45  			ctx := memlogger.Use(context.Background())
    46  			logs := logging.Get(ctx).(*memlogger.MemLogger)
    47  			lw := makeLoggingWriter(ctx, "some log")
    48  
    49  			Convey(`single line`, func() {
    50  				n, err := lw.Write([]byte("hello world\n"))
    51  				So(n, ShouldEqual, 12)
    52  				So(err, ShouldBeNil)
    53  
    54  				So(logs, memlogger.ShouldHaveLog, logging.Info, "hello world", logging.Fields{
    55  					"build.logname": "some log",
    56  				})
    57  
    58  				So(lw.Close(), ShouldBeNil)
    59  				So(logs.Messages(), ShouldHaveLength, 1)
    60  			})
    61  
    62  			Convey(`multi line`, func() {
    63  				n, err := lw.Write([]byte("hello world\ncool\nbeans\n"))
    64  				So(n, ShouldEqual, 23)
    65  				So(err, ShouldBeNil)
    66  
    67  				So(logs, memlogger.ShouldHaveLog, logging.Info, "hello world")
    68  				So(logs, memlogger.ShouldHaveLog, logging.Info, "cool")
    69  				So(logs, memlogger.ShouldHaveLog, logging.Info, "beans")
    70  
    71  				So(lw.Close(), ShouldBeNil)
    72  				So(logs.Messages(), ShouldHaveLength, 3)
    73  			})
    74  
    75  			Convey(`partial line`, func() {
    76  				n, err := lw.Write([]byte("hello worl"))
    77  				So(n, ShouldEqual, 10)
    78  				So(err, ShouldBeNil)
    79  
    80  				So(logs.Messages(), ShouldHaveLength, 0)
    81  
    82  				n, err = lw.Write([]byte("d\ncool\n\n\n"))
    83  				So(n, ShouldEqual, 9)
    84  				So(err, ShouldBeNil)
    85  
    86  				So(logs, memlogger.ShouldHaveLog, logging.Info, "hello world")
    87  				So(logs, memlogger.ShouldHaveLog, logging.Info, "cool")
    88  				So(logs, memlogger.ShouldHaveLog, logging.Info, "")
    89  
    90  				So(lw.Close(), ShouldBeNil)
    91  				So(logs.Messages(), ShouldHaveLength, 4)
    92  			})
    93  
    94  			Convey(`partial flush`, func() {
    95  				n, err := lw.Write([]byte("hello worl"))
    96  				So(n, ShouldEqual, 10)
    97  				So(err, ShouldBeNil)
    98  
    99  				So(lw.Close(), ShouldBeNil)
   100  				So(logs.Messages(), ShouldHaveLength, 1)
   101  
   102  				So(logs, memlogger.ShouldHaveLog, logging.Info, "hello worl")
   103  			})
   104  		})
   105  
   106  		Convey(`loggingWriter - debug`, func() {
   107  			ctx := memlogger.Use(context.Background())
   108  			ctx = logging.SetLevel(ctx, logging.Debug)
   109  			logs := logging.Get(ctx).(*memlogger.MemLogger)
   110  			lw := makeLoggingWriter(ctx, "$some log")
   111  
   112  			n, err := lw.Write([]byte("hello world\n"))
   113  			So(n, ShouldEqual, 12)
   114  			So(err, ShouldBeNil)
   115  
   116  			So(logs, memlogger.ShouldHaveLog, logging.Debug, "hello world", logging.Fields{
   117  				"build.logname": "$some log",
   118  			})
   119  
   120  			So(lw.Close(), ShouldBeNil)
   121  			So(logs.Messages(), ShouldHaveLength, 1)
   122  		})
   123  
   124  		Convey(`loggingWriter - debug skip`, func() {
   125  			ctx := memlogger.Use(context.Background())
   126  			ctx = logging.SetLevel(ctx, logging.Info) // ignore debug
   127  			logs := logging.Get(ctx).(*memlogger.MemLogger)
   128  			lw := makeLoggingWriter(ctx, "$some log")
   129  
   130  			n, err := lw.Write([]byte("hello world\n"))
   131  			So(n, ShouldEqual, 12)
   132  			So(err, ShouldBeNil)
   133  
   134  			So(lw.Close(), ShouldBeNil)
   135  			So(logs.Messages(), ShouldHaveLength, 0)
   136  		})
   137  	})
   138  }