From 8cd80fec8cd5807fda7e6c86911a77b3fae8b2c7 Mon Sep 17 00:00:00 2001 From: Krishnan Parthasarathi Date: Thu, 19 Oct 2023 21:33:28 -0700 Subject: [PATCH] Add unit test for lifecycle.FilterRules (#18284) --- internal/bucket/lifecycle/lifecycle_test.go | 60 +++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/internal/bucket/lifecycle/lifecycle_test.go b/internal/bucket/lifecycle/lifecycle_test.go index 9364d772f..b73bb8964 100644 --- a/internal/bucket/lifecycle/lifecycle_test.go +++ b/internal/bucket/lifecycle/lifecycle_test.go @@ -1016,3 +1016,63 @@ func TestFilterAndSetPredictionHeaders(t *testing.T) { }) } } + +func TestFilterRules(t *testing.T) { + lc := Lifecycle{ + Rules: []Rule{ + { + ID: "rule-1", + Status: "Enabled", + Filter: Filter{ + Tag: Tag{ + Key: "key1", + Value: "val1", + }, + }, + Expiration: Expiration{ + Days: 1, + }, + }, + }, + } + tests := []struct { + opts ObjectOpts + wantRule string + }{ + { // Delete marker should match filter without tags + opts: ObjectOpts{ + DeleteMarker: true, + IsLatest: true, + Name: "obj-1", + }, + wantRule: "rule-1", + }, + { // PUT version with no matching tags + opts: ObjectOpts{ + IsLatest: true, + Name: "obj-1", + }, + wantRule: "", + }, + { // PUT version with matching tags + opts: ObjectOpts{ + IsLatest: true, + UserTags: "key1=val1", + Name: "obj-1", + }, + wantRule: "rule-1", + }, + } + + for i, tc := range tests { + t.Run(fmt.Sprintf("test-%d", i+1), func(t *testing.T) { + rules := lc.FilterRules(tc.opts) + if tc.wantRule != "" && len(rules) == 0 { + t.Fatalf("%d: Expected rule match %s but none matched", i+1, tc.wantRule) + } + if tc.wantRule == "" && len(rules) > 0 { + t.Fatalf("%d: Expected no rules to match but got matches %v", i+1, rules) + } + }) + } +}