Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ private ArmClient GetCleanupClient()
{
return new ArmClient(
TestEnvironment.SubscriptionId,
GetUri(TestEnvironment.ResourceManagerUrl),
TestEnvironment.Credential,
new ArmClientOptions());
}
Expand All @@ -78,10 +79,16 @@ protected ArmClient GetArmClient(ArmClientOptions clientOptions = default)

return CreateClient<ArmClient>(
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()
{
Expand Down Expand Up @@ -159,6 +166,7 @@ public void OneTimeSetUp()

GlobalClient = CreateClient<ArmClient>(
SessionEnvironment.SubscriptionId,
GetUri(SessionEnvironment.ResourceManagerUrl),
SessionEnvironment.Credential,
options);
}
Expand Down
54 changes: 54 additions & 0 deletions common/ManagementTestShared/Redesign/MockTestBase.cs
Original file line number Diff line number Diff line change
@@ -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<MockTestEnvironment>
{
public MockTestBase(bool isAsync) : base(isAsync)
{
EnsureMockServerRunning();
}

public MockTestBase(bool isAsync, RecordedTestMode mode) : base(isAsync, mode)
{
EnsureMockServerRunning();
}

protected async Task<ResourceGroupContainer> 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");
}
}
}
}
}
24 changes: 24 additions & 0 deletions common/ManagementTestShared/Redesign/MockTestEnvironment.cs
Original file line number Diff line number Diff line change
@@ -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");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't do this. This might break other tests that run in the same process after.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Override the property instead.

Environment.SetEnvironmentVariable("RESOURCE_MANAGER_URL", MockEndPoint);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

}

public virtual string MockEndPoint => $"https://localhost:8441";

private TokenCredential _mockCredential;

public override TokenCredential Credential => _mockCredential ??= new MockCredential();
}
}
2 changes: 1 addition & 1 deletion sdk/core/Azure.Core.TestFramework/src/TestEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ static TestEnvironment()
/// </summary>
public string ClientSecret => GetVariable("CLIENT_SECRET");

public TokenCredential Credential
public virtual TokenCredential Credential
{
get
{
Expand Down