Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nibhati - 5191 - add ClientContext.cs #19602

Merged
merged 42 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
9142607
WIP
nisha-bhatia Mar 15, 2021
441c399
WIP
nisha-bhatia Mar 17, 2021
9147c1e
Update ResourceOperationsBase.cs
nisha-bhatia Mar 17, 2021
1ff9f8c
Merge branch 'mgmt-track2' of https://github.com/AME-Redmond/azure-sd…
nisha-bhatia Mar 18, 2021
14ae581
update for tenant extensions
nisha-bhatia Mar 18, 2021
ebf1176
update csproj
nisha-bhatia Mar 18, 2021
ae5a455
Update Azure.ResourceManager.Network.csproj
nisha-bhatia Mar 19, 2021
954c0b3
update csproj
nisha-bhatia Mar 19, 2021
a9af1d4
update csproj
nisha-bhatia Mar 19, 2021
1bf3b5b
WIP
nisha-bhatia Mar 22, 2021
0f952dc
WIP
nisha-bhatia Mar 22, 2021
2e6bf22
update csproj
nisha-bhatia Mar 23, 2021
bccdf27
update csproj
nisha-bhatia Mar 24, 2021
4fe6c13
update csproj
nisha-bhatia Mar 24, 2021
05c4e73
add tests
nisha-bhatia Mar 25, 2021
ee494c6
update test
nisha-bhatia Mar 25, 2021
c82f5b5
update
nisha-bhatia Mar 25, 2021
e39abf3
fix test cases to use the diagnostics case fix a missing mocking cons…
Mar 25, 2021
9710dd8
WIP
nisha-bhatia Mar 26, 2021
c8b9a43
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
nisha-bhatia Mar 29, 2021
49f1ee7
update framework for tests
nisha-bhatia Mar 29, 2021
30ae183
Update Azure.Test.Perf.csproj
nisha-bhatia Mar 29, 2021
0fe1ef3
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
nisha-bhatia Mar 29, 2021
8bbe309
merge
nisha-bhatia Mar 29, 2021
44feea8
revert list json files
nisha-bhatia Mar 30, 2021
bd58a81
revert
nisha-bhatia Mar 30, 2021
ed92f8d
revert merge
nisha-bhatia Mar 30, 2021
e95570e
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
nisha-bhatia Mar 30, 2021
9ec2abd
WIP
nisha-bhatia Mar 30, 2021
0f3b47a
merge with upstream
nisha-bhatia Mar 30, 2021
daa3681
fix ci
nisha-bhatia Mar 30, 2021
d80c1dc
Update Program.cs
nisha-bhatia Mar 30, 2021
d7e8be6
update proto
nisha-bhatia Mar 30, 2021
bef867e
fix test
nisha-bhatia Mar 30, 2021
cb57efe
Update ClientContextTests.cs
nisha-bhatia Mar 30, 2021
99846dc
merge with upstream feature/mgmt-track2
nisha-bhatia Mar 31, 2021
9db7649
update proto
nisha-bhatia Mar 31, 2021
2fbf256
Update Azure.ResourceManager.Billing.csproj
nisha-bhatia Mar 31, 2021
6444198
update version
nisha-bhatia Mar 31, 2021
3fb4117
update ci
nisha-bhatia Mar 31, 2021
6df9f7f
Update Azure.ResourceManager.EventHubs.csproj
nisha-bhatia Apr 1, 2021
a912bc9
add packable
nisha-bhatia Apr 1, 2021
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
42 changes: 42 additions & 0 deletions common/ManagementCoreShared/ClientContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Azure.Core;

