public void TestUninstallAzureRmUnauthorized()
        {
            var dataStore = new MockDataStore();

            AzureSession.Instance.DataStore = dataStore;
            dataStore.CreateDirectory("testmodulepath");
            dataStore.CreateDirectory(Path.Combine("testmodulepath", "AzureRM.ApiManagement"));
            dataStore.WriteFile(Path.Combine("testmodulepath", Path.Combine("AzureRM.ApiManagement", "file1")), new byte[2]);
            dataStore.LockAccessToFile(Path.Combine("testmodulepath", "AzureRM.ApiManagement"));
            // Ensure read does not throw
            Assert.True(dataStore.DirectoryExists(Path.Combine("testmodulepath", "AzureRM.ApiManagement")));

            var cmdlet = new UninstallAzureRmCommand();

            Environment.SetEnvironmentVariable("PSModulePath", "testmodulepath");
            try
            {
                cmdlet.ExecuteCmdlet();
                // Throw incorrect exception if cmdlet does not throw
                Assert.True(false);
            }
            catch (UnauthorizedAccessException e)
            {
                Assert.Equal("Module deletion failed. Please close all PowerShell sessions to ensure no AzureRM modules are currently loaded, and rerun this cmdlet in Administrator mode.", e.Message);
            }
        }
        public void LoadingProfileWorks()
        {
            string contents  = @"{
  ""Environments"": {
    ""testCloud"": {
      ""Name"": ""testCloud"",
      ""OnPremise"": false,
      ""Endpoints"": {
        ""ActiveDirectory"": ""http://contoso.com""
      }
    }
  },
  ""Context"": {
    ""TokenCache"": ""AgAAAAAAAAA="",
    ""Account"": {
      ""Id"": ""*****@*****.**"",
      ""Type"": 1,
      ""Properties"": {
        ""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6""
      }
    },
    ""Subscription"": {
      ""Id"": ""00000000-0000-0000-0000-000000000000"",
      ""Name"": ""Contoso Test Subscription"",
      ""Environment"": ""testCloud"",
      ""Account"": ""*****@*****.**"",
      ""Properties"": {
        ""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6""
      }
    },
    ""Environment"": {
      ""Name"": ""testCloud"",
      ""OnPremise"": false,
      ""Endpoints"": {
        ""ActiveDirectory"": ""http://contoso.com""
      }
    },
    ""Tenant"": {
      ""Id"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6"",
      ""Domain"": ""contoso.com""
    }
  }
}";
            var    path      = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AzureSession.Instance.ProfileFile);
            var    dataStore = new MockDataStore();

            AzureSession.Instance.DataStore = dataStore;
            dataStore.WriteFile(path, contents);
            var profile = new AzureRmProfile(path);

            Assert.Equal(5, profile.Environments.Count());
            Assert.Equal("3c0ff8a7-e8bb-40e8-ae66-271343379af6", profile.DefaultContext.Tenant.Id.ToString());
            Assert.Equal("contoso.com", profile.DefaultContext.Tenant.Directory);
            Assert.Equal("00000000-0000-0000-0000-000000000000", profile.DefaultContext.Subscription.Id.ToString());
            Assert.Equal("testCloud", profile.DefaultContext.Environment.Name);
            Assert.Equal("*****@*****.**", profile.DefaultContext.Account.Id);
            Assert.Equal(AzureAccount.AccountType.User, profile.DefaultContext.Account.Type);
            Assert.Equal(new byte[] { 2, 0, 0, 0, 0, 0, 0, 0 }, profile.DefaultContext.TokenCache.CacheData);
            Assert.Equal(path, profile.ProfilePath);
        }
示例#3
0
        public void ProcessEnvHigherThanUserConfig()
        {
            const string retryKey = "Retry";
            const string envName  = "ENV_FOR_RETRY";
            var          config   = new SimpleTypedConfig <int>(retryKey, "", -1, envName);

            var path      = Path.GetRandomFileName();
            var dataStore = new MockDataStore();

            dataStore.WriteFile(path,
                                @"{
    ""Az"": {
        ""Retry"": 100
    }
}");
            var env = new MockEnvironmentVariableProvider();

            env.Set(envName, "10", System.EnvironmentVariableTarget.Process);

            IConfigManager icm = GetConfigManager(
                new InitSettings()
            {
                DataStore = dataStore,
                Path      = path,
                EnvironmentVariableProvider = env
            },
                config);

            Assert.Equal(10, icm.GetConfigValue <int>(retryKey));
        }
        public void CanImportConfig()
        {
            string key           = "key1";
            var    config        = new SimpleTypedConfig <bool>(key, "", true);
            var    dataStore     = new MockDataStore();
            var    configManager = GetConfigManager(
                new InitSettings()
            {
                DataStore = dataStore
            },
                config);

            // import a config file
            string path = Path.GetTempFileName();

            dataStore.WriteFile(path,
                                @"{
    ""Az"": {
        ""key1"": false
    }
}");
            new JsonConfigHelper(configManager.ConfigFilePath, dataStore).ImportConfigFile(path);
            configManager.BuildConfig();

            // configs should be imported correctly
            Assert.False(configManager.GetConfigValue <bool>(key));
        }
