1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/pkg/log/log_test.go
Jk1484 ffc931aad1
feat(golangBuild): use 'unit' build tag to include tests during test execution (#4345)
* Added unit tag as argument. Added description to runTests command. Changed code generator to have unit build tag in generated unit test files.

* Added unit build tag to all unit test files.

* added to new unit test unit build tag

* Update verify-go.yml

* small fix

---------

Co-authored-by: Muhammadali Nazarov <Muhammadali.Nazarov@acronis.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2023-05-03 21:02:11 +05:00

71 lines
1.8 KiB
Go

//go:build unit
// +build unit
package log
import (
"bytes"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSecrets(t *testing.T) {
t.Run("should log", func(t *testing.T) {
secret := "password"
outWriter := Entry().Logger.Out
var buffer bytes.Buffer
Entry().Logger.SetOutput(&buffer)
defer func() { Entry().Logger.SetOutput(outWriter) }()
Entry().Infof("My secret is %s.", secret)
assert.Contains(t, buffer.String(), secret)
buffer.Reset()
RegisterSecret(secret)
Entry().Infof("My secret is %s.", secret)
assert.NotContains(t, buffer.String(), secret)
})
t.Run("should log url encoded", func(t *testing.T) {
secret := "secret-token!0"
encodedSecret := url.QueryEscape(secret)
outWriter := Entry().Logger.Out
var buffer bytes.Buffer
Entry().Logger.SetOutput(&buffer)
defer func() { Entry().Logger.SetOutput(outWriter) }()
Entry().Infof("My secret is %s.", secret)
assert.Contains(t, buffer.String(), secret)
buffer.Reset()
RegisterSecret(secret)
Entry().Infof("My secret is %s.", encodedSecret)
assert.NotContains(t, buffer.String(), encodedSecret)
})
}
func TestWriteLargeBuffer(t *testing.T) {
t.Run("should log large buffer without linebreaks via Writer()", func(t *testing.T) {
b := []byte("a")
size := 131072
b = bytes.Repeat(b, size)
written, err := Writer().Write(b)
assert.Equal(t, size, written)
assert.NoError(t, err)
})
t.Run("fails writing large buffer without linebreaks via Entry().Writer()", func(t *testing.T) {
// NOTE: If this test starts failing, then the logrus issue has been fixed and
// the work-around with Writer() can be removed.
b := []byte("a")
size := 131072
b = bytes.Repeat(b, size)
written, err := Entry().Writer().Write(b)
assert.Error(t, err)
assert.True(t, size != written)
})
}