namespace Azure.ResourceManager.Core
{
/// <summary>
/// helper class
/// </summary>
internal class ClientContext
{
/// <summary>
/// client options
/// </summary>
public ArmClientOptions ClientOptions { get; set; }

/// <summary>
/// credential
/// </summary>
public TokenCredential Credential { get; set; }

/// <summary>
/// baseuri
/// </summary>
public Uri BaseUri { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="ClientContext"/> class.
/// </summary>
/// <param name="clientOptions"></param>
/// <param name="credential"></param>
/// <param name="uri"></param>
internal ClientContext(ArmClientOptions clientOptions, TokenCredential credential, Uri uri)
{
ClientOptions = clientOptions;
Credential = credential;
BaseUri = uri;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected void StopSessionRecording()
{
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;
}
Expand Down
14 changes: 13 additions & 1 deletion eng/Directory.Build.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,23 @@
<!-- Add ProjectReferences -->
<ProjectReference Include="@(ProjectsToConvert -> '%(ProjectPath)')" />
</ItemGroup>

<!--TODO: update build targets - ADO 5668-->
<PropertyGroup>
<MgmtCoreShared>$(MSBuildThisFileDirectory)/../common/ManagementCoreShared</MgmtCoreShared>
<ExcludeMgmtCoreShared Condition="'$(ExcludeMgmtCoreShared)' == ''">false</ExcludeMgmtCoreShared>
</PropertyGroup>

<ItemGroup Condition="'$(IsMgmtLibrary)' == 'true' and '$(IsTestProject)' != 'true' and '$(ExcludeMgmtCoreShared)' == 'false'">
<Compile Include="$(MgmtCoreShared)/**/*.cs"
Link="Shared/Management/%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>

<!--TODO: end-->
<!-- *********** Management Client Library Override section ************* -->
<ItemGroup Condition="'$(IsMgmtLibrary)' == 'true' and '$(IsTestProject)' != 'true'">
<PackageReference Include="System.Text.Json" />

<!-- TODO: Review these file references-->
<Compile Include="$(AzureCoreSharedSources)ArrayBufferWriter.cs" Link="Shared\%(RecursiveDir)\%(Filename)%(Extension)" />
<Compile Include="$(AzureCoreSharedSources)ClientDiagnostics.cs" Link="Shared\%(RecursiveDir)\%(Filename)%(Extension)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</Description>
<PackageTags>azure;management;appconfiguration</PackageTags>
<NoWarn>$(NoWarn);AZC0001;AZC0008;</NoWarn>
<ExcludeMgmtCoreShared>true</ExcludeMgmtCoreShared>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
AZC0001 - Azure.ResourceManager.Communication namespace needs to be registered in the analyzer
-->
<NoWarn>$(NoWarn);AZC0001</NoWarn>
<ExcludeMgmtCoreShared>true</ExcludeMgmtCoreShared>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>1.0.0-preview.3</Version>
<PackageId>Azure.ResourceManager.Compute</PackageId>
Expand All @@ -7,6 +7,7 @@
This is a beta preview vesion. This version uses a next-generation code generator that introduces important breaking changes, but also new features (such as intuitive authentication, custom HTTP pipeline, distributed tracing and much more).
</Description>
<PackageTags>azure;management;compute</PackageTags>
<ExcludeMgmtCoreShared>true</ExcludeMgmtCoreShared>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ internal class DiagnosticScopeValidatingAsyncEnumerable<T> : AsyncPageable<T>
private readonly bool _strict;
private readonly bool _overridesGetAsyncEnumerator;

//for mocking
protected DiagnosticScopeValidatingAsyncEnumerable()
{
}

public DiagnosticScopeValidatingAsyncEnumerable(AsyncPageable<T> pageable, string expectedName, string methodName, bool strict)
{
if (pageable == null) throw new ArgumentNullException(nameof(pageable), "Operations returning [Async]Pageable should never return null.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void Intercept(IInvocation invocation)

if (
// Generated ARM clients will have a property containing the sub-client that ends with Operations.
(invocation.Method.Name.StartsWith("get_") && type.Name.EndsWith("Operations")) ||
(invocation.Method.Name.StartsWith("get_") && (type.Name.EndsWith("Operations") || type.BaseType.Name.EndsWith("Operations"))) ||
// Instrument the container construction methods inside Operations objects
(invocation.Method.Name.StartsWith("Get") && type.Name.EndsWith("Container")) ||
// Instrument the operations construction methods inside Operations objects
Expand Down
13 changes: 12 additions & 1 deletion sdk/core/Azure.Core.TestFramework/src/ManagementInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public void Intercept(IInvocation invocation)
}

var type = result.GetType();

if (type.Name.StartsWith("Task"))
{
var taskResultType = type.GetGenericArguments()[0];
Expand All @@ -43,6 +42,18 @@ public void Intercept(IInvocation invocation)
invocation.ReturnValue = genericMethod.Invoke(null, new object[] { instrumentedResult });
}
}
else if (type.Name.StartsWith("ValueTask"))
{
var taskResultType = type.GetGenericArguments()[0];
if (taskResultType.Name.StartsWith("Response") || taskResultType.Name.StartsWith("ArmResponse"))
{
var taskResult = result.GetType().GetProperty("Result").GetValue(result);
var instrumentedResult = _testBase.InstrumentClient(taskResultType, taskResult, new IInterceptor[] { new ManagementInterceptor(_testBase) });
var genericValueTask = typeof(ValueTask<>).MakeGenericType(taskResultType);
var vtCtor = genericValueTask.GetConstructor(new Type[] { taskResultType });
invocation.ReturnValue = vtCtor.Invoke(new object[] { instrumentedResult });
}
}
else if (invocation.Method.Name.EndsWith("Value") && type.BaseType.Name.EndsWith("Operations"))
{
invocation.ReturnValue = _testBase.InstrumentClient(type, result, new IInterceptor[] { new ManagementInterceptor(_testBase) });
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Runtime.CompilerServices;
using Castle.Core.Internal;

[assembly: InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)]
26 changes: 20 additions & 6 deletions sdk/core/Azure.Core/tests/ManagementRecordedTestBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@

using System.Threading.Tasks;
using Azure.Core.TestFramework;
using Azure.ResourceManager.Core;
using Azure.ResourceManager.Resources.Models;
using Azure.ResourceManager.TestFramework;
using NUnit.Framework;

namespace Azure.Core.Tests.Management
{
[Parallelizable]
internal class ManagementRecordedTestBaseTests : ManagementRecordedTestBase<TestEnvironmentTests.MockTestEnvironment>
{
public ManagementRecordedTestBaseTests(bool isAsync)
: base(isAsync)
{
TestDiagnostics = false;
TestDiagnostics = true;
}

[Test]
Expand All @@ -35,7 +38,7 @@ public void ValidateInstrumentDefaultSubscription()
var sub = client.DefaultSubscription;
var result = sub.Method();

Assert.AreEqual("TestResourceOperationsProxy", sub.GetType().Name);
Assert.AreEqual("TestResourceProxy", sub.GetType().Name);
Assert.AreEqual("success", result);
}

Expand All @@ -47,7 +50,7 @@ public async Task ValidateInstrumentArmOperation()
var operation = (await sub.GetArmOperationAsync()).Value;
var result = operation.Method();

Assert.AreEqual("TestResourceOperationsProxy", operation.GetType().Name);
Assert.AreEqual("TestResourceProxy", operation.GetType().Name);
Assert.AreEqual("success", result);
}

Expand All @@ -59,7 +62,7 @@ public async Task ValidateInstrumentArmResponse()
var response = (await sub.GetArmOperationAsync()).Value;
var result = response.Method();

Assert.AreEqual("TestResourceOperationsProxy", response.GetType().Name);
Assert.AreEqual("TestResourceProxy", response.GetType().Name);
Assert.AreEqual("success", result);
}

Expand All @@ -71,7 +74,7 @@ public async Task ValidateInstrumentPhArmOperation()
var operation = (await sub.GetPhArmOperationAsync()).Value;
var result = operation.Method();

Assert.AreEqual("TestResourceOperationsProxy", operation.GetType().Name);
Assert.AreEqual("TestResourceProxy", operation.GetType().Name);
Assert.AreEqual("success", result);
}

Expand All @@ -83,7 +86,7 @@ public async Task ValidateInstrumentPhArmResponse()
var response = (await sub.GetPhArmOperationAsync()).Value;
var result = response.Method();

Assert.AreEqual("TestResourceOperationsProxy", response.GetType().Name);
Assert.AreEqual("TestResourceProxy", response.GetType().Name);
Assert.AreEqual("success", result);
}

Expand Down Expand Up @@ -120,5 +123,16 @@ public async Task ValidateInstrumentPhPageable()
Assert.AreEqual("success", item.Method());
}
}

