Fix lifecycle rules not applied in some cases (#10072)

HasActiveRules was not behaving as expected, this commit fixes it
and adds more unit tests.
master
Anis Elleuch 4 years ago committed by GitHub
parent 4bfc50411c
commit 4a447a439a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      pkg/bucket/lifecycle/lifecycle.go
  2. 53
      pkg/bucket/lifecycle/lifecycle_test.go

@ -62,16 +62,21 @@ func (lc Lifecycle) HasActiveRules(prefix string, recursive bool) bool {
if rule.Status == Disabled { if rule.Status == Disabled {
continue continue
} }
if len(prefix) > 0 && len(rule.Filter.Prefix) > 0 { if len(prefix) > 0 && len(rule.Filter.Prefix) > 0 {
// incoming prefix must be in rule prefix if !recursive {
if !recursive && !strings.HasPrefix(prefix, rule.Filter.Prefix) { // If not recursive, incoming prefix must be in rule prefix
if !strings.HasPrefix(prefix, rule.Filter.Prefix) {
continue continue
} }
}
if recursive {
// If recursive, we can skip this rule if it doesn't match the tested prefix. // If recursive, we can skip this rule if it doesn't match the tested prefix.
if recursive && !strings.HasPrefix(rule.Filter.Prefix, prefix) { if !strings.HasPrefix(prefix, rule.Filter.Prefix) && !strings.HasPrefix(rule.Filter.Prefix, prefix) {
continue continue
} }
} }
}
if rule.NoncurrentVersionExpiration.NoncurrentDays > 0 { if rule.NoncurrentVersionExpiration.NoncurrentDays > 0 {
return true return true

@ -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: `<LifecycleConfiguration><Rule><Filter><Prefix>foodir/</Prefix></Filter><Status>Enabled</Status><Expiration><Days>5</Days></Expiration></Rule></LifecycleConfiguration>`,
prefix: "foodir/foobject",
expectedNonRec: true, expectedRec: true,
},
{
inputConfig: `<LifecycleConfiguration><Rule><Filter><Prefix>foodir/</Prefix></Filter><Status>Enabled</Status><Expiration><Days>5</Days></Expiration></Rule></LifecycleConfiguration>`,
prefix: "zdir/foobject",
expectedNonRec: false, expectedRec: false,
},
{
inputConfig: `<LifecycleConfiguration><Rule><Filter><Prefix>foodir/zdir/</Prefix></Filter><Status>Enabled</Status><Expiration><Days>5</Days></Expiration></Rule></LifecycleConfiguration>`,
prefix: "foodir/",
expectedNonRec: false, expectedRec: true,
},
{
inputConfig: `<LifecycleConfiguration><Rule><Filter><Prefix></Prefix></Filter><Status>Disabled</Status><Expiration><Days>5</Days></Expiration></Rule></LifecycleConfiguration>`,
prefix: "foodir/",
expectedNonRec: false, expectedRec: false,
},
{
inputConfig: `<LifecycleConfiguration><Rule><Filter><Prefix>foodir/</Prefix></Filter><Status>Enabled</Status><Expiration><Date>2999-01-01T00:00:00.000Z</Date></Expiration></Rule></LifecycleConfiguration>`,
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)
}
})
}
}

Loading…
Cancel
Save