tests: improve unit tests for xl-v1-metadata. (#2172)

Fixes #2124
This commit is contained in:
Bala FA 2016-07-12 00:12:01 +05:30 committed by Harshavardhana
parent a162198623
commit bfc59b7d50

View File

@ -17,70 +17,133 @@
package main package main
import ( import (
"strconv"
"testing" "testing"
) )
// Test cases for xlMetaV1{} const MiB = 1024 * 1024
func TestXLMetaV1(t *testing.T) {
fiveMB := int64(5 * 1024 * 1024)
// Test xlMetaV1.AddObjectPart()
func TestAddObjectPart(t *testing.T) {
testCases := []struct { testCases := []struct {
partNum int partNum int
partName string expectedIndex int
etag string
size int64
index int
}{ }{
{5, "part.5", "etag5", fiveMB + 5, 3}, {1, 0},
{4, "part.4", "etag4", fiveMB + 4, 2}, {2, 1},
{7, "part.7", "etag7", fiveMB + 7, 4}, {4, 2},
{2, "part.2", "etag2", fiveMB + 2, 1}, {5, 3},
{1, "part.1", "etag1", fiveMB + 1, 0}, {7, 4},
// Insert part.
{3, 2},
// Replace existing part.
{4, 3},
// Missing part.
{6, -1},
} }
// Create a XLMetaV1 structure to test on. // Setup.
meta := newXLMetaV1("minio", 8, 8) xlMeta := newXLMetaV1("test-object", 8, 8)
if !xlMeta.IsValid() {
// Add 5 parts. t.Fatalf("unable to get xl meta")
for _, test := range testCases {
meta.AddObjectPart(test.partNum, test.partName, test.etag, test.size)
} }
// Test for ObjectPartIndex() // Test them.
for i, test := range testCases { for _, testCase := range testCases {
expected := test.index if testCase.expectedIndex > -1 {
got := meta.ObjectPartIndex(test.partNum) partNumString := strconv.Itoa(testCase.partNum)
if expected != got { xlMeta.AddObjectPart(testCase.partNum, "part."+partNumString, "etag."+partNumString, int64(testCase.partNum+MiB))
t.Errorf("Test %d: Expected %d, obtained %d", i+1, expected, got)
} }
}
offsetCases := []struct { if index := xlMeta.ObjectPartIndex(testCase.partNum); index != testCase.expectedIndex {
offset int64 t.Fatalf("%+v: expected = %d, got: %d", testCase, testCase.expectedIndex, index)
partIndex int }
partOffset int64 }
}{ }
{4 * 1024 * 1024, 0, 4 * 1024 * 1024},
{8 * 1024 * 1024, 1, 3145727}, // Test xlMetaV1.ObjectPartIndex()
{12 * 1024 * 1024, 2, 2097149}, func TestObjectPartIndex(t *testing.T) {
{16 * 1024 * 1024, 3, 1048569}, testCases := []struct {
{20 * 1024 * 1024, 3, 5242873}, partNum int
{24 * 1024 * 1024, 4, 4194292}, expectedIndex int
} }{
{2, 1},
// Test for ObjectToPartOffset() {1, 0},
for i, test := range offsetCases { {5, 3},
expectedIndex := test.partIndex {4, 2},
expectedOffset := test.partOffset {7, 4},
gotIndex, gotOffset, err := meta.ObjectToPartOffset(test.offset) }
if err != nil {
t.Errorf("Test %d: %s", i+1, err) // Setup.
} xlMeta := newXLMetaV1("test-object", 8, 8)
if gotIndex != expectedIndex { if !xlMeta.IsValid() {
t.Errorf("Test %d: Expected %v got %v", i+1, expectedIndex, gotIndex) t.Fatalf("unable to get xl meta")
} }
if gotOffset != expectedOffset {
t.Errorf("Test %d: Expected %v got %v", i+1, expectedOffset, gotOffset) // 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))
}
// 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)
}
}
}
// 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")
}
// 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
}{
{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{}},
}
// 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)
}
if index != testCase.expectedIndex {
t.Fatalf("%+v: index: expected = %d, got: %d", testCase, testCase.expectedIndex, index)
}
if offset != testCase.expectedOffset {
t.Fatalf("%+v: offset: expected = %d, got: %d", testCase, testCase.expectedOffset, offset)
} }
} }
} }