github.com/apache/beam/sdks/v2@v2.48.2/go/container/tools/logging_test.go (about) 1 // Licensed to the Apache Software Foundation (ASF) under one or more 2 // contributor license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright ownership. 4 // The ASF licenses this file to You under the Apache License, Version 2.0 5 // (the "License"); you may not use this file except in compliance with 6 // the License. You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package tools 17 18 import ( 19 "bytes" 20 "context" 21 "errors" 22 "log" 23 "strings" 24 "testing" 25 26 fnpb "github.com/apache/beam/sdks/v2/go/pkg/beam/model/fnexecution_v1" 27 ) 28 29 type logCatcher struct { 30 msgs []*fnpb.LogEntry_List 31 err error 32 } 33 34 func (l *logCatcher) Send(msg *fnpb.LogEntry_List) error { 35 l.msgs = append(l.msgs, msg) 36 return l.err 37 } 38 39 func (l *logCatcher) CloseSend() error { 40 return nil 41 } 42 43 func TestLogger(t *testing.T) { 44 ctx := context.Background() 45 t.Run("SuccessfulLogging", func(t *testing.T) { 46 catcher := &logCatcher{} 47 l := &Logger{client: catcher} 48 49 l.Printf(ctx, "foo %v", "bar") 50 51 received := catcher.msgs[0].GetLogEntries()[0] 52 53 if got, want := received.Message, "foo bar"; got != want { 54 t.Errorf("l.Printf(\"foo %%v\", \"bar\"): got message %q, want %q", got, want) 55 } 56 57 if got, want := received.Severity, fnpb.LogEntry_Severity_DEBUG; got != want { 58 t.Errorf("l.Printf(\"foo %%v\", \"bar\"): got severity %v, want %v", got, want) 59 } 60 }) 61 t.Run("backup path", func(t *testing.T) { 62 catcher := &logCatcher{} 63 l := &Logger{client: catcher} 64 65 // Validate error outputs. 66 var buf bytes.Buffer 67 ll := log.Default() 68 ll.SetOutput(&buf) 69 70 catcher.err = errors.New("test error") 71 wantMsg := "checking for error?" 72 l.Printf(ctx, wantMsg) 73 74 line, err := buf.ReadString('\n') 75 if err != nil { 76 t.Errorf("unexpected error reading form backup log buffer: %v", err) 77 } 78 79 if got, want := line, "boot.go: error logging message over FnAPI"; !strings.Contains(got, want) { 80 t.Errorf("backup log buffer didn't contain expected log, got %q, want it to contain %q", got, want) 81 } 82 if got, want := line, "test error"; !strings.Contains(got, want) { 83 t.Errorf("backup log buffer didn't contain expected log, got %q, want it to contain %q", got, want) 84 } 85 86 line, err = buf.ReadString('\n') 87 if err != nil { 88 t.Errorf("unexpected error reading form backup log buffer: %v", err) 89 } 90 91 if got, want := line, wantMsg; !strings.Contains(got, want) { 92 t.Errorf("backup log buffer didn't contain the message, got %q, want it to contain %q", got, want) 93 } 94 }) 95 96 t.Run("no endpoint", func(t *testing.T) { 97 l := &Logger{} 98 99 var buf bytes.Buffer 100 ll := log.Default() 101 ll.SetOutput(&buf) 102 103 l.Printf(ctx, "trying to log") 104 105 line, err := buf.ReadString('\n') 106 if err != nil { 107 t.Errorf("unexpected error reading form backup log buffer: %v", err) 108 } 109 if got, want := line, "no logging endpoint set"; !strings.Contains(got, want) { 110 t.Errorf("backup log buffer didn't contain expected error, got %q, want it to contain %q", got, want) 111 } 112 113 }) 114 115 }