Skip to content

Commit 73e9244

Browse files
committed
Add test for CmdletInfoHandler
1 parent f9bdd0c commit 73e9244

File tree

5 files changed

+174
-6
lines changed

5 files changed

+174
-6
lines changed

src/Common/Commands.Common/CmdletInfoHandler.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,15 @@ namespace Microsoft.WindowsAzure.Commands.Common
2323
/// </summary>
2424
public class CmdletInfoHandler : DelegatingHandler
2525
{
26-
private string _cmdlet;
27-
private string _parameterSet;
26+
/// <summary>
27+
/// The name of the cmdlet.
28+
/// </summary>
29+
public string Cmdlet { get; private set; }
30+
31+
/// <summary>
32+
/// The name of the parameter set specified by user.
33+
/// </summary>
34+
public string ParameterSet { get; private set; }
2835

2936
/// <summary>
3037
/// Initializes an instance of a CmdletInfoHandler with the name of the cmdlet and the parameter set.
@@ -33,14 +40,20 @@ public class CmdletInfoHandler : DelegatingHandler
3340
/// <param name="parameterSet">the name of the parameter set specified by user</param>
3441
public CmdletInfoHandler(string cmdlet, string parameterSet)
3542
{
36-
this._cmdlet = cmdlet;
37-
this._parameterSet = parameterSet;
43+
this.Cmdlet = cmdlet;
44+
this.ParameterSet = parameterSet;
3845
}
3946

4047
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
4148
{
42-
request.Headers.Add("CommandName", _cmdlet);
43-
request.Headers.Add("ParameterSetName", _parameterSet);
49+
if (Cmdlet != null)
50+
{
51+
request.Headers.Add("CommandName", Cmdlet);
52+
}
53+
if (ParameterSet != null)
54+
{
55+
request.Headers.Add("ParameterSetName", ParameterSet);
56+
}
4457
return base.SendAsync(request, cancellationToken);
4558
}
4659
}

