Merge pull request #166 from mattmoyer/add-cli-test-debug-output

Add some verbose logging to TestCLILoginOIDC.
This commit is contained in:
Matt Moyer 2020-10-22 11:17:18 -05:00 committed by GitHub
commit 4b24e9c625
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 2 deletions

View File

@ -208,7 +208,7 @@ func TestCLILoginOIDC(t *testing.T) {
}
}()
reader := bufio.NewReader(stderr)
reader := bufio.NewReader(library.NewLoggerReader(t, "stderr", stderr))
line, err := reader.ReadString('\n')
if err != nil {
return fmt.Errorf("could not read login URL line from stderr: %w", err)
@ -233,7 +233,7 @@ func TestCLILoginOIDC(t *testing.T) {
err = fmt.Errorf("stdout stream closed with error: %w", closeErr)
}
}()
reader := bufio.NewReader(stdout)
reader := bufio.NewReader(library.NewLoggerReader(t, "stdout", stdout))
var out clientauthenticationv1beta1.ExecCredential
if err := json.NewDecoder(reader).Decode(&out); err != nil {
return fmt.Errorf("could not read ExecCredential from stdout: %w", err)

43
test/library/iotest.go Normal file
View File

@ -0,0 +1,43 @@
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package library
import (
"fmt"
"io"
"regexp"
"testing"
)
// NewLoggerReader wraps an io.Reader to log its input and output. It also performs some heuristic token masking.
func NewLoggerReader(t *testing.T, name string, reader io.Reader) io.Reader {
t.Helper()
return &testlogReader{t: t, name: name, r: reader}
}
type testlogReader struct {
t *testing.T
name string
r io.Reader
}
func (l *testlogReader) Read(p []byte) (n int, err error) {
l.t.Helper()
n, err = l.r.Read(p)
if err != nil {
l.t.Logf("%s > %q: %v", l.name, maskTokens(p[0:n]), err)
} else {
l.t.Logf("%s > %q", l.name, maskTokens(p[0:n]))
}
return
}
//nolint: gochecknoglobals
var tokenLike = regexp.MustCompile(`(?mi)[a-zA-Z0-9._-]{30,}|[a-zA-Z0-9]{20,}`)
func maskTokens(in []byte) string {
return tokenLike.ReplaceAllStringFunc(string(in), func(t string) string {
return fmt.Sprintf("[...%d bytes...]", len(t))
})
}