Don't skip rules with ExpiredObjectDeleteMarker (#18256)

This commit is contained in:
Krishnan Parthasarathi 2023-10-16 22:46:46 -07:00 committed by GitHub
parent f91b257f50
commit 557df666fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 8 deletions

View File

@ -156,7 +156,8 @@ func (lc Lifecycle) HasActiveRules(prefix string) bool {
}
if len(prefix) > 0 && len(rule.GetPrefix()) > 0 {
// If recursive, we can skip this rule if it doesn't match the tested prefix.
// we can skip this rule if it doesn't match the tested
// prefix.
if !strings.HasPrefix(prefix, rule.GetPrefix()) && !strings.HasPrefix(rule.GetPrefix(), prefix) {
continue
}
@ -171,15 +172,15 @@ func (lc Lifecycle) HasActiveRules(prefix string) bool {
if !rule.NoncurrentVersionTransition.IsNull() {
return true
}
if rule.Expiration.IsNull() && rule.Transition.IsNull() {
continue
}
if !rule.Expiration.IsDateNull() && rule.Expiration.Date.Before(time.Now().UTC()) {
return true
}
if !rule.Expiration.IsDaysNull() {
return true
}
if rule.Expiration.DeleteMarker.val {
return true
}
if !rule.Transition.IsDateNull() && rule.Transition.Date.Before(time.Now().UTC()) {
return true
}

View File

@ -575,7 +575,7 @@ func TestHasActiveRules(t *testing.T) {
want: true,
},
{ // empty prefix
inputConfig: `<LifecycleConfiguration><Rule><Status>Enabled</Status><Expiration><Days>5</Days></Expiration></Rule></LifecycleConfiguration>`,
inputConfig: `<LifecycleConfiguration><Rule><Status>Enabled</Status><Filter></Filter><Expiration><Days>5</Days></Expiration></Rule></LifecycleConfiguration>`,
prefix: "foodir/foobject/foo.txt",
want: true,
},
@ -600,15 +600,25 @@ func TestHasActiveRules(t *testing.T) {
want: false,
},
{
inputConfig: `<LifecycleConfiguration><Rule><Status>Enabled</Status><Transition><StorageClass>S3TIER-1</StorageClass></Transition></Rule></LifecycleConfiguration>`,
inputConfig: `<LifecycleConfiguration><Rule><Status>Enabled</Status><Filter></Filter><Transition><StorageClass>S3TIER-1</StorageClass></Transition></Rule></LifecycleConfiguration>`,
prefix: "foodir/foobject/foo.txt",
want: true,
},
{
inputConfig: `<LifecycleConfiguration><Rule><Status>Enabled</Status><NoncurrentVersionTransition><StorageClass>S3TIER-1</StorageClass></NoncurrentVersionTransition></Rule></LifecycleConfiguration>`,
inputConfig: `<LifecycleConfiguration><Rule><Status>Enabled</Status><Filter></Filter><NoncurrentVersionTransition><StorageClass>S3TIER-1</StorageClass></NoncurrentVersionTransition></Rule></LifecycleConfiguration>`,
prefix: "foodir/foobject/foo.txt",
want: true,
},
{
inputConfig: `<LifecycleConfiguration><Rule><Status>Enabled</Status><Filter></Filter><Expiration><ExpiredObjectDeleteMarker>true</ExpiredObjectDeleteMarker></Expiration></Rule></LifecycleConfiguration>`,
prefix: "",
want: true,
},
{
inputConfig: `<LifecycleConfiguration><Rule><Status>Enabled</Status><Filter></Filter><Expiration><Days>42</Days><ExpiredObjectAllVersions>true</ExpiredObjectAllVersions></Expiration></Rule></LifecycleConfiguration>`,
prefix: "",
want: true,
},
}
for i, tc := range testCases {
@ -618,8 +628,12 @@ func TestHasActiveRules(t *testing.T) {
if err != nil {
t.Fatalf("Got unexpected error: %v", err)
}
// To ensure input lifecycle configurations are valid
if err := lc.Validate(); err != nil {
t.Fatalf("Invalid test case: %d %v", i+1, err)
}
if got := lc.HasActiveRules(tc.prefix); got != tc.want {
t.Fatalf("Expected result with recursive set to false: `%v`, got: `%v`", tc.want, got)
t.Fatalf("Expected result: `%v`, got: `%v`", tc.want, got)
}
})
}