github.com/minio/madmin-go@v1.7.5/tier_test.go (about) 1 // 2 // MinIO Object Storage (c) 2021 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 madmin 18 19 import ( 20 "encoding/base64" 21 "fmt" 22 "log" 23 "reflect" 24 "testing" 25 ) 26 27 func ExampleNewTierS3() { 28 simpleS3SC, err := NewTierS3("simple-s3", "accessKey", "secretKey", "testbucket") 29 if err != nil { 30 log.Fatalln(err, "Failed to create s3 backed tier") 31 } 32 fmt.Println(simpleS3SC) 33 34 fullyCustomS3SC, err := NewTierS3("custom-s3", "accessKey", "secretKey", "testbucket", 35 S3Endpoint("https://s3.amazonaws.com"), S3Prefix("testprefix"), S3Region("us-west-1"), S3StorageClass("S3_IA")) 36 if err != nil { 37 log.Fatalln(err, "Failed to create s3 tier") 38 } 39 fmt.Println(fullyCustomS3SC) 40 } 41 42 func ExampleNewTierAzure() { 43 simpleAzSC, err := NewTierAzure("simple-az", "accessKey", "secretKey", "testbucket") 44 if err != nil { 45 log.Fatalln(err, "Failed to create azure backed tier") 46 } 47 fmt.Println(simpleAzSC) 48 49 fullyCustomAzSC, err := NewTierAzure("custom-az", "accessKey", "secretKey", "testbucket", AzureEndpoint("http://blob.core.windows.net"), AzurePrefix("testprefix")) 50 if err != nil { 51 log.Fatalln(err, "Failed to create azure backed tier") 52 } 53 fmt.Println(fullyCustomAzSC) 54 } 55 56 func ExampleNewTierGCS() { 57 credsJSON := []byte("credentials json content goes here") 58 simpleGCSSC, err := NewTierGCS("simple-gcs", credsJSON, "testbucket") 59 if err != nil { 60 log.Fatalln(err, "Failed to create GCS backed tier") 61 } 62 fmt.Println(simpleGCSSC) 63 64 fullyCustomGCSSC, err := NewTierGCS("custom-gcs", credsJSON, "testbucket", GCSPrefix("testprefix")) 65 if err != nil { 66 log.Fatalln(err, "Failed to create GCS backed tier") 67 } 68 fmt.Println(fullyCustomGCSSC) 69 } 70 71 // TestS3Tier tests S3Options helpers 72 func TestS3Tier(t *testing.T) { 73 scName := "test-s3" 74 endpoint := "https://mys3.com" 75 accessKey, secretKey := "accessKey", "secretKey" 76 bucket, prefix := "testbucket", "testprefix" 77 region := "us-west-1" 78 storageClass := "S3_IA" 79 80 want := &TierConfig{ 81 Version: TierConfigVer, 82 Type: S3, 83 Name: scName, 84 S3: &TierS3{ 85 AccessKey: accessKey, 86 SecretKey: secretKey, 87 Bucket: bucket, 88 89 // custom values 90 Endpoint: endpoint, 91 Prefix: prefix, 92 Region: region, 93 StorageClass: storageClass, 94 }, 95 } 96 options := []S3Options{ 97 S3Endpoint(endpoint), 98 S3Prefix(prefix), 99 S3Region(region), 100 S3StorageClass(storageClass), 101 } 102 got, err := NewTierS3(scName, accessKey, secretKey, bucket, options...) 103 if err != nil { 104 t.Fatalf("Failed to create a custom s3 tier %s", err) 105 } 106 107 if !reflect.DeepEqual(got, want) { 108 t.Fatalf("got != want, got = %v want = %v", *got, *want) 109 } 110 } 111 112 // TestAzTier tests AzureOptions helpers 113 func TestAzTier(t *testing.T) { 114 scName := "test-az" 115 endpoint := "https://myazure.com" 116 117 accountName, accountKey := "accountName", "accountKey" 118 bucket, prefix := "testbucket", "testprefix" 119 region := "us-east-1" 120 want := &TierConfig{ 121 Version: TierConfigVer, 122 Type: Azure, 123 Name: scName, 124 Azure: &TierAzure{ 125 AccountName: accountName, 126 AccountKey: accountKey, 127 Bucket: bucket, 128 129 // custom values 130 Endpoint: endpoint, 131 Prefix: prefix, 132 Region: region, 133 }, 134 } 135 136 options := []AzureOptions{ 137 AzureEndpoint(endpoint), 138 AzurePrefix(prefix), 139 AzureRegion(region), 140 } 141 142 got, err := NewTierAzure(scName, accountName, accountKey, bucket, options...) 143 if err != nil { 144 t.Fatalf("Failed to create a custom azure tier %s", err) 145 } 146 147 if !reflect.DeepEqual(got, want) { 148 t.Fatalf("got != want, got = %v want = %v", *got, *want) 149 } 150 } 151 152 // TestGCSStorageClass tests GCSOptions helpers 153 func TestGCSStorageClass(t *testing.T) { 154 scName := "test-gcs" 155 credsJSON := []byte("test-creds-json") 156 encodedCreds := base64.URLEncoding.EncodeToString(credsJSON) 157 bucket, prefix := "testbucket", "testprefix" 158 region := "us-west-2" 159 160 want := &TierConfig{ 161 Version: TierConfigVer, 162 Type: GCS, 163 Name: scName, 164 GCS: &TierGCS{ 165 Bucket: bucket, 166 Creds: encodedCreds, 167 168 // custom values 169 Endpoint: "https://storage.googleapis.com/", 170 Prefix: prefix, 171 Region: region, 172 }, 173 } 174 options := []GCSOptions{ 175 GCSRegion(region), 176 GCSPrefix(prefix), 177 } 178 got, err := NewTierGCS(scName, credsJSON, bucket, options...) 179 if err != nil { 180 t.Fatalf("Failed to create a custom gcs tier %s", err) 181 } 182 183 if !reflect.DeepEqual(got, want) { 184 t.Fatalf("got != want, got = %v want = %v", *got, *want) 185 } 186 }