Skip to content

Commit d844b33

Browse files
author
Hovsep Mkrtchyan
committed
Added Select-AzureRMContext tests.
1 parent c71df24 commit d844b33

File tree

4 files changed

+147
-5
lines changed

4 files changed

+147
-5
lines changed

src/Common/Commands.ResourceManager.Profile.Test/Commands.ResourceManager.Profile.Test.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@
178178
</Reference>
179179
</ItemGroup>
180180
<ItemGroup>
181+
<Compile Include="ContextCmdletTests.Live.cs" />
181182
<Compile Include="LoginCmdletTests.cs" />
182-
<Compile Include="ContextCmdletTests.cs" />
183+
<Compile Include="ContextCmdletTests.Mocked.cs" />
183184
<Compile Include="ProfileCmdletTests.cs" />
184185
<Compile Include="Properties\AssemblyInfo.cs" />
185186
</ItemGroup>
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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.WindowsAzure.Commands.Utilities.Common;
16+
using Microsoft.Azure.Commands.Profile;
17+
using Microsoft.Azure.Commands.ResourceManager.Common;
18+
using Microsoft.Azure.Common.Authentication;
19+
using Microsoft.Azure.Common.Authentication.Models;
20+
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
21+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
22+
using System.Linq;
23+
using Xunit;
24+
using System;
25+
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
26+
using System.Management.Automation;
27+
28+
namespace Microsoft.Azure.Commands.ResourceManager.Profile.Test
29+
{
30+
public class ContextCmdletTestsLive
31+
{
32+
private MemoryDataStore dataStore;
33+
private MockCommandRuntime commandRuntimeMock;
34+
35+
public ContextCmdletTestsLive()
36+
{
37+
dataStore = new MemoryDataStore();
38+
AzureSession.DataStore = dataStore;
39+
commandRuntimeMock = new MockCommandRuntime();
40+
AzureRMCmdlet.DefaultProfile = new AzureRMProfile();
41+
}
42+
43+
[Fact]
44+
[Trait(Category.AcceptanceType, Category.LiveOnly)]
45+
public void SelectAzureContextWithSubscriptionAndTenant()
46+
{
47+
var cmdlt = new SelectAzureRMContextCommand();
48+
// Setup
49+
cmdlt.CommandRuntime = commandRuntimeMock;
50+
cmdlt.SubscriptionId = "db1ab6f0-4769-4b27-930e-01e2ef9c123c";
51+
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
52+
53+
// Act
54+
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
55+
cmdlt.InvokeBeginProcessing();
56+
cmdlt.ExecuteCmdlet();
57+
cmdlt.InvokeEndProcessing();
58+
59+
// Verify
60+
Assert.True(commandRuntimeMock.OutputPipeline.Count == 2);
61+
var context = (AzureContext)commandRuntimeMock.OutputPipeline[1];
62+
Assert.Equal("db1ab6f0-4769-4b27-930e-01e2ef9c123c", context.Subscription.Id.ToString());
63+
}
64+
65+
[Fact]
66+
[Trait(Category.AcceptanceType, Category.LiveOnly)]
67+
public void SelectAzureContextWithSubscriptionAndNoTenant()
68+
{
69+
var cmdlt = new SelectAzureRMContextCommand();
70+
// Setup
71+
cmdlt.CommandRuntime = commandRuntimeMock;
72+
cmdlt.SubscriptionId = "db1ab6f0-4769-4b27-930e-01e2ef9c123c";
73+
74+
// Act
75+
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
76+
cmdlt.InvokeBeginProcessing();
77+
cmdlt.ExecuteCmdlet();
78+
cmdlt.InvokeEndProcessing();
79+
80+
// Verify
81+
Assert.True(commandRuntimeMock.OutputPipeline.Count == 2);
82+
var context = (AzureContext)commandRuntimeMock.OutputPipeline[1];
83+
Assert.Equal("db1ab6f0-4769-4b27-930e-01e2ef9c123c", context.Subscription.Id.ToString());
84+
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.Id.ToString());
85+
}
86+
87+
[Fact]
88+
[Trait(Category.AcceptanceType, Category.LiveOnly)]
89+
public void SelectAzureContextWithNoSubscriptionAndTenant()
90+
{
91+
var cmdlt = new SelectAzureRMContextCommand();
92+
// Setup
93+
cmdlt.CommandRuntime = commandRuntimeMock;
94+
cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47";
95+
96+
// Act
97+
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
98+
cmdlt.InvokeBeginProcessing();
99+
cmdlt.ExecuteCmdlet();
100+
cmdlt.InvokeEndProcessing();
101+
102+
// Verify
103+
Assert.True(commandRuntimeMock.OutputPipeline.Count == 2);
104+
var context = (AzureContext)commandRuntimeMock.OutputPipeline[1];
105+
Assert.Null(context.Subscription);
106+
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.Id.ToString());
107+
}
108+
109+
[Fact]
110+
[Trait(Category.AcceptanceType, Category.LiveOnly)]
111+
public void SelectAzureContextWithNoSubscriptionAndNoTenant()
112+
{
113+
var cmdlt = new SelectAzureRMContextCommand();
114+
// Setup
115+
cmdlt.CommandRuntime = commandRuntimeMock;
116+
117+
// Act
118+
Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null);
119+
cmdlt.InvokeBeginProcessing();
120+
121+
// Verify
122+
Assert.Throws<PSNotSupportedException>(() => cmdlt.ExecuteCmdlet());
123+
cmdlt.InvokeEndProcessing();
124+
}
125+
126+
private void Login(string subscriptionId, string tenantId)
127+
{
128+
var cmdlt = new LoginAzureRMAccountCommand();
129+
// Setup
130+
cmdlt.CommandRuntime = commandRuntimeMock;
131+
cmdlt.SubscriptionId = subscriptionId;
132+
cmdlt.Tenant = tenantId;
133+
134+
// Act
135+
cmdlt.InvokeBeginProcessing();
136+
cmdlt.ExecuteCmdlet();
137+
cmdlt.InvokeEndProcessing();
138+
139+
Assert.NotNull(AzureRMCmdlet.DefaultProfile.Context);
140+
}
141+
}
142+
}

src/Common/Commands.ResourceManager.Profile.Test/ContextCmdletTests.cs renamed to src/Common/Commands.ResourceManager.Profile.Test/ContextCmdletTests.Mocked.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626

2727
namespace Microsoft.Azure.Commands.ResourceManager.Profile.Test
2828
{
29-
public class ContextCmdletTests : RMTestBase
29+
public class ContextCmdletTestsMocked : RMTestBase
3030
{
3131
private MemoryDataStore dataStore;
3232
private MockCommandRuntime commandRuntimeMock;
3333

34-
public ContextCmdletTests()
34+
public ContextCmdletTestsMocked()
3535
{
3636
dataStore = new MemoryDataStore();
3737
AzureSession.DataStore = dataStore;
@@ -41,7 +41,7 @@ public ContextCmdletTests()
4141

4242
[Fact]
4343
[Trait(Category.AcceptanceType, Category.CheckIn)]
44-
public void GetAzureContextInMemory()
44+
public void GetAzureContext()
4545
{
4646
var cmdlt = new GetAzureRMContextCommand();
4747
// Setup

src/Common/Commands.ResourceManager.Profile/Account/LoginAzureRMAccount.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ protected override void ProcessRecord()
8585
else
8686
{
8787
azureAccount.Type = AzureAccount.AccountType.User;
88-
8988
}
9089

9190
SecureString password = null;

0 commit comments

Comments
 (0)