Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

storage: Implement StorageHTTPSigner test #49

Merged
merged 6 commits into from Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -3,7 +3,7 @@ module github.com/beyondstorage/go-integration-test/v4
go 1.15

require (
github.com/beyondstorage/go-storage/v4 v4.4.0
github.com/beyondstorage/go-storage/v4 v4.5.0
github.com/google/uuid v1.3.0
github.com/smartystreets/goconvey v1.6.4
)
4 changes: 2 additions & 2 deletions go.sum
@@ -1,6 +1,6 @@
github.com/Xuanwo/templateutils v0.1.0/go.mod h1:OdE0DJ+CJxDBq6psX5DPV+gOZi8bhuHuVUpPCG++Wb8=
github.com/beyondstorage/go-storage/v4 v4.4.0 h1:sWURraKFjNR4qpwthr45cAGOIx6EOLrrJcz6su4Je30=
github.com/beyondstorage/go-storage/v4 v4.4.0/go.mod h1:mc9VzBImjXDg1/1sLfta2MJH79elfM6m47ZZvZ+q/Uw=
github.com/beyondstorage/go-storage/v4 v4.5.0 h1:cWkX1A+yVLldEpj/Ad9capev9K1fbVIVcRvwqWsMgpw=
github.com/beyondstorage/go-storage/v4 v4.5.0/go.mod h1:mc9VzBImjXDg1/1sLfta2MJH79elfM6m47ZZvZ+q/Uw=
github.com/dave/dst v0.26.2/go.mod h1:UMDJuIRPfyUCC78eFuB+SV/WI8oDeyFDvM/JR6NI3IU=
github.com/dave/gopackages v0.0.0-20170318123100-46e7023ec56e/go.mod h1:i00+b/gKdIDIxuLDFob7ustLAVqhsZRk2qVZrArELGQ=
github.com/dave/jennifer v1.2.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhrIygKg=
Expand Down
124 changes: 124 additions & 0 deletions http_signer.go
@@ -0,0 +1,124 @@
package tests

import (
"bytes"
"crypto/sha256"
"io"
"io/ioutil"
"math/rand"
"net/http"
"testing"
"time"

"github.com/google/uuid"
. "github.com/smartystreets/goconvey/convey"

"github.com/beyondstorage/go-storage/v4/pkg/randbytes"
"github.com/beyondstorage/go-storage/v4/types"
)

func TestHTTPSignerRead(t *testing.T, store types.Storager) {
Convey("Given a basic Storager", t, func() {
signer, ok := store.(types.HTTPSigner)
So(ok, ShouldBeTrue)

Convey("When read via QuerySignHTTP", func() {
size := rand.Int63n(4 * 1024 * 1024)
content, err := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), size))
if err != nil {
t.Error(err)
}

path := uuid.New().String()
_, err = store.Write(path, bytes.NewReader(content), size)
if err != nil {
t.Error(err)
}
defer func() {
err := store.Delete(path)
if err != nil {
t.Error(err)
}
}()

req, err := signer.QuerySignHTTP(types.OpStoragerRead, 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{}
resp, err := client.Do(req)
Convey("The request returned error should be nil", func() {
So(err, ShouldBeNil)
So(resp, ShouldNotBeNil)
})

defer resp.Body.Close()

buf, err := ioutil.ReadAll(resp.Body)
Convey("The content should be match", func() {
So(err, ShouldBeNil)
So(buf, ShouldNotBeNil)

So(resp.ContentLength, ShouldEqual, size)
So(sha256.Sum256(buf), ShouldResemble, sha256.Sum256(content))
})
})
})
}

func TestHTTPSignerWrite(t *testing.T, store types.Storager) {
Convey("Given a basic Storager", t, func() {
signer, ok := store.(types.HTTPSigner)
So(ok, ShouldBeTrue)

Convey("When write via QuerySignHTTP", func() {
size := rand.Int63n(4 * 1024 * 1024)
content, err := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), size))
if err != nil {
t.Error(err)
}

path := uuid.New().String()
req, err := signer.QuerySignHTTP(types.OpStoragerWrite, path, time.Duration(time.Hour))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to set size here?

I think we need a demo to make sure our test logic is correct. Maybe in s3?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to set size here?

Dose it mean set Content-Length? I tried to add req.Header.Add("Content-Length", strconv.FormatInt(size,10)) and tested with qingstor, the request was successful, but the request is chunked transfer encoding. So I removed the content-length setting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the comment for Header in http.Request, for client requests, certain headers such as Content-Length and Connection are automatically written when needed and values in Header may be ignored.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a demo to make sure our test logic is correct. Maybe in s3?

I'll privde a demo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chunked transfer encoding

Strange, chunked transfer encoding should be used for requests that don't have content-length header.

Let's discuss the detail in the demo of s3 instead.


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{}
_, err = client.Do(req)
Convey("The request returned error should be nil", func() {
So(err, ShouldBeNil)
})

defer func() {
err := store.Delete(path)
if err != nil {
t.Error(err)
}
}()

Convey("Read should get object data without error", func() {
var buf bytes.Buffer
n, err := store.Read(path, &buf)

Convey("The content should be match", func() {
So(err, ShouldBeNil)
So(buf, ShouldNotBeNil)

So(n, ShouldEqual, size)
So(sha256.Sum256(buf.Bytes()), ShouldResemble, sha256.Sum256(content))
})
})
})
})
}