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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
//go:build !gitaly_test_sha256
package main
import (
"bytes"
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v15/internal/praefect/config"
"gitlab.com/gitlab-org/gitaly/v15/internal/praefect/datastore"
"gitlab.com/gitlab-org/gitaly/v15/internal/praefect/service/info"
"gitlab.com/gitlab-org/gitaly/v15/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/v15/internal/testhelper/testdb"
)
func TestVerifySubcommand(t *testing.T) {
t.Parallel()
// state contains the asserted state as virtual storage -> relative path -> storage -> verified/not verified.
type state map[string]map[string]map[string]bool
startingState := state{
"virtual-storage-1": {
"relative-path-1": {
"unverified": false,
"verified-1": true,
"verified-2": true,
},
"relative-path-2": {
"unverified": false,
"verified-1": true,
"verified-2": true,
},
},
"virtual-storage-2": {
"relative-path-1": {
"unverified": false,
"verified-1": true,
"verified-2": true,
},
},
}
for _, tc := range []struct {
desc string
args []string
replicasMarked int
error error
expectedState state
}{
{
desc: "no selector given",
error: errors.New("(repository id), (virtual storage) or (virtual storage, storage) required"),
expectedState: startingState,
},
{
desc: "virtual storage passed with repository id",
args: []string{"-repository-id=1", "-virtual-storage=virtual-storage"},
error: errors.New("virtual storage and storage can't be provided with a repository ID"),
expectedState: startingState,
},
{
desc: "storage passed with repository id",
args: []string{"-repository-id=1", "-storage=storage"},
error: errors.New("virtual storage and storage can't be provided with a repository ID"),
expectedState: startingState,
},
{
desc: "storage passed without virtual storage",
args: []string{"-storage=storage"},
error: errors.New("virtual storage must be passed with storage"),
expectedState: startingState,
},
{
desc: "no repository matched",
args: []string{"-repository-id=1000"},
expectedState: startingState,
},
{
desc: "scheduled by repository id",
args: []string{"-repository-id=1"},
replicasMarked: 2,
expectedState: state{
"virtual-storage-1": {
"relative-path-1": {
"unverified": false,
"verified-1": false,
"verified-2": false,
},
"relative-path-2": {
"unverified": false,
"verified-1": true,
"verified-2": true,
},
},
"virtual-storage-2": {
"relative-path-1": {
"unverified": false,
"verified-1": true,
"verified-2": true,
},
},
},
},
{
desc: "scheduled by virtual storage",
args: []string{"-virtual-storage=virtual-storage-1"},
replicasMarked: 4,
expectedState: state{
"virtual-storage-1": {
"relative-path-1": {
"unverified": false,
"verified-1": false,
"verified-2": false,
},
"relative-path-2": {
"unverified": false,
"verified-1": false,
"verified-2": false,
},
},
"virtual-storage-2": {
"relative-path-1": {
"unverified": false,
"verified-1": true,
"verified-2": true,
},
},
},
},
{
desc: "scheduled by storage",
args: []string{"-virtual-storage=virtual-storage-1", "-storage=verified-1"},
replicasMarked: 2,
expectedState: state{
"virtual-storage-1": {
"relative-path-1": {
"unverified": false,
"verified-1": false,
"verified-2": true,
},
"relative-path-2": {
"unverified": false,
"verified-1": false,
"verified-2": true,
},
},
"virtual-storage-2": {
"relative-path-1": {
"unverified": false,
"verified-1": true,
"verified-2": true,
},
},
},
},
} {
t.Run(tc.desc, func(t *testing.T) {
db := testdb.New(t)
rs := datastore.NewPostgresRepositoryStore(db, nil)
ln, clean := listenAndServe(t, []svcRegistrar{
registerPraefectInfoServer(info.NewServer(config.Config{}, rs, nil, nil, nil)),
})
defer clean()
ctx := testhelper.Context(t)
require.NoError(t,
rs.CreateRepository(ctx, 1, "virtual-storage-1", "relative-path-1", "replica-path-1", "unverified", []string{"verified-1", "verified-2"}, nil, false, false),
)
require.NoError(t,
rs.CreateRepository(ctx, 2, "virtual-storage-1", "relative-path-2", "replica-path-2", "unverified", []string{"verified-1", "verified-2"}, nil, false, false),
)
require.NoError(t,
rs.CreateRepository(ctx, 3, "virtual-storage-2", "relative-path-1", "replica-path-3", "unverified", []string{"verified-1", "verified-2"}, nil, false, false),
)
_, err := db.ExecContext(ctx, `
UPDATE storage_repositories
SET verified_at = now()
FROM repositories
WHERE repositories.repository_id = storage_repositories.repository_id
AND storage != 'unverified'
`)
require.NoError(t, err)
stdout := &bytes.Buffer{}
cmd := newVerifySubcommand(stdout)
fs := cmd.FlagSet()
require.NoError(t, fs.Parse(tc.args))
err = cmd.Exec(fs, config.Config{SocketPath: ln.Addr().String()})
testhelper.RequireGrpcError(t, tc.error, err)
if tc.error != nil {
return
}
require.Equal(t, fmt.Sprintf("%d replicas marked unverified\n", tc.replicasMarked), stdout.String())
actualState := state{}
rows, err := db.QueryContext(ctx, `
SELECT
repositories.virtual_storage,
repositories.relative_path,
storage,
verified_at IS NOT NULL as verified
FROM repositories
JOIN storage_repositories USING (repository_id)
`)
require.NoError(t, err)
defer rows.Close()
for rows.Next() {
var virtualStorage, relativePath, storage string
var verified bool
require.NoError(t, rows.Scan(&virtualStorage, &relativePath, &storage, &verified))
if actualState[virtualStorage] == nil {
actualState[virtualStorage] = map[string]map[string]bool{}
}
if actualState[virtualStorage][relativePath] == nil {
actualState[virtualStorage][relativePath] = map[string]bool{}
}
actualState[virtualStorage][relativePath][storage] = verified
}
require.NoError(t, rows.Err())
require.Equal(t, tc.expectedState, actualState)
})
}
}
|