private IEnumerable <IWorkerProvider> TestReadWorkerProviderFromConfig(IEnumerable <TestLanguageWorkerConfig> configs, ILogger testLogger, string language = null) { try { foreach (var workerConfig in configs) { string workerPath = Path.Combine(rootPath, workerConfig.Language); Directory.CreateDirectory(workerPath); File.WriteAllText(Path.Combine(workerPath, ScriptConstants.WorkerConfigFileName), workerConfig.Json); } var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder() .AddInMemoryCollection(new Dictionary <string, string> { ["workers:config:path"] = rootPath }); var config = configBuilder.Build(); var scriptHostConfig = new ScriptHostConfiguration(); var scriptSettingsManager = new ScriptSettingsManager(config); return(GenericWorkerProvider.ReadWorkerProviderFromConfig(scriptHostConfig, testLogger, scriptSettingsManager, language: language)); } finally { if (Directory.Exists(rootPath)) { Directory.Delete(rootPath, true); } } }
public void LanguageWorker_HydratedWorkerPath_UnsupportedDefaultRuntimeVersion() { RpcWorkerDescription workerDescription = new RpcWorkerDescription() { Arguments = new List <string>(), DefaultExecutablePath = "python", SupportedRuntimeVersions = new List <string>() { "3.6", "3.7" }, DefaultWorkerPath = $"{RpcWorkerConstants.RuntimeVersionPlaceholder}/worker.py", WorkerDirectory = string.Empty, Extensions = new List <string>() { ".py" }, Language = "python", DefaultRuntimeVersion = "3.5" }; var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder() .AddInMemoryCollection(new Dictionary <string, string> { ["languageWorker"] = "test" }); var config = configBuilder.Build(); var scriptSettingsManager = new ScriptSettingsManager(config); var testLogger = new TestLogger("test"); var configFactory = new RpcWorkerConfigFactory(config, testLogger, _testSysRuntimeInfo, _testEnvironment, new TestMetricsLogger()); var ex = Assert.Throws <NotSupportedException>(() => configFactory.GetHydratedWorkerPath(workerDescription)); Assert.Equal(ex.Message, $"Version {workerDescription.DefaultRuntimeVersion} is not supported for language {workerDescription.Language}"); }
public void LanguageWorker_FormatWorkerPath_EnvironmentVersionSet( string defaultWorkerPath, string environmentRuntimeVersion, string sanitizeRuntimeVersionRegex, string expectedPath) { _testEnvironment.SetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeSettingName, "python"); _testEnvironment.SetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeVersionSettingName, environmentRuntimeVersion); RpcWorkerDescription workerDescription = new RpcWorkerDescription() { Arguments = new List <string>(), DefaultExecutablePath = "python", DefaultWorkerPath = defaultWorkerPath, DefaultRuntimeVersion = "3.6", SanitizeRuntimeVersionRegex = sanitizeRuntimeVersionRegex, SupportedArchitectures = new List <string>() { Architecture.X64.ToString(), Architecture.X86.ToString() }, SupportedRuntimeVersions = new List <string>() { "3.6", "3.7" }, SupportedOperatingSystems = new List <string>() { OSPlatform.Windows.ToString(), OSPlatform.OSX.ToString(), OSPlatform.Linux.ToString() }, WorkerDirectory = string.Empty, Extensions = new List <string>() { ".py" }, Language = "python" }; var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder() .AddInMemoryCollection(new Dictionary <string, string> { ["languageWorker"] = "test" }); var config = configBuilder.Build(); var scriptSettingsManager = new ScriptSettingsManager(config); var testLogger = new TestLogger("test"); workerDescription.FormatWorkerPathIfNeeded(_testSysRuntimeInfo, _testEnvironment, testLogger); // Override file exists to return true workerDescription.FileExists = path => { Assert.Equal(workerDescription.DefaultWorkerPath, path); return(true); }; Assert.Equal(expectedPath, workerDescription.DefaultWorkerPath); var expectedLogMessage = string.Format($"EnvironmentVariable FUNCTIONS_WORKER_RUNTIME_VERSION: {environmentRuntimeVersion}"); Assert.Collection(testLogger.GetLogMessages(), p => Assert.Equal(expectedLogMessage, p.FormattedMessage)); }
public void LanguageWorker_FormatWorkerPath_UnsupportedEnvironmentRuntimeVersion() { _testEnvironment.SetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeVersionSettingName, "3.4"); RpcWorkerDescription workerDescription = new RpcWorkerDescription() { Arguments = new List <string>(), DefaultExecutablePath = "python", SupportedRuntimeVersions = new List <string>() { "3.6", "3.7" }, DefaultWorkerPath = $"{RpcWorkerConstants.RuntimeVersionPlaceholder}/worker.py", WorkerDirectory = string.Empty, Extensions = new List <string>() { ".py" }, Language = "python", DefaultRuntimeVersion = "3.7" // Ignore this if environment is set }; var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder() .AddInMemoryCollection(new Dictionary <string, string> { ["languageWorker"] = "test" }); var config = configBuilder.Build(); var scriptSettingsManager = new ScriptSettingsManager(config); var testLogger = new TestLogger("test"); var ex = Assert.Throws <NotSupportedException>(() => workerDescription.FormatWorkerPathIfNeeded(_testSysRuntimeInfo, _testEnvironment, testLogger)); Assert.Equal(ex.Message, $"Version 3.4 is not supported for language {workerDescription.Language}"); }
public void DefaultWorkerConfigs_Overrides_DefaultWorkerRuntimeVersion_AppSetting() { var testEnvVariables = new Dictionary <string, string> { { "languageWorkers:python:defaultRuntimeVersion", "3.8" } }; var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder() .AddInMemoryCollection(testEnvVariables); var config = configBuilder.Build(); var scriptSettingsManager = new ScriptSettingsManager(config); var testLogger = new TestLogger("test"); using (var variables = new TestScopedSettings(scriptSettingsManager, testEnvVariables)) { var configFactory = new RpcWorkerConfigFactory(config, testLogger, _testSysRuntimeInfo, _testEnvironment, new TestMetricsLogger()); var workerConfigs = configFactory.GetConfigs(); var pythonWorkerConfig = workerConfigs.Where(w => w.Description.Language.Equals("python", StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); var powershellWorkerConfig = workerConfigs.Where(w => w.Description.Language.Equals("powershell", StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); Assert.Equal(4, workerConfigs.Count); Assert.NotNull(pythonWorkerConfig); Assert.NotNull(powershellWorkerConfig); Assert.Equal("3.8", pythonWorkerConfig.Description.DefaultRuntimeVersion); Assert.Equal("6", powershellWorkerConfig.Description.DefaultRuntimeVersion); } }
public void JavaPath_FromEnvVars() { var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder(); var config = configBuilder.Build(); var scriptSettingsManager = new ScriptSettingsManager(config); var testLogger = new TestLogger("test"); var configFactory = new RpcWorkerConfigFactory(config, testLogger, _testSysRuntimeInfo, _testEnvironment, new TestMetricsLogger()); var workerConfigs = configFactory.GetConfigs(); var javaPath = workerConfigs.Where(c => c.Description.Language.Equals("java", StringComparison.OrdinalIgnoreCase)).FirstOrDefault().Description.DefaultExecutablePath; Assert.DoesNotContain(@"%JAVA_HOME%", javaPath); Assert.Contains(@"/bin/java", javaPath); }
public void LanguageWorker_FormatWorkerPath_EnvironmentVersionNotSet( string defaultWorkerPath, string sanitizeRuntimeVersionRegex, string expectedPath) { // We fall back to the default version when this is not set // Environment.SetEnvironmentVariable(LanguageWorkerConstants.FunctionWorkerRuntimeVersionSettingName, "3.7"); RpcWorkerDescription workerDescription = new RpcWorkerDescription() { Arguments = new List <string>(), DefaultExecutablePath = "python", SupportedArchitectures = new List <string>() { Architecture.X64.ToString(), Architecture.X86.ToString() }, SupportedRuntimeVersions = new List <string>() { "3.6", "3.7" }, SupportedOperatingSystems = new List <string>() { OSPlatform.Windows.ToString(), OSPlatform.OSX.ToString(), OSPlatform.Linux.ToString() }, DefaultWorkerPath = defaultWorkerPath, WorkerDirectory = string.Empty, Extensions = new List <string>() { ".py" }, Language = "python", DefaultRuntimeVersion = "3.6", SanitizeRuntimeVersionRegex = sanitizeRuntimeVersionRegex }; var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder() .AddInMemoryCollection(new Dictionary <string, string> { ["languageWorker"] = "test" }); var config = configBuilder.Build(); var scriptSettingsManager = new ScriptSettingsManager(config); var testLogger = new TestLogger("test"); workerDescription.FormatWorkerPathIfNeeded(_testSysRuntimeInfo, _testEnvironment, testLogger); Assert.Equal(expectedPath, workerDescription.DefaultWorkerPath); Assert.Equal("3.6", workerDescription.DefaultRuntimeVersion); }
public void LanguageWorker_WorkersDir_NotSet() { var expectedWorkersDir = Path.Combine(Path.GetDirectoryName(new Uri(typeof(WorkerConfigFactory).Assembly.CodeBase).LocalPath), LanguageWorkerConstants.DefaultWorkersDirectoryName); var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder() .AddInMemoryCollection(new Dictionary <string, string> { ["languageWorker"] = "test" }); var config = configBuilder.Build(); var scriptSettingsManager = new ScriptSettingsManager(config); var testLogger = new TestLogger("test"); var configFactory = new WorkerConfigFactory(config, testLogger); Assert.Equal(expectedWorkersDir, configFactory.WorkersDirPath); }
public void LanguageWorker_HydratedWorkerPath_UnsupportedArchitecture(Architecture unsupportedArch) { RpcWorkerDescription workerDescription = new RpcWorkerDescription() { Arguments = new List <string>(), DefaultExecutablePath = "python", DefaultWorkerPath = "{architecture}/worker.py", WorkerDirectory = string.Empty, SupportedArchitectures = new List <string>() { Architecture.X64.ToString(), Architecture.X86.ToString() }, SupportedRuntimeVersions = new List <string>() { "3.6", "3.7" }, SupportedOperatingSystems = new List <string>() { OSPlatform.Windows.ToString(), OSPlatform.OSX.ToString(), OSPlatform.Linux.ToString() }, Extensions = new List <string>() { ".py" }, Language = "python", DefaultRuntimeVersion = "3.7" }; var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder() .AddInMemoryCollection(new Dictionary <string, string> { ["languageWorker"] = "test" }); var config = configBuilder.Build(); var scriptSettingsManager = new ScriptSettingsManager(config); var testLogger = new TestLogger("test"); Mock <ISystemRuntimeInformation> mockRuntimeInfo = new Mock <ISystemRuntimeInformation>(); mockRuntimeInfo.Setup(r => r.GetOSArchitecture()).Returns(unsupportedArch); mockRuntimeInfo.Setup(r => r.GetOSPlatform()).Returns(OSPlatform.Linux); var configFactory = new RpcWorkerConfigFactory(config, testLogger, mockRuntimeInfo.Object, _testEnvironment, new TestMetricsLogger()); var ex = Assert.Throws <PlatformNotSupportedException>(() => configFactory.GetHydratedWorkerPath(workerDescription)); Assert.Equal(ex.Message, $"Architecture {unsupportedArch.ToString()} is not supported for language {workerDescription.Language}"); }
private static IConfigurationRoot TestConfigBuilder(string workerPathSection, Dictionary <string, string> keyValuePairs = null) { var configBuilderData = new Dictionary <string, string> { [workerPathSection] = rootPath }; if (keyValuePairs != null) { configBuilderData.AddRange(keyValuePairs); } var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder() .AddInMemoryCollection(configBuilderData); var config = configBuilder.Build(); return(config); }
public void LanguageWorker_HydratedWorkerPath_EnvironmentVersionNotSet(string defaultWorkerPath, string expectedPath) { // We fall back to the default version when this is not set // Environment.SetEnvironmentVariable(LanguageWorkerConstants.FunctionWorkerRuntimeVersionSettingName, "3.7"); RpcWorkerDescription workerDescription = new RpcWorkerDescription() { Arguments = new List <string>(), DefaultExecutablePath = "python", SupportedArchitectures = new List <string>() { Architecture.X64.ToString(), Architecture.X86.ToString() }, SupportedRuntimeVersions = new List <string>() { "3.6", "3.7" }, SupportedOperatingSystems = new List <string>() { OSPlatform.Windows.ToString(), OSPlatform.OSX.ToString(), OSPlatform.Linux.ToString() }, DefaultWorkerPath = defaultWorkerPath, WorkerDirectory = string.Empty, Extensions = new List <string>() { ".py" }, Language = "python", DefaultRuntimeVersion = "3.6" }; var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder() .AddInMemoryCollection(new Dictionary <string, string> { ["languageWorker"] = "test" }); var config = configBuilder.Build(); var scriptSettingsManager = new ScriptSettingsManager(config); var testLogger = new TestLogger("test"); var configFactory = new RpcWorkerConfigFactory(config, testLogger, _testSysRuntimeInfo, _testEnvironment, new TestMetricsLogger()); Assert.Equal(expectedPath, configFactory.GetHydratedWorkerPath(workerDescription)); Assert.Collection(testLogger.GetLogMessages(), p => Assert.Equal("EnvironmentVariable FUNCTIONS_WORKER_RUNTIME_VERSION: 3.6", p.FormattedMessage)); }
public void DefaultWorkerConfigs_Overrides_VersionAppSetting() { var testEnvironment = new TestEnvironment(); testEnvironment.SetEnvironmentVariable("FUNCTIONS_WORKER_RUNTIME_VERSION", "7.0"); testEnvironment.SetEnvironmentVariable("FUNCTIONS_WORKER_RUNTIME", "powerShell"); var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder(); var config = configBuilder.Build(); var scriptSettingsManager = new ScriptSettingsManager(config); var testLogger = new TestLogger("test"); var configFactory = new RpcWorkerConfigFactory(config, testLogger, _testSysRuntimeInfo, testEnvironment, new TestMetricsLogger()); var workerConfigs = configFactory.GetConfigs(); var powershellWorkerConfig = workerConfigs.Where(w => w.Description.Language.Equals("powershell", StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); Assert.Equal(1, workerConfigs.Count); Assert.NotNull(powershellWorkerConfig); Assert.Equal("7", powershellWorkerConfig.Description.DefaultRuntimeVersion); }
public void LanguageWorker_HydratedWorkerPath_EnvironmentVersionSet(string defaultWorkerPath, string expectedPath) { _testEnvironment.SetEnvironmentVariable(LanguageWorkerConstants.FunctionWorkerRuntimeVersionSettingName, "3.7"); RpcWorkerDescription workerDescription = new RpcWorkerDescription() { Arguments = new List <string>(), DefaultExecutablePath = "python", DefaultWorkerPath = defaultWorkerPath, DefaultRuntimeVersion = "3.6", SupportedArchitectures = new List <string>() { Architecture.X64.ToString(), Architecture.X86.ToString() }, SupportedRuntimeVersions = new List <string>() { "3.6", "3.7" }, SupportedOperatingSystems = new List <string>() { OSPlatform.Windows.ToString(), OSPlatform.OSX.ToString(), OSPlatform.Linux.ToString() }, WorkerDirectory = string.Empty, Extensions = new List <string>() { ".py" }, Language = "python" }; var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder() .AddInMemoryCollection(new Dictionary <string, string> { ["languageWorker"] = "test" }); var config = configBuilder.Build(); var scriptSettingsManager = new ScriptSettingsManager(config); var testLogger = new TestLogger("test"); var configFactory = new WorkerConfigFactory(config, testLogger, _testSysRuntimeInfo, _testEnvironment); Assert.Equal(expectedPath, configFactory.GetHydratedWorkerPath(workerDescription)); }
public SystemLoggerTests() { _subscriptionId = "e3235165-1600-4819-85f0-2ab362e909e4"; _hostInstanceId = Guid.NewGuid().ToString(); _websiteName = "functionstest"; _mockEventGenerator = new Mock <IEventGenerator>(MockBehavior.Strict); var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder() .AddInMemoryCollection(new Dictionary <string, string> { { EnvironmentSettingNames.AzureWebsiteOwnerName, $"{_subscriptionId}+westuswebspace" }, { EnvironmentSettingNames.AzureWebsiteName, _websiteName }, }); var config = configBuilder.Build(); _settingsManager = new ScriptSettingsManager(config); _category = LogCategories.CreateFunctionCategory(_functionName); _logger = new SystemLogger(_hostInstanceId, _category, _mockEventGenerator.Object, _settingsManager); }
public void JavaPath_JavaHome_Set() { var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder() .AddInMemoryCollection(new Dictionary <string, string> { ["languageWorker"] = "test" }); var config = configBuilder.Build(); var scriptSettingsManager = new ScriptSettingsManager(config); var testLogger = new TestLogger("test"); var configFactory = new WorkerConfigFactory(config, testLogger); var testEnvVariables = new Dictionary <string, string> { { "JAVA_HOME", @"D:\Program Files\Java\jdk1.7.0_51" } }; using (var variables = new TestScopedSettings(scriptSettingsManager, testEnvVariables)) { var javaPath = configFactory.GetExecutablePathForJava("java"); Assert.Equal(@"D:\Program Files\Java\jdk1.7.0_51\bin\java", javaPath); } }
public void JavaPath_JavaHome_NotSet() { var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder() .AddInMemoryCollection(new Dictionary <string, string> { ["languageWorker"] = "test" }); var config = configBuilder.Build(); var scriptSettingsManager = new ScriptSettingsManager(config); var testLogger = new TestLogger("test"); var configFactory = new RpcWorkerConfigFactory(config, testLogger, _testSysRuntimeInfo, _testEnvironment, new TestMetricsLogger()); var testEnvVariables = new Dictionary <string, string> { { "JAVA_HOME", string.Empty } }; using (var variables = new TestScopedSettings(scriptSettingsManager, testEnvVariables)) { var javaPath = configFactory.GetExecutablePathForJava("java"); Assert.Equal("java", javaPath); } }
public void AddArgumentsFromAppSettings_JavaOpts(string expectedArgument, string javaOpts) { WorkerDescription workerDescription = new WorkerDescription() { Arguments = new List <string>() { "-jar" }, DefaultExecutablePath = "java", DefaultWorkerPath = "javaworker.jar", Extensions = new List <string>() { ".jar" }, Language = "java" }; var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder() .AddInMemoryCollection(new Dictionary <string, string> { ["languageWorker"] = "test", ["languageWorkers:java:arguments"] = "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005" }); var config = configBuilder.Build(); var scriptSettingsManager = new ScriptSettingsManager(config); var testLogger = new TestLogger("test"); var configFactory = new WorkerConfigFactory(config, testLogger); var languageSection = config.GetSection("languageWorkers:java"); var testEnvVariables = new Dictionary <string, string> { { "JAVA_OPTS", javaOpts } }; using (var variables = new TestScopedSettings(scriptSettingsManager, testEnvVariables)) { WorkerConfigFactory.AddArgumentsFromAppSettings(workerDescription, languageSection); Assert.Equal(2, workerDescription.Arguments.Count); Assert.Equal(expectedArgument, workerDescription.Arguments[1]); } }
public void LanguageWorker_FormatWorkerPath_UnsupportedOS() { OSPlatform bogusOS = OSPlatform.Create("BogusOS"); RpcWorkerDescription workerDescription = new RpcWorkerDescription() { Arguments = new List <string>(), DefaultExecutablePath = "python", DefaultWorkerPath = "{os}/worker.py", SupportedOperatingSystems = new List <string>() { OSPlatform.Windows.ToString(), OSPlatform.OSX.ToString(), OSPlatform.Linux.ToString() }, WorkerDirectory = string.Empty, Extensions = new List <string>() { ".py" }, Language = "python", DefaultRuntimeVersion = "3.7" }; var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder() .AddInMemoryCollection(new Dictionary <string, string> { ["languageWorker"] = "test" }); var config = configBuilder.Build(); var scriptSettingsManager = new ScriptSettingsManager(config); var testLogger = new TestLogger("test"); Mock <ISystemRuntimeInformation> mockRuntimeInfo = new Mock <ISystemRuntimeInformation>(); mockRuntimeInfo.Setup(r => r.GetOSArchitecture()).Returns(Architecture.X64); mockRuntimeInfo.Setup(r => r.GetOSPlatform()).Returns(bogusOS); var ex = Assert.Throws <PlatformNotSupportedException>(() => workerDescription.FormatWorkerPathIfNeeded(mockRuntimeInfo.Object, _testEnvironment, testLogger)); Assert.Equal(ex.Message, $"OS BogusOS is not supported for language {workerDescription.Language}"); }
public void JavaPath_AppServiceEnv() { var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder() .AddInMemoryCollection(new Dictionary <string, string> { ["languageWorker"] = "test" }); var config = configBuilder.Build(); var scriptSettingsManager = new ScriptSettingsManager(config); var testLogger = new TestLogger("test"); var configFactory = new WorkerConfigFactory(config, testLogger); var testEnvVariables = new Dictionary <string, string> { { EnvironmentSettingNames.AzureWebsiteInstanceId, "123" }, { "JAVA_HOME", @"D:\Program Files\Java\jdk1.7.0_51" } }; using (var variables = new TestScopedSettings(scriptSettingsManager, testEnvVariables)) { var javaPath = configFactory.GetExecutablePathForJava("../../zulu8.23.0.3-jdk8.0.144-win_x64/bin/java"); Assert.Equal(@"D:\Program Files\Java\zulu8.23.0.3-jdk8.0.144-win_x64\bin\java", javaPath); } }