Skip to content

Commit

Permalink
Adding tests for FilterBlobs and fixing encoding issue (#19742)
Browse files Browse the repository at this point in the history
* Adding tests for FilterBlobs and fixing encoding issue

* Adding changes to generated files

* Updating CHANGELOG.md

* fix tests

* Updating length checks in tests

* Adding sleep to test

* Adding sleep for pageblob

* updating pageblob test
  • Loading branch information
siminsavani-msft committed Jan 6, 2023
1 parent 00a8837 commit 2679c6b
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 73 deletions.
1 change: 1 addition & 0 deletions sdk/storage/azblob/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Breaking Changes

### Bugs Fixed
* Fixed encoding issues seen in FilterBlobs. Fixes [#17421](https://github.com/Azure/azure-sdk-for-go/issues/17421).

### Other Changes

Expand Down
11 changes: 11 additions & 0 deletions sdk/storage/azblob/appendblob/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"context"
"crypto/md5"
"encoding/binary"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/service"
"hash/crc64"
"io"
"math/rand"
Expand Down Expand Up @@ -1501,6 +1502,7 @@ func (s *AppendBlobUnrecordedTestsSuite) TestCreateAppendBlobWithTags() {
createResp, err := abClient.Create(context.Background(), &createAppendBlobOptions)
_require.Nil(err)
_require.NotNil(createResp.VersionID)
time.Sleep(10 * time.Second)

_, err = abClient.GetProperties(context.Background(), nil)
_require.Nil(err)
Expand Down Expand Up @@ -1532,6 +1534,15 @@ func (s *AppendBlobUnrecordedTestsSuite) TestCreateAppendBlobWithTags() {
for _, blobTag := range blobTagsSet {
_require.Equal(testcommon.SpecialCharBlobTagsMap[*blobTag.Key], *blobTag.Value)
}

// Tags with spaces
where := "\"GO \"='.Net'"
lResp, err := svcClient.FilterBlobs(context.Background(), &service.FilterBlobsOptions{
Where: &where,
})
_require.Nil(err)
_require.Len(lResp.FilterBlobSegment.Blobs[0].Tags.BlobTagSet, 1)
_require.Equal(lResp.FilterBlobSegment.Blobs[0].Tags.BlobTagSet[0], blobTagsSet[2])
}

func (s *AppendBlobRecordedTestsSuite) TestAppendBlobGetPropertiesUsingVID() {
Expand Down
131 changes: 59 additions & 72 deletions sdk/storage/azblob/blockblob/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2788,78 +2788,65 @@ func (s *BlockBlobUnrecordedTestsSuite) TestListBlobReturnsTags() {
}
}

// func (s *BlockBlobUnrecordedTestsSuite) TestFindBlobsByTags() {
// _require := require.New(s.T())
// testName := s.T().Name()
// _context := getTestContext(testName)
// ignoreHeaders(_context.recording, []string{"x-ms-tags", "X-Ms-Tags"})
// svcClient, err := testcommon.GetServiceClient(s.T(), testcommon.TestAccountDefault, nil)
// if err != nil {
// s.Fail("Unable to fetch service client because " + err.Error())
// }
//
// containerClient1 := testcommon.CreateNewContainer(context.Background(), _require, testcommon.GenerateContainerName(testName) + "1", svcClient)
// defer testcommon.DeleteContainer(context.Background(), _require, containerClient1)
//
// containerClient2 := testcommon.CreateNewContainer(context.Background(), _require, testcommon.GenerateContainerName(testName) + "2", svcClient)
// defer testcommon.DeleteContainer(context.Background(), _require, containerClient2)
//
// containerClient3 := testcommon.CreateNewContainer(context.Background(), _require, testcommon.GenerateContainerName(testName) + "3", svcClient)
// defer testcommon.DeleteContainer(context.Background(), _require, containerClient3)
//
// blobTagsMap1 := map[string]string{
// "tag2": "tagsecond",
// "tag3": "tagthird",
// }
// blobTagsMap2 := map[string]string{
// "tag1": "firsttag",
// "tag2": "secondtag",
// "tag3": "thirdtag",
// }
//
// blobURL11 := testcommon.GetBlockBlobClient(testcommon.GenerateBlobName(testName) + "11", containerClient1)
// _, err = blobURL11.Upload(context.Background(), bytes.NewReader([]byte("random data")), &blockblob.UploadOptions{
// Metadata: testcommon.BasicMetadata,
// Tags: blobTagsMap1,
// })
// _require.Nil(err)
//
// blobURL12 := testcommon.GetBlockBlobClient(testcommon.GenerateBlobName(testName) + "12", containerClient1)
// _, err = blobURL12.Upload(context.Background(), bytes.NewReader([]byte("another random data")), &blockblob.UploadOptions{
// Metadata: testcommon.BasicMetadata,
// Tags: blobTagsMap2,
// })
// _require.Nil(err)
//
// blobURL21 := testcommon.GetBlockBlobClient(testcommon.GenerateBlobName(testName) + "21", containerClient2)
// _, err = blobURL21.Upload(context.Background(), bytes.NewReader([]byte("random data")), HTTPHeaders{}, testcommon.BasicMetadata, LeaseAccessConditions{}, DefaultAccessTier, nil, ClientProvidedKeyOptions{})
// _require.Nil(err)
//
// blobURL22 := testcommon.GetBlockBlobClient(testcommon.GenerateBlobName(testName) + "22", containerClient2)
// _, err = blobURL22.Upload(context.Background(), bytes.NewReader([]byte("another random data")), HTTPHeaders{}, testcommon.BasicMetadata, LeaseAccessConditions{}, DefaultAccessTier, blobTagsMap2, ClientProvidedKeyOptions{})
// _require.Nil(err)
//
// blobURL31 := testcommon.GetBlockBlobClient(testcommon.GenerateBlobName(testName) + "31", containerClient3)
// _, err = blobURL31.Upload(context.Background(), bytes.NewReader([]byte("random data")), HTTPHeaders{}, testcommon.BasicMetadata, LeaseAccessConditions{}, DefaultAccessTier, nil, ClientProvidedKeyOptions{})
// _require.Nil(err)
//
// where := "\"tag4\"='fourthtag'"
// lResp, err := svcClient.FindBlobByTags(context.Background(), nil, nil, &where, Marker{}, nil)
// _require.Nil(err)
// _assert(lResp.Blobs, chk.HasLen, 0)
//
// //where = "\"tag1\"='firsttag'AND\"tag2\"='secondtag'AND\"@container\"='"+ containerName1 + "'"
// //TODO: Figure out how to do a composite query based on container.
// where = "\"tag1\"='firsttag'AND\"tag2\"='secondtag'"
//
// lResp, err = svcClient.FindBlobsByTags(context.Background(), nil, nil, &where, Marker{}, nil)
// _require.Nil(err)
//
// for _, blob := range lResp.Blobs {
// _assert(blob.TagValue, chk.Equals, "firsttag")
// }
// }
//
func (s *BlockBlobUnrecordedTestsSuite) TestFilterBlobsWithTags() {
_require := require.New(s.T())
testName := s.T().Name()
svcClient, err := testcommon.GetServiceClient(s.T(), testcommon.TestAccountDefault, nil)
_require.NoError(err)

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

blobTagsMap1 := map[string]string{
"tag2": "tagsecond",
"tag3": "tagthird",
}
blobTagsMap2 := map[string]string{
"tag1": "firsttag",
"tag2": "secondtag",
"tag3": "thirdtag",
"tag key": "tag value", // tags with spaces
}

blobName1 := testcommon.GenerateBlobName(testName) + "1"
blobClient1 := testcommon.CreateNewBlockBlob(context.Background(), _require, blobName1, containerClient)
_, err = blobClient1.SetTags(context.Background(), blobTagsMap1, nil)
_require.Nil(err)

blobName2 := testcommon.GenerateBlobName(testName) + "2"
blobClient2 := testcommon.CreateNewBlockBlob(context.Background(), _require, blobName2, containerClient)
_, err = blobClient2.SetTags(context.Background(), blobTagsMap2, nil)
_require.Nil(err)
time.Sleep(10 * time.Second)

blobTagsResp, err := blobClient2.GetTags(context.Background(), nil)
_require.Nil(err)
blobTagsSet := blobTagsResp.BlobTagSet
_require.NotNil(blobTagsSet)

// Test invalid tag
where := "\"tag4\"='fourthtag'"
lResp, err := svcClient.FilterBlobs(context.Background(), &service.FilterBlobsOptions{Where: &where})
_require.Nil(err)
_require.Equal(len(lResp.Blobs), 0)

// Test multiple valid tags
where = "\"tag1\"='firsttag'AND\"tag2\"='secondtag'"
// where := "foo=\"value 1\""
lResp, err = svcClient.FilterBlobs(context.Background(), &service.FilterBlobsOptions{Where: &where})
_require.Nil(err)
_require.Len(lResp.FilterBlobSegment.Blobs[0].Tags.BlobTagSet, 2)
_require.Equal(lResp.FilterBlobSegment.Blobs[0].Tags.BlobTagSet[0], blobTagsSet[1])
_require.Equal(lResp.FilterBlobSegment.Blobs[0].Tags.BlobTagSet[1], blobTagsSet[2])

// Test tags with spaces
where = "\"tag key\"='tag value'"
lResp, err = svcClient.FilterBlobs(context.Background(), &service.FilterBlobsOptions{Where: &where})
_require.Nil(err)
_require.Len(lResp.FilterBlobSegment.Blobs[0].Tags.BlobTagSet, 1)
_require.Equal(lResp.FilterBlobSegment.Blobs[0].Tags.BlobTagSet[0], blobTagsSet[0])

}

// func (s *BlockBlobUnrecordedTestsSuite) TestFilterBlobsUsingAccountSAS() {
// accountName, accountKey := accountInfo()
Expand Down
11 changes: 11 additions & 0 deletions sdk/storage/azblob/internal/generated/autorest.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,14 @@ directive:
from: BlobPropertiesInternal
to: BlobProperties
```

### Updating encoding URL, Golang adds '+' which disrupts encoding with service

``` yaml
directive:
- from: zz_service_client.go
where: $
transform: >-
return $.
replace(/req.Raw\(\).URL.RawQuery \= reqQP.Encode\(\)/, `req.Raw().URL.RawQuery = strings.Replace(reqQP.Encode(), "+", "%20", -1)`)
```
2 changes: 1 addition & 1 deletion sdk/storage/azblob/internal/generated/zz_service_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions sdk/storage/azblob/pageblob/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"context"
"crypto/md5"
"encoding/binary"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/service"
"hash/crc64"
"io"
"math/rand"
Expand Down Expand Up @@ -3917,6 +3918,7 @@ func (s *PageBlobUnrecordedTestsSuite) TestCreatePageBlobWithTags() {

_, err = pbClient.SetTags(context.Background(), testcommon.BasicBlobTagsMap, nil)
_require.Nil(err)
time.Sleep(10 * time.Second)
// _require.Equal(setTagResp.RawResponse.StatusCode, 204)

gpResp, err := pbClient.GetProperties(context.Background(), nil)
Expand Down Expand Up @@ -3957,6 +3959,13 @@ func (s *PageBlobUnrecordedTestsSuite) TestCreatePageBlobWithTags() {
for _, blobTag := range blobTagsSet {
_require.Equal(modifiedBlobTags[*blobTag.Key], *blobTag.Value)
}

// Test FilterBlobs API
where := "\"azure\"='blob'"
lResp, err := svcClient.FilterBlobs(context.Background(), &service.FilterBlobsOptions{Where: &where})
_require.Nil(err)
_require.Equal(*lResp.FilterBlobSegment.Blobs[0].Tags.BlobTagSet[0].Key, "azure")
_require.Equal(*lResp.FilterBlobSegment.Blobs[0].Tags.BlobTagSet[0].Value, "blob")
}

func (s *PageBlobUnrecordedTestsSuite) TestPageBlobSetBlobTagForSnapshot() {
Expand All @@ -3972,6 +3981,7 @@ func (s *PageBlobUnrecordedTestsSuite) TestPageBlobSetBlobTagForSnapshot() {

_, err = pbClient.SetTags(context.Background(), testcommon.SpecialCharBlobTagsMap, nil)
_require.Nil(err)
time.Sleep(10 * time.Second)

resp, err := pbClient.CreateSnapshot(context.Background(), nil)
_require.Nil(err)
Expand All @@ -3990,6 +4000,15 @@ func (s *PageBlobUnrecordedTestsSuite) TestPageBlobSetBlobTagForSnapshot() {
for _, blobTag := range blobTagsSet {
_require.Equal(testcommon.SpecialCharBlobTagsMap[*blobTag.Key], *blobTag.Value)
}

// Tags with spaces
where := "\"GO \"='.Net'"
lResp, err := svcClient.FilterBlobs(context.Background(), &service.FilterBlobsOptions{
Where: &where,
})
_require.Nil(err)
_require.Equal(*lResp.FilterBlobSegment.Blobs[0].Tags.BlobTagSet[0].Key, "GO ")
_require.Equal(*lResp.FilterBlobSegment.Blobs[0].Tags.BlobTagSet[0].Value, ".Net")
}

func (s *PageBlobRecordedTestsSuite) TestCreatePageBlobReturnsVID() {
Expand Down

0 comments on commit 2679c6b

Please sign in to comment.