2016-07-19 02:56:16 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 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.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-07-19 02:56:16 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
2017-08-14 21:08:42 -04:00
|
|
|
"io"
|
2016-07-19 02:56:16 -04:00
|
|
|
"testing"
|
2016-07-28 05:20:34 -04:00
|
|
|
|
2016-11-22 21:18:22 -05:00
|
|
|
humanize "github.com/dustin/go-humanize"
|
2016-07-19 02:56:16 -04:00
|
|
|
)
|
|
|
|
|
2017-08-14 21:08:42 -04:00
|
|
|
type badDisk struct{ StorageAPI }
|
2016-07-19 02:56:16 -04:00
|
|
|
|
2017-08-14 21:08:42 -04:00
|
|
|
func (a badDisk) AppendFile(volume string, path string, buf []byte) error {
|
2016-07-19 02:56:16 -04:00
|
|
|
return errFaultyDisk
|
|
|
|
}
|
|
|
|
|
2017-08-14 21:08:42 -04:00
|
|
|
const oneMiByte = 1 * humanize.MiByte
|
|
|
|
|
|
|
|
var erasureCreateFileTests = []struct {
|
|
|
|
dataBlocks int
|
|
|
|
onDisks, offDisks int
|
|
|
|
blocksize, data int64
|
|
|
|
offset int
|
|
|
|
algorithm BitrotAlgorithm
|
|
|
|
shouldFail, shouldFailQuorum bool
|
|
|
|
}{
|
|
|
|
{dataBlocks: 2, onDisks: 4, offDisks: 0, blocksize: int64(blockSizeV1), data: oneMiByte, offset: 0, algorithm: BLAKE2b512, shouldFail: false, shouldFailQuorum: false}, // 0
|
|
|
|
{dataBlocks: 3, onDisks: 6, offDisks: 0, blocksize: int64(blockSizeV1), data: oneMiByte, offset: 1, algorithm: SHA256, shouldFail: false, shouldFailQuorum: false}, // 1
|
|
|
|
{dataBlocks: 4, onDisks: 8, offDisks: 2, blocksize: int64(blockSizeV1), data: oneMiByte, offset: 2, algorithm: DefaultBitrotAlgorithm, shouldFail: false, shouldFailQuorum: false}, // 2
|
|
|
|
{dataBlocks: 5, onDisks: 10, offDisks: 3, blocksize: int64(blockSizeV1), data: oneMiByte, offset: oneMiByte, algorithm: BLAKE2b512, shouldFail: false, shouldFailQuorum: false}, // 3
|
|
|
|
{dataBlocks: 6, onDisks: 12, offDisks: 4, blocksize: int64(blockSizeV1), data: oneMiByte, offset: oneMiByte, algorithm: BLAKE2b512, shouldFail: false, shouldFailQuorum: false}, // 4
|
|
|
|
{dataBlocks: 7, onDisks: 14, offDisks: 5, blocksize: int64(blockSizeV1), data: 0, offset: 0, shouldFail: false, algorithm: SHA256, shouldFailQuorum: false}, // 5
|
|
|
|
{dataBlocks: 8, onDisks: 16, offDisks: 7, blocksize: int64(blockSizeV1), data: 0, offset: 0, shouldFail: false, algorithm: DefaultBitrotAlgorithm, shouldFailQuorum: false}, // 6
|
|
|
|
{dataBlocks: 2, onDisks: 4, offDisks: 2, blocksize: int64(blockSizeV1), data: oneMiByte, offset: 0, algorithm: BLAKE2b512, shouldFail: false, shouldFailQuorum: true}, // 7
|
|
|
|
{dataBlocks: 4, onDisks: 8, offDisks: 4, blocksize: int64(blockSizeV1), data: oneMiByte, offset: 0, algorithm: SHA256, shouldFail: false, shouldFailQuorum: true}, // 8
|
|
|
|
{dataBlocks: 7, onDisks: 14, offDisks: 7, blocksize: int64(blockSizeV1), data: oneMiByte, offset: 0, algorithm: DefaultBitrotAlgorithm, shouldFail: false, shouldFailQuorum: true}, // 9
|
|
|
|
{dataBlocks: 8, onDisks: 16, offDisks: 8, blocksize: int64(blockSizeV1), data: oneMiByte, offset: 0, algorithm: DefaultBitrotAlgorithm, shouldFail: false, shouldFailQuorum: true}, // 10
|
|
|
|
{dataBlocks: 5, onDisks: 10, offDisks: 3, blocksize: int64(oneMiByte), data: oneMiByte, offset: 0, algorithm: DefaultBitrotAlgorithm, shouldFail: false, shouldFailQuorum: false}, // 11
|
|
|
|
{dataBlocks: 6, onDisks: 12, offDisks: 5, blocksize: int64(blockSizeV1), data: oneMiByte, offset: 102, algorithm: 0, shouldFail: true, shouldFailQuorum: false}, // 12
|
|
|
|
{dataBlocks: 3, onDisks: 6, offDisks: 1, blocksize: int64(blockSizeV1), data: oneMiByte, offset: oneMiByte / 2, algorithm: DefaultBitrotAlgorithm, shouldFail: false, shouldFailQuorum: false}, // 13
|
|
|
|
{dataBlocks: 2, onDisks: 4, offDisks: 0, blocksize: int64(oneMiByte / 2), data: oneMiByte, offset: oneMiByte/2 + 1, algorithm: DefaultBitrotAlgorithm, shouldFail: false, shouldFailQuorum: false}, // 14
|
|
|
|
{dataBlocks: 4, onDisks: 8, offDisks: 0, blocksize: int64(oneMiByte - 1), data: oneMiByte, offset: oneMiByte - 1, algorithm: BLAKE2b512, shouldFail: false, shouldFailQuorum: false}, // 15
|
|
|
|
{dataBlocks: 8, onDisks: 12, offDisks: 2, blocksize: int64(blockSizeV1), data: oneMiByte, offset: 2, algorithm: DefaultBitrotAlgorithm, shouldFail: false, shouldFailQuorum: false}, // 16
|
|
|
|
{dataBlocks: 8, onDisks: 10, offDisks: 1, blocksize: int64(blockSizeV1), data: oneMiByte, offset: 0, algorithm: DefaultBitrotAlgorithm, shouldFail: false, shouldFailQuorum: false}, // 17
|
|
|
|
{dataBlocks: 10, onDisks: 14, offDisks: 0, blocksize: int64(blockSizeV1), data: oneMiByte, offset: 17, algorithm: DefaultBitrotAlgorithm, shouldFail: false, shouldFailQuorum: false}, // 18
|
|
|
|
{dataBlocks: 2, onDisks: 6, offDisks: 2, blocksize: int64(oneMiByte), data: oneMiByte, offset: oneMiByte / 2, algorithm: DefaultBitrotAlgorithm, shouldFail: false, shouldFailQuorum: false}, // 19
|
|
|
|
{dataBlocks: 10, onDisks: 16, offDisks: 8, blocksize: int64(blockSizeV1), data: oneMiByte, offset: 0, algorithm: DefaultBitrotAlgorithm, shouldFail: false, shouldFailQuorum: true}, // 20
|
2016-07-19 02:56:16 -04:00
|
|
|
}
|
2016-07-25 23:36:41 -04:00
|
|
|
|
2017-08-14 21:08:42 -04:00
|
|
|
func TestErasureCreateFile(t *testing.T) {
|
|
|
|
for i, test := range erasureCreateFileTests {
|
|
|
|
setup, err := newErasureTestSetup(test.dataBlocks, test.onDisks-test.dataBlocks, test.blocksize)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: failed to create test setup: %v", i, err)
|
|
|
|
}
|
|
|
|
storage, err := NewErasureStorage(setup.disks, test.dataBlocks, test.onDisks-test.dataBlocks)
|
|
|
|
if err != nil {
|
|
|
|
setup.Remove()
|
|
|
|
t.Fatalf("Test %d: failed to create ErasureStorage: %v", i, err)
|
|
|
|
}
|
|
|
|
buffer := make([]byte, test.blocksize, 2*test.blocksize)
|
2016-07-25 23:36:41 -04:00
|
|
|
|
2017-08-14 21:08:42 -04:00
|
|
|
data := make([]byte, test.data)
|
|
|
|
if _, err = io.ReadFull(rand.Reader, data); err != nil {
|
|
|
|
setup.Remove()
|
|
|
|
t.Fatalf("Test %d: failed to generate random test data: %v", i, err)
|
|
|
|
}
|
|
|
|
file, err := storage.CreateFile(bytes.NewReader(data[test.offset:]), "testbucket", "object", buffer, test.algorithm, test.dataBlocks+1)
|
|
|
|
if err != nil && !test.shouldFail {
|
|
|
|
t.Errorf("Test %d: should pass but failed with: %v", i, err)
|
2016-07-25 23:36:41 -04:00
|
|
|
}
|
2017-08-14 21:08:42 -04:00
|
|
|
if err == nil && test.shouldFail {
|
|
|
|
t.Errorf("Test %d: should fail but it passed", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if length := int64(len(data[test.offset:])); file.Size != length {
|
|
|
|
t.Errorf("Test %d: invalid number of bytes written: got: #%d want #%d", i, file.Size, length)
|
|
|
|
}
|
|
|
|
for j := range storage.disks[:test.offDisks] {
|
|
|
|
storage.disks[j] = badDisk{nil}
|
|
|
|
}
|
|
|
|
if test.offDisks > 0 {
|
|
|
|
storage.disks[0] = OfflineDisk
|
|
|
|
}
|
|
|
|
file, err = storage.CreateFile(bytes.NewReader(data[test.offset:]), "testbucket", "object2", buffer, test.algorithm, test.dataBlocks+1)
|
|
|
|
if err != nil && !test.shouldFailQuorum {
|
|
|
|
t.Errorf("Test %d: should pass but failed with: %v", i, err)
|
|
|
|
}
|
|
|
|
if err == nil && test.shouldFailQuorum {
|
|
|
|
t.Errorf("Test %d: should fail but it passed", i)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
if length := int64(len(data[test.offset:])); file.Size != length {
|
|
|
|
t.Errorf("Test %d: invalid number of bytes written: got: #%d want #%d", i, file.Size, length)
|
|
|
|
}
|
2016-07-25 23:36:41 -04:00
|
|
|
}
|
|
|
|
}
|
2017-08-14 21:08:42 -04:00
|
|
|
setup.Remove()
|
2016-07-25 23:36:41 -04:00
|
|
|
}
|
|
|
|
}
|