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.
150 lines
3.8 KiB
150 lines
3.8 KiB
9 years ago
|
/*
|
||
|
* Minio Cloud Storage, (C) 2015, 2016 Minio, Inc.
|
||
|
*
|
||
|
* 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
|
||
9 years ago
|
|
||
|
import (
|
||
8 years ago
|
"strconv"
|
||
9 years ago
|
"testing"
|
||
|
)
|
||
|
|
||
8 years ago
|
const MiB = 1024 * 1024
|
||
9 years ago
|
|
||
8 years ago
|
// Test xlMetaV1.AddObjectPart()
|
||
|
func TestAddObjectPart(t *testing.T) {
|
||
9 years ago
|
testCases := []struct {
|
||
8 years ago
|
partNum int
|
||
|
expectedIndex int
|
||
9 years ago
|
}{
|
||
8 years ago
|
{1, 0},
|
||
|
{2, 1},
|
||
|
{4, 2},
|
||
|
{5, 3},
|
||
|
{7, 4},
|
||
|
// Insert part.
|
||
|
{3, 2},
|
||
|
// Replace existing part.
|
||
|
{4, 3},
|
||
|
// Missing part.
|
||
|
{6, -1},
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
// Setup.
|
||
|
xlMeta := newXLMetaV1("test-object", 8, 8)
|
||
|
if !xlMeta.IsValid() {
|
||
|
t.Fatalf("unable to get xl meta")
|
||
|
}
|
||
|
|
||
|
// Test them.
|
||
|
for _, testCase := range testCases {
|
||
|
if testCase.expectedIndex > -1 {
|
||
|
partNumString := strconv.Itoa(testCase.partNum)
|
||
|
xlMeta.AddObjectPart(testCase.partNum, "part."+partNumString, "etag."+partNumString, int64(testCase.partNum+MiB))
|
||
|
}
|
||
|
|
||
|
if index := xlMeta.ObjectPartIndex(testCase.partNum); index != testCase.expectedIndex {
|
||
|
t.Fatalf("%+v: expected = %d, got: %d", testCase, testCase.expectedIndex, index)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Test xlMetaV1.ObjectPartIndex()
|
||
|
func TestObjectPartIndex(t *testing.T) {
|
||
|
testCases := []struct {
|
||
|
partNum int
|
||
|
expectedIndex int
|
||
|
}{
|
||
|
{2, 1},
|
||
|
{1, 0},
|
||
|
{5, 3},
|
||
|
{4, 2},
|
||
|
{7, 4},
|
||
|
}
|
||
|
|
||
|
// Setup.
|
||
|
xlMeta := newXLMetaV1("test-object", 8, 8)
|
||
|
if !xlMeta.IsValid() {
|
||
|
t.Fatalf("unable to get xl meta")
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
// Add some parts for testing.
|
||
|
for _, testCase := range testCases {
|
||
|
partNumString := strconv.Itoa(testCase.partNum)
|
||
|
xlMeta.AddObjectPart(testCase.partNum, "part."+partNumString, "etag."+partNumString, int64(testCase.partNum+MiB))
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
// Add failure test case.
|
||
|
testCases = append(testCases, struct {
|
||
|
partNum int
|
||
|
expectedIndex int
|
||
|
}{6, -1})
|
||
|
|
||
|
// Test them.
|
||
|
for _, testCase := range testCases {
|
||
|
if index := xlMeta.ObjectPartIndex(testCase.partNum); index != testCase.expectedIndex {
|
||
|
t.Fatalf("%+v: expected = %d, got: %d", testCase, testCase.expectedIndex, index)
|
||
9 years ago
|
}
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
|
// Test xlMetaV1.ObjectToPartOffset().
|
||
|
func TestObjectToPartOffset(t *testing.T) {
|
||
|
// Setup.
|
||
|
xlMeta := newXLMetaV1("test-object", 8, 8)
|
||
|
if !xlMeta.IsValid() {
|
||
|
t.Fatalf("unable to get xl meta")
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
// Add some parts for testing.
|
||
|
// Total size of all parts is 5,242,899 bytes.
|
||
|
for _, partNum := range []int{1, 2, 4, 5, 7} {
|
||
|
partNumString := strconv.Itoa(partNum)
|
||
|
xlMeta.AddObjectPart(partNum, "part."+partNumString, "etag."+partNumString, int64(partNum+MiB))
|
||
|
}
|
||
|
|
||
|
testCases := []struct {
|
||
|
offset int64
|
||
|
expectedIndex int
|
||
|
expectedOffset int64
|
||
|
expectedErr error
|
||
9 years ago
|
}{
|
||
8 years ago
|
{0, 0, 0, nil},
|
||
|
{MiB, 0, MiB, nil},
|
||
|
{1 + MiB, 1, 0, nil},
|
||
|
{2 + MiB, 1, 1, nil},
|
||
|
// Its valid for zero sized object.
|
||
|
{-1, 0, -1, nil},
|
||
|
// Max fffset is always (size - 1).
|
||
|
{(1 + 2 + 4 + 5 + 7) + (5 * MiB) - 1, 4, 1048582, nil},
|
||
|
// Error if offset is size.
|
||
|
{(1 + 2 + 4 + 5 + 7) + (5 * MiB), 0, 0, InvalidRange{}},
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
// Test them.
|
||
|
for _, testCase := range testCases {
|
||
|
index, offset, err := xlMeta.ObjectToPartOffset(testCase.offset)
|
||
|
if err != testCase.expectedErr {
|
||
|
t.Fatalf("%+v: expected = %s, got: %s", testCase, testCase.expectedErr, err)
|
||
9 years ago
|
}
|
||
8 years ago
|
if index != testCase.expectedIndex {
|
||
|
t.Fatalf("%+v: index: expected = %d, got: %d", testCase, testCase.expectedIndex, index)
|
||
9 years ago
|
}
|
||
8 years ago
|
if offset != testCase.expectedOffset {
|
||
|
t.Fatalf("%+v: offset: expected = %d, got: %d", testCase, testCase.expectedOffset, offset)
|
||
9 years ago
|
}
|
||
|
}
|
||
|
}
|