2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 15:29:08 -04:00
|
|
|
|
2016-04-06 19:05:30 -04:00
|
|
|
package safe
|
2015-07-07 15:29:08 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-08-08 23:56:29 -04:00
|
|
|
"path"
|
2015-07-07 15:29:08 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-09-20 16:50:07 -04:00
|
|
|
type MySuite struct {
|
|
|
|
root string
|
|
|
|
}
|
2015-07-07 15:29:08 -04:00
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
func (s *MySuite) SetUpSuite(t *testing.T) {
|
2016-05-27 00:43:33 -04:00
|
|
|
root, err := ioutil.TempDir(os.TempDir(), "safe_test.go.")
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-09-20 16:50:07 -04:00
|
|
|
s.root = root
|
|
|
|
}
|
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
func (s *MySuite) TearDownSuite(t *testing.T) {
|
|
|
|
err := os.RemoveAll(s.root)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-09-20 16:50:07 -04:00
|
|
|
}
|
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
func TestSafeAbort(t *testing.T) {
|
|
|
|
s := &MySuite{}
|
|
|
|
s.SetUpSuite(t)
|
|
|
|
defer s.TearDownSuite(t)
|
|
|
|
|
2016-08-08 23:56:29 -04:00
|
|
|
f, err := CreateFile(path.Join(s.root, "testfile-abort"))
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-13 06:01:15 -04:00
|
|
|
_, err = os.Stat(path.Join(s.root, "testfile-abort"))
|
2017-10-10 05:14:42 -04:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-08 23:56:29 -04:00
|
|
|
err = f.Abort()
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-08 23:56:29 -04:00
|
|
|
err = f.Close()
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
if err.Error() != "close on aborted file" {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2016-08-08 23:56:29 -04:00
|
|
|
}
|
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
func TestSafeClose(t *testing.T) {
|
|
|
|
s := &MySuite{}
|
|
|
|
s.SetUpSuite(t)
|
|
|
|
defer s.TearDownSuite(t)
|
|
|
|
|
2016-08-08 23:56:29 -04:00
|
|
|
f, err := CreateFile(path.Join(s.root, "testfile-close"))
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-13 06:01:15 -04:00
|
|
|
_, err = os.Stat(path.Join(s.root, "testfile-close"))
|
2017-10-10 05:14:42 -04:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-08 23:56:29 -04:00
|
|
|
err = f.Close()
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-13 06:01:15 -04:00
|
|
|
_, err = os.Stat(path.Join(s.root, "testfile-close"))
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-08 23:56:29 -04:00
|
|
|
err = os.Remove(path.Join(s.root, "testfile-close"))
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-08 23:56:29 -04:00
|
|
|
err = f.Abort()
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
if err.Error() != "abort on closed file" {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2016-08-08 23:56:29 -04:00
|
|
|
}
|
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
func TestSafe(t *testing.T) {
|
|
|
|
s := &MySuite{}
|
|
|
|
s.SetUpSuite(t)
|
|
|
|
defer s.TearDownSuite(t)
|
|
|
|
|
2016-08-08 23:56:29 -04:00
|
|
|
f, err := CreateFile(path.Join(s.root, "testfile-safe"))
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-13 06:01:15 -04:00
|
|
|
_, err = os.Stat(path.Join(s.root, "testfile-safe"))
|
2017-10-10 05:14:42 -04:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-07 15:29:08 -04:00
|
|
|
err = f.Close()
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-08 23:56:29 -04:00
|
|
|
_, err = f.Write([]byte("Test"))
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
if err.Error() != "write on closed file" {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 23:56:29 -04:00
|
|
|
err = f.Close()
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
if err.Error() != "close on closed file" {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 06:01:15 -04:00
|
|
|
_, err = os.Stat(path.Join(s.root, "testfile-safe"))
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-08 23:56:29 -04:00
|
|
|
err = os.Remove(path.Join(s.root, "testfile-safe"))
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-07 15:29:08 -04:00
|
|
|
}
|
|
|
|
|
2017-10-10 05:14:42 -04:00
|
|
|
func TestSafeAbortWrite(t *testing.T) {
|
|
|
|
s := &MySuite{}
|
|
|
|
s.SetUpSuite(t)
|
|
|
|
defer s.TearDownSuite(t)
|
|
|
|
|
2016-08-08 23:56:29 -04:00
|
|
|
f, err := CreateFile(path.Join(s.root, "purgefile-abort"))
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-13 06:01:15 -04:00
|
|
|
_, err = os.Stat(path.Join(s.root, "purgefile-abort"))
|
2017-10-10 05:14:42 -04:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-05-27 00:43:33 -04:00
|
|
|
err = f.Abort()
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-13 06:01:15 -04:00
|
|
|
_, err = os.Stat(path.Join(s.root, "purgefile-abort"))
|
2017-10-10 05:14:42 -04:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-08 23:56:29 -04:00
|
|
|
err = f.Abort()
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
if err.Error() != "abort on aborted file" {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 23:56:29 -04:00
|
|
|
_, err = f.Write([]byte("Test"))
|
2017-10-10 05:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
if err.Error() != "write on aborted file" {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2015-07-07 15:29:08 -04:00
|
|
|
}
|