-
Notifications
You must be signed in to change notification settings - Fork 301
/
cluster.go
305 lines (269 loc) · 8.85 KB
/
cluster.go
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package test
import (
"context"
"reflect"
"go.thethings.network/lorawan-stack/pkg/cluster"
"go.thethings.network/lorawan-stack/pkg/rpcclient"
"go.thethings.network/lorawan-stack/pkg/rpcserver"
"go.thethings.network/lorawan-stack/pkg/ttnpb"
"google.golang.org/grpc"
)
// MockPeer is a mock cluster.Peer used for testing.
type MockPeer struct {
NameFunc func() string
ConnFunc func() (*grpc.ClientConn, error)
HasRoleFunc func(ttnpb.ClusterRole) bool
RolesFunc func() []ttnpb.ClusterRole
TagsFunc func() map[string]string
}
// Name calls NameFunc if set and panics otherwise.
func (m MockPeer) Name() string {
if m.NameFunc == nil {
panic("Name called, but not set")
}
return m.NameFunc()
}
// Conn calls ConnFunc if set and panics otherwise.
func (m MockPeer) Conn() (*grpc.ClientConn, error) {
if m.ConnFunc == nil {
panic("Conn called, but not set")
}
return m.ConnFunc()
}
// HasRole calls HasRoleFunc if set and panics otherwise.
func (m MockPeer) HasRole(r ttnpb.ClusterRole) bool {
if m.HasRoleFunc == nil {
panic("HasRole called, but not set")
}
return m.HasRoleFunc(r)
}
// Roles calls RolesFunc if set and panics otherwise.
func (m MockPeer) Roles() []ttnpb.ClusterRole {
if m.RolesFunc == nil {
panic("Roles called, but not set")
}
return m.RolesFunc()
}
// Tags calls TagsFunc if set and panics otherwise.
func (m MockPeer) Tags() map[string]string {
if m.TagsFunc == nil {
panic("Tags called, but not set")
}
return m.TagsFunc()
}
// NewGRPCServerPeer creates a new MockPeer with ConnFunc, which always returns the same loopback connection to the server itself.
// srv is the implementation of the gRPC interface.
// registrators represents a slice of functions, which register the gRPC interface implementation at a gRPC server.
func NewGRPCServerPeer(ctx context.Context, srv interface{}, registrators ...interface{}) (*MockPeer, error) {
grpcSrv := rpcserver.New(ctx).Server
for _, r := range registrators {
reflect.ValueOf(r).Call([]reflect.Value{
reflect.ValueOf(grpcSrv),
reflect.ValueOf(srv),
})
}
conn, err := rpcserver.StartLoopback(ctx, grpcSrv, rpcclient.DefaultDialOptions(ctx)...)
if err != nil {
return nil, err
}
return &MockPeer{
ConnFunc: func() (*grpc.ClientConn, error) { return conn, nil },
}, nil
}
// MockCluster is a mock cluster.Cluster used for testing.
type MockCluster struct {
JoinFunc func() error
LeaveFunc func() error
GetPeersFunc func(ctx context.Context, role ttnpb.ClusterRole) ([]cluster.Peer, error)
GetPeerFunc func(ctx context.Context, role ttnpb.ClusterRole, ids ttnpb.Identifiers) (cluster.Peer, error)
ClaimIDsFunc func(ctx context.Context, ids ttnpb.Identifiers) error
UnclaimIDsFunc func(ctx context.Context, ids ttnpb.Identifiers) error
TLSFunc func() bool
AuthFunc func() grpc.CallOption
WithVerifiedSourceFunc func(ctx context.Context) context.Context
}
// Join calls JoinFunc if set and panics otherwise.
func (m MockCluster) Join() error {
if m.JoinFunc == nil {
panic("Join called, but not set")
}
return m.JoinFunc()
}
// Leave calls LeaveFunc if set and panics otherwise.
func (m MockCluster) Leave() error {
if m.LeaveFunc == nil {
panic("Leave called, but not set")
}
return m.LeaveFunc()
}
// GetPeers calls GetPeersFunc if set and panics otherwise.
func (m MockCluster) GetPeers(ctx context.Context, role ttnpb.ClusterRole) ([]cluster.Peer, error) {
if m.GetPeersFunc == nil {
panic("GetPeers called, but not set")
}
return m.GetPeersFunc(ctx, role)
}
// GetPeer calls GetPeerFunc if set and panics otherwise.
func (m MockCluster) GetPeer(ctx context.Context, role ttnpb.ClusterRole, ids ttnpb.Identifiers) (cluster.Peer, error) {
if m.GetPeerFunc == nil {
panic("GetPeer called, but not set")
}
return m.GetPeerFunc(ctx, role, ids)
}
// GetPeerConn calls GetPeer and then Conn.
func (m MockCluster) GetPeerConn(ctx context.Context, role ttnpb.ClusterRole, ids ttnpb.Identifiers) (*grpc.ClientConn, error) {
peer, err := m.GetPeer(ctx, role, ids)
if err != nil {
return nil, err
}
return peer.Conn()
}
// ClaimIDs calls ClaimIDsFunc if set and panics otherwise.
func (m MockCluster) ClaimIDs(ctx context.Context, ids ttnpb.Identifiers) error {
if m.ClaimIDsFunc == nil {
panic("ClaimIDs called, but not set")
}
return m.ClaimIDsFunc(ctx, ids)
}
// UnclaimIDs calls UnclaimIDsFunc if set and panics otherwise.
func (m MockCluster) UnclaimIDs(ctx context.Context, ids ttnpb.Identifiers) error {
if m.UnclaimIDsFunc == nil {
panic("UnclaimIDs called, but not set")
}
return m.UnclaimIDsFunc(ctx, ids)
}
// TLS calls TLSFunc if set and panics otherwise.
func (m MockCluster) TLS() bool {
if m.TLSFunc == nil {
panic("TLS called, but not set")
}
return m.TLSFunc()
}
// Auth calls AuthFunc if set and panics otherwise.
func (m MockCluster) Auth() grpc.CallOption {
if m.AuthFunc == nil {
panic("Auth called, but not set")
}
return m.AuthFunc()
}
// WithVerifiedSource calls WithVerifiedSourceFunc if set and panics otherwise.
func (m MockCluster) WithVerifiedSource(ctx context.Context) context.Context {
if m.WithVerifiedSourceFunc == nil {
panic("WithVerifiedSource called, but not set")
}
return m.WithVerifiedSourceFunc(ctx)
}
type ClusterAuthRequest struct {
Response chan<- grpc.CallOption
}
func MakeClusterAuthChFunc(reqCh chan<- ClusterAuthRequest) func() grpc.CallOption {
return func() grpc.CallOption {
respCh := make(chan grpc.CallOption)
reqCh <- ClusterAuthRequest{
Response: respCh,
}
return <-respCh
}
}
type ClusterGetPeerResponse struct {
Peer cluster.Peer
Error error
}
type ClusterGetPeerRequest struct {
Context context.Context
Role ttnpb.ClusterRole
Identifiers ttnpb.Identifiers
Response chan<- ClusterGetPeerResponse
}
func MakeClusterGetPeerChFunc(reqCh chan<- ClusterGetPeerRequest) func(context.Context, ttnpb.ClusterRole, ttnpb.Identifiers) (cluster.Peer, error) {
return func(ctx context.Context, role ttnpb.ClusterRole, ids ttnpb.Identifiers) (cluster.Peer, error) {
respCh := make(chan ClusterGetPeerResponse)
reqCh <- ClusterGetPeerRequest{
Context: ctx,
Role: role,
Identifiers: ids,
Response: respCh,
}
resp := <-respCh
return resp.Peer, resp.Error
}
}
type ClusterJoinRequest struct {
Response chan<- error
}
func MakeClusterJoinChFunc(reqCh chan<- ClusterJoinRequest) func() error {
return func() error {
respCh := make(chan error)
reqCh <- ClusterJoinRequest{
Response: respCh,
}
return <-respCh
}
}
func ClusterJoinNilFunc() error { return nil }
func AssertClusterAuthRequest(ctx context.Context, reqCh <-chan ClusterAuthRequest, resp grpc.CallOption) bool {
t := MustTFromContext(ctx)
t.Helper()
select {
case <-ctx.Done():
t.Error("Timed out while waiting for Cluster.Auth to be called")
return false
case req := <-reqCh:
t.Log("Cluster.Auth called")
select {
case <-ctx.Done():
t.Error("Timed out while waiting for Cluster.Auth response to be processed")
return false
case req.Response <- resp:
return true
}
}
}
func AssertClusterGetPeerRequest(ctx context.Context, reqCh <-chan ClusterGetPeerRequest, assert func(ctx context.Context, role ttnpb.ClusterRole, ids ttnpb.Identifiers) bool, resp ClusterGetPeerResponse) bool {
t := MustTFromContext(ctx)
t.Helper()
select {
case <-ctx.Done():
t.Error("Timed out while waiting for Cluster.GetPeer to be called")
return false
case req := <-reqCh:
t.Log("Cluster.GetPeer called")
if !assert(req.Context, req.Role, req.Identifiers) {
return false
}
select {
case <-ctx.Done():
t.Error("Timed out while waiting for Cluster.GetPeer response to be processed")
return false
case req.Response <- resp:
return true
}
}
}
func AssertClusterGetPeerRequestSequence(ctx context.Context, reqCh <-chan ClusterGetPeerRequest, peers []ClusterGetPeerResponse, assertions ...func(context.Context, ttnpb.ClusterRole, ttnpb.Identifiers) bool) bool {
t := MustTFromContext(ctx)
t.Helper()
if len(peers) != len(assertions) {
panic("Length mismatch between peers and assertions")
}
for i, p := range peers {
if !AssertClusterGetPeerRequest(ctx, reqCh, assertions[i], p) {
t.Errorf("Cluster.GetPeer assertion failed for peer %d", i)
return false
}
}
return true
}