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
|
// +build postgres
package main
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/internal/praefect/config"
"gitlab.com/gitlab-org/gitaly/internal/praefect/datastore"
"gitlab.com/gitlab-org/gitaly/internal/praefect/service/info"
"gitlab.com/gitlab-org/gitaly/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
)
func TestAcceptDatalossSubcommand(t *testing.T) {
const (
vs = "test-virtual-storage-1"
repo = "test-repository-1"
st1 = "test-physical-storage-1"
st2 = "test-physical-storage-2"
st3 = "test-physical-storage-3"
)
conf := config.Config{
VirtualStorages: []*config.VirtualStorage{
{
Name: vs,
Nodes: []*config.Node{{Storage: st1}, {Storage: st2}, {Storage: st3}},
},
},
}
ctx, cancel := testhelper.Context()
defer cancel()
rs := datastore.NewPostgresRepositoryStore(getDB(t), conf.StorageNames())
startingGenerations := map[string]int{st1: 1, st2: 0, st3: datastore.GenerationUnknown}
for storage, generation := range startingGenerations {
if generation == datastore.GenerationUnknown {
continue
}
require.NoError(t, rs.SetGeneration(ctx, vs, repo, storage, generation))
}
q := &datastore.MockReplicationEventQueue{
EnqueueFunc: func(ctx context.Context, event datastore.ReplicationEvent) (datastore.ReplicationEvent, error) {
if event.Job.TargetNodeStorage == st2 {
return event, fmt.Errorf("replication event scheduled for authoritative storage %q", st2)
}
generation, err := rs.GetGeneration(ctx, event.Job.VirtualStorage, event.Job.RelativePath, event.Job.SourceNodeStorage)
if err != nil {
return event, err
}
return event, rs.SetGeneration(ctx, event.Job.VirtualStorage, event.Job.RelativePath, event.Job.TargetNodeStorage, generation)
},
}
ln, clean := listenAndServe(t, []svcRegistrar{registerPraefectInfoServer(info.NewServer(nil, conf, q, rs, nil, nil, nil))})
defer clean()
conf.SocketPath = ln.Addr().String()
type errorMatcher func(t *testing.T, err error)
matchEqual := func(expected error) errorMatcher {
return func(t *testing.T, actual error) {
t.Helper()
require.Equal(t, expected, actual)
}
}
matchNoError := func() errorMatcher {
return func(t *testing.T, actual error) {
t.Helper()
require.NoError(t, actual)
}
}
matchErrorMsg := func(expected string) errorMatcher {
return func(t *testing.T, actual error) {
t.Helper()
require.EqualError(t, actual, expected)
}
}
for _, tc := range []struct {
desc string
args []string
virtualStorages []*config.VirtualStorage
datalossCheck func(context.Context, *gitalypb.DatalossCheckRequest) (*gitalypb.DatalossCheckResponse, error)
output string
matchError errorMatcher
expectedGenerations map[string]int
}{
{
desc: "missing virtual storage",
args: []string{},
matchError: matchEqual(requiredParameterError(paramVirtualStorage)),
expectedGenerations: startingGenerations,
},
{
desc: "missing repository",
args: []string{"-virtual-storage=test-virtual-storage-1"},
matchError: matchEqual(requiredParameterError(paramRelativePath)),
expectedGenerations: startingGenerations,
},
{
desc: "missing authoritative storage",
args: []string{"-virtual-storage=test-virtual-storage-1", "-repository=test-repository-1"},
matchError: matchEqual(requiredParameterError(paramAuthoritativeStorage)),
expectedGenerations: startingGenerations,
},
{
desc: "positional arguments",
args: []string{"-virtual-storage=test-virtual-storage-1", "-repository=test-repository-1", "-authoritative-storage=test-physical-storage-2", "positional-arg"},
matchError: matchEqual(unexpectedPositionalArgsError{Command: "accept-dataloss"}),
expectedGenerations: startingGenerations,
},
{
desc: "non-existent virtual storage",
args: []string{"-virtual-storage=non-existent", "-repository=test-repository-1", "-authoritative-storage=test-physical-storage-2"},
matchError: matchErrorMsg(`set authoritative storage: rpc error: code = InvalidArgument desc = unknown virtual storage: "non-existent"`),
expectedGenerations: startingGenerations,
},
{
desc: "non-existent authoritative storage",
args: []string{"-virtual-storage=test-virtual-storage-1", "-repository=test-repository-1", "-authoritative-storage=non-existent"},
matchError: matchErrorMsg(`set authoritative storage: rpc error: code = InvalidArgument desc = unknown authoritative storage: "non-existent"`),
expectedGenerations: startingGenerations,
},
{
desc: "non-existent repository",
args: []string{"-virtual-storage=test-virtual-storage-1", "-repository=non-existent", "-authoritative-storage=test-physical-storage-2"},
matchError: matchErrorMsg(`set authoritative storage: rpc error: code = InvalidArgument desc = repository "non-existent" does not exist on virtual storage "test-virtual-storage-1"`),
expectedGenerations: startingGenerations,
},
{
desc: "success",
args: []string{"-virtual-storage=test-virtual-storage-1", "-repository=test-repository-1", "-authoritative-storage=test-physical-storage-2"},
matchError: matchNoError(),
expectedGenerations: map[string]int{st1: 2, st2: 2, st3: 2},
},
} {
t.Run(tc.desc, func(t *testing.T) {
cmd := &acceptDatalossSubcommand{}
fs := cmd.FlagSet()
require.NoError(t, fs.Parse(tc.args))
tc.matchError(t, cmd.Exec(fs, conf))
for storage, expected := range tc.expectedGenerations {
actual, err := rs.GetGeneration(ctx, vs, repo, storage)
require.NoError(t, err)
require.Equal(t, expected, actual, storage)
}
})
}
}
|