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-08-23 03:34:00 +02:00
|
|
|
. "gopkg.in/check.v1"
|
2015-07-07 21:29:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test(t *testing.T) { TestingT(t) }
|
|
|
|
|
2015-09-20 22:50:07 +02:00
|
|
|
type MySuite struct {
|
|
|
|
root string
|
|
|
|
}
|
2015-07-07 21:29:08 +02:00
|
|
|
|
|
|
|
var _ = Suite(&MySuite{})
|
|
|
|
|
2015-09-20 22:50:07 +02:00
|
|
|
func (s *MySuite) SetUpSuite(c *C) {
|
2016-05-27 06:43:33 +02:00
|
|
|
root, err := ioutil.TempDir(os.TempDir(), "safe_test.go.")
|
2015-07-07 21:29:08 +02:00
|
|
|
c.Assert(err, IsNil)
|
2015-09-20 22:50:07 +02:00
|
|
|
s.root = root
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MySuite) TearDownSuite(c *C) {
|
2016-05-27 06:43:33 +02:00
|
|
|
err := os.Remove(s.root)
|
|
|
|
c.Assert(err, IsNil)
|
2015-09-20 22:50:07 +02:00
|
|
|
}
|
|
|
|
|
2016-08-09 05:56:29 +02:00
|
|
|
func (s *MySuite) TestSafeAbort(c *C) {
|
|
|
|
f, err := CreateFile(path.Join(s.root, "testfile-abort"))
|
|
|
|
c.Assert(err, IsNil)
|
2017-05-02 11:35:27 +02:00
|
|
|
_, err = os2.Stat(path.Join(s.root, "testfile-abort"))
|
2016-08-09 05:56:29 +02:00
|
|
|
c.Assert(err, Not(IsNil))
|
|
|
|
err = f.Abort()
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
err = f.Close()
|
|
|
|
c.Assert(err.Error(), Equals, "close on aborted file")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MySuite) TestSafeClose(c *C) {
|
|
|
|
f, err := CreateFile(path.Join(s.root, "testfile-close"))
|
|
|
|
c.Assert(err, IsNil)
|
2017-05-02 11:35:27 +02:00
|
|
|
_, err = os2.Stat(path.Join(s.root, "testfile-close"))
|
2016-08-09 05:56:29 +02:00
|
|
|
c.Assert(err, Not(IsNil))
|
|
|
|
err = f.Close()
|
|
|
|
c.Assert(err, IsNil)
|
2017-05-02 11:35:27 +02:00
|
|
|
_, err = os2.Stat(path.Join(s.root, "testfile-close"))
|
2016-08-09 05:56:29 +02:00
|
|
|
c.Assert(err, IsNil)
|
|
|
|
err = os.Remove(path.Join(s.root, "testfile-close"))
|
|
|
|
c.Assert(err, IsNil)
|
|
|
|
err = f.Abort()
|
|
|
|
c.Assert(err.Error(), Equals, "abort on closed file")
|
|
|
|
}
|
|
|
|
|
2016-04-07 01:05:30 +02:00
|
|
|
func (s *MySuite) TestSafe(c *C) {
|
2016-08-09 05:56:29 +02:00
|
|
|
f, err := CreateFile(path.Join(s.root, "testfile-safe"))
|
2015-07-07 21:29:08 +02:00
|
|
|
c.Assert(err, IsNil)
|
2017-05-02 11:35:27 +02:00
|
|
|
_, err = os2.Stat(path.Join(s.root, "testfile-safe"))
|
2015-07-07 21:29:08 +02:00
|
|
|
c.Assert(err, Not(IsNil))
|
|
|
|
err = f.Close()
|
|
|
|
c.Assert(err, IsNil)
|
2016-08-09 05:56:29 +02:00
|
|
|
_, err = f.Write([]byte("Test"))
|
|
|
|
c.Assert(err.Error(), Equals, "write on closed file")
|
|
|
|
err = f.Close()
|
|
|
|
c.Assert(err.Error(), Equals, "close on closed file")
|
2017-05-02 11:35:27 +02:00
|
|
|
_, err = os2.Stat(path.Join(s.root, "testfile-safe"))
|
2015-07-07 21:29:08 +02:00
|
|
|
c.Assert(err, IsNil)
|
2016-08-09 05:56:29 +02:00
|
|
|
err = os.Remove(path.Join(s.root, "testfile-safe"))
|
2016-05-27 06:43:33 +02:00
|
|
|
c.Assert(err, IsNil)
|
2015-07-07 21:29:08 +02:00
|
|
|
}
|
|
|
|
|
2016-08-09 05:56:29 +02:00
|
|
|
func (s *MySuite) TestSafeAbortWrite(c *C) {
|
|
|
|
f, err := CreateFile(path.Join(s.root, "purgefile-abort"))
|
2015-07-07 21:29:08 +02:00
|
|
|
c.Assert(err, IsNil)
|
2017-05-02 11:35:27 +02:00
|
|
|
_, err = os2.Stat(path.Join(s.root, "purgefile-abort"))
|
2015-07-07 21:29:08 +02:00
|
|
|
c.Assert(err, Not(IsNil))
|
2016-05-27 06:43:33 +02:00
|
|
|
err = f.Abort()
|
2015-07-07 21:29:08 +02:00
|
|
|
c.Assert(err, IsNil)
|
2017-05-02 11:35:27 +02:00
|
|
|
_, err = os2.Stat(path.Join(s.root, "purgefile-abort"))
|
2015-07-07 21:29:08 +02:00
|
|
|
c.Assert(err, Not(IsNil))
|
2016-08-09 05:56:29 +02:00
|
|
|
err = f.Abort()
|
|
|
|
c.Assert(err.Error(), Equals, "abort on aborted file")
|
|
|
|
_, err = f.Write([]byte("Test"))
|
|
|
|
c.Assert(err.Error(), Equals, "write on aborted file")
|
2015-07-07 21:29:08 +02:00
|
|
|
}
|