2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-10-07 01:50:24 -04:00
|
|
|
|
|
|
|
package storageclass
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseStorageClass(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
storageClassEnv string
|
|
|
|
wantSc StorageClass
|
|
|
|
expectedError error
|
|
|
|
}{
|
2022-01-02 12:15:06 -05:00
|
|
|
{
|
|
|
|
"EC:3",
|
|
|
|
StorageClass{
|
|
|
|
Parity: 3,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"EC:4",
|
|
|
|
StorageClass{
|
|
|
|
Parity: 4,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"AB:4",
|
|
|
|
StorageClass{
|
|
|
|
Parity: 4,
|
|
|
|
},
|
|
|
|
errors.New("Unsupported scheme AB. Supported scheme is EC"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"EC:4:5",
|
|
|
|
StorageClass{
|
|
|
|
Parity: 4,
|
|
|
|
},
|
|
|
|
errors.New("Too many sections in EC:4:5"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"EC:A",
|
|
|
|
StorageClass{
|
|
|
|
Parity: 4,
|
|
|
|
},
|
|
|
|
errors.New(`strconv.Atoi: parsing "A": invalid syntax`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"AB",
|
|
|
|
StorageClass{
|
|
|
|
Parity: 4,
|
|
|
|
},
|
|
|
|
errors.New("Too few sections in AB"),
|
|
|
|
},
|
2019-10-07 01:50:24 -04:00
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
gotSc, err := parseStorageClass(tt.storageClassEnv)
|
|
|
|
if err != nil && tt.expectedError == nil {
|
|
|
|
t.Errorf("Test %d, Expected %s, got %s", i+1, tt.expectedError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err == nil && tt.expectedError != nil {
|
|
|
|
t.Errorf("Test %d, Expected %s, got %s", i+1, tt.expectedError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if tt.expectedError == nil && !reflect.DeepEqual(gotSc, tt.wantSc) {
|
|
|
|
t.Errorf("Test %d, Expected %v, got %v", i+1, tt.wantSc, gotSc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if tt.expectedError != nil && err.Error() != tt.expectedError.Error() {
|
|
|
|
t.Errorf("Test %d, Expected `%v`, got `%v`", i+1, tt.expectedError, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateParity(t *testing.T) {
|
|
|
|
tests := []struct {
|
2020-08-26 22:29:35 -04:00
|
|
|
rrsParity int
|
|
|
|
ssParity int
|
|
|
|
success bool
|
|
|
|
setDriveCount int
|
2019-10-07 01:50:24 -04:00
|
|
|
}{
|
|
|
|
{2, 4, true, 16},
|
|
|
|
{3, 3, true, 16},
|
|
|
|
{0, 0, true, 16},
|
2022-06-27 23:22:18 -04:00
|
|
|
{1, 4, true, 16},
|
|
|
|
{0, 4, true, 16},
|
2019-10-07 01:50:24 -04:00
|
|
|
{7, 6, false, 16},
|
|
|
|
{9, 0, false, 16},
|
|
|
|
{9, 9, false, 16},
|
|
|
|
{2, 9, false, 16},
|
|
|
|
{9, 2, false, 16},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
2020-08-26 22:29:35 -04:00
|
|
|
err := validateParity(tt.ssParity, tt.rrsParity, tt.setDriveCount)
|
2019-10-07 01:50:24 -04:00
|
|
|
if err != nil && tt.success {
|
|
|
|
t.Errorf("Test %d, Expected success, got %s", i+1, err)
|
|
|
|
}
|
|
|
|
if err == nil && !tt.success {
|
|
|
|
t.Errorf("Test %d, Expected failure, got success", i+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParityCount(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
sc string
|
|
|
|
disksCount int
|
|
|
|
expectedData int
|
|
|
|
expectedParity int
|
|
|
|
}{
|
|
|
|
{RRS, 16, 14, 2},
|
|
|
|
{STANDARD, 16, 8, 8},
|
|
|
|
{"", 16, 8, 8},
|
|
|
|
{RRS, 16, 9, 7},
|
|
|
|
{STANDARD, 16, 10, 6},
|
|
|
|
{"", 16, 9, 7},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
scfg := Config{
|
|
|
|
Standard: StorageClass{
|
|
|
|
Parity: 8,
|
|
|
|
},
|
|
|
|
RRS: StorageClass{
|
2022-06-27 23:22:18 -04:00
|
|
|
Parity: 2,
|
2019-10-07 01:50:24 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
// Set env var for test case 4
|
|
|
|
if i+1 == 4 {
|
|
|
|
scfg.RRS.Parity = 7
|
|
|
|
}
|
|
|
|
// Set env var for test case 5
|
|
|
|
if i+1 == 5 {
|
|
|
|
scfg.Standard.Parity = 6
|
|
|
|
}
|
|
|
|
// Set env var for test case 6
|
|
|
|
if i+1 == 6 {
|
|
|
|
scfg.Standard.Parity = 7
|
|
|
|
}
|
|
|
|
parity := scfg.GetParityForSC(tt.sc)
|
|
|
|
if (tt.disksCount - parity) != tt.expectedData {
|
2022-08-04 19:10:08 -04:00
|
|
|
t.Errorf("Test %d, Expected data drives %d, got %d", i+1, tt.expectedData, tt.disksCount-parity)
|
2019-10-07 01:50:24 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if parity != tt.expectedParity {
|
2022-08-04 19:10:08 -04:00
|
|
|
t.Errorf("Test %d, Expected parity drives %d, got %d", i+1, tt.expectedParity, parity)
|
2019-10-07 01:50:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test IsValid method with valid and invalid inputs
|
|
|
|
func TestIsValidStorageClassKind(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
sc string
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{"STANDARD", true},
|
|
|
|
{"REDUCED_REDUNDANCY", true},
|
|
|
|
{"", false},
|
|
|
|
{"INVALID", false},
|
|
|
|
{"123", false},
|
|
|
|
{"MINIO_STORAGE_CLASS_RRS", false},
|
|
|
|
{"MINIO_STORAGE_CLASS_STANDARD", false},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
if got := IsValid(tt.sc); got != tt.want {
|
|
|
|
t.Errorf("Test %d, Expected Storage Class to be %t, got %t", i+1, tt.want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|