storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/disk-cache_test.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2018,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 cmd 18 19 import ( 20 "testing" 21 "time" 22 ) 23 24 // Tests ToObjectInfo function. 25 func TestCacheMetadataObjInfo(t *testing.T) { 26 m := cacheMeta{Meta: nil} 27 objInfo := m.ToObjectInfo("testbucket", "testobject") 28 if objInfo.Size != 0 { 29 t.Fatal("Unexpected object info value for Size", objInfo.Size) 30 } 31 if !objInfo.ModTime.Equal(time.Time{}) { 32 t.Fatal("Unexpected object info value for ModTime ", objInfo.ModTime) 33 } 34 if objInfo.IsDir { 35 t.Fatal("Unexpected object info value for IsDir", objInfo.IsDir) 36 } 37 if !objInfo.Expires.IsZero() { 38 t.Fatal("Unexpected object info value for Expires ", objInfo.Expires) 39 } 40 } 41 42 // test wildcard patterns for excluding entries from cache 43 func TestCacheExclusion(t *testing.T) { 44 cobjects := &cacheObjects{ 45 cache: nil, 46 } 47 48 testCases := []struct { 49 bucketName string 50 objectName string 51 excludePattern string 52 expectedResult bool 53 }{ 54 {"testbucket", "testobjectmatch", "testbucket/testobj*", true}, 55 {"testbucket", "testobjectnomatch", "testbucet/testobject*", false}, 56 {"testbucket", "testobject/pref1/obj1", "*/*", true}, 57 {"testbucket", "testobject/pref1/obj1", "*/pref1/*", true}, 58 {"testbucket", "testobject/pref1/obj1", "testobject/*", false}, 59 {"photos", "image1.jpg", "*.jpg", true}, 60 {"photos", "europe/paris/seine.jpg", "seine.jpg", false}, 61 {"photos", "europe/paris/seine.jpg", "*/seine.jpg", true}, 62 {"phil", "z/likes/coffee", "*/likes/*", true}, 63 {"failbucket", "no/slash/prefixes", "/failbucket/no/", false}, 64 {"failbucket", "no/slash/prefixes", "/failbucket/no/*", false}, 65 } 66 67 for i, testCase := range testCases { 68 cobjects.exclude = []string{testCase.excludePattern} 69 if cobjects.isCacheExclude(testCase.bucketName, testCase.objectName) != testCase.expectedResult { 70 t.Fatal("Cache exclusion test failed for case ", i) 71 } 72 } 73 }