-
Notifications
You must be signed in to change notification settings - Fork 56
/
cmd_internal_test.go
233 lines (193 loc) · 5.38 KB
/
cmd_internal_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
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
package cmd
import (
"bytes"
"context"
"math/rand"
"os"
"path/filepath"
"strings"
"testing"
"github.com/omni-network/omni/halo/app"
halocfg "github.com/omni-network/omni/halo/config"
libcmd "github.com/omni-network/omni/lib/cmd"
"github.com/omni-network/omni/lib/log"
"github.com/omni-network/omni/lib/netconf"
"github.com/omni-network/omni/lib/tutil"
fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/require"
)
//go:generate go test . -golden -clean
func TestRunCmd(t *testing.T) {
setMonikerForT(t)
tests := []struct {
Name string
Args []string
Files map[string][]byte
Env map[string]string
}{
{
Name: "defaults",
Args: slice("run"),
},
{
Name: "flags",
Args: slice("run", "--home=foo", "--engine-jwt-file=bar"),
},
{
Name: "toml files",
Args: slice("run", "--home=testinput/input1"),
},
{
Name: "json files",
Args: slice("run", "--home=testinput/input2"),
Env: map[string]string{"HALO_NETWORK": "GOTEST"},
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
cmd := newRunCmd("run", func(_ context.Context, actual app.Config) error {
tutil.RequireGoldenJSON(t, actual)
return nil
})
for k, v := range test.Env {
t.Setenv(k, v)
}
rootCmd := libcmd.NewRootCmd("halo", "", cmd)
rootCmd.SetArgs(test.Args)
require.NoError(t, rootCmd.Execute())
})
}
}
func TestCLIReference(t *testing.T) {
t.Parallel()
const root = "halo" // Use to identify root command (vs subcommands).
tests := []struct {
Command string
}{
{root},
{"run"},
{"init"},
{"rollback"},
}
for _, test := range tests {
t.Run(test.Command, func(t *testing.T) {
t.Parallel()
var args []string
if test.Command != root {
if strings.Contains(test.Command, "operator") {
subCmd := strings.Split(test.Command, " ")
args = append(args, subCmd...)
} else {
args = append(args, test.Command)
}
}
args = append(args, "--help")
cmd := New()
cmd.SetArgs(args)
var bz bytes.Buffer
cmd.SetOut(&bz)
require.NoError(t, cmd.Execute())
tutil.RequireGoldenBytes(t, bz.Bytes())
})
}
}
//go:generate go test . -run=TestTomlConfig -count=100
func TestTomlConfig(t *testing.T) {
t.Parallel()
dir := t.TempDir()
var chars = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
randomString := func() string {
var resp string
for i := 0; i < 1+rand.Intn(64); i++ {
resp += string(chars[rand.Intn(len(chars))])
}
return resp
}
// Create a fuzzer with small uint64s and ansi strings (toml struggles with large numbers and UTF8).
fuzzer := fuzz.New().NilChance(0).NumElements(1, 8).Funcs(
func(i *uint64, c fuzz.Continue) {
*i = uint64(rand.Intn(1_000_000))
},
func(i *uint, c fuzz.Continue) {
*i = uint(rand.Intn(1_000_000))
},
func(s *string, c fuzz.Continue) {
*s = randomString()
},
func(s *netconf.ID, c fuzz.Continue) {
*s = netconf.ID(randomString())
},
)
var expect halocfg.Config
fuzzer.Fuzz(&expect)
expect.HomeDir = dir
// The Toml library converts map keys to lower case. So do this so expect==actual.
for k := range expect.RPCEndpoints {
expect.RPCEndpoints[strings.ToLower(randomString())] = randomString()
delete(expect.RPCEndpoints, k)
}
// Ensure the <home>/config directory exists.
require.NoError(t, os.Mkdir(filepath.Join(dir, "config"), 0o755))
// Write the randomized config to disk.
require.NoError(t, halocfg.WriteConfigTOML(expect, log.DefaultConfig()))
// Create a run command that asserts the config is as expected.
cmd := newRunCmd("run", func(_ context.Context, actual app.Config) error {
require.EqualValues(t, expect, actual.Config)
return nil
})
// Create and execute a root command that runs the run command.
rootCmd := libcmd.NewRootCmd("halo", "", cmd)
rootCmd.SetArgs([]string{"run", "--home=" + dir})
tutil.RequireNoError(t, rootCmd.Execute())
}
func TestInvalidCmds(t *testing.T) {
t.Parallel()
tests := []struct {
Name string
Args []string
ErrContains string
}{
{
Name: "no args",
Args: []string{},
ErrContains: "no sub-command specified",
},
{
Name: "invalid args",
Args: []string{"invalid"},
ErrContains: "unknown command \"invalid\" for \"halo\"",
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
t.Parallel()
rootCmd := libcmd.NewRootCmd("halo", "", New())
libcmd.WrapRunE(rootCmd, func(ctx context.Context, err error) {})
rootCmd.SetArgs(test.Args)
err := rootCmd.Execute()
require.Error(t, err)
require.Contains(t, err.Error(), test.ErrContains)
})
}
}
//nolint:paralleltest // Global test moniker is used.
func TestDefaultCometConfig(t *testing.T) {
setMonikerForT(t)
home := t.TempDir()
path := filepath.Join(home, "config", "config.toml")
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755))
cfg := DefaultCometConfig(home)
err := WriteCometConfig(path, &cfg)
require.NoError(t, err)
cfg2, err := parseCometConfig(context.Background(), home)
require.NoError(t, err)
cfg.StateSync.RPCServers = []string{} // Replace nil with empty slice for comparison.
require.Equal(t, cfg, cfg2)
bz, err := os.ReadFile(path)
require.NoError(t, err)
tutil.RequireGoldenBytes(t, bz, tutil.WithFilename("default_config.toml"))
}
// slice is a convenience function for creating string slice literals.
func slice(strs ...string) []string {
return strs
}