From 2679c6b2d1ba59f74d63d5fff020485335b83c0d Mon Sep 17 00:00:00 2001 From: siminsavani-msft <77068571+siminsavani-msft@users.noreply.github.com> Date: Thu, 5 Jan 2023 19:44:21 -0500 Subject: [PATCH] Adding tests for FilterBlobs and fixing encoding issue (#19742) * 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 --- sdk/storage/azblob/CHANGELOG.md | 1 + sdk/storage/azblob/appendblob/client_test.go | 11 ++ sdk/storage/azblob/blockblob/client_test.go | 131 ++++++++---------- .../azblob/internal/generated/autorest.md | 11 ++ .../internal/generated/zz_service_client.go | 2 +- sdk/storage/azblob/pageblob/client_test.go | 19 +++ 6 files changed, 102 insertions(+), 73 deletions(-) diff --git a/sdk/storage/azblob/CHANGELOG.md b/sdk/storage/azblob/CHANGELOG.md index e55907fdd8e1..850c4b9d5153 100644 --- a/sdk/storage/azblob/CHANGELOG.md +++ b/sdk/storage/azblob/CHANGELOG.md @@ -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 diff --git a/sdk/storage/azblob/appendblob/client_test.go b/sdk/storage/azblob/appendblob/client_test.go index 449dd2e86195..2a5c53fa8056 100644 --- a/sdk/storage/azblob/appendblob/client_test.go +++ b/sdk/storage/azblob/appendblob/client_test.go @@ -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" @@ -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) @@ -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() { diff --git a/sdk/storage/azblob/blockblob/client_test.go b/sdk/storage/azblob/blockblob/client_test.go index 069c508a207d..5adffc332679 100644 --- a/sdk/storage/azblob/blockblob/client_test.go +++ b/sdk/storage/azblob/blockblob/client_test.go @@ -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() diff --git a/sdk/storage/azblob/internal/generated/autorest.md b/sdk/storage/azblob/internal/generated/autorest.md index 5a3520160a30..c5e55bf104a2 100644 --- a/sdk/storage/azblob/internal/generated/autorest.md +++ b/sdk/storage/azblob/internal/generated/autorest.md @@ -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)`) +``` \ No newline at end of file diff --git a/sdk/storage/azblob/internal/generated/zz_service_client.go b/sdk/storage/azblob/internal/generated/zz_service_client.go index 1cb779d84812..16c7ac92a880 100644 --- a/sdk/storage/azblob/internal/generated/zz_service_client.go +++ b/sdk/storage/azblob/internal/generated/zz_service_client.go @@ -80,7 +80,7 @@ func (client *ServiceClient) filterBlobsCreateRequest(ctx context.Context, optio if options != nil && options.Maxresults != nil { reqQP.Set("maxresults", strconv.FormatInt(int64(*options.Maxresults), 10)) } - req.Raw().URL.RawQuery = reqQP.Encode() + req.Raw().URL.RawQuery = strings.Replace(reqQP.Encode(), "+", "%20", -1) req.Raw().Header["x-ms-version"] = []string{"2020-10-02"} if options != nil && options.RequestID != nil { req.Raw().Header["x-ms-client-request-id"] = []string{*options.RequestID} diff --git a/sdk/storage/azblob/pageblob/client_test.go b/sdk/storage/azblob/pageblob/client_test.go index b7d29413651b..da895cda756f 100644 --- a/sdk/storage/azblob/pageblob/client_test.go +++ b/sdk/storage/azblob/pageblob/client_test.go @@ -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" @@ -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) @@ -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() { @@ -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) @@ -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() {