github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/ilm/options_test.go (about)

     1  // Copyright (c) 2015-2023 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package ilm
    19  
    20  import (
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/dustin/go-humanize"
    25  	"github.com/minio/minio-go/v7/pkg/lifecycle"
    26  )
    27  
    28  func TestOptionFilter(t *testing.T) {
    29  	emptyFilter := lifecycle.Filter{}
    30  	emptyOpts := LifecycleOptions{}
    31  
    32  	filterWithPrefix := lifecycle.Filter{
    33  		Prefix: "doc/",
    34  	}
    35  	optsWithPrefix := LifecycleOptions{
    36  		Prefix: strPtr("doc/"),
    37  	}
    38  
    39  	filterWithTag := lifecycle.Filter{
    40  		Tag: lifecycle.Tag{
    41  			Key:   "key1",
    42  			Value: "value1",
    43  		},
    44  	}
    45  	optsWithTag := LifecycleOptions{
    46  		Tags: strPtr("key1=value1"),
    47  	}
    48  
    49  	filterWithSzLt := lifecycle.Filter{
    50  		ObjectSizeLessThan: 100 * humanize.MiByte,
    51  	}
    52  	optsWithSzLt := LifecycleOptions{
    53  		ObjectSizeLessThan: int64Ptr(100 * humanize.MiByte),
    54  	}
    55  
    56  	filterWithSzGt := lifecycle.Filter{
    57  		ObjectSizeGreaterThan: 1 * humanize.MiByte,
    58  	}
    59  	optsWithSzGt := LifecycleOptions{
    60  		ObjectSizeGreaterThan: int64Ptr(1 * humanize.MiByte),
    61  	}
    62  
    63  	filterWithAnd := lifecycle.Filter{
    64  		And: lifecycle.And{
    65  			Prefix: "doc/",
    66  			Tags: []lifecycle.Tag{
    67  				{
    68  					Key:   "key1",
    69  					Value: "value1",
    70  				},
    71  			},
    72  			ObjectSizeLessThan:    100 * humanize.MiByte,
    73  			ObjectSizeGreaterThan: 1 * humanize.MiByte,
    74  		},
    75  	}
    76  	optsWithAnd := LifecycleOptions{
    77  		Prefix:                strPtr("doc/"),
    78  		Tags:                  strPtr("key1=value1"),
    79  		ObjectSizeLessThan:    int64Ptr(100 * humanize.MiByte),
    80  		ObjectSizeGreaterThan: int64Ptr(1 * humanize.MiByte),
    81  	}
    82  
    83  	tests := []struct {
    84  		opts LifecycleOptions
    85  		want lifecycle.Filter
    86  	}{
    87  		{
    88  			opts: emptyOpts,
    89  			want: emptyFilter,
    90  		},
    91  		{
    92  			opts: optsWithPrefix,
    93  			want: filterWithPrefix,
    94  		},
    95  		{
    96  			opts: optsWithTag,
    97  			want: filterWithTag,
    98  		},
    99  		{
   100  			opts: optsWithSzGt,
   101  			want: filterWithSzGt,
   102  		},
   103  		{
   104  			opts: optsWithSzLt,
   105  			want: filterWithSzLt,
   106  		},
   107  		{
   108  			opts: optsWithAnd,
   109  			want: filterWithAnd,
   110  		},
   111  	}
   112  
   113  	filterEq := func(a, b lifecycle.Filter) bool {
   114  		if a.ObjectSizeGreaterThan != b.ObjectSizeGreaterThan {
   115  			return false
   116  		}
   117  		if a.ObjectSizeLessThan != b.ObjectSizeLessThan {
   118  			return false
   119  		}
   120  		if a.Prefix != b.Prefix {
   121  			return false
   122  		}
   123  		if a.Tag != b.Tag {
   124  			return false
   125  		}
   126  
   127  		if a.And.ObjectSizeGreaterThan != b.And.ObjectSizeGreaterThan {
   128  			return false
   129  		}
   130  		if a.And.ObjectSizeLessThan != b.And.ObjectSizeLessThan {
   131  			return false
   132  		}
   133  		if a.And.Prefix != b.And.Prefix {
   134  			return false
   135  		}
   136  		if len(a.And.Tags) != len(b.And.Tags) {
   137  			return false
   138  		}
   139  		for i := range a.And.Tags {
   140  			if a.And.Tags[i] != b.And.Tags[i] {
   141  				return false
   142  			}
   143  		}
   144  
   145  		return true
   146  	}
   147  
   148  	for i, test := range tests {
   149  		t.Run(fmt.Sprintf("Test %d", i+1), func(t *testing.T) {
   150  			if got := test.opts.Filter(); !filterEq(got, test.want) {
   151  				t.Fatalf("Expected %#v but got %#v", test.want, got)
   152  			}
   153  		})
   154  	}
   155  }