Skip to content

Commit c5bafad

Browse files
committed
Refactorings and tests for stack - set-azurermenv
1 parent 09d4f33 commit c5bafad

File tree

3 files changed

+93
-10
lines changed

3 files changed

+93
-10
lines changed

src/ResourceManager/Profile/Commands.Profile.Test/EnvironmentCmdletTests.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,93 @@ public void SetEnvironmentWithOnPremise()
611611
Assert.Equal(env3.GetEndpoint(AzureEnvironment.Endpoint.Gallery), cmdlet3.GalleryEndpoint);
612612
}
613613

614+
[Fact]
615+
[Trait(Category.AcceptanceType, Category.CheckIn)]
616+
public void SetEnvironmentForStack()
617+
{
618+
Mock<ICommandRuntime> commandRuntimeMock = new Mock<ICommandRuntime>();
619+
SetupConfirmation(commandRuntimeMock);
620+
var cmdlet = new SetAzureRMEnvironmentCommand()
621+
{
622+
CommandRuntime = commandRuntimeMock.Object,
623+
Name = "Stack",
624+
ARMEndpoint = "https://management.local.azurestack.external/"
625+
};
626+
627+
Mock<EnvironmentHelper> envHelperMock = new Mock<EnvironmentHelper>();
628+
MetadataResponse metadataEndpoints = new MetadataResponse
629+
{
630+
GalleryEndpoint = "https://galleryendpoint",
631+
GraphEndpoint = "https://graphendpoint",
632+
PortalEndpoint = "https://portalendpoint",
633+
authentication = new Authentication
634+
{
635+
Audiences = new[] { "audience1", "audience2" },
636+
LoginEndpoint = "https://loginendpoint"
637+
}
638+
};
639+
envHelperMock.Setup(f => f.RetrieveMetaDataEndpoints(It.IsAny<string>())).ReturnsAsync(metadataEndpoints);
640+
envHelperMock.Setup(f => f.RetrieveDomain(It.IsAny<string>())).Returns("domain");
641+
cmdlet.EnvHelper = envHelperMock.Object;
642+
cmdlet.SetParameterSet("ARMEndpoint");
643+
cmdlet.InvokeBeginProcessing();
644+
cmdlet.ExecuteCmdlet();
645+
cmdlet.InvokeEndProcessing();
646+
647+
commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<PSAzureEnvironment>()), Times.Once());
648+
IAzureEnvironment env = AzureRmProfileProvider.Instance.Profile.GetEnvironment("Stack");
649+
Assert.Equal(env.Name, cmdlet.Name);
650+
Assert.Equal(cmdlet.ARMEndpoint, env.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager));
651+
Assert.Equal("https://loginendpoint/", env.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectory));
652+
Assert.Equal("audience1", env.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId));
653+
Assert.Equal("https://graphendpoint", env.GetEndpoint(AzureEnvironment.Endpoint.GraphEndpointResourceId));
654+
envHelperMock.Verify(f => f.RetrieveDomain(It.IsAny<string>()), Times.Once);
655+
envHelperMock.Verify(f => f.RetrieveMetaDataEndpoints(It.IsAny<string>()), Times.Once);
656+
657+
// Update onpremise to true
658+
var cmdlet2 = new SetAzureRMEnvironmentCommand()
659+
{
660+
CommandRuntime = commandRuntimeMock.Object,
661+
Name = "Stack",
662+
EnableAdfsAuthentication = true
663+
};
664+
665+
cmdlet2.MyInvocation.BoundParameters.Add("Name", "Stack");
666+
cmdlet2.MyInvocation.BoundParameters.Add("EnableAdfsAuthentication", true);
667+
668+
cmdlet2.InvokeBeginProcessing();
669+
cmdlet2.ExecuteCmdlet();
670+
cmdlet2.InvokeEndProcessing();
671+
672+
commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<PSAzureEnvironment>()), Times.Exactly(2));
673+
IAzureEnvironment env2 = AzureRmProfileProvider.Instance.Profile.GetEnvironment("stack");
674+
Assert.Equal(env2.Name, cmdlet2.Name);
675+
Assert.Equal(env2.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl), cmdlet.PublishSettingsFileUrl);
676+
Assert.True(env2.OnPremise);
677+
678+
// Update gallery endpoint
679+
var cmdlet3 = new SetAzureRMEnvironmentCommand()
680+
{
681+
CommandRuntime = commandRuntimeMock.Object,
682+
Name = "Stack",
683+
GalleryEndpoint = "http://galleryendpoint.com",
684+
};
685+
686+
cmdlet3.MyInvocation.BoundParameters.Add("Name", "stack");
687+
cmdlet3.MyInvocation.BoundParameters.Add("GalleryEndpoint", "http://galleryendpoint.com");
688+
689+
cmdlet3.InvokeBeginProcessing();
690+
cmdlet3.ExecuteCmdlet();
691+
cmdlet3.InvokeEndProcessing();
692+
693+
// Ensure gallery endpoint is updated and OnPremise value is preserved
694+
commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<PSAzureEnvironment>()), Times.Exactly(3));
695+
IAzureEnvironment env3 = AzureRmProfileProvider.Instance.Profile.GetEnvironment("stack");
696+
Assert.Equal(env3.Name, cmdlet3.Name);
697+
Assert.Equal(env3.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl), cmdlet.PublishSettingsFileUrl);
698+
Assert.True(env3.OnPremise);
699+
Assert.Equal(env3.GetEndpoint(AzureEnvironment.Endpoint.Gallery), cmdlet3.GalleryEndpoint);
700+
}
614701

