forked from hyperledger/fabric-x-committer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
425 lines (387 loc) · 12 KB
/
Copy pathutils.go
File metadata and controls
425 lines (387 loc) · 12 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package test
import (
"context"
"encoding/json"
"fmt"
"maps"
"runtime"
"slices"
"strings"
"sync"
"testing"
"time"
"github.com/cockroachdb/errors"
"github.com/onsi/gomega"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
healthgrpc "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/status"
"github.com/hyperledger/fabric-x-committer/api/protoblocktx"
"github.com/hyperledger/fabric-x-committer/utils/channel"
"github.com/hyperledger/fabric-x-committer/utils/connection"
"github.com/hyperledger/fabric-x-committer/utils/logging"
)
type (
// GrpcServers holds the server instances and their respective configurations.
GrpcServers struct {
Servers []*grpc.Server
Configs []*connection.ServerConfig
}
)
// FailHandler registers a [gomega] fail handler.
func FailHandler(t *testing.T) {
t.Helper()
gomega.RegisterFailHandler(func(message string, _ ...int) {
t.Helper()
t.Errorf("received error message: %s", message)
t.FailNow()
})
}
var (
// InsecureTLSConfig defines an empty tls config.
InsecureTLSConfig connection.TLSConfig
// defaultGrpcRetryProfile defines the retry policy for a gRPC client connection.
defaultGrpcRetryProfile connection.RetryProfile
)
// ServerToMultiClientConfig is used to create a multi client configuration from existing server(s).
func ServerToMultiClientConfig(servers ...*connection.ServerConfig) *connection.MultiClientConfig {
endpoints := make([]*connection.Endpoint, len(servers))
for i, server := range servers {
endpoints[i] = &server.Endpoint
}
return &connection.MultiClientConfig{
Endpoints: endpoints,
}
}
// RunGrpcServerForTest starts a GRPC server using a register method.
// It handles the cleanup of the GRPC server at the end of a test, and ensure the test is ended
// only when the GRPC server is down.
// It also updates the server config endpoint port to the actual port if the configuration
// did not specify a port.
// The method asserts that the GRPC server did not end with failure.
func RunGrpcServerForTest(
ctx context.Context, tb testing.TB, serverConfig *connection.ServerConfig, register func(server *grpc.Server),
) *grpc.Server {
tb.Helper()
listener, err := serverConfig.Listener(ctx)
require.NoError(tb, err)
server, err := serverConfig.GrpcServer()
require.NoError(tb, err)
if register != nil {
register(server)
} else {
healthgrpc.RegisterHealthServer(server, connection.DefaultHealthCheckService())
}
var wg sync.WaitGroup
tb.Cleanup(wg.Wait)
tb.Cleanup(server.Stop)
wg.Add(1)
go func() {
defer wg.Done()
// We use assert to prevent panicking for cleanup errors.
assert.NoError(tb, server.Serve(listener))
}()
_ = context.AfterFunc(ctx, func() {
server.Stop()
})
return server
}
// StartGrpcServersForTest starts multiple GRPC servers with a default configuration.
func StartGrpcServersForTest(
ctx context.Context,
t *testing.T,
numService int,
register func(*grpc.Server, int),
) *GrpcServers {
t.Helper()
sc := make([]*connection.ServerConfig, numService)
for i := range sc {
sc[i] = connection.NewLocalHostServerWithTLS(InsecureTLSConfig)
}
return StartGrpcServersWithConfigForTest(ctx, t, sc, register)
}
// StartGrpcServersWithConfigForTest starts multiple GRPC servers with given configurations.
func StartGrpcServersWithConfigForTest(
ctx context.Context, t *testing.T, sc []*connection.ServerConfig, register func(*grpc.Server, int),
) *GrpcServers {
t.Helper()
grpcServers := make([]*grpc.Server, len(sc))
for i, s := range sc {
i := i
grpcServers[i] = RunGrpcServerForTest(ctx, t, s, func(server *grpc.Server) {
if register != nil {
register(server, i)
} else {
healthgrpc.RegisterHealthServer(server, connection.DefaultHealthCheckService())
}
})
}
return &GrpcServers{
Servers: grpcServers,
Configs: sc,
}
}
// RunServiceForTest runs a service using the given service method, and waits for it to be ready
// given the waitFunc method.
// It handles the cleanup of the service at the end of a test, and ensure the test is ended
// only when the service return.
// The method asserts that the service did not end with failure.
// Returns a ready flag that indicate that the service is done.
func RunServiceForTest(
ctx context.Context,
tb testing.TB,
service func(ctx context.Context) error,
waitFunc func(ctx context.Context) bool,
) *channel.Ready {
tb.Helper()
doneFlag := channel.NewReady()
var wg sync.WaitGroup
// NOTE: we should cancel the context before waiting for the completion. Therefore, the
// order of cleanup matters, which is last added first called.
tb.Cleanup(wg.Wait)
dCtx, cancel := context.WithCancel(ctx)
tb.Cleanup(cancel)
wg.Add(1)
// We extract caller information to ensure we have sufficient information for debugging.
pc, file, no, ok := runtime.Caller(1)
require.True(tb, ok)
go func() {
defer wg.Done()
defer doneFlag.SignalReady()
// We use assert to prevent panicking for cleanup errors.
assert.NoErrorf(tb, service(dCtx), "called from %s:%d\n\t%s", file, no, runtime.FuncForPC(pc).Name())
}()
if waitFunc == nil {
return doneFlag
}
initCtx, initCancel := context.WithTimeout(dCtx, 2*time.Minute)
tb.Cleanup(initCancel)
require.True(tb, waitFunc(initCtx), "service is not ready")
return doneFlag
}
// RunServiceAndGrpcForTest combines running a service and its GRPC server.
// It is intended for services that implements the Service API (i.e., command line services).
func RunServiceAndGrpcForTest(
ctx context.Context,
t *testing.T,
service connection.Service,
serverConfig ...*connection.ServerConfig,
) *channel.Ready {
t.Helper()
doneFlag := RunServiceForTest(ctx, t, func(ctx context.Context) error {
return connection.FilterStreamRPCError(service.Run(ctx))
}, service.WaitForReady)
for _, server := range serverConfig {
RunGrpcServerForTest(ctx, t, server, service.RegisterService)
}
return doneFlag
}
// WaitUntilGrpcServerIsReady uses the health check API to check a service readiness.
func WaitUntilGrpcServerIsReady(
ctx context.Context,
t *testing.T,
conn grpc.ClientConnInterface,
) {
t.Helper()
if conn == nil {
return
}
healthClient := healthgrpc.NewHealthClient(conn)
res, err := healthClient.Check(ctx, nil, grpc.WaitForReady(true))
assert.NotEqual(t, codes.Canceled, status.Code(err))
require.NoError(t, err)
require.Equal(t, healthgrpc.HealthCheckResponse_SERVING, res.Status)
}
// StatusRetriever provides implementation retrieve status of given transaction identifiers.
type StatusRetriever interface {
GetTransactionsStatus(context.Context, *protoblocktx.QueryStatus, ...grpc.CallOption) (
*protoblocktx.TransactionsStatus, error,
)
}
// EnsurePersistedTxStatus fails the test if the given TX IDs does not match the expected status.
//
//nolint:revive // maximum number of arguments per function exceeded; max 4 but got 5.
func EnsurePersistedTxStatus(
ctx context.Context,
t *testing.T,
r StatusRetriever,
txIDs []string,
expected map[string]*protoblocktx.StatusWithHeight,
) {
t.Helper()
if len(txIDs) == 0 {
return
}
actualStatus, err := r.GetTransactionsStatus(ctx, &protoblocktx.QueryStatus{TxIDs: txIDs})
require.NoError(t, err)
require.EqualExportedValues(t, expected, actualStatus.Status)
}
// CheckServerStopped returns true if the grpc server listening on a
// given address has been stopped.
func CheckServerStopped(t *testing.T, addr string) bool {
t.Helper()
ctx, cancel := context.WithTimeout(t.Context(), 2*time.Second)
defer cancel()
conn, err := grpc.DialContext( //nolint:staticcheck
ctx,
addr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(), //nolint:staticcheck
)
if err != nil {
return true
}
_ = conn.Close()
return false
}
// SetupDebugging can be added for development to tests that required additional debugging info.
func SetupDebugging() {
logging.SetupWithConfig(&logging.Config{
Enabled: true,
Level: logging.Debug,
Caller: true,
Development: true,
})
}
// NewSecuredDialConfig creates the default dial config with given transport credentials.
func NewSecuredDialConfig(
t *testing.T,
endpoint connection.WithAddress,
tlsConfig connection.TLSConfig,
) *connection.DialConfig {
t.Helper()
clientCreds, err := tlsConfig.ClientCredentials()
require.NoError(t, err)
return connection.NewDialConfig(connection.DialConfigParameters{
Address: endpoint.Address(),
Creds: clientCreds,
Retry: &defaultGrpcRetryProfile,
})
}
// NewInsecureDialConfig creates the default dial config with insecure credentials.
func NewInsecureDialConfig(endpoint connection.WithAddress) *connection.DialConfig {
return connection.NewDialConfig(connection.DialConfigParameters{
Address: endpoint.Address(),
Creds: insecure.NewCredentials(),
Retry: &defaultGrpcRetryProfile,
})
}
// NewInsecureLoadBalancedDialConfig creates the default dial config with insecure credentials.
func NewInsecureLoadBalancedDialConfig(t *testing.T, endpoints []*connection.Endpoint) *connection.DialConfig {
t.Helper()
dialConfig, err := connection.NewLoadBalancedDialConfig(connection.MultiClientConfig{
Endpoints: endpoints,
Retry: &defaultGrpcRetryProfile,
})
require.NoError(t, err)
return dialConfig
}
// NewTLSMultiClientConfig creates a multi client configuration for test purposes
// given number of endpoints and a TLS configuration.
func NewTLSMultiClientConfig(
tlsConfig connection.TLSConfig,
ep ...*connection.Endpoint,
) *connection.MultiClientConfig {
return &connection.MultiClientConfig{
Endpoints: ep,
TLS: tlsConfig,
Retry: &defaultGrpcRetryProfile,
}
}
// NewInsecureClientConfig creates a client configuration for test purposes given an endpoint.
func NewInsecureClientConfig(ep *connection.Endpoint) *connection.ClientConfig {
return NewTLSClientConfig(InsecureTLSConfig, ep)
}
// NewTLSClientConfig creates a client configuration for test purposes given a single endpoint and creds.
func NewTLSClientConfig(tlsConfig connection.TLSConfig, ep *connection.Endpoint) *connection.ClientConfig {
return &connection.ClientConfig{
Endpoint: ep,
TLS: tlsConfig,
Retry: &defaultGrpcRetryProfile,
}
}
// LogStruct logs a struct in a flat representation.
func LogStruct(t *testing.T, name string, v any) {
t.Helper()
// Marshal struct to JSON
data, err := json.Marshal(v)
require.NoError(t, err)
// Unmarshal to map
var m map[string]any
err = json.Unmarshal(data, &m)
require.NoError(t, err)
// Flatten
flat := make(map[string]any)
flatten("", m, flat)
sb := &strings.Builder{}
for _, k := range slices.Sorted(maps.Keys(flat)) {
sb.WriteString(k)
sb.WriteString(": ")
_, printErr := fmt.Fprintf(sb, "%v", flat[k])
require.NoError(t, printErr)
sb.WriteString("\n")
}
t.Logf("%s:\n%s", name, sb.String())
}
func flatten(prefix string, in any, out map[string]any) {
switch val := in.(type) {
default:
out[prefix] = val
case map[string]any:
e := flattenEndpoint(val)
if e != nil {
out[prefix] = e
return
}
for k, v := range val {
key := k
if prefix != "" {
key = prefix + "." + k
}
flatten(key, v, out)
}
case []any:
for i, item := range val {
key := fmt.Sprintf("%s.%d", prefix, i)
flatten(key, item, out)
}
}
}
func flattenEndpoint(in map[string]any) *connection.Endpoint {
if len(in) != 2 {
return nil
}
host, okHost := in["host"]
if !okHost {
return nil
}
hostStr, okHostStr := host.(string)
if !okHostStr {
return nil
}
port, okPort := in["port"]
if !okPort {
return nil
}
portFloat, okPortFloat := port.(float64)
if !okPortFloat {
return nil
}
return &connection.Endpoint{Host: hostStr, Port: int(portFloat)}
}
// MustCreateEndpoint parses an endpoint from an address string.
// It panics if it fails to parse.
func MustCreateEndpoint(value string) *connection.Endpoint {
endpoint, err := connection.NewEndpoint(value)
if err != nil {
panic(errors.Wrap(err, "could not create endpoint"))
}
return endpoint
}