github.com/minio/console@v1.4.1/api/admin_tiers_test.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"fmt"
    23  	"testing"
    24  
    25  	tieringApi "github.com/minio/console/api/operations/tiering"
    26  	"github.com/minio/console/models"
    27  	"github.com/minio/madmin-go/v3"
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestGetTiers(t *testing.T) {
    32  	assert := assert.New(t)
    33  	// mock minIO client
    34  	adminClient := AdminClientMock{}
    35  
    36  	function := "getTiers()"
    37  	ctx, cancel := context.WithCancel(context.Background())
    38  	defer cancel()
    39  	// Test-1 : getBucketLifecycle() get list of tiers
    40  	// mock lifecycle response from MinIO
    41  	returnListMock := []*madmin.TierConfig{
    42  		{
    43  			Version: "V1",
    44  			Type:    madmin.TierType(0),
    45  			Name:    "S3 Tier",
    46  			S3: &madmin.TierS3{
    47  				Endpoint:     "https://s3tier.test.com/",
    48  				AccessKey:    "Access Key",
    49  				SecretKey:    "Secret Key",
    50  				Bucket:       "buckets3",
    51  				Prefix:       "pref1",
    52  				Region:       "us-west-1",
    53  				StorageClass: "TT1",
    54  			},
    55  		},
    56  	}
    57  
    58  	returnStatsMock := []madmin.TierInfo{
    59  		{
    60  			Name:  "STANDARD",
    61  			Type:  "internal",
    62  			Stats: madmin.TierStats{NumObjects: 2, NumVersions: 2, TotalSize: 228915},
    63  		},
    64  		{
    65  			Name:  "S3 Tier",
    66  			Type:  "s3",
    67  			Stats: madmin.TierStats{NumObjects: 0, NumVersions: 0, TotalSize: 0},
    68  		},
    69  	}
    70  
    71  	expectedOutput := &models.TierListResponse{
    72  		Items: []*models.Tier{
    73  			{
    74  				Type: "S3",
    75  				S3: &models.TierS3{
    76  					Accesskey:    "Access Key",
    77  					Secretkey:    "Secret Key",
    78  					Bucket:       "buckets3",
    79  					Endpoint:     "https://s3tier.test.com/",
    80  					Name:         "S3 Tier",
    81  					Prefix:       "pref1",
    82  					Region:       "us-west-1",
    83  					Storageclass: "TT1",
    84  					Usage:        "0 B",
    85  					Objects:      "0",
    86  					Versions:     "0",
    87  				},
    88  			},
    89  		},
    90  	}
    91  
    92  	minioListTiersMock = func(_ context.Context) ([]*madmin.TierConfig, error) {
    93  		return returnListMock, nil
    94  	}
    95  
    96  	minioTierStatsMock = func(_ context.Context) ([]madmin.TierInfo, error) {
    97  		return returnStatsMock, nil
    98  	}
    99  
   100  	tiersList, err := getTiers(ctx, adminClient)
   101  	if err != nil {
   102  		t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
   103  	}
   104  	// verify length of tiers list is correct
   105  	assert.Equal(len(tiersList.Items), len(returnListMock), fmt.Sprintf("Failed on %s: length of lists is not the same", function))
   106  	for i, conf := range returnListMock {
   107  		switch conf.Type {
   108  		case madmin.TierType(0):
   109  			// S3
   110  			assert.Equal(expectedOutput.Items[i].S3.Name, conf.Name)
   111  			assert.Equal(expectedOutput.Items[i].S3.Bucket, conf.S3.Bucket)
   112  			assert.Equal(expectedOutput.Items[i].S3.Prefix, conf.S3.Prefix)
   113  			assert.Equal(expectedOutput.Items[i].S3.Accesskey, conf.S3.AccessKey)
   114  			assert.Equal(expectedOutput.Items[i].S3.Secretkey, conf.S3.SecretKey)
   115  			assert.Equal(expectedOutput.Items[i].S3.Endpoint, conf.S3.Endpoint)
   116  			assert.Equal(expectedOutput.Items[i].S3.Region, conf.S3.Region)
   117  			assert.Equal(expectedOutput.Items[i].S3.Storageclass, conf.S3.StorageClass)
   118  		case madmin.TierType(1):
   119  			// Azure
   120  			assert.Equal(expectedOutput.Items[i].Azure.Name, conf.Name)
   121  			assert.Equal(expectedOutput.Items[i].Azure.Bucket, conf.Azure.Bucket)
   122  			assert.Equal(expectedOutput.Items[i].Azure.Prefix, conf.Azure.Prefix)
   123  			assert.Equal(expectedOutput.Items[i].Azure.Accountkey, conf.Azure.AccountKey)
   124  			assert.Equal(expectedOutput.Items[i].Azure.Accountname, conf.Azure.AccountName)
   125  			assert.Equal(expectedOutput.Items[i].Azure.Endpoint, conf.Azure.Endpoint)
   126  			assert.Equal(expectedOutput.Items[i].Azure.Region, conf.Azure.Region)
   127  		case madmin.TierType(2):
   128  			// GCS
   129  			assert.Equal(expectedOutput.Items[i].Gcs.Name, conf.Name)
   130  			assert.Equal(expectedOutput.Items[i].Gcs.Bucket, conf.GCS.Bucket)
   131  			assert.Equal(expectedOutput.Items[i].Gcs.Prefix, conf.GCS.Prefix)
   132  			assert.Equal(expectedOutput.Items[i].Gcs.Creds, conf.GCS.Creds)
   133  			assert.Equal(expectedOutput.Items[i].Gcs.Endpoint, conf.GCS.Endpoint)
   134  			assert.Equal(expectedOutput.Items[i].Gcs.Region, conf.GCS.Region)
   135  		}
   136  	}
   137  
   138  	// Test-2 : getBucketLifecycle() list is empty
   139  	returnListMockT2 := []*madmin.TierConfig{}
   140  
   141  	minioListTiersMock = func(_ context.Context) ([]*madmin.TierConfig, error) {
   142  		return returnListMockT2, nil
   143  	}
   144  
   145  	tiersListT2, err := getTiers(ctx, adminClient)
   146  	if err != nil {
   147  		t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
   148  	}
   149  
   150  	if len(tiersListT2.Items) != 0 {
   151  		t.Errorf("Failed on %s:, returned list was not empty", function)
   152  	}
   153  }
   154  
   155  func TestAddTier(t *testing.T) {
   156  	assert := assert.New(t)
   157  	// mock minIO client
   158  	adminClient := AdminClientMock{}
   159  
   160  	function := "addTier()"
   161  	ctx, cancel := context.WithCancel(context.Background())
   162  	defer cancel()
   163  	// Test-1: addTier() add new Tier
   164  	minioAddTiersMock = func(_ context.Context, _ *madmin.TierConfig) error {
   165  		return nil
   166  	}
   167  
   168  	paramsToAdd := tieringApi.AddTierParams{
   169  		Body: &models.Tier{
   170  			Type: "S3",
   171  			S3: &models.TierS3{
   172  				Accesskey:    "TestAK",
   173  				Bucket:       "bucket1",
   174  				Endpoint:     "https://test.com/",
   175  				Name:         "TIERS3",
   176  				Prefix:       "Pr1",
   177  				Region:       "us-west-1",
   178  				Secretkey:    "SecretK",
   179  				Storageclass: "STCLASS",
   180  			},
   181  		},
   182  	}
   183  
   184  	err := addTier(ctx, adminClient, &paramsToAdd)
   185  	assert.Equal(nil, err, fmt.Sprintf("Failed on %s: Error returned", function))
   186  
   187  	// Test-2: addTier() error adding Tier
   188  	minioAddTiersMock = func(_ context.Context, _ *madmin.TierConfig) error {
   189  		return errors.New("error setting new tier")
   190  	}
   191  
   192  	err2 := addTier(ctx, adminClient, &paramsToAdd)
   193  
   194  	assert.Equal(errors.New("error setting new tier"), err2, fmt.Sprintf("Failed on %s: Error returned", function))
   195  }
   196  
   197  func TestUpdateTierCreds(t *testing.T) {
   198  	assert := assert.New(t)
   199  	// mock minIO client
   200  	adminClient := AdminClientMock{}
   201  
   202  	function := "editTierCredentials()"
   203  	ctx, cancel := context.WithCancel(context.Background())
   204  	defer cancel()
   205  	// Test-1: editTierCredentials() update Tier configuration
   206  	minioEditTiersMock = func(_ context.Context, _ string, _ madmin.TierCreds) error {
   207  		return nil
   208  	}
   209  
   210  	params := &tieringApi.EditTierCredentialsParams{
   211  		Name: "TESTTIER",
   212  		Body: &models.TierCredentialsRequest{
   213  			AccessKey: "New Key",
   214  			SecretKey: "Secret Key",
   215  		},
   216  	}
   217  
   218  	err := editTierCredentials(ctx, adminClient, params)
   219  
   220  	assert.Equal(nil, err, fmt.Sprintf("Failed on %s: Error returned", function))
   221  
   222  	// Test-2: editTierCredentials() update Tier configuration failure
   223  	minioEditTiersMock = func(_ context.Context, _ string, _ madmin.TierCreds) error {
   224  		return errors.New("error message")
   225  	}
   226  
   227  	errT2 := editTierCredentials(ctx, adminClient, params)
   228  
   229  	assert.Equal(errors.New("error message"), errT2, fmt.Sprintf("Failed on %s: Error returned", function))
   230  }