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
132
133
134
135
136
137
138
|
package catfile
import (
"fmt"
"io"
"io/ioutil"
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v14/internal/git"
"gitlab.com/gitlab-org/gitaly/v14/internal/git/gittest"
"gitlab.com/gitlab-org/gitaly/v14/internal/helper/text"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper/testcfg"
)
func TestObjectReader_reader(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
cfg, repoProto, repoPath := testcfg.BuildWithRepo(t)
commitID, err := git.NewObjectIDFromHex(text.ChompBytes(gittest.Exec(t, cfg, "-C", repoPath, "rev-parse", "refs/heads/master")))
require.NoError(t, err)
commitContents := gittest.Exec(t, cfg, "-C", repoPath, "cat-file", "-p", "refs/heads/master")
t.Run("read existing object by ref", func(t *testing.T) {
reader, err := newObjectReader(ctx, newRepoExecutor(t, cfg, repoProto), nil)
require.NoError(t, err)
object, err := reader.reader(ctx, "refs/heads/master", "commit")
require.NoError(t, err)
data, err := ioutil.ReadAll(object)
require.NoError(t, err)
require.Equal(t, commitContents, data)
})
t.Run("read existing object by object ID", func(t *testing.T) {
reader, err := newObjectReader(ctx, newRepoExecutor(t, cfg, repoProto), nil)
require.NoError(t, err)
object, err := reader.reader(ctx, commitID.Revision(), "commit")
require.NoError(t, err)
data, err := ioutil.ReadAll(object)
require.NoError(t, err)
require.Contains(t, string(data), "Merge branch 'cherry-pick-ce369011' into 'master'\n")
})
t.Run("read commit with wrong type", func(t *testing.T) {
reader, err := newObjectReader(ctx, newRepoExecutor(t, cfg, repoProto), nil)
require.NoError(t, err)
_, err = reader.reader(ctx, commitID.Revision(), "tag")
require.EqualError(t, err, fmt.Sprintf("expected %s to be a tag, got commit", commitID))
// Verify that we're still able to read a commit after the previous read has failed.
object, err := reader.reader(ctx, commitID.Revision(), "commit")
require.NoError(t, err)
data, err := ioutil.ReadAll(object)
require.NoError(t, err)
require.Equal(t, commitContents, data)
})
t.Run("read missing ref", func(t *testing.T) {
reader, err := newObjectReader(ctx, newRepoExecutor(t, cfg, repoProto), nil)
require.NoError(t, err)
_, err = reader.reader(ctx, "refs/heads/does-not-exist", "commit")
require.EqualError(t, err, "object not found")
// Verify that we're still able to read a commit after the previous read has failed.
object, err := reader.reader(ctx, commitID.Revision(), "commit")
require.NoError(t, err)
data, err := ioutil.ReadAll(object)
require.NoError(t, err)
require.Equal(t, commitContents, data)
})
t.Run("read fails when not consuming previous object", func(t *testing.T) {
reader, err := newObjectReader(ctx, newRepoExecutor(t, cfg, repoProto), nil)
require.NoError(t, err)
_, err = reader.reader(ctx, commitID.Revision(), "commit")
require.NoError(t, err)
// We haven't yet consumed the previous object, so this must now fail.
_, err = reader.reader(ctx, commitID.Revision(), "commit")
require.EqualError(t, err, fmt.Sprintf("cannot create new Object: batch contains %d unread bytes", len(commitContents)+1))
})
t.Run("read fails when partially consuming previous object", func(t *testing.T) {
reader, err := newObjectReader(ctx, newRepoExecutor(t, cfg, repoProto), nil)
require.NoError(t, err)
object, err := reader.reader(ctx, commitID.Revision(), "commit")
require.NoError(t, err)
_, err = io.CopyN(io.Discard, object, 100)
require.NoError(t, err)
// We haven't yet consumed the previous object, so this must now fail.
_, err = reader.reader(ctx, commitID.Revision(), "commit")
require.EqualError(t, err, fmt.Sprintf("cannot create new Object: batch contains %d unread bytes", len(commitContents)-100+1))
})
t.Run("read increments Prometheus counter", func(t *testing.T) {
counter := prometheus.NewCounterVec(prometheus.CounterOpts{}, []string{"type"})
reader, err := newObjectReader(ctx, newRepoExecutor(t, cfg, repoProto), counter)
require.NoError(t, err)
for objectType, revision := range map[string]git.Revision{
"commit": "refs/heads/master",
"tree": "refs/heads/master^{tree}",
"blob": "refs/heads/master:README",
"tag": "refs/tags/v1.1.1",
} {
require.Equal(t, float64(0), testutil.ToFloat64(counter.WithLabelValues(objectType)))
object, err := reader.reader(ctx, revision, objectType)
require.NoError(t, err)
require.Equal(t, float64(1), testutil.ToFloat64(counter.WithLabelValues(objectType)))
_, err = io.Copy(io.Discard, object)
require.NoError(t, err)
}
})
}
|