|
| 1 | +// Copyright 2026 Google LLC |
| 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 galog |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "strings" |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | +) |
| 23 | + |
| 24 | +// helper1 and newTestEntry together wrapper functions to simulate a stack |
| 25 | +// depth of 3 layers between the test code calling newTestEntry and the |
| 26 | +// runtime.Caller(3) call site inside newEntry. |
| 27 | +func helper1(level Level, prefix string, msg string) *LogEntry { |
| 28 | + return newEntry(level, prefix, msg) |
| 29 | +} |
| 30 | + |
| 31 | +func newTestEntry(level Level, prefix string, msg string) *LogEntry { |
| 32 | + return helper1(level, prefix, msg) |
| 33 | +} |
| 34 | + |
| 35 | +func TestStdoutWriteFailure(t *testing.T) { |
| 36 | + logBuffer := &errorWriter{failureType: writeFailure} |
| 37 | + be := NewStdoutBackend() |
| 38 | + be.writer = logBuffer |
| 39 | + |
| 40 | + entry := newTestEntry(InfoLevel, "", "foobar") |
| 41 | + if err := be.Log(entry); err == nil { |
| 42 | + t.Fatalf("Log(%+v) succeeded, want error due to write failure", entry) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestStdoutWriteLenFailure(t *testing.T) { |
| 47 | + logBuffer := &errorWriter{failureType: writeLenFailure} |
| 48 | + be := NewStdoutBackend() |
| 49 | + be.writer = logBuffer |
| 50 | + |
| 51 | + entry := newTestEntry(InfoLevel, "", "foobar") |
| 52 | + if err := be.Log(entry); err == nil { |
| 53 | + t.Fatalf("Log(%+v) succeeded, want error due to write len failure", entry) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestStdoutInvalidFormat(t *testing.T) { |
| 58 | + logBuffer := bytes.NewBuffer(nil) |
| 59 | + be := NewStdoutBackend() |
| 60 | + be.writer = logBuffer |
| 61 | + |
| 62 | + be.Config().SetFormat(InfoLevel, "{{.Foobar}}") |
| 63 | + |
| 64 | + entry := newTestEntry(InfoLevel, "", "foobar") |
| 65 | + if err := be.Log(entry); err == nil { |
| 66 | + t.Fatalf("Log(%+v) succeeded, want error due to invalid format", entry) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestStdoutSuccess(t *testing.T) { |
| 71 | + tests := []struct { |
| 72 | + desc string |
| 73 | + message string |
| 74 | + level Level |
| 75 | + prefix string |
| 76 | + want string |
| 77 | + }{ |
| 78 | + { |
| 79 | + desc: "error_level_skip", |
| 80 | + message: "foo bar", |
| 81 | + level: ErrorLevel, |
| 82 | + want: "", |
| 83 | + }, |
| 84 | + { |
| 85 | + desc: "fatal_level_skip", |
| 86 | + message: "foo bar", |
| 87 | + level: FatalLevel, |
| 88 | + want: "", |
| 89 | + }, |
| 90 | + { |
| 91 | + desc: "warning_level", |
| 92 | + message: "foo bar", |
| 93 | + level: WarningLevel, |
| 94 | + want: "[WARNING]: foo bar\n", |
| 95 | + }, |
| 96 | + { |
| 97 | + desc: "info_level", |
| 98 | + message: "foo bar", |
| 99 | + level: InfoLevel, |
| 100 | + want: "[INFO]: foo bar\n", |
| 101 | + }, |
| 102 | + { |
| 103 | + desc: "info_level_with_prefix", |
| 104 | + message: "foo bar", |
| 105 | + level: InfoLevel, |
| 106 | + prefix: "my-prefix", |
| 107 | + want: " my-prefix: [INFO]: foo bar\n", |
| 108 | + }, |
| 109 | + { |
| 110 | + desc: "debug_level", |
| 111 | + message: "foo bar", |
| 112 | + level: DebugLevel, |
| 113 | + want: "[DEBUG]:", |
| 114 | + }, |
| 115 | + } |
| 116 | + |
| 117 | + for _, tc := range tests { |
| 118 | + t.Run(tc.desc, func(t *testing.T) { |
| 119 | + logBuffer := bytes.NewBuffer(nil) |
| 120 | + be := NewStdoutBackend() |
| 121 | + be.writer = logBuffer |
| 122 | + if be.Config() == nil { |
| 123 | + t.Fatal("NewStdoutBackend() failed: Config() returned nil") |
| 124 | + } |
| 125 | + |
| 126 | + entry := newTestEntry(tc.level, tc.prefix, tc.message) |
| 127 | + if err := be.Log(entry); err != nil { |
| 128 | + t.Fatalf("Log(%+v) failed: %v", entry, err) |
| 129 | + } |
| 130 | + |
| 131 | + got := logBuffer.String() |
| 132 | + |
| 133 | + if tc.want == "" { |
| 134 | + if logBuffer.Len() != 0 { |
| 135 | + t.Fatalf("Log(%+v) output = %q, want empty", entry, got) |
| 136 | + } |
| 137 | + return |
| 138 | + } |
| 139 | + |
| 140 | + if tc.level == DebugLevel { |
| 141 | + if !strings.Contains(got, "[DEBUG]:") || !strings.Contains(got, "galog_stdout_test.go:") || !strings.HasSuffix(got, tc.message+"\n") { |
| 142 | + t.Fatalf("Log(%+v) output = %q, want [DEBUG]:, galog_stdout_test.go:, and suffix %q", entry, got, tc.message+"\n") |
| 143 | + } |
| 144 | + } else { |
| 145 | + if !strings.HasSuffix(got, tc.want) { |
| 146 | + t.Fatalf("Log(%+v) output = %q, want suffix %q", entry, got, tc.want) |
| 147 | + } |
| 148 | + } |
| 149 | + Shutdown(time.Millisecond) |
| 150 | + }) |
| 151 | + } |
| 152 | +} |
0 commit comments