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] More managed disk coverage #23140

Merged
merged 11 commits into from
Aug 5, 2021
20 changes: 10 additions & 10 deletions sdk/storage/Azure.Storage.Blobs/tests/ManagedDiskFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public ManagedDiskFixture()

private ManagedDiskConfiguration _config;
private ComputeManagementClient _computeClient;
private Snapshot _snapshot1;
private Snapshot _snapshot2;

public Snapshot Snapshot1 { get; private set; }
public Snapshot Snapshot2 { get; private set; }
public Uri Snapshot1SASUri { get; private set; }
public Uri Snapshot2SASUri { get; private set; }

Expand All @@ -48,15 +48,15 @@ public override async Task SetUp()
var disks = await _computeClient.Disks.ListByResourceGroupAsync(_config.ResourceGroupName).ToListAsync();
var disk = disks.Where(d => d.Name.Contains(_config.DiskNamePrefix)).First();

_snapshot1 = await CreateSnapshot(disk, _config.DiskNamePrefix + Guid.NewGuid().ToString().Replace("-", ""));
Snapshot1 = await CreateSnapshot(disk, _config.DiskNamePrefix + Guid.NewGuid().ToString().Replace("-", ""));

// The disk is attached to VM, wait some time to let OS background jobs write something to disk to create delta.
await Task.Delay(TimeSpan.FromSeconds(60));

_snapshot2 = await CreateSnapshot(disk, _config.DiskNamePrefix + Guid.NewGuid().ToString().Replace("-", ""));
Snapshot2 = await CreateSnapshot(disk, _config.DiskNamePrefix + Guid.NewGuid().ToString().Replace("-", ""));

Snapshot1SASUri = await GrantAccess(_snapshot1);
Snapshot2SASUri = await GrantAccess(_snapshot2);
Snapshot1SASUri = await GrantAccess(Snapshot1);
Snapshot2SASUri = await GrantAccess(Snapshot2);
}

Instance = this;
Expand All @@ -66,11 +66,11 @@ public override async Task TearDown()
{
if (Environment.Mode != RecordedTestMode.Playback)
{
await RevokeAccess(_snapshot1);
await RevokeAccess(_snapshot2);
await RevokeAccess(Snapshot1);
await RevokeAccess(Snapshot2);

await DeleteSnapshot(_snapshot1);
await DeleteSnapshot(_snapshot2);
await DeleteSnapshot(Snapshot1);
await DeleteSnapshot(Snapshot2);
}
}

Expand Down
91 changes: 91 additions & 0 deletions sdk/storage/Azure.Storage.Blobs/tests/ManagedDiskTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Azure.Core.TestFramework;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
using Azure.Storage.Blobs.Test;
using Azure.Storage.Test;
using Azure.Storage.Test.Shared;
using NUnit.Framework;

namespace Azure.Storage.Blobs.Tests.ManagedDisk
{
[ServiceVersion(Min = BlobClientOptions.ServiceVersion.V2019_07_07)]
public class ManagedDiskTests : BlobTestBase
{
private Uri snapshot1SASUri;
private Uri snapshot2SASUri;
private long snapshot1Size;

public ManagedDiskTests(bool async, BlobClientOptions.ServiceVersion serviceVersion)
: base(async, serviceVersion, null /* RecordedTestMode.Record /* to re-record */)
Expand All @@ -27,6 +33,7 @@ public void Setup()
{
snapshot1SASUri = new Uri(Recording.GetVariable(nameof(snapshot1SASUri), ManagedDiskFixture.Instance.Snapshot1SASUri?.AbsoluteUri, v => Sanitizer.SanitizeUri(v)));
snapshot2SASUri = new Uri(Recording.GetVariable(nameof(snapshot2SASUri), ManagedDiskFixture.Instance.Snapshot2SASUri?.AbsoluteUri, v => Sanitizer.SanitizeUri(v)));
snapshot1Size = long.Parse(Recording.GetVariable(nameof(snapshot1Size), ManagedDiskFixture.Instance.Snapshot1?.DiskSizeBytes.ToString()));
}

[Test]
Expand Down Expand Up @@ -61,6 +68,90 @@ public async Task CanDiffPagesBetweenSnapshots()
}
}

