| 1 |
// Copyright 2024, Google Inc. |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// Redistribution and use in source and binary forms, with or without |
| 5 |
// modification, are permitted provided that the following conditions are |
| 6 |
// met: |
| 7 |
// |
| 8 |
// - Redistributions of source code must retain the above copyright |
| 9 |
// |
| 10 |
// notice, this list of conditions and the following disclaimer. |
| 11 |
// - Redistributions in binary form must reproduce the above |
| 12 |
// |
| 13 |
// copyright notice, this list of conditions and the following disclaimer |
| 14 |
// in the documentation and/or other materials provided with the |
| 15 |
// distribution. |
| 16 |
// - Neither the name of Google Inc. nor the names of its |
| 17 |
// |
| 18 |
// contributors may be used to endorse or promote products derived from |
| 19 |
// this software without specific prior written permission. |
| 20 |
// |
| 21 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
|
| 33 |
package grpclog |
| 34 |
|
| 35 |
import ( |
| 36 |
"context" |
| 37 |
"flag" |
| 38 |
"log/slog" |
| 39 |
"os" |
| 40 |
"testing" |
| 41 |
|
| 42 |
"github.com/googleapis/gax-go/v2/internallog/internal" |
| 43 |
"github.com/googleapis/gax-go/v2/internallog/internal/bookpb" |
| 44 |
"github.com/googleapis/gax-go/v2/internallog/internal/logtest" |
| 45 |
"google.golang.org/grpc/metadata" |
| 46 |
) |
| 47 |
|
| 48 |
// To update conformance tests in this package run `go test -update_golden` |
| 49 |
func TestMain(m *testing.M) { |
| 50 |
flag.Parse() |
| 51 |
os.Exit(m.Run()) |
| 52 |
} |
| 53 |
|
| 54 |
func TestLog_protoMessageRequest(t *testing.T) { |
| 55 |
golden := "request.log" |
| 56 |
t.Setenv(internal.LoggingLevelEnvVar, "debug") |
| 57 |
logger, f := setupLogger(t, golden) |
| 58 |
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("foo", "bar")) |
| 59 |
book := &bookpb.Book{ |
| 60 |
Title: "The book", |
| 61 |
Author: "The author", |
| 62 |
} |
| 63 |
logger.DebugContext(ctx, "msg", "request", ProtoMessageRequest(ctx, book)) |
| 64 |
f.Close() |
| 65 |
logtest.DiffTest(t, f.Name(), golden) |
| 66 |
} |
| 67 |
|
| 68 |
func TestLog_protoMessageResponse(t *testing.T) { |
| 69 |
golden := "response.log" |
| 70 |
t.Setenv(internal.LoggingLevelEnvVar, "debug") |
| 71 |
logger, f := setupLogger(t, golden) |
| 72 |
ctx := context.Background() |
| 73 |
book := &bookpb.Book{ |
| 74 |
Title: "The book", |
| 75 |
Author: "The author", |
| 76 |
} |
| 77 |
logger.DebugContext(ctx, "msg", "response", ProtoMessageResponse(book)) |
| 78 |
f.Close() |
| 79 |
logtest.DiffTest(t, f.Name(), golden) |
| 80 |
} |
| 81 |
|
| 82 |
func setupLogger(t *testing.T, golden string) (*slog.Logger, *os.File) { |
| 83 |
t.Helper() |
| 84 |
f, err := os.CreateTemp(t.TempDir(), golden) |
| 85 |
if err != nil { |
| 86 |
t.Fatal(err) |
| 87 |
} |
| 88 |
logger := internal.NewLoggerWithWriter(f) |
| 89 |
return logger, f |
| 90 |
} |