Skip to content

Commit 44515e2

Browse files
authored
Merge pull request Azure#8392 from maddieclayton/testdebtgetsub
Add test coverage for Get-AzureRmSubscription list call
2 parents ddf937f + aa1150e commit 44515e2

File tree

2 files changed

+83
-11
lines changed

2 files changed

+83
-11
lines changed

src/Accounts/Accounts.Test/AzureRMProfileTests.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,79 @@ public void GetAzureRmSubscriptionByNameMultiplePages()
662662
Assert.Equal(tenants[1], resultSubscription.TenantId);
663663
}
664664

665+
[Fact]
666+
[Trait(Category.AcceptanceType, Category.CheckIn)]
667+
public void GetAzureRmSubscriptionManagedService()
668+
{
669+
var tenants = new List<string> { Guid.NewGuid().ToString(), DefaultTenant.ToString() };
670+
var firstTenantSubscriptions = new List<string> { Guid.NewGuid().ToString(),
671+
Guid.NewGuid().ToString(),
672+
Guid.NewGuid().ToString(),
673+
Guid.NewGuid().ToString() };
674+
var secondTenantSubscriptions = new List<string> { Guid.NewGuid().ToString(),
675+
Guid.NewGuid().ToString(),
676+
Guid.NewGuid().ToString(),
677+
Guid.NewGuid().ToString() };
678+
679+
var firstList = new List<string> { firstTenantSubscriptions[0], firstTenantSubscriptions[1] };
680+
var secondList = new List<string> { firstTenantSubscriptions[2], firstTenantSubscriptions[3] };
681+
682+
var thirdList = new List<string> { secondTenantSubscriptions[0], secondTenantSubscriptions[1] };
683+
var fourthList = new List<string> { secondTenantSubscriptions[2], secondTenantSubscriptions[3] };
684+
685+
var client = SetupTestEnvironment(tenants, firstList, secondList, thirdList, fourthList);
686+
687+
// TEST WITH USER TYPE
688+
var dataStore = new MemoryDataStore();
689+
AzureSession.Instance.DataStore = dataStore;
690+
var commandRuntimeMock = new MockCommandRuntime();
691+
AzureSession.Instance.AuthenticationFactory = new MockTokenAuthenticationFactory();
692+
var profile = new AzureRmProfile();
693+
profile.EnvironmentTable.Add("foo", new AzureEnvironment(AzureEnvironment.PublicEnvironments.Values.FirstOrDefault()));
694+
profile.DefaultContext = Context;
695+
profile.DefaultContext.Account = new AzureAccount();
696+
profile.DefaultContext.Tenant.Id = DefaultTenant.ToString();
697+
698+
profile.DefaultContext.Account.Type = "User";
699+
var cmdlt = new GetAzureRMSubscriptionCommand();
700+
// Setup
701+
cmdlt.DefaultProfile = profile;
702+
cmdlt.CommandRuntime = commandRuntimeMock;
703+
Assert.Null(cmdlt.TenantId);
704+
// Act
705+
cmdlt.InvokeBeginProcessing();
706+
cmdlt.ExecuteCmdlet();
707+
cmdlt.InvokeEndProcessing();
708+
Assert.Null(cmdlt.TenantId);
709+
Assert.True(commandRuntimeMock.OutputPipeline.Count == 8);
710+
711+
// TEST WITH MANAGEDSERVICE
712+
client = SetupTestEnvironment(tenants, firstList, secondList, thirdList, fourthList);
713+
714+
dataStore = new MemoryDataStore();
715+
AzureSession.Instance.DataStore = dataStore;
716+
commandRuntimeMock = new MockCommandRuntime();
717+
AzureSession.Instance.AuthenticationFactory = new MockTokenAuthenticationFactory();
718+
profile = new AzureRmProfile();
719+
profile.EnvironmentTable.Add("foo", new AzureEnvironment(AzureEnvironment.PublicEnvironments.Values.FirstOrDefault()));
720+
profile.DefaultContext = Context;
721+
profile.DefaultContext.Account = new AzureAccount();
722+
profile.DefaultContext.Tenant.Id = DefaultTenant.ToString();
723+
724+
profile.DefaultContext.Account.Type = "ManagedService";
725+
cmdlt = new GetAzureRMSubscriptionCommand();
726+
// Setup
727+
cmdlt.DefaultProfile = profile;
728+
cmdlt.CommandRuntime = commandRuntimeMock;
729+
Assert.Null(cmdlt.TenantId);
730+
// Act
731+
cmdlt.InvokeBeginProcessing();
732+
cmdlt.ExecuteCmdlet();
733+
cmdlt.InvokeEndProcessing();
734+
Assert.NotNull(cmdlt.TenantId);
735+
Assert.True(commandRuntimeMock.OutputPipeline.Count == 4);
736+
}
737+
665738
#if NETSTANDARD
666739
[Fact(Skip = "ConcurrentDictionary is not marked as Serializable")]
667740
[Trait(Category.RunType, Category.DesktopOnly)]

