Skip to content

Commit 6d29414

Browse files
committed
fix: secret tests were failing on osx due to difference in error message format
Signed-off-by: Adam Talbot <[email protected]>
1 parent ae552f4 commit 6d29414

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

pkg/inspect/secret/secret_test.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package secret
1818

1919
import (
2020
"crypto/x509"
21+
"fmt"
22+
"slices"
2123
"strings"
2224
"testing"
2325
"time"
@@ -186,7 +188,7 @@ func Test_describeDebugging(t *testing.T) {
186188
tests := []struct {
187189
name string
188190
args args
189-
want string
191+
want []string
190192
}{
191193
{
192194
name: "Debug test cert without trusting CA",
@@ -195,18 +197,19 @@ func Test_describeDebugging(t *testing.T) {
195197
intermediates: nil,
196198
ca: nil,
197199
},
198-
want: `Debugging:
199-
Trusted by this computer: no: x509: certificate signed by unknown authority
200-
CRL Status: No CRL endpoints set
201-
OCSP Status: Cannot check OCSP, does not have a CA or intermediate certificate provided`,
200+
want: []string{
201+
"Debugging:\n\tTrusted by this computer:\tno: x509: certificate signed by unknown authority\n\tCRL Status:\tNo CRL endpoints set\n\tOCSP Status:\tCannot check OCSP, does not have a CA or intermediate certificate provided",
202+
"Debugging:\n\tTrusted by this computer:\tno: x509: “cert-manager” certificate is not trusted\n\tCRL Status:\tNo CRL endpoints set\n\tOCSP Status:\tCannot check OCSP, does not have a CA or intermediate certificate provided",
203+
},
202204
},
203205
// TODO: add fake clock and test with trusting CA
204206
}
205207
for _, tt := range tests {
206208
t.Run(tt.name, func(t *testing.T) {
207209
got, err := describeDebugging(t.Context(), tt.args.cert, tt.args.intermediates, tt.args.ca)
208-
if got != tt.want {
209-
t.Errorf("describeDebugging() = %v, want %v", makeInvisibleVisible(got), makeInvisibleVisible(tt.want))
210+
211+
if len(tt.want) > 0 && !slices.Contains(tt.want, got) {
212+
t.Errorf("describeDebugging() = %q, want one of %s", got, quotedSlice(tt.want))
210213
}
211214
if err != nil {
212215
t.Errorf("describeCertificate() error = %v", err)
@@ -309,29 +312,32 @@ func Test_describeTrusted(t *testing.T) {
309312
tests := []struct {
310313
name string
311314
args args
312-
want string
315+
want []string
313316
}{
314317
{
315318
name: "Describe test certificate",
316319
args: args{
317320
cert: MustParseCertificate(t, testCert),
318321
intermediates: nil,
319322
},
320-
want: "no: x509: certificate signed by unknown authority",
323+
want: []string{
324+
"no: x509: certificate signed by unknown authority",
325+
"no: x509: “cert-manager” certificate is not trusted",
326+
},
321327
},
322328
{
323329
name: "Describe test certificate with adding it to the trust store",
324330
args: args{
325331
cert: MustParseCertificate(t, testCert),
326332
intermediates: [][]byte{[]byte(testCert)},
327333
},
328-
want: "yes",
334+
want: []string{"yes"},
329335
},
330336
}
331337
for _, tt := range tests {
332338
t.Run(tt.name, func(t *testing.T) {
333-
if got := describeTrusted(tt.args.cert, tt.args.intermediates); got != tt.want {
334-
t.Errorf("describeTrusted() = %v, want %v", makeInvisibleVisible(got), makeInvisibleVisible(tt.want))
339+
if got := describeTrusted(tt.args.cert, tt.args.intermediates); len(tt.want) > 0 && !slices.Contains(tt.want, got) {
340+
t.Errorf("describeTrusted() = %q, want one of %s", got, quotedSlice(tt.want))
335341
}
336342
})
337343
}
@@ -408,3 +414,11 @@ func makeInvisibleVisible(in string) string {
408414

409415
return in
410416
}
417+
418+
func quotedSlice(in []string) string {
419+
quoted := make([]string, len(in))
420+
for i, v := range in {
421+
quoted[i] = fmt.Sprintf("%q", v)
422+
}
423+
return strings.Join(quoted, ", ")
424+
}

0 commit comments

Comments
 (0)