store/types: Fix pruning opts validation (#6511)

* store/types: Fix pruning opts validation

* store/types: Add case
This commit is contained in:
Alexander Bezobchuk 2020-06-25 12:41:44 -04:00 committed by GitHub
parent 0d1a7293b7
commit 51c35f4dce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 2 deletions

View File

@ -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
}

View File

@ -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)
}
}