cosmos-sdk/server/start_test.go
Alessio Treglia e8cedf243f
fix all bits broken by viper API changes (#5982)
github.com/spf13/viper's recent releases introduced a semantic
change in some public API such as viper.IsSet(), which have
broken some of our flags checks. Instead of checking whether
users have changed a flag's default value we should rely on such
defaults and adjust runtime behaviour accordingly. In order to do
so, it's important that we pick sane defaults for all our flags.

The --pruning flag and configuration option now allow for a
fake custom strategy. When users elect custom, then the
pruning-{keep,snapshot}-every options are interpreted and
parsed; else they're ignored.
Zero is pruning-{keep,snapshot}-every default value. When
users choose to set a custom pruning strategy they are
signalling that they want more fine-grainted control, therefore
it's legitimate to expect them to know what they are doing and
enter valid values for both options.

Ref #5964
2020-04-14 17:24:27 +02:00

86 lines
1.8 KiB
Go

package server
import (
"fmt"
"testing"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)
func TestPruningOptions(t *testing.T) {
tests := []struct {
name string
paramInit func()
returnsErr bool
expectedErr error
}{
{
name: "default",
paramInit: func() {},
returnsErr: false,
expectedErr: nil,
},
{
name: "unknown strategy",
paramInit: func() { viper.Set(flagPruning, "unknown") },
returnsErr: true,
expectedErr: fmt.Errorf("unknown pruning strategy unknown"),
},
{
name: "only keep-every provided",
paramInit: func() {
viper.Set(flagPruning, "custom")
viper.Set(flagPruningKeepEvery, 12345)
},
returnsErr: false,
expectedErr: nil,
},
{
name: "only snapshot-every provided",
paramInit: func() {
viper.Set(flagPruning, "custom")
viper.Set(flagPruningSnapshotEvery, 12345)
},
returnsErr: true,
expectedErr: fmt.Errorf("invalid granular options"),
},
{
name: "pruning flag with other granular options 3",
paramInit: func() {
viper.Set(flagPruning, "custom")
viper.Set(flagPruningKeepEvery, 1234)
viper.Set(flagPruningSnapshotEvery, 1234)
},
returnsErr: false,
expectedErr: nil,
},
{
name: "nothing strategy",
paramInit: func() {
viper.Set(flagPruning, "nothing")
},
returnsErr: false,
expectedErr: nil,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
viper.Reset()
viper.SetDefault(flagPruning, "syncable")
startCommand := StartCmd(nil, nil)
tt.paramInit()
err := startCommand.PreRunE(startCommand, nil)
if tt.returnsErr {
require.EqualError(t, err, tt.expectedErr.Error())
} else {
require.NoError(t, err)
}
})
}
}