2015-07-07 15:29:08 -04:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Client (C) 2015, 2016, 2017 MinIO, Inc.
|
2015-07-07 15:29:08 -04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-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
|
|
|
}
|