This repository has been archived by the owner on Oct 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
test: Add integration tests for MultipartHTTPSigner #62
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f37c074
test: Add integration tests for MultipartHTTPSigner
abyss-w c3951b9
Merge branch 'master' into multipart-http-signer
abyss-w 833e727
Updates tests
abyss-w 1647161
Merge branch 'master' into multipart-http-signer
abyss-w 7f72ead
Merge branch 'multipart-http-signer' of github.com:abyss-w/go-integra…
abyss-w 605b1b1
Updates imports
abyss-w c1f5cab
Updates delete
abyss-w 4706b45
Updates multipart http signer tests
abyss-w File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
package tests | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"io/ioutil" | ||
"math/rand" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
. "github.com/smartystreets/goconvey/convey" | ||
|
||
"github.com/beyondstorage/go-storage/v4/pairs" | ||
"github.com/beyondstorage/go-storage/v4/pkg/randbytes" | ||
"github.com/beyondstorage/go-storage/v4/types" | ||
) | ||
|
||
func TestMultipartHTTPSignerCreateMultipart(t *testing.T, store types.Storager) { | ||
Convey("Given a basic Storager", t, func() { | ||
signer, ok := store.(types.MultipartHTTPSigner) | ||
So(ok, ShouldBeTrue) | ||
|
||
Convey("When CreateMultipart via QuerySignHTTPCreateMultipart", func() { | ||
path := uuid.New().String() | ||
req, err := signer.QuerySignHTTPCreateMultipart(path, time.Duration(time.Hour)) | ||
|
||
Convey("The error should be nil", func() { | ||
So(err, ShouldBeNil) | ||
|
||
So(req, ShouldNotBeNil) | ||
So(req.URL, ShouldNotBeNil) | ||
}) | ||
|
||
client := http.Client{} | ||
_, err = client.Do(req) | ||
|
||
Convey("The request returned error should be nil", func() { | ||
So(err, ShouldBeNil) | ||
}) | ||
|
||
Convey("List with ModePart should get the object without error", func() { | ||
it, err := store.List(path, pairs.WithListMode(types.ListModePart)) | ||
|
||
So(err, ShouldBeNil) | ||
|
||
o, err := it.Next() | ||
So(err, ShouldBeNil) | ||
So(o, ShouldNotBeNil) | ||
So(o.Path, ShouldEqual, path) | ||
}) | ||
|
||
defer func() { | ||
it, err := store.List(path, pairs.WithListMode(types.ListModePart)) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
o, err := it.Next() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
err = store.Delete(path, pairs.WithMultipartID(o.MustGetMultipartID())) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
}() | ||
}) | ||
}) | ||
} | ||
|
||
func TestMultipartHTTPSignerWriteMultipart(t *testing.T, store types.Storager) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we could combine these test cases into one method for |
||
Convey("Given a basic Storager", t, func() { | ||
signer, ok := store.(types.MultipartHTTPSigner) | ||
So(ok, ShouldBeTrue) | ||
|
||
Convey("When WriteMultipart via QuerySignHTTPWriteMultipart", func() { | ||
path := uuid.New().String() | ||
o, err := store.(types.Multiparter).CreateMultipart(path) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
defer func() { | ||
err := store.Delete(path, pairs.WithMultipartID(o.MustGetMultipartID())) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
}() | ||
|
||
size := rand.Int63n(4 * 1024 * 1024) | ||
content, err := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), size)) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
req, err := signer.QuerySignHTTPWriteMultipart(o, size, 0, time.Duration(time.Hour)) | ||
|
||
Convey("The error should be nil", func() { | ||
So(err, ShouldBeNil) | ||
|
||
So(req, ShouldNotBeNil) | ||
So(req.URL, ShouldNotBeNil) | ||
}) | ||
|
||
req.Body = ioutil.NopCloser(bytes.NewReader(content)) | ||
|
||
client := http.Client{} | ||
resp, err := client.Do(req) | ||
|
||
Convey("The request returned error should be nil", func() { | ||
So(err, ShouldBeNil) | ||
So(resp, ShouldNotBeNil) | ||
}) | ||
|
||
Convey("The size should be match", func() { | ||
So(resp.Request.ContentLength, ShouldEqual, size) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
func TestMultipartHTTPSignerListMultipart(t *testing.T, store types.Storager) { | ||
Convey("Given a basic Storager", t, func() { | ||
signer, ok := store.(types.MultipartHTTPSigner) | ||
So(ok, ShouldBeTrue) | ||
|
||
Convey("When ListMultiPart via QuerySignHTTPListMultiPart", func() { | ||
mu, ok := store.(types.Multiparter) | ||
So(ok, ShouldBeTrue) | ||
|
||
path := uuid.New().String() | ||
o, err := mu.CreateMultipart(path) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
defer func() { | ||
err := store.Delete(path, pairs.WithMultipartID(o.MustGetMultipartID())) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
}() | ||
|
||
size := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB | ||
partNumber := rand.Intn(1000) // Choose a random part number from [0, 1000) | ||
r := io.LimitReader(randbytes.NewRand(), size) | ||
|
||
_, _, err = mu.WriteMultipart(o, r, size, partNumber) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
req, err := signer.QuerySignHTTPListMultipart(o, time.Duration(time.Hour)) | ||
|
||
Convey("The error should be nil", func() { | ||
So(err, ShouldBeNil) | ||
|
||
So(req, ShouldNotBeNil) | ||
So(req.URL, ShouldNotBeNil) | ||
}) | ||
|
||
client := http.Client{} | ||
_, err = client.Do(req) | ||
|
||
Convey("The request returned error should be nil", func() { | ||
So(err, ShouldBeNil) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
func TestMultipartHTTPSignerCompleteMultipart(t *testing.T, store types.Storager) { | ||
Convey("Given a basic Storager", t, func() { | ||
signer, ok := store.(types.MultipartHTTPSigner) | ||
So(ok, ShouldBeTrue) | ||
|
||
Convey("When CompletePart via QuerySignHTTPCompletePart", func() { | ||
mu, ok := store.(types.Multiparter) | ||
So(ok, ShouldBeTrue) | ||
|
||
path := uuid.New().String() | ||
o, err := mu.CreateMultipart(path) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
defer func() { | ||
err := store.Delete(path) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
}() | ||
|
||
size := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB | ||
// Set 0 to `partNumber` here as the part numbers must be continuous for `CompleteMultipartUpload` in `cos` which is different with other storages. | ||
partNumber := 0 | ||
r := io.LimitReader(randbytes.NewRand(), size) | ||
|
||
_, part, err := mu.WriteMultipart(o, r, size, partNumber) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
req, err := signer.QuerySignHTTPCompleteMultipart(o, []*types.Part{part}, time.Duration(time.Hour)) | ||
|
||
Convey("The error should be nil", func() { | ||
So(err, ShouldBeNil) | ||
|
||
So(req, ShouldNotBeNil) | ||
So(req.URL, ShouldNotBeNil) | ||
}) | ||
|
||
client := http.Client{} | ||
_, err = client.Do(req) | ||
|
||
Convey("The request returned error should be nil", func() { | ||
So(err, ShouldBeNil) | ||
}) | ||
|
||
Convey("The object should be readable after complete", func() { | ||
ro, err := store.Stat(path) | ||
|
||
So(err, ShouldBeNil) | ||
So(ro.Mode.IsRead(), ShouldBeTrue) | ||
So(ro.Mode.IsPart(), ShouldBeFalse) | ||
}) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about recording the
MultipartID
and passing it as the deferred call's argument so that there's ni need to callList
again?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not feasible because tests at the same level as
list
do not run thelist
test case and end the test, so there is noo
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about use
store.Create()
to build theo *Object
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we use
store.Create()
to buildo *Object
we still needmultipartID
.