public void CanExportConfig() { string key = "key1"; var config = new SimpleTypedConfig <bool>(key, "", true); string configPath = Path.GetTempFileName(); var dataStore = new MockDataStore(); var configManager = GetConfigManager( new InitSettings() { DataStore = dataStore, Path = configPath }, config); // update config to false configManager.UpdateConfig(key, false, ConfigScope.CurrentUser); // export config file string path = Path.GetTempFileName(); Assert.False(dataStore.FileExists(path)); new JsonConfigHelper(configPath, dataStore).ExportConfigFile(path); // config file should be exported and be correct Assert.True(dataStore.FileExists(path)); var json = JsonUtilities.DeserializeJson(dataStore.ReadFileAsText(path), true); Assert.True(json.ContainsKey(ConfigFilter.GlobalAppliesTo)); var jsonConfig = json.GetProperty(ConfigFilter.GlobalAppliesTo) as IDictionary <string, object>; Assert.NotNull(jsonConfig); Assert.False((bool)jsonConfig.GetProperty(key)); }
public void ExportImportE2E() { string key = "key1"; var config = new SimpleTypedConfig <bool>(key, "", true); var dataStore = new MockDataStore(); var configManager = GetConfigManager( new InitSettings() { DataStore = dataStore }, config); // update configManager.UpdateConfig(key, false, ConfigScope.CurrentUser); // exportconfig string path = Path.GetRandomFileName(); var helper = new JsonConfigHelper(configManager.ConfigFilePath, dataStore); helper.ExportConfigFile(path); // clear configManager.ClearConfig(key, ConfigScope.CurrentUser); Assert.True(configManager.GetConfigValue <bool>(key)); // import helper.ImportConfigFile(path); configManager.BuildConfig(); Assert.False(configManager.GetConfigValue <bool>(key)); }
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)); }
public void CanGetFromJson() { var config1 = new SimpleTypedConfig <int>("Retry", "", -1); var config2 = new SimpleTypedConfig <string[]>("Array", "", null); IConfigManager icm = GetConfigManagerWithInitState((dataStore, path) => { dataStore.WriteFile(path, @"{ ""Az"": { ""Retry"": 100 }, ""Az.KeyVault"": { ""Array"": [""a"",""b""] }, ""Get-AzKeyVault"": { ""Array"": [""k"",""v""] } }"); }, null, 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" })); }
public void CanFilterByAppliesTo() { const string key1 = "key"; var config1 = new SimpleTypedConfig <bool>(key1, "", true); const string key2 = "key2"; var config2 = new SimpleTypedConfig <bool>(key2, "", true); var icm = GetConfigManager(config1, config2); const string module = "Az.KeyVault"; icm.UpdateConfig(new UpdateConfigOptions(key1, false, ConfigScope.CurrentUser) { AppliesTo = module }); icm.UpdateConfig(new UpdateConfigOptions(key2, false, ConfigScope.CurrentUser) { AppliesTo = module }); var listResults = icm.ListConfigs(new ConfigFilter() { AppliesTo = module }); Assert.Equal(2, listResults.Count()); listResults = icm.ListConfigs(new ConfigFilter() { AppliesTo = ConfigFilter.GlobalAppliesTo }); Assert.Equal(2, listResults.Count()); }
public void ShouldNotThrowToClearConfigNeverSet() { string key1 = "key1"; var config1 = new SimpleTypedConfig <bool>(key1, "{help message}", false); string key2 = "key2"; var config2 = new SimpleTypedConfig <bool>(key2, "{help message}", false); IConfigManager icm = GetConfigManager(config1, config2); icm.ClearConfig(key1, ConfigScope.CurrentUser); icm.ClearConfig(key2, ConfigScope.Process); icm.ClearConfig(null, ConfigScope.CurrentUser); icm.ClearConfig(null, ConfigScope.Process); icm.ClearConfig(new ClearConfigOptions(null, ConfigScope.CurrentUser) { AppliesTo = null }); icm.ClearConfig(new ClearConfigOptions(null, ConfigScope.Process) { AppliesTo = null }); icm.ClearConfig(new ClearConfigOptions(null, ConfigScope.CurrentUser) { AppliesTo = "Az.Accounts" }); icm.ClearConfig(new ClearConfigOptions(null, ConfigScope.Process) { AppliesTo = "Az.Accounts" }); }
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 ShouldClearWhateverAppliesTo() { const string boolKey = "BoolKey"; var boolConfig = new SimpleTypedConfig <bool>(boolKey, "", false); var icm = GetConfigManager(boolConfig); icm.UpdateConfig(new UpdateConfigOptions(boolKey, true, ConfigScope.CurrentUser) { AppliesTo = "Az.Module" }); icm.UpdateConfig(new UpdateConfigOptions(boolKey, true, ConfigScope.CurrentUser) { AppliesTo = "Get-Cmdlet" }); icm.UpdateConfig(new UpdateConfigOptions(boolKey, true, ConfigScope.CurrentUser) { AppliesTo = "Az" }); icm.ClearConfig(boolKey, ConfigScope.CurrentUser); var results = icm.ListConfigs(new ConfigFilter() { Keys = new string[] { boolKey } }); Assert.Single(results); }
public void CanGetAndListRegisteredConfigs() { const string key1 = "EnableTelemetry"; var config1 = new SimpleTypedConfig <bool>( key1, "Enable telemetry", true); TestConfig config2 = new TestConfig(); IConfigManager configurationManager = GetConfigManager(config1, config2); var listResult = configurationManager.ListConfigs(); Assert.Equal(2, listResult.Count()); ConfigData configData = listResult.Where(x => x.Definition.Key == key1).Single(); Assert.Equal(true, configData.Value); Assert.True(configurationManager.GetConfigValue <bool>(key1)); ConfigData tempConfigResult = listResult.Where(x => x.Definition.Key == config2.Key).Single(); Assert.Equal(config2.DefaultValue, tempConfigResult.Value); Assert.Equal(config2.HelpMessage, tempConfigResult.Definition.HelpMessage); Assert.Equal(config2.DefaultValue, configurationManager.GetConfigValue <int>(config2.Key)); }
public void CanClearByScope() { const string boolKey = "BoolKey"; var boolConfig = new SimpleTypedConfig <bool>(boolKey, "", false); const string intKey = "intKey"; var intConfig = new SimpleTypedConfig <int>(intKey, "", 0); var icm = GetConfigManager(boolConfig, intConfig); icm.UpdateConfig(new UpdateConfigOptions(boolKey, true, ConfigScope.CurrentUser)); icm.UpdateConfig(new UpdateConfigOptions(intKey, 10, ConfigScope.CurrentUser)); icm.UpdateConfig(new UpdateConfigOptions(boolKey, true, ConfigScope.Process)); icm.UpdateConfig(new UpdateConfigOptions(intKey, 10, ConfigScope.Process)); icm.ClearConfig(new ClearConfigOptions(boolKey, ConfigScope.Process)); icm.ClearConfig(new ClearConfigOptions(intKey, ConfigScope.Process)); foreach (var configData in icm.ListConfigs()) { Assert.NotEqual(ConfigScope.Process, configData.Scope); } icm.ClearConfig(boolKey, ConfigScope.CurrentUser); icm.ClearConfig(intKey, ConfigScope.CurrentUser); foreach (var configData in icm.ListConfigs()) { Assert.NotEqual(ConfigScope.CurrentUser, configData.Scope); } }
public void AppliesToShouldDefaultToAz() { const string boolKey = "BoolKey"; var boolConfig = new SimpleTypedConfig <bool>(boolKey, "", false); var icm = GetConfigManager(boolConfig); const string appliesTo = "Az.A"; icm.UpdateConfig(new UpdateConfigOptions(boolKey, true, ConfigScope.CurrentUser) { AppliesTo = appliesTo }); icm.ClearConfig(boolKey, ConfigScope.CurrentUser); Assert.Single(icm.ListConfigs(new ConfigFilter() { Keys = new string[] { boolKey }, AppliesTo = appliesTo })); icm.ClearConfig(new ClearConfigOptions(boolKey, ConfigScope.CurrentUser) { AppliesTo = appliesTo }); Assert.Empty(icm.ListConfigs(new ConfigFilter() { Keys = new string[] { boolKey }, AppliesTo = appliesTo })); }
public void CanFilterByScope() { const string key1 = "key"; var config1 = new SimpleTypedConfig <bool>(key1, "", true); const string key2 = "key2"; var config2 = new SimpleTypedConfig <bool>(key2, "", true); var icm = GetConfigManager(config1, config2); icm.UpdateConfig(new UpdateConfigOptions(key1, false, ConfigScope.CurrentUser)); icm.UpdateConfig(new UpdateConfigOptions(key1, true, ConfigScope.Process)); var listResults = icm.ListConfigs(new ConfigFilter() { Scope = ConfigScope.Default }); Assert.Equal(2, listResults.Count()); listResults = icm.ListConfigs(new ConfigFilter() { Scope = ConfigScope.CurrentUser }); Assert.Single(listResults); listResults = icm.ListConfigs(new ConfigFilter() { Scope = ConfigScope.Process }); Assert.Single(listResults); listResults = icm.ListConfigs(new ConfigFilter() { Scope = ConfigScope.Environment }); Assert.Empty(listResults); }
public void CanGetScope() { const string key1 = "key"; var config1 = new SimpleTypedConfig <bool>(key1, "", true); var config2 = new TestConfig(); var icm = GetConfigManager(config1, config2); var listResults = icm.ListConfigs(); foreach (var config in listResults) { Assert.Equal(ConfigScope.Default, config.Scope); } var updated = icm.UpdateConfig(new UpdateConfigOptions(key1, false, ConfigScope.CurrentUser)); Assert.Equal(ConfigScope.CurrentUser, updated.Scope); updated = icm.UpdateConfig(new UpdateConfigOptions(key1, true, ConfigScope.Process)); Assert.Equal(ConfigScope.Process, updated.Scope); icm.ClearConfig(new ClearConfigOptions(key1, ConfigScope.Process)); updated = icm.ListConfigs(new ConfigFilter() { Keys = new string[] { key1 } }).Single(); Assert.Equal(ConfigScope.CurrentUser, updated.Scope); }
public void ShouldNotThrowWhenClearConfigNeverSet() { string key = "DisableSomething"; var config = new SimpleTypedConfig <bool>(key, "{help message}", false); IConfigManager icm = GetConfigManager(config); icm.ClearConfig(key, ConfigScope.CurrentUser); }
public void CanRegisterSameConfigTwice() { IConfigManager entry = GetConfigManager(); const string key = "CanRegisterTwice"; SimpleTypedConfig <int> config = new SimpleTypedConfig <int>(key, "", -1); entry.RegisterConfig(config); entry.RegisterConfig(config); }
public void CanValidateInput() { const string boolKey = "BoolKey"; var boolConfig = new SimpleTypedConfig <bool>(boolKey, "", false); var rangedIntConfig = new RangedConfig(); var icm = GetConfigManagerWithInitState(null, null, boolConfig, rangedIntConfig); Assert.Throws <AzPSArgumentException>(() => { icm.UpdateConfig(boolKey, 0, ConfigScope.CurrentUser); }); Assert.Throws <AzPSArgumentException>(() => { icm.UpdateConfig(rangedIntConfig.Key, true, ConfigScope.CurrentUser); }); Assert.Throws <AzPSArgumentException>(() => { icm.UpdateConfig(rangedIntConfig.Key, -1, ConfigScope.CurrentUser); }); }
public void CanGetFromEnvironmentVar() { const string key = "FromEnv"; const string envKey = "ENV_VAR_FOR_CONFIG"; var config = new SimpleTypedConfig <int>(key, "", -1, envKey); const int value = 20; var configurationManager = GetConfigManagerWithInitState(null, (envVar) => { envVar.Set(envKey, value.ToString()); }, config); Assert.Equal(value, configurationManager.GetConfigValue <int>(key)); }
public void ShouldThrowIfAppliesToIsWrong() { var key = "OnlyAppliesToAz"; var config = new SimpleTypedConfig <bool>(key, "", true, null, new AppliesTo[] { AppliesTo.Az }); var icm = GetConfigManager(config); Assert.Throws <AzPSArgumentException>(() => icm.UpdateConfig(new UpdateConfigOptions(key, true, ConfigScope.CurrentUser) { AppliesTo = "Az.Accounts" })); }
public void ThrowWhenOptionIsInvalid() { const string key1 = "key"; var config1 = new SimpleTypedConfig <bool>(key1, "", true); const string key2 = "key2"; var config2 = new SimpleTypedConfig <bool>(key2, "", true); var icm = GetConfigManager(config1, config2); Assert.Throws <AzPSArgumentNullException>(() => icm.UpdateConfig(null)); Assert.Throws <ArgumentNullException>(() => icm.UpdateConfig(new UpdateConfigOptions(null, null, ConfigScope.CurrentUser))); Assert.Throws <AzPSArgumentException>(() => icm.UpdateConfig(new UpdateConfigOptions(key1, "ThisShouldNotBeAString", ConfigScope.CurrentUser))); }
public void ProcessConfigHigherThanProcessEnv() { const string retryKey = "Retry"; const string envName = "ENV_FOR_RETRY"; var config = new SimpleTypedConfig <int>(retryKey, "", -1, envName); IConfigManager icm = GetConfigManagerWithInitState(null, envVar => { envVar.Set(envName, "10", System.EnvironmentVariableTarget.Process); }, config); icm.UpdateConfig(new UpdateConfigOptions(retryKey, 100, ConfigScope.Process)); Assert.Equal(100, icm.GetConfigValue <int>(retryKey)); }
public void CanGetDefaultValue() { IConfigManager entry = GetConfigManager(); const string key = "CanGetConfigValue"; SimpleTypedConfig <int> config = new SimpleTypedConfig <int>(key, "", -1); entry.RegisterConfig(config); entry.BuildConfig(); Assert.Equal(-1, entry.GetConfigValue <int>(key)); entry.UpdateConfig(new UpdateConfigOptions(key, 10, ConfigScope.Process)); Assert.Equal(10, entry.GetConfigValue <int>(key)); }
public void ShouldNotThrowWhenEnvVarIsWrong() { const string key = "FromEnv"; const string envKey = "ENV_VAR_FOR_CONFIG"; const int defaultValue = -1; var config = new SimpleTypedConfig <int>(key, "", defaultValue, envKey); const bool valueWithWrongType = true; var configurationManager = GetConfigManagerWithInitState(null, envVar => { envVar.Set(envKey, valueWithWrongType.ToString()); }, config); Assert.Equal(defaultValue, configurationManager.GetConfigValue <int>(key)); }
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)); }
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 EachUpdateShouldBeIndependent() { const string key1 = "key"; var config1 = new SimpleTypedConfig <bool>(key1, "", true); const string key2 = "key2"; var config2 = new SimpleTypedConfig <bool>(key2, "", true); var icm = GetConfigManager(config1, config2); icm.UpdateConfig(key1, false, ConfigScope.Process); Assert.False(icm.GetConfigValue <bool>(key1)); Assert.True(icm.GetConfigValue <bool>(key2)); icm.UpdateConfig(key2, false, ConfigScope.CurrentUser); Assert.False(icm.GetConfigValue <bool>(key1)); Assert.False(icm.GetConfigValue <bool>(key2)); }
public void CanFilterByKeyAndAppliesTo() { const string key = "key"; var config = new SimpleTypedConfig <bool>(key, "", true); var icm = GetConfigManager(config); const string module = "Az.KeyVault"; icm.UpdateConfig(new UpdateConfigOptions(key, false, ConfigScope.CurrentUser) { AppliesTo = module }); Assert.Single(icm.ListConfigs(new ConfigFilter() { Keys = new[] { key }, AppliesTo = module })); }
public void CanGetFromEnvironmentVar() { const string key = "FromEnv"; const string envKey = "ENV_VAR_FOR_CONFIG"; var config = new SimpleTypedConfig <int>(key, "", -1, envKey); const int value = 20; IEnvironmentVariableProvider env = new MockEnvironmentVariableProvider(); env.Set(envKey, value.ToString()); var configurationManager = GetConfigManager( new InitSettings() { EnvironmentVariableProvider = env }, config); Assert.Equal(value, configurationManager.GetConfigValue <int>(key)); }
public void ShouldNotThrowWhenEnvVarIsWrong() { const string key = "FromEnv"; const string envKey = "ENV_VAR_FOR_CONFIG"; const int defaultValue = -1; var config = new SimpleTypedConfig <int>(key, "", defaultValue, envKey); const bool valueWithWrongType = true; IEnvironmentVariableProvider env = new MockEnvironmentVariableProvider(); env.Set(envKey, valueWithWrongType.ToString()); var configurationManager = GetConfigManager( new InitSettings() { EnvironmentVariableProvider = env }, config); Assert.Equal(defaultValue, configurationManager.GetConfigValue <int>(key)); }
public void AppliesToShouldBeCaseInsensitive() { const string key = "key"; var config = new SimpleTypedConfig <int>(key, "", 0); var icm = GetConfigManager(config); icm.UpdateConfig(new UpdateConfigOptions(key, 1, ConfigScope.CurrentUser) { AppliesTo = "az.abc" }); Assert.Equal(1, icm.ListConfigs(new ConfigFilter() { Keys = new[] { key }, AppliesTo = "az.abc" }).Single().Value); Assert.Equal(1, icm.ListConfigs(new ConfigFilter() { Keys = new[] { key }, AppliesTo = "Az.Abc" }).Single().Value); }
public void CanListDefinitions() { const string key1 = "key"; var config1 = new SimpleTypedConfig <bool>(key1, "", true); const string key2 = "key2"; var config2 = new SimpleTypedConfig <bool>(key2, "", true); var config3 = new TestConfig(); var icm = GetConfigManager(config1, config2, config3); Assert.Equal(3, icm.ListConfigDefinitions().Count()); const string module = "Az.KeyVault"; icm.UpdateConfig(new UpdateConfigOptions(key1, false, ConfigScope.CurrentUser) { AppliesTo = module }); Assert.Equal(3, icm.ListConfigDefinitions().Count()); }