github.com/minio/madmin-go/v2@v2.2.1/tier-config_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/json"
    24  	"testing"
    25  )
    26  
    27  // TestUnmarshalInvalidTierConfig tests that TierConfig parsing can catch invalid tier configs
    28  func TestUnmarshalInvalidTierConfig(t *testing.T) {
    29  	testCases := []struct {
    30  		cfg TierConfig
    31  		err error
    32  	}{
    33  		{
    34  			cfg: TierConfig{
    35  				Version: TierConfigVer,
    36  				Name:    "S3TIER?",
    37  				Type:    S3,
    38  				GCS: &TierGCS{
    39  					Creds:        "VWJ1bnR1IDIwLjA0LjEgTFRTIFxuIFxsCgo",
    40  					Bucket:       "ilmtesting",
    41  					Endpoint:     "https://storage.googleapis.com/",
    42  					Prefix:       "testprefix",
    43  					Region:       "us-west-2",
    44  					StorageClass: "",
    45  				},
    46  			},
    47  			err: ErrTierInvalidConfig,
    48  		},
    49  		{
    50  			cfg: TierConfig{
    51  				Version: "invalid-version",
    52  				Name:    "INVALIDTIER",
    53  				Type:    GCS,
    54  				GCS: &TierGCS{
    55  					Creds:        "VWJ1bnR1IDIwLjA0LjEgTFRTIFxuIFxsCgo",
    56  					Bucket:       "ilmtesting",
    57  					Endpoint:     "https://storage.googleapis.com/",
    58  					Prefix:       "testprefix",
    59  					Region:       "us-west-2",
    60  					StorageClass: "",
    61  				},
    62  			},
    63  			err: ErrTierInvalidConfigVersion,
    64  		},
    65  		{
    66  			cfg: TierConfig{
    67  				Version: TierConfigVer,
    68  				Type:    GCS,
    69  				GCS: &TierGCS{
    70  					Creds:        "VWJ1bnR1IDIwLjA0LjEgTFRTIFxuIFxsCgo",
    71  					Bucket:       "ilmtesting",
    72  					Endpoint:     "https://storage.googleapis.com/",
    73  					Prefix:       "testprefix",
    74  					Region:       "us-west-2",
    75  					StorageClass: "",
    76  				},
    77  			},
    78  			err: ErrTierNameEmpty,
    79  		},
    80  		{
    81  			cfg: TierConfig{
    82  				Version: TierConfigVer,
    83  				Name:    "GCSTIER",
    84  				Type:    GCS,
    85  				GCS: &TierGCS{
    86  					Creds:        "VWJ1bnR1IDIwLjA0LjEgTFRTIFxuIFxsCgo",
    87  					Bucket:       "ilmtesting",
    88  					Endpoint:     "https://storage.googleapis.com/",
    89  					Prefix:       "testprefix",
    90  					Region:       "us-west-2",
    91  					StorageClass: "",
    92  				},
    93  			},
    94  			err: nil,
    95  		},
    96  	}
    97  	for i, tc := range testCases {
    98  		data, err := json.Marshal(tc.cfg)
    99  		if err != nil {
   100  			t.Fatalf("Test %d: Failed to marshal tier config %v: %v", i+1, tc.cfg, err)
   101  		}
   102  		var cfg TierConfig
   103  		err = json.Unmarshal(data, &cfg)
   104  		if err != tc.err {
   105  			t.Fatalf("Test %d: Failed in unmarshal tier config %s: expected %v got %v", i+1, data, tc.err, err)
   106  		}
   107  	}
   108  
   109  	// Test invalid tier type
   110  	evilJSON := []byte(`{
   111                               "Version": "v1",
   112                               "Type" : "not-a-type",
   113                               "Name" : "GCSTIER3",
   114                               "GCS" : {
   115                                 "Bucket" : "ilmtesting",
   116                                 "Prefix" : "testprefix3",
   117                                 "Endpoint" : "https://storage.googleapis.com/",
   118                                 "Creds": "VWJ1bnR1IDIwLjA0LjEgTFRTIFxuIFxsCgo",
   119                                 "Region" : "us-west-2",
   120                                 "StorageClass" : ""
   121                               }
   122                              }`)
   123  	var cfg TierConfig
   124  	err := json.Unmarshal(evilJSON, &cfg)
   125  	if err != ErrTierTypeUnsupported {
   126  		t.Fatalf("Expected to fail with unsupported type but got %v", err)
   127  	}
   128  }