Skip to content

Commit

Permalink
store/types: Fix pruning opts validation (#6511)
Browse files Browse the repository at this point in the history
* store/types: Fix pruning opts validation

* store/types: Add case
  • Loading branch information
alexanderbez authored Jun 25, 2020
1 parent 0d1a729 commit 51c35f4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
7 changes: 5 additions & 2 deletions store/types/pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
29 changes: 29 additions & 0 deletions store/types/pruning_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 51c35f4

Please sign in to comment.