-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Update test project to consume framework for recorded tests #19171
Changes from 2 commits
95a48a2
6cf8c22
f17e9c3
54a864b
8395bcf
5309308
83cc07e
edf8a40
5cc3e44
77c29a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Azure.Core; | ||
using Azure.Core.TestFramework; | ||
using Azure.ResourceManager.Core; | ||
using NUnit.Framework; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Azure.ResourceManager.TestFramework | ||
{ | ||
public abstract class Track2ManagementRecordedTestBase<TEnvironment> : RecordedTestBase<TEnvironment> | ||
where TEnvironment: TestEnvironment, new() | ||
{ | ||
protected ResourceGroupCleanupPolicy CleanupPolicy = new ResourceGroupCleanupPolicy(); | ||
|
||
protected ResourceGroupCleanupPolicy OneTimeCleanupPolicy = new ResourceGroupCleanupPolicy(); | ||
|
||
protected static AzureResourceManagerClient GlobalClient { get; private set; } | ||
|
||
public TestEnvironment SessionEnvironment { get; private set; } | ||
|
||
public TestRecording SessionRecording { get; private set; } | ||
|
||
private AzureResourceManagerClient _cleanupClient; | ||
|
||
protected Track2ManagementRecordedTestBase(bool isAsync) : base(isAsync) | ||
{ | ||
SessionEnvironment = new TEnvironment(); | ||
SessionEnvironment.Mode = Mode; | ||
} | ||
|
||
protected Track2ManagementRecordedTestBase(bool isAsync, RecordedTestMode mode) : base(isAsync, mode) | ||
{ | ||
SessionEnvironment = new TEnvironment(); | ||
SessionEnvironment.Mode = Mode; | ||
} | ||
|
||
private AzureResourceManagerClient GetCleanupClient() | ||
{ | ||
if (Mode != RecordedTestMode.Playback) | ||
{ | ||
return new AzureResourceManagerClient( | ||
SessionEnvironment.SubscriptionId, | ||
SessionEnvironment.Credential, | ||
new AzureResourceManagerClientOptions()); | ||
} | ||
return null; | ||
} | ||
|
||
protected AzureResourceManagerClient GetArmClient() | ||
{ | ||
var options = InstrumentClientOptions(new AzureResourceManagerClientOptions()); | ||
options.AddPolicy(CleanupPolicy, HttpPipelinePosition.PerCall); | ||
|
||
return CreateClient<AzureResourceManagerClient>( | ||
TestEnvironment.SubscriptionId, | ||
TestEnvironment.Credential, | ||
options); | ||
} | ||
|
||
[TearDown] | ||
protected void CleanupResourceGroups() | ||
{ | ||
if (Mode != RecordedTestMode.Playback) | ||
{ | ||
Parallel.ForEach(CleanupPolicy.ResourceGroupsCreated, resourceGroup => | ||
{ | ||
_cleanupClient.GetResourceGroupOperations(TestEnvironment.SubscriptionId, resourceGroup).StartDelete(); | ||
}); | ||
} | ||
} | ||
|
||
private void StartSessionRecording() | ||
{ | ||
// Only create test recordings for the latest version of the service | ||
TestContext.TestAdapter test = TestContext.CurrentContext.Test; | ||
if (Mode != RecordedTestMode.Live && | ||
test.Properties.ContainsKey("SkipRecordings")) | ||
{ | ||
throw new IgnoreException((string)test.Properties.Get("SkipRecordings")); | ||
} | ||
SessionRecording = new TestRecording(Mode, GetSessionFilePath(), Sanitizer, Matcher); | ||
SessionEnvironment.SetRecording(SessionRecording); | ||
ValidateClientInstrumentation = SessionRecording.HasRequests; | ||
} | ||
|
||
private void StopSessionRecording() | ||
{ | ||
if (ValidateClientInstrumentation) | ||
{ | ||
throw new InvalidOperationException("The test didn't instrument any clients but had recordings. Please call InstrumentClient for the client being recorded."); | ||
} | ||
|
||
SessionRecording?.Dispose(true); | ||
} | ||
|
||
[OneTimeSetUp] | ||
public void OneTimeSetUp() | ||
{ | ||
StartSessionRecording(); | ||
|
||
_cleanupClient = GetCleanupClient(); | ||
|
||
var options = InstrumentClientOptions(new AzureResourceManagerClientOptions(), SessionRecording); | ||
options.AddPolicy(OneTimeCleanupPolicy, HttpPipelinePosition.PerCall); | ||
|
||
GlobalClient = CreateClient<AzureResourceManagerClient>( | ||
SessionEnvironment.SubscriptionId, | ||
SessionEnvironment.Credential, | ||
options); | ||
} | ||
|
||
[OneTimeTearDown] | ||
public void OneTimeCleanupResourceGroups() | ||
{ | ||
StopSessionRecording(); | ||
|
||
if (Mode != RecordedTestMode.Playback) | ||
{ | ||
Parallel.ForEach(OneTimeCleanupPolicy.ResourceGroupsCreated, resourceGroup => | ||
{ | ||
_cleanupClient.GetResourceGroupOperations(resourceGroup).StartDelete(); | ||
}); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -264,6 +264,11 @@ protected string GetOptionalVariable(string name) | |
value = Environment.GetEnvironmentVariable(name); | ||
} | ||
|
||
if (value == null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to support the "AZURE_" prefix? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so that it can read the regular defaultcredential env variables locally. They are the exact same variables just with AZURE_ prepended so I added that to the check list |
||
{ | ||
value = Environment.GetEnvironmentVariable($"AZURE_{name}"); | ||
} | ||
|
||
if (value == null) | ||
{ | ||
_environmentFile.TryGetValue(name, out value); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Azure.Core.TestFramework; | ||
using Azure.ResourceManager.TestFramework; | ||
|
||
namespace Azure.ResourceManager.Core.Tests | ||
m-nash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public class ResourceManagerTestBase : Track2ManagementRecordedTestBase<ResourceManagerTestEnvironment> | ||
{ | ||
protected ResourceManagerTestBase(bool isAsync, RecordedTestMode mode) | ||
: base(isAsync, mode) | ||
{ | ||
} | ||
|
||
protected ResourceManagerTestBase(bool isAsync) | ||
: base(isAsync) | ||
{ | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Azure.Core.TestFramework; | ||
|
||
namespace Azure.ResourceManager.Core.Tests | ||
{ | ||
public class ResourceManagerTestEnvironment : TestEnvironment | ||
{ | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Text; | ||
using Azure.Core.TestFramework; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.ResourceManager.Core.Tests | ||
{ | ||
public class SubscriptionOperationsTests : ResourceManagerTestBase | ||
{ | ||
private AzureResourceManagerClient _client; | ||
|
||
public SubscriptionOperationsTests(bool isAsync) | ||
: base(isAsync)//, RecordedTestMode.Record) | ||
{ | ||
} | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
_client = GetArmClient(); | ||
} | ||
|
||
[TestCase(null)] | ||
[TestCase("")] | ||
[SyncOnly] | ||
[RecordedTest] | ||
public void TestGetResourceGroupOpsArgNullException(string resourceGroupName) | ||
{ | ||
var subOps = _client.DefaultSubscription; | ||
Assert.Throws<ArgumentOutOfRangeException>(delegate { subOps.GetResourceGroupOperations(resourceGroupName); }); | ||
} | ||
|
||
[TestCase("te%st")] | ||
[TestCase("test ")] | ||
[TestCase("te$st")] | ||
[TestCase("te#st")] | ||
[TestCase("te#st")] | ||
[SyncOnly] | ||
[RecordedTest] | ||
public void TestGetResourceGroupOpsArgException(string resourceGroupName) | ||
{ | ||
var subOps = _client.DefaultSubscription; | ||
Assert.Throws<ArgumentException>(delegate { subOps.GetResourceGroupOperations(resourceGroupName); }); | ||
} | ||
|
||
[TestCase(91)] | ||
[SyncOnly] | ||
[RecordedTest] | ||
public void TestGetResourceGroupOpsOutOfRangeArgException(int length) | ||
{ | ||
var resourceGroupName = GetLongString(length); | ||
var subOps = _client.DefaultSubscription; | ||
Assert.Throws<ArgumentOutOfRangeException>(delegate { subOps.GetResourceGroupOperations(resourceGroupName); }); | ||
} | ||
|
||
[TestCase("te.st")] | ||
[TestCase("te")] | ||
[TestCase("t")] | ||
[SyncOnly] | ||
[RecordedTest] | ||
public void TestGetResourceGroupOpsValid(string resourceGroupName) | ||
{ | ||
var subOps = _client.DefaultSubscription; | ||
Assert.DoesNotThrow(delegate { subOps.GetResourceGroupOperations(resourceGroupName); }); | ||
} | ||
|
||
[TestCase(89)] | ||
[TestCase(90)] | ||
[SyncOnly] | ||
[RecordedTest] | ||
public void TestGetResourceGroupOpsLong(int length) | ||
{ | ||
var resourceGroupName = GetLongString(length); | ||
var subOps = _client.DefaultSubscription; | ||
Assert.DoesNotThrow(delegate { subOps.GetResourceGroupOperations(resourceGroupName); }); | ||
} | ||
|
||
private string GetLongString(int length) | ||
{ | ||
StringBuilder builder = new StringBuilder(); | ||
for(int i=0; i<length; i++) | ||
{ | ||
builder.Append('a'); | ||
} | ||
return builder.ToString(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can, please, add a comment here about why we need this condition