diff --git a/pkg/bucket/lifecycle/lifecycle.go b/pkg/bucket/lifecycle/lifecycle.go index 2a53caa0a..ad74339b8 100644 --- a/pkg/bucket/lifecycle/lifecycle.go +++ b/pkg/bucket/lifecycle/lifecycle.go @@ -62,14 +62,19 @@ func (lc Lifecycle) HasActiveRules(prefix string, recursive bool) bool { if rule.Status == Disabled { continue } + if len(prefix) > 0 && len(rule.Filter.Prefix) > 0 { - // incoming prefix must be in rule prefix - if !recursive && !strings.HasPrefix(prefix, rule.Filter.Prefix) { - continue + if !recursive { + // If not recursive, incoming prefix must be in rule prefix + if !strings.HasPrefix(prefix, rule.Filter.Prefix) { + continue + } } - // If recursive, we can skip this rule if it doesn't match the tested prefix. - if recursive && !strings.HasPrefix(rule.Filter.Prefix, prefix) { - continue + if recursive { + // If recursive, we can skip this rule if it doesn't match the tested prefix. + if !strings.HasPrefix(prefix, rule.Filter.Prefix) && !strings.HasPrefix(rule.Filter.Prefix, prefix) { + continue + } } } diff --git a/pkg/bucket/lifecycle/lifecycle_test.go b/pkg/bucket/lifecycle/lifecycle_test.go index 10bc547fe..9bfea5509 100644 --- a/pkg/bucket/lifecycle/lifecycle_test.go +++ b/pkg/bucket/lifecycle/lifecycle_test.go @@ -387,3 +387,56 @@ func TestComputeActions(t *testing.T) { } } + +func TestHasActiveRules(t *testing.T) { + testCases := []struct { + inputConfig string + prefix string + expectedNonRec bool + expectedRec bool + }{ + { + inputConfig: `foodir/Enabled5`, + prefix: "foodir/foobject", + expectedNonRec: true, expectedRec: true, + }, + { + inputConfig: `foodir/Enabled5`, + prefix: "zdir/foobject", + expectedNonRec: false, expectedRec: false, + }, + { + inputConfig: `foodir/zdir/Enabled5`, + prefix: "foodir/", + expectedNonRec: false, expectedRec: true, + }, + { + inputConfig: `Disabled5`, + prefix: "foodir/", + expectedNonRec: false, expectedRec: false, + }, + { + inputConfig: `foodir/Enabled2999-01-01T00:00:00.000Z`, + prefix: "foodir/foobject", + expectedNonRec: false, expectedRec: false, + }, + } + + for i, tc := range testCases { + tc := tc + t.Run(fmt.Sprintf("Test_%d", i+1), func(t *testing.T) { + lc, err := ParseLifecycleConfig(bytes.NewReader([]byte(tc.inputConfig))) + if err != nil { + t.Fatalf("Got unexpected error: %v", err) + } + if got := lc.HasActiveRules(tc.prefix, false); got != tc.expectedNonRec { + t.Fatalf("Expected result with recursive set to false: `%v`, got: `%v`", tc.expectedNonRec, got) + } + if got := lc.HasActiveRules(tc.prefix, true); got != tc.expectedRec { + t.Fatalf("Expected result with recursive set to true: `%v`, got: `%v`", tc.expectedRec, got) + } + + }) + + } +}