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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
package protoregistry_test
import (
"errors"
"fmt"
"testing"
"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/protoregistry"
"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
)
func TestProtoRegistryTargetRepo(t *testing.T) {
testRepos := []*gitalypb.Repository{
&gitalypb.Repository{
GitAlternateObjectDirectories: []string{"a", "b", "c"},
GitObjectDirectory: "d",
GlProjectPath: "e",
GlRepository: "f",
RelativePath: "g",
StorageName: "h",
},
&gitalypb.Repository{
GitAlternateObjectDirectories: []string{"1", "2", "3"},
GitObjectDirectory: "4",
GlProjectPath: "5",
GlRepository: "6",
RelativePath: "7",
StorageName: "8",
},
}
testcases := []struct {
desc string
svc string
method string
pbMsg proto.Message
expectRepo *gitalypb.Repository
expectAdditionalRepo *gitalypb.Repository
expectErr error
}{
{
desc: "valid request type single depth",
svc: "RepositoryService",
method: "RepackIncremental",
pbMsg: &gitalypb.RepackIncrementalRequest{
Repository: testRepos[0],
},
expectRepo: testRepos[0],
},
{
desc: "incorrect request type",
svc: "RepositoryService",
method: "RepackIncremental",
pbMsg: &gitalypb.RepackIncrementalResponse{},
expectErr: errors.New("proto message gitaly.RepackIncrementalResponse does not match expected RPC request message gitaly.RepackIncrementalRequest"),
},
{
desc: "target nested in oneOf",
svc: "OperationService",
method: "UserCommitFiles",
pbMsg: &gitalypb.UserCommitFilesRequest{
UserCommitFilesRequestPayload: &gitalypb.UserCommitFilesRequest_Header{
Header: &gitalypb.UserCommitFilesRequestHeader{
Repository: testRepos[1],
},
},
},
expectRepo: testRepos[1],
},
{
desc: "target nested, includes additional repository",
svc: "ObjectPoolService",
method: "FetchIntoObjectPool",
pbMsg: &gitalypb.FetchIntoObjectPoolRequest{
Origin: testRepos[0],
ObjectPool: &gitalypb.ObjectPool{Repository: testRepos[1]},
Repack: false,
},
expectRepo: testRepos[1],
expectAdditionalRepo: testRepos[0],
},
{
desc: "target repo is nil",
svc: "RepositoryService",
method: "RepackIncremental",
pbMsg: &gitalypb.RepackIncrementalRequest{Repository: nil},
expectErr: protoregistry.ErrTargetRepoMissing,
},
}
for _, tc := range testcases {
desc := fmt.Sprintf("%s:%s %s", tc.svc, tc.method, tc.desc)
t.Run(desc, func(t *testing.T) {
info, err := protoregistry.GitalyProtoPreregistered.LookupMethod(fmt.Sprintf("/gitaly.%s/%s", tc.svc, tc.method))
require.NoError(t, err)
actualTarget, actualErr := info.TargetRepo(tc.pbMsg)
require.Equal(t, tc.expectErr, actualErr)
// not only do we want the value to be the same, but we actually want the
// exact same instance to be returned
if tc.expectRepo != actualTarget {
t.Fatal("pointers do not match")
}
if tc.expectAdditionalRepo != nil {
additionalRepo, ok, err := info.AdditionalRepo(tc.pbMsg)
require.True(t, ok)
require.NoError(t, err)
require.Equal(t, tc.expectAdditionalRepo, additionalRepo)
}
})
}
}
func TestProtoRegistryStorage(t *testing.T) {
testcases := []struct {
desc string
svc string
method string
pbMsg proto.Message
expectStorage string
expectErr error
}{
{
desc: "valid request type single depth",
svc: "NamespaceService",
method: "AddNamespace",
pbMsg: &gitalypb.AddNamespaceRequest{
StorageName: "some_storage",
},
expectStorage: "some_storage",
},
{
desc: "incorrect request type",
svc: "RepositoryService",
method: "RepackIncremental",
pbMsg: &gitalypb.RepackIncrementalResponse{},
expectErr: errors.New("proto message gitaly.RepackIncrementalResponse does not match expected RPC request message gitaly.RepackIncrementalRequest"),
},
}
for _, tc := range testcases {
desc := fmt.Sprintf("%s:%s %s", tc.svc, tc.method, tc.desc)
t.Run(desc, func(t *testing.T) {
info, err := protoregistry.GitalyProtoPreregistered.LookupMethod(fmt.Sprintf("/gitaly.%s/%s", tc.svc, tc.method))
require.NoError(t, err)
actualStorage, actualErr := info.Storage(tc.pbMsg)
require.Equal(t, tc.expectErr, actualErr)
// not only do we want the value to be the same, but we actually want the
// exact same instance to be returned
if tc.expectStorage != actualStorage {
t.Fatal("pointers do not match")
}
})
}
}
func TestMethodInfo_SetStorage(t *testing.T) {
testCases := []struct {
desc string
service string
method string
pbMsg proto.Message
storage string
expectErr error
}{
{
desc: "valid request type",
service: "NamespaceService",
method: "AddNamespace",
pbMsg: &gitalypb.AddNamespaceRequest{
StorageName: "old_storage",
},
storage: "new_storage",
},
{
desc: "incorrect request type",
service: "RepositoryService",
method: "RepackIncremental",
pbMsg: &gitalypb.RepackIncrementalResponse{},
expectErr: errors.New("proto message gitaly.RepackIncrementalResponse does not match expected RPC request message gitaly.RepackIncrementalRequest"),
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
info, err := protoregistry.GitalyProtoPreregistered.LookupMethod("/gitaly." + tc.service + "/" + tc.method)
require.NoError(t, err)
err = info.SetStorage(tc.pbMsg, tc.storage)
if tc.expectErr == nil {
require.NoError(t, err)
changed, err := info.Storage(tc.pbMsg)
require.NoError(t, err)
require.Equal(t, tc.storage, changed)
} else {
require.Equal(t, tc.expectErr, err)
}
})
}
}
|