Skip to content

Adding cmdlets for managing MachineLearning CommitmentPlans ARM resources #2993

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

Merged
merged 10 commits into from
Oct 21, 2016
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 @@ -59,7 +59,7 @@
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Authorization.2.0.0\lib\net40\Microsoft.Azure.Management.Authorization.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="..\..\..\packages\Microsoft.Azure.Management.MachineLearning.0.9.3-preview\lib\net45\Microsoft.Azure.Management.MachineLearning.dll">
<Reference Include="..\..\..\packages\Microsoft.Azure.Management.MachineLearning.0.9.5-preview\lib\net45\Microsoft.Azure.Management.MachineLearning.dll">
<Name>Microsoft.Azure.Management.MachineLearning</Name>
<Private>True</Private>
</Reference>
Expand Down Expand Up @@ -142,24 +142,32 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ScenarioTests\BaseTestController.cs" />
<Compile Include="ScenarioTests\CommitmentPlansTestController.cs" />
<Compile Include="ScenarioTests\CommitmentPlanTests.cs" />
<Compile Include="ScenarioTests\WebServicesTestController.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScenarioTests\WebServiceTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\Common\Commands.ScenarioTests.ResourceManager.Common\AzureRM.Storage.ps1">
<Link>ScenarioTests\AzureRM.Storage.ps1</Link>
</None>
</None>
<None Include="MSSharedLibKey.snk" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
<None Include="ScenarioTests\Common.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="ScenarioTests\CommitmentPlanTests.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="ScenarioTests\WebServiceTests.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.MachineLearning.Test.ScenarioTests.CommitmentPlanTests\TestListCommitmentPlans.json" />
<None Include="SessionRecords\Microsoft.Azure.Commands.MachineLearning.Test.ScenarioTests.CommitmentPlanTests\TestUpdateCommitmentPlan.json" />
<None Include="SessionRecords\Microsoft.Azure.Commands.MachineLearning.Test.ScenarioTests.WebServiceTests\TestCreateGetRemoveWebService.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand All @@ -172,6 +180,9 @@
<None Include="SessionRecords\Microsoft.Azure.Commands.MachineLearning.Test.ScenarioTests.WebServiceTests\TestUpdateWebService.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.MachineLearning.Test.ScenarioTests.CommitmentPlanTests\TestCreateGetRemoveCommitmentPlan.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="TestData\GraphWebServiceDefinition_Dogfood.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -200,7 +211,7 @@
<Project>{e1f5201d-6067-430e-b303-4e367652991b}</Project>
<Name>Commands.Resources</Name>
</ProjectReference>
<ProjectReference Include="..\..\Resources\Commands.ResourceManager\Cmdlets\Commands.Resources.Rest.csproj">
<ProjectReference Include="..\..\Resources\Commands.ResourceManager\Cmdlets\Commands.Resources.Rest.csproj">
<Project>{8058d403-06e3-4bed-8924-d166ce303961}</Project>
<Name>Commands.Resources.Rest</Name>
</ProjectReference>
Expand All @@ -214,4 +225,4 @@
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using LegacyTest = Microsoft.Azure.Test;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
using Microsoft.Azure.Gallery;
using Microsoft.Azure.Management.Authorization;
using Microsoft.Azure.Management.Resources;
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure.ServiceManagemenet.Common.Models;
using Microsoft.Azure.Subscriptions;
using Microsoft.Azure.Test.Authentication;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Microsoft.WindowsAzure.Commands.ScenarioTest;

namespace Microsoft.Azure.Commands.MachineLearning.Test.ScenarioTests
{
public abstract class BaseTestController<T> where T : ServiceClient<T>
{
private readonly EnvironmentSetupHelper helper;
private LegacyTest.CSMTestEnvironmentFactory csmTestFactory;
private T serviceClient;
private ResourceManagementClient resourceManagementClient;
private StorageManagementClient storageManagementClient;

protected BaseTestController()
{
this.helper = new EnvironmentSetupHelper();
}

protected abstract T ConstructServiceClient(MockContext context);

public void RunPsTest(XunitTracingInterceptor logger, params string[] scripts)
{
var callingClassType = TestUtilities.GetCallingClass(2);
var mockName = TestUtilities.GetCurrentMethodName(2);
this.helper.TracingInterceptor = logger;

this.RunPsTestWorkflow(
() => scripts,
// no custom initializer
null,
// no custom cleanup
null,
callingClassType,
mockName);
}

public void RunPsTestWorkflow(
Func<string[]> scriptBuilder,
Action<LegacyTest.CSMTestEnvironmentFactory> initialize,
Action cleanup,
string callingClassType,
string mockName)
{
var providers = new Dictionary<string, string>
{
{ "Microsoft.Resources", null },
{ "Microsoft.Features", null },
{ "Microsoft.Authorization", null }
};
var providersToIgnore = new Dictionary<string, string>
{
{"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"}
};
HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, providers, providersToIgnore);

HttpMockServer.RecordsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords");
using (var context = MockContext.Start(callingClassType, mockName))
{
this.csmTestFactory = new LegacyTest.CSMTestEnvironmentFactory();
if (initialize != null)
{
initialize(this.csmTestFactory);
}

this.SetupManagementClients(context);
helper.SetupEnvironment(AzureModule.AzureResourceManager);

var callingClassName = callingClassType
.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries)
.Last();

helper.SetupModules(AzureModule.AzureResourceManager,
"ScenarioTests\\Common.ps1",
"ScenarioTests\\" + callingClassName + ".ps1",
helper.RMProfileModule,
helper.RMResourceModule,
helper.GetRMModulePath(@"AzureRM.MachineLearning.psd1"),
"AzureRM.Storage.ps1",
"AzureRM.Resources.ps1");

try
{
if (scriptBuilder != null)
{
var psScripts = scriptBuilder();

if (psScripts != null)
{
helper.RunPowerShellTest(psScripts);
}
}
}
finally
{
if (cleanup != null)
{
cleanup();
}
}
}
}

