Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store/types: Fix pruning opts validation #6511

Merged
merged 2 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
require.Equal(t, tc.expectErr, err != nil, "options: %v, err: %s", po, err)
if tc.expectErr {
require.Error(t, err, po)
} else {
require.NoError(t, err, po)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whyyyyy

}
}