diff --git a/common/ManagementTestShared/Redesign/ManagementRecordedTestBase.cs b/common/ManagementTestShared/Redesign/ManagementRecordedTestBase.cs index c36b0dad6470..49c4eb021ea8 100644 --- a/common/ManagementTestShared/Redesign/ManagementRecordedTestBase.cs +++ b/common/ManagementTestShared/Redesign/ManagementRecordedTestBase.cs @@ -62,6 +62,7 @@ private ArmClient GetCleanupClient() { return new ArmClient( TestEnvironment.SubscriptionId, + GetUri(TestEnvironment.ResourceManagerUrl), TestEnvironment.Credential, new ArmClientOptions()); } @@ -78,10 +79,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() { @@ -159,6 +166,7 @@ public void OneTimeSetUp() GlobalClient = CreateClient( SessionEnvironment.SubscriptionId, + GetUri(SessionEnvironment.ResourceManagerUrl), SessionEnvironment.Credential, options); } diff --git a/common/ManagementTestShared/Redesign/MockTestBase.cs b/common/ManagementTestShared/Redesign/MockTestBase.cs new file mode 100644 index 000000000000..a57e18a36896 --- /dev/null +++ b/common/ManagementTestShared/Redesign/MockTestBase.cs @@ -0,0 +1,54 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using Azure.Core.TestFramework; +using Azure.ResourceManager.Core; +using System; +using System.Net; +using System.Net.Sockets; +using System.Threading.Tasks; + +namespace Azure.ResourceManager.TestFramework +{ + public abstract class MockTestBase : ManagementRecordedTestBase + { + 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..c20ae78aba03 --- /dev/null +++ b/common/ManagementTestShared/Redesign/MockTestEnvironment.cs @@ -0,0 +1,24 @@ +// 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"; + + private TokenCredential _mockCredential; + + public override TokenCredential Credential => _mockCredential ??= new MockCredential(); + } +} diff --git a/sdk/core/Azure.Core.TestFramework/src/TestEnvironment.cs b/sdk/core/Azure.Core.TestFramework/src/TestEnvironment.cs index 04b9d11a5d67..c5dd57b43fdb 100644 --- a/sdk/core/Azure.Core.TestFramework/src/TestEnvironment.cs +++ b/sdk/core/Azure.Core.TestFramework/src/TestEnvironment.cs @@ -150,7 +150,7 @@ static TestEnvironment() /// public string ClientSecret => GetVariable("CLIENT_SECRET"); - public TokenCredential Credential + public virtual TokenCredential Credential { get {