public static async Task CollectAsync(DateTime testStartTime, CancellationToken token) { string prefix = $"{Context.Current.DeviceId}-{TestContext.CurrentContext.Test.NormalizedName()}"; IEnumerable <string> paths = await EdgeLogs.CollectAsync(testStartTime, prefix, token); foreach (string path in paths) { TestContext.AddTestAttachment(path); } }
// Make an effort to collect logs, but swallow any exceptions to prevent tests/fixtures // from failing if this function fails. public static async Task CollectAsync(DateTime testStartTime, CancellationToken token) { try { string prefix = $"{Context.Current.DeviceId}-{TestContext.CurrentContext.Test.NormalizedName()}"; IEnumerable <string> paths = await EdgeLogs.CollectAsync(testStartTime, prefix, token); foreach (string path in paths) { TestContext.AddTestAttachment(path); } } catch (Exception e) { Log.Warning("Log collection failed for context " + $"'{TestContext.CurrentContext.Test.Name}' with exception:\n{e.ToString()}"); } }
public async Task TeardownAsync() { await Profiler.Run( async() => { this.cts.Dispose(); if (TestContext.CurrentContext.Result.Outcome != ResultState.Ignored) { using (var cts = new CancellationTokenSource(Context.Current.TeardownTimeout)) { string prefix = $"{Context.Current.DeviceId}-{TestContext.CurrentContext.Test.NormalizedName()}"; IEnumerable <string> paths = await EdgeLogs.CollectAsync(this.testStartTime, prefix, cts.Token); foreach (string path in paths) { TestContext.AddTestAttachment(path); } } } }, "Completed test teardown"); }
public async Task Setup() { await Profiler.Run( async() => { // Set up logging LogEventLevel consoleLevel = Context.Current.Verbose ? LogEventLevel.Verbose : LogEventLevel.Information; var loggerConfig = new LoggerConfiguration() .MinimumLevel.Verbose() .WriteTo.NUnit(consoleLevel); Context.Current.LogFile.ForEach(f => loggerConfig.WriteTo.File(f)); Log.Logger = loggerConfig.CreateLogger(); // Install IoT Edge using (var cts = new CancellationTokenSource(Context.Current.SetupTimeout)) { // NUnit's [Timeout] attribute isn't supported in .NET Standard // and even if it were, it doesn't run the teardown method when // a test times out. We need teardown to run, to remove the // device registration from IoT Hub and stop the daemon. So // we have our own timeout mechanism. DateTime startTime = DateTime.Now; CancellationToken token = cts.Token; Assert.IsNull(this.device); this.device = await EdgeDevice.GetOrCreateIdentityAsync( Context.Current.DeviceId, this.iotHub, token); await this.daemon.UninstallAsync(token); await this.daemon.InstallAsync( this.device.ConnectionString, Context.Current.PackagePath, Context.Current.Proxy, token); try { await this.daemon.WaitForStatusAsync(EdgeDaemonStatus.Running, token); var agent = new EdgeAgent(this.device.Id, this.iotHub); await agent.WaitForStatusAsync(EdgeModuleStatus.Running, token); await agent.PingAsync(token); } // ReSharper disable once RedundantCatchClause catch { throw; } finally { string prefix = $"{Context.Current.DeviceId}-{TestContext.CurrentContext.Test.NormalizedName()}"; IEnumerable <string> paths = await EdgeLogs.CollectAsync(startTime, prefix, token); foreach (string path in paths) { TestContext.AddTestAttachment(path); } } } }, "Completed end-to-end test setup"); }