Skip to content
Closed
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
12 changes: 10 additions & 2 deletions common/ManagementTestShared/Redesign/ManagementRecordedTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private ArmClient GetCleanupClient()
{
return new ArmClient(
TestEnvironment.SubscriptionId,
GetUri(TestEnvironment.ResourceManagerUrl),
TestEnvironment.Credential,
new ArmClientOptions());
}
Expand All @@ -80,10 +81,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 @@ -148,7 +155,7 @@ protected void StopSessionRecording()
}

[OneTimeSetUp]
public void OneTimeSetUp()
public virtual void OneTimeSetUp()
{
if (!HasOneTimeSetup())
return;
Expand All @@ -161,6 +168,7 @@ public void OneTimeSetUp()

GlobalClient = CreateClient<ArmClient>(
SessionEnvironment.SubscriptionId,
GetUri(SessionEnvironment.ResourceManagerUrl),
SessionEnvironment.Credential,
options);
}
Expand Down Expand Up @@ -189,7 +197,7 @@ private bool HasOneTimeSetup()
}

[OneTimeTearDown]
public void OneTimeCleanupResourceGroups()
public virtual void OneTimeCleanupResourceGroups()
{
if (Mode != RecordedTestMode.Playback)
{
Expand Down
56 changes: 56 additions & 0 deletions common/ManagementTestShared/Redesign/MockTestBase.cs
Original file line number Diff line number Diff line change
@@ -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<MockTestEnvironment>
{
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<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");
}
}
}
}
}
35 changes: 35 additions & 0 deletions common/ManagementTestShared/Redesign/MockTestEnvironment.cs
Original file line number Diff line number Diff line change
@@ -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
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.

You don't need a test environment for mock tests because they would never run live.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That is true, but I need the Credential to be the mock credential even not in Playback mode

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.

Can you use new MockCredential()?

{
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;
}
}
}
}
2 changes: 1 addition & 1 deletion eng/Packages.Data.props
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@

<!-- Packages intended for Management libraries only -->
<ItemGroup Condition="'$(IsMgmtLibrary)' == 'true'">
<PackageReference Update="Azure.ResourceManager.Core" Version="1.0.0-alpha.20210526.3" />
<PackageReference Update="Azure.ResourceManager.Core" Version="1.0.0-alpha.20210708.1" />
</ItemGroup>

<!-- Packages intended for Extensions libraries only -->
Expand Down
4 changes: 2 additions & 2 deletions sdk/core/Azure.Core.TestFramework/src/TestEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> _environmentFile = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Expand Down 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
2 changes: 2 additions & 0 deletions sdk/core/Azure.Core/tests/ManagementRecordedTestBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace Azure.Core.Tests.Management
[Parallelizable]
internal class ManagementRecordedTestBaseTests : ManagementRecordedTestBase<TestEnvironmentTests.MockTestEnvironment>
{
protected override Type OperationInternalsType => typeof(OperationInternals);

public ManagementRecordedTestBaseTests(bool isAsync)
: base(isAsync)
{
Expand Down