github.com/minio/madmin-go/v2@v2.2.1/tier_test.go (about)

     1  //
     2  // Copyright (c) 2015-2022 MinIO, Inc.
     3  //
     4  // This file is part of MinIO Object Storage stack
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU Affero General Public License as
     8  // published by the Free Software Foundation, either version 3 of the
     9  // License, or (at your option) any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU Affero General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU Affero General Public License
    17  // along with this program. If not, see <http://www.gnu.org/licenses/>.
    18  //
    19  
    20  package madmin
    21  
    22  import (
    23  	"encoding/base64"
    24  	"fmt"
    25  	"log"
    26  	"reflect"
    27  	"testing"
    28  )
    29  
    30  func ExampleNewTierS3() {
    31  	simpleS3SC, err := NewTierS3("simple-s3", "accessKey", "secretKey", "testbucket")
    32  	if err != nil {
    33  		log.Fatalln(err, "Failed to create s3 backed tier")
    34  	}
    35  	fmt.Println(simpleS3SC)
    36  
    37  	fullyCustomS3SC, err := NewTierS3("custom-s3", "accessKey", "secretKey", "testbucket",
    38  		S3Endpoint("https://s3.amazonaws.com"), S3Prefix("testprefix"), S3Region("us-west-1"), S3StorageClass("S3_IA"))
    39  	if err != nil {
    40  		log.Fatalln(err, "Failed to create s3 tier")
    41  	}
    42  	fmt.Println(fullyCustomS3SC)
    43  }
    44  
    45  func ExampleNewTierAzure() {
    46  	simpleAzSC, err := NewTierAzure("simple-az", "accessKey", "secretKey", "testbucket")
    47  	if err != nil {
    48  		log.Fatalln(err, "Failed to create azure backed tier")
    49  	}
    50  	fmt.Println(simpleAzSC)
    51  
    52  	fullyCustomAzSC, err := NewTierAzure("custom-az", "accessKey", "secretKey", "testbucket", AzureEndpoint("http://blob.core.windows.net"), AzurePrefix("testprefix"))
    53  	if err != nil {
    54  		log.Fatalln(err, "Failed to create azure backed tier")
    55  	}
    56  	fmt.Println(fullyCustomAzSC)
    57  }
    58  
    59  func ExampleNewTierGCS() {
    60  	credsJSON := []byte("credentials json content goes here")
    61  	simpleGCSSC, err := NewTierGCS("simple-gcs", credsJSON, "testbucket")
    62  	if err != nil {
    63  		log.Fatalln(err, "Failed to create GCS backed tier")
    64  	}
    65  	fmt.Println(simpleGCSSC)
    66  
    67  	fullyCustomGCSSC, err := NewTierGCS("custom-gcs", credsJSON, "testbucket", GCSPrefix("testprefix"))
    68  	if err != nil {
    69  		log.Fatalln(err, "Failed to create GCS backed tier")
    70  	}
    71  	fmt.Println(fullyCustomGCSSC)
    72  }
    73  
    74  // TestS3Tier tests S3Options helpers
    75  func TestS3Tier(t *testing.T) {
    76  	scName := "test-s3"
    77  	endpoint := "https://mys3.com"
    78  	accessKey, secretKey := "accessKey", "secretKey"
    79  	bucket, prefix := "testbucket", "testprefix"
    80  	region := "us-west-1"
    81  	storageClass := "S3_IA"
    82  
    83  	want := &TierConfig{
    84  		Version: TierConfigVer,
    85  		Type:    S3,
    86  		Name:    scName,
    87  		S3: &TierS3{
    88  			AccessKey: accessKey,
    89  			SecretKey: secretKey,
    90  			Bucket:    bucket,
    91  
    92  			// custom values
    93  			Endpoint:     endpoint,
    94  			Prefix:       prefix,
    95  			Region:       region,
    96  			StorageClass: storageClass,
    97  		},
    98  	}
    99  	options := []S3Options{
   100  		S3Endpoint(endpoint),
   101  		S3Prefix(prefix),
   102  		S3Region(region),
   103  		S3StorageClass(storageClass),
   104  	}
   105  	got, err := NewTierS3(scName, accessKey, secretKey, bucket, options...)
   106  	if err != nil {
   107  		t.Fatalf("Failed to create a custom s3 tier %s", err)
   108  	}
   109  
   110  	if !reflect.DeepEqual(got, want) {
   111  		t.Fatalf("got != want, got = %v want = %v", *got, *want)
   112  	}
   113  }
   114  
   115  // TestAzTier tests AzureOptions helpers
   116  func TestAzTier(t *testing.T) {
   117  	scName := "test-az"
   118  	endpoint := "https://myazure.com"
   119  
   120  	accountName, accountKey := "accountName", "accountKey"
   121  	bucket, prefix := "testbucket", "testprefix"
   122  	region := "us-east-1"
   123  	want := &TierConfig{
   124  		Version: TierConfigVer,
   125  		Type:    Azure,
   126  		Name:    scName,
   127  		Azure: &TierAzure{
   128  			AccountName: accountName,
   129  			AccountKey:  accountKey,
   130  			Bucket:      bucket,
   131  
   132  			// custom values
   133  			Endpoint: endpoint,
   134  			Prefix:   prefix,
   135  			Region:   region,
   136  		},
   137  	}
   138  
   139  	options := []AzureOptions{
   140  		AzureEndpoint(endpoint),
   141  		AzurePrefix(prefix),
   142  		AzureRegion(region),
   143  	}
   144  
   145  	got, err := NewTierAzure(scName, accountName, accountKey, bucket, options...)
   146  	if err != nil {
   147  		t.Fatalf("Failed to create a custom azure tier %s", err)
   148  	}
   149  
   150  	if !reflect.DeepEqual(got, want) {
   151  		t.Fatalf("got != want, got = %v want = %v", *got, *want)
   152  	}
   153  }
   154  
   155  // TestGCSStorageClass tests GCSOptions helpers
   156  func TestGCSStorageClass(t *testing.T) {
   157  	scName := "test-gcs"
   158  	credsJSON := []byte("test-creds-json")
   159  	encodedCreds := base64.URLEncoding.EncodeToString(credsJSON)
   160  	bucket, prefix := "testbucket", "testprefix"
   161  	region := "us-west-2"
   162  
   163  	want := &TierConfig{
   164  		Version: TierConfigVer,
   165  		Type:    GCS,
   166  		Name:    scName,
   167  		GCS: &TierGCS{
   168  			Bucket: bucket,
   169  			Creds:  encodedCreds,
   170  
   171  			// custom values
   172  			Endpoint: "https://storage.googleapis.com/",
   173  			Prefix:   prefix,
   174  			Region:   region,
   175  		},
   176  	}
   177  	options := []GCSOptions{
   178  		GCSRegion(region),
   179  		GCSPrefix(prefix),
   180  	}
   181  	got, err := NewTierGCS(scName, credsJSON, bucket, options...)
   182  	if err != nil {
   183  		t.Fatalf("Failed to create a custom gcs tier %s", err)
   184  	}
   185  
   186  	if !reflect.DeepEqual(got, want) {
   187  		t.Fatalf("got != want, got = %v want = %v", *got, *want)
   188  	}
   189  }