[Test]
public async Task ValidateWaitForCompletion()
{
ManagementTestClient client = InstrumentClient(new ManagementTestClient());
TestResourceOperations rgOp = client.GetTestResourceOperations();
var testResourceOp = await rgOp.GetArmOperationAsync();
TestResource testResource = await testResourceOp.WaitForCompletionAsync();
Assert.AreEqual("TestResourceProxy", testResource.GetType().Name);
Assert.AreEqual("success", testResource.Method());
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 41 additions & 3 deletions sdk/core/Azure.Core/tests/TestClients/ArmOperationTest.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,57 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Threading;
using System.Threading.Tasks;
using Azure.ResourceManager.Core;

namespace Azure.Core.Tests
{
public class ArmOperationTest<T>
public class ArmOperationTest<T> : ArmOperation<T>
where T : class
{
private T _value;
protected ArmOperationTest()
{
}

public ArmOperationTest(T value)
{
Value = value;
_value = value;
}

public override string Id => "testId";

public override T Value => _value;

public override bool HasCompleted => true;

public override bool HasValue => true;

public override Response GetRawResponse()
{
return Response.FromValue(_value, null) as Response;
}

public virtual T Value { get; private set; }
public override ValueTask<Response<T>> WaitForCompletionAsync(CancellationToken cancellationToken = default)
{
return new ValueTask<Response<T>>(Response.FromValue(_value, null));
}

public override ValueTask<Response<T>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancellationToken)
{
return new ValueTask<Response<T>>(Response.FromValue(_value, null));
}

public override ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default)
{
return new ValueTask<Response>(Response.FromValue(_value, null) as Response);
}

public override Response UpdateStatus(CancellationToken cancellationToken = default)
{
return Response.FromValue(_value, null) as Response;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public virtual TestResourceContainer GetTestResourceContainer()
return new TestResourceContainer();
}

public virtual TestResourceOperations DefaultSubscription => new TestResourceOperations();
public virtual TestResource DefaultSubscription => new TestResource();

public virtual string Method()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Azure.Core.Tests
{
public class PhArmOperationTest<T> : ArmOperationTest<T>
where T : class
{
protected PhArmOperationTest()
{
Expand Down
Loading