api/handlers: Implement streaming signature v4 support. (#2370)

* api/handlers: Implement streaming signature v4 support.

Fixes #2326

* tests: Add tests for quick/safe
This commit is contained in:
Harshavardhana
2016-08-08 20:56:29 -07:00
committed by GitHub
parent 0c125f3596
commit 7e46055a15
11 changed files with 835 additions and 63 deletions

View File

@@ -35,14 +35,14 @@ type File struct {
// Write writes len(b) bytes to the temporary File. In case of error, the temporary file is removed.
func (file *File) Write(b []byte) (n int, err error) {
if file.aborted {
err = errors.New("write on aborted file")
return
}
if file.closed {
err = errors.New("write on closed file")
return
}
if file.aborted {
err = errors.New("write on aborted file")
return
}
defer func() {
if err != nil {
@@ -64,7 +64,12 @@ func (file *File) Close() (err error) {
}
}()
if file.aborted || file.closed {
if file.closed {
err = errors.New("close on closed file")
return
}
if file.aborted {
err = errors.New("close on aborted file")
return
}
@@ -80,7 +85,12 @@ func (file *File) Close() (err error) {
// Abort aborts the temporary File by closing and removing the temporary file.
func (file *File) Abort() (err error) {
if file.aborted || file.closed {
if file.closed {
err = errors.New("abort on closed file")
return
}
if file.aborted {
err = errors.New("abort on aborted file")
return
}

View File

@@ -19,7 +19,7 @@ package safe
import (
"io/ioutil"
"os"
"path/filepath"
"path"
"testing"
. "gopkg.in/check.v1"
@@ -44,26 +44,60 @@ func (s *MySuite) TearDownSuite(c *C) {
c.Assert(err, IsNil)
}
func (s *MySuite) TestSafe(c *C) {
f, err := CreateFile(filepath.Join(s.root, "testfile"))
c.Assert(err, IsNil)
_, err = os.Stat(filepath.Join(s.root, "testfile"))
c.Assert(err, Not(IsNil))
err = f.Close()
c.Assert(err, IsNil)
_, err = os.Stat(filepath.Join(s.root, "testfile"))
c.Assert(err, IsNil)
err = os.Remove(filepath.Join(s.root, "testfile"))
c.Assert(err, IsNil)
}
func (s *MySuite) TestSafeAbort(c *C) {
f, err := CreateFile(filepath.Join(s.root, "purgefile"))
f, err := CreateFile(path.Join(s.root, "testfile-abort"))
c.Assert(err, IsNil)
_, err = os.Stat(filepath.Join(s.root, "purgefile"))
_, err = os.Stat(path.Join(s.root, "testfile-abort"))
c.Assert(err, Not(IsNil))
err = f.Abort()
c.Assert(err, IsNil)
_, err = os.Stat(filepath.Join(s.root, "purgefile"))
c.Assert(err, Not(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)
_, err = os.Stat(path.Join(s.root, "testfile-close"))
c.Assert(err, Not(IsNil))
err = f.Close()
c.Assert(err, IsNil)
_, err = os.Stat(path.Join(s.root, "testfile-close"))
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")
}
func (s *MySuite) TestSafe(c *C) {
f, err := CreateFile(path.Join(s.root, "testfile-safe"))
c.Assert(err, IsNil)
_, err = os.Stat(path.Join(s.root, "testfile-safe"))
c.Assert(err, Not(IsNil))
err = f.Close()
c.Assert(err, IsNil)
_, 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")
_, err = os.Stat(path.Join(s.root, "testfile-safe"))
c.Assert(err, IsNil)
err = os.Remove(path.Join(s.root, "testfile-safe"))
c.Assert(err, IsNil)
}
func (s *MySuite) TestSafeAbortWrite(c *C) {
f, err := CreateFile(path.Join(s.root, "purgefile-abort"))
c.Assert(err, IsNil)
_, err = os.Stat(path.Join(s.root, "purgefile-abort"))
c.Assert(err, Not(IsNil))
err = f.Abort()
c.Assert(err, IsNil)
_, err = os.Stat(path.Join(s.root, "purgefile-abort"))
c.Assert(err, Not(IsNil))
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")
}