2015-07-07 21:29:08 +02:00
|
|
|
/*
|
2017-05-02 11:35:27 +02:00
|
|
|
* Minio Client (C) 2015, 2016, 2017 Minio, Inc.
|
2015-07-07 21:29:08 +02: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-07 01:05:30 +02:00
|
|
|
package safe
|
2015-07-07 21:29:08 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-08-09 05:56:29 +02:00
|
|
|
"path"
|
2015-07-07 21:29:08 +02:00
|
|
|
"testing"
|
|
|
|
|
2017-05-02 11:35:27 +02:00
|
|
|
os2 "github.com/minio/minio/pkg/x/os"
|
2015-07-07 21:29:08 +02:00
|
|
|
)
|
|
|
|
|
2015-09-20 22:50:07 +02:00
|
|
|
type MySuite struct {
|
|
|
|
root string
|
|
|
|
}
|
2015-07-07 21:29:08 +02:00
|
|
|
|
2017-10-10 11:14:42 +02:00
|
|
|
func (s *MySuite) SetUpSuite(t *testing.T) {
|
2016-05-27 06:43:33 +02:00
|
|
|
root, err := ioutil.TempDir(os.TempDir(), "safe_test.go.")
|
2017-10-10 11:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-09-20 22:50:07 +02:00
|
|
|
s.root = root
|
|
|
|
}
|
|
|
|
|
2017-10-10 11:14:42 +02:00
|
|
|
func (s *MySuite) TearDownSuite(t *testing.T) {
|
|
|
|
err := os.RemoveAll(s.root)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-09-20 22:50:07 +02:00
|
|
|
}
|
|
|
|
|
2017-10-10 11:14:42 +02:00
|
|
|
func TestSafeAbort(t *testing.T) {
|
|
|
|
s := &MySuite{}
|
|
|
|
s.SetUpSuite(t)
|
|
|
|
defer s.TearDownSuite(t)
|
|
|
|
|
2016-08-09 05:56:29 +02:00
|
|
|
f, err := CreateFile(path.Join(s.root, "testfile-abort"))
|
2017-10-10 11:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-05-02 11:35:27 +02:00
|
|
|
_, err = os2.Stat(path.Join(s.root, "testfile-abort"))
|
2017-10-10 11:14:42 +02:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-09 05:56:29 +02:00
|
|
|
err = f.Abort()
|
2017-10-10 11:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-09 05:56:29 +02:00
|
|
|
err = f.Close()
|
2017-10-10 11:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
if err.Error() != "close on aborted file" {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2016-08-09 05:56:29 +02:00
|
|
|
}
|
|
|
|
|
2017-10-10 11:14:42 +02:00
|
|
|
func TestSafeClose(t *testing.T) {
|
|
|
|
s := &MySuite{}
|
|
|
|
s.SetUpSuite(t)
|
|
|
|
defer s.TearDownSuite(t)
|
|
|
|
|
2016-08-09 05:56:29 +02:00
|
|
|
f, err := CreateFile(path.Join(s.root, "testfile-close"))
|
2017-10-10 11:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-05-02 11:35:27 +02:00
|
|
|
_, err = os2.Stat(path.Join(s.root, "testfile-close"))
|
2017-10-10 11:14:42 +02:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-09 05:56:29 +02:00
|
|
|
err = f.Close()
|
2017-10-10 11:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-05-02 11:35:27 +02:00
|
|
|
_, err = os2.Stat(path.Join(s.root, "testfile-close"))
|
2017-10-10 11:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-09 05:56:29 +02:00
|
|
|
err = os.Remove(path.Join(s.root, "testfile-close"))
|
2017-10-10 11:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-09 05:56:29 +02:00
|
|
|
err = f.Abort()
|
2017-10-10 11:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
if err.Error() != "abort on closed file" {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2016-08-09 05:56:29 +02:00
|
|
|
}
|
|
|
|
|
2017-10-10 11:14:42 +02:00
|
|
|
func TestSafe(t *testing.T) {
|
|
|
|
s := &MySuite{}
|
|
|
|
s.SetUpSuite(t)
|
|
|
|
defer s.TearDownSuite(t)
|
|
|
|
|
2016-08-09 05:56:29 +02:00
|
|
|
f, err := CreateFile(path.Join(s.root, "testfile-safe"))
|
2017-10-10 11:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-05-02 11:35:27 +02:00
|
|
|
_, err = os2.Stat(path.Join(s.root, "testfile-safe"))
|
2017-10-10 11:14:42 +02:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-07 21:29:08 +02:00
|
|
|
err = f.Close()
|
2017-10-10 11:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-09 05:56:29 +02:00
|
|
|
_, err = f.Write([]byte("Test"))
|
2017-10-10 11:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
if err.Error() != "write on closed file" {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 05:56:29 +02:00
|
|
|
err = f.Close()
|
2017-10-10 11:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
if err.Error() != "close on closed file" {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 11:35:27 +02:00
|
|
|
_, err = os2.Stat(path.Join(s.root, "testfile-safe"))
|
2017-10-10 11:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-09 05:56:29 +02:00
|
|
|
err = os.Remove(path.Join(s.root, "testfile-safe"))
|
2017-10-10 11:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-07 21:29:08 +02:00
|
|
|
}
|
|
|
|
|
2017-10-10 11:14:42 +02:00
|
|
|
func TestSafeAbortWrite(t *testing.T) {
|
|
|
|
s := &MySuite{}
|
|
|
|
s.SetUpSuite(t)
|
|
|
|
defer s.TearDownSuite(t)
|
|
|
|
|
2016-08-09 05:56:29 +02:00
|
|
|
f, err := CreateFile(path.Join(s.root, "purgefile-abort"))
|
2017-10-10 11:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-05-02 11:35:27 +02:00
|
|
|
_, err = os2.Stat(path.Join(s.root, "purgefile-abort"))
|
2017-10-10 11:14:42 +02:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-05-27 06:43:33 +02:00
|
|
|
err = f.Abort()
|
2017-10-10 11:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-05-02 11:35:27 +02:00
|
|
|
_, err = os2.Stat(path.Join(s.root, "purgefile-abort"))
|
2017-10-10 11:14:42 +02:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-09 05:56:29 +02:00
|
|
|
err = f.Abort()
|
2017-10-10 11:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
if err.Error() != "abort on aborted file" {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 05:56:29 +02:00
|
|
|
_, err = f.Write([]byte("Test"))
|
2017-10-10 11:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
if err.Error() != "write on aborted file" {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2015-07-07 21:29:08 +02:00
|
|
|
}
|