Add testutil.RequireLogLines to verify multiple log lines at once

This commit is contained in:
Joshua Casey
2024-11-27 13:53:00 -06:00
parent 4f9530eec7
commit b20e890f15
4 changed files with 27 additions and 19 deletions
+2 -7
View File
@@ -11,7 +11,6 @@ import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
@@ -3293,14 +3292,10 @@ func TestGetKubeconfig(t *testing.T) {
require.NoError(t, err)
}
var expectedLogs string
if tt.wantLogs != nil {
temp := tt.wantLogs(string(testServerCA), testServer.URL)
if len(temp) > 0 {
expectedLogs = strings.Join(tt.wantLogs(string(testServerCA), testServer.URL), "\n") + "\n"
}
wantLogs := tt.wantLogs(string(testServerCA), testServer.URL)
testutil.RequireLogLines(t, wantLogs, &log)
}
require.Equal(t, expectedLogs, log.String())
expectedStdout := ""
if tt.wantStdout != nil {
@@ -2555,12 +2555,7 @@ func TestController(t *testing.T) {
require.Len(t, actualIDP.Status.Conditions, countExpectedConditions)
require.Equal(t, tt.wantResultingUpstreams[i], *actualIDP)
}
expectedLogs := ""
if len(tt.wantLogs) > 0 {
expectedLogs = strings.Join(tt.wantLogs, "\n") + "\n"
}
require.Equal(t, expectedLogs, log.String())
testutil.RequireLogLines(t, tt.wantLogs, &log)
// This needs to happen after the expected condition LastTransitionTime has been updated.
wantActions := make([]coretesting.Action, 3+len(tt.wantResultingUpstreams))
+2 -6
View File
@@ -4,12 +4,10 @@
package ptls
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
"go.pinniped.dev/internal/plog"
"go.pinniped.dev/internal/testutil"
)
func TestLogAllProfiles(t *testing.T) {
@@ -22,7 +20,5 @@ func TestLogAllProfiles(t *testing.T) {
`{"level":"info","timestamp":"2099-08-08T13:57:36.123456Z","caller":"ptls/log_profiles.go:<line>$ptls.logProfile","message":"tls configuration","profile name":"DefaultLDAP","MinVersion":"TLS 1.2","MaxVersion":"NONE","CipherSuites":["TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256","TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256","TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384","TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384","TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256","TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256","TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA","TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA","TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA","TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA"],"NextProtos":["h2","http/1.1"]}`,
`{"level":"info","timestamp":"2099-08-08T13:57:36.123456Z","caller":"ptls/log_profiles.go:<line>$ptls.logProfile","message":"tls configuration","profile name":"Secure","MinVersion":"TLS 1.3","MaxVersion":"NONE","CipherSuites":[],"NextProtos":["h2","http/1.1"]}`,
}
expectedOutput := strings.Join(expectedLines, "\n") + "\n"
require.Equal(t, expectedOutput, log.String())
testutil.RequireLogLines(t, expectedLines, log)
}
+22
View File
@@ -0,0 +1,22 @@
// Copyright 2024 the Pinniped contributors. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package testutil
import (
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func RequireLogLines(t *testing.T, wantLogs []string, log *bytes.Buffer) {
t.Helper()
expectedLogs := ""
if len(wantLogs) > 0 {
expectedLogs = strings.Join(wantLogs, "\n") + "\n"
}
require.Equal(t, expectedLogs, log.String())
}