From 259fd92306785b0254785ef1435e42a233b084f0 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Wed, 15 Jan 2025 21:20:43 -0800 Subject: [PATCH] http3: migrate the error tests away from Ginkgo (#4876) --- http3/error_codes_test.go | 50 +++++++++-------- http3/error_test.go | 110 +++++++++++++++++++++++++++----------- 2 files changed, 103 insertions(+), 57 deletions(-) diff --git a/http3/error_codes_test.go b/http3/error_codes_test.go index a50afc60..af7642b1 100644 --- a/http3/error_codes_test.go +++ b/http3/error_codes_test.go @@ -7,33 +7,31 @@ import ( "path" "runtime" "strconv" + "testing" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" + "github.com/stretchr/testify/require" ) -var _ = Describe("error codes", func() { - It("has a string representation for every error code", func() { - // We parse the error code file, extract all constants, and verify that - // each of them has a string version. Go FTW! - _, thisfile, _, ok := runtime.Caller(0) - if !ok { - panic("Failed to get current frame") - } - filename := path.Join(path.Dir(thisfile), "error_codes.go") - fileAst, err := parser.ParseFile(token.NewFileSet(), filename, nil, 0) - Expect(err).NotTo(HaveOccurred()) - constSpecs := fileAst.Decls[2].(*ast.GenDecl).Specs - Expect(len(constSpecs)).To(BeNumerically(">", 4)) // at time of writing - for _, c := range constSpecs { - valString := c.(*ast.ValueSpec).Values[0].(*ast.BasicLit).Value - val, err := strconv.ParseInt(valString, 0, 64) - Expect(err).NotTo(HaveOccurred()) - Expect(ErrCode(val).String()).ToNot(Equal("unknown error code")) - } - }) +func TestErrorCodes(t *testing.T) { + // We parse the error code file, extract all constants, and verify that + // each of them has a string version. Go FTW! + _, thisfile, _, ok := runtime.Caller(0) + require.True(t, ok, "Failed to get current frame") - It("has a string representation for unknown error codes", func() { - Expect(ErrCode(0x1337).String()).To(Equal("unknown error code: 0x1337")) - }) -}) + filename := path.Join(path.Dir(thisfile), "error_codes.go") + fileAst, err := parser.ParseFile(token.NewFileSet(), filename, nil, 0) + require.NoError(t, err) + + constSpecs := fileAst.Decls[2].(*ast.GenDecl).Specs + require.Greater(t, len(constSpecs), 4) // at time of writing + + for _, c := range constSpecs { + valString := c.(*ast.ValueSpec).Values[0].(*ast.BasicLit).Value + val, err := strconv.ParseInt(valString, 0, 64) + require.NoError(t, err) + require.NotEqual(t, "unknown error code", ErrCode(val).String()) + } + + // Test unknown error code + require.Equal(t, "unknown error code: 0x1337", ErrCode(0x1337).String()) +} diff --git a/http3/error_test.go b/http3/error_test.go index 71567128..2ee08739 100644 --- a/http3/error_test.go +++ b/http3/error_test.go @@ -2,40 +2,88 @@ package http3 import ( "errors" + "testing" "github.com/quic-go/quic-go" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" + "github.com/stretchr/testify/require" ) -var _ = Describe("HTTP/3 errors", func() { - It("converts", func() { - Expect(maybeReplaceError(nil)).To(BeNil()) - Expect(maybeReplaceError(errors.New("foobar"))).To(MatchError("foobar")) - Expect(maybeReplaceError(&quic.StreamError{ - ErrorCode: 1337, - Remote: true, - })).To(Equal(&Error{ - Remote: true, - ErrorCode: 1337, - })) - Expect(maybeReplaceError(&quic.ApplicationError{ - ErrorCode: 42, - Remote: true, - ErrorMessage: "foobar", - })).To(Equal(&Error{ - Remote: true, - ErrorCode: 42, - ErrorMessage: "foobar", - })) - }) +func TestErrorConversion(t *testing.T) { + regularErr := errors.New("foobar") - It("has a string representation", func() { - Expect((&Error{ErrorCode: 0x10c, Remote: true}).Error()).To(Equal("H3_REQUEST_CANCELLED")) - Expect((&Error{ErrorCode: 0x10c, Remote: true, ErrorMessage: "foobar"}).Error()).To(Equal("H3_REQUEST_CANCELLED: foobar")) - Expect((&Error{ErrorCode: 0x10c, Remote: false}).Error()).To(Equal("H3_REQUEST_CANCELLED (local)")) - Expect((&Error{ErrorCode: 0x10c, Remote: false, ErrorMessage: "foobar"}).Error()).To(Equal("H3_REQUEST_CANCELLED (local): foobar")) - Expect((&Error{ErrorCode: 0x1337, Remote: true}).Error()).To(Equal("H3 error (0x1337)")) - }) -}) + tests := []struct { + name string + input error + expected error + }{ + {name: "nil error", input: nil, expected: nil}, + {name: "regular error", input: regularErr, expected: regularErr}, + { + name: "stream error", + input: &quic.StreamError{ErrorCode: 1337, Remote: true}, + expected: &Error{Remote: true, ErrorCode: 1337}, + }, + { + name: "application error", + input: &quic.ApplicationError{ErrorCode: 42, Remote: true, ErrorMessage: "foobar"}, + expected: &Error{Remote: true, ErrorCode: 42, ErrorMessage: "foobar"}, + }, + { + name: "transport error", + input: &quic.TransportError{ErrorCode: 42, Remote: true, ErrorMessage: "foobar"}, + expected: &quic.TransportError{ErrorCode: 42, Remote: true, ErrorMessage: "foobar"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := maybeReplaceError(tt.input) + if tt.expected == nil { + require.Nil(t, result) + } else { + require.ErrorIs(t, tt.expected, result) + } + }) + } +} + +func TestErrorString(t *testing.T) { + tests := []struct { + name string + err *Error + expected string + }{ + { + name: "remote error", + err: &Error{ErrorCode: 0x10c, Remote: true}, + expected: "H3_REQUEST_CANCELLED", + }, + { + name: "remote error with message", + err: &Error{ErrorCode: 0x10c, Remote: true, ErrorMessage: "foobar"}, + expected: "H3_REQUEST_CANCELLED: foobar", + }, + { + name: "local error", + err: &Error{ErrorCode: 0x10c, Remote: false}, + expected: "H3_REQUEST_CANCELLED (local)", + }, + { + name: "local error with message", + err: &Error{ErrorCode: 0x10c, Remote: false, ErrorMessage: "foobar"}, + expected: "H3_REQUEST_CANCELLED (local): foobar", + }, + { + name: "unknown error code", + err: &Error{ErrorCode: 0x1337, Remote: true}, + expected: "H3 error (0x1337)", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require.Equal(t, tt.expected, tt.err.Error()) + }) + } +}