diff --git a/common/ManagementTestShared/Redesign/ManagementRecordedTestBase.cs b/common/ManagementTestShared/Redesign/ManagementRecordedTestBase.cs index 985e9297c2a0..36df70070903 100644 --- a/common/ManagementTestShared/Redesign/ManagementRecordedTestBase.cs +++ b/common/ManagementTestShared/Redesign/ManagementRecordedTestBase.cs @@ -64,6 +64,7 @@ private ArmClient GetCleanupClient() { return new ArmClient( TestEnvironment.SubscriptionId, + GetUri(TestEnvironment.ResourceManagerUrl), TestEnvironment.Credential, new ArmClientOptions()); } @@ -80,10 +81,16 @@ protected ArmClient GetArmClient(ArmClientOptions clientOptions = default) return CreateClient( TestEnvironment.SubscriptionId, + GetUri(TestEnvironment.ResourceManagerUrl), TestEnvironment.Credential, options); } + private Uri GetUri(string endpoint) + { + return !string.IsNullOrEmpty(endpoint) ? new Uri(endpoint) : null; + } + [SetUp] protected void CreateCleanupClient() { @@ -148,7 +155,7 @@ protected void StopSessionRecording() } [OneTimeSetUp] - public void OneTimeSetUp() + public virtual void OneTimeSetUp() { if (!HasOneTimeSetup()) return; @@ -161,6 +168,7 @@ public void OneTimeSetUp() GlobalClient = CreateClient( SessionEnvironment.SubscriptionId, + GetUri(SessionEnvironment.ResourceManagerUrl), SessionEnvironment.Credential, options); } @@ -189,7 +197,7 @@ private bool HasOneTimeSetup() } [OneTimeTearDown] - public void OneTimeCleanupResourceGroups() + public virtual void OneTimeCleanupResourceGroups() { if (Mode != RecordedTestMode.Playback) { diff --git a/common/ManagementTestShared/Redesign/MockTestBase.cs b/common/ManagementTestShared/Redesign/MockTestBase.cs new file mode 100644 index 000000000000..cc704a518852 --- /dev/null +++ b/common/ManagementTestShared/Redesign/MockTestBase.cs @@ -0,0 +1,56 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using Azure.Core; +using Azure.Core.TestFramework; +using Azure.ResourceManager.Core; +using System; +using System.Net.Sockets; +using System.Threading.Tasks; + +namespace Azure.ResourceManager.TestFramework +{ + public abstract class MockTestBase : ManagementRecordedTestBase + { + protected override Type OperationInternalsType => typeof(OperationInternals); + + public MockTestBase(bool isAsync) : base(isAsync) + { + EnsureMockServerRunning(); + } + + public MockTestBase(bool isAsync, RecordedTestMode mode) : base(isAsync, mode) + { + EnsureMockServerRunning(); + } + + protected async Task GetResourceGroupContainer(ArmClientOptions clientOptions = default) + { + var client = GetArmClient(clientOptions); + var sub = await client.GetSubscriptions().GetAsync(TestEnvironment.SubscriptionId); + return sub.Value.GetResourceGroups(); + } + + private void EnsureMockServerRunning() + { + if (Mode == RecordedTestMode.Record) + TestMockServerRunning(); + } + + private void TestMockServerRunning() + { + using (var tcpClient = new TcpClient()) + { + try + { + var uri = new Uri(TestEnvironment.MockEndPoint); + tcpClient.Connect(uri.Host, uri.Port); + } + catch (SocketException) + { + throw new InvalidOperationException("The mock server is not running, please start the mock server following this guide `https://devdiv.visualstudio.com/DevDiv/_git/avs?path=%2FREADME.md` in order to record the test"); + } + } + } + } +} diff --git a/common/ManagementTestShared/Redesign/MockTestEnvironment.cs b/common/ManagementTestShared/Redesign/MockTestEnvironment.cs new file mode 100644 index 000000000000..1883bb2f053a --- /dev/null +++ b/common/ManagementTestShared/Redesign/MockTestEnvironment.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using Azure.Core; +using Azure.Core.TestFramework; +using System; + +namespace Azure.ResourceManager.TestFramework +{ + public class MockTestEnvironment : TestEnvironment + { + public MockTestEnvironment() : base() + { + Environment.SetEnvironmentVariable("SUBSCRIPTION_ID", "00000000-0000-0000-0000-000000000000"); + Environment.SetEnvironmentVariable("RESOURCE_MANAGER_URL", MockEndPoint); + } + + public virtual string MockEndPoint => $"https://localhost:8441"; + + public override TokenCredential Credential + { + get + { + if (_credential != null) + { + return _credential; + } + + _credential = new MockCredential(); + + return _credential; + } + } + } +} diff --git a/eng/Packages.Data.props b/eng/Packages.Data.props index 32768c983059..59088c1e33ac 100644 --- a/eng/Packages.Data.props +++ b/eng/Packages.Data.props @@ -116,7 +116,7 @@ - + diff --git a/sdk/core/Azure.Core.TestFramework/src/TestEnvironment.cs b/sdk/core/Azure.Core.TestFramework/src/TestEnvironment.cs index 04b9d11a5d67..7423e49a80f2 100644 --- a/sdk/core/Azure.Core.TestFramework/src/TestEnvironment.cs +++ b/sdk/core/Azure.Core.TestFramework/src/TestEnvironment.cs @@ -28,7 +28,7 @@ public abstract class TestEnvironment private readonly string _prefix; - private TokenCredential _credential; + protected internal TokenCredential _credential; private TestRecording _recording; private readonly Dictionary _environmentFile = new Dictionary(StringComparer.OrdinalIgnoreCase); @@ -150,7 +150,7 @@ static TestEnvironment() /// public string ClientSecret => GetVariable("CLIENT_SECRET"); - public TokenCredential Credential + public virtual TokenCredential Credential { get { diff --git a/sdk/core/Azure.Core/tests/ManagementRecordedTestBaseTests.cs b/sdk/core/Azure.Core/tests/ManagementRecordedTestBaseTests.cs index f69cf17aaea1..f3c55788c3c8 100644 --- a/sdk/core/Azure.Core/tests/ManagementRecordedTestBaseTests.cs +++ b/sdk/core/Azure.Core/tests/ManagementRecordedTestBaseTests.cs @@ -13,6 +13,8 @@ namespace Azure.Core.Tests.Management [Parallelizable] internal class ManagementRecordedTestBaseTests : ManagementRecordedTestBase { + protected override Type OperationInternalsType => typeof(OperationInternals); + public ManagementRecordedTestBaseTests(bool isAsync) : base(isAsync) {