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.
1769 lines
58 KiB
1769 lines
58 KiB
9 years ago
|
/*
|
||
5 years ago
|
* MinIO Cloud Storage, (C) 2016-2020 MinIO, Inc.
|
||
9 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
|
||
9 years ago
|
|
||
|
import (
|
||
8 years ago
|
"bytes"
|
||
7 years ago
|
"crypto/rand"
|
||
7 years ago
|
"fmt"
|
||
8 years ago
|
"io"
|
||
9 years ago
|
"io/ioutil"
|
||
|
"os"
|
||
9 years ago
|
slashpath "path"
|
||
|
"runtime"
|
||
8 years ago
|
"strings"
|
||
9 years ago
|
"syscall"
|
||
9 years ago
|
"testing"
|
||
8 years ago
|
|
||
7 years ago
|
"github.com/minio/minio/pkg/disk"
|
||
9 years ago
|
)
|
||
|
|
||
5 years ago
|
func TestCheckPathLength(t *testing.T) {
|
||
|
// Check path length restrictions are not same on windows/darwin
|
||
|
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
|
||
|
t.Skip()
|
||
|
}
|
||
|
|
||
|
testCases := []struct {
|
||
|
path string
|
||
|
expectedErr error
|
||
|
}{
|
||
|
{".", errFileAccessDenied},
|
||
|
{"/", errFileAccessDenied},
|
||
|
{"..", errFileAccessDenied},
|
||
|
{"data/G_792/srv-tse/c/users/denis/documents/gestion!20locative/heritier/propri!E9taire/20190101_a2.03!20-!20m.!20heritier!20re!B4mi!20-!20proce!60s-verbal!20de!20livraison!20et!20de!20remise!20des!20cle!B4s!20acque!B4reurs!20-!204-!20livraison!20-!20lp!20promotion!20toulouse!20-!20encre!20et!20plume!20-!205!20de!B4c.!202019!20a!60!2012-49.pdf.ecc", errFileNameTooLong},
|
||
|
{"data/G_792/srv-tse/c/users/denis/documents/gestionlocative.txt", nil},
|
||
|
}
|
||
|
|
||
|
for _, testCase := range testCases {
|
||
|
gotErr := checkPathLength(testCase.path)
|
||
|
t.Run("", func(t *testing.T) {
|
||
|
if gotErr != testCase.expectedErr {
|
||
|
t.Errorf("Expected %s, got %s", testCase.expectedErr, gotErr)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
// Tests validate volume name.
|
||
|
func TestIsValidVolname(t *testing.T) {
|
||
|
testCases := []struct {
|
||
|
volName string
|
||
|
shouldPass bool
|
||
|
}{
|
||
|
// Cases which should pass the test.
|
||
|
// passing in valid bucket names.
|
||
|
{"lol", true},
|
||
|
{"1-this-is-valid", true},
|
||
|
{"1-this-too-is-valid-1", true},
|
||
|
{"this.works.too.1", true},
|
||
|
{"1234567", true},
|
||
|
{"123", true},
|
||
|
{"s3-eu-west-1.amazonaws.com", true},
|
||
|
{"ideas-are-more-powerful-than-guns", true},
|
||
|
{"testbucket", true},
|
||
|
{"1bucket", true},
|
||
|
{"bucket1", true},
|
||
|
{"$this-is-not-valid-too", true},
|
||
|
{"contains-$-dollar", true},
|
||
|
{"contains-^-carrot", true},
|
||
|
{"contains-$-dollar", true},
|
||
|
{"contains-$-dollar", true},
|
||
|
{".starts-with-a-dot", true},
|
||
|
{"ends-with-a-dot.", true},
|
||
|
{"ends-with-a-dash-", true},
|
||
|
{"-starts-with-a-dash", true},
|
||
|
{"THIS-BEINGS-WITH-UPPERCASe", true},
|
||
|
{"tHIS-ENDS-WITH-UPPERCASE", true},
|
||
|
{"ThisBeginsAndEndsWithUpperCase", true},
|
||
|
{"una ñina", true},
|
||
|
{"lalalallalallalalalallalallalala-theString-size-is-greater-than-64", true},
|
||
|
// cases for which test should fail.
|
||
|
// passing invalid bucket names.
|
||
|
{"", false},
|
||
5 years ago
|
{SlashSeparator, false},
|
||
7 years ago
|
{"a", false},
|
||
|
{"ab", false},
|
||
|
{"ab/", true},
|
||
|
{"......", true},
|
||
|
}
|
||
|
|
||
|
for i, testCase := range testCases {
|
||
|
isValidVolname := isValidVolname(testCase.volName)
|
||
|
if testCase.shouldPass && !isValidVolname {
|
||
|
t.Errorf("Test case %d: Expected \"%s\" to be a valid bucket name", i+1, testCase.volName)
|
||
|
}
|
||
|
if !testCase.shouldPass && isValidVolname {
|
||
|
t.Errorf("Test case %d: Expected bucket name \"%s\" to be invalid", i+1, testCase.volName)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// creates a temp dir and sets up xlStorage layer.
|
||
|
// returns xlStorage layer, temp dir path to be used for the purpose of tests.
|
||
|
func newXLStorageTestSetup() (*xlStorageDiskIDCheck, string, error) {
|
||
8 years ago
|
diskPath, err := ioutil.TempDir(globalTestTmpDir, "minio-")
|
||
8 years ago
|
if err != nil {
|
||
|
return nil, "", err
|
||
|
}
|
||
5 years ago
|
|
||
|
// Initialize a new xlStorage layer.
|
||
|
storage, err := newXLStorage(diskPath, "")
|
||
8 years ago
|
if err != nil {
|
||
|
return nil, "", err
|
||
|
}
|
||
5 years ago
|
err = storage.MakeVol(minioMetaBucket)
|
||
|
if err != nil {
|
||
|
return nil, "", err
|
||
|
}
|
||
|
// Create a sample format.json file
|
||
|
err = storage.WriteAll(minioMetaBucket, formatConfigFile, bytes.NewBufferString(`{"version":"1","format":"xl","id":"592a41c2-b7cc-4130-b883-c4b5cb15965b","xl":{"version":"3","this":"da017d62-70e3-45f1-8a1a-587707e69ad1","sets":[["e07285a6-8c73-4962-89c6-047fb939f803","33b8d431-482d-4376-b63c-626d229f0a29","cff6513a-4439-4dc1-bcaa-56c9e880c352","da017d62-70e3-45f1-8a1a-587707e69ad1","9c9f21d5-1f15-4737-bce6-835faa0d9626","0a59b346-1424-4fc2-9fa2-a2e80541d0c1","7924a3dc-b69a-4971-9a2e-014966d6aebb","4d2b8dd9-4e48-444b-bdca-c89194b26042"]],"distributionAlgo":"CRCMOD"}}`))
|
||
|
if err != nil {
|
||
|
return nil, "", err
|
||
|
}
|
||
5 years ago
|
return &xlStorageDiskIDCheck{storage: storage, diskID: "da017d62-70e3-45f1-8a1a-587707e69ad1"}, diskPath, nil
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
// createPermDeniedFile - creates temporary directory and file with path '/mybucket/myobject'
|
||
|
func createPermDeniedFile(t *testing.T) (permDeniedDir string) {
|
||
|
var errMsg string
|
||
|
|
||
|
defer func() {
|
||
|
if errMsg == "" {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if permDeniedDir != "" {
|
||
|
os.RemoveAll(permDeniedDir)
|
||
|
}
|
||
|
|
||
|
t.Fatalf(errMsg)
|
||
|
}()
|
||
|
|
||
|
var err error
|
||
|
if permDeniedDir, err = ioutil.TempDir(globalTestTmpDir, "minio-"); err != nil {
|
||
|
errMsg = fmt.Sprintf("Unable to create temporary directory. %v", err)
|
||
|
return permDeniedDir
|
||
|
}
|
||
|
|
||
|
if err = os.Mkdir(slashpath.Join(permDeniedDir, "mybucket"), 0775); err != nil {
|
||
|
errMsg = fmt.Sprintf("Unable to create temporary directory %v. %v", slashpath.Join(permDeniedDir, "mybucket"), err)
|
||
|
return permDeniedDir
|
||
|
}
|
||
|
|
||
|
if err = ioutil.WriteFile(slashpath.Join(permDeniedDir, "mybucket", "myobject"), []byte(""), 0400); err != nil {
|
||
|
errMsg = fmt.Sprintf("Unable to create file %v. %v", slashpath.Join(permDeniedDir, "mybucket", "myobject"), err)
|
||
|
return permDeniedDir
|
||
|
}
|
||
|
|
||
|
if err = os.Chmod(slashpath.Join(permDeniedDir, "mybucket"), 0400); err != nil {
|
||
|
errMsg = fmt.Sprintf("Unable to change permission to temporary directory %v. %v", slashpath.Join(permDeniedDir, "mybucket"), err)
|
||
|
return permDeniedDir
|
||
|
}
|
||
|
|
||
|
if err = os.Chmod(permDeniedDir, 0400); err != nil {
|
||
|
errMsg = fmt.Sprintf("Unable to change permission to temporary directory %v. %v", permDeniedDir, err)
|
||
|
}
|
||
|
|
||
|
return permDeniedDir
|
||
|
}
|
||
|
|
||
|
// removePermDeniedFile - removes temporary directory and file with path '/mybucket/myobject'
|
||
|
func removePermDeniedFile(permDeniedDir string) {
|
||
|
if err := os.Chmod(permDeniedDir, 0775); err == nil {
|
||
|
if err = os.Chmod(slashpath.Join(permDeniedDir, "mybucket"), 0775); err == nil {
|
||
|
os.RemoveAll(permDeniedDir)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorages xlStorage.getDiskInfo()
|
||
|
func TestXLStorageGetDiskInfo(t *testing.T) {
|
||
8 years ago
|
path, err := ioutil.TempDir(globalTestTmpDir, "minio-")
|
||
9 years ago
|
if err != nil {
|
||
|
t.Fatalf("Unable to create a temporary directory, %s", err)
|
||
|
}
|
||
7 years ago
|
defer os.RemoveAll(path)
|
||
9 years ago
|
|
||
|
testCases := []struct {
|
||
|
diskPath string
|
||
|
expectedErr error
|
||
|
}{
|
||
|
{path, nil},
|
||
|
{"/nonexistent-dir", errDiskNotFound},
|
||
|
}
|
||
|
|
||
|
// Check test cases.
|
||
|
for _, testCase := range testCases {
|
||
|
if _, err := getDiskInfo(testCase.diskPath); err != testCase.expectedErr {
|
||
|
t.Fatalf("expected: %s, got: %s", testCase.expectedErr, err)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
func TestXLStorageIsDirEmpty(t *testing.T) {
|
||
7 years ago
|
tmp, err := ioutil.TempDir(globalTestTmpDir, "minio-")
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
7 years ago
|
defer os.RemoveAll(tmp)
|
||
7 years ago
|
|
||
|
// Should give false on non-existent directory.
|
||
|
dir1 := slashpath.Join(tmp, "non-existent-directory")
|
||
6 years ago
|
if isDirEmpty(dir1) {
|
||
7 years ago
|
t.Error("expected false for non-existent directory, got true")
|
||
|
}
|
||
|
|
||
|
// Should give false for not-a-directory.
|
||
|
dir2 := slashpath.Join(tmp, "file")
|
||
|
err = ioutil.WriteFile(dir2, []byte("hello"), 0777)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
6 years ago
|
if isDirEmpty(dir2) {
|
||
7 years ago
|
t.Error("expected false for a file, got true")
|
||
|
}
|
||
|
|
||
|
// Should give true for a real empty directory.
|
||
|
dir3 := slashpath.Join(tmp, "empty")
|
||
|
err = os.Mkdir(dir3, 0777)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
6 years ago
|
if !isDirEmpty(dir3) {
|
||
7 years ago
|
t.Error("expected true for empty dir, got false")
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorageReadAll - TestXLStorages the functionality implemented by xlStorage ReadAll storage API.
|
||
|
func TestXLStorageReadAll(t *testing.T) {
|
||
|
// create xlStorage test setup
|
||
|
xlStorage, path, err := newXLStorageTestSetup()
|
||
9 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to create xlStorage test setup, %s", err)
|
||
9 years ago
|
}
|
||
|
|
||
7 years ago
|
defer os.RemoveAll(path)
|
||
9 years ago
|
|
||
|
// Create files for the test cases.
|
||
5 years ago
|
if err = xlStorage.MakeVol("exists"); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create a volume \"exists\", %s", err)
|
||
|
}
|
||
5 years ago
|
if err = xlStorage.AppendFile("exists", "as-directory/as-file", []byte("Hello, World")); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create a file \"as-directory/as-file\", %s", err)
|
||
|
}
|
||
5 years ago
|
if err = xlStorage.AppendFile("exists", "as-file", []byte("Hello, World")); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create a file \"as-file\", %s", err)
|
||
|
}
|
||
5 years ago
|
if err = xlStorage.AppendFile("exists", "as-file-parent", []byte("Hello, World")); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create a file \"as-file-parent\", %s", err)
|
||
|
}
|
||
9 years ago
|
|
||
5 years ago
|
// TestXLStoragecases to validate different conditions for ReadAll API.
|
||
9 years ago
|
testCases := []struct {
|
||
|
volume string
|
||
|
path string
|
||
|
err error
|
||
|
}{
|
||
5 years ago
|
// TestXLStorage case - 1.
|
||
9 years ago
|
// Validate volume does not exist.
|
||
|
{
|
||
8 years ago
|
volume: "i-dont-exist",
|
||
|
path: "",
|
||
|
err: errVolumeNotFound,
|
||
9 years ago
|
},
|
||
5 years ago
|
// TestXLStorage case - 2.
|
||
9 years ago
|
// Validate bad condition file does not exist.
|
||
|
{
|
||
8 years ago
|
volume: "exists",
|
||
|
path: "as-file-not-found",
|
||
|
err: errFileNotFound,
|
||
9 years ago
|
},
|
||
5 years ago
|
// TestXLStorage case - 3.
|
||
9 years ago
|
// Validate bad condition file exists as prefix/directory and
|
||
|
// we are attempting to read it.
|
||
|
{
|
||
8 years ago
|
volume: "exists",
|
||
|
path: "as-directory",
|
||
|
err: errFileNotFound,
|
||
9 years ago
|
},
|
||
5 years ago
|
// TestXLStorage case - 4.
|
||
9 years ago
|
{
|
||
8 years ago
|
volume: "exists",
|
||
|
path: "as-file-parent/as-file",
|
||
|
err: errFileNotFound,
|
||
9 years ago
|
},
|
||
5 years ago
|
// TestXLStorage case - 5.
|
||
9 years ago
|
// Validate the good condition file exists and we are able to read it.
|
||
9 years ago
|
{
|
||
8 years ago
|
volume: "exists",
|
||
|
path: "as-file",
|
||
|
err: nil,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 6.
|
||
|
// TestXLStorage case with invalid volume name.
|
||
8 years ago
|
{
|
||
|
volume: "ab",
|
||
|
path: "as-file",
|
||
7 years ago
|
err: errVolumeNotFound,
|
||
9 years ago
|
},
|
||
|
}
|
||
|
|
||
8 years ago
|
var dataRead []byte
|
||
9 years ago
|
// Run through all the test cases and validate for ReadAll.
|
||
|
for i, testCase := range testCases {
|
||
5 years ago
|
dataRead, err = xlStorage.ReadAll(testCase.volume, testCase.path)
|
||
9 years ago
|
if err != testCase.err {
|
||
5 years ago
|
t.Fatalf("TestXLStorage %d: Expected err \"%s\", got err \"%s\"", i+1, testCase.err, err)
|
||
9 years ago
|
}
|
||
8 years ago
|
if err == nil {
|
||
|
if string(dataRead) != string([]byte("Hello, World")) {
|
||
5 years ago
|
t.Errorf("TestXLStorage %d: Expected the data read to be \"%s\", but instead got \"%s\"", i+1, "Hello, World", string(dataRead))
|
||
8 years ago
|
}
|
||
|
}
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
5 years ago
|
// TestNewXLStorage all the cases handled in xlStorage storage layer initialization.
|
||
|
func TestNewXLStorage(t *testing.T) {
|
||
9 years ago
|
// Temporary dir name.
|
||
5 years ago
|
tmpDirName := globalTestTmpDir + SlashSeparator + "minio-" + nextSuffix()
|
||
9 years ago
|
// Temporary file name.
|
||
5 years ago
|
tmpFileName := globalTestTmpDir + SlashSeparator + "minio-" + nextSuffix()
|
||
9 years ago
|
f, _ := os.Create(tmpFileName)
|
||
|
f.Close()
|
||
|
defer os.Remove(tmpFileName)
|
||
|
|
||
5 years ago
|
// List of all tests for xlStorage initialization.
|
||
9 years ago
|
testCases := []struct {
|
||
8 years ago
|
name string
|
||
|
err error
|
||
9 years ago
|
}{
|
||
|
// Validates input argument cannot be empty.
|
||
|
{
|
||
|
"",
|
||
|
errInvalidArgument,
|
||
|
},
|
||
|
// Validates if the directory does not exist and
|
||
|
// gets automatically created.
|
||
|
{
|
||
|
tmpDirName,
|
||
|
nil,
|
||
|
},
|
||
|
// Validates if the disk exists as file and returns error
|
||
|
// not a directory.
|
||
|
{
|
||
|
tmpFileName,
|
||
5 years ago
|
errDiskNotDir,
|
||
9 years ago
|
},
|
||
|
}
|
||
|
|
||
|
// Validate all test cases.
|
||
|
for i, testCase := range testCases {
|
||
5 years ago
|
// Initialize a new xlStorage layer.
|
||
|
_, err := newXLStorage(testCase.name, "")
|
||
9 years ago
|
if err != testCase.err {
|
||
5 years ago
|
t.Fatalf("TestXLStorage %d failed wanted: %s, got: %s", i+1, err, testCase.err)
|
||
9 years ago
|
}
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
5 years ago
|
// TestXLStorageMakeVol - TestXLStorage validate the logic for creation of new xlStorage volume.
|
||
8 years ago
|
// Asserts the failures too against the expected failures.
|
||
5 years ago
|
func TestXLStorageMakeVol(t *testing.T) {
|
||
|
// create xlStorage test setup
|
||
|
xlStorage, path, err := newXLStorageTestSetup()
|
||
9 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to create xlStorage test setup, %s", err)
|
||
9 years ago
|
}
|
||
7 years ago
|
defer os.RemoveAll(path)
|
||
9 years ago
|
|
||
|
// Setup test environment.
|
||
|
// Create a file.
|
||
|
if err := ioutil.WriteFile(slashpath.Join(path, "vol-as-file"), []byte{}, os.ModePerm); err != nil {
|
||
|
t.Fatalf("Unable to create file, %s", err)
|
||
|
}
|
||
|
// Create a directory.
|
||
|
if err := os.Mkdir(slashpath.Join(path, "existing-vol"), 0777); err != nil {
|
||
|
t.Fatalf("Unable to create directory, %s", err)
|
||
|
}
|
||
|
|
||
|
testCases := []struct {
|
||
|
volName string
|
||
|
expectedErr error
|
||
|
}{
|
||
5 years ago
|
// TestXLStorage case - 1.
|
||
8 years ago
|
// A valid case, volume creation is expected to succeed.
|
||
|
{
|
||
|
volName: "success-vol",
|
||
|
expectedErr: nil,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 2.
|
||
8 years ago
|
// Case where a file exists by the name of the volume to be created.
|
||
|
{
|
||
|
volName: "vol-as-file",
|
||
|
expectedErr: errVolumeExists,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 3.
|
||
8 years ago
|
{
|
||
|
volName: "existing-vol",
|
||
|
expectedErr: errVolumeExists,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 5.
|
||
|
// TestXLStorage case with invalid volume name.
|
||
8 years ago
|
{
|
||
|
volName: "ab",
|
||
|
expectedErr: errInvalidArgument,
|
||
|
},
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
for i, testCase := range testCases {
|
||
5 years ago
|
if err := xlStorage.MakeVol(testCase.volName); err != testCase.expectedErr {
|
||
|
t.Fatalf("TestXLStorage %d: Expected: \"%s\", got: \"%s\"", i+1, testCase.expectedErr, err)
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorage for permission denied.
|
||
8 years ago
|
if runtime.GOOS != globalWindowsOSName {
|
||
7 years ago
|
permDeniedDir, err := ioutil.TempDir(globalTestTmpDir, "minio-")
|
||
|
if err != nil {
|
||
|
t.Fatalf("Unable to create temporary directory. %v", err)
|
||
|
}
|
||
|
defer os.RemoveAll(permDeniedDir)
|
||
|
if err = os.Chmod(permDeniedDir, 0400); err != nil {
|
||
|
t.Fatalf("Unable to change permission to temporary directory %v. %v", permDeniedDir, err)
|
||
|
}
|
||
7 years ago
|
|
||
5 years ago
|
// Initialize xlStorage storage layer for permission denied error.
|
||
|
_, err = newXLStorage(permDeniedDir, "")
|
||
7 years ago
|
if err != nil && !os.IsPermission(err) {
|
||
5 years ago
|
t.Fatalf("Unable to initialize xlStorage, %s", err)
|
||
7 years ago
|
}
|
||
|
|
||
|
if err = os.Chmod(permDeniedDir, 0755); err != nil {
|
||
|
t.Fatalf("Unable to change permission to temporary directory %v. %v", permDeniedDir, err)
|
||
|
}
|
||
|
|
||
5 years ago
|
xlStorageNew, err := newXLStorage(permDeniedDir, "")
|
||
9 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to initialize xlStorage, %s", err)
|
||
9 years ago
|
}
|
||
|
|
||
7 years ago
|
// change backend permissions for MakeVol error.
|
||
|
if err = os.Chmod(permDeniedDir, 0400); err != nil {
|
||
|
t.Fatalf("Unable to change permission to temporary directory %v. %v", permDeniedDir, err)
|
||
|
}
|
||
|
|
||
5 years ago
|
if err := xlStorageNew.MakeVol("test-vol"); err != errDiskAccessDenied {
|
||
9 years ago
|
t.Fatalf("expected: %s, got: %s", errDiskAccessDenied, err)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorageDeleteVol - Validates the expected behavior of xlStorage.DeleteVol for various cases.
|
||
|
func TestXLStorageDeleteVol(t *testing.T) {
|
||
|
// create xlStorage test setup
|
||
|
xlStorage, path, err := newXLStorageTestSetup()
|
||
9 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to create xlStorage test setup, %s", err)
|
||
9 years ago
|
}
|
||
7 years ago
|
defer os.RemoveAll(path)
|
||
9 years ago
|
|
||
|
// Setup test environment.
|
||
5 years ago
|
if err = xlStorage.MakeVol("success-vol"); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create volume, %s", err)
|
||
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorage failure cases.
|
||
9 years ago
|
vol := slashpath.Join(path, "nonempty-vol")
|
||
8 years ago
|
if err = os.Mkdir(vol, 0777); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create directory, %s", err)
|
||
|
}
|
||
8 years ago
|
if err = ioutil.WriteFile(slashpath.Join(vol, "test-file"), []byte{}, os.ModePerm); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create file, %s", err)
|
||
|
}
|
||
|
|
||
|
testCases := []struct {
|
||
|
volName string
|
||
|
expectedErr error
|
||
|
}{
|
||
5 years ago
|
// TestXLStorage case - 1.
|
||
8 years ago
|
// A valida case. Empty vol, should be possible to delete.
|
||
|
{
|
||
|
volName: "success-vol",
|
||
|
expectedErr: nil,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 2.
|
||
8 years ago
|
// volume is non-existent.
|
||
|
{
|
||
|
volName: "nonexistent-vol",
|
||
|
expectedErr: errVolumeNotFound,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 3.
|
||
8 years ago
|
// It shouldn't be possible to delete an non-empty volume, validating the same.
|
||
|
{
|
||
|
volName: "nonempty-vol",
|
||
|
expectedErr: errVolumeNotEmpty,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 5.
|
||
8 years ago
|
// Invalid volume name.
|
||
|
{
|
||
|
volName: "ab",
|
||
7 years ago
|
expectedErr: errVolumeNotFound,
|
||
8 years ago
|
},
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
for i, testCase := range testCases {
|
||
5 years ago
|
if err = xlStorage.DeleteVol(testCase.volName, false); err != testCase.expectedErr {
|
||
|
t.Fatalf("TestXLStorage: %d, expected: %s, got: %s", i+1, testCase.expectedErr, err)
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorage for permission denied.
|
||
8 years ago
|
if runtime.GOOS != globalWindowsOSName {
|
||
7 years ago
|
var permDeniedDir string
|
||
|
if permDeniedDir, err = ioutil.TempDir(globalTestTmpDir, "minio-"); err != nil {
|
||
|
t.Fatalf("Unable to create temporary directory. %v", err)
|
||
|
}
|
||
|
defer removePermDeniedFile(permDeniedDir)
|
||
|
if err = os.Mkdir(slashpath.Join(permDeniedDir, "mybucket"), 0400); err != nil {
|
||
|
t.Fatalf("Unable to create temporary directory %v. %v", slashpath.Join(permDeniedDir, "mybucket"), err)
|
||
|
}
|
||
|
if err = os.Chmod(permDeniedDir, 0400); err != nil {
|
||
|
t.Fatalf("Unable to change permission to temporary directory %v. %v", permDeniedDir, err)
|
||
|
}
|
||
7 years ago
|
|
||
5 years ago
|
// Initialize xlStorage storage layer for permission denied error.
|
||
|
_, err = newXLStorage(permDeniedDir, "")
|
||
7 years ago
|
if err != nil && !os.IsPermission(err) {
|
||
5 years ago
|
t.Fatalf("Unable to initialize xlStorage, %s", err)
|
||
7 years ago
|
}
|
||
|
|
||
|
if err = os.Chmod(permDeniedDir, 0755); err != nil {
|
||
|
t.Fatalf("Unable to change permission to temporary directory %v. %v", permDeniedDir, err)
|
||
|
}
|
||
|
|
||
5 years ago
|
xlStorageNew, err := newXLStorage(permDeniedDir, "")
|
||
9 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to initialize xlStorage, %s", err)
|
||
9 years ago
|
}
|
||
|
|
||
7 years ago
|
// change backend permissions for MakeVol error.
|
||
|
if err = os.Chmod(permDeniedDir, 0400); err != nil {
|
||
|
t.Fatalf("Unable to change permission to temporary directory %v. %v", permDeniedDir, err)
|
||
|
}
|
||
|
|
||
5 years ago
|
if err = xlStorageNew.DeleteVol("mybucket", false); err != errDiskAccessDenied {
|
||
9 years ago
|
t.Fatalf("expected: Permission error, got: %s", err)
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
5 years ago
|
xlStorageDeletedStorage, diskPath, err := newXLStorageTestSetup()
|
||
8 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to create xlStorage test setup, %s", err)
|
||
8 years ago
|
}
|
||
|
// removing the disk, used to recreate disk not found error.
|
||
7 years ago
|
os.RemoveAll(diskPath)
|
||
8 years ago
|
|
||
5 years ago
|
// TestXLStorage for delete on an removed disk.
|
||
8 years ago
|
// should fail with disk not found.
|
||
5 years ago
|
err = xlStorageDeletedStorage.DeleteVol("Del-Vol", false)
|
||
8 years ago
|
if err != errDiskNotFound {
|
||
|
t.Errorf("Expected: \"Disk not found\", got \"%s\"", err)
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorageStatVol - TestXLStorages validate the volume info returned by xlStorage.StatVol() for various inputs.
|
||
|
func TestXLStorageStatVol(t *testing.T) {
|
||
|
// create xlStorage test setup
|
||
|
xlStorage, path, err := newXLStorageTestSetup()
|
||
9 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to create xlStorage test setup, %s", err)
|
||
9 years ago
|
}
|
||
7 years ago
|
defer os.RemoveAll(path)
|
||
9 years ago
|
|
||
|
// Setup test environment.
|
||
5 years ago
|
if err = xlStorage.MakeVol("success-vol"); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create volume, %s", err)
|
||
|
}
|
||
|
|
||
|
testCases := []struct {
|
||
|
volName string
|
||
|
expectedErr error
|
||
|
}{
|
||
5 years ago
|
// TestXLStorage case - 1.
|
||
8 years ago
|
{
|
||
|
volName: "success-vol",
|
||
|
expectedErr: nil,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 2.
|
||
8 years ago
|
{
|
||
|
volName: "nonexistent-vol",
|
||
|
expectedErr: errVolumeNotFound,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 3.
|
||
8 years ago
|
{
|
||
|
volName: "ab",
|
||
7 years ago
|
expectedErr: errVolumeNotFound,
|
||
8 years ago
|
},
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
for i, testCase := range testCases {
|
||
5 years ago
|
var volInfo VolInfo
|
||
|
volInfo, err = xlStorage.StatVol(testCase.volName)
|
||
8 years ago
|
if err != testCase.expectedErr {
|
||
5 years ago
|
t.Fatalf("TestXLStorage case : %d, Expected: \"%s\", got: \"%s\"", i+1, testCase.expectedErr, err)
|
||
8 years ago
|
}
|
||
|
|
||
|
if err == nil {
|
||
5 years ago
|
if volInfo.Name != testCase.volName {
|
||
5 years ago
|
t.Errorf("TestXLStorage case %d: Expected the volume name to be \"%s\", instead found \"%s\"",
|
||
|
i+1, volInfo.Name, testCase.volName)
|
||
8 years ago
|
}
|
||
9 years ago
|
}
|
||
|
}
|
||
8 years ago
|
|
||
5 years ago
|
xlStorageDeletedStorage, diskPath, err := newXLStorageTestSetup()
|
||
8 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to create xlStorage test setup, %s", err)
|
||
8 years ago
|
}
|
||
|
// removing the disk, used to recreate disk not found error.
|
||
7 years ago
|
os.RemoveAll(diskPath)
|
||
8 years ago
|
|
||
5 years ago
|
// TestXLStorage for delete on an removed disk.
|
||
8 years ago
|
// should fail with disk not found.
|
||
5 years ago
|
_, err = xlStorageDeletedStorage.StatVol("Stat vol")
|
||
8 years ago
|
if err != errDiskNotFound {
|
||
|
t.Errorf("Expected: \"Disk not found\", got \"%s\"", err)
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorageListVols - Validates the result and the error output for xlStorage volume listing functionality xlStorage.ListVols().
|
||
|
func TestXLStorageListVols(t *testing.T) {
|
||
|
// create xlStorage test setup
|
||
|
xlStorage, path, err := newXLStorageTestSetup()
|
||
9 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to create xlStorage test setup, %s", err)
|
||
9 years ago
|
}
|
||
|
|
||
5 years ago
|
var volInfos []VolInfo
|
||
5 years ago
|
// TestXLStorage empty list vols.
|
||
|
if volInfos, err = xlStorage.ListVols(); err != nil {
|
||
9 years ago
|
t.Fatalf("expected: <nil>, got: %s", err)
|
||
5 years ago
|
} else if len(volInfos) != 1 {
|
||
|
t.Fatalf("expected: one entry, got: %s", volInfos)
|
||
9 years ago
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorage non-empty list vols.
|
||
|
if err = xlStorage.MakeVol("success-vol"); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create volume, %s", err)
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
volInfos, err = xlStorage.ListVols()
|
||
5 years ago
|
if err != nil {
|
||
9 years ago
|
t.Fatalf("expected: <nil>, got: %s", err)
|
||
8 years ago
|
}
|
||
5 years ago
|
if len(volInfos) != 2 {
|
||
|
t.Fatalf("expected: 2, got: %d", len(volInfos))
|
||
|
}
|
||
|
volFound := false
|
||
|
for _, info := range volInfos {
|
||
|
if info.Name == "success-vol" {
|
||
|
volFound = true
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
if !volFound {
|
||
|
t.Errorf("expected: success-vol to be created")
|
||
|
}
|
||
|
|
||
8 years ago
|
// removing the path and simulating disk failure
|
||
7 years ago
|
os.RemoveAll(path)
|
||
8 years ago
|
// should fail with errDiskNotFound.
|
||
5 years ago
|
if _, err = xlStorage.ListVols(); err != errDiskNotFound {
|
||
8 years ago
|
t.Errorf("Expected to fail with \"%s\", but instead failed with \"%s\"", errDiskNotFound, err)
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorageXlStorageListDir - TestXLStorages validate the directory listing functionality provided by xlStorage.ListDir .
|
||
|
func TestXLStorageXlStorageListDir(t *testing.T) {
|
||
|
// create xlStorage test setup
|
||
|
xlStorage, path, err := newXLStorageTestSetup()
|
||
8 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to create xlStorage test setup, %s", err)
|
||
8 years ago
|
}
|
||
7 years ago
|
defer os.RemoveAll(path)
|
||
8 years ago
|
|
||
5 years ago
|
// create xlStorage test setup.
|
||
|
xlStorageDeletedStorage, diskPath, err := newXLStorageTestSetup()
|
||
8 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to create xlStorage test setup, %s", err)
|
||
8 years ago
|
}
|
||
|
// removing the disk, used to recreate disk not found error.
|
||
7 years ago
|
os.RemoveAll(diskPath)
|
||
8 years ago
|
// Setup test environment.
|
||
5 years ago
|
if err = xlStorage.MakeVol("success-vol"); err != nil {
|
||
8 years ago
|
t.Fatalf("Unable to create volume, %s", err)
|
||
|
}
|
||
5 years ago
|
if err = xlStorage.AppendFile("success-vol", "abc/def/ghi/success-file", []byte("Hello, world")); err != nil {
|
||
8 years ago
|
t.Fatalf("Unable to create file, %s", err)
|
||
|
}
|
||
5 years ago
|
if err = xlStorage.AppendFile("success-vol", "abc/xyz/ghi/success-file", []byte("Hello, world")); err != nil {
|
||
8 years ago
|
t.Fatalf("Unable to create file, %s", err)
|
||
|
}
|
||
|
|
||
|
testCases := []struct {
|
||
5 years ago
|
srcVol string
|
||
|
srcPath string
|
||
8 years ago
|
// expected result.
|
||
|
expectedListDir []string
|
||
|
expectedErr error
|
||
|
}{
|
||
5 years ago
|
// TestXLStorage case - 1.
|
||
8 years ago
|
// valid case with existing volume and file to delete.
|
||
|
{
|
||
|
srcVol: "success-vol",
|
||
|
srcPath: "abc",
|
||
|
expectedListDir: []string{"def/", "xyz/"},
|
||
|
expectedErr: nil,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 1.
|
||
8 years ago
|
// valid case with existing volume and file to delete.
|
||
|
{
|
||
|
srcVol: "success-vol",
|
||
|
srcPath: "abc/def",
|
||
|
expectedListDir: []string{"ghi/"},
|
||
|
expectedErr: nil,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 1.
|
||
8 years ago
|
// valid case with existing volume and file to delete.
|
||
|
{
|
||
|
srcVol: "success-vol",
|
||
|
srcPath: "abc/def/ghi",
|
||
|
expectedListDir: []string{"success-file"},
|
||
|
expectedErr: nil,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 2.
|
||
8 years ago
|
{
|
||
|
srcVol: "success-vol",
|
||
|
srcPath: "abcdef",
|
||
|
expectedErr: errFileNotFound,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 3.
|
||
|
// TestXLStorage case with invalid volume name.
|
||
8 years ago
|
{
|
||
|
srcVol: "ab",
|
||
|
srcPath: "success-file",
|
||
7 years ago
|
expectedErr: errVolumeNotFound,
|
||
8 years ago
|
},
|
||
5 years ago
|
// TestXLStorage case - 4.
|
||
|
// TestXLStorage case with non existent volume.
|
||
8 years ago
|
{
|
||
|
srcVol: "non-existent-vol",
|
||
|
srcPath: "success-file",
|
||
|
expectedErr: errVolumeNotFound,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for i, testCase := range testCases {
|
||
|
var dirList []string
|
||
5 years ago
|
dirList, err = xlStorage.ListDir(testCase.srcVol, testCase.srcPath, -1)
|
||
8 years ago
|
if err != testCase.expectedErr {
|
||
5 years ago
|
t.Fatalf("TestXLStorage case %d: Expected: \"%s\", got: \"%s\"", i+1, testCase.expectedErr, err)
|
||
8 years ago
|
}
|
||
|
if err == nil {
|
||
|
for _, expected := range testCase.expectedListDir {
|
||
|
if !strings.Contains(strings.Join(dirList, ","), expected) {
|
||
5 years ago
|
t.Errorf("TestXLStorage case %d: Expected the directory listing to be \"%v\", but got \"%v\"", i+1, testCase.expectedListDir, dirList)
|
||
8 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorage for permission denied.
|
||
8 years ago
|
if runtime.GOOS != globalWindowsOSName {
|
||
7 years ago
|
permDeniedDir := createPermDeniedFile(t)
|
||
|
defer removePermDeniedFile(permDeniedDir)
|
||
|
|
||
5 years ago
|
// Initialize xlStorage storage layer for permission denied error.
|
||
|
_, err = newXLStorage(permDeniedDir, "")
|
||
7 years ago
|
if err != nil && !os.IsPermission(err) {
|
||
5 years ago
|
t.Fatalf("Unable to initialize xlStorage, %s", err)
|
||
7 years ago
|
}
|
||
|
|
||
|
if err = os.Chmod(permDeniedDir, 0755); err != nil {
|
||
|
t.Fatalf("Unable to change permission to temporary directory %v. %v", permDeniedDir, err)
|
||
|
}
|
||
|
|
||
5 years ago
|
xlStorageNew, err := newXLStorage(permDeniedDir, "")
|
||
8 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to initialize xlStorage, %s", err)
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
if err = xlStorageNew.DeleteFile("mybucket", "myobject"); err != errFileAccessDenied {
|
||
7 years ago
|
t.Errorf("expected: %s, got: %s", errFileAccessDenied, err)
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorage for delete on an removed disk.
|
||
8 years ago
|
// should fail with disk not found.
|
||
5 years ago
|
err = xlStorageDeletedStorage.DeleteFile("del-vol", "my-file")
|
||
8 years ago
|
if err != errDiskNotFound {
|
||
|
t.Errorf("Expected: \"Disk not found\", got \"%s\"", err)
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorageDeleteFile - Series of test cases construct valid and invalid input data and validates the result and the error response.
|
||
|
func TestXLStorageDeleteFile(t *testing.T) {
|
||
|
// create xlStorage test setup
|
||
|
xlStorage, path, err := newXLStorageTestSetup()
|
||
9 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to create xlStorage test setup, %s", err)
|
||
9 years ago
|
}
|
||
7 years ago
|
defer os.RemoveAll(path)
|
||
9 years ago
|
|
||
5 years ago
|
// create xlStorage test setup
|
||
|
xlStorageDeletedStorage, diskPath, err := newXLStorageTestSetup()
|
||
8 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to create xlStorage test setup, %s", err)
|
||
8 years ago
|
}
|
||
|
// removing the disk, used to recreate disk not found error.
|
||
7 years ago
|
os.RemoveAll(diskPath)
|
||
9 years ago
|
// Setup test environment.
|
||
5 years ago
|
if err = xlStorage.MakeVol("success-vol"); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create volume, %s", err)
|
||
|
}
|
||
5 years ago
|
if err = xlStorage.AppendFile("success-vol", "success-file", []byte("Hello, world")); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create file, %s", err)
|
||
|
}
|
||
|
|
||
5 years ago
|
if err = xlStorage.MakeVol("no-permissions"); err != nil {
|
||
7 years ago
|
t.Fatalf("Unable to create volume, %s", err.Error())
|
||
|
}
|
||
5 years ago
|
if err = xlStorage.AppendFile("no-permissions", "dir/file", []byte("Hello, world")); err != nil {
|
||
7 years ago
|
t.Fatalf("Unable to create file, %s", err.Error())
|
||
|
}
|
||
|
// Parent directory must have write permissions, this is read + execute.
|
||
|
if err = os.Chmod(pathJoin(path, "no-permissions"), 0555); err != nil {
|
||
|
t.Fatalf("Unable to chmod directory, %s", err.Error())
|
||
|
}
|
||
|
|
||
9 years ago
|
testCases := []struct {
|
||
8 years ago
|
srcVol string
|
||
|
srcPath string
|
||
9 years ago
|
expectedErr error
|
||
|
}{
|
||
5 years ago
|
// TestXLStorage case - 1.
|
||
8 years ago
|
// valid case with existing volume and file to delete.
|
||
|
{
|
||
|
srcVol: "success-vol",
|
||
|
srcPath: "success-file",
|
||
|
expectedErr: nil,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 2.
|
||
8 years ago
|
// The file was deleted in the last case, so DeleteFile should fail.
|
||
|
{
|
||
|
srcVol: "success-vol",
|
||
|
srcPath: "success-file",
|
||
|
expectedErr: errFileNotFound,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 3.
|
||
|
// TestXLStorage case with segment of the volume name > 255.
|
||
8 years ago
|
{
|
||
7 years ago
|
srcVol: "my",
|
||
8 years ago
|
srcPath: "success-file",
|
||
7 years ago
|
expectedErr: errVolumeNotFound,
|
||
8 years ago
|
},
|
||
5 years ago
|
// TestXLStorage case - 4.
|
||
|
// TestXLStorage case with non-existent volume.
|
||
8 years ago
|
{
|
||
|
srcVol: "non-existent-vol",
|
||
|
srcPath: "success-file",
|
||
|
expectedErr: errVolumeNotFound,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 5.
|
||
|
// TestXLStorage case with src path segment > 255.
|
||
8 years ago
|
{
|
||
|
srcVol: "success-vol",
|
||
|
srcPath: "my-obj-del-0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
|
||
|
expectedErr: errFileNameTooLong,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 6.
|
||
|
// TestXLStorage case with undeletable parent directory.
|
||
7 years ago
|
// File can delete, dir cannot delete because no-permissions doesn't have write perms.
|
||
|
{
|
||
|
srcVol: "no-permissions",
|
||
|
srcPath: "dir/file",
|
||
|
expectedErr: nil,
|
||
|
},
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
for i, testCase := range testCases {
|
||
5 years ago
|
if err = xlStorage.DeleteFile(testCase.srcVol, testCase.srcPath); err != testCase.expectedErr {
|
||
|
t.Errorf("TestXLStorage case %d: Expected: \"%s\", got: \"%s\"", i+1, testCase.expectedErr, err)
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorage for permission denied.
|
||
8 years ago
|
if runtime.GOOS != globalWindowsOSName {
|
||
7 years ago
|
permDeniedDir := createPermDeniedFile(t)
|
||
|
defer removePermDeniedFile(permDeniedDir)
|
||
|
|
||
5 years ago
|
// Initialize xlStorage storage layer for permission denied error.
|
||
|
_, err = newXLStorage(permDeniedDir, "")
|
||
7 years ago
|
if err != nil && !os.IsPermission(err) {
|
||
5 years ago
|
t.Fatalf("Unable to initialize xlStorage, %s", err)
|
||
7 years ago
|
}
|
||
|
|
||
|
if err = os.Chmod(permDeniedDir, 0755); err != nil {
|
||
|
t.Fatalf("Unable to change permission to temporary directory %v. %v", permDeniedDir, err)
|
||
|
}
|
||
|
|
||
5 years ago
|
xlStorageNew, err := newXLStorage(permDeniedDir, "")
|
||
9 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to initialize xlStorage, %s", err)
|
||
9 years ago
|
}
|
||
|
|
||
5 years ago
|
if err = xlStorageNew.DeleteFile("mybucket", "myobject"); err != errFileAccessDenied {
|
||
7 years ago
|
t.Errorf("expected: %s, got: %s", errFileAccessDenied, err)
|
||
9 years ago
|
}
|
||
|
}
|
||
8 years ago
|
|
||
5 years ago
|
// TestXLStorage for delete on an removed disk.
|
||
8 years ago
|
// should fail with disk not found.
|
||
5 years ago
|
err = xlStorageDeletedStorage.DeleteFile("del-vol", "my-file")
|
||
8 years ago
|
if err != errDiskNotFound {
|
||
|
t.Errorf("Expected: \"Disk not found\", got \"%s\"", err)
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorageReadFile - TestXLStorages xlStorage.ReadFile with wide range of cases and asserts the result and error response.
|
||
|
func TestXLStorageReadFile(t *testing.T) {
|
||
|
// create xlStorage test setup
|
||
|
xlStorage, path, err := newXLStorageTestSetup()
|
||
8 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to create xlStorage test setup, %s", err)
|
||
8 years ago
|
}
|
||
7 years ago
|
defer os.RemoveAll(path)
|
||
8 years ago
|
|
||
8 years ago
|
volume := "success-vol"
|
||
8 years ago
|
// Setup test environment.
|
||
5 years ago
|
if err = xlStorage.MakeVol(volume); err != nil {
|
||
8 years ago
|
t.Fatalf("Unable to create volume, %s", err)
|
||
|
}
|
||
|
|
||
|
// Create directory to make errIsNotRegular
|
||
|
if err = os.Mkdir(slashpath.Join(path, "success-vol", "object-as-dir"), 0777); err != nil {
|
||
|
t.Fatalf("Unable to create directory, %s", err)
|
||
|
}
|
||
|
|
||
|
testCases := []struct {
|
||
8 years ago
|
volume string
|
||
8 years ago
|
fileName string
|
||
|
offset int64
|
||
|
bufSize int
|
||
|
expectedBuf []byte
|
||
|
expectedErr error
|
||
|
}{
|
||
|
// Successful read at offset 0 and proper buffer size. - 1
|
||
|
{
|
||
8 years ago
|
volume, "myobject", 0, 5,
|
||
8 years ago
|
[]byte("hello"), nil,
|
||
|
},
|
||
|
// Success read at hierarchy. - 2
|
||
|
{
|
||
8 years ago
|
volume, "path/to/my/object", 0, 5,
|
||
8 years ago
|
[]byte("hello"), nil,
|
||
|
},
|
||
6 years ago
|
// Object is a directory. - 3
|
||
8 years ago
|
{
|
||
8 years ago
|
volume, "object-as-dir",
|
||
8 years ago
|
0, 5, nil, errIsNotRegular},
|
||
6 years ago
|
// One path segment length is > 255 chars long. - 4
|
||
8 years ago
|
{
|
||
8 years ago
|
volume, "path/to/my/object0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
|
||
8 years ago
|
0, 5, nil, errFileNameTooLong},
|
||
6 years ago
|
// Path length is > 1024 chars long. - 5
|
||
8 years ago
|
{
|
||
8 years ago
|
volume, "level0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001/level0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002/level0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003/object000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
|
||
8 years ago
|
0, 5, nil, errFileNameTooLong},
|
||
6 years ago
|
// Buffer size greater than object size. - 6
|
||
8 years ago
|
{
|
||
8 years ago
|
volume, "myobject", 0, 16,
|
||
8 years ago
|
[]byte("hello, world"),
|
||
|
io.ErrUnexpectedEOF,
|
||
|
},
|
||
6 years ago
|
// Reading from an offset success. - 7
|
||
8 years ago
|
{
|
||
8 years ago
|
volume, "myobject", 7, 5,
|
||
8 years ago
|
[]byte("world"), nil,
|
||
|
},
|
||
6 years ago
|
// Reading from an object but buffer size greater. - 8
|
||
8 years ago
|
{
|
||
8 years ago
|
volume, "myobject",
|
||
8 years ago
|
7, 8,
|
||
|
[]byte("world"),
|
||
|
io.ErrUnexpectedEOF,
|
||
|
},
|
||
6 years ago
|
// Seeking ahead returns io.EOF. - 9
|
||
8 years ago
|
{
|
||
8 years ago
|
volume, "myobject", 14, 1, nil, io.EOF,
|
||
|
},
|
||
6 years ago
|
// Empty volume name. - 10
|
||
8 years ago
|
{
|
||
7 years ago
|
"", "myobject", 14, 1, nil, errVolumeNotFound,
|
||
8 years ago
|
},
|
||
6 years ago
|
// Empty filename name. - 11
|
||
8 years ago
|
{
|
||
|
volume, "", 14, 1, nil, errIsNotRegular,
|
||
|
},
|
||
6 years ago
|
// Non existent volume name - 12
|
||
8 years ago
|
{
|
||
|
"abcd", "", 14, 1, nil, errVolumeNotFound,
|
||
|
},
|
||
6 years ago
|
// Non existent filename - 13
|
||
8 years ago
|
{
|
||
|
volume, "abcd", 14, 1, nil, errFileNotFound,
|
||
8 years ago
|
},
|
||
|
}
|
||
|
|
||
|
// Create all files needed during testing.
|
||
|
appendFiles := testCases[:4]
|
||
6 years ago
|
v := NewBitrotVerifier(SHA256, getSHA256Sum([]byte("hello, world")))
|
||
8 years ago
|
// Create test files for further reading.
|
||
|
for i, appendFile := range appendFiles {
|
||
5 years ago
|
err = xlStorage.AppendFile(volume, appendFile.fileName, []byte("hello, world"))
|
||
8 years ago
|
if err != appendFile.expectedErr {
|
||
|
t.Fatalf("Creating file failed: %d %#v, expected: %s, got: %s", i+1, appendFile, appendFile.expectedErr, err)
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
{
|
||
|
buf := make([]byte, 5)
|
||
6 years ago
|
// Test for negative offset.
|
||
5 years ago
|
if _, err = xlStorage.ReadFile(volume, "myobject", -1, buf, v); err == nil {
|
||
6 years ago
|
t.Fatalf("expected: error, got: <nil>")
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
// Following block validates all ReadFile test cases.
|
||
|
for i, testCase := range testCases {
|
||
8 years ago
|
var n int64
|
||
8 years ago
|
// Common read buffer.
|
||
|
var buf = make([]byte, testCase.bufSize)
|
||
5 years ago
|
n, err = xlStorage.ReadFile(testCase.volume, testCase.fileName, testCase.offset, buf, v)
|
||
8 years ago
|
if err != nil && testCase.expectedErr != nil {
|
||
|
// Validate if the type string of the errors are an exact match.
|
||
|
if err.Error() != testCase.expectedErr.Error() {
|
||
8 years ago
|
if runtime.GOOS != globalWindowsOSName {
|
||
8 years ago
|
t.Errorf("Case: %d %#v, expected: %s, got: %s", i+1, testCase, testCase.expectedErr, err)
|
||
|
} else {
|
||
|
var resultErrno, expectErrno uintptr
|
||
|
if pathErr, ok := err.(*os.PathError); ok {
|
||
|
if errno, pok := pathErr.Err.(syscall.Errno); pok {
|
||
|
resultErrno = uintptr(errno)
|
||
|
}
|
||
|
}
|
||
|
if pathErr, ok := testCase.expectedErr.(*os.PathError); ok {
|
||
|
if errno, pok := pathErr.Err.(syscall.Errno); pok {
|
||
|
expectErrno = uintptr(errno)
|
||
|
}
|
||
|
}
|
||
|
if !(expectErrno != 0 && resultErrno != 0 && expectErrno == resultErrno) {
|
||
|
t.Errorf("Case: %d %#v, expected: %s, got: %s", i+1, testCase, testCase.expectedErr, err)
|
||
|
}
|
||
|
}
|
||
8 years ago
|
}
|
||
|
// Err unexpected EOF special case, where we verify we have provided a larger
|
||
|
// buffer than the data itself, but the results are in-fact valid. So we validate
|
||
|
// this error condition specifically treating it as a good condition with valid
|
||
|
// results. In this scenario return 'n' is always lesser than the input buffer.
|
||
|
if err == io.ErrUnexpectedEOF {
|
||
|
if !bytes.Equal(testCase.expectedBuf, buf[:n]) {
|
||
6 years ago
|
t.Errorf("Case: %d %#v, expected: \"%s\", got: \"%s\"", i+1, testCase, string(testCase.expectedBuf), string(buf[:n]))
|
||
8 years ago
|
}
|
||
|
if n > int64(len(buf)) {
|
||
|
t.Errorf("Case: %d %#v, expected: %d, got: %d", i+1, testCase, testCase.bufSize, n)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
// ReadFile has returned success, but our expected error is non 'nil'.
|
||
|
if err == nil && err != testCase.expectedErr {
|
||
|
t.Errorf("Case: %d %#v, expected: %s, got :%s", i+1, testCase, testCase.expectedErr, err)
|
||
|
}
|
||
|
// Expected error retured, proceed further to validate the returned results.
|
||
|
if err == nil && err == testCase.expectedErr {
|
||
|
if !bytes.Equal(testCase.expectedBuf, buf) {
|
||
|
t.Errorf("Case: %d %#v, expected: \"%s\", got: \"%s\"", i+1, testCase, string(testCase.expectedBuf), string(buf[:testCase.bufSize]))
|
||
|
}
|
||
|
if n != int64(testCase.bufSize) {
|
||
|
t.Errorf("Case: %d %#v, expected: %d, got: %d", i+1, testCase, testCase.bufSize, n)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorage for permission denied.
|
||
7 years ago
|
if runtime.GOOS != globalWindowsOSName {
|
||
|
permDeniedDir := createPermDeniedFile(t)
|
||
|
defer removePermDeniedFile(permDeniedDir)
|
||
|
|
||
5 years ago
|
// Initialize xlStorage storage layer for permission denied error.
|
||
|
_, err = newXLStorage(permDeniedDir, "")
|
||
7 years ago
|
if err != nil && !os.IsPermission(err) {
|
||
5 years ago
|
t.Fatalf("Unable to initialize xlStorage, %s", err)
|
||
7 years ago
|
}
|
||
|
|
||
|
if err = os.Chmod(permDeniedDir, 0755); err != nil {
|
||
|
t.Fatalf("Unable to change permission to temporary directory %v. %v", permDeniedDir, err)
|
||
|
}
|
||
|
|
||
5 years ago
|
xlStoragePermStorage, err := newXLStorage(permDeniedDir, "")
|
||
8 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to initialize xlStorage, %s", err)
|
||
8 years ago
|
}
|
||
7 years ago
|
|
||
|
// Common read buffer.
|
||
|
var buf = make([]byte, 10)
|
||
5 years ago
|
if _, err = xlStoragePermStorage.ReadFile("mybucket", "myobject", 0, buf, v); err != errFileAccessDenied {
|
||
7 years ago
|
t.Errorf("expected: %s, got: %s", errFileAccessDenied, err)
|
||
8 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
var xlStorageReadFileWithVerifyTests = []struct {
|
||
7 years ago
|
file string
|
||
|
offset int
|
||
|
length int
|
||
|
algorithm BitrotAlgorithm
|
||
|
expError error
|
||
|
}{
|
||
5 years ago
|
{file: "myobject", offset: 0, length: 100, algorithm: SHA256, expError: nil}, // 0
|
||
|
{file: "myobject", offset: 25, length: 74, algorithm: SHA256, expError: nil}, // 1
|
||
|
{file: "myobject", offset: 29, length: 70, algorithm: SHA256, expError: nil}, // 2
|
||
|
{file: "myobject", offset: 100, length: 0, algorithm: SHA256, expError: nil}, // 3
|
||
|
{file: "myobject", offset: 1, length: 120, algorithm: SHA256, expError: errFileCorrupt}, // 4
|
||
|
{file: "myobject", offset: 3, length: 1100, algorithm: SHA256, expError: nil}, // 5
|
||
|
{file: "myobject", offset: 2, length: 100, algorithm: SHA256, expError: errFileCorrupt}, // 6
|
||
|
{file: "myobject", offset: 1000, length: 1001, algorithm: SHA256, expError: nil}, // 7
|
||
|
{file: "myobject", offset: 0, length: 100, algorithm: BLAKE2b512, expError: errFileCorrupt}, // 8
|
||
|
{file: "myobject", offset: 25, length: 74, algorithm: BLAKE2b512, expError: nil}, // 9
|
||
|
{file: "myobject", offset: 29, length: 70, algorithm: BLAKE2b512, expError: errFileCorrupt}, // 10
|
||
|
{file: "myobject", offset: 100, length: 0, algorithm: BLAKE2b512, expError: nil}, // 11
|
||
|
{file: "myobject", offset: 1, length: 120, algorithm: BLAKE2b512, expError: nil}, // 12
|
||
|
{file: "myobject", offset: 3, length: 1100, algorithm: BLAKE2b512, expError: nil}, // 13
|
||
|
{file: "myobject", offset: 2, length: 100, algorithm: BLAKE2b512, expError: nil}, // 14
|
||
|
{file: "myobject", offset: 1000, length: 1001, algorithm: BLAKE2b512, expError: nil}, // 15
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorageReadFile with bitrot verification - tests the xlStorage level
|
||
7 years ago
|
// ReadFile API with a BitrotVerifier. Only tests hashing related
|
||
8 years ago
|
// functionality. Other functionality is tested with
|
||
5 years ago
|
// TestXLStorageReadFile.
|
||
|
func TestXLStorageReadFileWithVerify(t *testing.T) {
|
||
7 years ago
|
volume, object := "test-vol", "myobject"
|
||
5 years ago
|
xlStorage, path, err := newXLStorageTestSetup()
|
||
8 years ago
|
if err != nil {
|
||
7 years ago
|
os.RemoveAll(path)
|
||
5 years ago
|
t.Fatalf("Unable to create xlStorage test setup, %s", err)
|
||
8 years ago
|
}
|
||
5 years ago
|
if err = xlStorage.MakeVol(volume); err != nil {
|
||
7 years ago
|
os.RemoveAll(path)
|
||
|
t.Fatalf("Unable to create volume %s: %v", volume, err)
|
||
|
}
|
||
|
data := make([]byte, 8*1024)
|
||
|
if _, err = io.ReadFull(rand.Reader, data); err != nil {
|
||
|
os.RemoveAll(path)
|
||
|
t.Fatalf("Unable to create generate random data: %v", err)
|
||
|
}
|
||
5 years ago
|
if err = xlStorage.AppendFile(volume, object, data); err != nil {
|
||
7 years ago
|
os.RemoveAll(path)
|
||
|
t.Fatalf("Unable to create object: %v", err)
|
||
|
}
|
||
|
|
||
5 years ago
|
for i, test := range xlStorageReadFileWithVerifyTests {
|
||
7 years ago
|
h := test.algorithm.New()
|
||
|
h.Write(data)
|
||
|
if test.expError != nil {
|
||
|
h.Write([]byte{0})
|
||
|
}
|
||
8 years ago
|
|
||
7 years ago
|
buffer := make([]byte, test.length)
|
||
5 years ago
|
n, err := xlStorage.ReadFile(volume, test.file, int64(test.offset), buffer, NewBitrotVerifier(test.algorithm, h.Sum(nil)))
|
||
8 years ago
|
|
||
|
switch {
|
||
7 years ago
|
case err == nil && test.expError != nil:
|
||
|
t.Errorf("Test %d: Expected error %v but got none.", i, test.expError)
|
||
|
case err == nil && n != int64(test.length):
|
||
|
t.Errorf("Test %d: %d bytes were expected, but %d were written", i, test.length, n)
|
||
|
case err == nil && !bytes.Equal(data[test.offset:test.offset+test.length], buffer):
|
||
|
t.Errorf("Test %d: Expected bytes: %v, but got: %v", i, data[test.offset:test.offset+test.length], buffer)
|
||
|
case err != nil && err != test.expError:
|
||
|
t.Errorf("Test %d: Expected error: %v, but got: %v", i, test.expError, err)
|
||
8 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorageFormatFileChange - to test if changing the diskID makes the calls fail.
|
||
|
func TestXLStorageFormatFileChange(t *testing.T) {
|
||
|
xlStorage, path, err := newXLStorageTestSetup()
|
||
5 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to create xlStorage test setup, %s", err)
|
||
5 years ago
|
}
|
||
|
defer os.RemoveAll(path)
|
||
|
|
||
5 years ago
|
if err = xlStorage.MakeVol(volume); err != nil {
|
||
5 years ago
|
t.Fatalf("MakeVol failed with %s", err)
|
||
|
}
|
||
|
|
||
|
// Change the format.json such that "this" is changed to "randomid".
|
||
5 years ago
|
if err = ioutil.WriteFile(pathJoin(xlStorage.String(), minioMetaBucket, formatConfigFile), []byte(`{"version":"1","format":"xl","id":"592a41c2-b7cc-4130-b883-c4b5cb15965b","xl":{"version":"3","this":"randomid","sets":[["e07285a6-8c73-4962-89c6-047fb939f803","33b8d431-482d-4376-b63c-626d229f0a29","cff6513a-4439-4dc1-bcaa-56c9e880c352","randomid","9c9f21d5-1f15-4737-bce6-835faa0d9626","0a59b346-1424-4fc2-9fa2-a2e80541d0c1","7924a3dc-b69a-4971-9a2e-014966d6aebb","4d2b8dd9-4e48-444b-bdca-c89194b26042"]],"distributionAlgo":"CRCMOD"}}`), 0644); err != nil {
|
||
5 years ago
|
t.Fatalf("ioutil.WriteFile failed with %s", err)
|
||
|
}
|
||
|
|
||
5 years ago
|
err = xlStorage.MakeVol(volume)
|
||
5 years ago
|
if err != errVolumeExists {
|
||
5 years ago
|
t.Fatalf("MakeVol expected to fail with errDiskNotFound but failed with %s", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorage xlStorage.AppendFile()
|
||
|
func TestXLStorageAppendFile(t *testing.T) {
|
||
|
// create xlStorage test setup
|
||
|
xlStorage, path, err := newXLStorageTestSetup()
|
||
9 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to create xlStorage test setup, %s", err)
|
||
9 years ago
|
}
|
||
7 years ago
|
defer os.RemoveAll(path)
|
||
9 years ago
|
|
||
|
// Setup test environment.
|
||
5 years ago
|
if err = xlStorage.MakeVol("success-vol"); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create volume, %s", err)
|
||
|
}
|
||
|
|
||
|
// Create directory to make errIsNotRegular
|
||
|
if err = os.Mkdir(slashpath.Join(path, "success-vol", "object-as-dir"), 0777); err != nil {
|
||
|
t.Fatalf("Unable to create directory, %s", err)
|
||
|
}
|
||
|
|
||
|
testCases := []struct {
|
||
|
fileName string
|
||
|
expectedErr error
|
||
|
}{
|
||
|
{"myobject", nil},
|
||
|
{"path/to/my/object", nil},
|
||
5 years ago
|
// TestXLStorage to append to previously created file.
|
||
9 years ago
|
{"myobject", nil},
|
||
5 years ago
|
// TestXLStorage to use same path of previously created file.
|
||
9 years ago
|
{"path/to/my/testobject", nil},
|
||
5 years ago
|
// TestXLStorage to use object is a directory now.
|
||
9 years ago
|
{"object-as-dir", errIsNotRegular},
|
||
|
// path segment uses previously uploaded object.
|
||
|
{"myobject/testobject", errFileAccessDenied},
|
||
|
// One path segment length is > 255 chars long.
|
||
|
{"path/to/my/object0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", errFileNameTooLong},
|
||
|
// path length is > 1024 chars long.
|
||
|
{"level0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001/level0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002/level0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003/object000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", errFileNameTooLong},
|
||
|
}
|
||
|
|
||
7 years ago
|
for i, testCase := range testCases {
|
||
5 years ago
|
if err = xlStorage.AppendFile("success-vol", testCase.fileName, []byte("hello, world")); err != testCase.expectedErr {
|
||
7 years ago
|
t.Errorf("Case: %d, expected: %s, got: %s", i+1, testCase.expectedErr, err)
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorage for permission denied.
|
||
8 years ago
|
if runtime.GOOS != globalWindowsOSName {
|
||
7 years ago
|
permDeniedDir := createPermDeniedFile(t)
|
||
|
defer removePermDeniedFile(permDeniedDir)
|
||
|
|
||
5 years ago
|
var xlStoragePermStorage StorageAPI
|
||
|
// Initialize xlStorage storage layer for permission denied error.
|
||
|
_, err = newXLStorage(permDeniedDir, "")
|
||
7 years ago
|
if err != nil && !os.IsPermission(err) {
|
||
5 years ago
|
t.Fatalf("Unable to initialize xlStorage, %s", err)
|
||
7 years ago
|
}
|
||
|
|
||
|
if err = os.Chmod(permDeniedDir, 0755); err != nil {
|
||
|
t.Fatalf("Unable to change permission to temporary directory %v. %v", permDeniedDir, err)
|
||
|
}
|
||
|
|
||
5 years ago
|
xlStoragePermStorage, err = newXLStorage(permDeniedDir, "")
|
||
9 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to initialize xlStorage, %s", err)
|
||
9 years ago
|
}
|
||
|
|
||
5 years ago
|
if err = xlStoragePermStorage.AppendFile("mybucket", "myobject", []byte("hello, world")); err != errFileAccessDenied {
|
||
7 years ago
|
t.Fatalf("expected: Permission error, got: %s", err)
|
||
8 years ago
|
}
|
||
|
}
|
||
7 years ago
|
|
||
5 years ago
|
// TestXLStorage case with invalid volume name.
|
||
8 years ago
|
// A valid volume name should be atleast of size 3.
|
||
5 years ago
|
err = xlStorage.AppendFile("bn", "yes", []byte("hello, world"))
|
||
7 years ago
|
if err != errVolumeNotFound {
|
||
8 years ago
|
t.Fatalf("expected: \"Invalid argument error\", got: \"%s\"", err)
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorage xlStorage.RenameFile()
|
||
|
func TestXLStorageRenameFile(t *testing.T) {
|
||
|
// create xlStorage test setup
|
||
|
xlStorage, path, err := newXLStorageTestSetup()
|
||
9 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to create xlStorage test setup, %s", err)
|
||
9 years ago
|
}
|
||
7 years ago
|
defer os.RemoveAll(path)
|
||
9 years ago
|
|
||
|
// Setup test environment.
|
||
5 years ago
|
if err := xlStorage.MakeVol("src-vol"); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create volume, %s", err)
|
||
|
}
|
||
|
|
||
5 years ago
|
if err := xlStorage.MakeVol("dest-vol"); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create volume, %s", err)
|
||
|
}
|
||
|
|
||
5 years ago
|
if err := xlStorage.AppendFile("src-vol", "file1", []byte("Hello, world")); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create file, %s", err)
|
||
|
}
|
||
|
|
||
5 years ago
|
if err := xlStorage.AppendFile("src-vol", "file2", []byte("Hello, world")); err != nil {
|
||
8 years ago
|
t.Fatalf("Unable to create file, %s", err)
|
||
|
}
|
||
5 years ago
|
if err := xlStorage.AppendFile("src-vol", "file3", []byte("Hello, world")); err != nil {
|
||
8 years ago
|
t.Fatalf("Unable to create file, %s", err)
|
||
|
}
|
||
5 years ago
|
if err := xlStorage.AppendFile("src-vol", "file4", []byte("Hello, world")); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create file, %s", err)
|
||
|
}
|
||
|
|
||
5 years ago
|
if err := xlStorage.AppendFile("src-vol", "file5", []byte("Hello, world")); err != nil {
|
||
8 years ago
|
t.Fatalf("Unable to create file, %s", err)
|
||
|
}
|
||
5 years ago
|
if err := xlStorage.AppendFile("src-vol", "path/to/file1", []byte("Hello, world")); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create file, %s", err)
|
||
|
}
|
||
|
|
||
|
testCases := []struct {
|
||
8 years ago
|
srcVol string
|
||
|
destVol string
|
||
9 years ago
|
srcPath string
|
||
|
destPath string
|
||
|
expectedErr error
|
||
|
}{
|
||
5 years ago
|
// TestXLStorage case - 1.
|
||
8 years ago
|
{
|
||
|
srcVol: "src-vol",
|
||
|
destVol: "dest-vol",
|
||
|
srcPath: "file1",
|
||
|
destPath: "file-one",
|
||
|
expectedErr: nil,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 2.
|
||
8 years ago
|
{
|
||
|
srcVol: "src-vol",
|
||
|
destVol: "dest-vol",
|
||
|
srcPath: "path/",
|
||
|
destPath: "new-path/",
|
||
|
expectedErr: nil,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 3.
|
||
|
// TestXLStorage to overwrite destination file.
|
||
8 years ago
|
{
|
||
|
srcVol: "src-vol",
|
||
|
destVol: "dest-vol",
|
||
|
srcPath: "file2",
|
||
|
destPath: "file-one",
|
||
|
expectedErr: nil,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 4.
|
||
|
// TestXLStorage case with io error count set to 1.
|
||
8 years ago
|
// expected not to fail.
|
||
|
{
|
||
|
srcVol: "src-vol",
|
||
|
destVol: "dest-vol",
|
||
|
srcPath: "file3",
|
||
|
destPath: "file-two",
|
||
|
expectedErr: nil,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 5.
|
||
|
// TestXLStorage case with io error count set to maximum allowed count.
|
||
8 years ago
|
// expected not to fail.
|
||
|
{
|
||
|
srcVol: "src-vol",
|
||
|
destVol: "dest-vol",
|
||
|
srcPath: "file4",
|
||
|
destPath: "file-three",
|
||
|
expectedErr: nil,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 6.
|
||
|
// TestXLStorage case with non-existent source file.
|
||
8 years ago
|
{
|
||
|
srcVol: "src-vol",
|
||
|
destVol: "dest-vol",
|
||
|
srcPath: "non-existent-file",
|
||
|
destPath: "file-three",
|
||
|
expectedErr: errFileNotFound,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 7.
|
||
|
// TestXLStorage to check failure of source and destination are not same type.
|
||
8 years ago
|
{
|
||
|
srcVol: "src-vol",
|
||
|
destVol: "dest-vol",
|
||
|
srcPath: "path/",
|
||
|
destPath: "file-one",
|
||
|
expectedErr: errFileAccessDenied,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 8.
|
||
|
// TestXLStorage to check failure of destination directory exists.
|
||
8 years ago
|
{
|
||
|
srcVol: "src-vol",
|
||
|
destVol: "dest-vol",
|
||
|
srcPath: "path/",
|
||
|
destPath: "new-path/",
|
||
|
expectedErr: errFileAccessDenied,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 9.
|
||
|
// TestXLStorage case with source being a file and destination being a directory.
|
||
8 years ago
|
// Either both have to be files or directories.
|
||
|
// Expecting to fail with `errFileAccessDenied`.
|
||
|
{
|
||
|
srcVol: "src-vol",
|
||
|
destVol: "dest-vol",
|
||
|
srcPath: "file4",
|
||
|
destPath: "new-path/",
|
||
|
expectedErr: errFileAccessDenied,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 10.
|
||
|
// TestXLStorage case with non-existent source volume.
|
||
8 years ago
|
// Expecting to fail with `errVolumeNotFound`.
|
||
|
{
|
||
|
srcVol: "src-vol-non-existent",
|
||
|
destVol: "dest-vol",
|
||
|
srcPath: "file4",
|
||
|
destPath: "new-path/",
|
||
|
expectedErr: errVolumeNotFound,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 11.
|
||
|
// TestXLStorage case with non-existent destination volume.
|
||
8 years ago
|
// Expecting to fail with `errVolumeNotFound`.
|
||
|
{
|
||
|
srcVol: "src-vol",
|
||
|
destVol: "dest-vol-non-existent",
|
||
|
srcPath: "file4",
|
||
|
destPath: "new-path/",
|
||
|
expectedErr: errVolumeNotFound,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 12.
|
||
|
// TestXLStorage case with invalid src volume name. Length should be atleast 3.
|
||
8 years ago
|
// Expecting to fail with `errInvalidArgument`.
|
||
|
{
|
||
|
srcVol: "ab",
|
||
|
destVol: "dest-vol-non-existent",
|
||
|
srcPath: "file4",
|
||
|
destPath: "new-path/",
|
||
7 years ago
|
expectedErr: errVolumeNotFound,
|
||
8 years ago
|
},
|
||
5 years ago
|
// TestXLStorage case - 13.
|
||
|
// TestXLStorage case with invalid destination volume name. Length should be atleast 3.
|
||
8 years ago
|
// Expecting to fail with `errInvalidArgument`.
|
||
|
{
|
||
|
srcVol: "abcd",
|
||
|
destVol: "ef",
|
||
|
srcPath: "file4",
|
||
|
destPath: "new-path/",
|
||
7 years ago
|
expectedErr: errVolumeNotFound,
|
||
8 years ago
|
},
|
||
5 years ago
|
// TestXLStorage case - 14.
|
||
|
// TestXLStorage case with invalid destination volume name. Length should be atleast 3.
|
||
8 years ago
|
// Expecting to fail with `errInvalidArgument`.
|
||
|
{
|
||
|
srcVol: "abcd",
|
||
|
destVol: "ef",
|
||
|
srcPath: "file4",
|
||
|
destPath: "new-path/",
|
||
7 years ago
|
expectedErr: errVolumeNotFound,
|
||
8 years ago
|
},
|
||
5 years ago
|
// TestXLStorage case - 15.
|
||
|
// TestXLStorage case with the parent of the destination being a file.
|
||
8 years ago
|
// expected to fail with `errFileAccessDenied`.
|
||
|
{
|
||
|
srcVol: "src-vol",
|
||
|
destVol: "dest-vol",
|
||
|
srcPath: "file5",
|
||
|
destPath: "file-one/parent-is-file",
|
||
|
expectedErr: errFileAccessDenied,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 16.
|
||
|
// TestXLStorage case with segment of source file name more than 255.
|
||
8 years ago
|
// expected not to fail.
|
||
|
{
|
||
|
srcVol: "src-vol",
|
||
|
destVol: "dest-vol",
|
||
|
srcPath: "path/to/my/object0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
|
||
|
destPath: "file-six",
|
||
|
expectedErr: errFileNameTooLong,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 17.
|
||
|
// TestXLStorage case with segment of destination file name more than 255.
|
||
8 years ago
|
// expected not to fail.
|
||
|
{
|
||
|
srcVol: "src-vol",
|
||
|
destVol: "dest-vol",
|
||
|
srcPath: "file6",
|
||
|
destPath: "path/to/my/object0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
|
||
|
expectedErr: errFileNameTooLong,
|
||
|
},
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
for i, testCase := range testCases {
|
||
5 years ago
|
if err := xlStorage.RenameFile(testCase.srcVol, testCase.srcPath, testCase.destVol, testCase.destPath); err != testCase.expectedErr {
|
||
|
t.Fatalf("TestXLStorage %d: Expected the error to be : \"%v\", got: \"%v\".", i+1, testCase.expectedErr, err)
|
||
9 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// TestXLStorage xlStorage.CheckFile()
|
||
|
func TestXLStorageCheckFile(t *testing.T) {
|
||
|
// create xlStorage test setup
|
||
|
xlStorage, path, err := newXLStorageTestSetup()
|
||
9 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to create xlStorage test setup, %s", err)
|
||
9 years ago
|
}
|
||
7 years ago
|
defer os.RemoveAll(path)
|
||
9 years ago
|
|
||
|
// Setup test environment.
|
||
5 years ago
|
if err := xlStorage.MakeVol("success-vol"); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create volume, %s", err)
|
||
|
}
|
||
|
|
||
5 years ago
|
if err := xlStorage.AppendFile("success-vol", pathJoin("success-file", xlStorageFormatFile), []byte("Hello, world")); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create file, %s", err)
|
||
|
}
|
||
|
|
||
5 years ago
|
if err := xlStorage.AppendFile("success-vol", pathJoin("path/to/success-file", xlStorageFormatFile), []byte("Hello, world")); err != nil {
|
||
9 years ago
|
t.Fatalf("Unable to create file, %s", err)
|
||
|
}
|
||
|
|
||
|
testCases := []struct {
|
||
8 years ago
|
srcVol string
|
||
|
srcPath string
|
||
9 years ago
|
expectedErr error
|
||
|
}{
|
||
5 years ago
|
// TestXLStorage case - 1.
|
||
|
// TestXLStorage case with valid inputs, expected to pass.
|
||
8 years ago
|
{
|
||
|
srcVol: "success-vol",
|
||
|
srcPath: "success-file",
|
||
|
expectedErr: nil,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 2.
|
||
|
// TestXLStorage case with valid inputs, expected to pass.
|
||
8 years ago
|
{
|
||
|
srcVol: "success-vol",
|
||
|
srcPath: "path/to/success-file",
|
||
|
expectedErr: nil,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 3.
|
||
|
// TestXLStorage case with non-existent file.
|
||
8 years ago
|
{
|
||
|
srcVol: "success-vol",
|
||
|
srcPath: "nonexistent-file",
|
||
|
expectedErr: errFileNotFound,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 4.
|
||
|
// TestXLStorage case with non-existent file path.
|
||
8 years ago
|
{
|
||
|
srcVol: "success-vol",
|
||
|
srcPath: "path/2/success-file",
|
||
|
expectedErr: errFileNotFound,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 5.
|
||
|
// TestXLStorage case with path being a directory.
|
||
8 years ago
|
{
|
||
|
srcVol: "success-vol",
|
||
|
srcPath: "path",
|
||
|
expectedErr: errFileNotFound,
|
||
|
},
|
||
5 years ago
|
// TestXLStorage case - 6.
|
||
|
// TestXLStorage case with non existent volume.
|
||
8 years ago
|
{
|
||
|
srcVol: "non-existent-vol",
|
||
|
srcPath: "success-file",
|
||
|
expectedErr: errVolumeNotFound,
|
||
|
},
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
for i, testCase := range testCases {
|
||
5 years ago
|
if err := xlStorage.CheckFile(testCase.srcVol, testCase.srcPath); err != testCase.expectedErr {
|
||
|
t.Fatalf("TestXLStorage case %d: Expected: \"%s\", got: \"%s\"", i+1, testCase.expectedErr, err)
|
||
9 years ago
|
}
|
||
|
}
|
||
|
}
|
||
7 years ago
|
|
||
5 years ago
|
// Test xlStorage.VerifyFile()
|
||
|
func TestXLStorageVerifyFile(t *testing.T) {
|
||
5 years ago
|
// We test 4 cases:
|
||
|
// 1) Whole-file bitrot check on proper file
|
||
|
// 2) Whole-file bitrot check on corrupted file
|
||
|
// 3) Streaming bitrot check on proper file
|
||
|
// 4) Streaming bitrot check on corrupted file
|
||
|
|
||
5 years ago
|
// create xlStorage test setup
|
||
|
xlStorage, path, err := newXLStorageTestSetup()
|
||
5 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Unable to create xlStorage test setup, %s", err)
|
||
5 years ago
|
}
|
||
|
defer os.RemoveAll(path)
|
||
|
|
||
|
volName := "testvol"
|
||
|
fileName := "testfile"
|
||
5 years ago
|
if err := xlStorage.MakeVol(volName); err != nil {
|
||
5 years ago
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
// 1) Whole-file bitrot check on proper file
|
||
|
size := int64(4*1024*1024 + 100*1024) // 4.1 MB
|
||
|
data := make([]byte, size)
|
||
|
if _, err := rand.Read(data); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
algo := HighwayHash256
|
||
|
h := algo.New()
|
||
|
h.Write(data)
|
||
|
hashBytes := h.Sum(nil)
|
||
5 years ago
|
if err := xlStorage.WriteAll(volName, fileName, bytes.NewBuffer(data)); err != nil {
|
||
5 years ago
|
t.Fatal(err)
|
||
|
}
|
||
5 years ago
|
if err := xlStorage.storage.bitrotVerify(pathJoin(path, volName, fileName), size, algo, hashBytes, 0); err != nil {
|
||
5 years ago
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
// 2) Whole-file bitrot check on corrupted file
|
||
5 years ago
|
if err := xlStorage.AppendFile(volName, fileName, []byte("a")); err != nil {
|
||
5 years ago
|
t.Fatal(err)
|
||
|
}
|
||
5 years ago
|
|
||
|
// Check if VerifyFile reports the incorrect file length (the correct length is `size+1`)
|
||
5 years ago
|
if err := xlStorage.storage.bitrotVerify(pathJoin(path, volName, fileName), size, algo, hashBytes, 0); err == nil {
|
||
5 years ago
|
t.Fatal("expected to fail bitrot check")
|
||
|
}
|
||
|
|
||
|
// Check if bitrot fails
|
||
5 years ago
|
if err := xlStorage.storage.bitrotVerify(pathJoin(path, volName, fileName), size+1, algo, hashBytes, 0); err == nil {
|
||
5 years ago
|
t.Fatal("expected to fail bitrot check")
|
||
|
}
|
||
|
|
||
5 years ago
|
if err := xlStorage.DeleteFile(volName, fileName); err != nil {
|
||
5 years ago
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
// 3) Streaming bitrot check on proper file
|
||
|
algo = HighwayHash256S
|
||
|
shardSize := int64(1024 * 1024)
|
||
|
shard := make([]byte, shardSize)
|
||
5 years ago
|
w := newStreamingBitrotWriter(xlStorage, volName, fileName, size, algo, shardSize)
|
||
5 years ago
|
reader := bytes.NewReader(data)
|
||
|
for {
|
||
|
// Using io.CopyBuffer instead of this loop will not work for us as io.CopyBuffer
|
||
|
// will use bytes.Buffer.ReadFrom() which will not do shardSize'ed writes causing error.
|
||
|
n, err := reader.Read(shard)
|
||
|
w.Write(shard[:n])
|
||
|
if err == nil {
|
||
|
continue
|
||
|
}
|
||
|
if err == io.EOF {
|
||
|
break
|
||
|
}
|
||
5 years ago
|
t.Fatal(err)
|
||
5 years ago
|
}
|
||
|
w.Close()
|
||
5 years ago
|
if err := xlStorage.storage.bitrotVerify(pathJoin(path, volName, fileName), size, algo, nil, shardSize); err != nil {
|
||
5 years ago
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
// 4) Streaming bitrot check on corrupted file
|
||
5 years ago
|
filePath := pathJoin(xlStorage.String(), volName, fileName)
|
||
5 years ago
|
f, err := os.OpenFile(filePath, os.O_WRONLY|os.O_SYNC, 0644)
|
||
5 years ago
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
if _, err := f.WriteString("a"); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
f.Close()
|
||
5 years ago
|
if err := xlStorage.storage.bitrotVerify(pathJoin(path, volName, fileName), size, algo, nil, shardSize); err == nil {
|
||
5 years ago
|
t.Fatal("expected to fail bitrot check")
|
||
|
}
|
||
5 years ago
|
if err := xlStorage.storage.bitrotVerify(pathJoin(path, volName, fileName), size+1, algo, nil, shardSize); err == nil {
|
||
5 years ago
|
t.Fatal("expected to fail bitrot check")
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
// Checks for restrictions for min total disk space and inodes.
|
||
|
func TestCheckDiskTotalMin(t *testing.T) {
|
||
|
testCases := []struct {
|
||
|
diskInfo disk.Info
|
||
|
err error
|
||
|
}{
|
||
|
// Test 1 - when fstype is nfs.
|
||
|
{
|
||
|
diskInfo: disk.Info{
|
||
|
Total: diskMinTotalSpace * 3,
|
||
|
FSType: "NFS",
|
||
|
},
|
||
|
err: nil,
|
||
|
},
|
||
7 years ago
|
// Test 2 - when fstype is xfs and total inodes are less than 10k.
|
||
7 years ago
|
{
|
||
|
diskInfo: disk.Info{
|
||
|
Total: diskMinTotalSpace * 3,
|
||
|
FSType: "XFS",
|
||
|
Files: 9999,
|
||
|
},
|
||
7 years ago
|
err: nil,
|
||
7 years ago
|
},
|
||
|
// Test 3 - when fstype is btrfs and total inodes is empty.
|
||
|
{
|
||
|
diskInfo: disk.Info{
|
||
|
Total: diskMinTotalSpace * 3,
|
||
|
FSType: "BTRFS",
|
||
|
Files: 0,
|
||
|
},
|
||
|
err: nil,
|
||
|
},
|
||
|
// Test 4 - when fstype is xfs and total disk space is really small.
|
||
|
{
|
||
|
diskInfo: disk.Info{
|
||
|
Total: diskMinTotalSpace - diskMinTotalSpace/1024,
|
||
|
FSType: "XFS",
|
||
|
Files: 9999,
|
||
|
},
|
||
7 years ago
|
err: errMinDiskSize,
|
||
7 years ago
|
},
|
||
|
}
|
||
|
|
||
|
// Validate all cases.
|
||
|
for i, test := range testCases {
|
||
|
if err := checkDiskMinTotal(test.diskInfo); test.err != err {
|
||
|
t.Errorf("Test %d: Expected error %s, got %s", i+1, test.err, err)
|
||
|
}
|
||
|
}
|
||
|
}
|