-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update test project to consume framework for recorded tests (#19171)
* Update test project to consume framework for recorded tests * Changes required to get instrumentation working for the new armClient added support for session recording for onetimesetup and onetimeteardown * move unit test files * update tests to not use username for prefix * update targets to separate existing track 2 libraries from new core until autorest changes are finished * merge in armbuildertest and update to use new framework * remove necessity on session files if no onetimesetup * Address review comments
- Loading branch information
Showing
107 changed files
with
6,008 additions
and
161 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
common/ManagementTestShared/Redesign/ManagementRecordedTestBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
// 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.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Azure.ResourceManager.TestFramework | ||
{ | ||
public abstract class ManagementRecordedTestBase<TEnvironment> : RecordedTestBase<TEnvironment> | ||
where TEnvironment: TestEnvironment, new() | ||
{ | ||
protected ResourceGroupCleanupPolicy CleanupPolicy = new ResourceGroupCleanupPolicy(); | ||
|
||
protected ResourceGroupCleanupPolicy OneTimeCleanupPolicy = new ResourceGroupCleanupPolicy(); | ||
|
||
protected AzureResourceManagerClient GlobalClient { get; private set; } | ||
|
||
public TestEnvironment SessionEnvironment { get; private set; } | ||
|
||
public TestRecording SessionRecording { get; private set; } | ||
|
||
private AzureResourceManagerClient _cleanupClient; | ||
|
||
protected ManagementRecordedTestBase(bool isAsync) : base(isAsync) | ||
{ | ||
SessionEnvironment = new TEnvironment(); | ||
SessionEnvironment.Mode = Mode; | ||
} | ||
|
||
protected ManagementRecordedTestBase(bool isAsync, RecordedTestMode mode) : base(isAsync, mode) | ||
{ | ||
SessionEnvironment = new TEnvironment(); | ||
SessionEnvironment.Mode = Mode; | ||
} | ||
|
||
private AzureResourceManagerClient GetCleanupClient() | ||
{ | ||
if (Mode != RecordedTestMode.Playback) | ||
{ | ||
return new AzureResourceManagerClient( | ||
TestEnvironment.SubscriptionId, | ||
TestEnvironment.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); | ||
} | ||
|
||
[SetUp] | ||
protected void Setup() | ||
{ | ||
_cleanupClient ??= GetCleanupClient(); | ||
} | ||
|
||
[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; | ||
} | ||
|
||
protected 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); | ||
GlobalClient = null; | ||
} | ||
|
||
[OneTimeSetUp] | ||
public void OneTimeSetUp() | ||
{ | ||
if (!HasOneTimeSetup()) | ||
return; | ||
|
||
StartSessionRecording(); | ||
|
||
var options = InstrumentClientOptions(new AzureResourceManagerClientOptions(), SessionRecording); | ||
options.AddPolicy(OneTimeCleanupPolicy, HttpPipelinePosition.PerCall); | ||
|
||
GlobalClient = CreateClient<AzureResourceManagerClient>( | ||
SessionEnvironment.SubscriptionId, | ||
SessionEnvironment.Credential, | ||
options); | ||
} | ||
|
||
private bool HasOneTimeSetup() | ||
{ | ||
HashSet<Type> types = new HashSet<Type>(); | ||
Type type = GetType(); | ||
Type endType = typeof(ManagementRecordedTestBase<TEnvironment>); | ||
while (type != endType) | ||
{ | ||
types.Add(type); | ||
type = type.BaseType; | ||
} | ||
|
||
var methods = GetType().GetMethods().Where(m => types.Contains(m.DeclaringType)); | ||
foreach (var method in methods) | ||
{ | ||
foreach(var attr in method.GetCustomAttributes(false)) | ||
{ | ||
if (attr is OneTimeSetUpAttribute) | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
[OneTimeTearDown] | ||
public void OneTimeCleanupResourceGroups() | ||
{ | ||
if (Mode != RecordedTestMode.Playback) | ||
{ | ||
Parallel.ForEach(OneTimeCleanupPolicy.ResourceGroupsCreated, resourceGroup => | ||
{ | ||
_cleanupClient.GetResourceGroupOperations(SessionEnvironment.SubscriptionId, resourceGroup).StartDelete(); | ||
}); | ||
} | ||
|
||
if (!(GlobalClient is null)) | ||
throw new InvalidOperationException("StopSessionRecording was never called please make sure you call that at the end of your OneTimeSetup"); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
common/ManagementTestShared/Redesign/ResourceGroupCleanupPolicy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Generic; | ||
using System.Text.RegularExpressions; | ||
|
||
using Azure.Core; | ||
using Azure.Core.Pipeline; | ||
|
||
namespace Azure.ResourceManager.TestFramework | ||
{ | ||
public class ResourceGroupCleanupPolicy : HttpPipelineSynchronousPolicy | ||
{ | ||
private readonly object _listLock = new object(); | ||
private Regex _resourceGroupPattern = new Regex(@"/subscriptions/[^/]+/resourcegroups/([^?/]+)\?api-version"); | ||
private readonly IList<string> _resourceGroupCreated = new List<string>(); | ||
|
||
public IList<string> ResourceGroupsCreated | ||
{ | ||
get { return _resourceGroupCreated; } | ||
} | ||
|
||
public override void OnSendingRequest(HttpMessage message) | ||
{ | ||
if (message.Request.Method == RequestMethod.Put) | ||
{ | ||
var match = _resourceGroupPattern.Match(message.Request.Uri.ToString()); | ||
if (match.Success) | ||
{ | ||
lock (_listLock) | ||
{ | ||
_resourceGroupCreated.Add(match.Groups[1].Value); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 0 additions & 43 deletions
43
sdk/resourcemanager/Azure.ResourceManager.Core/tests/ArmBuilderTests.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.