snapshot of https://github.com/seaweedfs/seaweedfs.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
go: build dev binaries / cleanup (push) Successful in 4s
Codespell / Check for spelling errors (push) Failing after 3s
docker: build dev containers / build-rust-binaries (amd64, x86_64-unknown-linux-musl) (push) Failing after 3s
docker: build dev containers / build-dev-containers (push) Has been skipped
docker: build dev containers / build-rust-binaries (arm64, true, aarch64-unknown-linux-musl) (push) Failing after 3s
End to End / FUSE Mount (push) Failing after 2s
EC Integration Tests / EC Integration Tests (push) Failing after 30s
FUSE DLM Integration Tests / FUSE DLM Integration Tests (push) Failing after 3s
FUSE Volume Server Failover Tests / FUSE Volume Server Failover (push) Failing after 3s
FUSE Integration Tests / FUSE Integration Testing (push) Failing after 2s
FUSE P2P Peer Chunk Sharing Integration Tests / FUSE P2P Peer Chunk Sharing (push) Failing after 3s
go: build binary / Go Vet (push) Failing after 2s
go: build binary / Go Vet 32-bit (push) Failing after 3s
go: build binary / Build (push) Failing after 4s
go: build binary / Build cross-platform (freebsd/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (darwin/arm64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/arm64) (push) Failing after 3s
go: build binary / Test (push) Failing after 2s
go: build binary / Test 32-bit (push) Failing after 3s
helm: lint and test charts / lint-test (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (11) (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (17) (push) Failing after 3s
go: build dev binaries / build_dev_linux_windows (amd64, linux) (push) Failing after 2s
go: build dev binaries / build_dev_linux_windows (amd64, windows) (push) Failing after 3s
go: build dev binaries / build_dev_darwin (amd64, darwin) (push) Has been skipped
go: build dev binaries / build_dev_darwin (arm64, darwin) (push) Has been skipped
Java Client Unit Tests / Java Unit Tests (11) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (17) (push) Failing after 2s
Java Client Unit Tests / Java Unit Tests (21) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (8) (push) Failing after 3s
Kafka Quick Test (Load Test with Schema Registry) / Kafka Client Load Test (Quick) (push) Failing after 3s
Kafka Gateway Tests / Kafka Unit Tests (unit-tests-1) (push) Failing after 11s
Kafka Gateway Tests / Kafka Integration Tests (Critical) (integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka End-to-End Tests (with SMQ) (e2e-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Consumer Group Tests (Highly Isolated) (consumer-group-1) (push) Failing after 2s
Kafka Gateway Tests / Kafka Client Compatibility (with SMQ) (client-compat-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka SMQ Integration Tests (Full Stack) (smq-integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Protocol Tests (Isolated) (protocol-1) (push) Failing after 3s
KMS Tests / KMS Provider Integration Tests (push) Failing after 2s
KMS Tests / S3 KMS End-to-End Tests (push) Failing after 3s
Master Cold Start Tests / Master Cold Start Tests (push) Failing after 4s
Metadata Subscribe Integration Tests / Metadata Subscribe Integration Tests (push) Failing after 3s
Multi-Master Tests / Multi-Master Failover Tests (push) Failing after 3s
Performance / CPU and Heap Profile (push) Failing after 2s
Performance / S3 Read/Write Benchmark (rust volume) (push) Failing after 3s
pjdfstest POSIX Compliance / pjdfstest (push) Failing after 2s
Plugin Worker Integration Tests / Plugin Worker: erasure_coding (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: vacuum (push) Failing after 3s
Rust Volume Server Tests / Rust Integration Tests (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 1) (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 1) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 3) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Distributed Tests (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Performance Tests (push) Failing after 3s
Performance / Throughput Benchmark (go volume) (push) Failing after 3s
Performance / Throughput Benchmark (rust volume) (push) Failing after 2s
Performance / S3 Read/Write Benchmark (go volume) (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: volume_balance (push) Failing after 3s
PostgreSQL Gateway Tests / PostgreSQL Basic Tests (push) Failing after 3s
Rust Volume Server Tests / Rust Unit Tests (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 3) (push) Failing after 3s
Rust Plugin Worker Tests / Rust Plugin Worker Build and Unit Tests (push) Failing after 3s
rust: build dev volume server binaries / cleanup (push) Successful in 3s
S3 ETag and ACL Tests / S3 ETag + ACL Integration Tests (push) Failing after 3s
S3 IAM Integration Tests / IAM Unit Tests (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (advanced) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (basic) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (group) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (policy-enforcement) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (sts) (push) Failing after 4s
S3 Keycloak Integration Tests / S3 Keycloak Integration Tests (push) Failing after 3s
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.11) (push) Failing after 3s
S3 PyArrow Parquet Tests / Go Unit Tests (Implicit Directory) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.12) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-amd64, x86_64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-arm64, aarch64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-linux (linux-amd64, x86_64-unknown-linux-gnu) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.9) (push) Has been cancelled
S3 Proxy Signature Tests / S3 Proxy Signature Verification Tests (push) Has been cancelled
S3 SDK V2 Route Disambiguation Tests / S3 SDK V2 Routing Tests (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (quick) (push) Has been cancelled
S3 SSE Tests / S3 SSE Compatibility Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Metadata Persistence Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Copy Operations Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Multipart Upload Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Performance Test (push) Has been cancelled
S3 SSE Tests / S3 Volume Encryption Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (comprehensive) (push) Has been cancelled
Spark Integration Tests / Spark Integration Tests (push) Failing after 2s
Telemetry Integration Tests / telemetry-integration-test (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 1) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 1) (push) Failing after 3s
Ceph S3 tests / Basic S3 tests (KV store) (push) Has been cancelled
Ceph S3 tests / S3 Versioning & Object Lock tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Unit Tests (push) Failing after 3s
Ceph S3 tests / S3 CORS tests (push) Has been cancelled
Ceph S3 tests / SeaweedFS Custom S3 Copy tests (push) Has been cancelled
Ceph S3 tests / Basic S3 tests (SQL store) (push) Has been cancelled
test s3 over https using aws-cli / awscli-tests (push) Has been cancelled
TLS Rotation Integration Tests / TLS Rotation Integration Tests (push) Has been cancelled
Vacuum Integration Tests / Vacuum Integration Tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Variables Integration Tests (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (advanced-policy) (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (basic-policy) (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (contextual-policy) (push) Failing after 4s
S3 Policy Integration Tests / Trusted Proxy Detection Tests (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Shell Integration Tests (push) Failing after 3s
Samba on FUSE Integration / samba-integration (push) Failing after 2s
SFTP Integration Tests / SFTP Integration Testing (push) Failing after 3s
terraform: validate and test modules / fmt, validate, plan-level tests (push) Failing after 3s
terraform: validate and test modules / local cluster smoke test (real weed) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 3) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 2) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 2) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 3) (push) Failing after 5s
mount: windows conformance / WinFsp conformance (push) Has been cancelled
mount: windows / Mount on Windows (push) Has been cancelled

This commit is contained in:
upstream-mirror
2026-09-16 03:23:42 +05:00
commit 89f5be005d
3997 changed files with 961970 additions and 0 deletions

View File

@@ -0,0 +1,251 @@
package lifecycle_xml
import (
"bytes"
"encoding/xml"
"sort"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3lifecycle"
)
// Parse decodes a BucketLifecycleConfiguration XML body into the wire-form
// Lifecycle struct.
func Parse(xmlBytes []byte) (*Lifecycle, error) {
var lc Lifecycle
if err := xml.NewDecoder(bytes.NewReader(xmlBytes)).Decode(&lc); err != nil {
return nil, err
}
return &lc, nil
}
// ParseCanonical is the one-shot path most non-server callers want:
// raw XML in, []*s3lifecycle.Rule out.
func ParseCanonical(xmlBytes []byte) ([]*s3lifecycle.Rule, error) {
lc, err := Parse(xmlBytes)
if err != nil {
return nil, err
}
return LifecycleToCanonical(lc), nil
}
// LifecycleToCanonical flattens the XML-deserialized Lifecycle into the
// engine's flat Rule shape. The optional <Filter> element may contain
// <Prefix> | <Tag> | <And>, or be absent (in which case the older top-level
// <Prefix> applies).
func LifecycleToCanonical(lc *Lifecycle) []*s3lifecycle.Rule {
if lc == nil {
return nil
}
out := make([]*s3lifecycle.Rule, 0, len(lc.Rules))
for i := range lc.Rules {
out = append(out, ruleToCanonical(&lc.Rules[i]))
}
return out
}
func ruleToCanonical(r *Rule) *s3lifecycle.Rule {
out := &s3lifecycle.Rule{
ID: r.ID,
Status: string(r.Status),
}
prefix, tags, sizeGT, sizeLT := flattenFilter(&r.Filter)
if prefix == "" && r.Prefix.set {
prefix = r.Prefix.val
}
out.Prefix = prefix
if len(tags) > 0 {
out.FilterTags = tags
}
out.FilterSizeGreaterThan = sizeGT
out.FilterSizeLessThan = sizeLT
if r.Expiration.set {
out.ExpirationDays = r.Expiration.Days
if !r.Expiration.Date.Time.IsZero() {
out.ExpirationDate = r.Expiration.Date.Time
}
if r.Expiration.DeleteMarker.set {
out.ExpiredObjectDeleteMarker = r.Expiration.DeleteMarker.val
}
}
if r.NoncurrentVersionExpiration.set {
out.NoncurrentVersionExpirationDays = r.NoncurrentVersionExpiration.NoncurrentDays
out.NewerNoncurrentVersions = r.NoncurrentVersionExpiration.NewerNoncurrentVersions
}
if r.AbortIncompleteMultipartUpload.set {
out.AbortMPUDaysAfterInitiation = r.AbortIncompleteMultipartUpload.DaysAfterInitiation
}
return out
}
// CanonicalToLifecycle is the inverse of LifecycleToCanonical: it builds a
// marshalable Lifecycle from the engine's flat Rule shape. Only the fields
// s3lifecycle.Rule can represent are populated — there is no way back to
// Transition / NoncurrentVersionTransition, which the canonical form never
// carries.
func CanonicalToLifecycle(rules []*s3lifecycle.Rule) *Lifecycle {
lc := &Lifecycle{
Rules: make([]Rule, 0, len(rules)),
}
for _, r := range rules {
lc.Rules = append(lc.Rules, ruleFromCanonical(r))
}
return lc
}
// s3XMLNamespace is the namespace every S3 client stamps on a lifecycle
// document it PUTs. GetBucketLifecycleConfiguration replays the stored bytes
// verbatim, so a document written here has to carry it too or it would come
// back stripped of a namespace the client-written ones have.
const s3XMLNamespace = "http://s3.amazonaws.com/doc/2006-03-01/"
// MarshalCanonical serializes the canonical rules straight to a
// BucketLifecycleConfiguration XML document, mirroring ParseCanonical.
func MarshalCanonical(rules []*s3lifecycle.Rule) ([]byte, error) {
lc := CanonicalToLifecycle(rules)
out, err := xml.Marshal(struct {
Lifecycle
XMLNS string `xml:"xmlns,attr"`
}{Lifecycle: *lc, XMLNS: s3XMLNamespace})
if err != nil {
return nil, err
}
return append([]byte(xml.Header), out...), nil
}
func ruleFromCanonical(r *s3lifecycle.Rule) Rule {
out := Rule{
ID: r.ID,
Status: RuleStatus(r.Status),
Filter: filterFromCanonical(r.Prefix, r.FilterTags, r.FilterSizeGreaterThan, r.FilterSizeLessThan),
}
if r.ExpirationDays > 0 || !r.ExpirationDate.IsZero() || r.ExpiredObjectDeleteMarker {
out.Expiration = Expiration{
set: true,
Days: r.ExpirationDays,
}
if !r.ExpirationDate.IsZero() {
out.Expiration.Date = ExpirationDate{Time: r.ExpirationDate}
}
if r.ExpiredObjectDeleteMarker {
out.Expiration.DeleteMarker = ExpireDeleteMarker{val: true, set: true}
}
}
if r.NoncurrentVersionExpirationDays > 0 || r.NewerNoncurrentVersions > 0 {
out.NoncurrentVersionExpiration = NoncurrentVersionExpiration{
set: true,
NoncurrentDays: r.NoncurrentVersionExpirationDays,
NewerNoncurrentVersions: r.NewerNoncurrentVersions,
}
}
if r.AbortMPUDaysAfterInitiation > 0 {
out.AbortIncompleteMultipartUpload = AbortIncompleteMultipartUpload{
set: true,
DaysAfterInitiation: r.AbortMPUDaysAfterInitiation,
}
}
return out
}
// filterFromCanonical is the inverse of flattenFilter: it picks the
// narrowest Filter shape that represents the given prefix/tags/size bounds,
// matching what Filter.MarshalXML (single Prefix|Tag branch plus optional
// size bounds) and its And branch can each express.
func filterFromCanonical(prefix string, tags map[string]string, sizeGT, sizeLT int64) Filter {
hasSize := sizeGT > 0 || sizeLT > 0
// <Filter> holds exactly one predicate; anything more goes under <And>,
// two size bounds included, which is the form AWS documents for a range.
discriminants := 0
if prefix != "" {
discriminants++
}
discriminants += len(tags)
if sizeGT > 0 {
discriminants++
}
if sizeLT > 0 {
discriminants++
}
f := Filter{set: true, ObjectSizeGreaterThan: sizeGT, ObjectSizeLessThan: sizeLT}
switch {
case discriminants > 1:
f.andSet = true
f.And = And{
ObjectSizeGreaterThan: sizeGT,
ObjectSizeLessThan: sizeLT,
}
if prefix != "" {
f.And.Prefix = NewPrefix(prefix)
}
if len(tags) > 0 {
keys := make([]string, 0, len(tags))
for k := range tags {
keys = append(keys, k)
}
sort.Strings(keys)
f.And.Tags = make([]Tag, 0, len(keys))
for _, k := range keys {
f.And.Tags = append(f.And.Tags, Tag{Key: k, Value: tags[k]})
}
}
// The And branch carries its own size bounds; the enclosing
// Filter only emits them on the non-And path (see
// Filter.MarshalXML), so clear them here to avoid duplication.
f.ObjectSizeGreaterThan = 0
f.ObjectSizeLessThan = 0
case len(tags) == 1:
f.tagSet = true
for k, v := range tags {
f.Tag = Tag{Key: k, Value: v}
}
case hasSize:
// Single discriminant and it's a size range: the bounds set above
// already cover it — no <Prefix> (not even an empty one; nothing
// was requested) and no <And> (nothing else to combine with).
default:
// Either a single prefix or no discriminant at all (whole-bucket
// filter) — both are expressed as a <Prefix> element, empty or not.
f.Prefix = NewPrefix(prefix)
}
return f
}
func flattenFilter(f *Filter) (prefix string, tags map[string]string, sizeGT, sizeLT int64) {
if !f.set {
return
}
if f.andSet {
if f.And.Prefix.set {
prefix = f.And.Prefix.val
}
if len(f.And.Tags) > 0 {
tags = make(map[string]string, len(f.And.Tags))
for _, t := range f.And.Tags {
tags[t.Key] = t.Value
}
}
sizeGT = f.And.ObjectSizeGreaterThan
sizeLT = f.And.ObjectSizeLessThan
return
}
if f.tagSet {
tags = map[string]string{f.Tag.Key: f.Tag.Value}
} else if f.Prefix.set {
prefix = f.Prefix.val
}
sizeGT = f.ObjectSizeGreaterThan
sizeLT = f.ObjectSizeLessThan
return
}

View File

@@ -0,0 +1,195 @@
package lifecycle_xml
import (
"reflect"
"testing"
"time"
)
// parseLifecycle is a thin helper for tests; production code reads XML via
// the regular bucket-config decoder, this shortcut goes through the
// package's public Parse so the tests exercise the exported entrypoint.
func parseLifecycle(t *testing.T, xmlSrc string) *Lifecycle {
t.Helper()
lc, err := Parse([]byte(xmlSrc))
if err != nil {
t.Fatalf("parse: %v", err)
}
return lc
}
func TestLifecycleToCanonical_TopLevelPrefix(t *testing.T) {
lc := parseLifecycle(t, `
<LifecycleConfiguration>
<Rule>
<ID>r1</ID>
<Status>Enabled</Status>
<Prefix>logs/</Prefix>
<Expiration><Days>30</Days></Expiration>
</Rule>
</LifecycleConfiguration>`)
got := LifecycleToCanonical(lc)
if len(got) != 1 {
t.Fatalf("want 1 rule, got %d", len(got))
}
r := got[0]
if r.ID != "r1" || r.Status != "Enabled" || r.Prefix != "logs/" || r.ExpirationDays != 30 {
t.Fatalf("unexpected: %+v", r)
}
}
func TestLifecycleToCanonical_FilterPrefix(t *testing.T) {
lc := parseLifecycle(t, `
<LifecycleConfiguration>
<Rule>
<Status>Enabled</Status>
<Filter><Prefix>logs/</Prefix></Filter>
<Expiration><Days>30</Days></Expiration>
</Rule>
</LifecycleConfiguration>`)
got := LifecycleToCanonical(lc)
if got[0].Prefix != "logs/" {
t.Fatalf("want logs/, got %q", got[0].Prefix)
}
}
func TestLifecycleToCanonical_FilterTagAndSize(t *testing.T) {
lc := parseLifecycle(t, `
<LifecycleConfiguration>
<Rule>
<Status>Enabled</Status>
<Filter>
<And>
<Prefix>data/</Prefix>
<Tag><Key>env</Key><Value>prod</Value></Tag>
<Tag><Key>tier</Key><Value>cold</Value></Tag>
<ObjectSizeGreaterThan>1024</ObjectSizeGreaterThan>
<ObjectSizeLessThan>10485760</ObjectSizeLessThan>
</And>
</Filter>
<Expiration><Days>90</Days></Expiration>
</Rule>
</LifecycleConfiguration>`)
got := LifecycleToCanonical(lc)[0]
if got.Prefix != "data/" {
t.Fatalf("prefix want data/, got %q", got.Prefix)
}
wantTags := map[string]string{"env": "prod", "tier": "cold"}
if !reflect.DeepEqual(got.FilterTags, wantTags) {
t.Fatalf("tags want %v, got %v", wantTags, got.FilterTags)
}
if got.FilterSizeGreaterThan != 1024 || got.FilterSizeLessThan != 10485760 {
t.Fatalf("size: gt=%d lt=%d", got.FilterSizeGreaterThan, got.FilterSizeLessThan)
}
}
func TestLifecycleToCanonical_SingleTagFilter(t *testing.T) {
lc := parseLifecycle(t, `
<LifecycleConfiguration>
<Rule>
<Status>Enabled</Status>
<Filter>
<Tag><Key>env</Key><Value>prod</Value></Tag>
</Filter>
<Expiration><Days>30</Days></Expiration>
</Rule>
</LifecycleConfiguration>`)
got := LifecycleToCanonical(lc)[0]
if !reflect.DeepEqual(got.FilterTags, map[string]string{"env": "prod"}) {
t.Fatalf("tags want {env:prod}, got %v", got.FilterTags)
}
}
func TestLifecycleToCanonical_MultipleActions(t *testing.T) {
// One XML <Rule> with three concurrent actions: Expiration.Days,
// NoncurrentVersionExpiration, AbortMultipartUpload. All must populate
// the corresponding canonical fields so RuleActionKinds expands them
// into three compiled actions downstream.
lc := parseLifecycle(t, `
<LifecycleConfiguration>
<Rule>
<ID>multi</ID>
<Status>Enabled</Status>
<Filter><Prefix>data/</Prefix></Filter>
<Expiration><Days>90</Days></Expiration>
<NoncurrentVersionExpiration>
<NoncurrentDays>30</NoncurrentDays>
<NewerNoncurrentVersions>3</NewerNoncurrentVersions>
</NoncurrentVersionExpiration>
<AbortIncompleteMultipartUpload>
<DaysAfterInitiation>7</DaysAfterInitiation>
</AbortIncompleteMultipartUpload>
</Rule>
</LifecycleConfiguration>`)
got := LifecycleToCanonical(lc)[0]
if got.ExpirationDays != 90 {
t.Fatalf("ExpirationDays want 90, got %d", got.ExpirationDays)
}
if got.NoncurrentVersionExpirationDays != 30 || got.NewerNoncurrentVersions != 3 {
t.Fatalf("NoncurrentVersion: %+v", got)
}
if got.AbortMPUDaysAfterInitiation != 7 {
t.Fatalf("AbortMPU want 7, got %d", got.AbortMPUDaysAfterInitiation)
}
}
func TestLifecycleToCanonical_ExpirationDate(t *testing.T) {
lc := parseLifecycle(t, `
<LifecycleConfiguration>
<Rule>
<Status>Enabled</Status>
<Filter><Prefix></Prefix></Filter>
<Expiration><Date>2025-06-15T00:00:00Z</Date></Expiration>
</Rule>
</LifecycleConfiguration>`)
got := LifecycleToCanonical(lc)[0]
want, _ := time.Parse(time.RFC3339, "2025-06-15T00:00:00Z")
if !got.ExpirationDate.Equal(want) {
t.Fatalf("date want %v, got %v", want, got.ExpirationDate)
}
}
func TestLifecycleToCanonical_ExpiredObjectDeleteMarker(t *testing.T) {
lc := parseLifecycle(t, `
<LifecycleConfiguration>
<Rule>
<Status>Enabled</Status>
<Filter><Prefix></Prefix></Filter>
<Expiration><ExpiredObjectDeleteMarker>true</ExpiredObjectDeleteMarker></Expiration>
</Rule>
</LifecycleConfiguration>`)
got := LifecycleToCanonical(lc)[0]
if !got.ExpiredObjectDeleteMarker {
t.Fatalf("want ExpiredObjectDeleteMarker=true")
}
}
func TestLifecycleToCanonical_DisabledRulePreserved(t *testing.T) {
// The engine's mode gate decides scheduling; conversion must preserve
// the Status verbatim regardless.
lc := parseLifecycle(t, `
<LifecycleConfiguration>
<Rule>
<Status>Disabled</Status>
<Filter><Prefix>x/</Prefix></Filter>
<Expiration><Days>30</Days></Expiration>
</Rule>
</LifecycleConfiguration>`)
got := LifecycleToCanonical(lc)[0]
if got.Status != "Disabled" {
t.Fatalf("status want Disabled, got %q", got.Status)
}
}
func TestLifecycleToCanonical_NilSafe(t *testing.T) {
if got := LifecycleToCanonical(nil); got != nil {
t.Fatalf("nil lc should return nil, got %v", got)
}
}
func TestLifecycleToCanonical_EmptyRules(t *testing.T) {
got := LifecycleToCanonical(&Lifecycle{})
if len(got) != 0 {
t.Fatalf("empty rules should be empty slice, got %d", len(got))
}
}

View File

@@ -0,0 +1,599 @@
package lifecycle_xml
import (
"encoding/xml"
"strings"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3lifecycle"
)
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 TestLifecycleXML_TransitionSetFlag(t *testing.T) {
// Verify that Transition.Set() is true after unmarshaling.
input := `<LifecycleConfiguration>
<Rule>
<ID>transition</ID>
<Status>Enabled</Status>
<Filter><Prefix></Prefix></Filter>
<Transition>
<Days>30</Days>
<StorageClass>GLACIER</StorageClass>
</Transition>
</Rule>
</LifecycleConfiguration>`
var lc Lifecycle
if err := xml.Unmarshal([]byte(input), &lc); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if !lc.Rules[0].Transition.Set() {
t.Error("expected Transition.Set()=true after unmarshal")
}
}
func TestLifecycleXML_NoncurrentVersionTransitionSetFlag(t *testing.T) {
input := `<LifecycleConfiguration>
<Rule>
<ID>nv-transition</ID>
<Status>Enabled</Status>
<Filter><Prefix></Prefix></Filter>
<NoncurrentVersionTransition>
<NoncurrentDays>60</NoncurrentDays>
<StorageClass>GLACIER</StorageClass>
</NoncurrentVersionTransition>
</Rule>
</LifecycleConfiguration>`
var lc Lifecycle
if err := xml.Unmarshal([]byte(input), &lc); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if !lc.Rules[0].NoncurrentVersionTransition.Set() {
t.Error("expected NoncurrentVersionTransition.Set()=true after unmarshal")
}
}
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)
}
}
}
// assertCanonicalRoundTrip drives a canonical rule through
// CanonicalToLifecycle -> MarshalCanonical -> ParseCanonical and checks the
// result matches the input, proving the admin write path (which only ever
// has the canonical form) produces XML the S3 API can read back unchanged.
func assertCanonicalRoundTrip(t *testing.T, in *s3lifecycle.Rule) *s3lifecycle.Rule {
t.Helper()
xmlBytes, err := MarshalCanonical([]*s3lifecycle.Rule{in})
if err != nil {
t.Fatalf("MarshalCanonical: %v", err)
}
out, err := ParseCanonical(xmlBytes)
if err != nil {
t.Fatalf("ParseCanonical(%s): %v", xmlBytes, err)
}
if len(out) != 1 {
t.Fatalf("expected 1 rule after round trip, got %d: %s", len(out), xmlBytes)
}
return out[0]
}
func TestCanonicalRoundTrip_WholeBucket(t *testing.T) {
in := &s3lifecycle.Rule{ID: "whole-bucket", Status: s3lifecycle.StatusEnabled, ExpirationDays: 30}
out := assertCanonicalRoundTrip(t, in)
if out.Prefix != "" {
t.Errorf("expected empty prefix, got %q", out.Prefix)
}
if out.ExpirationDays != 30 {
t.Errorf("expected ExpirationDays=30, got %d", out.ExpirationDays)
}
}
func TestCanonicalRoundTrip_PrefixOnly(t *testing.T) {
in := &s3lifecycle.Rule{ID: "prefix-only", Status: s3lifecycle.StatusEnabled, Prefix: "logs/", ExpirationDays: 7}
out := assertCanonicalRoundTrip(t, in)
if out.Prefix != "logs/" {
t.Errorf("expected prefix 'logs/', got %q", out.Prefix)
}
if len(out.FilterTags) != 0 {
t.Errorf("expected no tags, got %v", out.FilterTags)
}
}
func TestCanonicalRoundTrip_TagOnly(t *testing.T) {
in := &s3lifecycle.Rule{
ID: "tag-only",
Status: s3lifecycle.StatusEnabled,
FilterTags: map[string]string{"env": "dev"},
ExpirationDays: 7,
}
out := assertCanonicalRoundTrip(t, in)
if out.Prefix != "" {
t.Errorf("expected empty prefix, got %q", out.Prefix)
}
if len(out.FilterTags) != 1 || out.FilterTags["env"] != "dev" {
t.Errorf("expected tags {env:dev}, got %v", out.FilterTags)
}
}
func TestCanonicalRoundTrip_PrefixAndTags(t *testing.T) {
in := &s3lifecycle.Rule{
ID: "prefix-and-tags",
Status: s3lifecycle.StatusEnabled,
Prefix: "logs/",
FilterTags: map[string]string{"env": "dev", "tier": "hot"},
ExpirationDays: 7,
}
out := assertCanonicalRoundTrip(t, in)
if out.Prefix != "logs/" {
t.Errorf("expected prefix 'logs/', got %q", out.Prefix)
}
if len(out.FilterTags) != 2 || out.FilterTags["env"] != "dev" || out.FilterTags["tier"] != "hot" {
t.Errorf("expected tags {env:dev, tier:hot}, got %v", out.FilterTags)
}
}
func TestCanonicalRoundTrip_SizeBounds(t *testing.T) {
in := &s3lifecycle.Rule{
ID: "size-bounds",
Status: s3lifecycle.StatusEnabled,
FilterSizeGreaterThan: 512,
FilterSizeLessThan: 1048576,
ExpirationDays: 30,
}
out := assertCanonicalRoundTrip(t, in)
if out.FilterSizeGreaterThan != 512 {
t.Errorf("expected FilterSizeGreaterThan=512, got %d", out.FilterSizeGreaterThan)
}
if out.FilterSizeLessThan != 1048576 {
t.Errorf("expected FilterSizeLessThan=1048576, got %d", out.FilterSizeLessThan)
}
}
func TestCanonicalRoundTrip_PrefixAndTagsWithSizeBounds(t *testing.T) {
// Multiple discriminants (prefix + tags) force the <And> branch, which
// carries its own size bounds distinct from the single-branch Filter.
in := &s3lifecycle.Rule{
ID: "and-with-size",
Status: s3lifecycle.StatusEnabled,
Prefix: "logs/",
FilterTags: map[string]string{"env": "dev"},
FilterSizeGreaterThan: 1024,
FilterSizeLessThan: 2048,
ExpirationDays: 7,
}
out := assertCanonicalRoundTrip(t, in)
if out.Prefix != "logs/" || len(out.FilterTags) != 1 {
t.Errorf("expected prefix 'logs/' and 1 tag, got prefix=%q tags=%v", out.Prefix, out.FilterTags)
}
if out.FilterSizeGreaterThan != 1024 || out.FilterSizeLessThan != 2048 {
t.Errorf("expected size bounds 1024/2048, got %d/%d", out.FilterSizeGreaterThan, out.FilterSizeLessThan)
}
}
func TestCanonicalRoundTrip_ExpirationDate(t *testing.T) {
date := time.Date(2030, 1, 15, 0, 0, 0, 0, time.UTC)
in := &s3lifecycle.Rule{ID: "expire-on-date", Status: s3lifecycle.StatusEnabled, ExpirationDate: date}
out := assertCanonicalRoundTrip(t, in)
if !out.ExpirationDate.Equal(date) {
t.Errorf("expected ExpirationDate=%v, got %v", date, out.ExpirationDate)
}
}
func TestCanonicalRoundTrip_ExpiredObjectDeleteMarker(t *testing.T) {
in := &s3lifecycle.Rule{ID: "delete-marker", Status: s3lifecycle.StatusEnabled, ExpiredObjectDeleteMarker: true}
out := assertCanonicalRoundTrip(t, in)
if !out.ExpiredObjectDeleteMarker {
t.Error("expected ExpiredObjectDeleteMarker=true")
}
}
func TestCanonicalRoundTrip_NoncurrentVersionExpiration(t *testing.T) {
in := &s3lifecycle.Rule{
ID: "noncurrent",
Status: s3lifecycle.StatusEnabled,
NoncurrentVersionExpirationDays: 30,
NewerNoncurrentVersions: 2,
}
out := assertCanonicalRoundTrip(t, in)
if out.NoncurrentVersionExpirationDays != 30 {
t.Errorf("expected NoncurrentVersionExpirationDays=30, got %d", out.NoncurrentVersionExpirationDays)
}
if out.NewerNoncurrentVersions != 2 {
t.Errorf("expected NewerNoncurrentVersions=2, got %d", out.NewerNoncurrentVersions)
}
}
func TestCanonicalRoundTrip_AbortMultipartUpload(t *testing.T) {
in := &s3lifecycle.Rule{ID: "abort-mpu", Status: s3lifecycle.StatusEnabled, AbortMPUDaysAfterInitiation: 7}
out := assertCanonicalRoundTrip(t, in)
if out.AbortMPUDaysAfterInitiation != 7 {
t.Errorf("expected AbortMPUDaysAfterInitiation=7, got %d", out.AbortMPUDaysAfterInitiation)
}
}
func TestCanonicalRoundTrip_Disabled(t *testing.T) {
in := &s3lifecycle.Rule{ID: "disabled-rule", Status: s3lifecycle.StatusDisabled, ExpirationDays: 30}
out := assertCanonicalRoundTrip(t, in)
if out.Status != s3lifecycle.StatusDisabled {
t.Errorf("expected Status=Disabled, got %q", out.Status)
}
}
func TestCanonicalRoundTrip_TagOrderIsStable(t *testing.T) {
// FilterTags is a map; And.Tags must be emitted in a deterministic
// (sorted) order so re-saving an unchanged rule doesn't churn the
// stored XML.
in := &s3lifecycle.Rule{
ID: "stable-order",
Status: s3lifecycle.StatusEnabled,
Prefix: "logs/",
FilterTags: map[string]string{"zeta": "1", "alpha": "2", "mu": "3"},
}
var firstXML []byte
for i := 0; i < 5; i++ {
xmlBytes, err := MarshalCanonical([]*s3lifecycle.Rule{in})
if err != nil {
t.Fatalf("MarshalCanonical: %v", err)
}
if i == 0 {
firstXML = xmlBytes
continue
}
if string(xmlBytes) != string(firstXML) {
t.Fatalf("marshal output is not stable across runs:\n%s\nvs\n%s", firstXML, xmlBytes)
}
}
if !strings.Contains(string(firstXML), "<Tag><Key>alpha</Key>") {
t.Errorf("expected tags sorted alphabetically (alpha first), got: %s", firstXML)
}
}
func TestCanonicalRoundTrip_PrefixWithSizeBoundsUsesAnd(t *testing.T) {
// A size range paired with a prefix is two different attributes, which
// requires <And> — unlike a size range alone (see
// TestCanonicalRoundTrip_SizeBounds), which doesn't need one.
in := &s3lifecycle.Rule{
ID: "prefix-with-size",
Status: s3lifecycle.StatusEnabled,
Prefix: "logs/",
FilterSizeGreaterThan: 1024,
ExpirationDays: 7,
}
xmlBytes, err := MarshalCanonical([]*s3lifecycle.Rule{in})
if err != nil {
t.Fatalf("MarshalCanonical: %v", err)
}
if !strings.Contains(string(xmlBytes), "<And>") {
t.Errorf("expected prefix+size to be wrapped in <And>, got: %s", xmlBytes)
}
out := assertCanonicalRoundTrip(t, in)
if out.Prefix != "logs/" {
t.Errorf("expected prefix 'logs/', got %q", out.Prefix)
}
if out.FilterSizeGreaterThan != 1024 {
t.Errorf("expected FilterSizeGreaterThan=1024, got %d", out.FilterSizeGreaterThan)
}
}
func TestCanonicalRoundTrip_TagWithSizeBoundsUsesAnd(t *testing.T) {
in := &s3lifecycle.Rule{
ID: "tag-with-size",
Status: s3lifecycle.StatusEnabled,
FilterTags: map[string]string{"env": "dev"},
FilterSizeLessThan: 2048,
ExpirationDays: 7,
}
xmlBytes, err := MarshalCanonical([]*s3lifecycle.Rule{in})
if err != nil {
t.Fatalf("MarshalCanonical: %v", err)
}
if !strings.Contains(string(xmlBytes), "<And>") {
t.Errorf("expected tag+size to be wrapped in <And>, got: %s", xmlBytes)
}
out := assertCanonicalRoundTrip(t, in)
if len(out.FilterTags) != 1 || out.FilterTags["env"] != "dev" {
t.Errorf("expected tags {env:dev}, got %v", out.FilterTags)
}
if out.FilterSizeLessThan != 2048 {
t.Errorf("expected FilterSizeLessThan=2048, got %d", out.FilterSizeLessThan)
}
}
func TestCanonicalRoundTrip_SizeOnlyOmitsPrefixElement(t *testing.T) {
// A size-only filter must not stamp a spurious empty <Prefix> — nothing
// was requested, so nothing besides the size bounds should appear.
in := &s3lifecycle.Rule{
ID: "size-only-no-prefix",
Status: s3lifecycle.StatusEnabled,
FilterSizeGreaterThan: 512,
ExpirationDays: 7,
}
xmlBytes, err := MarshalCanonical([]*s3lifecycle.Rule{in})
if err != nil {
t.Fatalf("MarshalCanonical: %v", err)
}
if strings.Contains(string(xmlBytes), "<Prefix>") {
t.Errorf("expected no <Prefix> element for a size-only filter, got: %s", xmlBytes)
}
out := assertCanonicalRoundTrip(t, in)
if out.Prefix != "" {
t.Errorf("expected empty prefix, got %q", out.Prefix)
}
if out.FilterSizeGreaterThan != 512 {
t.Errorf("expected FilterSizeGreaterThan=512, got %d", out.FilterSizeGreaterThan)
}
}
func TestCanonicalRoundTrip_SizeRangeUsesAnd(t *testing.T) {
// <Filter> holds one predicate; a range is two, so it belongs under <And>,
// which is the form AWS documents.
in := &s3lifecycle.Rule{
ID: "size-range",
Status: s3lifecycle.StatusEnabled,
FilterSizeGreaterThan: 512,
FilterSizeLessThan: 1048576,
ExpirationDays: 30,
}
xmlBytes, err := MarshalCanonical([]*s3lifecycle.Rule{in})
if err != nil {
t.Fatalf("MarshalCanonical: %v", err)
}
if !strings.Contains(string(xmlBytes), "<And><ObjectSizeGreaterThan>512</ObjectSizeGreaterThan><ObjectSizeLessThan>1048576</ObjectSizeLessThan></And>") {
t.Errorf("expected both size bounds under <And>, got: %s", xmlBytes)
}
out := assertCanonicalRoundTrip(t, in)
if out.FilterSizeGreaterThan != 512 || out.FilterSizeLessThan != 1048576 {
t.Errorf("expected the size range to survive the round trip, got %+v", out)
}
}
func TestMarshalCanonical_CarriesTheS3Namespace(t *testing.T) {
// GetBucketLifecycleConfiguration replays these bytes verbatim, so a
// document written here has to look like one a client PUT.
xmlBytes, err := MarshalCanonical([]*s3lifecycle.Rule{{
ID: "ns",
Status: s3lifecycle.StatusEnabled,
ExpirationDays: 1,
}})
if err != nil {
t.Fatalf("MarshalCanonical: %v", err)
}
if !strings.Contains(string(xmlBytes), `<LifecycleConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">`) {
t.Errorf("expected the S3 namespace on the root element, got: %s", xmlBytes)
}
if _, err := ParseCanonical(xmlBytes); err != nil {
t.Errorf("the namespaced document must still parse: %v", err)
}
}

View File

@@ -0,0 +1,400 @@
// Package lifecycle_xml is the XML wire-form for S3 BucketLifecycleConfiguration:
// the structs S3 PutBucketLifecycleConfiguration accepts and
// GetBucketLifecycleConfiguration returns. It lives outside weed/s3api so
// callers outside that package (shell, lifecycle worker) can parse and
// emit the same shape without pulling weed/s3api's full dependency graph
// — weed/s3api transitively imports weed/server, which imports weed/shell.
//
// Conversion to the engine-friendly canonical form
// (weed/s3api/s3lifecycle.Rule) lives next to the types here as
// LifecycleToCanonical.
package lifecycle_xml
import (
"encoding/xml"
"time"
)
// RuleStatus represents lifecycle rule status.
type RuleStatus string
// Supported status types.
const (
Enabled RuleStatus = "Enabled"
Disabled RuleStatus = "Disabled"
)
// Tag is the lifecycle filter tag pair. Defined locally so this package
// has no dependency on weed/s3api.
type Tag struct {
Key string `xml:"Key"`
Value string `xml:"Value"`
}
// Lifecycle - Configuration for bucket lifecycle.
type Lifecycle struct {
XMLName xml.Name `xml:"LifecycleConfiguration"`
Rules []Rule `xml:"Rule"`
}
// Rule - a rule for lifecycle configuration.
type Rule struct {
XMLName xml.Name `xml:"Rule"`
ID string `xml:"ID,omitempty"`
Status RuleStatus `xml:"Status"`
Filter Filter `xml:"Filter,omitempty"`
Prefix Prefix `xml:"Prefix,omitempty"`
Expiration Expiration `xml:"Expiration,omitempty"`
Transition Transition `xml:"Transition,omitempty"`
NoncurrentVersionExpiration NoncurrentVersionExpiration `xml:"NoncurrentVersionExpiration,omitempty"`
NoncurrentVersionTransition NoncurrentVersionTransition `xml:"NoncurrentVersionTransition,omitempty"`
AbortIncompleteMultipartUpload AbortIncompleteMultipartUpload `xml:"AbortIncompleteMultipartUpload,omitempty"`
}
// Filter - a filter for a lifecycle configuration Rule.
type Filter struct {
XMLName xml.Name `xml:"Filter"`
set bool
Prefix Prefix
And And
andSet bool
Tag Tag
tagSet bool
ObjectSizeGreaterThan int64
ObjectSizeLessThan int64
}
// Prefix holds the prefix xml tag in <Rule> and <Filter>
type Prefix struct {
XMLName xml.Name `xml:"Prefix"`
set bool
val string
}
func (p Prefix) String() string {
return p.val
}
// Set returns whether the Prefix carries a value (vs being absent).
func (p Prefix) Set() bool { return p.set }
// Val returns the prefix string. Empty when !Set.
func (p Prefix) Val() string { return p.val }
// NewPrefix builds a Prefix marked as set.
func NewPrefix(val string) Prefix { return Prefix{val: val, set: true} }
// NewExpirationDays builds an Expiration with Days set.
func NewExpirationDays(days int) Expiration { return Expiration{Days: days, set: true} }
// Set reports whether this Filter is present in the XML.
func (f Filter) Set() bool { return f.set }
// AndSet reports whether the Filter contains an <And> branch.
func (f Filter) AndSet() bool { return f.andSet }
// TagSet reports whether the Filter contains a single <Tag> branch.
func (f Filter) TagSet() bool { return f.tagSet }
// Set reports whether the Transition element was present.
func (t Transition) Set() bool { return t.set }
// Set reports whether the NoncurrentVersionTransition element was present.
func (n NoncurrentVersionTransition) Set() bool { return n.set }
// Set reports whether the Expiration element was present.
func (e Expiration) Set() bool { return e.set }
// MarshalXML encodes Prefix field into an XML form.
func (p Prefix) MarshalXML(e *xml.Encoder, startElement xml.StartElement) error {
if !p.set {
return nil
}
return e.EncodeElement(p.val, startElement)
}
func (p *Prefix) UnmarshalXML(d *xml.Decoder, startElement xml.StartElement) error {
prefix := ""
_ = d.DecodeElement(&prefix, &startElement)
*p = Prefix{set: true, val: prefix}
return nil
}
// MarshalXML encodes Filter field into an XML form.
func (f Filter) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if !f.set {
return nil
}
if err := e.EncodeToken(start); err != nil {
return err
}
if f.andSet {
if err := e.EncodeElement(f.And, xml.StartElement{Name: xml.Name{Local: "And"}}); err != nil {
return err
}
} else if f.tagSet {
if err := e.EncodeElement(f.Tag, xml.StartElement{Name: xml.Name{Local: "Tag"}}); err != nil {
return err
}
} else {
if err := e.EncodeElement(f.Prefix, xml.StartElement{Name: xml.Name{Local: "Prefix"}}); err != nil {
return err
}
}
if f.ObjectSizeGreaterThan > 0 {
if err := e.EncodeElement(f.ObjectSizeGreaterThan, xml.StartElement{Name: xml.Name{Local: "ObjectSizeGreaterThan"}}); err != nil {
return err
}
}
if f.ObjectSizeLessThan > 0 {
if err := e.EncodeElement(f.ObjectSizeLessThan, xml.StartElement{Name: xml.Name{Local: "ObjectSizeLessThan"}}); err != nil {
return err
}
}
return e.EncodeToken(xml.EndElement{Name: start.Name})
}
// UnmarshalXML decodes Filter from XML, handling all child elements.
func (f *Filter) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
f.set = true
for {
tok, err := d.Token()
if err != nil {
return err
}
switch t := tok.(type) {
case xml.StartElement:
switch t.Name.Local {
case "Prefix":
if err := d.DecodeElement(&f.Prefix, &t); err != nil {
return err
}
case "Tag":
f.tagSet = true
if err := d.DecodeElement(&f.Tag, &t); err != nil {
return err
}
case "And":
f.andSet = true
if err := d.DecodeElement(&f.And, &t); err != nil {
return err
}
case "ObjectSizeGreaterThan":
if err := d.DecodeElement(&f.ObjectSizeGreaterThan, &t); err != nil {
return err
}
case "ObjectSizeLessThan":
if err := d.DecodeElement(&f.ObjectSizeLessThan, &t); err != nil {
return err
}
default:
if err := d.Skip(); err != nil {
return err
}
}
case xml.EndElement:
return nil
}
}
}
// And - a tag to combine a prefix and multiple tags for lifecycle configuration rule.
type And struct {
XMLName xml.Name `xml:"And"`
Prefix Prefix `xml:"Prefix,omitempty"`
Tags []Tag `xml:"Tag,omitempty"`
ObjectSizeGreaterThan int64 `xml:"ObjectSizeGreaterThan,omitempty"`
ObjectSizeLessThan int64 `xml:"ObjectSizeLessThan,omitempty"`
}
// Expiration - expiration actions for a rule in lifecycle configuration.
type Expiration struct {
XMLName xml.Name `xml:"Expiration"`
Days int `xml:"Days,omitempty"`
Date ExpirationDate `xml:"Date,omitempty"`
DeleteMarker ExpireDeleteMarker `xml:"ExpiredObjectDeleteMarker"`
set bool
}
// MarshalXML encodes expiration field into an XML form.
func (e Expiration) MarshalXML(enc *xml.Encoder, startElement xml.StartElement) error {
if !e.set {
return nil
}
type expirationWrapper Expiration
return enc.EncodeElement(expirationWrapper(e), startElement)
}
func (e *Expiration) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type wrapper Expiration
var w wrapper
if err := d.DecodeElement(&w, &start); err != nil {
return err
}
*e = Expiration(w)
e.set = true
return nil
}
// ExpireDeleteMarker represents value of ExpiredObjectDeleteMarker field in Expiration XML element.
type ExpireDeleteMarker struct {
val bool
set bool
}
// MarshalXML encodes delete marker boolean into an XML form.
func (b ExpireDeleteMarker) MarshalXML(e *xml.Encoder, startElement xml.StartElement) error {
if !b.set {
return nil
}
return e.EncodeElement(b.val, startElement)
}
func (b *ExpireDeleteMarker) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v bool
if err := d.DecodeElement(&v, &start); err != nil {
return err
}
*b = ExpireDeleteMarker{val: v, set: true}
return nil
}
// ExpirationDate is a embedded type containing time.Time to unmarshal
// Date in Expiration
type ExpirationDate struct {
time.Time
}
// MarshalXML encodes expiration date if it is non-zero and encodes
// empty string otherwise
func (eDate ExpirationDate) MarshalXML(e *xml.Encoder, startElement xml.StartElement) error {
if eDate.Time.IsZero() {
return nil
}
return e.EncodeElement(eDate.Format(time.RFC3339), startElement)
}
// Transition - transition actions for a rule in lifecycle configuration.
type Transition struct {
XMLName xml.Name `xml:"Transition"`
Days int `xml:"Days,omitempty"`
Date time.Time `xml:"Date,omitempty"`
StorageClass string `xml:"StorageClass,omitempty"`
set bool
}
// MarshalXML encodes transition field into an XML form.
func (t Transition) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
if !t.set {
return nil
}
type transitionWrapper Transition
return enc.EncodeElement(transitionWrapper(t), start)
}
func (t *Transition) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type wrapper Transition
var w wrapper
if err := d.DecodeElement(&w, &start); err != nil {
return err
}
*t = Transition(w)
t.set = true
return nil
}
// TransitionDays is a type alias to unmarshal Days in Transition
type TransitionDays int
// NoncurrentVersionExpiration - expiration actions for non-current object versions.
type NoncurrentVersionExpiration struct {
XMLName xml.Name `xml:"NoncurrentVersionExpiration"`
NoncurrentDays int `xml:"NoncurrentDays,omitempty"`
NewerNoncurrentVersions int `xml:"NewerNoncurrentVersions,omitempty"`
set bool
}
// MarshalXML encodes NoncurrentVersionExpiration field into an XML form.
func (n NoncurrentVersionExpiration) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
if !n.set {
return nil
}
type wrapper NoncurrentVersionExpiration
return enc.EncodeElement(wrapper(n), start)
}
func (n *NoncurrentVersionExpiration) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type wrapper NoncurrentVersionExpiration
var w wrapper
if err := d.DecodeElement(&w, &start); err != nil {
return err
}
*n = NoncurrentVersionExpiration(w)
n.set = true
return nil
}
// NoncurrentVersionTransition - transition actions for non-current object versions.
type NoncurrentVersionTransition struct {
XMLName xml.Name `xml:"NoncurrentVersionTransition"`
NoncurrentDays int `xml:"NoncurrentDays,omitempty"`
StorageClass string `xml:"StorageClass,omitempty"`
set bool
}
// MarshalXML encodes NoncurrentVersionTransition field into an XML form.
func (n NoncurrentVersionTransition) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
if !n.set {
return nil
}
type wrapper NoncurrentVersionTransition
return enc.EncodeElement(wrapper(n), start)
}
func (n *NoncurrentVersionTransition) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type wrapper NoncurrentVersionTransition
var w wrapper
if err := d.DecodeElement(&w, &start); err != nil {
return err
}
*n = NoncurrentVersionTransition(w)
n.set = true
return nil
}
// AbortIncompleteMultipartUpload - abort action for incomplete multipart uploads.
type AbortIncompleteMultipartUpload struct {
XMLName xml.Name `xml:"AbortIncompleteMultipartUpload"`
DaysAfterInitiation int `xml:"DaysAfterInitiation,omitempty"`
set bool
}
// MarshalXML encodes AbortIncompleteMultipartUpload field into an XML form.
func (a AbortIncompleteMultipartUpload) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
if !a.set {
return nil
}
type wrapper AbortIncompleteMultipartUpload
return enc.EncodeElement(wrapper(a), start)
}
func (a *AbortIncompleteMultipartUpload) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type wrapper AbortIncompleteMultipartUpload
var w wrapper
if err := d.DecodeElement(&w, &start); err != nil {
return err
}
*a = AbortIncompleteMultipartUpload(w)
a.set = true
return nil
}