<commit_before>/*
 * 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 cmd

import (
	"bytes"
	"os"
	"path/filepath"
	"testing"
)

// TestNewFS - tests initialization of all input disks
// and constructs a valid `FS` object layer.
func TestNewFS(t *testing.T) {
	// Do not attempt to create this path, the test validates
	// so that newFSObjects initializes non existing paths
	// and successfully returns initialized object layer.
	disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix())
	defer removeAll(disk)

	// Setup to test errFSDiskFormat.
	disks := []string{}
	for i := 0; i < 6; i++ {
		xlDisk := filepath.Join(os.TempDir(), "minio-"+nextSuffix())
		defer removeAll(xlDisk)
		disks = append(disks, xlDisk)
	}

	endpoints, err := parseStorageEndpoints([]string{disk})
	if err != nil {
		t.Fatal("Uexpected error: ", err)
	}

	fsStorageDisks, err := initStorageDisks(endpoints, nil)
	if err != nil {
		t.Fatal("Uexpected error: ", err)
	}

	endpoints, err = parseStorageEndpoints(disks)
	if err != nil {
		t.Fatal("Uexpected error: ", err)
	}

	xlStorageDisks, err := initStorageDisks(endpoints, nil)
	if err != nil {
		t.Fatal("Uexpected error: ", err)
	}

	// Initializes all disks with XL
	err = waitForFormatDisks(true, endpoints, xlStorageDisks)
	if err != nil {
		t.Fatalf("Unable to format XL %s", err)
	}
	_, err = newXLObjects(xlStorageDisks)
	if err != nil {
		t.Fatalf("Unable to initialize XL object, %s", err)
	}

	testCases := []struct {
		disk        StorageAPI
		expectedErr error
	}{
		{fsStorageDisks[0], nil},
		{xlStorageDisks[0], errFSDiskFormat},
	}

	for _, testCase := range testCases {
		if err = waitForFormatDisks(true, endpoints, []StorageAPI{testCase.disk}); err != testCase.expectedErr {
			t.Errorf("expected: %s, got :%s", testCase.expectedErr, err)
		}
	}
	_, err = newFSObjects(nil)
	if err != errInvalidArgument {
		t.Errorf("Expecting error invalid argument, got %s", err)
	}
	_, err = newFSObjects(xlStorageDisks[0])
	if err != nil {
		errMsg := "Unable to recognize backend format, Disk is not in FS format."
		if err.Error() == errMsg {
			t.Errorf("Expecting %s, got %s", errMsg, err)
		}
	}
}

// TestFSShutdown - initialize a new FS object layer then calls Shutdown
// to check returned results
func TestFSShutdown(t *testing.T) {
	// Prepare for tests
	disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix())
	defer removeAll(disk)
	obj := initFSObjects(disk, t)

	fs := obj.(fsObjects)
	fsStorage := fs.storage.(*posix)

	bucketName := "testbucket"
	objectName := "object"
	objectContent := "12345"

	obj.MakeBucket(bucketName)
	sha256sum := ""
	obj.PutObject(bucketName, objectName, int64(len(objectContent)), bytes.NewReader([]byte(objectContent)), nil, sha256sum)

	// Test Shutdown with regular conditions
	if err := fs.Shutdown(); err != nil {
		t.Fatal("Cannot shutdown the FS object: ", err)
	}

	// Test Shutdown with faulty disks
	for i := 1; i <= 5; i++ {
		fs.storage = newNaughtyDisk(fsStorage, map[int]error{i: errFaultyDisk}, nil)
		if err := fs.Shutdown(); errorCause(err) != errFaultyDisk {
			t.Fatal(i, ", Got unexpected fs shutdown error: ", err)
		}
	}

}

// TestFSLoadFormatFS - test loadFormatFS with healty and faulty disks
func TestFSLoadFormatFS(t *testing.T) {
	// Prepare for testing
	disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix())<commit_msg>Avoid shutdown fs multiple times and create a new fs in each loop (#3213)<commit_after>/*
 * 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 cmd

import (
	"bytes"
	"os"
	"path/filepath"
	"testing"
)

// TestNewFS - tests initialization of all input disks
// and constructs a valid `FS` object layer.
func TestNewFS(t *testing.T) {
	// Do not attempt to create this path, the test validates
	// so that newFSObjects initializes non existing paths
	// and successfully returns initialized object layer.
	disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix())
	defer removeAll(disk)

	// Setup to test errFSDiskFormat.
	disks := []string{}
	for i := 0; i < 6; i++ {
		xlDisk := filepath.Join(os.TempDir(), "minio-"+nextSuffix())
		defer removeAll(xlDisk)
		disks = append(disks, xlDisk)
	}

	endpoints, err := parseStorageEndpoints([]string{disk})
	if err != nil {
		t.Fatal("Uexpected error: ", err)
	}

	fsStorageDisks, err := initStorageDisks(endpoints, nil)
	if err != nil {
		t.Fatal("Uexpected error: ", err)
	}

	endpoints, err = parseStorageEndpoints(disks)
	if err != nil {
		t.Fatal("Uexpected error: ", err)
	}

	xlStorageDisks, err := initStorageDisks(endpoints, nil)
	if err != nil {
		t.Fatal("Uexpected error: ", err)
	}

	// Initializes all disks with XL
	err = waitForFormatDisks(true, endpoints, xlStorageDisks)
	if err != nil {
		t.Fatalf("Unable to format XL %s", err)
	}
	_, err = newXLObjects(xlStorageDisks)
	if err != nil {
		t.Fatalf("Unable to initialize XL object, %s", err)
	}

	testCases := []struct {
		disk        StorageAPI
		expectedErr error
	}{
		{fsStorageDisks[0], nil},
		{xlStorageDisks[0], errFSDiskFormat},
	}

	for _, testCase := range testCases {
		if err = waitForFormatDisks(true, endpoints, []StorageAPI{testCase.disk}); err != testCase.expectedErr {
			t.Errorf("expected: %s, got :%s", testCase.expectedErr, err)
		}
	}
	_, err = newFSObjects(nil)
	if err != errInvalidArgument {
		t.Errorf("Expecting error invalid argument, got %s", err)
	}
	_, err = newFSObjects(xlStorageDisks[0])
	if err != nil {
		errMsg := "Unable to recognize backend format, Disk is not in FS format."
		if err.Error() == errMsg {
			t.Errorf("Expecting %s, got %s", errMsg, err)
		}
	}
}

// TestFSShutdown - initialize a new FS object layer then calls Shutdown
// to check returned results
func TestFSShutdown(t *testing.T) {

	// Create and return an fsObject with its path in the disk
	prepareTest := func() (fsObjects, string) {
		disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix())
		obj := initFSObjects(disk, t)
		fs := obj.(fsObjects)
		bucketName := "testbucket"
		objectName := "object"
		objectContent := "12345"
		obj.MakeBucket(bucketName)
		sha256sum := ""
		obj.PutObject(bucketName, objectName, int64(len(objectContent)), bytes.NewReader([]byte(objectContent)), nil, sha256sum)
		return fs, disk
	}

	// Test Shutdown with regular conditions
	fs, disk := prepareTest()
	if err := fs.Shutdown(); err != nil {
		t.Fatal("Cannot shutdown the FS object: ", err)
	}
	removeAll(disk)

	// Test Shutdown with faulty disk
	for i := 1; i <= 5; i++ {
		fs, disk := prepareTest()
		fsStorage := fs.storage.(*posix)
		fs.storage = newNaughtyDisk(fsStorage, map[int]error{i: errFaultyDisk}, nil)
		if err := fs.Shutdown(); errorCause(err) != errFaultyDisk {
			t.Fatal(i, ", Got unexpected fs shutdown error: ", err)
		}
		removeAll(disk)
	}
}

// TestFSLoadFormatFS - test loadFormatFS with healty and faulty disks
func TestFSLoadFormatFS(t *testing.T) {
	// Prepare for testing
	disk := filepath.Join(os.TempDir(), "minio-"+nextSuffix())
{'fraction_non_alphanumeric': 0.13163455562461157, 'fraction_numerical': 0.0074580484773151025, 'mean_word_length': 4.003731343283582, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '574319', 'n_tokens_mistral': 2832, 'n_tokens_neox': 2648, 'n_words': 829}