mirror of
https://github.com/minio/minio.git
synced 2025-01-24 13:13:16 -05:00
Fix lifecycle rules not applied in some cases (#10072)
HasActiveRules was not behaving as expected, this commit fixes it and adds more unit tests.
This commit is contained in:
parent
4bfc50411c
commit
4a447a439a
@ -62,14 +62,19 @@ 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
|
||||||
continue
|
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 {
|
||||||
if recursive && !strings.HasPrefix(rule.Filter.Prefix, prefix) {
|
// If recursive, we can skip this rule if it doesn't match the tested prefix.
|
||||||
continue
|
if !strings.HasPrefix(prefix, rule.Filter.Prefix) && !strings.HasPrefix(rule.Filter.Prefix, prefix) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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…
x
Reference in New Issue
Block a user