github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/log/log_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package log 5 6 import ( 7 "bytes" 8 "net/url" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestSecrets(t *testing.T) { 15 t.Run("should log", func(t *testing.T) { 16 secret := "password" 17 18 outWriter := Entry().Logger.Out 19 var buffer bytes.Buffer 20 Entry().Logger.SetOutput(&buffer) 21 defer func() { Entry().Logger.SetOutput(outWriter) }() 22 23 Entry().Infof("My secret is %s.", secret) 24 assert.Contains(t, buffer.String(), secret) 25 26 buffer.Reset() 27 RegisterSecret(secret) 28 Entry().Infof("My secret is %s.", secret) 29 assert.NotContains(t, buffer.String(), secret) 30 }) 31 32 t.Run("should log url encoded", func(t *testing.T) { 33 secret := "secret-token!0" 34 encodedSecret := url.QueryEscape(secret) 35 36 outWriter := Entry().Logger.Out 37 var buffer bytes.Buffer 38 Entry().Logger.SetOutput(&buffer) 39 defer func() { Entry().Logger.SetOutput(outWriter) }() 40 41 Entry().Infof("My secret is %s.", secret) 42 assert.Contains(t, buffer.String(), secret) 43 44 buffer.Reset() 45 RegisterSecret(secret) 46 Entry().Infof("My secret is %s.", encodedSecret) 47 assert.NotContains(t, buffer.String(), encodedSecret) 48 }) 49 } 50 51 func TestWriteLargeBuffer(t *testing.T) { 52 t.Run("should log large buffer without linebreaks via Writer()", func(t *testing.T) { 53 b := []byte("a") 54 size := 131072 55 b = bytes.Repeat(b, size) 56 written, err := Writer().Write(b) 57 assert.Equal(t, size, written) 58 assert.NoError(t, err) 59 }) 60 t.Run("fails writing large buffer without linebreaks via Entry().Writer()", func(t *testing.T) { 61 // NOTE: If this test starts failing, then the logrus issue has been fixed and 62 // the work-around with Writer() can be removed. 63 b := []byte("a") 64 size := 131072 65 b = bytes.Repeat(b, size) 66 written, err := Entry().Writer().Write(b) 67 assert.Error(t, err) 68 assert.True(t, size != written) 69 }) 70 }