[Test]
public async Task GetManagedDiskPageRangesDiffAsync_Error()
{
// Arrange
var snapshot1Client = InstrumentClient(new PageBlobClient(snapshot1SASUri, GetOptions()));

// Act
await TestHelper.AssertExpectedExceptionAsync<RequestFailedException>(
snapshot1Client.GetManagedDiskPageRangesDiffAsync(range: new HttpRange(snapshot1Size + Constants.MB, 4 * Constants.KB)),
e =>
{
Assert.AreEqual("InvalidRange", e.ErrorCode);
Assert.AreEqual("The range specified is invalid for the current size of the resource.",
e.Message.Split('\n')[0]);
});
}

[Test]
public async Task GetManagedDiskPageRangesDiffAsync_AccessConditions()
{
var snapshot2Client = InstrumentClient(new PageBlobClient(snapshot2SASUri, GetOptions()));

foreach (var parameters in Reduced_AccessConditions_Data)
{
parameters.Match = await SetupBlobMatchCondition(snapshot2Client, parameters.Match);

PageBlobRequestConditions accessConditions = PageBlobClientTests.BuildAccessConditions(
parameters: parameters,
lease: false);

// Act
Response<PageRangesInfo> response = await snapshot2Client.GetManagedDiskPageRangesDiffAsync(
range: new HttpRange(0, Constants.KB),
previousSnapshotUri: snapshot1SASUri,
conditions: accessConditions);

// Assert
Assert.IsNotNull(response.Value.PageRanges);
}
}

[Test]
public async Task GetManagedDiskPageRangesDiffAsync_AccessConditionsFail()
{
var snapshot2Client = InstrumentClient(new PageBlobClient(snapshot2SASUri, GetOptions()));
foreach (var parameters in Reduced_AccessConditions_Fail_Data)
{
parameters.NoneMatch = await SetupBlobMatchCondition(snapshot2Client, parameters.NoneMatch);

PageBlobRequestConditions accessConditions = PageBlobClientTests.BuildAccessConditions(
parameters: parameters,
lease: false);

// Act
await TestHelper.CatchAsync<Exception>(
async () =>
{
var _ = (await snapshot2Client.GetManagedDiskPageRangesDiffAsync(
range: new HttpRange(0, Constants.KB),
previousSnapshotUri: snapshot1SASUri,
conditions: accessConditions)).Value;
});
}
}

public IEnumerable<PageBlobClientTests.AccessConditionParameters> Reduced_AccessConditions_Data
=> new[]
{
new PageBlobClientTests.AccessConditionParameters(),
new PageBlobClientTests.AccessConditionParameters { IfModifiedSince = OldDate },
new PageBlobClientTests.AccessConditionParameters { IfUnmodifiedSince = NewDate },
new PageBlobClientTests.AccessConditionParameters { Match = ReceivedETag },
new PageBlobClientTests.AccessConditionParameters { NoneMatch = GarbageETag }
};

public IEnumerable<PageBlobClientTests.AccessConditionParameters> Reduced_AccessConditions_Fail_Data
=> new[]
{
new PageBlobClientTests.AccessConditionParameters { IfModifiedSince = NewDate },
new PageBlobClientTests.AccessConditionParameters { IfUnmodifiedSince = OldDate },
new PageBlobClientTests.AccessConditionParameters { Match = GarbageETag },
new PageBlobClientTests.AccessConditionParameters { NoneMatch = ReceivedETag }
};

private async Task<byte[]> DownloadRange(PageBlobClient client, HttpRange range)
{
var memoryStream = new MemoryStream();
Expand Down
Loading