forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_test.go
164 lines (133 loc) · 3.83 KB
/
start_test.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
package main
import (
"bytes"
"context"
"path/filepath"
"strings"
"testing"
"time"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)
// retryUntilTimeout runs the callback until the timeout is exceeded, or
// the callback returns a flag indicating completion
func retryUntilTimeout(ctx context.Context, cb func() bool) error {
ch := make(chan error, 1)
go func() {
defer close(ch)
for {
select {
case <-ctx.Done():
ch <- ctx.Err()
return
default:
retry := cb()
if !retry {
ch <- nil
return
}
}
time.Sleep(500 * time.Millisecond)
}
}()
return <-ch
}
// prepareNodeRPC sets the RPC listen address for the node to be an arbitrary
// free address. Setting the listen port to a free port on the machine avoids
// node collisions between different testing suites
func prepareNodeRPC(t *testing.T, nodeDir string) {
t.Helper()
path := constructConfigPath(nodeDir)
args := []string{
"config",
"init",
"--config-path",
path,
}
// Prepare the IO
mockOut := new(bytes.Buffer)
mockErr := new(bytes.Buffer)
io := commands.NewTestIO()
io.SetOut(commands.WriteNopCloser(mockOut))
io.SetErr(commands.WriteNopCloser(mockErr))
// Prepare the cmd context
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFn()
// Run config init
require.NoError(t, newRootCmd(io).ParseAndRun(ctx, args))
args = []string{
"config",
"set",
"--config-path",
path,
"rpc.laddr",
"tcp://0.0.0.0:0",
}
// Run config set
require.NoError(t, newRootCmd(io).ParseAndRun(ctx, args))
}
func TestStart_Lazy(t *testing.T) {
t.Parallel()
var (
nodeDir = t.TempDir()
genesisFile = filepath.Join(nodeDir, "test_genesis.json")
)
// Prepare the config
prepareNodeRPC(t, nodeDir)
args := []string{
"start",
"--lazy",
"--skip-failing-genesis-txs",
// These two flags are tested together as they would otherwise
// pollute this directory (cmd/gnoland) if not set.
"--data-dir",
nodeDir,
"--genesis",
genesisFile,
}
// Prepare the IO
mockOut := new(bytes.Buffer)
mockErr := new(bytes.Buffer)
io := commands.NewTestIO()
io.SetOut(commands.WriteNopCloser(mockOut))
io.SetErr(commands.WriteNopCloser(mockErr))
// Create and run the command
ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFn()
// Set up the command ctx
g, gCtx := errgroup.WithContext(ctx)
// Start the node
g.Go(func() error {
return newRootCmd(io).ParseAndRun(gCtx, args)
})
// Set up the retry ctx
retryCtx, retryCtxCancelFn := context.WithTimeout(ctx, 5*time.Second)
defer retryCtxCancelFn()
// This is a very janky way to verify the node has started.
// The alternative is to poll the node's RPC endpoints, but for some reason
// this introduces a lot of flakyness to the testing suite -- shocking!
// In an effort to keep this simple, and avoid randomly failing tests,
// we query the CLI output of the command
require.NoError(t, retryUntilTimeout(retryCtx, func() bool {
return !strings.Contains(mockOut.String(), startGraphic)
}))
cancelFn() // stop the node
require.NoError(t, g.Wait())
// Make sure the genesis is generated
assert.FileExists(t, genesisFile)
// Make sure the config is generated (default)
assert.FileExists(t, constructConfigPath(nodeDir))
// Make sure the secrets are generated
var (
secretsPath = constructSecretsPath(nodeDir)
validatorKeyPath = filepath.Join(secretsPath, defaultValidatorKeyName)
validatorStatePath = filepath.Join(secretsPath, defaultValidatorStateName)
nodeKeyPath = filepath.Join(secretsPath, defaultNodeKeyName)
)
assert.DirExists(t, secretsPath)
assert.FileExists(t, validatorKeyPath)
assert.FileExists(t, validatorStatePath)
assert.FileExists(t, nodeKeyPath)
}