Skip to content

Commit 9a88d6e

Browse files
author
Hovsep Mkrtchyan
committed
Fixing Common.Profile tests
1 parent 822cb3d commit 9a88d6e

File tree

3 files changed

+20
-59
lines changed

3 files changed

+20
-59
lines changed

src/Common/Commands.Common.Test/Common/ProfileCmdltsTests.cs

Lines changed: 16 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@ public void SetAzureSubscriptionAddsSubscriptionWithCertificate()
193193
[Fact]
194194
public void SetAzureSubscriptionDerivesEnvironmentFromEnvironmentParameterOnAdd()
195195
{
196-
SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
197196
// Setup
198197
ProfileClient client = new ProfileClient();
199198
client.AddOrSetEnvironment(azureEnvironment);
200199
client.Profile.Save();
200+
SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
201201

202202
cmdlt.CommandRuntime = commandRuntimeMock;
203203
cmdlt.SubscriptionId = Guid.NewGuid().ToString();
@@ -242,13 +242,13 @@ public void SetAzureSubscriptionThrowsExceptionWithoutCertificateOnAdd()
242242
[Fact]
243243
public void SetAzureSubscriptionDerivesEnvironmentFromEnvironmentParameterOnSet()
244244
{
245-
SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
246245
// Setup
247246
ProfileClient client = new ProfileClient();
248247
client.AddOrSetAccount(azureAccount);
249248
client.AddOrSetEnvironment(azureEnvironment);
250249
client.AddOrSetSubscription(azureSubscription1);
251250
client.Profile.Save();
251+
SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
252252

253253
cmdlt.CommandRuntime = commandRuntimeMock;
254254
cmdlt.SubscriptionId = azureSubscription1.Id.ToString();
@@ -270,13 +270,13 @@ public void SetAzureSubscriptionDerivesEnvironmentFromEnvironmentParameterOnSet(
270270
[Fact]
271271
public void SetAzureSubscriptionDerivesEnvironmentFromServiceEndpointParameterOnSet()
272272
{
273-
SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
274273
// Setup
275274
ProfileClient client = new ProfileClient();
276275
client.AddOrSetAccount(azureAccount);
277276
client.AddOrSetEnvironment(azureEnvironment);
278277
client.AddOrSetSubscription(azureSubscription1);
279278
client.Profile.Save();
279+
SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
280280

281281
cmdlt.CommandRuntime = commandRuntimeMock;
282282
cmdlt.SubscriptionId = azureSubscription1.Id.ToString();
@@ -299,13 +299,13 @@ public void SetAzureSubscriptionDerivesEnvironmentFromServiceEndpointParameterOn
299299
[Fact]
300300
public void SetAzureSubscriptionDerivesEnvironmentFromResourcesEndpointParameterOnSet()
301301
{
302-
SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
303302
// Setup
304303
ProfileClient client = new ProfileClient();
305304
client.AddOrSetAccount(azureAccount);
306305
client.AddOrSetEnvironment(azureEnvironment);
307306
client.AddOrSetSubscription(azureSubscription1);
308307
client.Profile.Save();
308+
SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
309309

310310
cmdlt.CommandRuntime = commandRuntimeMock;
311311
cmdlt.SubscriptionId = azureSubscription1.Id.ToString();
@@ -327,14 +327,15 @@ public void SetAzureSubscriptionDerivesEnvironmentFromResourcesEndpointParameter
327327
[Fact]
328328
public void SetAzureSubscriptionDerivesEnvironmentFromBothEndpointParameters()
329329
{
330-
SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
331330
// Setup
332331
ProfileClient client = new ProfileClient();
333332
client.AddOrSetAccount(azureAccount);
334333
client.AddOrSetEnvironment(azureEnvironment);
335334
client.AddOrSetSubscription(azureSubscription1);
336335
client.Profile.Save();
337336

337+
SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
338+
338339
cmdlt.CommandRuntime = commandRuntimeMock;
339340
cmdlt.SubscriptionId = azureSubscription1.Id.ToString();
340341
cmdlt.CurrentStorageAccountName = "NewCloudStorage";
@@ -356,15 +357,15 @@ public void SetAzureSubscriptionDerivesEnvironmentFromBothEndpointParameters()
356357
[Fact]
357358
public void SetAzureSubscriptionUpdatesSubscriptionWithCertificate()
358359
{
359-
SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
360-
361360
// Setup
362361
ProfileClient client = new ProfileClient();
363362
client.AddOrSetAccount(azureAccount);
364363
client.AddOrSetEnvironment(azureEnvironment);
365364
client.AddOrSetSubscription(azureSubscription1);
366365
client.Profile.Save();
367366

367+
SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
368+
368369
cmdlt.CommandRuntime = commandRuntimeMock;
369370
cmdlt.SubscriptionId = azureSubscription1.Id.ToString();
370371
cmdlt.CurrentStorageAccountName = "NewCloudStorage";
@@ -475,8 +476,8 @@ public void ImportPublishSettingsFileOverwritesEnvironment()
475476
[Fact]
476477
public void SelectDefaultAzureSubscriptionByNameUpdatesProfile()
477478
{
478-
SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
479479
var client = SetupDefaultProfile();
480+
SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
480481

481482
// Setup
482483
cmdlt.CommandRuntime = commandRuntimeMock;
@@ -499,14 +500,14 @@ public void SelectDefaultAzureSubscriptionByNameUpdatesProfile()
499500
[Fact]
500501
public void SelectAzureSubscriptionByNameUpdatesProfile()
501502
{
502-
SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
503503
SetupDefaultProfile();
504+
SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
504505

505506
// Setup
506507
cmdlt.CommandRuntime = commandRuntimeMock;
507508
cmdlt.SetParameterSet("SelectSubscriptionByNameParameterSet");
508509
cmdlt.SubscriptionName = azureSubscription2.Name;
509-
Assert.Null(AzureSession.Profile.CurrentContext.Subscription);
510+
Assert.NotEqual(azureSubscription2.Id, AzureSession.Profile.CurrentContext.Subscription.Id);
510511

511512
// Act
512513
cmdlt.InvokeBeginProcessing();
@@ -521,14 +522,13 @@ public void SelectAzureSubscriptionByNameUpdatesProfile()
521522
[Fact]
522523
public void SelectAzureSubscriptionWithoutPassthroughDoesNotPrint()
523524
{
524-
SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
525525
SetupDefaultProfile();
526+
SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
526527

527528
// Setup
528529
cmdlt.CommandRuntime = commandRuntimeMock;
529530
cmdlt.SetParameterSet("SelectSubscriptionByNameParameterSet");
530531
cmdlt.SubscriptionName = azureSubscription2.Name;
531-
Assert.Null(AzureSession.Profile.CurrentContext.Subscription);
532532

533533
// Act
534534
cmdlt.InvokeBeginProcessing();
@@ -542,15 +542,14 @@ public void SelectAzureSubscriptionWithoutPassthroughDoesNotPrint()
542542
[Fact]
543543
public void SelectAzureSubscriptionWithPassthroughPrintsSubscription()
544544
{
545-
SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
546545
SetupDefaultProfile();
546+
SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
547547

548548
// Setup
549549
cmdlt.CommandRuntime = commandRuntimeMock;
550550
cmdlt.SetParameterSet("SelectSubscriptionByNameParameterSet");
551551
cmdlt.SubscriptionName = azureSubscription2.Name;
552552
cmdlt.PassThru = new SwitchParameter(true);
553-
Assert.Null(AzureSession.Profile.CurrentContext.Subscription);
554553

555554
// Act
556555
cmdlt.InvokeBeginProcessing();
@@ -565,8 +564,8 @@ public void SelectAzureSubscriptionWithPassthroughPrintsSubscription()
565564
[Fact]
566565
public void SelectDefaultAzureSubscriptionByIdAndNoDefaultUpdatesProfile()
567566
{
568-
SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
569567
var client = SetupDefaultProfile();
568+
SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
570569

571570
// Setup
572571
cmdlt.CommandRuntime = commandRuntimeMock;
@@ -602,55 +601,17 @@ public void SelectDefaultAzureSubscriptionByIdAndNoDefaultUpdatesProfile()
602601
Assert.Null(client.Profile.DefaultSubscription);
603602
}
604603

605-
[Fact]
606-
public void SelectAzureSubscriptionByIdAndNoCurrentUpdatesProfile()
607-
{
608-
SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
609-
SetupDefaultProfile();
610-
611-
// Setup
612-
cmdlt.CommandRuntime = commandRuntimeMock;
613-
cmdlt.SetParameterSet("SelectSubscriptionByIdParameterSet");
614-
cmdlt.SubscriptionId = azureSubscription2.Id.ToString();
615-
Assert.Null(AzureSession.Profile.CurrentContext.Subscription);
616-
617-
// Act
618-
cmdlt.InvokeBeginProcessing();
619-
cmdlt.ExecuteCmdlet();
620-
cmdlt.InvokeEndProcessing();
621-
622-
// Verify
623-
Assert.NotNull(AzureSession.Profile.CurrentContext.Subscription);
624-
Assert.Equal(azureSubscription2.Id, AzureSession.Profile.CurrentContext.Subscription.Id);
625-
626-
cmdlt = new SelectAzureSubscriptionCommand();
627-
628-
// Setup
629-
cmdlt.CommandRuntime = commandRuntimeMock;
630-
cmdlt.SetParameterSet("NoCurrentSubscriptionParameterSet");
631-
cmdlt.NoCurrent = new SwitchParameter(true);
632-
633-
// Act
634-
cmdlt.InvokeBeginProcessing();
635-
cmdlt.ExecuteCmdlet();
636-
cmdlt.InvokeEndProcessing();
637-
638-
// Verify
639-
Assert.Null(AzureSession.Profile.CurrentContext.Subscription);
640-
}
641-
642604
[Fact]
643605
public void SelectAzureSubscriptionByInvalidIdThrowsException()
644606
{
645-
SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
646607
SetupDefaultProfile();
608+
SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
647609

648610
// Setup
649611
cmdlt.CommandRuntime = commandRuntimeMock;
650612
cmdlt.SetParameterSet("SelectSubscriptionByIdParameterSet");
651613
string invalidGuid = Guid.NewGuid().ToString();
652614
cmdlt.SubscriptionId = invalidGuid;
653-
Assert.Null(AzureSession.Profile.CurrentContext.Subscription);
654615

655616
// Act
656617
cmdlt.InvokeBeginProcessing();
@@ -661,7 +622,7 @@ public void SelectAzureSubscriptionByInvalidIdThrowsException()
661622
}
662623
catch (ArgumentException ex)
663624
{
664-
Assert.Contains(string.Format(Microsoft.WindowsAzure.Commands.Common.Properties.Resources.InvalidSubscriptionId, invalidGuid), ex.Message);
625+
Assert.Contains(string.Format("The subscription id {0} doesn't exist.\r\nParameter name: id", invalidGuid), ex.Message);
665626
}
666627
}
667628

src/Common/Commands.Common/AzurePSCmdlet.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ public abstract class AzurePSCmdlet : PSCmdlet
2929

3030
static AzurePSCmdlet()
3131
{
32-
AzureSession.Profile = new AzureProfile(
33-
Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
34-
3532
if (!TestMockSupport.RunningMocked)
3633
{
3734
AzureSession.ClientFactory.AddAction(new RPRegistrationAction());
@@ -42,6 +39,9 @@ static AzurePSCmdlet()
4239

4340
public AzurePSCmdlet()
4441
{
42+
AzureSession.Profile = new AzureProfile(
43+
Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
44+
4545
DefaultProfileClient = new ProfileClient(AzureSession.Profile);
4646
}
4747

src/Common/Commands.Common/SubscriptionCmdletBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected override void BeginProcessing()
4545
}
4646
else
4747
{
48-
ProfileClient = new ProfileClient();
48+
ProfileClient = new ProfileClient(AzureSession.Profile);
4949
}
5050
ProfileClient.WarningLog = WriteWarning;
5151
ProfileClient.DebugLog = WriteDebug;

0 commit comments

Comments
 (0)