Skip to content

Commit feb874b

Browse files
Merge pull request #660 from ava-labs/respect-stdout-stderr
Respect value of RedirectStdout/RedirectStderr
2 parents 51cafa6 + 0439a87 commit feb874b

File tree

7 files changed

+62
-18
lines changed

7 files changed

+62
-18
lines changed

examples/local/fivenodenetwork/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func main() {
6868

6969
func run(log logging.Logger, binaryPath string) error {
7070
// Create the network
71-
nw, err := local.NewDefaultNetwork(log, binaryPath, true)
71+
nw, err := local.NewDefaultNetwork(log, binaryPath, true, true, true)
7272
if err != nil {
7373
return err
7474
}

examples/local/indepth/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func main() {
7373

7474
func run(log logging.Logger, binaryPath string) error {
7575
// Create the network
76-
nw, err := local.NewDefaultNetwork(log, binaryPath, true)
76+
nw, err := local.NewDefaultNetwork(log, binaryPath, true, true, true)
7777
if err != nil {
7878
return err
7979
}

local/blockchain.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,11 @@ func (ln *localNetwork) installCustomChains(
292292
_, ok := ln.nodes[nodeName]
293293
if !ok {
294294
ln.log.Info(logging.Green.Wrap(fmt.Sprintf("adding new participant %s", nodeName)))
295-
if _, err := ln.addNode(node.Config{Name: nodeName}); err != nil {
295+
if _, err := ln.addNode(node.Config{
296+
Name: nodeName,
297+
RedirectStdout: ln.redirectStdout,
298+
RedirectStderr: ln.redirectStderr,
299+
}); err != nil {
296300
return nil, err
297301
}
298302
}
@@ -437,7 +441,11 @@ func (ln *localNetwork) installSubnets(
437441
_, ok := ln.nodes[nodeName]
438442
if !ok {
439443
ln.log.Info(logging.Green.Wrap(fmt.Sprintf("adding new participant %s", nodeName)))
440-
if _, err := ln.addNode(node.Config{Name: nodeName}); err != nil {
444+
if _, err := ln.addNode(node.Config{
445+
Name: nodeName,
446+
RedirectStdout: ln.redirectStdout,
447+
RedirectStderr: ln.redirectStderr,
448+
}); err != nil {
441449
return nil, err
442450
}
443451
}
@@ -1065,7 +1073,11 @@ func (ln *localNetwork) addPermissionlessValidators(
10651073
_, ok := ln.nodes[validatorSpec.NodeName]
10661074
if !ok {
10671075
ln.log.Info(logging.Green.Wrap(fmt.Sprintf("adding new participant %s", validatorSpec.NodeName)))
1068-
if _, err := ln.addNode(node.Config{Name: validatorSpec.NodeName}); err != nil {
1076+
if _, err := ln.addNode(node.Config{
1077+
Name: validatorSpec.NodeName,
1078+
RedirectStdout: ln.redirectStdout,
1079+
RedirectStderr: ln.redirectStderr,
1080+
}); err != nil {
10691081
return err
10701082
}
10711083
}

local/network.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ type localNetwork struct {
115115
subnetConfigFiles map[string]string
116116
// if true, for ports given in conf that are already taken, assign new random ones
117117
reassignPortsIfUsed bool
118+
// if true, direct this node's Stdout to os.Stdout
119+
redirectStdout bool
120+
// if true, direct this node's Stderr to os.Stderr
121+
redirectStderr bool
118122
// map from subnet id to elastic subnet tx id
119123
subnetID2ElasticSubnetID map[ids.ID]ids.ID
120124
}
@@ -265,6 +269,8 @@ func NewNetwork(
265269
rootDir string,
266270
snapshotsDir string,
267271
reassignPortsIfUsed bool,
272+
redirectStdout bool,
273+
redirectStderr bool,
268274
) (network.Network, error) {
269275
net, err := newNetwork(
270276
log,
@@ -278,6 +284,8 @@ func NewNetwork(
278284
rootDir,
279285
snapshotsDir,
280286
reassignPortsIfUsed,
287+
redirectStdout,
288+
redirectStderr,
281289
)
282290
if err != nil {
283291
return net, err
@@ -295,6 +303,8 @@ func newNetwork(
295303
rootDir string,
296304
snapshotsDir string,
297305
reassignPortsIfUsed bool,
306+
redirectStdout bool,
307+
redirectStderr bool,
298308
) (*localNetwork, error) {
299309
var err error
300310
if rootDir == "" {
@@ -329,6 +339,8 @@ func newNetwork(
329339
rootDir: rootDir,
330340
snapshotsDir: snapshotsDir,
331341
reassignPortsIfUsed: reassignPortsIfUsed,
342+
redirectStdout: redirectStdout,
343+
redirectStderr: redirectStderr,
332344
subnetID2ElasticSubnetID: map[ids.ID]ids.ID{},
333345
}
334346
return net, nil
@@ -357,9 +369,11 @@ func NewDefaultNetwork(
357369
log logging.Logger,
358370
binaryPath string,
359371
reassignPortsIfUsed bool,
372+
redirectStdout bool,
373+
redirectStderr bool,
360374
) (network.Network, error) {
361375
config := NewDefaultConfig(binaryPath)
362-
return NewNetwork(log, config, "", "", reassignPortsIfUsed)
376+
return NewNetwork(log, config, "", "", reassignPortsIfUsed, redirectStdout, redirectStderr)
363377
}
364378

365379
// NewDefaultConfig creates a new default network config

local/network_test.go

+23-11
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ func TestNewNetworkEmpty(t *testing.T) {
150150
"",
151151
"",
152152
false,
153+
false,
154+
false,
153155
)
154156
require.NoError(err)
155157
err = net.loadConfig(context.Background(), networkConfig)
@@ -213,6 +215,8 @@ func TestNewNetworkOneNode(t *testing.T) {
213215
"",
214216
"",
215217
false,
218+
false,
219+
false,
216220
)
217221
require.NoError(err)
218222
err = net.loadConfig(context.Background(), networkConfig)
@@ -241,6 +245,8 @@ func TestNewNetworkFailToStartNode(t *testing.T) {
241245
"",
242246
"",
243247
false,
248+
false,
249+
false,
244250
)
245251
require.NoError(err)
246252
err = net.loadConfig(context.Background(), networkConfig)
@@ -477,7 +483,7 @@ func TestWrongNetworkConfigs(t *testing.T) {
477483
require := require.New(t)
478484
for name, tt := range tests {
479485
t.Run(name, func(t *testing.T) {
480-
net, err := newNetwork(logging.NoLog{}, newMockAPISuccessful, &localTestSuccessfulNodeProcessCreator{}, "", "", false)
486+
net, err := newNetwork(logging.NoLog{}, newMockAPISuccessful, &localTestSuccessfulNodeProcessCreator{}, "", "", false, false, false)
481487
require.NoError(err)
482488
err = net.loadConfig(context.Background(), tt.config)
483489
require.Error(err)
@@ -491,7 +497,7 @@ func TestUnhealthyNetwork(t *testing.T) {
491497
t.Parallel()
492498
require := require.New(t)
493499
networkConfig := testNetworkConfig(t)
494-
net, err := newNetwork(logging.NoLog{}, newMockAPIUnhealthy, &localTestSuccessfulNodeProcessCreator{}, "", "", false)
500+
net, err := newNetwork(logging.NoLog{}, newMockAPIUnhealthy, &localTestSuccessfulNodeProcessCreator{}, "", "", false, false, false)
495501
require.NoError(err)
496502
err = net.loadConfig(context.Background(), networkConfig)
497503
require.NoError(err)
@@ -506,7 +512,7 @@ func TestGeneratedNodesNames(t *testing.T) {
506512
for i := range networkConfig.NodeConfigs {
507513
networkConfig.NodeConfigs[i].Name = ""
508514
}
509-
net, err := newNetwork(logging.NoLog{}, newMockAPISuccessful, &localTestSuccessfulNodeProcessCreator{}, "", "", false)
515+
net, err := newNetwork(logging.NoLog{}, newMockAPISuccessful, &localTestSuccessfulNodeProcessCreator{}, "", "", false, false, false)
510516
require.NoError(err)
511517
err = net.loadConfig(context.Background(), networkConfig)
512518
require.NoError(err)
@@ -526,7 +532,7 @@ func TestGenerateDefaultNetwork(t *testing.T) {
526532
require := require.New(t)
527533
binaryPath := "pepito"
528534
networkConfig := NewDefaultConfig(binaryPath)
529-
net, err := newNetwork(logging.NoLog{}, newMockAPISuccessful, &localTestSuccessfulNodeProcessCreator{}, "", "", false)
535+
net, err := newNetwork(logging.NoLog{}, newMockAPISuccessful, &localTestSuccessfulNodeProcessCreator{}, "", "", false, false, false)
530536
require.NoError(err)
531537
err = net.loadConfig(context.Background(), networkConfig)
532538
require.NoError(err)
@@ -576,7 +582,7 @@ func TestNetworkFromConfig(t *testing.T) {
576582
t.Parallel()
577583
require := require.New(t)
578584
networkConfig := testNetworkConfig(t)
579-
net, err := newNetwork(logging.NoLog{}, newMockAPISuccessful, &localTestSuccessfulNodeProcessCreator{}, "", "", false)
585+
net, err := newNetwork(logging.NoLog{}, newMockAPISuccessful, &localTestSuccessfulNodeProcessCreator{}, "", "", false, false, false)
580586
require.NoError(err)
581587
err = net.loadConfig(context.Background(), networkConfig)
582588
require.NoError(err)
@@ -600,7 +606,7 @@ func TestNetworkNodeOps(t *testing.T) {
600606
// Start a new, empty network
601607
emptyNetworkConfig, err := emptyNetworkConfig()
602608
require.NoError(err)
603-
net, err := newNetwork(logging.NoLog{}, newMockAPISuccessful, &localTestSuccessfulNodeProcessCreator{}, "", "", false)
609+
net, err := newNetwork(logging.NoLog{}, newMockAPISuccessful, &localTestSuccessfulNodeProcessCreator{}, "", "", false, false, false)
604610
require.NoError(err)
605611
err = net.loadConfig(context.Background(), emptyNetworkConfig)
606612
require.NoError(err)
@@ -638,7 +644,7 @@ func TestNodeNotFound(t *testing.T) {
638644
emptyNetworkConfig, err := emptyNetworkConfig()
639645
require.NoError(err)
640646
networkConfig := testNetworkConfig(t)
641-
net, err := newNetwork(logging.NoLog{}, newMockAPISuccessful, &localTestSuccessfulNodeProcessCreator{}, "", "", false)
647+
net, err := newNetwork(logging.NoLog{}, newMockAPISuccessful, &localTestSuccessfulNodeProcessCreator{}, "", "", false, false, false)
642648
require.NoError(err)
643649
err = net.loadConfig(context.Background(), emptyNetworkConfig)
644650
require.NoError(err)
@@ -671,7 +677,7 @@ func TestStoppedNetwork(t *testing.T) {
671677
emptyNetworkConfig, err := emptyNetworkConfig()
672678
require.NoError(err)
673679
networkConfig := testNetworkConfig(t)
674-
net, err := newNetwork(logging.NoLog{}, newMockAPISuccessful, &localTestSuccessfulNodeProcessCreator{}, "", "", false)
680+
net, err := newNetwork(logging.NoLog{}, newMockAPISuccessful, &localTestSuccessfulNodeProcessCreator{}, "", "", false, false, false)
675681
require.NoError(err)
676682
err = net.loadConfig(context.Background(), emptyNetworkConfig)
677683
require.NoError(err)
@@ -704,7 +710,7 @@ func TestStoppedNetwork(t *testing.T) {
704710
func TestGetAllNodes(t *testing.T) {
705711
require := require.New(t)
706712
networkConfig := testNetworkConfig(t)
707-
net, err := newNetwork(logging.NoLog{}, newMockAPISuccessful, &localTestSuccessfulNodeProcessCreator{}, "", "", false)
713+
net, err := newNetwork(logging.NoLog{}, newMockAPISuccessful, &localTestSuccessfulNodeProcessCreator{}, "", "", false, false, false)
708714
require.NoError(err)
709715
err = net.loadConfig(context.Background(), networkConfig)
710716
require.NoError(err)
@@ -756,6 +762,8 @@ func TestFlags(t *testing.T) {
756762
"",
757763
"",
758764
false,
765+
false,
766+
false,
759767
)
760768
require.NoError(err)
761769
err = nw.loadConfig(context.Background(), networkConfig)
@@ -785,6 +793,8 @@ func TestFlags(t *testing.T) {
785793
"",
786794
"",
787795
false,
796+
false,
797+
false,
788798
)
789799
require.NoError(err)
790800
err = nw.loadConfig(context.Background(), networkConfig)
@@ -813,6 +823,8 @@ func TestFlags(t *testing.T) {
813823
"",
814824
"",
815825
false,
826+
false,
827+
false,
816828
)
817829
require.NoError(err)
818830
err = nw.loadConfig(context.Background(), networkConfig)
@@ -1269,7 +1281,7 @@ func TestRemoveBeacon(t *testing.T) {
12691281
// create a network with no nodes in it
12701282
emptyNetworkConfig, err := emptyNetworkConfig()
12711283
require.NoError(err)
1272-
net, err := newNetwork(logging.NoLog{}, newMockAPISuccessful, &localTestSuccessfulNodeProcessCreator{}, "", "", false)
1284+
net, err := newNetwork(logging.NoLog{}, newMockAPISuccessful, &localTestSuccessfulNodeProcessCreator{}, "", "", false, false, false)
12731285
require.NoError(err)
12741286
err = net.loadConfig(context.Background(), emptyNetworkConfig)
12751287
require.NoError(err)
@@ -1320,7 +1332,7 @@ func TestHealthyDuringNetworkStop(t *testing.T) {
13201332
require := require.New(t)
13211333
networkConfig := testNetworkConfig(t)
13221334
// Calls to a node's Healthy() function blocks until context cancelled
1323-
net, err := newNetwork(logging.NoLog{}, newMockAPIHealthyBlocks, &localTestSuccessfulNodeProcessCreator{}, "", "", false)
1335+
net, err := newNetwork(logging.NoLog{}, newMockAPIHealthyBlocks, &localTestSuccessfulNodeProcessCreator{}, "", "", false, false, false)
13241336
require.NoError(err)
13251337
err = net.loadConfig(context.Background(), networkConfig)
13261338
require.NoError(err)

local/snapshot.go

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ func NewNetworkFromSnapshot(
7070
subnetConfigs map[string]string,
7171
flags map[string]interface{},
7272
reassignPortsIfUsed bool,
73+
redirectStdout bool,
74+
redirectStderr bool,
7375
) (network.Network, error) {
7476
net, err := newNetwork(
7577
log,
@@ -83,6 +85,8 @@ func NewNetworkFromSnapshot(
8385
rootDir,
8486
snapshotsDir,
8587
reassignPortsIfUsed,
88+
redirectStdout,
89+
redirectStderr,
8690
)
8791
if err != nil {
8892
return net, err

server/network.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func (lc *localNetwork) Start(ctx context.Context) error {
235235
}
236236

237237
ux.Print(lc.log, logging.Blue.Wrap(logging.Bold.Wrap("create and run local network")))
238-
nw, err := local.NewNetwork(lc.log, lc.cfg, lc.options.rootDataDir, lc.options.snapshotsDir, lc.options.reassignPortsIfUsed)
238+
nw, err := local.NewNetwork(lc.log, lc.cfg, lc.options.rootDataDir, lc.options.snapshotsDir, lc.options.reassignPortsIfUsed, lc.options.redirectNodesOutput, lc.options.redirectNodesOutput)
239239
if err != nil {
240240
return err
241241
}
@@ -519,6 +519,8 @@ func (lc *localNetwork) LoadSnapshot(snapshotName string) error {
519519
lc.options.subnetConfigs,
520520
globalNodeConfig,
521521
lc.options.reassignPortsIfUsed,
522+
lc.options.redirectNodesOutput,
523+
lc.options.redirectNodesOutput,
522524
)
523525
if err != nil {
524526
return err

0 commit comments

Comments
 (0)