615702
[Fact]
616703
[Trait(Category.AcceptanceType, Category.CheckIn)]

src/ResourceManager/Profile/Commands.Profile/Environment/AddAzureRMEnvironment.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,11 @@ public override void ExecuteCmdlet()
171171
env.Value?.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager)?.ToLowerInvariant(),
172172
GeneralUtilities.EnsureTrailingSlash(ARMEndpoint)?.ToLowerInvariant(), StringComparison.CurrentCultureIgnoreCase));
173173

174-
IAzureEnvironment newEnvironment = new AzureEnvironment { Name = this.Name };
175174
var defProfile = GetDefaultProfile();
176-
if (defProfile != null)
175+
IAzureEnvironment newEnvironment;
176+
if (!defProfile.TryGetEnvironment(this.Name, out newEnvironment))
177177
{
178-
IAzureEnvironment _newEnvironment;
179-
defProfile.TryGetEnvironment(this.Name, out _newEnvironment);
180-
newEnvironment = (_newEnvironment ?? newEnvironment);
178+
newEnvironment = new AzureEnvironment { Name = this.Name };
181179
}
182180

183181
if (publicEnvironment.Key == null)

src/ResourceManager/Profile/Commands.Profile/Environment/SetAzureRMEnvironment.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,11 @@ public override void ExecuteCmdlet()
173173
env.Value?.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager)?.ToLowerInvariant(),
174174
GeneralUtilities.EnsureTrailingSlash(ARMEndpoint)?.ToLowerInvariant(), StringComparison.CurrentCultureIgnoreCase));
175175

176-
IAzureEnvironment newEnvironment = new AzureEnvironment { Name = this.Name };
177176
var defProfile = GetDefaultProfile();
178-
if (defProfile != null)
177+
IAzureEnvironment newEnvironment;
178+
if (!defProfile.TryGetEnvironment(this.Name, out newEnvironment))
179179
{
180-
IAzureEnvironment _newEnvironment;
181-
defProfile.TryGetEnvironment(this.Name, out _newEnvironment);
182-
newEnvironment = (_newEnvironment ?? newEnvironment);
180+
newEnvironment = new AzureEnvironment { Name = this.Name };
183181
}
184182

185183
if (publicEnvironment.Key == null)

0 commit comments

Comments
 (0)