Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Storage] Make Specialized Blob Clients retrieval mockable. #15105

Merged
merged 32 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
78abf66
Prepare Storage for release
kasobol-msft May 6, 2020
e7907a4
pr feedback.
kasobol-msft May 6, 2020
58814fc
PR feedback.
kasobol-msft May 6, 2020
1286434
pr feedback.
kasobol-msft May 6, 2020
324afd0
merge upstream/master
kasobol-msft May 15, 2020
b216c89
Merge remote-tracking branch 'upstream/master'
kasobol-msft May 19, 2020
1c9e87d
Merge remote-tracking branch 'upstream/master'
kasobol-msft Jun 2, 2020
712ecda
Merge remote-tracking branch 'upstream/master'
kasobol-msft Jun 4, 2020
ad612b5
merge upstream/master
kasobol-msft Jun 10, 2020
5849bef
master merge
kasobol-msft Jul 7, 2020
1bcba8b
Merge remote-tracking branch 'upstream/master'
kasobol-msft Jul 29, 2020
8a2e048
Merge remote-tracking branch 'upstream/master'
kasobol-msft Aug 3, 2020
1bf033e
Merge remote-tracking branch 'upstream/master'
kasobol-msft Aug 8, 2020
b35274e
Merge remote-tracking branch 'upstream/master'
kasobol-msft Aug 10, 2020
feb0649
Merge remote-tracking branch 'upstream/master'
kasobol-msft Aug 11, 2020
5974722
Merge remote-tracking branch 'upstream/master'
kasobol-msft Aug 11, 2020
9252944
Merge remote-tracking branch 'upstream/master'
kasobol-msft Aug 14, 2020
f90886e
Merge remote-tracking branch 'upstream/master'
kasobol-msft Aug 20, 2020
3b9b259
Merge remote-tracking branch 'upstream/master'
kasobol-msft Aug 21, 2020
f97e9bd
Merge remote-tracking branch 'upstream/master'
kasobol-msft Sep 1, 2020
ae9d658
make open write work with using.
kasobol-msft Sep 1, 2020
4a24b96
move recordings to right place
kasobol-msft Sep 1, 2020
e970ed7
pr feedback.
kasobol-msft Sep 1, 2020
ed559ba
Merge remote-tracking branch 'upstream/master'
kasobol-msft Sep 1, 2020
f477f9a
Merge remote-tracking branch 'upstream/master'
kasobol-msft Sep 3, 2020
70bac18
Merge remote-tracking branch 'upstream/master'
kasobol-msft Sep 3, 2020
342d763
Merge remote-tracking branch 'upstream/master'
kasobol-msft Sep 8, 2020
4c8ee0b
Merge remote-tracking branch 'upstream/master'
kasobol-msft Sep 9, 2020
7856b23
make specialized clients retrieval mockable.
kasobol-msft Sep 11, 2020
fa28938
pr feedback.
kasobol-msft Sep 11, 2020
d628cec
export api.
kasobol-msft Sep 11, 2020
984ae2b
record mode...
kasobol-msft Sep 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,15 @@ public BlobContainerClient(System.Uri blobContainerUri, Azure.Storage.StorageSha
public virtual System.Threading.Tasks.Task<Azure.Response<bool>> ExistsAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response<Azure.Storage.Blobs.Models.BlobContainerAccessPolicy> GetAccessPolicy(Azure.Storage.Blobs.Models.BlobRequestConditions conditions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Storage.Blobs.Models.BlobContainerAccessPolicy>> GetAccessPolicyAsync(Azure.Storage.Blobs.Models.BlobRequestConditions conditions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
protected internal virtual Azure.Storage.Blobs.Specialized.AppendBlobClient GetAppendBlobClientCore(string blobName) { throw null; }
public virtual Azure.Storage.Blobs.BlobClient GetBlobClient(string blobName) { throw null; }
protected internal virtual Azure.Storage.Blobs.Specialized.BlobLeaseClient GetBlobLeaseClientCore(string leaseId) { throw null; }
public virtual Azure.Pageable<Azure.Storage.Blobs.Models.BlobItem> GetBlobs(Azure.Storage.Blobs.Models.BlobTraits traits = Azure.Storage.Blobs.Models.BlobTraits.None, Azure.Storage.Blobs.Models.BlobStates states = Azure.Storage.Blobs.Models.BlobStates.None, string prefix = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.AsyncPageable<Azure.Storage.Blobs.Models.BlobItem> GetBlobsAsync(Azure.Storage.Blobs.Models.BlobTraits traits = Azure.Storage.Blobs.Models.BlobTraits.None, Azure.Storage.Blobs.Models.BlobStates states = Azure.Storage.Blobs.Models.BlobStates.None, string prefix = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Pageable<Azure.Storage.Blobs.Models.BlobHierarchyItem> GetBlobsByHierarchy(Azure.Storage.Blobs.Models.BlobTraits traits = Azure.Storage.Blobs.Models.BlobTraits.None, Azure.Storage.Blobs.Models.BlobStates states = Azure.Storage.Blobs.Models.BlobStates.None, string delimiter = null, string prefix = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.AsyncPageable<Azure.Storage.Blobs.Models.BlobHierarchyItem> GetBlobsByHierarchyAsync(Azure.Storage.Blobs.Models.BlobTraits traits = Azure.Storage.Blobs.Models.BlobTraits.None, Azure.Storage.Blobs.Models.BlobStates states = Azure.Storage.Blobs.Models.BlobStates.None, string delimiter = null, string prefix = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
protected internal virtual Azure.Storage.Blobs.Specialized.BlockBlobClient GetBlockBlobClientCore(string blobName) { throw null; }
protected internal virtual Azure.Storage.Blobs.Specialized.PageBlobClient GetPageBlobClientCore(string blobName) { throw null; }
public virtual Azure.Response<Azure.Storage.Blobs.Models.BlobContainerProperties> GetProperties(Azure.Storage.Blobs.Models.BlobRequestConditions conditions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Storage.Blobs.Models.BlobContainerProperties>> GetPropertiesAsync(Azure.Storage.Blobs.Models.BlobRequestConditions conditions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response<Azure.Storage.Blobs.Models.BlobContainerInfo> SetAccessPolicy(Azure.Storage.Blobs.Models.PublicAccessType accessType = Azure.Storage.Blobs.Models.PublicAccessType.None, System.Collections.Generic.IEnumerable<Azure.Storage.Blobs.Models.BlobSignedIdentifier> permissions = null, Azure.Storage.Blobs.Models.BlobRequestConditions conditions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
Expand Down Expand Up @@ -1222,6 +1226,7 @@ public BlobBaseClient(System.Uri blobUri, Azure.Storage.StorageSharedKeyCredenti
public virtual System.Threading.Tasks.Task<Azure.Response> DownloadToAsync(string path, System.Threading.CancellationToken cancellationToken) { throw null; }
public virtual Azure.Response<bool> Exists(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<bool>> ExistsAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
protected internal virtual Azure.Storage.Blobs.Specialized.BlobLeaseClient GetBlobLeaseClientCore(string leaseId) { throw null; }
public virtual Azure.Response<Azure.Storage.Blobs.Models.BlobProperties> GetProperties(Azure.Storage.Blobs.Models.BlobRequestConditions conditions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Storage.Blobs.Models.BlobProperties>> GetPropertiesAsync(Azure.Storage.Blobs.Models.BlobRequestConditions conditions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response<Azure.Storage.Blobs.Models.GetBlobTagResult> GetTags(Azure.Storage.Blobs.Models.BlobRequestConditions conditions = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
Expand Down
18 changes: 1 addition & 17 deletions sdk/storage/Azure.Storage.Blobs/src/AppendBlobClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1589,23 +1589,7 @@ public static AppendBlobClient GetAppendBlobClient(
this BlobContainerClient client,
string blobName)
{
if (client.ClientSideEncryption != default)
{
throw Errors.ClientSideEncryption.TypeNotSupported(typeof(AppendBlobClient));
}

BlobUriBuilder blobUriBuilder = new BlobUriBuilder(client.Uri)
{
BlobName = blobName
};

return new AppendBlobClient(
blobUriBuilder.ToUri(),
client.Pipeline,
client.Version,
client.ClientDiagnostics,
client.CustomerProvidedKey,
client.EncryptionScope);
return client.GetAppendBlobClientCore(blobName);
}
}
}
10 changes: 10 additions & 0 deletions sdk/storage/Azure.Storage.Blobs/src/BlobBaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,16 @@ private protected virtual BlobBaseClient WithVersionCore(string versionId)
EncryptionScope);
}

/// <summary>
/// Initializes a new instance of the <see cref="BlobLeaseClient"/> class.
/// </summary>
/// <param name="leaseId">
/// An optional lease ID. If no lease ID is provided, a random lease
/// ID will be created.
/// </param>
protected internal virtual BlobLeaseClient GetBlobLeaseClientCore(string leaseId) =>
new BlobLeaseClient(this, leaseId);

/// <summary>
/// Sets the various name fields if they are currently null.
/// </summary>
Expand Down
102 changes: 102 additions & 0 deletions sdk/storage/Azure.Storage.Blobs/src/BlobContainerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,108 @@ public virtual BlobClient GetBlobClient(string blobName)
EncryptionScope);
}

/// <summary>
/// Create a new <see cref="BlockBlobClient"/> object by
/// concatenating <paramref name="blobName"/> to
/// the end of the <see cref="Uri"/>. The new
/// <see cref="BlockBlobClient"/>
/// uses the same request policy pipeline as the
/// <see cref="BlobContainerClient"/>.
/// </summary>
/// <param name="blobName">The name of the block blob.</param>
/// <returns>A new <see cref="BlockBlobClient"/> instance.</returns>
protected internal virtual BlockBlobClient GetBlockBlobClientCore(string blobName)
{
if (ClientSideEncryption != default)
{
throw Errors.ClientSideEncryption.TypeNotSupported(typeof(BlockBlobClient));
}

BlobUriBuilder blobUriBuilder = new BlobUriBuilder(Uri)
{
BlobName = blobName
};

return new BlockBlobClient(
blobUriBuilder.ToUri(),
Pipeline,
Version,
ClientDiagnostics,
CustomerProvidedKey,
EncryptionScope);
}

/// <summary>
/// Create a new <see cref="AppendBlobClient"/> object by
/// concatenating <paramref name="blobName"/> to
/// the end of the <see cref="BlobContainerClient.Uri"/>. The new
/// <see cref="AppendBlobClient"/>
/// uses the same request policy pipeline as the
/// <see cref="BlobContainerClient"/>.
/// </summary>
/// <param name="blobName">The name of the append blob.</param>
/// <returns>A new <see cref="AppendBlobClient"/> instance.</returns>
protected internal virtual AppendBlobClient GetAppendBlobClientCore(string blobName)
{
if (ClientSideEncryption != default)
{
throw Errors.ClientSideEncryption.TypeNotSupported(typeof(AppendBlobClient));
}

BlobUriBuilder blobUriBuilder = new BlobUriBuilder(Uri)
{
BlobName = blobName
};

return new AppendBlobClient(
blobUriBuilder.ToUri(),
Pipeline,
Version,
ClientDiagnostics,
CustomerProvidedKey,
EncryptionScope);
}

/// <summary>
/// Create a new <see cref="PageBlobClient"/> object by
/// concatenating <paramref name="blobName"/> to
/// the end of the <see cref="BlobContainerClient.Uri"/>. The new
/// <see cref="PageBlobClient"/>
/// uses the same request policy pipeline as the
/// <see cref="BlobContainerClient"/>.
/// </summary>
/// <param name="blobName">The name of the page blob.</param>
/// <returns>A new <see cref="PageBlobClient"/> instance.</returns>
protected internal virtual PageBlobClient GetPageBlobClientCore(string blobName)
{
if (ClientSideEncryption != default)
{
throw Errors.ClientSideEncryption.TypeNotSupported(typeof(PageBlobClient));
}

BlobUriBuilder blobUriBuilder = new BlobUriBuilder(Uri)
{
BlobName = blobName
};

return new PageBlobClient(
blobUriBuilder.ToUri(),
Pipeline,
Version,
ClientDiagnostics,
CustomerProvidedKey,
EncryptionScope);
}

/// <summary>
/// Initializes a new instance of the <see cref="BlobLeaseClient"/> class.
/// </summary>
/// <param name="leaseId">
/// An optional lease ID. If no lease ID is provided, a random lease
/// ID will be created.
/// </param>
protected internal virtual BlobLeaseClient GetBlobLeaseClientCore(string leaseId) =>
new BlobLeaseClient(this, leaseId);

/// <summary>
/// Sets the various name fields if they are currently null.
Expand Down
4 changes: 2 additions & 2 deletions sdk/storage/Azure.Storage.Blobs/src/BlobLeaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ public static partial class SpecializedBlobExtensions
public static BlobLeaseClient GetBlobLeaseClient(
this BlobBaseClient client,
string leaseId = null) =>
new BlobLeaseClient(client, leaseId);
client.GetBlobLeaseClientCore(leaseId);

/// <summary>
/// Initializes a new instance of the <see cref="BlobLeaseClient"/> class.
Expand All @@ -1201,6 +1201,6 @@ public static BlobLeaseClient GetBlobLeaseClient(
public static BlobLeaseClient GetBlobLeaseClient(
this BlobContainerClient client,
string leaseId = null) =>
new BlobLeaseClient(client, leaseId);
client.GetBlobLeaseClientCore(leaseId);
}
}
18 changes: 1 addition & 17 deletions sdk/storage/Azure.Storage.Blobs/src/BlockBlobClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2232,23 +2232,7 @@ public static BlockBlobClient GetBlockBlobClient(
this BlobContainerClient client,
string blobName)
{
if (client.ClientSideEncryption != default)
{
throw Errors.ClientSideEncryption.TypeNotSupported(typeof(BlockBlobClient));
}

BlobUriBuilder blobUriBuilder = new BlobUriBuilder(client.Uri)
{
BlobName = blobName
};

return new BlockBlobClient(
blobUriBuilder.ToUri(),
client.Pipeline,
client.Version,
client.ClientDiagnostics,
client.CustomerProvidedKey,
client.EncryptionScope);
return client.GetBlockBlobClientCore(blobName);
}
}
}
18 changes: 1 addition & 17 deletions sdk/storage/Azure.Storage.Blobs/src/PageBlobClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3114,23 +3114,7 @@ public static PageBlobClient GetPageBlobClient(
this BlobContainerClient client,
string blobName)
{
if (client.ClientSideEncryption != default)
{
throw Errors.ClientSideEncryption.TypeNotSupported(typeof(PageBlobClient));
}

BlobUriBuilder blobUriBuilder = new BlobUriBuilder(client.Uri)
{
BlobName = blobName
};

return new PageBlobClient(
blobUriBuilder.ToUri(),
client.Pipeline,
client.Version,
client.ClientDiagnostics,
client.CustomerProvidedKey,
client.EncryptionScope);
return client.GetPageBlobClientCore(blobName);
}
}
}
19 changes: 19 additions & 0 deletions sdk/storage/Azure.Storage.Blobs/tests/BlobBaseClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
using Azure.Storage.Sas;
using Azure.Storage.Test;
using Azure.Storage.Test.Shared;
using Moq;
using Moq.Protected;
using NUnit.Framework;
using TestConstants = Azure.Storage.Test.TestConstants;

Expand Down Expand Up @@ -5556,6 +5558,23 @@ public void WithVersion()
Assert.AreEqual(versionUri, blobUriBuilder.ToUri());
}

[Test]
public void CanMockBlobLeaseClientRetrieval()
{
// Arrange
string leaseId = "leaseId";
Mock<BlobBaseClient> blobBaseClientMock = new Mock<BlobBaseClient>();
Mock<BlobLeaseClient> blobLeaseClientMock = new Mock<BlobLeaseClient>();
blobBaseClientMock.Protected().Setup<BlobLeaseClient>("GetBlobLeaseClientCore", leaseId).Returns(blobLeaseClientMock.Object);

// Act
var blobLeaseClient = blobBaseClientMock.Object.GetBlobLeaseClient(leaseId);

// Assert
Assert.IsNotNull(blobLeaseClient);
Assert.AreSame(blobLeaseClientMock.Object, blobLeaseClient);
}

private async Task<BlobBaseClient> GetNewBlobClient(BlobContainerClient container, string blobName = default)
{
blobName ??= GetNewBlobName();
Expand Down
36 changes: 35 additions & 1 deletion sdk/storage/Azure.Storage.Blobs/tests/ContainerClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -17,6 +16,8 @@
using Azure.Storage.Sas;
using Azure.Storage.Test;
using Azure.Storage.Test.Shared;
using Moq;
using Moq.Protected;
using NUnit.Framework;

namespace Azure.Storage.Blobs.Test
Expand Down Expand Up @@ -2512,6 +2513,39 @@ public async Task GetBlobClients_SpecialCharacters(string blobName)
Assert.AreEqual(blobName, pageBlobClientFromConnectionString.Name);
}

[Test]
public void CanMockBlobClientsRetrieval()
{
// Arrange
string blobName = "test";
string leaseId = "leaseId";
Mock<BlobContainerClient> containerClientMock = new Mock<BlobContainerClient>();
Mock<BlockBlobClient> blockBlobClientMock = new Mock<BlockBlobClient>();
Mock<AppendBlobClient> appendBlobClientMock = new Mock<AppendBlobClient>();
Mock<PageBlobClient> pageBlobClientMock = new Mock<PageBlobClient>();
Mock<BlobLeaseClient> blobLeaseClientMock = new Mock<BlobLeaseClient>();
containerClientMock.Protected().Setup<BlockBlobClient>("GetBlockBlobClientCore", blobName).Returns(blockBlobClientMock.Object);
containerClientMock.Protected().Setup<AppendBlobClient>("GetAppendBlobClientCore", blobName).Returns(appendBlobClientMock.Object);
containerClientMock.Protected().Setup<PageBlobClient>("GetPageBlobClientCore", blobName).Returns(pageBlobClientMock.Object);
containerClientMock.Protected().Setup<BlobLeaseClient>("GetBlobLeaseClientCore", leaseId).Returns(blobLeaseClientMock.Object);

// Act
var blockBlobClient = containerClientMock.Object.GetBlockBlobClient(blobName);
var appendBlobClient = containerClientMock.Object.GetAppendBlobClient(blobName);
var pageBlobClient = containerClientMock.Object.GetPageBlobClient(blobName);
var blobLeaseClient = containerClientMock.Object.GetBlobLeaseClient(leaseId);

// Assert
Assert.IsNotNull(blockBlobClient);
Assert.AreSame(blockBlobClientMock.Object, blockBlobClient);
Assert.IsNotNull(appendBlobClient);
Assert.AreSame(appendBlobClientMock.Object, appendBlobClient);
Assert.IsNotNull(pageBlobClient);
Assert.AreSame(pageBlobClientMock.Object, pageBlobClient);
Assert.IsNotNull(blobLeaseClient);
Assert.AreSame(blobLeaseClientMock.Object, blobLeaseClient);
}

#region Secondary Storage
[Test]
public async Task ListContainersSegmentAsync_SecondaryStorageFirstRetrySuccessful()
Expand Down

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

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

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

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