From 506c60b079fc6ced002efa60900a86c88facbf21 Mon Sep 17 00:00:00 2001 From: "Mendes, Jose (KT)" Date: Thu, 21 Nov 2024 19:49:41 +0000 Subject: [PATCH 1/2] Add env var check for worker runtime in StartHostAction --- .../Actions/HostActions/StartHostAction.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Azure.Functions.Cli/Actions/HostActions/StartHostAction.cs b/src/Azure.Functions.Cli/Actions/HostActions/StartHostAction.cs index 559d66be3..5dd6aa469 100644 --- a/src/Azure.Functions.Cli/Actions/HostActions/StartHostAction.cs +++ b/src/Azure.Functions.Cli/Actions/HostActions/StartHostAction.cs @@ -737,7 +737,13 @@ private void EnsureWorkerRuntimeIsSet() return; } - if (GlobalCoreToolsSettings.CurrentWorkerRuntimeOrNone == WorkerRuntime.None) + var environmentRuntimeSettingValue = Environment.GetEnvironmentVariable(Constants.FunctionsWorkerRuntime); + if (environmentRuntimeSettingValue is not null) + { + GlobalCoreToolsSettings.CurrentWorkerRuntime = WorkerRuntimeLanguageHelper.NormalizeWorkerRuntime(environmentRuntimeSettingValue); + } + + else if (GlobalCoreToolsSettings.CurrentWorkerRuntimeOrNone == WorkerRuntime.None) { SelectionMenuHelper.DisplaySelectionWizardPrompt("worker runtime"); IDictionary workerRuntimeToDisplayString = WorkerRuntimeLanguageHelper.GetWorkerToDisplayStrings(); From 90b709c539c854caa2738fc6050b9ae6f36e88e6 Mon Sep 17 00:00:00 2001 From: Zemfm Date: Fri, 22 Nov 2024 01:05:14 +0000 Subject: [PATCH 2/2] Add unit test --- .../E2E/StartTests.cs | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/test/Azure.Functions.Cli.Tests/E2E/StartTests.cs b/test/Azure.Functions.Cli.Tests/E2E/StartTests.cs index 429eb1703..e17be1d3f 100644 --- a/test/Azure.Functions.Cli.Tests/E2E/StartTests.cs +++ b/test/Azure.Functions.Cli.Tests/E2E/StartTests.cs @@ -1115,6 +1115,66 @@ await CliTester.Run(new RunConfiguration[] } + [Fact] + public async Task Start_MissingLocalSettingsJson_SetWithEnvVar_SuccessfulFunctionExecution() + { + try + { + Environment.SetEnvironmentVariable("FUNCTIONS_WORKER_RUNTIME", "dotnet-isolated"); + + await CliTester.Run(new RunConfiguration[] + { + new RunConfiguration + { + Commands = new[] + { + $"init . --worker-runtime dotnet-isolated", + $"new --template Httptrigger --name HttpTriggerFunc", + }, + CommandTimeout = TimeSpan.FromSeconds(300), + }, + new RunConfiguration + { + PreTest = (workingDir) => + { + var LocalSettingsJson = Path.Combine(workingDir, "local.settings.json"); + File.Delete(LocalSettingsJson); + }, + Commands = new[] + { + $"start --port {_funcHostPort}", + }, + ExpectExit = false, + OutputContains = new[] + { + $"local.settings.json", + "Functions:", + $"HttpTriggerFunc: [GET,POST] http://localhost:{_funcHostPort}/api/HttpTriggerFunc" + }, + OutputDoesntContain = new string[] + { + "Initializing function HTTP routes" + }, + Test = async (_, p,_) => + { + using (var client = new HttpClient() { BaseAddress = new Uri($"http://localhost:{_funcHostPort}/") }) + { + (await WaitUntilReady(client)).Should().BeTrue(because: _serverNotReady); + var response = await client.GetAsync("/api/HttpTriggerFunc?name=Test"); + response.StatusCode.Should().Be(HttpStatusCode.OK); + await Task.Delay(TimeSpan.FromSeconds(2)); + p.Kill(); + } + } + } + }, _output); + } + finally + { + Environment.SetEnvironmentVariable("FUNCTIONS_WORKER_RUNTIME", null); + } + } + private async Task WaitUntilReady(HttpClient client) { for (var limit = 0; limit < 10; limit++)