public void ThrowsWhenSettingPublicEnvironment()
        {
            Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>();

            SetupConfirmation(commandRuntimeMock);
            var profile = new AzureRmProfile();

            foreach (var env in AzureEnvironment.PublicEnvironments)
            {
                Assert.True(profile.GetEnvironment(env.Key) != null, string.Format("Key: {0} produces null environment.  From profile {1}", env.Key, profile.ToString()));
                var cmdlet = new SetAzureRMEnvironmentCommand
                {
                    CommandRuntime         = commandRuntimeMock.Object,
                    Name                   = env.Key,
                    PublishSettingsFileUrl = "http://microsoft.com",
                    DefaultProfile         = profile
                };
                var savedValue = env.Value.PublishSettingsFileUrl;
                cmdlet.InvokeBeginProcessing();
                Assert.Throws <InvalidOperationException>(() => cmdlet.ExecuteCmdlet());
                var environment = profile.GetEnvironment(env.Key);
                Assert.True(environment != null, string.Format("Key: {0} produces null environment.  From profile {1}", env.Key, profile.ToString()));
                Assert.Equal(savedValue, environment.PublishSettingsFileUrl);
                Assert.NotEqual(cmdlet.PublishSettingsFileUrl, environment.PublishSettingsFileUrl);
            }
        }
Beispiel #2
0
        public void ThrowsWhenSettingPublicEnvironment()
        {
            Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>();

            foreach (string name in AzureEnvironment.PublicEnvironments.Keys)
            {
                var cmdlet = new SetAzureRMEnvironmentCommand()
                {
                    CommandRuntime         = commandRuntimeMock.Object,
                    Name                   = name,
                    PublishSettingsFileUrl = "http://microsoft.com"
                };
                var savedValue = AzureEnvironment.PublicEnvironments[name].GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl);
                cmdlet.InvokeBeginProcessing();
                Assert.Throws <InvalidOperationException>(() => cmdlet.ExecuteCmdlet());
                var newValue = AzureRmProfileProvider.Instance.Profile.Environments[name].GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl);
                Assert.Equal(savedValue, newValue);
                Assert.NotEqual(cmdlet.PublishSettingsFileUrl, newValue);
            }
        }
        public void SetEnvironmentForStack()
        {
            Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>();

            SetupConfirmation(commandRuntimeMock);
            var cmdlet = new SetAzureRMEnvironmentCommand()
            {
                CommandRuntime = commandRuntimeMock.Object,
                Name           = "Stack",
                ARMEndpoint    = "https://management.local.azurestack.external/"
            };

            Mock <EnvironmentHelper> envHelperMock     = new Mock <EnvironmentHelper>();
            MetadataResponse         metadataEndpoints = new MetadataResponse
            {
                GalleryEndpoint = "https://galleryendpoint",
                GraphEndpoint   = "https://graphendpoint",
                PortalEndpoint  = "https://portalendpoint",
                authentication  = new Authentication
                {
                    Audiences     = new[] { "audience1", "audience2" },
                    LoginEndpoint = "https://loginendpoint"
                }
            };

            envHelperMock.Setup(f => f.RetrieveMetaDataEndpoints(It.IsAny <string>())).ReturnsAsync(metadataEndpoints);
            envHelperMock.Setup(f => f.RetrieveDomain(It.IsAny <string>())).Returns("domain");
            cmdlet.EnvHelper = envHelperMock.Object;
            cmdlet.SetParameterSet("ARMEndpoint");
            cmdlet.InvokeBeginProcessing();
            cmdlet.ExecuteCmdlet();
            cmdlet.InvokeEndProcessing();

            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Once());
            IAzureEnvironment env = AzureRmProfileProvider.Instance.Profile.GetEnvironment("Stack");

            Assert.Equal(env.Name, cmdlet.Name);
            Assert.Equal(cmdlet.ARMEndpoint, env.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager));
            Assert.Equal("https://loginendpoint/", env.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectory));
            Assert.Equal("audience1", env.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId));
            Assert.Equal("https://graphendpoint", env.GetEndpoint(AzureEnvironment.Endpoint.GraphEndpointResourceId));
            envHelperMock.Verify(f => f.RetrieveDomain(It.IsAny <string>()), Times.Once);
            envHelperMock.Verify(f => f.RetrieveMetaDataEndpoints(It.IsAny <string>()), Times.Once);

            // Update onpremise to true
            var cmdlet2 = new SetAzureRMEnvironmentCommand()
            {
                CommandRuntime           = commandRuntimeMock.Object,
                Name                     = "Stack",
                EnableAdfsAuthentication = true
            };

            cmdlet2.MyInvocation.BoundParameters.Add("Name", "Stack");
            cmdlet2.MyInvocation.BoundParameters.Add("EnableAdfsAuthentication", true);

            cmdlet2.InvokeBeginProcessing();
            cmdlet2.ExecuteCmdlet();
            cmdlet2.InvokeEndProcessing();

            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Exactly(2));
            IAzureEnvironment env2 = AzureRmProfileProvider.Instance.Profile.GetEnvironment("stack");

            Assert.Equal(env2.Name, cmdlet2.Name);
            Assert.Equal(env2.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl), cmdlet.PublishSettingsFileUrl);
            Assert.True(env2.OnPremise);

            // Update gallery endpoint
            var cmdlet3 = new SetAzureRMEnvironmentCommand()
            {
                CommandRuntime  = commandRuntimeMock.Object,
                Name            = "Stack",
                GalleryEndpoint = "http://galleryendpoint.com",
            };

            cmdlet3.MyInvocation.BoundParameters.Add("Name", "stack");
            cmdlet3.MyInvocation.BoundParameters.Add("GalleryEndpoint", "http://galleryendpoint.com");

            cmdlet3.InvokeBeginProcessing();
            cmdlet3.ExecuteCmdlet();
            cmdlet3.InvokeEndProcessing();

            // Ensure gallery endpoint is updated and OnPremise value is preserved
            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Exactly(3));
            IAzureEnvironment env3 = AzureRmProfileProvider.Instance.Profile.GetEnvironment("stack");

            Assert.Equal(env3.Name, cmdlet3.Name);
            Assert.Equal(env3.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl), cmdlet.PublishSettingsFileUrl);
            Assert.True(env3.OnPremise);
            Assert.Equal(env3.GetEndpoint(AzureEnvironment.Endpoint.Gallery), cmdlet3.GalleryEndpoint);
        }
        public void SetEnvironmentWithOnPremise()
        {
            // Setup a new environment
            Mock <ICommandRuntime> commandRuntimeMock = new Mock <ICommandRuntime>();

            SetupConfirmation(commandRuntimeMock);

            var cmdlet = new SetAzureRMEnvironmentCommand()
            {
                CommandRuntime           = commandRuntimeMock.Object,
                Name                     = "Katal",
                PublishSettingsFileUrl   = "http://microsoft.com",
                EnableAdfsAuthentication = false
            };

            cmdlet.MyInvocation.BoundParameters.Add("Name", "Katal");
            cmdlet.MyInvocation.BoundParameters.Add("PublishSettingsFileUrl", "http://microsoft.com");
            cmdlet.MyInvocation.BoundParameters.Add("EnableAdfsAuthentication", false);

            cmdlet.InvokeBeginProcessing();
            cmdlet.ExecuteCmdlet();
            cmdlet.InvokeEndProcessing();

            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Once());
            IAzureEnvironment env = AzureRmProfileProvider.Instance.Profile.GetEnvironment("KaTaL");

            Assert.Equal(env.Name, cmdlet.Name);
            Assert.Equal(env.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl), cmdlet.PublishSettingsFileUrl);
            Assert.False(env.OnPremise);

            // Update onpremise to true
            var cmdlet2 = new SetAzureRMEnvironmentCommand()
            {
                CommandRuntime           = commandRuntimeMock.Object,
                Name                     = "Katal",
                EnableAdfsAuthentication = true
            };

            cmdlet2.MyInvocation.BoundParameters.Add("Name", "Katal");
            cmdlet2.MyInvocation.BoundParameters.Add("EnableAdfsAuthentication", true);

            cmdlet2.InvokeBeginProcessing();
            cmdlet2.ExecuteCmdlet();
            cmdlet2.InvokeEndProcessing();

            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Exactly(2));
            IAzureEnvironment env2 = AzureRmProfileProvider.Instance.Profile.GetEnvironment("KaTaL");

            Assert.Equal(env2.Name, cmdlet2.Name);
            Assert.Equal(env2.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl), cmdlet.PublishSettingsFileUrl);
            Assert.True(env2.OnPremise);

            // Update gallery endpoint
            var cmdlet3 = new SetAzureRMEnvironmentCommand()
            {
                CommandRuntime  = commandRuntimeMock.Object,
                Name            = "Katal",
                GalleryEndpoint = "http://galleryendpoint.com",
            };

            cmdlet3.MyInvocation.BoundParameters.Add("Name", "Katal");
            cmdlet3.MyInvocation.BoundParameters.Add("GalleryEndpoint", "http://galleryendpoint.com");

            cmdlet3.InvokeBeginProcessing();
            cmdlet3.ExecuteCmdlet();
            cmdlet3.InvokeEndProcessing();

            // Ensure gallery endpoint is updated and OnPremise value is preserved
            commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny <PSAzureEnvironment>()), Times.Exactly(3));
            IAzureEnvironment env3 = AzureRmProfileProvider.Instance.Profile.GetEnvironment("KaTaL");

            Assert.Equal(env3.Name, cmdlet3.Name);
            Assert.Equal(env3.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl), cmdlet.PublishSettingsFileUrl);
            Assert.True(env3.OnPremise);
            Assert.Equal(env3.GetEndpoint(AzureEnvironment.Endpoint.Gallery), cmdlet3.GalleryEndpoint);
        }