Skip to content

Commit

Permalink
fix: [main] cloud uploader should set content type (#5470)
Browse files Browse the repository at this point in the history
* fix: cloud uploader should match content type (#5456)

* fix: cloud uploader should match content type by name (#5458)

* fix: presigned url needs to send contentType (#5461)
  • Loading branch information
povilasv authored May 21, 2024
1 parent 35b96dd commit c16ae5c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
5 changes: 5 additions & 0 deletions pkg/cloud/data/artifact/scraper_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestCloudScraper_ArchiveFilesystemExtractor_Integration(t *testing.T) {
ExecutionID: "my-execution-id",
TestName: "my-test",
TestSuiteName: "my-test-suite",
ContentType: "application/gzip",
}
mockExecutor.
EXPECT().
Expand All @@ -77,6 +78,7 @@ func TestCloudScraper_ArchiveFilesystemExtractor_Integration(t *testing.T) {
ExecutionID: "my-execution-id",
TestName: "my-test",
TestSuiteName: "my-test-suite",
ContentType: "text/plain",
}
mockExecutor.
EXPECT().
Expand Down Expand Up @@ -154,6 +156,7 @@ func TestCloudScraper_RecursiveFilesystemExtractor_Integration(t *testing.T) {
ExecutionID: "my-execution-id",
TestName: "my-test",
TestSuiteName: "my-test-suite",
ContentType: "text/plain",
}
mockExecutor.
EXPECT().
Expand All @@ -165,6 +168,7 @@ func TestCloudScraper_RecursiveFilesystemExtractor_Integration(t *testing.T) {
ExecutionID: "my-execution-id",
TestName: "my-test",
TestSuiteName: "my-test-suite",
ContentType: "text/plain",
}
mockExecutor.
EXPECT().
Expand All @@ -176,6 +180,7 @@ func TestCloudScraper_RecursiveFilesystemExtractor_Integration(t *testing.T) {
ExecutionID: "my-execution-id",
TestName: "my-test",
TestSuiteName: "my-test-suite",
ContentType: "text/plain",
}
mockExecutor.
EXPECT().
Expand Down
12 changes: 7 additions & 5 deletions pkg/cloud/data/artifact/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"crypto/tls"
"encoding/json"
"io"
"net/http"
"path/filepath"

Expand Down Expand Up @@ -33,18 +32,21 @@ func NewCloudUploader(executor executor.Executor, skipVerify bool) *CloudUploade

func (u *CloudUploader) Upload(ctx context.Context, object *scraper.Object, execution testkube.Execution) error {
log.DefaultLogger.Debugw("cloud uploader is requesting signed URL", "file", object.Name, "folder", execution.Id, "size", object.Size)

contentType := getContentType(object.Name)
req := &PutObjectSignedURLRequest{
Object: object.Name,
ExecutionID: execution.Id,
TestName: execution.TestName,
TestSuiteName: execution.TestSuiteName,
ContentType: contentType,
}
signedURL, err := u.getSignedURL(ctx, req)
if err != nil {
return errors.Wrapf(err, "failed to get signed URL for object [%s]", req.Object)
}

if err := u.putObject(ctx, signedURL, object.Data); err != nil {
if err := u.putObject(ctx, signedURL, object, contentType); err != nil {
return errors.Wrapf(err, "failed to send object [%s] to cloud", req.Object)
}

Expand All @@ -65,13 +67,13 @@ func (u *CloudUploader) getSignedURL(ctx context.Context, req *PutObjectSignedUR
return commandResponse.URL, nil
}

func (u *CloudUploader) putObject(ctx context.Context, url string, data io.Reader) error {
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, data)
func (u *CloudUploader) putObject(ctx context.Context, url string, object *scraper.Object, contentType string) error {
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, object.Data)
if err != nil {
return err
}

req.Header.Set("Content-Type", "application/octet-stream")
req.Header.Set("Content-Type", contentType)
tr := http.DefaultTransport.(*http.Transport).Clone()
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: u.skipVerify}
client := &http.Client{Transport: tr}
Expand Down
2 changes: 2 additions & 0 deletions pkg/cloud/data/artifact/uploader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func TestCloudLoader_Load(t *testing.T) {
ExecutionID: "my-execution-id",
TestName: "my-test",
TestSuiteName: "my-test-suite",
ContentType: "text/plain",
}

mockExecutor.EXPECT().Execute(gomock.Any(), cloudscraper.CmdScraperPutObjectSignedURL, gomock.Eq(req)).Return([]byte(`{"URL":"`+testServer.URL+`/dummy"}`), nil).Times(1)
Expand All @@ -79,6 +80,7 @@ func TestCloudLoader_Load(t *testing.T) {
ExecutionID: "my-execution-id",
TestName: "my-test",
TestSuiteName: "my-test-suite",
ContentType: "text/plain",
}

mockExecutor.EXPECT().Execute(gomock.Any(), cloudscraper.CmdScraperPutObjectSignedURL, gomock.Eq(req)).Return(nil, errors.New("connection error")).Times(1)
Expand Down

0 comments on commit c16ae5c

Please sign in to comment.