You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
319 lines
12 KiB
319 lines
12 KiB
8 years ago
|
/*
|
||
5 years ago
|
* MinIO Cloud Storage, (C) 2020 MinIO, Inc.
|
||
8 years ago
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
8 years ago
|
package cmd
|
||
8 years ago
|
|
||
8 years ago
|
import (
|
||
7 years ago
|
"bytes"
|
||
|
"encoding/hex"
|
||
8 years ago
|
"encoding/json"
|
||
8 years ago
|
"testing"
|
||
8 years ago
|
|
||
5 years ago
|
"github.com/dustin/go-humanize"
|
||
|
jsoniter "github.com/json-iterator/go"
|
||
8 years ago
|
)
|
||
8 years ago
|
|
||
5 years ago
|
func TestIsXLMetaFormatValid(t *testing.T) {
|
||
|
tests := []struct {
|
||
|
name int
|
||
|
version string
|
||
|
format string
|
||
|
want bool
|
||
8 years ago
|
}{
|
||
5 years ago
|
{1, "123", "fs", false},
|
||
|
{2, "123", xlMetaFormat, false},
|
||
|
{3, xlMetaVersion100, "test", false},
|
||
|
{4, xlMetaVersion101, "hello", false},
|
||
|
{5, xlMetaVersion100, xlMetaFormat, true},
|
||
|
{6, xlMetaVersion101, xlMetaFormat, true},
|
||
|
}
|
||
|
for _, tt := range tests {
|
||
|
if got := isXLMetaFormatValid(tt.version, tt.format); got != tt.want {
|
||
|
t.Errorf("Test %d: Expected %v but received %v", tt.name, got, tt.want)
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
|
}
|
||
8 years ago
|
|
||
5 years ago
|
func TestIsXLMetaErasureInfoValid(t *testing.T) {
|
||
|
tests := []struct {
|
||
|
name int
|
||
|
data int
|
||
|
parity int
|
||
|
want bool
|
||
8 years ago
|
}{
|
||
5 years ago
|
{1, 5, 6, false},
|
||
|
{2, 5, 5, true},
|
||
|
{3, 0, 5, false},
|
||
|
{4, 5, 0, false},
|
||
|
{5, 5, 0, false},
|
||
|
{6, 5, 4, true},
|
||
|
}
|
||
|
for _, tt := range tests {
|
||
|
if got := isXLMetaErasureInfoValid(tt.data, tt.parity); got != tt.want {
|
||
|
t.Errorf("Test %d: Expected %v but received %v", tt.name, got, tt.want)
|
||
8 years ago
|
}
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
5 years ago
|
// newTestXLMetaV1 - initializes new xlMetaV1Object, adds version, allocates a fresh erasure info and metadata.
|
||
|
func newTestXLMetaV1() xlMetaV1Object {
|
||
|
xlMeta := xlMetaV1Object{}
|
||
|
xlMeta.Version = xlMetaVersion101
|
||
8 years ago
|
xlMeta.Format = xlMetaFormat
|
||
|
xlMeta.Minio.Release = "test"
|
||
7 years ago
|
xlMeta.Erasure = ErasureInfo{
|
||
8 years ago
|
Algorithm: "klauspost/reedsolomon/vandermonde",
|
||
|
DataBlocks: 5,
|
||
|
ParityBlocks: 5,
|
||
|
BlockSize: 10485760,
|
||
|
Index: 10,
|
||
|
Distribution: []int{9, 10, 1, 2, 3, 4, 5, 6, 7, 8},
|
||
|
}
|
||
5 years ago
|
xlMeta.Stat = StatInfo{
|
||
8 years ago
|
Size: int64(20),
|
||
8 years ago
|
ModTime: UTCNow(),
|
||
8 years ago
|
}
|
||
|
// Set meta data.
|
||
|
xlMeta.Meta = make(map[string]string)
|
||
|
xlMeta.Meta["testKey1"] = "val1"
|
||
|
xlMeta.Meta["testKey2"] = "val2"
|
||
|
return xlMeta
|
||
|
}
|
||
|
|
||
5 years ago
|
func (m *xlMetaV1Object) AddTestObjectCheckSum(partNumber int, algorithm BitrotAlgorithm, hash string) {
|
||
7 years ago
|
checksum, err := hex.DecodeString(hash)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
8 years ago
|
}
|
||
5 years ago
|
m.Erasure.Checksums[partNumber-1] = ChecksumInfo{partNumber, algorithm, checksum}
|
||
8 years ago
|
}
|
||
|
|
||
|
// AddTestObjectPart - add a new object part in order.
|
||
5 years ago
|
func (m *xlMetaV1Object) AddTestObjectPart(partNumber int, partSize int64) {
|
||
6 years ago
|
partInfo := ObjectPartInfo{
|
||
8 years ago
|
Number: partNumber,
|
||
|
Size: partSize,
|
||
|
}
|
||
|
|
||
|
// Proceed to include new part info.
|
||
5 years ago
|
m.Parts[partNumber-1] = partInfo
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
// Constructs xlMetaV1Object{} for given number of parts and converts it into bytes.
|
||
8 years ago
|
func getXLMetaBytes(totalParts int) []byte {
|
||
|
xlSampleMeta := getSampleXLMeta(totalParts)
|
||
|
xlMetaBytes, err := json.Marshal(xlSampleMeta)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return xlMetaBytes
|
||
|
}
|
||
|
|
||
5 years ago
|
// Returns sample xlMetaV1Object{} for number of parts.
|
||
|
func getSampleXLMeta(totalParts int) xlMetaV1Object {
|
||
8 years ago
|
xlMeta := newTestXLMetaV1()
|
||
|
// Number of checksum info == total parts.
|
||
7 years ago
|
xlMeta.Erasure.Checksums = make([]ChecksumInfo, totalParts)
|
||
8 years ago
|
// total number of parts.
|
||
6 years ago
|
xlMeta.Parts = make([]ObjectPartInfo, totalParts)
|
||
8 years ago
|
for i := 0; i < totalParts; i++ {
|
||
5 years ago
|
// hard coding hash and algo value for the checksum, Since we are benchmarking the parsing of xl.meta the magnitude doesn't affect the test,
|
||
8 years ago
|
// The magnitude doesn't make a difference, only the size does.
|
||
5 years ago
|
xlMeta.AddTestObjectCheckSum(i+1, BLAKE2b512, "a23f5eff248c4372badd9f3b2455a285cd4ca86c3d9a570b091d3fc5cd7ca6d9484bbea3f8c5d8d4f84daae96874419eda578fd736455334afbac2c924b3915a")
|
||
|
xlMeta.AddTestObjectPart(i+1, 67108864)
|
||
8 years ago
|
}
|
||
|
return xlMeta
|
||
|
}
|
||
|
|
||
5 years ago
|
// Compare the unmarshaled XLMetaV1 with the one obtained from jsoniter parsing.
|
||
5 years ago
|
func compareXLMetaV1(t *testing.T, unMarshalXLMeta, jsoniterXLMeta xlMetaV1Object) {
|
||
|
// Start comparing the fields of xlMetaV1Object obtained from jsoniter parsing with one parsed using json unmarshaling.
|
||
5 years ago
|
if unMarshalXLMeta.Version != jsoniterXLMeta.Version {
|
||
|
t.Errorf("Expected the Version to be \"%s\", but got \"%s\".", unMarshalXLMeta.Version, jsoniterXLMeta.Version)
|
||
8 years ago
|
}
|
||
5 years ago
|
if unMarshalXLMeta.Format != jsoniterXLMeta.Format {
|
||
|
t.Errorf("Expected the format to be \"%s\", but got \"%s\".", unMarshalXLMeta.Format, jsoniterXLMeta.Format)
|
||
8 years ago
|
}
|
||
5 years ago
|
if unMarshalXLMeta.Stat.Size != jsoniterXLMeta.Stat.Size {
|
||
|
t.Errorf("Expected the stat size to be %v, but got %v.", unMarshalXLMeta.Stat.Size, jsoniterXLMeta.Stat.Size)
|
||
8 years ago
|
}
|
||
5 years ago
|
if !unMarshalXLMeta.Stat.ModTime.Equal(jsoniterXLMeta.Stat.ModTime) {
|
||
|
t.Errorf("Expected the modTime to be \"%v\", but got \"%v\".", unMarshalXLMeta.Stat.ModTime, jsoniterXLMeta.Stat.ModTime)
|
||
8 years ago
|
}
|
||
5 years ago
|
if unMarshalXLMeta.Erasure.Algorithm != jsoniterXLMeta.Erasure.Algorithm {
|
||
|
t.Errorf("Expected the erasure algorithm to be \"%v\", but got \"%v\".", unMarshalXLMeta.Erasure.Algorithm, jsoniterXLMeta.Erasure.Algorithm)
|
||
8 years ago
|
}
|
||
5 years ago
|
if unMarshalXLMeta.Erasure.DataBlocks != jsoniterXLMeta.Erasure.DataBlocks {
|
||
|
t.Errorf("Expected the erasure data blocks to be %v, but got %v.", unMarshalXLMeta.Erasure.DataBlocks, jsoniterXLMeta.Erasure.DataBlocks)
|
||
8 years ago
|
}
|
||
5 years ago
|
if unMarshalXLMeta.Erasure.ParityBlocks != jsoniterXLMeta.Erasure.ParityBlocks {
|
||
|
t.Errorf("Expected the erasure parity blocks to be %v, but got %v.", unMarshalXLMeta.Erasure.ParityBlocks, jsoniterXLMeta.Erasure.ParityBlocks)
|
||
8 years ago
|
}
|
||
5 years ago
|
if unMarshalXLMeta.Erasure.BlockSize != jsoniterXLMeta.Erasure.BlockSize {
|
||
|
t.Errorf("Expected the erasure block size to be %v, but got %v.", unMarshalXLMeta.Erasure.BlockSize, jsoniterXLMeta.Erasure.BlockSize)
|
||
8 years ago
|
}
|
||
5 years ago
|
if unMarshalXLMeta.Erasure.Index != jsoniterXLMeta.Erasure.Index {
|
||
|
t.Errorf("Expected the erasure index to be %v, but got %v.", unMarshalXLMeta.Erasure.Index, jsoniterXLMeta.Erasure.Index)
|
||
8 years ago
|
}
|
||
5 years ago
|
if len(unMarshalXLMeta.Erasure.Distribution) != len(jsoniterXLMeta.Erasure.Distribution) {
|
||
|
t.Errorf("Expected the size of Erasure Distribution to be %d, but got %d.", len(unMarshalXLMeta.Erasure.Distribution), len(jsoniterXLMeta.Erasure.Distribution))
|
||
8 years ago
|
} else {
|
||
|
for i := 0; i < len(unMarshalXLMeta.Erasure.Distribution); i++ {
|
||
5 years ago
|
if unMarshalXLMeta.Erasure.Distribution[i] != jsoniterXLMeta.Erasure.Distribution[i] {
|
||
|
t.Errorf("Expected the Erasure Distribution to be %d, got %d.", unMarshalXLMeta.Erasure.Distribution[i], jsoniterXLMeta.Erasure.Distribution[i])
|
||
8 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
if len(unMarshalXLMeta.Erasure.Checksums) != len(jsoniterXLMeta.Erasure.Checksums) {
|
||
|
t.Errorf("Expected the size of Erasure Checksums to be %d, but got %d.", len(unMarshalXLMeta.Erasure.Checksums), len(jsoniterXLMeta.Erasure.Checksums))
|
||
8 years ago
|
} else {
|
||
7 years ago
|
for i := 0; i < len(unMarshalXLMeta.Erasure.Checksums); i++ {
|
||
5 years ago
|
if unMarshalXLMeta.Erasure.Checksums[i].PartNumber != jsoniterXLMeta.Erasure.Checksums[i].PartNumber {
|
||
|
t.Errorf("Expected the Erasure Checksum PartNumber to be \"%d\", got \"%d\".", unMarshalXLMeta.Erasure.Checksums[i].PartNumber, jsoniterXLMeta.Erasure.Checksums[i].PartNumber)
|
||
8 years ago
|
}
|
||
5 years ago
|
if unMarshalXLMeta.Erasure.Checksums[i].Algorithm != jsoniterXLMeta.Erasure.Checksums[i].Algorithm {
|
||
|
t.Errorf("Expected the Erasure Checksum Algorithm to be \"%s\", got \"%s\".", unMarshalXLMeta.Erasure.Checksums[i].Algorithm, jsoniterXLMeta.Erasure.Checksums[i].Algorithm)
|
||
8 years ago
|
}
|
||
5 years ago
|
if !bytes.Equal(unMarshalXLMeta.Erasure.Checksums[i].Hash, jsoniterXLMeta.Erasure.Checksums[i].Hash) {
|
||
|
t.Errorf("Expected the Erasure Checksum Hash to be \"%s\", got \"%s\".", unMarshalXLMeta.Erasure.Checksums[i].Hash, jsoniterXLMeta.Erasure.Checksums[i].Hash)
|
||
8 years ago
|
}
|
||
|
}
|
||
|
}
|
||
6 years ago
|
|
||
5 years ago
|
if unMarshalXLMeta.Minio.Release != jsoniterXLMeta.Minio.Release {
|
||
|
t.Errorf("Expected the Release string to be \"%s\", but got \"%s\".", unMarshalXLMeta.Minio.Release, jsoniterXLMeta.Minio.Release)
|
||
8 years ago
|
}
|
||
5 years ago
|
if len(unMarshalXLMeta.Parts) != len(jsoniterXLMeta.Parts) {
|
||
|
t.Errorf("Expected info of %d parts to be present, but got %d instead.", len(unMarshalXLMeta.Parts), len(jsoniterXLMeta.Parts))
|
||
8 years ago
|
} else {
|
||
|
for i := 0; i < len(unMarshalXLMeta.Parts); i++ {
|
||
5 years ago
|
if unMarshalXLMeta.Parts[i].Number != jsoniterXLMeta.Parts[i].Number {
|
||
|
t.Errorf("Expected the number of part %d to be \"%d\", got \"%d\".", i+1, unMarshalXLMeta.Parts[i].Number, jsoniterXLMeta.Parts[i].Number)
|
||
8 years ago
|
}
|
||
5 years ago
|
if unMarshalXLMeta.Parts[i].Size != jsoniterXLMeta.Parts[i].Size {
|
||
|
t.Errorf("Expected the size of part %d to be %v, got %v.", i+1, unMarshalXLMeta.Parts[i].Size, jsoniterXLMeta.Parts[i].Size)
|
||
8 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for key, val := range unMarshalXLMeta.Meta {
|
||
5 years ago
|
jsoniterVal, exists := jsoniterXLMeta.Meta[key]
|
||
8 years ago
|
if !exists {
|
||
|
t.Errorf("No meta data entry for Key \"%s\" exists.", key)
|
||
|
}
|
||
5 years ago
|
if val != jsoniterVal {
|
||
|
t.Errorf("Expected the value for Meta data key \"%s\" to be \"%s\", but got \"%s\".", key, val, jsoniterVal)
|
||
8 years ago
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// Tests the correctness of constructing XLMetaV1 using jsoniter lib.
|
||
8 years ago
|
// The result will be compared with the result obtained from json.unMarshal of the byte data.
|
||
5 years ago
|
func TestGetXLMetaV1Jsoniter1(t *testing.T) {
|
||
8 years ago
|
xlMetaJSON := getXLMetaBytes(1)
|
||
|
|
||
5 years ago
|
var unMarshalXLMeta xlMetaV1Object
|
||
8 years ago
|
if err := json.Unmarshal(xlMetaJSON, &unMarshalXLMeta); err != nil {
|
||
7 years ago
|
t.Errorf("Unmarshalling failed: %v", err)
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
var jsoniterXLMeta xlMetaV1Object
|
||
|
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||
|
if err := json.Unmarshal(xlMetaJSON, &jsoniterXLMeta); err != nil {
|
||
5 years ago
|
t.Errorf("jsoniter parsing of XLMeta failed: %v", err)
|
||
8 years ago
|
}
|
||
5 years ago
|
compareXLMetaV1(t, unMarshalXLMeta, jsoniterXLMeta)
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
// Tests the correctness of constructing XLMetaV1 using jsoniter lib for XLMetaV1 of size 10 parts.
|
||
8 years ago
|
// The result will be compared with the result obtained from json.unMarshal of the byte data.
|
||
5 years ago
|
func TestGetXLMetaV1Jsoniter10(t *testing.T) {
|
||
8 years ago
|
|
||
|
xlMetaJSON := getXLMetaBytes(10)
|
||
|
|
||
5 years ago
|
var unMarshalXLMeta xlMetaV1Object
|
||
8 years ago
|
if err := json.Unmarshal(xlMetaJSON, &unMarshalXLMeta); err != nil {
|
||
7 years ago
|
t.Errorf("Unmarshalling failed: %v", err)
|
||
8 years ago
|
}
|
||
5 years ago
|
|
||
|
var jsoniterXLMeta xlMetaV1Object
|
||
|
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||
|
if err := json.Unmarshal(xlMetaJSON, &jsoniterXLMeta); err != nil {
|
||
5 years ago
|
t.Errorf("jsoniter parsing of XLMeta failed: %v", err)
|
||
8 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
compareXLMetaV1(t, unMarshalXLMeta, jsoniterXLMeta)
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
|
// Test the predicted part size from the part index
|
||
|
func TestGetPartSizeFromIdx(t *testing.T) {
|
||
|
// Create test cases
|
||
|
testCases := []struct {
|
||
|
totalSize int64
|
||
|
partSize int64
|
||
|
partIndex int
|
||
|
expectedSize int64
|
||
|
}{
|
||
|
// Total size is zero
|
||
|
{0, 10, 1, 0},
|
||
|
// part size 2MiB, total size 4MiB
|
||
|
{4 * humanize.MiByte, 2 * humanize.MiByte, 1, 2 * humanize.MiByte},
|
||
|
{4 * humanize.MiByte, 2 * humanize.MiByte, 2, 2 * humanize.MiByte},
|
||
|
{4 * humanize.MiByte, 2 * humanize.MiByte, 3, 0},
|
||
|
// part size 2MiB, total size 5MiB
|
||
|
{5 * humanize.MiByte, 2 * humanize.MiByte, 1, 2 * humanize.MiByte},
|
||
|
{5 * humanize.MiByte, 2 * humanize.MiByte, 2, 2 * humanize.MiByte},
|
||
|
{5 * humanize.MiByte, 2 * humanize.MiByte, 3, 1 * humanize.MiByte},
|
||
|
{5 * humanize.MiByte, 2 * humanize.MiByte, 4, 0},
|
||
|
}
|
||
|
|
||
|
for i, testCase := range testCases {
|
||
5 years ago
|
s, err := calculatePartSizeFromIdx(GlobalContext, testCase.totalSize, testCase.partSize, testCase.partIndex)
|
||
8 years ago
|
if err != nil {
|
||
|
t.Errorf("Test %d: Expected to pass but failed. %s", i+1, err)
|
||
|
}
|
||
|
if err == nil && s != testCase.expectedSize {
|
||
8 years ago
|
t.Errorf("Test %d: The calculated part size is incorrect: expected = %d, found = %d\n", i+1, testCase.expectedSize, s)
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
|
testCasesFailure := []struct {
|
||
|
totalSize int64
|
||
|
partSize int64
|
||
|
partIndex int
|
||
|
err error
|
||
|
}{
|
||
7 years ago
|
// partSize is 0, returns error.
|
||
8 years ago
|
{10, 0, 1, errPartSizeZero},
|
||
7 years ago
|
// partIndex is 0, returns error.
|
||
8 years ago
|
{10, 1, 0, errPartSizeIndex},
|
||
7 years ago
|
// Total size is -1, returns error.
|
||
6 years ago
|
{-2, 10, 1, errInvalidArgument},
|
||
8 years ago
|
}
|
||
|
|
||
|
for i, testCaseFailure := range testCasesFailure {
|
||
5 years ago
|
_, err := calculatePartSizeFromIdx(GlobalContext, testCaseFailure.totalSize, testCaseFailure.partSize, testCaseFailure.partIndex)
|
||
8 years ago
|
if err == nil {
|
||
|
t.Errorf("Test %d: Expected to failed but passed. %s", i+1, err)
|
||
|
}
|
||
7 years ago
|
if err != nil && err != testCaseFailure.err {
|
||
|
t.Errorf("Test %d: Expected err %s, but got %s", i+1, testCaseFailure.err, err)
|
||
8 years ago
|
}
|
||
|
}
|
||
8 years ago
|
}
|