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
|
package txinfo
import (
"net"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v14/internal/backchannel"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/config"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/peer"
)
func muxedPeer(t testing.TB, p *peer.Peer) *peer.Peer {
t.Helper()
authInfo := p.AuthInfo
if authInfo == nil {
var err error
_, authInfo, err = backchannel.Insecure().ServerHandshake(nil)
require.NoError(t, err)
}
p.AuthInfo = backchannel.WithID(authInfo, 1)
return p
}
func tcpPeer(t *testing.T, ip string, port int) *peer.Peer {
parsedAddress := net.ParseIP(ip)
require.NotNil(t, parsedAddress)
return &peer.Peer{
Addr: &net.TCPAddr{
IP: parsedAddress,
Port: port,
},
}
}
func tlsPeer(t *testing.T, ip string, port int) *peer.Peer {
parsedAddress := net.ParseIP(ip)
require.NotNil(t, parsedAddress)
return &peer.Peer{
Addr: &net.TCPAddr{
IP: parsedAddress,
Port: port,
},
AuthInfo: credentials.TLSInfo{},
}
}
func unixPeer(t *testing.T, socket string) *peer.Peer {
return &peer.Peer{
Addr: &net.UnixAddr{
Name: socket,
},
}
}
func TestPraefect_InjectMetadata(t *testing.T) {
testcases := []struct {
desc string
listenAddress string
tlsListenAddress string
socketPath string
peer *peer.Peer
expectedAddress string
}{
{
desc: "wildcard listen address",
listenAddress: "0.0.0.0:1234",
peer: tcpPeer(t, "1.2.3.4", 4321),
expectedAddress: "tcp://1.2.3.4:1234",
},
{
desc: "explicit listen address",
listenAddress: "127.0.0.1:1234",
peer: tcpPeer(t, "1.2.3.4", 4321),
expectedAddress: "tcp://1.2.3.4:1234",
},
{
desc: "explicit listen address with explicit prefix",
listenAddress: "tcp://127.0.0.1:1234",
peer: tcpPeer(t, "1.2.3.4", 4321),
expectedAddress: "tcp://1.2.3.4:1234",
},
{
desc: "explicit TLS listen address",
tlsListenAddress: "127.0.0.1:1234",
peer: tlsPeer(t, "1.2.3.4", 4321),
expectedAddress: "tls://1.2.3.4:1234",
},
{
desc: "explicit TLS listen address with explicit prefix",
tlsListenAddress: "tls://127.0.0.1:1234",
peer: tlsPeer(t, "1.2.3.4", 4321),
expectedAddress: "tls://1.2.3.4:1234",
},
{
desc: "named host listen address",
listenAddress: "example.com:1234",
peer: tcpPeer(t, "1.2.3.4", 4321),
expectedAddress: "tcp://1.2.3.4:1234",
},
{
desc: "named host listen address with IPv6 peer",
listenAddress: "example.com:1234",
peer: tcpPeer(t, "2001:1db8:ac10:fe01::", 4321),
expectedAddress: "tcp://[2001:1db8:ac10:fe01::]:1234",
},
{
desc: "Unix socket path",
socketPath: "/tmp/socket",
peer: unixPeer(t, "@"),
expectedAddress: "unix:///tmp/socket",
},
{
desc: "Unix socket path with explicit prefix",
socketPath: "unix:///tmp/socket",
peer: unixPeer(t, "@"),
expectedAddress: "unix:///tmp/socket",
},
{
desc: "both addresses configured with TCP peer",
listenAddress: "0.0.0.0:1234",
socketPath: "/tmp/socket",
peer: tcpPeer(t, "1.2.3.4", 4321),
expectedAddress: "tcp://1.2.3.4:1234",
},
{
desc: "both addresses configured with Unix peer",
listenAddress: "0.0.0.0:1234",
socketPath: "/tmp/socket",
peer: unixPeer(t, "@"),
expectedAddress: "unix:///tmp/socket",
},
{
desc: "listen address with Unix peer",
listenAddress: "0.0.0.0:1234",
peer: unixPeer(t, "@"),
expectedAddress: "",
},
{
desc: "socket path with TCP peer",
socketPath: "/tmp/socket",
peer: tcpPeer(t, "1.2.3.4", 4321),
expectedAddress: "",
},
{
desc: "socket path with TLS peer",
socketPath: "/tmp/socket",
peer: tlsPeer(t, "1.2.3.4", 4321),
expectedAddress: "",
},
}
for _, tc := range testcases {
t.Run(tc.desc, func(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
cfg := config.Config{
ListenAddr: tc.listenAddress,
TLSListenAddr: tc.tlsListenAddress,
SocketPath: tc.socketPath,
}
for _, muxed := range []bool{false, true} {
desc := "unmuxed"
if muxed {
desc = "muxed"
}
t.Run(desc, func(t *testing.T) {
p := tc.peer
if muxed {
p = muxedPeer(t, tc.peer)
}
ctx = peer.NewContext(ctx, p)
praefectServer, err := PraefectFromConfig(cfg)
require.NoError(t, err)
ctx, err = praefectServer.Inject(ctx)
require.NoError(t, err)
server, err := PraefectFromContext(ctx)
if tc.expectedAddress == "" {
require.Error(t, err)
} else {
require.NoError(t, err)
address, err := server.Address()
require.NoError(t, err)
require.Equal(t, tc.expectedAddress, address)
}
})
}
})
}
}
|