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
|
package ref
import (
"context"
"io"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/internal/git/log"
"gitlab.com/gitlab-org/gitaly/internal/testhelper"
"google.golang.org/grpc/codes"
)
func TestSuccessfulFindAllRemoteBranchesRequest(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
server, serverSocketPath := runRefServiceServer(t)
defer server.Stop()
client, conn := newRefServiceClient(t, serverSocketPath)
defer conn.Close()
testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t)
defer cleanupFn()
remoteName := "my-remote"
expectedBranches := map[string]string{
"foo": "c7fbe50c7c7419d9701eebe64b1fdacc3df5b9dd",
"bar": "60ecb67744cb56576c30214ff52294f8ce2def98",
}
excludedRemote := "my-remote-2"
excludedBranches := map[string]string{
"from-another-remote": "5937ac0a7beb003549fc5fd26fc247adbce4a52e",
}
for branchName, commitID := range expectedBranches {
testhelper.CreateRemoteBranch(t, testRepoPath, remoteName, branchName, commitID)
}
for branchName, commitID := range excludedBranches {
testhelper.CreateRemoteBranch(t, testRepoPath, excludedRemote, branchName, commitID)
}
request := &gitalypb.FindAllRemoteBranchesRequest{Repository: testRepo, RemoteName: remoteName}
c, err := client.FindAllRemoteBranches(ctx, request)
if err != nil {
t.Fatal(err)
}
branches := readFindAllRemoteBranchesResponsesFromClient(t, c)
require.Len(t, branches, len(expectedBranches))
for branchName, commitID := range expectedBranches {
targetCommit, err := log.GetCommit(ctx, testRepo, commitID)
require.NoError(t, err)
expectedBranch := &gitalypb.Branch{
Name: []byte("refs/remotes/" + remoteName + "/" + branchName),
TargetCommit: targetCommit,
}
require.Contains(t, branches, expectedBranch)
}
for branchName, commitID := range excludedBranches {
targetCommit, err := log.GetCommit(ctx, testRepo, commitID)
require.NoError(t, err)
excludedBranch := &gitalypb.Branch{
Name: []byte("refs/remotes/" + excludedRemote + "/" + branchName),
TargetCommit: targetCommit,
}
require.NotContains(t, branches, excludedBranch)
}
}
func TestInvalidFindAllRemoteBranchesRequest(t *testing.T) {
server, serverSocketPath := runRefServiceServer(t)
defer server.Stop()
client, conn := newRefServiceClient(t, serverSocketPath)
defer conn.Close()
testRepo, _, cleanupFn := testhelper.NewTestRepo(t)
defer cleanupFn()
testCases := []struct {
description string
request gitalypb.FindAllRemoteBranchesRequest
}{
{
description: "Invalid repo",
request: gitalypb.FindAllRemoteBranchesRequest{
Repository: &gitalypb.Repository{
StorageName: "fake",
RelativePath: "repo",
},
},
},
{
description: "Empty repo",
request: gitalypb.FindAllRemoteBranchesRequest{RemoteName: "myRemote"},
},
{
description: "Empty remote name",
request: gitalypb.FindAllRemoteBranchesRequest{Repository: testRepo},
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c, err := client.FindAllRemoteBranches(ctx, &tc.request)
if err != nil {
t.Fatal(err)
}
var recvError error
for recvError == nil {
_, recvError = c.Recv()
}
testhelper.RequireGrpcError(t, recvError, codes.InvalidArgument)
})
}
}
func readFindAllRemoteBranchesResponsesFromClient(t *testing.T, c gitalypb.RefService_FindAllRemoteBranchesClient) []*gitalypb.Branch {
var branches []*gitalypb.Branch
for {
r, err := c.Recv()
if err == io.EOF {
break
}
require.NoError(t, err)
branches = append(branches, r.GetBranches()...)
}
return branches
}
|