storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/config/cache/config_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2019 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package cache
    18  
    19  import (
    20  	"reflect"
    21  	"runtime"
    22  	"testing"
    23  )
    24  
    25  // Tests cache drive parsing.
    26  func TestParseCacheDrives(t *testing.T) {
    27  	testCases := []struct {
    28  		driveStr         string
    29  		expectedPatterns []string
    30  		success          bool
    31  	}{
    32  		// Invalid input
    33  
    34  		{"bucket1/*;*.png;images/trip/barcelona/*", []string{}, false},
    35  		{"bucket1", []string{}, false},
    36  		{";;;", []string{}, false},
    37  		{",;,;,;", []string{}, false},
    38  	}
    39  
    40  	// Valid inputs
    41  	if runtime.GOOS == "windows" {
    42  		testCases = append(testCases, struct {
    43  			driveStr         string
    44  			expectedPatterns []string
    45  			success          bool
    46  		}{"C:/home/drive1;C:/home/drive2;C:/home/drive3", []string{"C:/home/drive1", "C:/home/drive2", "C:/home/drive3"}, true})
    47  		testCases = append(testCases, struct {
    48  			driveStr         string
    49  			expectedPatterns []string
    50  			success          bool
    51  		}{"C:/home/drive{1...3}", []string{"C:/home/drive1", "C:/home/drive2", "C:/home/drive3"}, true})
    52  		testCases = append(testCases, struct {
    53  			driveStr         string
    54  			expectedPatterns []string
    55  			success          bool
    56  		}{"C:/home/drive{1..3}", []string{}, false})
    57  	} else {
    58  		testCases = append(testCases, struct {
    59  			driveStr         string
    60  			expectedPatterns []string
    61  			success          bool
    62  		}{"/home/drive1;/home/drive2;/home/drive3", []string{"/home/drive1", "/home/drive2", "/home/drive3"}, true})
    63  		testCases = append(testCases, struct {
    64  			driveStr         string
    65  			expectedPatterns []string
    66  			success          bool
    67  		}{"/home/drive1,/home/drive2,/home/drive3", []string{"/home/drive1", "/home/drive2", "/home/drive3"}, true})
    68  		testCases = append(testCases, struct {
    69  			driveStr         string
    70  			expectedPatterns []string
    71  			success          bool
    72  		}{"/home/drive{1...3}", []string{"/home/drive1", "/home/drive2", "/home/drive3"}, true})
    73  		testCases = append(testCases, struct {
    74  			driveStr         string
    75  			expectedPatterns []string
    76  			success          bool
    77  		}{"/home/drive{1..3}", []string{}, false})
    78  	}
    79  	for i, testCase := range testCases {
    80  		drives, err := parseCacheDrives(testCase.driveStr)
    81  		if err != nil && testCase.success {
    82  			t.Errorf("Test %d: Expected success but failed instead %s", i+1, err)
    83  		}
    84  		if err == nil && !testCase.success {
    85  			t.Errorf("Test %d: Expected failure but passed instead", i+1)
    86  		}
    87  		if err == nil {
    88  			if !reflect.DeepEqual(drives, testCase.expectedPatterns) {
    89  				t.Errorf("Test %d: Expected %v, got %v", i+1, testCase.expectedPatterns, drives)
    90  			}
    91  		}
    92  	}
    93  }
    94  
    95  // Tests cache exclude parsing.
    96  func TestParseCacheExclude(t *testing.T) {
    97  	testCases := []struct {
    98  		excludeStr       string
    99  		expectedPatterns []string
   100  		success          bool
   101  	}{
   102  		// Invalid input
   103  		{"/home/drive1;/home/drive2;/home/drive3", []string{}, false},
   104  		{"/", []string{}, false},
   105  		{";;;", []string{}, false},
   106  
   107  		// valid input
   108  		{"bucket1/*;*.png;images/trip/barcelona/*", []string{"bucket1/*", "*.png", "images/trip/barcelona/*"}, true},
   109  		{"bucket1/*,*.png,images/trip/barcelona/*", []string{"bucket1/*", "*.png", "images/trip/barcelona/*"}, true},
   110  		{"bucket1", []string{"bucket1"}, true},
   111  	}
   112  
   113  	for i, testCase := range testCases {
   114  		excludes, err := parseCacheExcludes(testCase.excludeStr)
   115  		if err != nil && testCase.success {
   116  			t.Errorf("Test %d: Expected success but failed instead %s", i+1, err)
   117  		}
   118  		if err == nil && !testCase.success {
   119  			t.Errorf("Test %d: Expected failure but passed instead", i+1)
   120  		}
   121  		if err == nil {
   122  			if !reflect.DeepEqual(excludes, testCase.expectedPatterns) {
   123  				t.Errorf("Test %d: Expected %v, got %v", i+1, testCase.expectedPatterns, excludes)
   124  			}
   125  		}
   126  	}
   127  }