示例#5
0
        public void LoadingProfileWorks()
        {
            string contents  = @"{
  ""Environments"": {
    ""testCloud"": {
      ""Name"": ""testCloud"",
      ""OnPremise"": false,
      ""Endpoints"": {
        ""ActiveDirectory"": ""http://contoso.com""
      }
    }
  },
  ""Context"": {
    ""TokenCache"": ""AQIDBAUGCAkA"",
    ""Account"": {
      ""Id"": ""*****@*****.**"",
      ""Type"": 1,
      ""Properties"": {
        ""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6""
      }
    },
    ""Subscription"": {
      ""Id"": ""00000000-0000-0000-0000-000000000000"",
      ""Name"": ""Contoso Test Subscription"",
      ""Environment"": ""testCloud"",
      ""Account"": ""*****@*****.**"",
      ""Properties"": {
        ""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6""
      }
    },
    ""Environment"": {
      ""Name"": ""testCloud"",
      ""OnPremise"": false,
      ""Endpoints"": {
        ""ActiveDirectory"": ""http://contoso.com""
      }
    },
    ""Tenant"": {
      ""Id"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6"",
      ""Domain"": ""contoso.com""
    }
  }
}";
            string path      = Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile);
            var    dataStore = new MockDataStore();

            AzureSession.DataStore = dataStore;
            dataStore.WriteFile(path, contents);
            var profile = new AzureRMProfile(path);

            Assert.Equal(4, profile.Environments.Count);
            Assert.Equal("3c0ff8a7-e8bb-40e8-ae66-271343379af6", profile.Context.Tenant.Id.ToString());
            Assert.Equal("contoso.com", profile.Context.Tenant.Domain);
            Assert.Equal("00000000-0000-0000-0000-000000000000", profile.Context.Subscription.Id.ToString());
            Assert.Equal("testCloud", profile.Context.Environment.Name);
            Assert.Equal("*****@*****.**", profile.Context.Account.Id);
            Assert.Equal(new byte[] { 1, 2, 3, 4, 5, 6, 8, 9, 0 }, profile.Context.TokenCache);
            Assert.Equal(path, profile.ProfilePath);
        }
        public void TestUninstallAzureRmMultiplePaths()
        {
            var dataStore = new MockDataStore();

            AzureSession.Instance.DataStore = dataStore;
            dataStore.CreateDirectory("testmodulepath");
            dataStore.CreateDirectory("testmodulepath2");
            dataStore.CreateDirectory(Path.Combine("testmodulepath", "AzureRM.ApiManagement"));
            dataStore.CreateDirectory(Path.Combine("testmodulepath2", "AzureRM.Profile"));
            dataStore.WriteFile(Path.Combine("testmodulepath", Path.Combine("AzureRM.ApiManagement", "file1")), new byte[2]);
            dataStore.WriteFile(Path.Combine("testmodulepath2", Path.Combine("AzureRM.Profile", "file1")), new byte[2]);
            // Ensure read does not throw
            Assert.True(dataStore.DirectoryExists(Path.Combine("testmodulepath", "AzureRM.ApiManagement")));
            Assert.True(dataStore.DirectoryExists(Path.Combine("testmodulepath2", "AzureRM.Profile")));

            var cmdlet = new UninstallAzureRmCommand();

            Environment.SetEnvironmentVariable("PSModulePath", "testmodulepath;testmodulepath2;pathdoesntexist");
            cmdlet.ExecuteCmdlet();
            Assert.False(dataStore.DirectoryExists(Path.Combine("testmodulepath", "AzureRM.ApiManagement")));
            Assert.False(dataStore.DirectoryExists(Path.Combine("testmodulepath2", "AzureRM.Profile")));
        }
