* s3api: extend lifecycle XML types with NoncurrentVersionExpiration, AbortIncompleteMultipartUpload Add missing S3 lifecycle rule types to the XML data model: - NoncurrentVersionExpiration with NoncurrentDays and NewerNoncurrentVersions - NoncurrentVersionTransition with NoncurrentDays and StorageClass - AbortIncompleteMultipartUpload with DaysAfterInitiation - Filter.ObjectSizeGreaterThan and ObjectSizeLessThan - And.ObjectSizeGreaterThan and ObjectSizeLessThan - Filter.UnmarshalXML to properly parse Tag, And, and size filter elements Each new type follows the existing set-field pattern for conditional XML marshaling. No behavior changes - these types are not yet wired into handlers or the lifecycle worker. * s3lifecycle: add lifecycle rule evaluator package New package weed/s3api/s3lifecycle/ provides a pure-function lifecycle rule evaluation engine. The evaluator accepts flattened Rule structs and ObjectInfo metadata, and returns the appropriate Action. Components: - evaluator.go: Evaluate() for per-object actions with S3 priority ordering (delete marker > noncurrent version > current expiration), ShouldExpireNoncurrentVersion() with NewerNoncurrentVersions support, EvaluateMPUAbort() for multipart upload rules - filter.go: prefix, tag, and size-based filter matching - tags.go: ExtractTags() extracts S3 tags from filer Extended metadata, HasTagRules() for scan-time optimization - version_time.go: GetVersionTimestamp() extracts timestamps from SeaweedFS version IDs (both old and new format) Comprehensive test coverage: 54 tests covering all action types, filter combinations, edge cases, and version ID formats. * s3api: add UnmarshalXML for Expiration, Transition, ExpireDeleteMarker Add UnmarshalXML methods that set the internal 'set' flag during XML parsing. Previously these flags were only set programmatically, causing XML round-trip to drop elements. This ensures lifecycle configurations stored as XML survive unmarshal/marshal cycles correctly. Add comprehensive XML round-trip tests for all lifecycle rule types including NoncurrentVersionExpiration, AbortIncompleteMultipartUpload, Filter with Tag/And/size constraints, and a complete Terraform-style lifecycle configuration. * s3lifecycle: address review feedback - Fix version_time.go overflow: guard timestampPart > MaxInt64 before the inversion subtraction to prevent uint64 wrap - Make all expiry checks inclusive (!now.Before instead of now.After) so actions trigger at the exact scheduled instant - Add NoncurrentIndex to ObjectInfo so Evaluate() can properly handle NewerNoncurrentVersions via ShouldExpireNoncurrentVersion() - Add test for high-bit overflow version ID * s3lifecycle: guard ShouldExpireNoncurrentVersion against zero SuccessorModTime Add early return when obj.IsLatest or obj.SuccessorModTime.IsZero() to prevent premature expiration of versions with uninitialized successor timestamps (zero value would compute to epoch, always expired). --------- Co-authored-by: Copilot <copilot@github.com>
232 lines
6.8 KiB
Go
232 lines
6.8 KiB
Go
package s3api
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestLifecycleXMLRoundTrip_NoncurrentVersionExpiration(t *testing.T) {
|
|
input := `<LifecycleConfiguration>
|
|
<Rule>
|
|
<ID>expire-noncurrent</ID>
|
|
<Status>Enabled</Status>
|
|
<Filter><Prefix></Prefix></Filter>
|
|
<NoncurrentVersionExpiration>
|
|
<NoncurrentDays>30</NoncurrentDays>
|
|
<NewerNoncurrentVersions>2</NewerNoncurrentVersions>
|
|
</NoncurrentVersionExpiration>
|
|
</Rule>
|
|
</LifecycleConfiguration>`
|
|
|
|
var lc Lifecycle
|
|
if err := xml.Unmarshal([]byte(input), &lc); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
|
|
if len(lc.Rules) != 1 {
|
|
t.Fatalf("expected 1 rule, got %d", len(lc.Rules))
|
|
}
|
|
rule := lc.Rules[0]
|
|
if rule.ID != "expire-noncurrent" {
|
|
t.Errorf("expected ID 'expire-noncurrent', got %q", rule.ID)
|
|
}
|
|
if rule.NoncurrentVersionExpiration.NoncurrentDays != 30 {
|
|
t.Errorf("expected NoncurrentDays=30, got %d", rule.NoncurrentVersionExpiration.NoncurrentDays)
|
|
}
|
|
if rule.NoncurrentVersionExpiration.NewerNoncurrentVersions != 2 {
|
|
t.Errorf("expected NewerNoncurrentVersions=2, got %d", rule.NoncurrentVersionExpiration.NewerNoncurrentVersions)
|
|
}
|
|
|
|
// Re-marshal and verify it round-trips.
|
|
out, err := xml.Marshal(lc)
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
s := string(out)
|
|
if !strings.Contains(s, "<NoncurrentDays>30</NoncurrentDays>") {
|
|
t.Errorf("marshaled XML missing NoncurrentDays: %s", s)
|
|
}
|
|
if !strings.Contains(s, "<NewerNoncurrentVersions>2</NewerNoncurrentVersions>") {
|
|
t.Errorf("marshaled XML missing NewerNoncurrentVersions: %s", s)
|
|
}
|
|
}
|
|
|
|
func TestLifecycleXMLRoundTrip_AbortIncompleteMultipartUpload(t *testing.T) {
|
|
input := `<LifecycleConfiguration>
|
|
<Rule>
|
|
<ID>abort-mpu</ID>
|
|
<Status>Enabled</Status>
|
|
<Filter><Prefix></Prefix></Filter>
|
|
<AbortIncompleteMultipartUpload>
|
|
<DaysAfterInitiation>7</DaysAfterInitiation>
|
|
</AbortIncompleteMultipartUpload>
|
|
</Rule>
|
|
</LifecycleConfiguration>`
|
|
|
|
var lc Lifecycle
|
|
if err := xml.Unmarshal([]byte(input), &lc); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
|
|
rule := lc.Rules[0]
|
|
if rule.AbortIncompleteMultipartUpload.DaysAfterInitiation != 7 {
|
|
t.Errorf("expected DaysAfterInitiation=7, got %d", rule.AbortIncompleteMultipartUpload.DaysAfterInitiation)
|
|
}
|
|
|
|
out, err := xml.Marshal(lc)
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
if !strings.Contains(string(out), "<DaysAfterInitiation>7</DaysAfterInitiation>") {
|
|
t.Errorf("marshaled XML missing DaysAfterInitiation: %s", string(out))
|
|
}
|
|
}
|
|
|
|
func TestLifecycleXMLRoundTrip_FilterWithTag(t *testing.T) {
|
|
input := `<LifecycleConfiguration>
|
|
<Rule>
|
|
<ID>tag-filter</ID>
|
|
<Status>Enabled</Status>
|
|
<Filter>
|
|
<Tag><Key>env</Key><Value>dev</Value></Tag>
|
|
</Filter>
|
|
<Expiration><Days>7</Days></Expiration>
|
|
</Rule>
|
|
</LifecycleConfiguration>`
|
|
|
|
var lc Lifecycle
|
|
if err := xml.Unmarshal([]byte(input), &lc); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
|
|
rule := lc.Rules[0]
|
|
if !rule.Filter.tagSet {
|
|
t.Error("expected Filter.tagSet to be true")
|
|
}
|
|
if rule.Filter.Tag.Key != "env" || rule.Filter.Tag.Value != "dev" {
|
|
t.Errorf("expected Tag{env:dev}, got Tag{%s:%s}", rule.Filter.Tag.Key, rule.Filter.Tag.Value)
|
|
}
|
|
}
|
|
|
|
func TestLifecycleXMLRoundTrip_FilterWithAnd(t *testing.T) {
|
|
input := `<LifecycleConfiguration>
|
|
<Rule>
|
|
<ID>and-filter</ID>
|
|
<Status>Enabled</Status>
|
|
<Filter>
|
|
<And>
|
|
<Prefix>logs/</Prefix>
|
|
<Tag><Key>env</Key><Value>dev</Value></Tag>
|
|
<Tag><Key>tier</Key><Value>hot</Value></Tag>
|
|
<ObjectSizeGreaterThan>1024</ObjectSizeGreaterThan>
|
|
<ObjectSizeLessThan>1048576</ObjectSizeLessThan>
|
|
</And>
|
|
</Filter>
|
|
<Expiration><Days>7</Days></Expiration>
|
|
</Rule>
|
|
</LifecycleConfiguration>`
|
|
|
|
var lc Lifecycle
|
|
if err := xml.Unmarshal([]byte(input), &lc); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
|
|
rule := lc.Rules[0]
|
|
if !rule.Filter.andSet {
|
|
t.Error("expected Filter.andSet to be true")
|
|
}
|
|
if rule.Filter.And.Prefix.String() != "logs/" {
|
|
t.Errorf("expected And.Prefix='logs/', got %q", rule.Filter.And.Prefix.String())
|
|
}
|
|
if len(rule.Filter.And.Tags) != 2 {
|
|
t.Fatalf("expected 2 And tags, got %d", len(rule.Filter.And.Tags))
|
|
}
|
|
if rule.Filter.And.ObjectSizeGreaterThan != 1024 {
|
|
t.Errorf("expected ObjectSizeGreaterThan=1024, got %d", rule.Filter.And.ObjectSizeGreaterThan)
|
|
}
|
|
if rule.Filter.And.ObjectSizeLessThan != 1048576 {
|
|
t.Errorf("expected ObjectSizeLessThan=1048576, got %d", rule.Filter.And.ObjectSizeLessThan)
|
|
}
|
|
}
|
|
|
|
func TestLifecycleXMLRoundTrip_FilterWithSizeOnly(t *testing.T) {
|
|
input := `<LifecycleConfiguration>
|
|
<Rule>
|
|
<ID>size-filter</ID>
|
|
<Status>Enabled</Status>
|
|
<Filter>
|
|
<ObjectSizeGreaterThan>512</ObjectSizeGreaterThan>
|
|
<ObjectSizeLessThan>10485760</ObjectSizeLessThan>
|
|
</Filter>
|
|
<Expiration><Days>30</Days></Expiration>
|
|
</Rule>
|
|
</LifecycleConfiguration>`
|
|
|
|
var lc Lifecycle
|
|
if err := xml.Unmarshal([]byte(input), &lc); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
|
|
rule := lc.Rules[0]
|
|
if rule.Filter.ObjectSizeGreaterThan != 512 {
|
|
t.Errorf("expected ObjectSizeGreaterThan=512, got %d", rule.Filter.ObjectSizeGreaterThan)
|
|
}
|
|
if rule.Filter.ObjectSizeLessThan != 10485760 {
|
|
t.Errorf("expected ObjectSizeLessThan=10485760, got %d", rule.Filter.ObjectSizeLessThan)
|
|
}
|
|
}
|
|
|
|
func TestLifecycleXMLRoundTrip_CompleteRule(t *testing.T) {
|
|
// A complete lifecycle config similar to what Terraform sends.
|
|
input := `<LifecycleConfiguration>
|
|
<Rule>
|
|
<ID>rotation</ID>
|
|
<Filter><Prefix></Prefix></Filter>
|
|
<Status>Enabled</Status>
|
|
<Expiration><Days>30</Days></Expiration>
|
|
<NoncurrentVersionExpiration>
|
|
<NoncurrentDays>1</NoncurrentDays>
|
|
</NoncurrentVersionExpiration>
|
|
<AbortIncompleteMultipartUpload>
|
|
<DaysAfterInitiation>1</DaysAfterInitiation>
|
|
</AbortIncompleteMultipartUpload>
|
|
</Rule>
|
|
</LifecycleConfiguration>`
|
|
|
|
var lc Lifecycle
|
|
if err := xml.Unmarshal([]byte(input), &lc); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
|
|
rule := lc.Rules[0]
|
|
if rule.ID != "rotation" {
|
|
t.Errorf("expected ID 'rotation', got %q", rule.ID)
|
|
}
|
|
if rule.Expiration.Days != 30 {
|
|
t.Errorf("expected Expiration.Days=30, got %d", rule.Expiration.Days)
|
|
}
|
|
if rule.NoncurrentVersionExpiration.NoncurrentDays != 1 {
|
|
t.Errorf("expected NoncurrentDays=1, got %d", rule.NoncurrentVersionExpiration.NoncurrentDays)
|
|
}
|
|
if rule.AbortIncompleteMultipartUpload.DaysAfterInitiation != 1 {
|
|
t.Errorf("expected DaysAfterInitiation=1, got %d", rule.AbortIncompleteMultipartUpload.DaysAfterInitiation)
|
|
}
|
|
|
|
// Re-marshal and verify all fields survive.
|
|
out, err := xml.Marshal(lc)
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
s := string(out)
|
|
for _, expected := range []string{
|
|
"<Days>30</Days>",
|
|
"<NoncurrentDays>1</NoncurrentDays>",
|
|
"<DaysAfterInitiation>1</DaysAfterInitiation>",
|
|
} {
|
|
if !strings.Contains(s, expected) {
|
|
t.Errorf("marshaled XML missing %q: %s", expected, s)
|
|
}
|
|
}
|
|
}
|