From 78ae69674961af1abd198e66dbf59b48dae9386f Mon Sep 17 00:00:00 2001 From: Krishnan Parthasarathi Date: Tue, 21 Jun 2016 15:48:27 -0700 Subject: [PATCH] Added couple of unit-tests to xlObjects (#1950) * Added couple of units to xlObjects * Used test_utils for initialize/destroying xlObjects --- xl-v1-object_test.go | 116 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 xl-v1-object_test.go diff --git a/xl-v1-object_test.go b/xl-v1-object_test.go new file mode 100644 index 000000000..a7f3c5aa9 --- /dev/null +++ b/xl-v1-object_test.go @@ -0,0 +1,116 @@ +/* + * 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. + */ + +package main + +import ( + "bytes" + "crypto/md5" + "encoding/hex" + "testing" +) + +func failDisks(xl xlObjects, n int) (removedDisks []StorageAPI) { + removedDisks = make([]StorageAPI, 8) + copy(removedDisks, xl.storageDisks[8:]) + xl.storageDisks = xl.storageDisks[:8] + for i := 0; i < n; i++ { + xl.storageDisks = append(xl.storageDisks, nil) + } + return removedDisks +} + +func TestRenameObjectWriteQuorum(t *testing.T) { + var objLayer ObjectLayer + var disks []string + var err error + + objLayer, disks, err = getXLObjectLayer() + if err != nil { + t.Fatal(err) + } + + // cleaning up of temporary test directories + defer removeRoots(disks) + + err = objLayer.MakeBucket("bucket1") + if err != nil { + t.Fatal(err) + } + data := bytes.NewReader([]byte("hello")) + _, err = objLayer.PutObject("bucket1", "obj1", 5, data, nil) + if err != nil { + t.Fatal(err) + } + + // Simulate failure of disks + xl := objLayer.(xlObjects) + removedDisks := failDisks(xl, 8) + if err = xl.renameObject("bucket1", "obj1", ".minio", "obj1"); err != errXLWriteQuorum { + t.Fatal(err) + } + + // Restoring the failed disks back + xl.storageDisks = append(xl.storageDisks[:4], removedDisks...) + + // With all disks back online, renameObject should succeed. + if err = xl.renameObject("bucket1", "obj1", ".minio", "obj1"); err != nil { + t.Fatal(err) + } + + // ... so should renaming back (succeed). + if err = xl.renameObject(".minio", "obj1", "bucket1", "obj1"); err != nil { + t.Fatal(err) + } +} + +func TestRepeatPutObjectPart(t *testing.T) { + var objLayer ObjectLayer + var disks []string + var err error + + objLayer, disks, err = getXLObjectLayer() + if err != nil { + t.Fatal(err) + } + + // cleaning up of temporary test directories + defer removeRoots(disks) + + err = objLayer.MakeBucket("bucket1") + if err != nil { + t.Fatal(err) + } + + uploadID, err := objLayer.NewMultipartUpload("bucket1", "mpartObj1", nil) + if err != nil { + t.Fatal(err) + } + fiveMBBytes := bytes.Repeat([]byte("a"), 5*1024*124) + md5Writer := md5.New() + md5Writer.Write(fiveMBBytes) + md5Hex := hex.EncodeToString(md5Writer.Sum(nil)) + _, err = objLayer.PutObjectPart("bucket1", "mpartObj1", uploadID, 1, 5*1024*1024, bytes.NewReader(fiveMBBytes), md5Hex) + if err != nil { + t.Fatal(err) + } + // PutObjectPart should succeed even if part already exists. ref: https://github.com/minio/minio/issues/1930 + _, err = objLayer.PutObjectPart("bucket1", "mpartObj1", uploadID, 1, 5*1024*1024, bytes.NewReader(fiveMBBytes), md5Hex) + if err != nil { + t.Fatal(err) + } + +}