github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/cmd/batch-job-common-types_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 cmd
    19  
    20  import (
    21  	"fmt"
    22  	"testing"
    23  )
    24  
    25  func TestBatchJobSizeInRange(t *testing.T) {
    26  	tests := []struct {
    27  		objSize    int64
    28  		sizeFilter BatchJobSizeFilter
    29  		want       bool
    30  	}{
    31  		{
    32  			// 1Mib < 2Mib < 10MiB -> in range
    33  			objSize: 2 << 20,
    34  			sizeFilter: BatchJobSizeFilter{
    35  				UpperBound: 10 << 20,
    36  				LowerBound: 1 << 20,
    37  			},
    38  			want: true,
    39  		},
    40  		{
    41  			// 2KiB < 1 MiB -> out of range from left
    42  			objSize: 2 << 10,
    43  			sizeFilter: BatchJobSizeFilter{
    44  				UpperBound: 10 << 20,
    45  				LowerBound: 1 << 20,
    46  			},
    47  			want: false,
    48  		},
    49  		{
    50  			// 11MiB > 10 MiB -> out of range from right
    51  			objSize: 11 << 20,
    52  			sizeFilter: BatchJobSizeFilter{
    53  				UpperBound: 10 << 20,
    54  				LowerBound: 1 << 20,
    55  			},
    56  			want: false,
    57  		},
    58  		{
    59  			//  2MiB < 10MiB -> in range
    60  			objSize: 2 << 20,
    61  			sizeFilter: BatchJobSizeFilter{
    62  				UpperBound: 10 << 20,
    63  			},
    64  			want: true,
    65  		},
    66  		{
    67  			//  2MiB > 1MiB -> in range
    68  			objSize: 2 << 20,
    69  			sizeFilter: BatchJobSizeFilter{
    70  				LowerBound: 1 << 20,
    71  			},
    72  			want: true,
    73  		},
    74  	}
    75  
    76  	for i, test := range tests {
    77  		t.Run(fmt.Sprintf("test-%d", i+1), func(t *testing.T) {
    78  			if got := test.sizeFilter.InRange(test.objSize); got != test.want {
    79  				t.Fatalf("Expected %v but got %v", test.want, got)
    80  			}
    81  		})
    82  	}
    83  }
    84  
    85  func TestBatchJobSizeValidate(t *testing.T) {
    86  	errInvalidBatchJobSizeFilter := BatchJobYamlErr{
    87  		msg: "invalid batch-job size filter",
    88  	}
    89  
    90  	tests := []struct {
    91  		sizeFilter BatchJobSizeFilter
    92  		err        error
    93  	}{
    94  		{
    95  			// Unspecified size filter is a valid filter
    96  			sizeFilter: BatchJobSizeFilter{
    97  				UpperBound: 0,
    98  				LowerBound: 0,
    99  			},
   100  			err: nil,
   101  		},
   102  		{
   103  			sizeFilter: BatchJobSizeFilter{
   104  				UpperBound: 0,
   105  				LowerBound: 1 << 20,
   106  			},
   107  			err: nil,
   108  		},
   109  		{
   110  			sizeFilter: BatchJobSizeFilter{
   111  				UpperBound: 10 << 20,
   112  				LowerBound: 0,
   113  			},
   114  			err: nil,
   115  		},
   116  		{
   117  			// LowerBound > UpperBound -> empty range
   118  			sizeFilter: BatchJobSizeFilter{
   119  				UpperBound: 1 << 20,
   120  				LowerBound: 10 << 20,
   121  			},
   122  			err: errInvalidBatchJobSizeFilter,
   123  		},
   124  		{
   125  			// LowerBound == UpperBound -> empty range
   126  			sizeFilter: BatchJobSizeFilter{
   127  				UpperBound: 1 << 20,
   128  				LowerBound: 1 << 20,
   129  			},
   130  			err: errInvalidBatchJobSizeFilter,
   131  		},
   132  	}
   133  	for i, test := range tests {
   134  		t.Run(fmt.Sprintf("test-%d", i+1), func(t *testing.T) {
   135  			err := test.sizeFilter.Validate()
   136  			if err != nil {
   137  				gotErr := err.(BatchJobYamlErr)
   138  				testErr := test.err.(BatchJobYamlErr)
   139  				if gotErr.message() != testErr.message() {
   140  					t.Fatalf("Expected %v but got %v", test.err, err)
   141  				}
   142  			}
   143  			if err == nil && test.err != nil {
   144  				t.Fatalf("Expected %v but got nil", test.err)
   145  			}
   146  		})
   147  	}
   148  }