src/ServiceManagement/Common/Commands.Common.Test/Commands.Common.Test.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171
<SpecificVersion>False</SpecificVersion>
7272
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Resources.2.18.7-preview\lib\net40\Microsoft.Azure.ResourceManager.dll</HintPath>
7373
</Reference>
74+
<Reference Include="Microsoft.Azure.Test.Framework, Version=1.0.5715.36130, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
75+
<SpecificVersion>False</SpecificVersion>
76+
<HintPath>..\..\..\packages\Microsoft.Azure.Test.Framework.1.0.5715.36130-prerelease\lib\net45\Microsoft.Azure.Test.Framework.dll</HintPath>
77+
</Reference>
7478
<Reference Include="Microsoft.Azure.Test.HttpRecorder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
7579
<HintPath>..\..\..\packages\Microsoft.Azure.Test.HttpRecorder.1.0.5715.36130-prerelease\lib\net45\Microsoft.Azure.Test.HttpRecorder.dll</HintPath>
7680
<Private>True</Private>
@@ -174,6 +178,8 @@
174178
</ItemGroup>
175179
<ItemGroup>
176180
<Compile Include="Common\AuthenticationFactoryTests.cs" />
181+
<Compile Include="Common\GetTestResource.cs" />
182+
<Compile Include="Common\PSCmdletTests.cs" />
177183
<Compile Include="Common\MockSubsciptionFactory.cs" />
178184
<Compile Include="Common\ProfileClientTests.cs" />
179185
<Compile Include="Common\ServicePrincipalStoreTests.cs" />
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Common.Authentication;
16+
using Microsoft.Azure.Common.Authentication.Models;
17+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
18+
using Microsoft.WindowsAzure.Management;
19+
using System.Management.Automation;
20+
using System.Security.Permissions;
21+
22+
namespace Microsoft.WindowsAzure.Commands.Common.Test.Common
23+
{
24+
/// </summary>
25+
[Cmdlet(VerbsCommon.Get, "TestResource", DefaultParameterSetName = ParameterSet1)]
26+
public class GetTestResource : AzureSMCmdlet
27+
{
28+
public ManagementClient client { get; set; }
29+
30+
/// <summary>
31+
/// Default parameter set name
32+
/// </summary>
33+
private const string ParameterSet1 = "ParameterSet1";
34+
35+
/// <summary>
36+
/// Another parameter set name
37+
/// </summary>
38+
private const string ParameterSet2 = "ParameterSet2";
39+
40+
[Alias("N", "Container")]
41+
[Parameter(Position = 0, HelpMessage = "Container Name",
42+
ParameterSetName = ParameterSet1)]
43+
public string Name { get; set; }
44+
45+
[Parameter(HelpMessage = "Container Prefix",
46+
ParameterSetName = ParameterSet2, Mandatory = true)]
47+
[ValidateNotNullOrEmpty]
48+
public string Prefix { get; set; }
49+
50+
/// <summary>
51+
/// Execute command
52+
/// </summary>
53+
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
54+
public override void ExecuteCmdlet()
55+
{
56+
client = AzureSession.ClientFactory.CreateClient<ManagementClient>(base.DefaultContext, AzureEnvironment.Endpoint.ServiceManagement);
57+
WriteObject(client);
58+
}
59+
60+
protected override void BeginProcessing()
61+
{
62+
base.BeginProcessing();
63+
}
64+
65+
protected override void EndProcessing()
66+
{
67+
base.EndProcessing();
68+
}
69+
}
70+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.IO;
17+
using System.Linq;
18+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
19+
using Xunit;
20+
using Microsoft.Azure.Common.Authentication;
21+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
22+
using Microsoft.Azure.Management.Resources;
23+
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
24+
using Microsoft.Azure.Test;
25+
using Microsoft.WindowsAzure.Management;
26+
using System.Collections.Generic;
27+
using System.Collections.ObjectModel;
28+
using System.Management.Automation;
29+
using Microsoft.Azure.Common.Authentication.Factories;
30+
using System.Net.Http;
31+
32+
namespace Microsoft.WindowsAzure.Commands.Common.Test.Common
33+
{
34+
public class PSCmdletTests
35+
{
36+
[Fact]
37+
[Trait(Category.Service, Category.ServiceManagement)]
38+
[Trait(Category.AcceptanceType, Category.CheckIn)]
39+
[Trait(Category.AcceptanceType, Category.BVT)]
40+
public void CmdletNameAndParameterSetInHeader()
41+
{
42+
var client = RunPowerShellTest("Get-TestResource").FirstOrDefault().BaseObject as ManagementClient;
43+
Assert.NotNull(client);
44+
var handler = client.GetHttpPipeline().FirstOrDefault(h => h is CmdletInfoHandler);
45+
Assert.NotNull(handler);
46+
Assert.Equal("Get-TestResource", ((CmdletInfoHandler)handler).Cmdlet);
47+
Assert.Equal("ParameterSet1", ((CmdletInfoHandler)handler).ParameterSet);
48+
}
49+
50+
private EnvironmentSetupHelper helper = new EnvironmentSetupHelper();
51+
52+
protected void SetupManagementClients()
53+
{
54+
var rdfeTestFactory = new RDFETestEnvironmentFactory();
55+
var managementClient = TestBase.GetServiceClient<ManagementClient>(rdfeTestFactory);
56+
57+
AzureSession.ClientFactory = new ClientFactory();
58+
}
59+
60+
protected Collection<PSObject> RunPowerShellTest(params string[] scripts)
61+
{
62+
using (UndoContext context = UndoContext.Current)
63+
{
64+
context.Start(TestUtilities.GetCallingClass(1), TestUtilities.GetCurrentMethodName(2));
65+
66+
SetupManagementClients();
67+
68+
List<string> modules = new List<string>();
69+
modules.Add("Microsoft.WindowsAzure.Commands.Common.Test.dll");
70+
71+
helper.SetupEnvironment(AzureModule.AzureServiceManagement);
72+
helper.SetupModules(modules.ToArray());
73+
74+
return helper.RunPowerShellTest(scripts);
75+
}
76+
}
77+
}
78+
}

src/ServiceManagement/Common/Commands.Common.Test/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
77
<package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net45" />
88
<package id="Microsoft.Azure.Management.Resources" version="2.18.7-preview" targetFramework="net45" />
9+
<package id="Microsoft.Azure.Test.Framework" version="1.0.5715.36130-prerelease" targetFramework="net45" />
910
<package id="Microsoft.Azure.Test.HttpRecorder" version="1.0.5715.36130-prerelease" targetFramework="net45" />
1011
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
1112
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />

0 commit comments

Comments
 (0)