示例#7
0
        public void CanUpdateJsonFile()
        {
            const string retryKey    = "Retry";
            var          intConfig   = new SimpleTypedConfig <int>(retryKey, "", -1);
            const string arrayKey    = "Array";
            var          arrayConfig = new SimpleTypedConfig <string[]>(arrayKey, "", null);
            var          path        = Path.GetRandomFileName();
            var          dataStore   = new MockDataStore();

            dataStore.WriteFile(path,
                                @"{
    ""Az"": {
        ""Retry"": 100
    },
    ""Az.KeyVault"": {
        ""Array"": [""a"",""b""]
    }
}");
            IConfigManager icm = GetConfigManager(
                new InitSettings()
            {
                DataStore = dataStore,
                Path      = path
            },
                intConfig, arrayConfig);
            ConfigManager cm = icm as ConfigManager;

            Assert.Equal(100, cm.GetConfigValue <int>(retryKey));
            Assert.Equal(new string[] { "a", "b" }, cm.GetConfigValueInternal <string[]>(arrayKey, new InternalInvocationInfo()
            {
                ModuleName = "Az.KeyVault"
            }));
            ConfigData updated = icm.UpdateConfig(new UpdateConfigOptions(retryKey, 10, ConfigScope.CurrentUser));

            Assert.Equal(10, updated.Value);
            Assert.Equal(10, icm.GetConfigValue <int>(retryKey));

            string[]   updatedArray = new string[] { "c", "d" };
            ConfigData updated2     = icm.UpdateConfig(new UpdateConfigOptions(arrayKey, updatedArray, ConfigScope.CurrentUser)
            {
                AppliesTo = "Az.KeyVault"
            });

            Assert.Equal(updatedArray, updated2.Value);
            Assert.Equal(updatedArray, cm.GetConfigValueInternal <string[]>(arrayKey, new InternalInvocationInfo()
            {
                ModuleName = "Az.KeyVault"
            }));
        }
        public void ConfigsShouldBeMergedWhenImport()
        {
            string key1    = "key1";
            var    config1 = new SimpleTypedConfig <bool>(key1, "", true);
            string key2    = "key2";
            var    config2 = new SimpleTypedConfig <bool>(key2, "", true);
            string key3    = "key3";
            var    config3 = new SimpleTypedConfig <bool>(key3, "", true);
            string key4    = "key4";
            var    config4 = new SimpleTypedConfig <bool>(key4, "", true);

            var dataStore     = new MockDataStore();
            var configManager = GetConfigManager(
                new InitSettings()
            {
                DataStore = dataStore
            },
                config1, config2, config3, config4);

            // update config3 and config4 to false
            configManager.UpdateConfig(key3, false, ConfigScope.CurrentUser);
            configManager.UpdateConfig(key4, false, ConfigScope.CurrentUser);
            Assert.True(configManager.GetConfigValue <bool>(key1));
            Assert.True(configManager.GetConfigValue <bool>(key2));
            Assert.False(configManager.GetConfigValue <bool>(key3));
            Assert.False(configManager.GetConfigValue <bool>(key4));

            // import a config file, setting config2 to false and config4 to true
            string path = Path.GetTempFileName();

            dataStore.WriteFile(path,
                                @"{
    ""Az"": {
        ""key2"": false,
        ""key4"": true
    }
}");
            new JsonConfigHelper(configManager.ConfigFilePath, dataStore).ImportConfigFile(path);
            configManager.BuildConfig();

            // config1 should be true => not imported, use default value
            Assert.True(configManager.GetConfigValue <bool>(key1));
            // config2 should be false => imported, should overwrite default value
            Assert.False(configManager.GetConfigValue <bool>(key2));
            // config3 should be false => not imported, use value in config
            Assert.False(configManager.GetConfigValue <bool>(key3));
            // config4 should be true => imported, should overwrite previous value in config
            Assert.True(configManager.GetConfigValue <bool>(key4));
        }
示例#9
0
        public void CanGetFromJson()
        {
            var        config1   = new SimpleTypedConfig <int>("Retry", "", -1);
            var        config2   = new SimpleTypedConfig <string[]>("Array", "", null);
            IDataStore dataStore = new MockDataStore();
            string     path      = Path.GetRandomFileName();

            dataStore.WriteFile(path,
                                @"{
    ""Az"": {
        ""Retry"": 100
    },
    ""Az.KeyVault"": {
        ""Array"": [""a"",""b""]
    },
    ""Get-AzKeyVault"": {
        ""Array"": [""k"",""v""]
    }
}");
            IConfigManager icm = GetConfigManager(
                new InitSettings()
            {
                DataStore = dataStore,
                Path      = path
            },
                config1, config2);
            ConfigManager cm = icm as ConfigManager;

            Assert.Equal(100, cm.GetConfigValue <int>("Retry"));
            Assert.Equal(new string[] { "a", "b" }, cm.GetConfigValueInternal <string[]>("Array", new InternalInvocationInfo()
            {
                ModuleName = "Az.KeyVault"
            }));
            Assert.Equal(new string[] { "k", "v" }, cm.GetConfigValueInternal <string[]>("Array", new InternalInvocationInfo()
            {
                ModuleName = "Az.KeyVault", CmdletName = "Get-AzKeyVault"
            }));
        }