private void SetupManagementClients(MockContext context)
{
this.serviceClient = this.ConstructServiceClient(context);

this.resourceManagementClient = LegacyTest.TestBase.GetServiceClient<ResourceManagementClient>(this.csmTestFactory);
this.storageManagementClient = LegacyTest.TestBase.GetServiceClient<StorageManagementClient>(this.csmTestFactory);
var subscriptionClient = LegacyTest.TestBase.GetServiceClient<SubscriptionClient>(this.csmTestFactory);
var authManagementClient = LegacyTest.TestBase.GetServiceClient<AuthorizationManagementClient>(this.csmTestFactory);
var gallleryClient = LegacyTest.TestBase.GetServiceClient<GalleryClient>(this.csmTestFactory);

var testEnvironment = this.csmTestFactory.GetTestEnvironment();

var credentials = new SubscriptionCredentialsAdapter(
testEnvironment.AuthorizationContext.TokenCredentials[Microsoft.Azure.Test.TokenAudience.Management],
testEnvironment.SubscriptionId);

HttpClientHelperFactory.Instance = new TestHttpClientHelperFactory(credentials);

helper.SetupManagementClients(
this.resourceManagementClient,
subscriptionClient,
this.serviceClient,
authManagementClient,
gallleryClient,
this.storageManagementClient);
}

/// <summary>
/// The test http client helper factory.
/// </summary>
private class TestHttpClientHelperFactory : HttpClientHelperFactory
{
/// <summary>
/// The subscription cloud credentials.
/// </summary>
private readonly SubscriptionCloudCredentials credential;

/// <summary>
/// Initializes a new instance of the <see cref="TestHttpClientHelperFactory"/> class.
/// </summary>
/// <param name="credentials"></param>
public TestHttpClientHelperFactory(SubscriptionCloudCredentials credentials)
{
this.credential = credentials;
}

/// <summary>
/// Creates new instances of the <see cref="HttpClientHelper"/> class.
/// </summary>
/// <param name="credentials">The credentials.</param>
/// <param name="headerValues">The headers.</param>
public override HttpClientHelper CreateHttpClientHelper(
SubscriptionCloudCredentials credentials,
IEnumerable<ProductInfoHeaderValue> headerValues,
Dictionary<string, string> cmdletHeaderValues)
{
return new HttpClientHelperImpl(
credentials: this.credential,
headerValues: headerValues,
cmdletHeaderValues: cmdletHeaderValues);
}

/// <summary>
/// An implementation of the <see cref="HttpClientHelper"/> abstract class.
/// </summary>
private class HttpClientHelperImpl : HttpClientHelper
{
/// <summary>
/// Initializes new instances of the <see cref="HttpClientHelperImpl"/> class.
/// </summary>
/// <param name="credentials">The credentials.</param>
/// <param name="headerValues">The headers.</param>
public HttpClientHelperImpl(
SubscriptionCloudCredentials credentials,
IEnumerable<ProductInfoHeaderValue> headerValues,
Dictionary<string, string> cmdletHeaderValues)
: base(
credentials: credentials,
headerValues: headerValues,
cmdletHeaderValues: cmdletHeaderValues)
{
}

/// <summary>
/// Creates an <see cref="HttpClient"/>
/// </summary>
/// <param name="primaryHandlers">The handlers that will be added to the top of the chain.</param>
public override HttpClient CreateHttpClient(params DelegatingHandler[] primaryHandlers)
{
return base.CreateHttpClient(HttpMockServer.CreateInstance()
.AsArray()
.Concat(primaryHandlers)
.ToArray());
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.ServiceManagemenet.Common.Models;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.Azure.Commands.MachineLearning.Test.ScenarioTests
{
public class CommitmentPlanTests : RMTestBase
{
private readonly XunitTracingInterceptor interceptor;

public CommitmentPlanTests(ITestOutputHelper output)
{
this.interceptor = new XunitTracingInterceptor(output);
XunitTracingInterceptor.AddToContext(this.interceptor);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestCreateGetRemoveCommitmentPlan()
{
CommitmentPlansTestController.NewInstance.RunPsTest(this.interceptor, "Test-CreateGetRemoveMLCommitmentPlan");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestUpdateCommitmentPlan()
{
CommitmentPlansTestController.NewInstance.RunPsTest(this.interceptor, "Test-UpdateMLCommitmentPlan");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestListCommitmentPlans()
{
CommitmentPlansTestController.NewInstance.RunPsTest(this.interceptor, "Test-ListMLCommitmentPlans");
}

}
}
Loading