Restores automatic log download on test failure

This commit is contained in:
benbierens 2023-04-13 11:53:54 +02:00
parent 7eab4840ef
commit 31e034ab67
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
9 changed files with 71 additions and 49 deletions

View File

@ -7,15 +7,6 @@ namespace CodexDistTestCore
void Log(Stream log);
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class DontDownloadLogsAndMetricsOnFailureAttribute : PropertyAttribute
{
public DontDownloadLogsAndMetricsOnFailureAttribute()
: base(Timing.UseLongTimeoutsKey)
{
}
}
public class PodLogDownloader
{
public const string DontDownloadLogsOnFailureKey = "DontDownloadLogsOnFailure";

View File

@ -1,7 +1,7 @@
using Logging;
using NUnit.Framework;
namespace DistTestCore.CodexLogs
namespace DistTestCore.CodexLogsAndMetrics
{
public interface ICodexNodeLog
{

View File

@ -0,0 +1,15 @@
using NUnit.Framework;
namespace DistTestCore.CodexLogsAndMetrics
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class DontDownloadLogsAndMetricsOnFailureAttribute : PropertyAttribute
{
public const string DontDownloadKey = "DontDownloadLogsAndMetrics";
public DontDownloadLogsAndMetricsOnFailureAttribute()
: base(DontDownloadKey)
{
}
}
}

View File

@ -1,7 +1,7 @@
using KubernetesWorkflow;
using Logging;
namespace DistTestCore.CodexLogs
namespace DistTestCore.CodexLogsAndMetrics
{
public class LogDownloadHandler : ILogHandler
{

View File

@ -32,15 +32,14 @@ namespace DistTestCore
public ICodexSetup BringOffline()
{
var result = Setup;
var containers = Containers;
lifecycle.CodexStarter.BringOffline(this);
var result = Setup;
// Clear everything. Prevent accidental use.
Setup = null!;
Containers = null!;
Nodes = Array.Empty<OnlineCodexNode>();
Containers = null!;
lifecycle.CodexStarter.BringOffline(containers);
return result;
}

View File

@ -1,7 +1,5 @@
using DistTestCore.Codex;
using DistTestCore.CodexLogs;
using KubernetesWorkflow;
using Nethereum.Merkle.Patricia;
namespace DistTestCore
{
@ -16,27 +14,40 @@ namespace DistTestCore
this.lifecycle = lifecycle;
}
public List<CodexNodeGroup> RunningGroups { get; } = new List<CodexNodeGroup>();
public ICodexNodeGroup BringOnline(CodexSetup codexSetup)
{
Log($"Starting {codexSetup.Describe()}...");
var workflow = CreateWorkflow();
var startupConfig = new StartupConfig();
startupConfig.Add(codexSetup);
var runningContainers = workflow.Start(codexSetup.NumberOfNodes, codexSetup.Location, new CodexContainerRecipe(), startupConfig);
return new CodexNodeGroup(lifecycle, codexSetup, runningContainers);
var group = new CodexNodeGroup(lifecycle, codexSetup, runningContainers);
RunningGroups.Add(group);
Log($"Started at '{group.Containers.RunningPod.Ip}'");
return group;
}
public void BringOffline(RunningContainers runningContainers)
public void BringOffline(CodexNodeGroup group)
{
Log($"Stopping {group.Describe()}...");
var workflow = CreateWorkflow();
workflow.Stop(runningContainers);
workflow.Stop(group.Containers);
RunningGroups.Remove(group);
Log("Stopped.");
}
public void DeleteAllResources()
{
var workflow = CreateWorkflow();
workflow.DeleteAllResources();
RunningGroups.Clear();
}
public void DownloadLog(RunningContainer container, ILogHandler logHandler)
@ -49,5 +60,10 @@ namespace DistTestCore
{
return workflowCreator.CreateWorkflow();
}
private void Log(string msg)
{
lifecycle.Log.Log(msg);
}
}
}

View File

@ -1,4 +1,5 @@
using NUnit.Framework;
using DistTestCore.CodexLogsAndMetrics;
using NUnit.Framework;
namespace DistTestCore
{
@ -68,20 +69,20 @@ namespace DistTestCore
private void IncludeLogsAndMetricsOnTestFailure()
{
//var result = TestContext.CurrentContext.Result;
//if (result.Outcome.Status == NUnit.Framework.Interfaces.TestStatus.Failed)
//{
// if (IsDownloadingLogsAndMetricsEnabled())
// {
// log.Log("Downloading all CodexNode logs and metrics because of test failure...");
// k8sManager.ForEachOnlineGroup(DownloadLogs);
// k8sManager.DownloadAllMetrics();
// }
// else
// {
// log.Log("Skipping download of all CodexNode logs and metrics due to [DontDownloadLogsAndMetricsOnFailure] attribute.");
// }
//}
var result = TestContext.CurrentContext.Result;
if (result.Outcome.Status == NUnit.Framework.Interfaces.TestStatus.Failed)
{
if (IsDownloadingLogsAndMetricsEnabled())
{
Log("Downloading all CodexNode logs and metrics because of test failure...");
DownloadAllLogs();
//k8sManager.DownloadAllMetrics();
}
else
{
Log("Skipping download of all CodexNode logs and metrics due to [DontDownloadLogsAndMetricsOnFailure] attribute.");
}
}
}
private void Log(string msg)
@ -99,21 +100,20 @@ namespace DistTestCore
lifecycle = new TestLifecycle(new Configuration());
}
private void DownloadLogs(CodexNodeGroup group)
private void DownloadAllLogs()
{
//foreach (var node in group)
//{
// var downloader = new PodLogDownloader(log, k8sManager);
// var n = (OnlineCodexNode)node;
// downloader.DownloadLog(n);
//}
var allNodes = lifecycle.CodexStarter.RunningGroups.SelectMany(g => g.Nodes);
foreach (var node in allNodes)
{
lifecycle.DownloadLog(node);
}
}
//private bool IsDownloadingLogsAndMetricsEnabled()
//{
// var testProperties = TestContext.CurrentContext.Test.Properties;
// return !testProperties.ContainsKey(PodLogDownloader.DontDownloadLogsOnFailureKey);
//}
private bool IsDownloadingLogsAndMetricsEnabled()
{
var testProperties = TestContext.CurrentContext.Test.Properties;
return !testProperties.ContainsKey(DontDownloadLogsAndMetricsOnFailureAttribute.DontDownloadKey);
}
}
public static class GlobalTestFailure

View File

@ -1,5 +1,5 @@
using DistTestCore.Codex;
using DistTestCore.CodexLogs;
using DistTestCore.CodexLogsAndMetrics;
using NUnit.Framework;
namespace DistTestCore

View File

@ -1,4 +1,4 @@
using DistTestCore.CodexLogs;
using DistTestCore.CodexLogsAndMetrics;
using Logging;
namespace DistTestCore
@ -28,6 +28,7 @@ namespace DistTestCore
var description = node.Describe();
var handler = new LogDownloadHandler(description, subFile);
Log.Log($"Downloading logs for {description} to file {subFile.FilenameWithoutPath}");
CodexStarter.DownloadLog(node.CodexAccess.Container, handler);
return new CodexNodeLog(subFile);