diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-08-02 08:35:07 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-08-03 07:58:02 +0300 |
commit | a4f0038ddfcdcecda9205079d1f5518275b54027 (patch) | |
tree | 777ab88dbb1bdd27262632aa40615d6696e636bf | |
parent | feaa4ee71db808d637f29c1df372d877f4cae2fd (diff) |
catfile: Refactor tests to be hash-agnostic
Refactor the catfile tests to be hash-agnostic so that they behave well
both with SHA1 and SHA256 repositories.
-rw-r--r-- | internal/git/catfile/object_info_reader_test.go | 23 | ||||
-rw-r--r-- | internal/git/catfile/parser_test.go | 36 | ||||
-rw-r--r-- | internal/git/catfile/request_queue_test.go | 65 |
3 files changed, 71 insertions, 53 deletions
diff --git a/internal/git/catfile/object_info_reader_test.go b/internal/git/catfile/object_info_reader_test.go index 6ae6006fe..92d1ff52c 100644 --- a/internal/git/catfile/object_info_reader_test.go +++ b/internal/git/catfile/object_info_reader_test.go @@ -33,9 +33,9 @@ func TestParseObjectInfo_success(t *testing.T) { }{ { desc: "existing object", - input: "7c9373883988204e5a9f72c4a5119cbcefc83627 commit 222\n", + input: fmt.Sprintf("%s commit 222\n", gittest.DefaultObjectHash.EmptyTreeOID), expectedObjectInfo: &ObjectInfo{ - Oid: "7c9373883988204e5a9f72c4a5119cbcefc83627", + Oid: gittest.DefaultObjectHash.EmptyTreeOID, Type: "commit", Size: 222, }, @@ -59,6 +59,8 @@ func TestParseObjectInfo_success(t *testing.T) { func TestParseObjectInfo_errors(t *testing.T) { t.Parallel() + oid := gittest.DefaultObjectHash.EmptyTreeOID + for _, tc := range []struct { desc string input string @@ -66,22 +68,27 @@ func TestParseObjectInfo_errors(t *testing.T) { }{ { desc: "missing newline", - input: "7c9373883988204e5a9f72c4a5119cbcefc83627 commit 222", + input: fmt.Sprintf("%s commit 222", oid), expectedErr: fmt.Errorf("read info line: %w", io.EOF), }, { desc: "too few words", - input: "7c9373883988204e5a9f72c4a5119cbcefc83627 commit\n", - expectedErr: fmt.Errorf("invalid info line: %q", "7c9373883988204e5a9f72c4a5119cbcefc83627 commit"), + input: fmt.Sprintf("%s commit\n", oid), + expectedErr: fmt.Errorf("invalid info line: %q", oid+" commit"), }, { desc: "too many words", - input: "7c9373883988204e5a9f72c4a5119cbcefc83627 commit 222 bla\n", - expectedErr: fmt.Errorf("invalid info line: %q", "7c9373883988204e5a9f72c4a5119cbcefc83627 commit 222 bla"), + input: fmt.Sprintf("%s commit 222 bla\n", oid), + expectedErr: fmt.Errorf("invalid info line: %q", oid+" commit 222 bla"), + }, + { + desc: "invalid object hash", + input: "7c9373883988204e5a9f72c4 commit 222 bla\n", + expectedErr: fmt.Errorf("invalid info line: %q", "7c9373883988204e5a9f72c4 commit 222 bla"), }, { desc: "parse object size", - input: "7c9373883988204e5a9f72c4a5119cbcefc83627 commit bla\n", + input: fmt.Sprintf("%s commit bla\n", oid), expectedErr: fmt.Errorf("parse object size: %w", &strconv.NumError{ Func: "ParseInt", Num: "bla", diff --git a/internal/git/catfile/parser_test.go b/internal/git/catfile/parser_test.go index e1fb34007..d2756b0ea 100644 --- a/internal/git/catfile/parser_test.go +++ b/internal/git/catfile/parser_test.go @@ -4,12 +4,14 @@ package catfile import ( "bytes" + "fmt" "strings" "testing" "time" "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/v15/internal/git" + "gitlab.com/gitlab-org/gitaly/v15/internal/git/gittest" "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb" "google.golang.org/protobuf/types/known/timestamppb" @@ -19,7 +21,7 @@ func TestParser_ParseCommit(t *testing.T) { t.Parallel() info := &ObjectInfo{ - Oid: "a984dfa4dee018c6d5f5f57ffec0d0e22763df16", + Oid: gittest.DefaultObjectHash.EmptyTreeOID, Type: "commit", } @@ -181,6 +183,8 @@ func TestParseCommitAuthor(t *testing.T) { func TestParser_ParseTag(t *testing.T) { t.Parallel() + oid := gittest.DefaultObjectHash.EmptyTreeOID.String() + for _, tc := range []struct { desc string oid git.ObjectID @@ -190,7 +194,7 @@ func TestParser_ParseTag(t *testing.T) { }{ { desc: "tag without a message", - contents: "object c92faf3e0a557270141be67f206d7cdb99bfc3a2\ntype commit\ntag v2.6.16.28\ntagger Adrian Bunk <bunk@stusta.de> 1156539089 +0200", + contents: fmt.Sprintf("object %s\ntype commit\ntag v2.6.16.28\ntagger Adrian Bunk <bunk@stusta.de> 1156539089 +0200", oid), oid: "1234", expectedTag: &gitalypb.Tag{ Id: "1234", @@ -205,13 +209,13 @@ func TestParser_ParseTag(t *testing.T) { }, }, expectedTagged: taggedObject{ - objectID: "c92faf3e0a557270141be67f206d7cdb99bfc3a2", + objectID: oid, objectType: "commit", }, }, { desc: "tag with message", - contents: "object c92faf3e0a557270141be67f206d7cdb99bfc3a2\ntype commit\ntag v2.6.16.28\ntagger Adrian Bunk <bunk@stusta.de> 1156539089 +0200\n\nmessage", + contents: fmt.Sprintf("object %s\ntype commit\ntag v2.6.16.28\ntagger Adrian Bunk <bunk@stusta.de> 1156539089 +0200\n\nmessage", oid), oid: "1234", expectedTag: &gitalypb.Tag{ Id: "1234", @@ -228,14 +232,14 @@ func TestParser_ParseTag(t *testing.T) { }, }, expectedTagged: taggedObject{ - objectID: "c92faf3e0a557270141be67f206d7cdb99bfc3a2", + objectID: oid, objectType: "commit", }, }, { desc: "tag with empty message", oid: "1234", - contents: "object c92faf3e0a557270141be67f206d7cdb99bfc3a2\ntype commit\ntag v2.6.16.28\ntagger Adrian Bunk <bunk@stusta.de> 1156539089 +0200\n\n", + contents: fmt.Sprintf("object %s\ntype commit\ntag v2.6.16.28\ntagger Adrian Bunk <bunk@stusta.de> 1156539089 +0200\n\n", oid), expectedTag: &gitalypb.Tag{ Id: "1234", Name: []byte("v2.6.16.28"), @@ -250,14 +254,14 @@ func TestParser_ParseTag(t *testing.T) { }, }, expectedTagged: taggedObject{ - objectID: "c92faf3e0a557270141be67f206d7cdb99bfc3a2", + objectID: oid, objectType: "commit", }, }, { desc: "tag with message with empty line", oid: "1234", - contents: "object c92faf3e0a557270141be67f206d7cdb99bfc3a2\ntype commit\ntag v2.6.16.28\ntagger Adrian Bunk <bunk@stusta.de> 1156539089 +0200\n\nHello world\n\nThis is a message", + contents: fmt.Sprintf("object %s\ntype commit\ntag v2.6.16.28\ntagger Adrian Bunk <bunk@stusta.de> 1156539089 +0200\n\nHello world\n\nThis is a message", oid), expectedTag: &gitalypb.Tag{ Id: "1234", Name: []byte("v2.6.16.28"), @@ -273,13 +277,13 @@ func TestParser_ParseTag(t *testing.T) { }, }, expectedTagged: taggedObject{ - objectID: "c92faf3e0a557270141be67f206d7cdb99bfc3a2", + objectID: oid, objectType: "commit", }, }, { desc: "tag with message with empty line and right side new line", - contents: "object c92faf3e0a557270141be67f206d7cdb99bfc3a2\ntype commit\ntag v2.6.16.28\ntagger Adrian Bunk <bunk@stusta.de> 1156539089 +0200\n\nHello world\n\nThis is a message\n\n", + contents: fmt.Sprintf("object %s\ntype commit\ntag v2.6.16.28\ntagger Adrian Bunk <bunk@stusta.de> 1156539089 +0200\n\nHello world\n\nThis is a message\n\n", oid), oid: "1234", expectedTag: &gitalypb.Tag{ Id: "1234", @@ -296,13 +300,13 @@ func TestParser_ParseTag(t *testing.T) { }, }, expectedTagged: taggedObject{ - objectID: "c92faf3e0a557270141be67f206d7cdb99bfc3a2", + objectID: oid, objectType: "commit", }, }, { desc: "tag with missing date and body", - contents: "object 422081655f743e03b01ee29a2eaf26aab0ee7eda\ntype commit\ntag syslinux-3.11-pre6\ntagger hpa <hpa>\n", + contents: fmt.Sprintf("object %s\ntype commit\ntag syslinux-3.11-pre6\ntagger hpa <hpa>\n", oid), oid: "1234", expectedTag: &gitalypb.Tag{ Id: "1234", @@ -313,14 +317,14 @@ func TestParser_ParseTag(t *testing.T) { }, }, expectedTagged: taggedObject{ - objectID: "422081655f743e03b01ee29a2eaf26aab0ee7eda", + objectID: oid, objectType: "commit", }, }, { desc: "tag signed with SSH", oid: "1234", - contents: `object c92faf3e0a557270141be67f206d7cdb99bfc3a2 + contents: fmt.Sprintf(`object %s type commit tag v2.6.16.28 tagger Adrian Bunk <bunk@stusta.de> 1156539089 +0200 @@ -331,7 +335,7 @@ U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgtc+Qk8jhMwVZk/jFEFCM16LNQb 30q5kK30bbetfjyTMAAAADZ2l0AAAAAAAAAAZzaGE1MTIAAABTAAAAC3NzaC1lZDI1NTE5 AAAAQLSyv010gOFwIs9QTtDvlfIEWiAw2iQL/T9usGcxHXn/W5l0cOFCd7O+WaMDg0t0nW fF3T79iV8paT4/OfX8Ygg= ------END SSH SIGNATURE-----`, +-----END SSH SIGNATURE-----`, oid), expectedTag: &gitalypb.Tag{ Id: "1234", Name: []byte("v2.6.16.28"), @@ -354,7 +358,7 @@ fF3T79iV8paT4/OfX8Ygg= SignatureType: gitalypb.SignatureType_SSH, }, expectedTagged: taggedObject{ - objectID: "c92faf3e0a557270141be67f206d7cdb99bfc3a2", + objectID: oid, objectType: "commit", }, }, diff --git a/internal/git/catfile/request_queue_test.go b/internal/git/catfile/request_queue_test.go index 7818a67af..c5c51ce63 100644 --- a/internal/git/catfile/request_queue_test.go +++ b/internal/git/catfile/request_queue_test.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "os" + "strings" "testing" "github.com/stretchr/testify/require" @@ -21,6 +22,8 @@ func TestRequestQueue_ReadObject(t *testing.T) { ctx := testhelper.Context(t) + oid := git.ObjectID(strings.Repeat("1", gittest.DefaultObjectHash.EncodedLen())) + t.Run("ReadInfo on ReadObject queue", func(t *testing.T) { _, queue := newInterceptedQueue(ctx, t, "#!/bin/sh\nread\n") @@ -49,9 +52,9 @@ func TestRequestQueue_ReadObject(t *testing.T) { }) t.Run("read with unconsumed object", func(t *testing.T) { - _, queue := newInterceptedQueue(ctx, t, `#!/bin/sh - echo "1111111111111111111111111111111111111111 commit 464" - `) + _, queue := newInterceptedQueue(ctx, t, fmt.Sprintf(`#!/bin/sh + echo "%s commit 464" + `, oid)) // We queue two revisions... require.NoError(t, queue.RequestRevision("foo")) @@ -97,9 +100,9 @@ func TestRequestQueue_ReadObject(t *testing.T) { }) t.Run("read with missing object", func(t *testing.T) { - _, queue := newInterceptedQueue(ctx, t, `#!/bin/sh - echo "1111111111111111111111111111111111111111 missing" - `) + _, queue := newInterceptedQueue(ctx, t, fmt.Sprintf(`#!/bin/sh + echo "%s missing" + `, oid)) require.NoError(t, queue.RequestRevision("foo")) @@ -112,10 +115,10 @@ func TestRequestQueue_ReadObject(t *testing.T) { }) t.Run("read single object", func(t *testing.T) { - _, queue := newInterceptedQueue(ctx, t, `#!/bin/sh - echo "1111111111111111111111111111111111111111 blob 10" + _, queue := newInterceptedQueue(ctx, t, fmt.Sprintf(`#!/bin/sh + echo "%s blob 10" echo "1234567890" - `) + `, oid)) require.NoError(t, queue.RequestRevision("foo")) require.True(t, queue.isDirty()) @@ -123,7 +126,7 @@ func TestRequestQueue_ReadObject(t *testing.T) { object, err := queue.ReadObject() require.NoError(t, err) require.Equal(t, ObjectInfo{ - Oid: "1111111111111111111111111111111111111111", + Oid: oid, Type: "blob", Size: 10, }, object.ObjectInfo) @@ -136,12 +139,14 @@ func TestRequestQueue_ReadObject(t *testing.T) { }) t.Run("read multiple objects", func(t *testing.T) { - _, queue := newInterceptedQueue(ctx, t, `#!/bin/sh - echo "1111111111111111111111111111111111111111 blob 10" + secondOID := git.ObjectID(strings.Repeat("2", gittest.DefaultObjectHash.EncodedLen())) + + _, queue := newInterceptedQueue(ctx, t, fmt.Sprintf(`#!/bin/sh + echo "%s blob 10" echo "1234567890" - echo "2222222222222222222222222222222222222222 commit 10" + echo "%s commit 10" echo "0987654321" - `) + `, oid, secondOID)) require.NoError(t, queue.RequestRevision("foo")) require.NoError(t, queue.RequestRevision("foo")) @@ -153,7 +158,7 @@ func TestRequestQueue_ReadObject(t *testing.T) { }{ { info: ObjectInfo{ - Oid: "1111111111111111111111111111111111111111", + Oid: oid, Type: "blob", Size: 10, }, @@ -161,7 +166,7 @@ func TestRequestQueue_ReadObject(t *testing.T) { }, { info: ObjectInfo{ - Oid: "2222222222222222222222222222222222222222", + Oid: secondOID, Type: "commit", Size: 10, }, @@ -183,10 +188,10 @@ func TestRequestQueue_ReadObject(t *testing.T) { }) t.Run("truncated object", func(t *testing.T) { - _, queue := newInterceptedQueue(ctx, t, `#!/bin/sh - echo "1111111111111111111111111111111111111111 blob 10" + _, queue := newInterceptedQueue(ctx, t, fmt.Sprintf(`#!/bin/sh + echo "%s blob 10" printf "123" - `) + `, oid)) require.NoError(t, queue.RequestRevision("foo")) require.True(t, queue.isDirty()) @@ -194,7 +199,7 @@ func TestRequestQueue_ReadObject(t *testing.T) { object, err := queue.ReadObject() require.NoError(t, err) require.Equal(t, ObjectInfo{ - Oid: "1111111111111111111111111111111111111111", + Oid: oid, Type: "blob", Size: 10, }, object.ObjectInfo) @@ -221,6 +226,8 @@ func TestRequestQueue_RequestRevision(t *testing.T) { ctx := testhelper.Context(t) + oid := git.ObjectID(strings.Repeat("1", gittest.DefaultObjectHash.EncodedLen())) + requireRevision := func(t *testing.T, queue *requestQueue, rev git.Revision) { object, err := queue.ReadObject() require.NoError(t, err) @@ -246,11 +253,11 @@ func TestRequestQueue_RequestRevision(t *testing.T) { }) t.Run("single request", func(t *testing.T) { - _, queue := newInterceptedQueue(ctx, t, `#!/bin/sh + _, queue := newInterceptedQueue(ctx, t, fmt.Sprintf(`#!/bin/sh read revision - echo "1111111111111111111111111111111111111111 blob ${#revision}" + echo "%s blob ${#revision}" echo "${revision}" - `) + `, oid)) require.NoError(t, queue.RequestRevision("foo")) require.NoError(t, queue.Flush()) @@ -259,13 +266,13 @@ func TestRequestQueue_RequestRevision(t *testing.T) { }) t.Run("multiple request", func(t *testing.T) { - _, queue := newInterceptedQueue(ctx, t, `#!/bin/sh + _, queue := newInterceptedQueue(ctx, t, fmt.Sprintf(`#!/bin/sh while read revision do - echo "1111111111111111111111111111111111111111 blob ${#revision}" + echo "%s blob ${#revision}" echo "${revision}" done - `) + `, oid)) require.NoError(t, queue.RequestRevision("foo")) require.NoError(t, queue.RequestRevision("bar")) @@ -280,7 +287,7 @@ func TestRequestQueue_RequestRevision(t *testing.T) { }) t.Run("multiple request with intermediate flushing", func(t *testing.T) { - _, queue := newInterceptedQueue(ctx, t, `#!/bin/sh + _, queue := newInterceptedQueue(ctx, t, fmt.Sprintf(`#!/bin/sh while read revision do read flush @@ -290,10 +297,10 @@ func TestRequestQueue_RequestRevision(t *testing.T) { exit 1 fi - echo "1111111111111111111111111111111111111111 blob ${#revision}" + echo "%s blob ${#revision}" echo "${revision}" done - `) + `, oid)) for _, revision := range []git.Revision{ "foo", |