Skip to content

Commit

Permalink
Transaction Validation - Blob Client (#20550)
Browse files Browse the repository at this point in the history
* Test for CopyFromURL in blob client

* Update sdk/storage/azblob/blob/client_test.go

Co-authored-by: Sean McCullough <44180881+seanmcc-msft@users.noreply.github.com>

---------

Co-authored-by: Sean McCullough <44180881+seanmcc-msft@users.noreply.github.com>
  • Loading branch information
siminsavani-msft and seanmcc-msft authored Apr 5, 2023
1 parent 6d09fb8 commit dd74ea3
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions sdk/storage/azblob/blob/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,67 @@ func waitForCopy(_require *require.Assertions, copyBlobClient *blockblob.Client,
}
}

func (s *BlobUnrecordedTestsSuite) TestCopyBlockBlobFromUrlSourceContentMD5() {
_require := require.New(s.T())
testName := s.T().Name()
svcClient, err := testcommon.GetServiceClient(s.T(), testcommon.TestAccountDefault, nil)
if err != nil {
s.Fail("Unable to fetch service client because " + err.Error())
}

containerName := testcommon.GenerateContainerName(testName)
containerClient := testcommon.CreateNewContainer(context.Background(), _require, containerName, svcClient)
defer testcommon.DeleteContainer(context.Background(), _require, containerClient)

const contentSize = 8 * 1024 // 8 KB
content := make([]byte, contentSize)
contentMD5 := md5.Sum(content)
body := bytes.NewReader(content)

srcBlob := containerClient.NewBlockBlobClient("srcblob")
destBlob := containerClient.NewBlockBlobClient("destblob")

// Prepare source bbClient for copy.
_, err = srcBlob.Upload(context.Background(), streaming.NopCloser(body), nil)
_require.Nil(err)

expiryTime, err := time.Parse(time.UnixDate, "Fri Jun 11 20:00:00 UTC 2049")
_require.Nil(err)

credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
if err != nil {
s.T().Fatal("Couldn't fetch credential because " + err.Error())
}

// Get source blob url with SAS for StageFromURL.
sasQueryParams, err := sas.AccountSignatureValues{
Protocol: sas.ProtocolHTTPS,
ExpiryTime: expiryTime,
Permissions: to.Ptr(sas.AccountPermissions{Read: true, List: true}).String(),
ResourceTypes: to.Ptr(sas.AccountResourceTypes{Container: true, Object: true}).String(),
}.SignWithSharedKey(credential)
_require.Nil(err)

srcBlobParts, _ := blob.ParseURL(srcBlob.URL())
srcBlobParts.SAS = sasQueryParams
srcBlobURLWithSAS := srcBlobParts.String()

// Invoke CopyFromURL.
sourceContentMD5 := contentMD5[:]
resp, err := destBlob.CopyFromURL(context.Background(), srcBlobURLWithSAS, &blob.CopyFromURLOptions{
SourceContentMD5: sourceContentMD5,
})
_require.Nil(err)
_require.EqualValues(resp.ContentMD5, sourceContentMD5)

// Provide bad MD5 and make sure the copy fails
_, badMD5 := testcommon.GetRandomDataAndReader(16)
resp, err = destBlob.CopyFromURL(context.Background(), srcBlobURLWithSAS, &blob.CopyFromURLOptions{
SourceContentMD5: badMD5,
})
_require.NotNil(err)
}

func (s *BlobRecordedTestsSuite) TestBlobStartCopyDestEmpty() {
_require := require.New(s.T())
testName := s.T().Name()
Expand Down

0 comments on commit dd74ea3

Please sign in to comment.