From c71ef7285ab8a7669d86157d3f7882b19af821f2 Mon Sep 17 00:00:00 2001 From: Philip Lin Date: Mon, 12 Nov 2018 16:37:48 -0800 Subject: [PATCH] Fix for Windows E2E tests (#531) explicitly add IOTEDGE_HOST environment variable in current session; in order to avoid hyper error when running "iotedge list" in e2e tests; and handle runtime log level in iotedgedWindows.cs. --- .../DirectMethodSender/src/Program.cs | 1 + smoke/IotEdgeQuickstart/details/Details.cs | 6 ++- .../details/IotedgedWindows.cs | 46 ++++++++++++++++++- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/edge-modules/DirectMethodSender/src/Program.cs b/edge-modules/DirectMethodSender/src/Program.cs index 3617294e92e..8a940aeb8bb 100644 --- a/edge-modules/DirectMethodSender/src/Program.cs +++ b/edge-modules/DirectMethodSender/src/Program.cs @@ -42,6 +42,7 @@ static async Task MainAsync() (CancellationTokenSource cts, ManualResetEventSlim completed, Option handler) = ShutdownHandler.Init(TimeSpan.FromSeconds(5), null); + Console.WriteLine($"Target device Id = [{targetDeviceId}], Target module Id = [{targetModuleId}]"); await CallDirectMethod(moduleClient, dmDelay, targetDeviceId, targetModuleId, cts).ConfigureAwait(false); await moduleClient.CloseAsync(); completed.Set(); diff --git a/smoke/IotEdgeQuickstart/details/Details.cs b/smoke/IotEdgeQuickstart/details/Details.cs index c7b8126609f..74d29ceb808 100644 --- a/smoke/IotEdgeQuickstart/details/Details.cs +++ b/smoke/IotEdgeQuickstart/details/Details.cs @@ -344,7 +344,11 @@ string BuildImageName(string name) string edgeHubImage = this.EdgeHubImage(); string tempSensorImage = this.TempSensorImage(); string deployJson = this.DeploymentFileName.Match( - f => JObject.Parse(File.ReadAllText(f)).ToString(), + f => + { + Console.WriteLine($"Deployment file used: {f}"); + return JObject.Parse(File.ReadAllText(f)).ToString(); + }, () => { string deployJsonRegistry = this.credentials.Match( c => diff --git a/smoke/IotEdgeQuickstart/details/IotedgedWindows.cs b/smoke/IotEdgeQuickstart/details/IotedgedWindows.cs index 44ec65d39df..f69249561b0 100644 --- a/smoke/IotEdgeQuickstart/details/IotedgedWindows.cs +++ b/smoke/IotEdgeQuickstart/details/IotedgedWindows.cs @@ -5,12 +5,15 @@ namespace IotEdgeQuickstart.Details using System.IO; using System.Linq; using System.ServiceProcess; + using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using Microsoft.Azure.Devices.Edge.Util; class IotedgedWindows : IBootstrapper { + const string ConfigYamlFile = @"C:\ProgramData\iotedge\config.yaml"; + readonly string archivePath; readonly Option credentials; readonly Option proxy; @@ -130,12 +133,50 @@ await Process.RunAsync("powershell", } // note: ignore hostname for now - + Console.WriteLine($"Run command to configure: {args}"); string[] result = await Process.RunAsync("powershell", args, cts.Token); WriteToConsole("Output from Configure iotedge windows service", result); + + UpdateConfigYamlFile(runtimeLogLevel); + + // Explicitly set IOTEDGE_HOST environment variable to current process + SetEnvironmentVariable(); + } + } + + static void UpdateConfigYamlFile(LogLevel runtimeLogLevel) + { + string config = File.ReadAllText(ConfigYamlFile); + var doc = new YamlDocument(config); + doc.ReplaceOrAdd("agent.env.RuntimeLogLevel", runtimeLogLevel.ToString()); + + FileAttributes attr = 0; + attr = File.GetAttributes(ConfigYamlFile); + File.SetAttributes(ConfigYamlFile, attr & ~FileAttributes.ReadOnly); + + File.WriteAllText(ConfigYamlFile, doc.ToString()); + + if (attr != 0) + { + File.SetAttributes(ConfigYamlFile, attr); } } + static void SetEnvironmentVariable() + { + string config = File.ReadAllText(ConfigYamlFile); + var managementUriRegex = new Regex(@"connect:\s*management_uri:\s*""(.*)"""); + Match result = managementUriRegex.Match(config); + + if (result.Groups.Count != 2) + { + throw new Exception("can't find management Uri in config file."); + } + + Console.WriteLine($"Explicitly set environment variable [IOTEDGE_HOST={result.Groups[1].Value}]"); + Environment.SetEnvironmentVariable("IOTEDGE_HOST", result.Groups[1].Value); + } + public Task Start() { Console.WriteLine("Starting up iotedge service on Windows"); @@ -163,7 +204,10 @@ public Task Start() throw new Exception($"Error starting iotedged: {e}"); } + // Add delay to ensure iotedge service is completely started up. + Task.Delay(new TimeSpan(0, 0, 0, 5)); Console.WriteLine("iotedge service started on Windows"); + return Task.CompletedTask; }