From c6547b80854159185c9ae57e00d114c664d8c5fe Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 25 Jun 2020 12:29:42 -0400 Subject: [PATCH 1/2] store/types: Fix pruning opts validation --- store/types/pruning.go | 4 ++-- store/types/pruning_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 store/types/pruning_test.go diff --git a/store/types/pruning.go b/store/types/pruning.go index 88783925a7f5..10a730930557 100644 --- a/store/types/pruning.go +++ b/store/types/pruning.go @@ -47,10 +47,10 @@ 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) } diff --git a/store/types/pruning_test.go b/store/types/pruning_test.go new file mode 100644 index 000000000000..952274c9814f --- /dev/null +++ b/store/types/pruning_test.go @@ -0,0 +1,28 @@ +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) + require.Equal(t, tc.expectErr, po.Validate() != nil, po) + } +} From 80dcc2bbab2229907cd9a1ee6ca41efd4221cf09 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 25 Jun 2020 12:35:36 -0400 Subject: [PATCH 2/2] store/types: Add case --- store/types/pruning.go | 3 +++ store/types/pruning_test.go | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/store/types/pruning.go b/store/types/pruning.go index 10a730930557..8aa7059a1821 100644 --- a/store/types/pruning.go +++ b/store/types/pruning.go @@ -53,6 +53,9 @@ func (po PruningOptions) Validate() error { 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 } diff --git a/store/types/pruning_test.go b/store/types/pruning_test.go index 952274c9814f..2b88905ee8d2 100644 --- a/store/types/pruning_test.go +++ b/store/types/pruning_test.go @@ -23,6 +23,7 @@ func TestPruningOptions_Validate(t *testing.T) { for _, tc := range testCases { po := NewPruningOptions(tc.keepRecent, tc.keepEvery, tc.interval) - require.Equal(t, tc.expectErr, po.Validate() != nil, po) + err := po.Validate() + require.Equal(t, tc.expectErr, err != nil, "options: %v, err: %s", po, err) } }