src/Accounts/Accounts/Subscription/GetAzureRMSubscription.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,12 @@ protected override void BeginProcessing()
6363

6464
public override void ExecuteCmdlet()
6565
{
66-
var tenant = TenantId;
6766
if (!string.IsNullOrWhiteSpace(this.SubscriptionName))
6867
{
6968
IAzureSubscription result;
7069
try
7170
{
72-
if (!this._client.TryGetSubscriptionByName(tenant, this.SubscriptionName, out result))
71+
if (!this._client.TryGetSubscriptionByName(TenantId, this.SubscriptionName, out result))
7372
{
7473
ThrowSubscriptionNotFoundError(this.TenantId, this.SubscriptionName);
7574
}
@@ -78,7 +77,7 @@ public override void ExecuteCmdlet()
7877
}
7978
catch (AadAuthenticationException exception)
8079
{
81-
ThrowTenantAuthenticationError(tenant, exception);
80+
ThrowTenantAuthenticationError(TenantId, exception);
8281
throw;
8382
}
8483

@@ -88,7 +87,7 @@ public override void ExecuteCmdlet()
8887
IAzureSubscription result;
8988
try
9089
{
91-
if (!this._client.TryGetSubscriptionById(tenant, this.SubscriptionId, out result))
90+
if (!this._client.TryGetSubscriptionById(TenantId, this.SubscriptionId, out result))
9291
{
9392
ThrowSubscriptionNotFoundError(this.TenantId, this.SubscriptionId);
9493
}
@@ -97,7 +96,7 @@ public override void ExecuteCmdlet()
9796
}
9897
catch (AadAuthenticationException exception)
9998
{
100-
ThrowTenantAuthenticationError(tenant, exception);
99+
ThrowTenantAuthenticationError(TenantId, exception);
101100
throw;
102101
}
103102

@@ -108,26 +107,26 @@ public override void ExecuteCmdlet()
108107
{
109108
if (DefaultContext.Account.Type.Equals("ManagedService"))
110109
{
111-
if (tenant == null)
110+
if (TenantId == null)
112111
{
113-
tenant = DefaultContext.Tenant.Id;
112+
TenantId = DefaultContext.Tenant.Id;
114113
}
115114

116-
if (tenant.Equals(DefaultContext.Tenant.Id))
115+
if (TenantId.Equals(DefaultContext.Tenant.Id))
117116
{
118-
var subscriptions = _client.ListSubscriptions(tenant);
117+
var subscriptions = _client.ListSubscriptions(TenantId);
119118
WriteObject(subscriptions.Select((s) => new PSAzureSubscription(s)), enumerateCollection: true);
120119
}
121120
}
122121
else
123122
{
124-
var subscriptions = _client.ListSubscriptions(tenant);
123+
var subscriptions = _client.ListSubscriptions(TenantId);
125124
WriteObject(subscriptions.Select((s) => new PSAzureSubscription(s)), enumerateCollection: true);
126125
}
127126
}
128127
catch (AadAuthenticationException exception)
129128
{
130-
ThrowTenantAuthenticationError(tenant, exception);
129+
ThrowTenantAuthenticationError(TenantId, exception);
131130
throw;
132131
}
133132
}

0 commit comments

Comments
 (0)