added basic s3blob tests

This commit is contained in:
Gani Georgiev
2025-03-08 22:28:51 +02:00
parent f799083c4f
commit ed36add853
13 changed files with 676 additions and 121 deletions
@@ -8,17 +8,18 @@ import (
"testing"
"github.com/pocketbase/pocketbase/tools/filesystem/internal/s3blob/s3"
"github.com/pocketbase/pocketbase/tools/filesystem/internal/s3blob/s3/tests"
)
func TestS3CopyObject(t *testing.T) {
t.Parallel()
httpClient := NewTestClient(
&RequestStub{
httpClient := tests.NewClient(
&tests.RequestStub{
Method: http.MethodPut,
URL: "http://test_bucket.example.com/@dst_test",
Match: func(req *http.Request) bool {
return checkHeaders(req.Header, map[string]string{
return tests.ExpectHeaders(req.Header, map[string]string{
"test_header": "test",
"x-amz-copy-source": "test_bucket%2F@src_test",
"Authorization": "^.+Credential=123/.+$",
@@ -6,17 +6,18 @@ import (
"testing"
"github.com/pocketbase/pocketbase/tools/filesystem/internal/s3blob/s3"
"github.com/pocketbase/pocketbase/tools/filesystem/internal/s3blob/s3/tests"
)
func TestS3DeleteObject(t *testing.T) {
t.Parallel()
httpClient := NewTestClient(
&RequestStub{
httpClient := tests.NewClient(
&tests.RequestStub{
Method: http.MethodDelete,
URL: "http://test_bucket.example.com/test_key",
Match: func(req *http.Request) bool {
return checkHeaders(req.Header, map[string]string{
return tests.ExpectHeaders(req.Header, map[string]string{
"test_header": "test",
"Authorization": "^.+Credential=123/.+$",
})
@@ -9,17 +9,18 @@ import (
"testing"
"github.com/pocketbase/pocketbase/tools/filesystem/internal/s3blob/s3"
"github.com/pocketbase/pocketbase/tools/filesystem/internal/s3blob/s3/tests"
)
func TestS3GetObject(t *testing.T) {
t.Parallel()
httpClient := NewTestClient(
&RequestStub{
httpClient := tests.NewClient(
&tests.RequestStub{
Method: http.MethodGet,
URL: "http://test_bucket.example.com/test_key",
Match: func(req *http.Request) bool {
return checkHeaders(req.Header, map[string]string{
return tests.ExpectHeaders(req.Header, map[string]string{
"test_header": "test",
"Authorization": "^.+Credential=123/.+$",
})
@@ -7,17 +7,18 @@ import (
"testing"
"github.com/pocketbase/pocketbase/tools/filesystem/internal/s3blob/s3"
"github.com/pocketbase/pocketbase/tools/filesystem/internal/s3blob/s3/tests"
)
func TestS3HeadObject(t *testing.T) {
t.Parallel()
httpClient := NewTestClient(
&RequestStub{
httpClient := tests.NewClient(
&tests.RequestStub{
Method: http.MethodHead,
URL: "http://test_bucket.example.com/test_key",
Match: func(req *http.Request) bool {
return checkHeaders(req.Header, map[string]string{
return tests.ExpectHeaders(req.Header, map[string]string{
"test_header": "test",
"Authorization": "^.+Credential=123/.+$",
})
@@ -9,6 +9,7 @@ import (
"testing"
"github.com/pocketbase/pocketbase/tools/filesystem/internal/s3blob/s3"
"github.com/pocketbase/pocketbase/tools/filesystem/internal/s3blob/s3/tests"
)
func TestS3ListParamsEncode(t *testing.T) {
@@ -62,12 +63,12 @@ func TestS3ListObjects(t *testing.T) {
FetchOwner: true,
}
httpClient := NewTestClient(
&RequestStub{
httpClient := tests.NewClient(
&tests.RequestStub{
Method: http.MethodGet,
URL: "http://test_bucket.example.com/?" + listParams.Encode(),
Match: func(req *http.Request) bool {
return checkHeaders(req.Header, map[string]string{
return tests.ExpectHeaders(req.Header, map[string]string{
"test_header": "test",
"Authorization": "^.+Credential=123/.+$",
})
@@ -7,6 +7,7 @@ import (
"testing"
"github.com/pocketbase/pocketbase/tools/filesystem/internal/s3blob/s3"
"github.com/pocketbase/pocketbase/tools/filesystem/internal/s3blob/s3/tests"
)
func TestS3URL(t *testing.T) {
@@ -111,12 +112,12 @@ func TestS3SignAndSend(t *testing.T) {
Endpoint: "https://example.com/",
AccessKey: "123",
SecretKey: "abc",
Client: NewTestClient(&RequestStub{
Client: tests.NewClient(&tests.RequestStub{
Method: http.MethodGet,
URL: "https://test_bucket.example.com/test",
Response: testResponse(),
Match: func(req *http.Request) bool {
return checkHeaders(req.Header, map[string]string{
return tests.ExpectHeaders(req.Header, map[string]string{
"Authorization": "AWS4-HMAC-SHA256 Credential=123/20250102/test_region/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=ea093662bc1deef08dfb4ac35453dfaad5ea89edf102e9dd3b7156c9a27e4c1f",
"Host": "test_bucket.example.com",
"X-Amz-Content-Sha256": "UNSIGNED-PAYLOAD",
@@ -137,12 +138,12 @@ func TestS3SignAndSend(t *testing.T) {
Endpoint: "https://example.com/",
AccessKey: "456",
SecretKey: "def",
Client: NewTestClient(&RequestStub{
Client: tests.NewClient(&tests.RequestStub{
Method: http.MethodGet,
URL: "https://test_bucket.example.com/test",
Response: testResponse(),
Match: func(req *http.Request) bool {
return checkHeaders(req.Header, map[string]string{
return tests.ExpectHeaders(req.Header, map[string]string{
"Authorization": "AWS4-HMAC-SHA256 Credential=456/20250102/test_region/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=17510fa1f724403dd0a563b61c9b31d1d718f877fcbd75455620d17a8afce5fb",
"Host": "test_bucket.example.com",
"X-Amz-Content-Sha256": "UNSIGNED-PAYLOAD",
@@ -168,12 +169,12 @@ func TestS3SignAndSend(t *testing.T) {
Endpoint: "https://example.com/",
AccessKey: "123",
SecretKey: "abc",
Client: NewTestClient(&RequestStub{
Client: tests.NewClient(&tests.RequestStub{
Method: http.MethodGet,
URL: "https://test_bucket.example.com/test",
Response: testResponse(),
Match: func(req *http.Request) bool {
return checkHeaders(req.Header, map[string]string{
return tests.ExpectHeaders(req.Header, map[string]string{
"authorization": "AWS4-HMAC-SHA256 Credential=123/20250102/test_region/s3/aws4_request, SignedHeaders=content-type;host;x-amz-content-sha256;x-amz-date;x-amz-example;x-amz-meta-a, Signature=86dccbcd012c33073dc99e9d0a9e0b717a4d8c11c37848cfa9a4a02716bc0db3",
"host": "test_bucket.example.com",
"x-amz-date": "20250102T150405Z",
@@ -205,7 +206,7 @@ func TestS3SignAndSend(t *testing.T) {
}
defer resp.Body.Close()
err = s.s3Client.Client.(*TestClient).AssertNoRemaining()
err = s.s3Client.Client.(*tests.Client).AssertNoRemaining()
if err != nil {
t.Fatal(err)
}
@@ -1,4 +1,6 @@
package s3_test
// Package tests contains various tests helpers and utilities to assist
// with the S3 client testing.
package tests
import (
"errors"
@@ -11,26 +13,9 @@ import (
"sync"
)
func checkHeaders(headers http.Header, expectations map[string]string) bool {
for h, expected := range expectations {
v := headers.Get(h)
pattern := expected
if !strings.HasPrefix(pattern, "^") && !strings.HasSuffix(pattern, "$") {
pattern = "^" + regexp.QuoteMeta(pattern) + "$"
}
expectedRegex, err := regexp.Compile(pattern)
if err != nil {
return false
}
if !expectedRegex.MatchString(v) {
return false
}
}
return true
// NewClient creates a new test Client loaded with the specified RequestStubs.
func NewClient(stubs ...*RequestStub) *Client {
return &Client{stubs: stubs}
}
type RequestStub struct {
@@ -40,16 +25,13 @@ type RequestStub struct {
Response *http.Response
}
func NewTestClient(stubs ...*RequestStub) *TestClient {
return &TestClient{stubs: stubs}
}
type TestClient struct {
type Client struct {
stubs []*RequestStub
mu sync.Mutex
}
func (c *TestClient) AssertNoRemaining() error {
// AssertNoRemaining asserts that current client has no unprocessed requests remaining.
func (c *Client) AssertNoRemaining() error {
c.mu.Lock()
defer c.mu.Unlock()
@@ -66,7 +48,8 @@ func (c *TestClient) AssertNoRemaining() error {
return errors.New(strings.Join(msgParts, "\n"))
}
func (c *TestClient) Do(req *http.Request) (*http.Response, error) {
// Do implements the [s3.HTTPClient] interface.
func (c *Client) Do(req *http.Request) (*http.Response, error) {
c.mu.Lock()
defer c.mu.Unlock()
@@ -0,0 +1,33 @@
package tests
import (
"net/http"
"regexp"
"strings"
)
// ExpectHeaders checks whether specified headers match the expectations.
// The expectations map entry key is the header name.
// The expectations map entry value is the first header value. If wrapped with `^...$`
// it is compared as regular expression.
func ExpectHeaders(headers http.Header, expectations map[string]string) bool {
for h, expected := range expectations {
v := headers.Get(h)
pattern := expected
if !strings.HasPrefix(pattern, "^") && !strings.HasSuffix(pattern, "$") {
pattern = "^" + regexp.QuoteMeta(pattern) + "$"
}
expectedRegex, err := regexp.Compile(pattern)
if err != nil {
return false
}
if !expectedRegex.MatchString(v) {
return false
}
}
return true
}
@@ -8,13 +8,14 @@ import (
"testing"
"github.com/pocketbase/pocketbase/tools/filesystem/internal/s3blob/s3"
"github.com/pocketbase/pocketbase/tools/filesystem/internal/s3blob/s3/tests"
)
func TestUploaderRequiredFields(t *testing.T) {
t.Parallel()
s3Client := &s3.S3{
Client: NewTestClient(&RequestStub{Method: "PUT", URL: `^.+$`}), // match every upload
Client: tests.NewClient(&tests.RequestStub{Method: "PUT", URL: `^.+$`}), // match every upload
Region: "test_region",
Bucket: "test_bucket",
Endpoint: "http://example.com",
@@ -71,8 +72,8 @@ func TestUploaderRequiredFields(t *testing.T) {
func TestUploaderSingleUpload(t *testing.T) {
t.Parallel()
httpClient := NewTestClient(
&RequestStub{
httpClient := tests.NewClient(
&tests.RequestStub{
Method: http.MethodPut,
URL: "http://test_bucket.example.com/test_key",
Match: func(req *http.Request) bool {
@@ -81,7 +82,7 @@ func TestUploaderSingleUpload(t *testing.T) {
return false
}
return string(body) == "abcdefg" && checkHeaders(req.Header, map[string]string{
return string(body) == "abcdefg" && tests.ExpectHeaders(req.Header, map[string]string{
"Content-Length": "7",
"x-amz-meta-a": "123",
"x-amz-meta-b": "456",
@@ -123,12 +124,12 @@ func TestUploaderSingleUpload(t *testing.T) {
func TestUploaderMultipartUploadSuccess(t *testing.T) {
t.Parallel()
httpClient := NewTestClient(
&RequestStub{
httpClient := tests.NewClient(
&tests.RequestStub{
Method: http.MethodPost,
URL: "http://test_bucket.example.com/test_key?uploads",
Match: func(req *http.Request) bool {
return checkHeaders(req.Header, map[string]string{
return tests.ExpectHeaders(req.Header, map[string]string{
"x-amz-meta-a": "123",
"x-amz-meta-b": "456",
"test_header": "test",
@@ -146,7 +147,7 @@ func TestUploaderMultipartUploadSuccess(t *testing.T) {
`)),
},
},
&RequestStub{
&tests.RequestStub{
Method: http.MethodPut,
URL: "http://test_bucket.example.com/test_key?partNumber=1&uploadId=test_id",
Match: func(req *http.Request) bool {
@@ -155,7 +156,7 @@ func TestUploaderMultipartUploadSuccess(t *testing.T) {
return false
}
return string(body) == "abc" && checkHeaders(req.Header, map[string]string{
return string(body) == "abc" && tests.ExpectHeaders(req.Header, map[string]string{
"Content-Length": "3",
"test_header": "test",
"Authorization": "^.+Credential=123/.+$",
@@ -165,7 +166,7 @@ func TestUploaderMultipartUploadSuccess(t *testing.T) {
Header: http.Header{"Etag": []string{"etag1"}},
},
},
&RequestStub{
&tests.RequestStub{
Method: http.MethodPut,
URL: "http://test_bucket.example.com/test_key?partNumber=2&uploadId=test_id",
Match: func(req *http.Request) bool {
@@ -174,7 +175,7 @@ func TestUploaderMultipartUploadSuccess(t *testing.T) {
return false
}
return string(body) == "def" && checkHeaders(req.Header, map[string]string{
return string(body) == "def" && tests.ExpectHeaders(req.Header, map[string]string{
"Content-Length": "3",
"test_header": "test",
"Authorization": "^.+Credential=123/.+$",
@@ -184,7 +185,7 @@ func TestUploaderMultipartUploadSuccess(t *testing.T) {
Header: http.Header{"Etag": []string{"etag2"}},
},
},
&RequestStub{
&tests.RequestStub{
Method: http.MethodPut,
URL: "http://test_bucket.example.com/test_key?partNumber=3&uploadId=test_id",
Match: func(req *http.Request) bool {
@@ -192,7 +193,7 @@ func TestUploaderMultipartUploadSuccess(t *testing.T) {
if err != nil {
return false
}
return string(body) == "g" && checkHeaders(req.Header, map[string]string{
return string(body) == "g" && tests.ExpectHeaders(req.Header, map[string]string{
"Content-Length": "1",
"test_header": "test",
"Authorization": "^.+Credential=123/.+$",
@@ -202,7 +203,7 @@ func TestUploaderMultipartUploadSuccess(t *testing.T) {
Header: http.Header{"Etag": []string{"etag3"}},
},
},
&RequestStub{
&tests.RequestStub{
Method: http.MethodPost,
URL: "http://test_bucket.example.com/test_key?uploadId=test_id",
Match: func(req *http.Request) bool {
@@ -213,7 +214,7 @@ func TestUploaderMultipartUploadSuccess(t *testing.T) {
expected := `<CompleteMultipartUpload><Part><ETag>etag1</ETag><PartNumber>1</PartNumber></Part><Part><ETag>etag2</ETag><PartNumber>2</PartNumber></Part><Part><ETag>etag3</ETag><PartNumber>3</PartNumber></Part></CompleteMultipartUpload>`
return strings.Contains(string(body), expected) && checkHeaders(req.Header, map[string]string{
return strings.Contains(string(body), expected) && tests.ExpectHeaders(req.Header, map[string]string{
"test_header": "test",
"Authorization": "^.+Credential=123/.+$",
})
@@ -252,12 +253,12 @@ func TestUploaderMultipartUploadSuccess(t *testing.T) {
func TestUploaderMultipartUploadPartFailure(t *testing.T) {
t.Parallel()
httpClient := NewTestClient(
&RequestStub{
httpClient := tests.NewClient(
&tests.RequestStub{
Method: http.MethodPost,
URL: "http://test_bucket.example.com/test_key?uploads",
Match: func(req *http.Request) bool {
return checkHeaders(req.Header, map[string]string{
return tests.ExpectHeaders(req.Header, map[string]string{
"x-amz-meta-a": "123",
"x-amz-meta-b": "456",
"test_header": "test",
@@ -275,7 +276,7 @@ func TestUploaderMultipartUploadPartFailure(t *testing.T) {
`)),
},
},
&RequestStub{
&tests.RequestStub{
Method: http.MethodPut,
URL: "http://test_bucket.example.com/test_key?partNumber=1&uploadId=test_id",
Match: func(req *http.Request) bool {
@@ -283,7 +284,7 @@ func TestUploaderMultipartUploadPartFailure(t *testing.T) {
if err != nil {
return false
}
return string(body) == "abc" && checkHeaders(req.Header, map[string]string{
return string(body) == "abc" && tests.ExpectHeaders(req.Header, map[string]string{
"Content-Length": "3",
"test_header": "test",
"Authorization": "^.+Credential=123/.+$",
@@ -293,11 +294,11 @@ func TestUploaderMultipartUploadPartFailure(t *testing.T) {
Header: http.Header{"Etag": []string{"etag1"}},
},
},
&RequestStub{
&tests.RequestStub{
Method: http.MethodPut,
URL: "http://test_bucket.example.com/test_key?partNumber=2&uploadId=test_id",
Match: func(req *http.Request) bool {
return checkHeaders(req.Header, map[string]string{
return tests.ExpectHeaders(req.Header, map[string]string{
"test_header": "test",
"Authorization": "^.+Credential=123/.+$",
})
@@ -306,11 +307,11 @@ func TestUploaderMultipartUploadPartFailure(t *testing.T) {
StatusCode: 400,
},
},
&RequestStub{
&tests.RequestStub{
Method: http.MethodDelete,
URL: "http://test_bucket.example.com/test_key?uploadId=test_id",
Match: func(req *http.Request) bool {
return checkHeaders(req.Header, map[string]string{
return tests.ExpectHeaders(req.Header, map[string]string{
"test_header": "test",
"Authorization": "^.+Credential=123/.+$",
})
@@ -349,12 +350,12 @@ func TestUploaderMultipartUploadPartFailure(t *testing.T) {
func TestUploaderMultipartUploadCompleteFailure(t *testing.T) {
t.Parallel()
httpClient := NewTestClient(
&RequestStub{
httpClient := tests.NewClient(
&tests.RequestStub{
Method: http.MethodPost,
URL: "http://test_bucket.example.com/test_key?uploads",
Match: func(req *http.Request) bool {
return checkHeaders(req.Header, map[string]string{
return tests.ExpectHeaders(req.Header, map[string]string{
"x-amz-meta-a": "123",
"x-amz-meta-b": "456",
"test_header": "test",
@@ -372,7 +373,7 @@ func TestUploaderMultipartUploadCompleteFailure(t *testing.T) {
`)),
},
},
&RequestStub{
&tests.RequestStub{
Method: http.MethodPut,
URL: "http://test_bucket.example.com/test_key?partNumber=1&uploadId=test_id",
Match: func(req *http.Request) bool {
@@ -380,7 +381,7 @@ func TestUploaderMultipartUploadCompleteFailure(t *testing.T) {
if err != nil {
return false
}
return string(body) == "abc" && checkHeaders(req.Header, map[string]string{
return string(body) == "abc" && tests.ExpectHeaders(req.Header, map[string]string{
"Content-Length": "3",
"test_header": "test",
"Authorization": "^.+Credential=123/.+$",
@@ -390,7 +391,7 @@ func TestUploaderMultipartUploadCompleteFailure(t *testing.T) {
Header: http.Header{"Etag": []string{"etag1"}},
},
},
&RequestStub{
&tests.RequestStub{
Method: http.MethodPut,
URL: "http://test_bucket.example.com/test_key?partNumber=2&uploadId=test_id",
Match: func(req *http.Request) bool {
@@ -398,7 +399,7 @@ func TestUploaderMultipartUploadCompleteFailure(t *testing.T) {
if err != nil {
return false
}
return string(body) == "def" && checkHeaders(req.Header, map[string]string{
return string(body) == "def" && tests.ExpectHeaders(req.Header, map[string]string{
"Content-Length": "3",
"test_header": "test",
"Authorization": "^.+Credential=123/.+$",
@@ -408,11 +409,11 @@ func TestUploaderMultipartUploadCompleteFailure(t *testing.T) {
Header: http.Header{"Etag": []string{"etag2"}},
},
},
&RequestStub{
&tests.RequestStub{
Method: http.MethodPost,
URL: "http://test_bucket.example.com/test_key?uploadId=test_id",
Match: func(req *http.Request) bool {
return checkHeaders(req.Header, map[string]string{
return tests.ExpectHeaders(req.Header, map[string]string{
"test_header": "test",
"Authorization": "^.+Credential=123/.+$",
})
@@ -421,11 +422,11 @@ func TestUploaderMultipartUploadCompleteFailure(t *testing.T) {
StatusCode: 400,
},
},
&RequestStub{
&tests.RequestStub{
Method: http.MethodDelete,
URL: "http://test_bucket.example.com/test_key?uploadId=test_id",
Match: func(req *http.Request) bool {
return checkHeaders(req.Header, map[string]string{
return tests.ExpectHeaders(req.Header, map[string]string{
"test_header": "test",
"Authorization": "^.+Credential=123/.+$",
})