|
15 | 15 | using Microsoft.Azure.Commands.Common.Authentication;
|
16 | 16 | using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
|
17 | 17 | using Microsoft.Azure.Commands.Common.Authentication.Models;
|
| 18 | +using Microsoft.Azure.Commands.Common.Authentication.ResourceManager; |
18 | 19 | using Microsoft.Azure.Commands.Profile;
|
19 | 20 | using Microsoft.Azure.Commands.Profile.Models;
|
20 | 21 | using Microsoft.Azure.Commands.Profile.Utilities;
|
@@ -715,6 +716,104 @@ public void SetEnvironmentForStack()
|
715 | 716 | Assert.Equal(env3.GetEndpoint(AzureEnvironment.Endpoint.Gallery), cmdlet3.GalleryEndpoint);
|
716 | 717 | }
|
717 | 718 |
|
| 719 | + [Fact] |
| 720 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 721 | + public void SetEnvironmentForMultipleContexts() |
| 722 | + { |
| 723 | + // Add new environment |
| 724 | + Mock<ICommandRuntime> commandRuntimeMock = new Mock<ICommandRuntime>(); |
| 725 | + SetupConfirmation(commandRuntimeMock); |
| 726 | + var cmdlet = new AddAzureRMEnvironmentCommand() |
| 727 | + { |
| 728 | + CommandRuntime = commandRuntimeMock.Object, |
| 729 | + Name = "Katal", |
| 730 | + ARMEndpoint = "https://management.azure.com/", |
| 731 | + AzureKeyVaultDnsSuffix = "vault.local.azurestack.external", |
| 732 | + AzureKeyVaultServiceEndpointResourceId = "https://vault.local.azurestack.external" |
| 733 | + |
| 734 | + }; |
| 735 | + var dict = new Dictionary<string, object> |
| 736 | + { |
| 737 | + { "ARMEndpoint", "https://management.azure.com/" }, |
| 738 | + { "AzureKeyVaultDnsSuffix", "vault.local.azurestack.external" }, |
| 739 | + { "AzureKeyVaultServiceEndpointResourceId", "https://vault.local.azurestack.external" } |
| 740 | + }; |
| 741 | + |
| 742 | + cmdlet.SetBoundParameters(dict); |
| 743 | + cmdlet.SetParameterSet("ARMEndpoint"); |
| 744 | + cmdlet.InvokeBeginProcessing(); |
| 745 | + cmdlet.ExecuteCmdlet(); |
| 746 | + cmdlet.InvokeEndProcessing(); |
| 747 | + var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.GetProfile<AzureRmProfile>()); |
| 748 | + IAzureEnvironment env = AzureRmProfileProvider.Instance.Profile.Environments.First((e) => string.Equals(e.Name, "KaTaL", StringComparison.OrdinalIgnoreCase)); |
| 749 | + Assert.Equal(env.Name, cmdlet.Name); |
| 750 | + Assert.Equal(env.GetEndpoint(AzureEnvironment.Endpoint.AzureKeyVaultDnsSuffix), dict["AzureKeyVaultDnsSuffix"]); |
| 751 | + Assert.Equal(env.GetEndpoint(AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId), dict["AzureKeyVaultServiceEndpointResourceId"]); |
| 752 | + |
| 753 | + // Create contexts using the new environment |
| 754 | + var profile = new AzureRmProfile(); |
| 755 | + string contextName1; |
| 756 | + var context1 = (new AzureContext { Environment = env }) |
| 757 | + .WithAccount(new AzureAccount { Id = "[email protected]" }) |
| 758 | + .WithTenant(new AzureTenant { Id = Guid.NewGuid().ToString(), Directory = "contoso.com" }) |
| 759 | + .WithSubscription(new AzureSubscription { Id = Guid.NewGuid().ToString(), Name = "Contoso Subscription 1" }); |
| 760 | + profile.TryAddContext(context1, out contextName1); |
| 761 | + string contextName2; |
| 762 | + var context2 = (new AzureContext { Environment = env }) |
| 763 | + .WithAccount(new AzureAccount { Id = "[email protected]" }) |
| 764 | + .WithTenant(new AzureTenant { Id = Guid.NewGuid().ToString(), Directory = "contoso.cn" }) |
| 765 | + .WithSubscription(new AzureSubscription { Id = Guid.NewGuid().ToString(), Name = "Contoso Subscription 2" }); |
| 766 | + profile.TryAddContext(context2, out contextName2); |
| 767 | + profile.TrySetDefaultContext(context1); |
| 768 | + AzureRmProfileProvider.Instance.Profile = profile; |
| 769 | + |
| 770 | + // Update the environment with new endpoints |
| 771 | + commandRuntimeMock = new Mock<ICommandRuntime>(); |
| 772 | + SetupConfirmation(commandRuntimeMock); |
| 773 | + var cmdlet2 = new AddAzureRMEnvironmentCommand() |
| 774 | + { |
| 775 | + CommandRuntime = commandRuntimeMock.Object, |
| 776 | + Name = "Katal", |
| 777 | + ARMEndpoint = "https://management.azure.com/", |
| 778 | + AzureKeyVaultDnsSuffix = "adminvault.local.azurestack.external", |
| 779 | + AzureKeyVaultServiceEndpointResourceId = "https://adminvault.local.azurestack.external" |
| 780 | + }; |
| 781 | + |
| 782 | + dict.Clear(); |
| 783 | + dict = new Dictionary<string, object> |
| 784 | + { |
| 785 | + { "ARMEndpoint", "https://management.azure.com/" }, |
| 786 | + { "AzureKeyVaultDnsSuffix", "adminvault.local.azurestack.external" }, |
| 787 | + { "AzureKeyVaultServiceEndpointResourceId", "https://adminvault.local.azurestack.external" } |
| 788 | + }; |
| 789 | + |
| 790 | + cmdlet2.SetBoundParameters(dict); |
| 791 | + cmdlet2.SetParameterSet("ARMEndpoint"); |
| 792 | + cmdlet2.InvokeBeginProcessing(); |
| 793 | + cmdlet2.ExecuteCmdlet(); |
| 794 | + cmdlet2.InvokeEndProcessing(); |
| 795 | + |
| 796 | + profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.GetProfile<AzureRmProfile>()); |
| 797 | + env = AzureRmProfileProvider.Instance.Profile.Environments.First((e) => string.Equals(e.Name, "KaTaL", StringComparison.OrdinalIgnoreCase)); |
| 798 | + Assert.Equal(env.Name, cmdlet.Name); |
| 799 | + Assert.Equal(env.GetEndpoint(AzureEnvironment.Endpoint.AzureKeyVaultDnsSuffix), dict["AzureKeyVaultDnsSuffix"]); |
| 800 | + Assert.Equal(env.GetEndpoint(AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId), dict["AzureKeyVaultServiceEndpointResourceId"]); |
| 801 | + |
| 802 | + // Validate that the endpoints were updated in the contexts |
| 803 | + profile = (AzureRmProfile)AzureRmProfileProvider.Instance.Profile; |
| 804 | + Assert.NotNull(profile); |
| 805 | + Assert.NotNull(profile.Contexts); |
| 806 | + Assert.NotEmpty(profile.Contexts); |
| 807 | + foreach (var context in profile.Contexts.Values) |
| 808 | + { |
| 809 | + Assert.NotNull(context); |
| 810 | + Assert.NotNull(context.Environment); |
| 811 | + Assert.Equal(context.Environment.Name, env.Name); |
| 812 | + Assert.Equal(context.Environment.AzureKeyVaultDnsSuffix, env.AzureKeyVaultDnsSuffix); |
| 813 | + Assert.Equal(context.Environment.AzureKeyVaultServiceEndpointResourceId, env.AzureKeyVaultServiceEndpointResourceId); |
| 814 | + } |
| 815 | + } |
| 816 | + |
718 | 817 | [Fact]
|
719 | 818 | [Trait(Category.AcceptanceType, Category.CheckIn)]
|
720 | 819 | public void RemovesAzureEnvironment()
|
|
0 commit comments