Welcome to mirror list, hosted at ThFree Co, Russian Federation.

blob_filter_test.go « blob « service « gitaly « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b716cd7c75e086ee4949d1bd1ffa0b964816bdea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package blob

import (
	"bytes"
	"errors"
	"io/ioutil"
	"strconv"
	"strings"
	"testing"

	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/internal/git"
	"gitlab.com/gitlab-org/gitaly/internal/git/localrepo"
	"gitlab.com/gitlab-org/gitaly/internal/testhelper"
	"golang.org/x/text/transform"
)

func TestBlobFilter(t *testing.T) {
	cfg, repo, _, _ := setup(t)
	localRepo := localrepo.NewTestRepo(t, cfg, repo)

	ctx, cancel := testhelper.Context()
	defer cancel()

	var batchCheckOutput bytes.Buffer
	err := localRepo.ExecAndWait(ctx, git.SubCmd{
		Name: "cat-file",
		Flags: []git.Option{
			git.Flag{"--batch-all-objects"},
			git.Flag{"--batch-check=%(objecttype) %(objectsize) %(objectname)"},
		},
	}, git.WithStdout(&batchCheckOutput))
	require.NoError(t, err)

	// We're manually reimplementing filtering of objects here. While this may seem hacky, it
	// asserts that we can parse largish output directly produced by the command. Also, given
	// that the system under test is implemented completely different, it cross-checks both
	// implementations against each other.
	var expectedOIDs []string
	for _, line := range strings.Split(batchCheckOutput.String(), "\n") {
		if len(line) == 0 {
			continue
		}
		objectInfo := strings.SplitN(line, " ", 3)
		require.Len(t, objectInfo, 3)
		objectSize, err := strconv.Atoi(objectInfo[1])
		require.NoError(t, err)

		if objectInfo[0] == "blob" && objectSize <= lfsPointerMaxSize {
			expectedOIDs = append(expectedOIDs, objectInfo[2])
		}
	}
	require.Greater(t, len(expectedOIDs), 100)

	for _, tc := range []struct {
		desc           string
		input          string
		maxSize        uint64
		expectedOutput string
		expectedErr    error
	}{
		{
			desc:           "empty",
			input:          "",
			expectedOutput: "",
		},
		{
			desc:        "newline only",
			input:       "\n",
			expectedErr: errors.New("invalid line \"\""),
		},
		{
			desc:        "invalid blob",
			input:       "x",
			expectedErr: errors.New("invalid trailing line"),
		},
		{
			desc:           "single blob",
			input:          "blob 140 1234\n",
			expectedOutput: "1234\n",
		},
		{
			desc:           "multiple blobs",
			input:          "blob 140 1234\nblob 150 4321\n",
			expectedOutput: "1234\n4321\n",
		},
		{
			desc:           "mixed blobs and other objects",
			input:          "blob 140 1234\ntree 150 4321\ncommit 50123 123123\n",
			maxSize:        160,
			expectedOutput: "1234\n",
		},
		{
			desc:           "big blob gets filtered",
			input:          "blob 140 1234\nblob 201 4321\n",
			maxSize:        200,
			expectedOutput: "1234\n",
		},
		{
			desc:        "missing trailing newline",
			input:       "blob 140 1234",
			expectedErr: errors.New("invalid trailing line"),
		},
		{
			desc:        "invalid object size",
			input:       "blob 140 1234\nblob x 4321\n",
			maxSize:     1,
			expectedErr: errors.New("invalid blob size \"x\""),
		},
		{
			desc:        "missing field",
			input:       "blob 1234\n",
			expectedErr: errors.New("invalid line \"blob 1234\""),
		},
		{
			desc:           "real-repo output",
			input:          batchCheckOutput.String(),
			maxSize:        lfsPointerMaxSize,
			expectedOutput: strings.Join(expectedOIDs, "\n") + "\n",
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			reader := transform.NewReader(strings.NewReader(tc.input), blobFilter{
				maxSize: tc.maxSize,
			})
			output, err := ioutil.ReadAll(reader)
			require.Equal(t, tc.expectedErr, err)
			require.Equal(t, tc.expectedOutput, string(output))
		})
	}
}