github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/stat_test.go (about) 1 // Copyright (c) 2015-2022 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 "os" 22 "reflect" 23 "strings" 24 "testing" 25 "time" 26 ) 27 28 func TestParseStat(t *testing.T) { 29 localTime := time.Unix(12001, 0).UTC() 30 testCases := []struct { 31 content ClientContent 32 targetAlias string 33 }{ 34 {ClientContent{URL: *newClientURL("https://play.min.io/abc"), Size: 0, Time: localTime, Type: os.ModeDir, ETag: "blahblah", Metadata: map[string]string{"cusom-key": "custom-value"}, Expires: time.Now()}, "play"}, 35 {ClientContent{URL: *newClientURL("https://play.min.io/testbucket"), Size: 500, Time: localTime, Type: os.ModeDir, ETag: "blahblah", Metadata: map[string]string{"cusom-key": "custom-value"}, Expires: time.Unix(0, 0).UTC()}, "play"}, 36 {ClientContent{URL: *newClientURL("https://s3.amazonaws.com/yrdy"), Size: 0, Time: localTime, Type: 0o644, ETag: "abcdefasaas", Metadata: map[string]string{}}, "s3"}, 37 {ClientContent{URL: *newClientURL("https://play.min.io/yrdy"), Size: 10000, Time: localTime, Type: 0o644, ETag: "blahblah", Metadata: map[string]string{"cusom-key": "custom-value"}}, "play"}, 38 } 39 for _, testCase := range testCases { 40 testCase := testCase 41 t.Run("", func(t *testing.T) { 42 statMsg := parseStat(&testCase.content) 43 if !reflect.DeepEqual(testCase.content.Metadata, statMsg.Metadata) { 44 t.Errorf("Expecting %s, got %s", testCase.content.Metadata, statMsg.Metadata) 45 } 46 if testCase.content.Size != statMsg.Size { 47 t.Errorf("Expecting %d, got %d", testCase.content.Size, statMsg.Size) 48 } 49 if statMsg.Expires != nil { 50 if testCase.content.Expires != *statMsg.Expires { 51 t.Errorf("Expecting %s, got %s", testCase.content.Expires, statMsg.Expires) 52 } 53 } 54 if testCase.content.Type.IsRegular() { 55 if statMsg.Type != "file" { 56 t.Errorf("Expecting file, got %s", statMsg.Type) 57 } 58 } else { 59 if statMsg.Type != "folder" { 60 t.Errorf("Expecting folder, got %s", statMsg.Type) 61 } 62 } 63 etag := strings.TrimPrefix(testCase.content.ETag, "\"") 64 etag = strings.TrimSuffix(etag, "\"") 65 if etag != statMsg.ETag { 66 t.Errorf("Expecting %s, got %s", etag, statMsg.ETag) 67 } 68 }) 69 } 70 }