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
|
package praefect
import (
"net"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/storage"
"gitlab.com/gitlab-org/gitaly/v16/internal/grpc/protoregistry"
"gitlab.com/gitlab-org/gitaly/v16/internal/praefect/config"
"gitlab.com/gitlab-org/gitaly/v16/internal/praefect/datastore"
"gitlab.com/gitlab-org/gitaly/v16/internal/structerr"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper/testdb"
"gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func TestWalkReposHandler(t *testing.T) {
t.Parallel()
db := testdb.New(t)
for _, tc := range []struct {
desc string
request *gitalypb.WalkReposRequest
responses []*gitalypb.WalkReposResponse
expectedErr error
}{
{
desc: "missing storage name",
request: &gitalypb.WalkReposRequest{},
expectedErr: structerr.NewInvalidArgument("%w", storage.ErrStorageNotSet),
},
{
desc: "repositories found",
request: &gitalypb.WalkReposRequest{StorageName: "virtual-storage"},
responses: []*gitalypb.WalkReposResponse{
{RelativePath: "relative-path"},
{RelativePath: "relative-path-2"},
},
},
} {
t.Run(tc.desc, func(t *testing.T) {
db.TruncateAll(t)
rs := datastore.NewPostgresRepositoryStore(db, map[string][]string{"virtual-storage": {"storage"}})
ctx := testhelper.Context(t)
require.NoError(t, rs.CreateRepository(ctx, 0, "virtual-storage", "relative-path", "relative-path", "storage", nil, nil, false, false))
require.NoError(t, rs.CreateRepository(ctx, 1, "virtual-storage", "relative-path-2", "relative-path-2", "storage", nil, nil, false, false))
tmp := testhelper.TempDir(t)
ln, err := net.Listen("unix", filepath.Join(tmp, "praefect"))
require.NoError(t, err)
srv := NewGRPCServer(&Dependencies{
Config: config.Config{Failover: config.Failover{ElectionStrategy: config.ElectionStrategyPerRepository}},
Logger: testhelper.SharedLogger(t),
RepositoryStore: rs,
Registry: protoregistry.GitalyProtoPreregistered,
}, nil)
defer srv.Stop()
go testhelper.MustServe(t, srv, ln)
clientConn, err := grpc.DialContext(ctx, "unix://"+ln.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err)
defer testhelper.MustClose(t, clientConn)
client := gitalypb.NewInternalGitalyClient(clientConn)
stream, err := client.WalkRepos(ctx, tc.request)
if tc.expectedErr != nil {
// Consume the first message and test for errors only if we're expecting an error.
_, err = stream.Recv()
testhelper.RequireGrpcError(t, tc.expectedErr, err)
return
}
require.NoError(t, err)
actualRepos, err := testhelper.Receive(stream.Recv)
require.NoError(t, err)
testhelper.ProtoEqual(t, tc.responses, actualRepos)
})
}
}
|