store/types: Fix pruning opts validation (#6511)
* store/types: Fix pruning opts validation * store/types: Add case
This commit is contained in:
parent
0d1a7293b7
commit
51c35f4dce
|
@ -47,12 +47,15 @@ func NewPruningOptions(keepRecent, keepEvery, interval uint64) PruningOptions {
|
|||
}
|
||||
|
||||
func (po PruningOptions) Validate() error {
|
||||
if po.KeepRecent == 0 && po.KeepEvery == 0 && po.Interval == 0 { // prune everything
|
||||
if po.KeepEvery == 0 && po.Interval == 0 {
|
||||
return fmt.Errorf("invalid 'Interval' when pruning everything: %d", po.Interval)
|
||||
}
|
||||
if po.KeepRecent == 0 && po.KeepEvery == 1 && po.Interval != 0 { // prune nothing
|
||||
if po.KeepEvery == 1 && po.Interval != 0 { // prune nothing
|
||||
return fmt.Errorf("invalid 'Interval' when pruning nothing: %d", po.Interval)
|
||||
}
|
||||
if po.KeepEvery > 1 && po.Interval == 0 {
|
||||
return fmt.Errorf("invalid 'Interval' when pruning: %d", po.Interval)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPruningOptions_Validate(t *testing.T) {
|
||||
testCases := []struct {
|
||||
keepRecent uint64
|
||||
keepEvery uint64
|
||||
interval uint64
|
||||
expectErr bool
|
||||
}{
|
||||
{100, 500, 10, false}, // default
|
||||
{0, 0, 10, false}, // everything
|
||||
{0, 1, 0, false}, // nothing
|
||||
{0, 10, 10, false},
|
||||
{100, 0, 0, true}, // invalid interval
|
||||
{0, 1, 5, true}, // invalid interval
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
po := NewPruningOptions(tc.keepRecent, tc.keepEvery, tc.interval)
|
||||
err := po.Validate()
|
||||
require.Equal(t, tc.expectErr, err != nil, "options: %v, err: %s", po, err)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue