Пример #1
0
        public void GetAzureContextNoLogin()
        {
            var cmdlt = new GetAzureRMContextCommand();

            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.DisableDataCollection();
            var profile = AzureRmProfileProvider.Instance.Profile;

            AzureRmProfileProvider.Instance.Profile = new AzureRmProfile();

            try
            {
                // Act
                cmdlt.InvokeBeginProcessing();
                cmdlt.ExecuteCmdlet();
                cmdlt.InvokeEndProcessing();
            }
            finally
            {
                AzureRmProfileProvider.Instance.Profile = profile;
            }

            // Verify
            Assert.True(commandRuntimeMock.OutputPipeline.Count == 0);
        }
Пример #2
0
        public void GetAzureContextNoLogin()
        {
            var cmdlt = new GetAzureRMContextCommand();

            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;
            cmdlt.DisableDataCollection();
            var profile = AzureRmProfileProvider.Instance.Profile;

            AzureRmProfileProvider.Instance.Profile = new AzureRmProfile();

            try
            {
                // Act
                cmdlt.InvokeBeginProcessing();
                cmdlt.ExecuteCmdlet();
                cmdlt.InvokeEndProcessing();
            }
            finally
            {
                AzureRmProfileProvider.Instance.Profile = profile;
            }

            // Verify
            Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
            var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];

            Assert.True(context == null || context.Account == null || context.Account.Id == null);
            Assert.True(commandRuntimeMock.ErrorStream.Count == 1);
            var error = commandRuntimeMock.ErrorStream[0];

            Assert.Equal("Run Connect-AzureRmAccount to login.", error.Exception.Message);
        }
Пример #3
0
        public void CheckHidenServicePrincipalSecret()
        {
            var cmdlet = new GetAzureRMContextCommand();

            // Setup
            cmdlet.CommandRuntime = commandRuntimeMock;
            var    profile          = new AzureRmProfile();
            string subscriptionName = "Contoso Subscription 1";
            string accountId        = "7a5db92d-499a-46be-8d6e-6666eeee0000";
            string contextName;
            var    contextTemp = (new AzureContext {
                Environment = AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud]
            })
                                 .WithAccount(new AzureAccount {
                Id = accountId, Type = "ServicePrincipal"
            })
                                 .WithTenant(new AzureTenant {
                Id = Guid.NewGuid().ToString(), Directory = "contoso.com"
            })
                                 .WithSubscription(new AzureSubscription {
                Id = Guid.NewGuid().ToString(), Name = subscriptionName
            });

            contextTemp.Account.SetProperty(AzureAccount.Property.ServicePrincipalSecret, "5P6******************");
            contextTemp.Account.SetProperty(AzureAccount.Property.Subscriptions, contextTemp.Subscription.Id);
            contextTemp.Account.SetProperty(AzureAccount.Property.Tenants, contextTemp.Tenant.Id);
            profile.TryAddContext(contextTemp, out contextName);
            cmdlet.DefaultProfile = profile;

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

            // Verify
            Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
            var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];

            Assert.Equal(subscriptionName, context.Subscription.Name);
            Assert.Equal(accountId, context.Account.Id);
            var accountExtendedProperties = context.Account.ExtendedProperties;

            Assert.Equal(2, accountExtendedProperties.Count());
            Assert.False(accountExtendedProperties.ContainsKey(AzureAccount.Property.ServicePrincipalSecret));
        }
Пример #4
0
        public void GetAzureContext()
        {
            var cmdlt = new GetAzureRMContextCommand();

            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;

            // Act
            cmdlt.InvokeBeginProcessing();
            cmdlt.ExecuteCmdlet();
            cmdlt.InvokeEndProcessing();

            // Verify
            Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
            var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];

            Assert.Equal("test", context.Subscription.Name);
        }
Пример #5
0
        public void DataCollectionHandlesDirectoryExistenceErrors()
        {
            TestExecutionHelpers.SetUpSessionAndProfile();
            Mock <IDataStore> mock = new Mock <IDataStore>();

            mock.Setup(f => f.DirectoryExists(It.IsAny <string>())).Throws(new IOException("This should not be raised"));
            mock.Setup(f => f.FileExists(It.IsAny <string>())).Returns(true);
            mock.Setup(f => f.DeleteFile(It.IsAny <string>()));
            var oldDataStore = AzureSession.Instance.DataStore;

            AzureSession.Instance.DataStore = mock.Object;
            try
            {
                GetAzureRMContextCommand command = new GetAzureRMContextCommand();
                command.CommandRuntime = new MockCommandRuntime();
                command.InvokeBeginProcessing();
                command.InvokeEndProcessing();
            }
            finally
            {
                AzureSession.Instance.DataStore = oldDataStore;
            }
        }
Пример #6
0
        public void ClearContextSetsDefaultContextName()
        {
            var getCmdlet          = new GetAzureRMContextCommand();
            var profile            = CreateMultipleContextProfile();
            var defaultContextName = profile.DefaultContextKey;

            getCmdlet.CommandRuntime = commandRuntimeMock;
            getCmdlet.DefaultProfile = profile;
            getCmdlet.InvokeBeginProcessing();
            getCmdlet.ExecuteCmdlet();
            getCmdlet.InvokeEndProcessing();
            Assert.True(commandRuntimeMock.OutputPipeline != null);
            Assert.Single(commandRuntimeMock.OutputPipeline);
            Assert.Equal(defaultContextName, ((PSAzureContext)commandRuntimeMock.OutputPipeline[0]).Name);

            var clearCmdlet = new ClearAzureRmContext();

            commandRuntimeMock         = new MockCommandRuntime();
            clearCmdlet.CommandRuntime = commandRuntimeMock;
            clearCmdlet.DefaultProfile = profile;
            clearCmdlet.Scope          = ContextModificationScope.Process;
            clearCmdlet.PassThru       = true;
            clearCmdlet.InvokeBeginProcessing();
            clearCmdlet.ExecuteCmdlet();
            clearCmdlet.InvokeEndProcessing();
            Assert.NotNull(commandRuntimeMock.OutputPipeline);
            Assert.Single(commandRuntimeMock.OutputPipeline);
            var result = (bool)commandRuntimeMock.OutputPipeline[0];

            Assert.True(result);
            Assert.True(profile.Contexts != null);
            Assert.Equal(1, profile.Contexts.Count);
            Assert.True(profile.Contexts.ContainsKey("Default"));
            Assert.NotNull(profile.DefaultContext);
            Assert.Null(profile.DefaultContext.Account);
            Assert.Null(profile.DefaultContext.Subscription);
        }
Пример #7
0
        public void ListMultipleContexts()
        {
            var cmdlet  = new GetAzureRMContextCommand();
            var profile = CreateMultipleContextProfile();

            cmdlet.CommandRuntime = commandRuntimeMock;
            cmdlet.DefaultProfile = profile;
            cmdlet.ListAvailable  = true;
            cmdlet.InvokeBeginProcessing();
            cmdlet.ExecuteCmdlet();
            cmdlet.InvokeEndProcessing();
            Assert.True(commandRuntimeMock.OutputPipeline != null && commandRuntimeMock.OutputPipeline.Count == profile.Contexts.Count);
            foreach (var output in commandRuntimeMock.OutputPipeline)
            {
                PSAzureContext testContext = output as PSAzureContext;
                Assert.NotNull(testContext);
                Assert.NotNull(testContext.Name);
                Assert.True(profile.Contexts.ContainsKey(testContext.Name));
                var validateContext = profile.Contexts[testContext.Name];
                Assert.NotNull(validateContext);
                Assert.True(validateContext.IsEqual(testContext));
                profile.TryRemoveContext(testContext.Name);
            }
        }
Пример #8
0
        public void DataCollectionHandlesSerializationErrors()
        {
            TestExecutionHelpers.SetUpSessionAndProfile();
            MemoryDataStore dataStore;
            string          oldFileValue       = null;
            var             dataCollectionPath = Path.Combine(AzureSession.Instance.ProfileDirectory, AzurePSDataCollectionProfile.DefaultFileName);
            var             oldDataStore       = AzureSession.Instance.DataStore;
            var             memoryStore        = oldDataStore as MemoryDataStore;

            if (memoryStore != null)
            {
                dataStore = memoryStore;
                if (dataStore.VirtualStore.ContainsKey(dataCollectionPath))
                {
                    oldFileValue = dataStore.VirtualStore[dataCollectionPath];
                }
            }
            else
            {
                dataStore = new MemoryDataStore();
            }

            dataStore.VirtualStore.Add(dataCollectionPath, "{{{{{{{{{{}}}--badly-formatted-file");
            AzureSession.Instance.DataStore = dataStore;
            try
            {
                GetAzureRMContextCommand command = new GetAzureRMContextCommand();
                command.CommandRuntime = new MockCommandRuntime();
                command.InvokeBeginProcessing();
            }
            finally
            {
                dataStore.VirtualStore[dataCollectionPath] = oldFileValue;
                AzureSession.Instance.DataStore            = oldDataStore;
            }
        }