-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathmanager_test.go
446 lines (385 loc) · 16.6 KB
/
manager_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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package config
import (
"context"
"io"
"os"
"path/filepath"
"testing"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
cmtcmds "github.com/cometbft/cometbft/cmd/cometbft/commands"
cmtconfig "github.com/cometbft/cometbft/config"
"cosmossdk.io/log"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
"github.com/provenance-io/provenance/app"
simappparams "github.com/provenance-io/provenance/app/params"
"github.com/provenance-io/provenance/internal/pioconfig"
)
type ConfigManagerTestSuite struct {
suite.Suite
Home string
EncodingConfig simappparams.EncodingConfig
}
func TestConfigManagerTestSuite(t *testing.T) {
suite.Run(t, new(ConfigManagerTestSuite))
}
func (s *ConfigManagerTestSuite) SetupTest() {
s.Home = s.T().TempDir()
s.T().Logf("%s Home: %s", s.T().Name(), s.Home)
s.EncodingConfig = app.MakeTestEncodingConfig(s.T())
}
// makeDummyCmd creates a dummy command with a context in it that can be used to test all the manager stuff.
func (s *ConfigManagerTestSuite) makeDummyCmd() *cobra.Command {
clientCtx := client.Context{}.
WithCodec(s.EncodingConfig.Marshaler).
WithHomeDir(s.Home)
clientCtx.Viper = viper.New()
serverCtx := server.NewContext(clientCtx.Viper, DefaultCmtConfig(), log.NewNopLogger())
ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx)
dummyCmd := &cobra.Command{
Use: "dummy",
Short: "Just used for testing. Doesn't do anything.",
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}
dummyCmd.SetOut(io.Discard)
dummyCmd.SetErr(io.Discard)
dummyCmd.SetContext(ctx)
return dummyCmd
}
func (s *ConfigManagerTestSuite) logFile(path string) {
if !FileExists(path) {
s.T().Logf("File does not exist: %s", path)
return
}
contents, err := os.ReadFile(path)
if err != nil {
s.T().Logf("Error reading %s: %v", path, err)
return
}
s.T().Logf("File: %s\nContents:\n%s", path, contents)
}
func (s *ConfigManagerTestSuite) TestConfigIndexEventsWriteRead() {
// The IndexEvents field has some special handling that was broken at one point.
// This test exists to make sure it doesn't break again.
// Create config with two IndexEvents entries, and write it to a file.
confFile := filepath.Join(s.Home, "app.toml")
appConfig := serverconfig.DefaultConfig()
appConfig.IndexEvents = []string{"key1", "key2"}
serverconfig.WriteConfigFile(confFile, appConfig)
// Read that file into viper.
vpr := viper.New()
vpr.SetConfigFile(confFile)
err := vpr.ReadInConfig()
s.Require().NoError(err, "reading config file into viper")
vprIndexEvents := vpr.GetStringSlice("index-events")
s.Require().Equal(appConfig.IndexEvents, vprIndexEvents, "viper's index events")
}
func (s *ConfigManagerTestSuite) TestManagerWriteAppConfigWithIndexEventsThenReadIt() {
// This test is just making sure that writing/reading index events works in our stuff.
dCmd := s.makeDummyCmd()
appConfig := serverconfig.DefaultConfig()
appConfig.IndexEvents = []string{"key1", "key2"}
SaveConfigs(dCmd, appConfig, nil, nil, false)
err := LoadConfigFromFiles(dCmd)
s.Require().NoError(err, "loading config from files")
appConfig2, err2 := ExtractAppConfig(dCmd)
s.Require().NoError(err2, "extracging app config")
s.Require().Equal(appConfig.IndexEvents, appConfig2.IndexEvents, "index events before/after")
}
func (s *ConfigManagerTestSuite) TestPackedConfigCosmosLoadDefaults() {
dCmd := s.makeDummyCmd()
appConfig := DefaultAppConfig()
cmtConfig := DefaultCmtConfig()
clientConfig := DefaultClientConfig()
generateAndWritePackedConfig(dCmd, appConfig, cmtConfig, clientConfig, false)
s.Require().NoError(loadPackedConfig(dCmd))
ctx := client.GetClientContextFromCmd(dCmd)
vpr := ctx.Viper
s.Require().NotPanics(func() {
appConfig2, err := serverconfig.GetConfig(vpr)
s.Require().NoError(err, "GetConfig")
s.Assert().Equal(*appConfig, appConfig2)
})
}
func (s *ConfigManagerTestSuite) TestPackedConfigCosmosLoadGlobalLabels() {
dCmd := s.makeDummyCmd()
appConfig := serverconfig.DefaultConfig()
appConfig.Telemetry.GlobalLabels = append(appConfig.Telemetry.GlobalLabels, []string{"key1", "value1"})
appConfig.Telemetry.GlobalLabels = append(appConfig.Telemetry.GlobalLabels, []string{"key2", "value2"})
cmtConfig := DefaultCmtConfig()
clientConfig := DefaultClientConfig()
generateAndWritePackedConfig(dCmd, appConfig, cmtConfig, clientConfig, false)
s.Require().NoError(loadPackedConfig(dCmd))
ctx := client.GetClientContextFromCmd(dCmd)
vpr := ctx.Viper
s.Require().NotPanics(func() {
appConfig2, err := serverconfig.GetConfig(vpr)
s.Require().NoError(err, "GetConfig")
s.Assert().Equal(appConfig.Telemetry.GlobalLabels, appConfig2.Telemetry.GlobalLabels)
}, "GetConfig")
}
func (s *ConfigManagerTestSuite) TestUnmanagedConfig() {
s.T().Run("unmanaged config is read with no other config files", func(t *testing.T) {
dCmd := s.makeDummyCmd()
configDir := GetFullPathToConfigDir(dCmd)
uFile := GetFullPathToUnmanagedConf(dCmd)
require.NoError(t, os.MkdirAll(configDir, 0o755), "making config dir")
require.NoError(t, os.WriteFile(uFile, []byte("banana = \"bananas\"\n"), 0o644), "writing unmanaged config")
require.NoError(t, LoadConfigFromFiles(dCmd))
ctx := client.GetClientContextFromCmd(dCmd)
vpr := ctx.Viper
actual := vpr.GetString("banana")
assert.Equal(t, "bananas", actual, "unmanaged field value")
})
s.T().Run("unmanaged config entry overrides other config", func(t *testing.T) {
dCmd := s.makeDummyCmd()
configDir := GetFullPathToConfigDir(dCmd)
uFile := GetFullPathToUnmanagedConf(dCmd)
require.NoError(t, os.MkdirAll(configDir, 0o755), "making config dir")
require.NoError(t, os.WriteFile(uFile, []byte("db_backend = \"still bananas\"\n"), 0o644), "writing unmanaged config")
require.NoError(t, LoadConfigFromFiles(dCmd))
ctx := client.GetClientContextFromCmd(dCmd)
vpr := ctx.Viper
actual := vpr.GetString("db_backend")
assert.Equal(t, "still bananas", actual, "unmanaged field value")
assert.NotEqual(t, DefaultCmtConfig().DBBackend, actual, "unmanaged field default value")
})
s.T().Run("unmanaged config is read with unpacked files", func(t *testing.T) {
dCmd := s.makeDummyCmd()
uFile := GetFullPathToUnmanagedConf(dCmd)
SaveConfigs(dCmd, DefaultAppConfig(), DefaultCmtConfig(), DefaultClientConfig(), false)
require.NoError(t, os.WriteFile(uFile, []byte("my-custom-entry = \"stuff\"\n"), 0o644), "writing unmanaged config")
require.NoError(t, LoadConfigFromFiles(dCmd))
ctx := client.GetClientContextFromCmd(dCmd)
vpr := ctx.Viper
actual := vpr.GetString("my-custom-entry")
assert.Equal(t, "stuff", actual, "unmanaged field value")
})
s.T().Run("unmanaged config is read with invalid packed files", func(t *testing.T) {
dCmd := s.makeDummyCmd()
uFile := GetFullPathToUnmanagedConf(dCmd)
pFile := GetFullPathToPackedConf(dCmd)
SaveConfigs(dCmd, DefaultAppConfig(), DefaultCmtConfig(), DefaultClientConfig(), false)
require.NoError(t, os.WriteFile(uFile, []byte("my-custom-entry = \"stuff\"\n"), 0o644), "writing unmanaged config")
require.NoError(t, os.WriteFile(pFile, []byte("kl234508923u5jl"), 0o644), "writing invalid data to packed config")
require.EqualError(t, LoadConfigFromFiles(dCmd), "packed config file parse error: invalid character 'k' looking for beginning of value", "should throw error with invalid packed config")
require.NoError(t, os.Remove(pFile), "removing packed config")
})
s.T().Run("unmanaged config is read with invalid unpacked files", func(t *testing.T) {
dCmd := s.makeDummyCmd()
uFile := GetFullPathToUnmanagedConf(dCmd)
pFile := GetFullPathToAppConf(dCmd)
SaveConfigs(dCmd, DefaultAppConfig(), DefaultCmtConfig(), DefaultClientConfig(), false)
require.NoError(t, os.WriteFile(uFile, []byte("my-custom-entry = \"stuff\"\n"), 0o644), "writing unmanaged config")
require.NoError(t, os.WriteFile(pFile, []byte("kl234508923u5jl"), 0o644), "writing invalid data to app config")
require.EqualError(t, LoadConfigFromFiles(dCmd), "app config file merge error: While parsing config: toml: expected = after a key, but the document ends there", "should throw error with invalid packed config")
})
s.T().Run("unmanaged config is read with packed config", func(t *testing.T) {
dCmd := s.makeDummyCmd()
uFile := GetFullPathToUnmanagedConf(dCmd)
SaveConfigs(dCmd, DefaultAppConfig(), DefaultCmtConfig(), DefaultClientConfig(), false)
require.NoError(t, PackConfig(dCmd), "packing config")
require.NoError(t, os.WriteFile(uFile, []byte("other-custom-entry = 8\n"), 0o644), "writing unmanaged config")
require.NoError(t, LoadConfigFromFiles(dCmd))
ctx := client.GetClientContextFromCmd(dCmd)
vpr := ctx.Viper
actual := vpr.GetInt("other-custom-entry")
assert.Equal(t, 8, actual, "unmanaged field value")
})
}
func (s *ConfigManagerTestSuite) TestServerGetConfigGlobalLabels() {
// This test exists because the telemetry.global-labels field used to be handled specially in
// serverconfig.GetConfig, so we had to have some work-around special handling for that field
// in the reflector. Now it exists to make sure it doesn't break again.
globalLabels := [][]string{
{"keya", "valuea"},
{"keyb", "valueb"},
{"keyc", "valuec"},
}
telemetry := map[string]interface{}{
"global-labels": globalLabels,
}
cfgMap := map[string]interface{}{
"telemetry": telemetry,
}
vpr := viper.New()
s.Require().NoError(vpr.MergeConfigMap(cfgMap), "MergeConfigMap")
cfg, err := serverconfig.GetConfig(vpr)
s.Require().NoError(err, "GetConfig")
actual := cfg.Telemetry.GlobalLabels
s.Assert().Equal(globalLabels, actual, "cfg.Telemetry.GlobalLabels")
}
func (s *ConfigManagerTestSuite) TestConfigMinGasPrices() {
configDir := GetFullPathToConfigDir(s.makeDummyCmd())
s.Require().NoError(os.MkdirAll(configDir, 0o755), "making config dir")
pioconfig.SetProvenanceConfig("manager", 42)
defaultMinGasPrices := pioconfig.GetProvenanceConfig().ProvenanceMinGasPrices
s.Require().NotEqual("", defaultMinGasPrices, "ProvenanceMinGasPrices")
s.Run("DefaultAppConfig has MinGasPrices", func() {
cfg := DefaultAppConfig()
actual := cfg.MinGasPrices
s.Assert().Equal(defaultMinGasPrices, actual, "MinGasPrices")
})
s.Run("no files", func() {
cmd := s.makeDummyCmd()
s.Require().NoError(LoadConfigFromFiles(cmd), "LoadConfigFromFiles")
cfg, err := ExtractAppConfig(cmd)
s.Require().NoError(err, "ExtractAppConfig")
actual := cfg.MinGasPrices
s.Assert().Equal(defaultMinGasPrices, actual, "MinGasPrices")
})
s.Run("cmt and client files but no app file", func() {
cmd1 := s.makeDummyCmd()
SaveConfigs(cmd1, nil, DefaultCmtConfig(), DefaultClientConfig(), false)
appCfgFile := GetFullPathToAppConf(cmd1)
_, err := os.Stat(appCfgFile)
fileExists := !os.IsNotExist(err)
s.Require().False(fileExists, "file exists: %s", appCfgFile)
cmd2 := s.makeDummyCmd()
s.Require().NoError(LoadConfigFromFiles(cmd2), "LoadConfigFromFiles")
cfg, err := ExtractAppConfig(cmd2)
s.Require().NoError(err, "ExtractAppConfig")
actual := cfg.MinGasPrices
s.Assert().Equal(defaultMinGasPrices, actual, "MinGasPrices")
})
s.Run("all files exist min gas price empty", func() {
cmd1 := s.makeDummyCmd()
appCfg := DefaultAppConfig()
appCfg.MinGasPrices = ""
SaveConfigs(cmd1, appCfg, DefaultCmtConfig(), DefaultClientConfig(), false)
appCfgFile := GetFullPathToAppConf(cmd1)
_, err := os.Stat(appCfgFile)
fileExists := !os.IsNotExist(err)
s.Require().True(fileExists, "file exists: %s", appCfgFile)
cmd2 := s.makeDummyCmd()
s.Require().NoError(LoadConfigFromFiles(cmd2), "LoadConfigFromFiles")
cfg, err := ExtractAppConfig(cmd2)
s.Require().NoError(err, "ExtractAppConfig")
actual := cfg.MinGasPrices
s.Assert().Equal("", actual, "MinGasPrices")
})
s.Run("all files exist min gas price something else", func() {
cmd1 := s.makeDummyCmd()
appCfg := DefaultAppConfig()
appCfg.MinGasPrices = "something else"
SaveConfigs(cmd1, appCfg, DefaultCmtConfig(), DefaultClientConfig(), false)
appCfgFile := GetFullPathToAppConf(cmd1)
_, err := os.Stat(appCfgFile)
fileExists := !os.IsNotExist(err)
s.Require().True(fileExists, "file exists: %s", appCfgFile)
cmd2 := s.makeDummyCmd()
s.Require().NoError(LoadConfigFromFiles(cmd2), "LoadConfigFromFiles")
cfg, err := ExtractAppConfig(cmd2)
s.Require().NoError(err, "ExtractAppConfig")
actual := cfg.MinGasPrices
s.Assert().Equal("something else", actual, "MinGasPrices")
})
s.Run("packed config without min-gas-prices", func() {
cmd1 := s.makeDummyCmd()
SaveConfigs(cmd1, DefaultAppConfig(), DefaultCmtConfig(), DefaultClientConfig(), false)
s.Require().NoError(PackConfig(cmd1), "PackConfig")
packedCfgFile := GetFullPathToPackedConf(cmd1)
_, err := os.Stat(packedCfgFile)
fileExists := !os.IsNotExist(err)
s.Require().True(fileExists, "file exists: %s", packedCfgFile)
// Just to be sure, rewrite the file as just "{}".
s.Require().NoError(os.WriteFile(packedCfgFile, []byte("{}"), 0o644), "writing packed config")
cmd2 := s.makeDummyCmd()
s.Require().NoError(LoadConfigFromFiles(cmd2), "LoadConfigFromFiles")
cfg, err := ExtractAppConfig(cmd2)
s.Require().NoError(err, "ExtractAppConfig")
actual := cfg.MinGasPrices
s.Assert().Equal(defaultMinGasPrices, actual)
})
s.Run("packed config with min-gas-prices", func() {
cmd1 := s.makeDummyCmd()
SaveConfigs(cmd1, DefaultAppConfig(), DefaultCmtConfig(), DefaultClientConfig(), false)
s.Require().NoError(PackConfig(cmd1), "PackConfig")
packedCfgFile := GetFullPathToPackedConf(cmd1)
_, err := os.Stat(packedCfgFile)
fileExists := !os.IsNotExist(err)
s.Require().True(fileExists, "file exists: %s", packedCfgFile)
// rewrite the packed file to include min-gas-prices
s.Require().NoError(os.WriteFile(packedCfgFile, []byte(`{"minimum-gas-prices":"65blue"}`), 0o644), "writing packed config")
cmd2 := s.makeDummyCmd()
s.Require().NoError(LoadConfigFromFiles(cmd2), "LoadConfigFromFiles")
cfg, err := ExtractAppConfig(cmd2)
s.Require().NoError(err, "ExtractAppConfig")
actual := cfg.MinGasPrices
s.Assert().Equal("65blue", actual)
})
}
func (s *ConfigManagerTestSuite) TestDefaultCmtConfig() {
cfg := DefaultCmtConfig()
s.Run("consensus.commit_timeout", func() {
exp := 1500 * time.Millisecond
act := cfg.Consensus.TimeoutCommit
s.Assert().Equal(exp, act, "cfg.Consensus.TimeoutCommit")
})
}
func (s *ConfigManagerTestSuite) TestPackedConfigCmtLoadDefaults() {
dCmd := s.makeDummyCmd()
dCmd.Flags().String("home", s.Home, "home dir")
appConfig := DefaultAppConfig()
cmtConfig := DefaultCmtConfig()
cmtConfig.SetRoot(s.Home)
clientConfig := DefaultClientConfig()
generateAndWritePackedConfig(dCmd, appConfig, cmtConfig, clientConfig, false)
s.logFile(GetFullPathToPackedConf(dCmd))
s.Require().NoError(loadPackedConfig(dCmd), "loadPackedConfig")
s.Run("cmtcmds.ParseConfig", func() {
var cmtConfig2 *cmtconfig.Config
var err error
s.Require().NotPanics(func() {
cmtConfig2, err = cmtcmds.ParseConfig(dCmd)
})
s.Require().NoError(err, "cmtcmds.ParseConfig")
s.Assert().Equal(cmtConfig, cmtConfig2)
})
s.Run("ExtractCmtConfig", func() {
var cmtConfig2 *cmtconfig.Config
var err error
s.Require().NotPanics(func() {
cmtConfig2, err = ExtractCmtConfig(dCmd)
})
s.Require().NoError(err, "ExtractCmtConfig")
s.Assert().Equal(cmtConfig, cmtConfig2)
})
}
func (s *ConfigManagerTestSuite) TestEntryUniqueness() {
// This test is basically a canary.
// In the config commands, we've taken advantage of the fact that no two config files have a field with the same name.
// If this test fails, it means that that is not the case anymore, and we'll need to make changes to accommodate.
// That'd be pretty bad for us (and probably the SDK) since everything gets loaded into viper which can only have one entry for a field.
dummyCmd := s.makeDummyCmd()
_, appMap, err := ExtractAppConfigAndMap(dummyCmd)
s.Require().NoError(err, "ExtractAppConfigAndMap")
_, cmtMap, err := ExtractCmtConfigAndMap(dummyCmd)
s.Require().NoError(err, "ExtractCmtConfigAndMap")
_, clientMap, err := ExtractClientConfigAndMap(dummyCmd)
s.Require().NoError(err, "ExtractClientConfigAndMap")
// key = field name, value = list of config type names that have that value
allMap := make(map[string][]string)
for k := range appMap {
allMap[k] = append(allMap[k], "app")
}
for k := range cmtMap {
allMap[k] = append(allMap[k], "cometbft")
}
for k := range clientMap {
allMap[k] = append(allMap[k], "client")
}
for field, configs := range allMap {
s.Assert().Len(configs, 1, "configs with field name = %q", field)
}
}