-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathintegration.go
More file actions
408 lines (349 loc) · 10.9 KB
/
Copy pathintegration.go
File metadata and controls
408 lines (349 loc) · 10.9 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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package integration
import (
"cmp"
"context"
"encoding/json"
"os"
"os/signal"
"path"
"path/filepath"
"strings"
"syscall"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/api"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/common"
nwo_context "github.com/hyperledger-labs/fabric-smart-client/integration/nwo/common/context"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fabric"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fsc"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fsc/commands"
smartclient "github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fsc/node"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/monitoring"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/services/logging"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/utils"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
)
var logger = logging.MustGetLogger()
func init() {
c := logging.Config{}
// set grpc logging level as default to error; note that flogging default log level is just info
c.LogSpec = cmp.Or(os.Getenv("FABRIC_LOGGING_SPEC"), "grpc=error:info")
logging.Init(c)
}
type Configuration struct {
StartPort int
}
const WithRaceDetection = true
type Infrastructure struct {
TestDir string
StartPort int
NWOCtx *nwo_context.NWOContext
NWO *nwo.NWO
BuildServer *common.BuildServer
DeleteOnStop bool
DeleteOnStart bool
PlatformFactories map[string]api.PlatformFactory
Topologies []api.Topology
FscPlatform *fsc.Platform
}
func New(startPort int, path string, topologies ...api.Topology) (*Infrastructure, error) {
gomega.RegisterFailHandler(failMe)
defer ginkgo.GinkgoRecover()
var testDir string
var err error
if len(path) != 0 {
testDir, err = filepath.Abs(path)
if err != nil {
return nil, err
}
if _, err := os.Stat(testDir); os.IsNotExist(err) {
err = os.MkdirAll(testDir, 0o700)
if err != nil {
return nil, err
}
}
logger.Infof("Instantiating Integration infrastructure at [%s -> %s]", path, testDir)
} else {
testDir, err = os.MkdirTemp("", "integration")
if err != nil {
return nil, err
}
}
// build parameter: add pkcs11 tag only when the topology contains HSM nodes
var params []string
outer:
for _, t := range topologies {
if ft, ok := t.(*fsc.Topology); ok {
for _, n := range ft.Nodes {
fo := fabric.Options(n.Options)
if fo.DefaultIdentityByHSM() || len(fo.X509IdentitiesByHSM()) > 0 {
params = append(params, "-tags", "pkcs11")
break outer
}
}
}
}
// build with coverage profiling, more details: https://go.dev/doc/build-cover
//
// -coverpkg is required: without it, `-cover` only instruments each spawned
// binary's own packages, which all live under integration/. Those are then
// discarded by scripts/filter-coverage.sh (which excludes /integration/), so
// the integration tests would report no coverage at all. Instrumenting the
// whole module credits the platform/core code the FSC nodes exercise at
// runtime, which is the coverage the integration tests are meant to measure.
//
// Downstream modules that embed FSC nodes (e.g. fabric-token-sdk) run their
// own code inside these node binaries and need their packages instrumented
// instead of — or in addition to — FSC's. Go's standard channel for that is
// -coverpkg supplied via GOFLAGS. Respect it: only fall back to instrumenting
// the FSC module when the caller has not set -coverpkg, so a consumer that
// exports GOFLAGS=-coverpkg=<their-module>/... has it honoured rather than
// silently overridden, while FSC's own coverage build is unchanged.
if _, ok := os.LookupEnv("GOCOVERDIR"); ok {
params = append(params, "-cover")
if !goflagsHasCoverpkg() {
params = append(params, "-coverpkg=github.com/hyperledger-labs/fabric-smart-client/...")
}
}
buildServer := common.NewBuildServer(params...)
buildServer.Serve()
builder := buildServer.Client()
n := &Infrastructure{
TestDir: testDir,
StartPort: startPort,
NWOCtx: nwo_context.New(testDir, uint16(startPort), builder, topologies...),
BuildServer: buildServer,
DeleteOnStop: true,
Topologies: topologies,
PlatformFactories: map[string]api.PlatformFactory{
"fabric": fabric.NewPlatformFactory(),
"monitoring": monitoring.NewPlatformFactory(),
},
}
return n, nil
}
// goflagsHasCoverpkg reports whether the caller has already supplied a -coverpkg
// flag through the GOFLAGS environment variable. GOFLAGS is a space-separated
// list of flags that every go command merges into its own; an explicit -coverpkg
// there is the standard way for a consumer to select the packages instrumented
// in the cover-built node binaries.
func goflagsHasCoverpkg() bool {
for _, f := range strings.Fields(os.Getenv("GOFLAGS")) {
if strings.HasPrefix(f, "-coverpkg") || strings.HasPrefix(f, "--coverpkg") {
return true
}
}
return false
}
func Generate(startPort int, race bool, topologies ...api.Topology) (*Infrastructure, error) {
return GenerateAt(startPort, "", race, topologies...)
}
func GenerateAt(startPort int, path string, race bool, topologies ...api.Topology) (*Infrastructure, error) {
n, err := New(startPort, path, topologies...)
if err != nil {
return nil, err
}
if race {
n.EnableRaceDetector()
}
n.Generate()
return n, nil
}
func Load(startPort int, dir string, race bool, topologies ...api.Topology) (*Infrastructure, error) {
n, err := New(startPort, dir, topologies...)
if err != nil {
return nil, err
}
if race {
n.EnableRaceDetector()
}
n.DeleteOnStop = false
n.Load()
return n, nil
}
// Clients instantiate a new test integration infrastructure to access view client and CLI
func Clients(dir string, topologies ...api.Topology) (*Infrastructure, error) {
n, err := New(0, dir, topologies...)
if err != nil {
return nil, err
}
n.DeleteOnStop = false
n.InitClients()
return n, nil
}
func (i *Infrastructure) ViewCmd(node *smartclient.Replica) commands.View {
p := i.NWOCtx.PlatformsByName[fsc.TopologyName].(*fsc.Platform)
return commands.View{
TLSCA: path.Join(p.NodeLocalTLSDir(node.Peer), "ca.crt"),
UserCert: p.LocalMSPIdentityCert(node.Peer),
UserKey: p.LocalMSPPrivateKey(node.Peer),
NetworkPrefix: p.NetworkID,
Server: p.Context.ConnectionConfig(node.UniqueName).Address,
}
}
func (i *Infrastructure) RegisterPlatformFactory(factory api.PlatformFactory) {
i.PlatformFactories[factory.Name()] = factory
}
func (i *Infrastructure) Generate() {
if i.DeleteOnStart {
logger.Infof("Delete test folder [%s]", i.TestDir)
if err := os.RemoveAll(i.TestDir); err != nil {
panic(err)
}
}
i.initNWO()
i.NWO.Generate()
i.storeAdditionalConfigurations()
}
func (i *Infrastructure) Load() {
i.initNWO()
i.NWO.Load()
}
func (i *Infrastructure) InitClients() {
i.initNWO()
i.FscPlatform.InitClients()
}
func (i *Infrastructure) Start() {
if i.NWO == nil {
panic("call generate or load first")
}
i.NWO.Start()
}
func (i *Infrastructure) Stop() {
logger.Infof("stopping ...")
if i.NWO == nil {
panic("call generate or load first")
}
defer i.BuildServer.Shutdown(i.DeleteOnStop)
if i.DeleteOnStop {
defer utils.IgnoreErrorWithOneArg(os.RemoveAll, i.TestDir)
}
i.NWO.Stop()
}
func (i *Infrastructure) Serve() error {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
logger.Infof("All GOOD, networks up and running...")
logger.Infof("If you want to shut down the networks, press CTRL+C")
logger.Infof("Open another terminal to interact with the networks")
<-ctx.Done()
logger.Infof("Received signal, exiting...")
return nil
}
func (i *Infrastructure) Client(name string) api.GRPCClient {
if i.NWO == nil {
panic("call generate or load first")
}
return i.NWOCtx.ViewClients[name]
}
func (i *Infrastructure) WebClient(name string) api.WebClient {
if i.NWO == nil {
panic("call generate or load first")
}
return i.NWOCtx.WebClients[name]
}
func (i *Infrastructure) CLI(name string) api.ViewClient {
if i.NWO == nil {
panic("call generate or load first")
}
return i.NWOCtx.ViewCLIs[name]
}
func (i *Infrastructure) Identity(name string) view.Identity {
if i.NWO == nil {
panic("call generate or load first")
}
id, ok := i.NWOCtx.ViewIdentities[name]
if !ok {
return []byte(name)
}
return id
}
func (i *Infrastructure) StopFSCNode(id string) {
if i.NWO == nil {
panic("call generate or load first")
}
i.NWO.StopFSCNode(id)
}
func (i *Infrastructure) StartFSCNode(id string) {
if i.NWO == nil {
panic("call generate or load first")
}
i.NWO.StartFSCNode(id)
}
func (i *Infrastructure) EnableRaceDetector() {
i.BuildServer.EnableRaceDetector()
}
func (i *Infrastructure) initNWO() {
if i.NWO != nil {
// skip
return
}
var platforms []api.Platform
var fscTopology api.Topology
for _, topology := range i.Topologies {
label := strings.ToLower(topology.Type())
switch label {
case "fsc":
// treat fsc as last topo
fscTopology = topology
continue
default:
logger.Infof("Register %s", label)
factory, ok := i.PlatformFactories[label]
gomega.Expect(ok).To(gomega.BeTrue(), "expected to find platform [%s]", label)
platforms = append(platforms, factory.New(i.NWOCtx, topology, i.BuildServer.Client()))
}
}
// Add FSC platform
if fscTopology != nil {
if _, ok := i.PlatformFactories["fsc"]; !ok {
i.RegisterPlatformFactory(&fscDefaultPlatformFactory{})
}
factory := i.PlatformFactories["fsc"]
fscPlatform := factory.New(i.NWOCtx, fscTopology, i.BuildServer.Client())
platforms = append(platforms, fscPlatform)
i.FscPlatform = fscPlatform.(*fsc.Platform)
}
// Register platforms to context
for _, platform := range platforms {
i.NWOCtx.AddPlatform(platform)
}
i.NWO = nwo.New(i.NWOCtx, platforms...)
}
func (i *Infrastructure) storeAdditionalConfigurations() {
// store configuration
conf := &Configuration{
StartPort: i.StartPort,
}
raw, err := json.Marshal(conf)
if err != nil {
panic(err)
}
if err := os.WriteFile(filepath.Join(i.TestDir, "conf.json"), raw, 0o770); err != nil {
panic(err)
}
// store topology
t := api.Topologies{Topologies: i.Topologies}
raw, err = t.Export()
if err != nil {
panic(err)
}
if err := os.WriteFile(filepath.Join(i.TestDir, "topology.yaml"), raw, 0o770); err != nil {
panic(err)
}
}
func failMe(message string, callerSkip ...int) {
panic(message)
}
type fscDefaultPlatformFactory struct{}
func (p *fscDefaultPlatformFactory) Name() string {
return "fsc"
}
func (p *fscDefaultPlatformFactory) New(registry api.Context, t api.Topology, builder api.Builder) api.Platform {
return fsc.NewPlatform(registry, t, builder)
}