159 lines
5.2 KiB
C#
Raw Normal View History

2023-09-19 16:22:07 +02:00
using CodexContractsPlugin;
using CodexNetDeployer;
2023-09-19 16:22:07 +02:00
using CodexPlugin;
2023-11-12 10:36:48 +01:00
using CodexTests.Helpers;
2023-09-20 13:56:01 +02:00
using Core;
2023-09-13 14:24:43 +02:00
using DistTestCore;
using DistTestCore.Helpers;
using DistTestCore.Logs;
using MetricsPlugin;
using Newtonsoft.Json;
2023-09-19 16:22:07 +02:00
using NUnit.Framework.Constraints;
2023-09-13 14:24:43 +02:00
namespace CodexTests
2023-09-13 14:24:43 +02:00
{
public class CodexDistTest : DistTest
{
2023-12-06 10:50:02 +01:00
private readonly Dictionary<TestLifecycle, List<ICodexNode>> onlineCodexNodes = new Dictionary<TestLifecycle, List<ICodexNode>>();
2023-09-13 14:24:43 +02:00
2023-09-20 13:56:01 +02:00
public CodexDistTest()
{
ProjectPlugin.Load<CodexPlugin.CodexPlugin>();
ProjectPlugin.Load<CodexContractsPlugin.CodexContractsPlugin>();
ProjectPlugin.Load<GethPlugin.GethPlugin>();
ProjectPlugin.Load<MetricsPlugin.MetricsPlugin>();
}
protected override void Initialize(FixtureLog fixtureLog)
{
var localBuilder = new LocalCodexBuilder(fixtureLog);
localBuilder.Intialize();
localBuilder.Build();
}
2023-12-06 10:50:02 +01:00
protected override void LifecycleStart(TestLifecycle lifecycle)
{
onlineCodexNodes.Add(lifecycle, new List<ICodexNode>());
}
protected override void LifecycleStop(TestLifecycle lifecycle)
{
DeleteBlockRepo(onlineCodexNodes[lifecycle]);
2023-12-06 10:50:02 +01:00
onlineCodexNodes.Remove(lifecycle);
}
2024-05-09 09:32:48 +02:00
public ICodexNode StartCodex()
2023-09-13 14:24:43 +02:00
{
2024-05-09 09:32:48 +02:00
return StartCodex(s => { });
2023-09-13 14:24:43 +02:00
}
2024-05-09 09:32:48 +02:00
public ICodexNode StartCodex(Action<ICodexSetup> setup)
2023-09-13 14:24:43 +02:00
{
2024-05-09 09:32:48 +02:00
return StartCodex(1, setup)[0];
2023-09-13 14:24:43 +02:00
}
2024-05-09 09:32:48 +02:00
public ICodexNodeGroup StartCodex(int numberOfNodes)
2023-09-13 14:24:43 +02:00
{
2024-05-09 09:32:48 +02:00
return StartCodex(numberOfNodes, s => { });
2023-09-13 14:24:43 +02:00
}
2024-05-09 09:32:48 +02:00
public ICodexNodeGroup StartCodex(int numberOfNodes, Action<ICodexSetup> setup)
2023-09-13 14:24:43 +02:00
{
2023-09-20 12:02:32 +02:00
var group = Ci.StartCodexNodes(numberOfNodes, s =>
2023-09-13 14:24:43 +02:00
{
setup(s);
OnCodexSetup(s);
});
2023-12-06 10:50:02 +01:00
onlineCodexNodes[Get()].AddRange(group);
2023-09-13 14:24:43 +02:00
return group;
}
public PeerConnectionTestHelpers CreatePeerConnectionTestHelpers()
{
return new PeerConnectionTestHelpers(GetTestLog());
}
public PeerDownloadTestHelpers CreatePeerDownloadTestHelpers()
{
return new PeerDownloadTestHelpers(GetTestLog(), Get().GetFileManager());
}
2023-09-19 11:51:59 +02:00
public IEnumerable<ICodexNode> GetAllOnlineCodexNodes()
2023-09-13 14:24:43 +02:00
{
2023-12-06 10:50:02 +01:00
return onlineCodexNodes[Get()];
2023-09-13 14:24:43 +02:00
}
2023-10-30 13:30:14 +01:00
public void AssertBalance(ICodexContracts contracts, ICodexNode codexNode, Constraint constraint, string msg = "")
2023-09-19 16:22:07 +02:00
{
2023-10-30 13:30:14 +01:00
AssertHelpers.RetryAssert(constraint, () => contracts.GetTestTokenBalance(codexNode), nameof(AssertBalance) + msg);
2023-09-19 16:22:07 +02:00
}
public void CheckLogForErrors(params ICodexNode[] nodes)
{
foreach (var node in nodes) CheckLogForErrors(node);
}
public void CheckLogForErrors(ICodexNode node)
{
2024-03-20 11:11:59 +01:00
Log($"Checking {node.GetName()} log for errors.");
var log = Ci.DownloadLog(node);
log.AssertLogDoesNotContain("Block validation failed");
log.AssertLogDoesNotContain("ERR ");
}
public void LogNodeStatus(ICodexNode node, IMetricsAccess? metrics = null)
{
Log("Status for " + node.GetName() + Environment.NewLine +
GetBasicNodeStatus(node));
}
private string GetBasicNodeStatus(ICodexNode node)
{
return JsonConvert.SerializeObject(node.GetDebugInfo(), Formatting.Indented) + Environment.NewLine +
node.Space().ToString() + Environment.NewLine;
}
// Disabled for now: Makes huge log files!
//private string GetNodeMetrics(IMetricsAccess? metrics)
//{
// if (metrics == null) return "No metrics enabled";
// var m = metrics.GetAllMetrics();
// if (m == null) return "No metrics received";
// return m.AsCsv();
//}
2023-09-13 14:24:43 +02:00
protected virtual void OnCodexSetup(ICodexSetup setup)
{
}
2023-12-06 10:50:02 +01:00
protected override void CollectStatusLogData(TestLifecycle lifecycle, Dictionary<string, string> data)
{
var nodes = onlineCodexNodes[lifecycle];
var upload = nodes.Select(n => n.TransferSpeeds.GetUploadSpeed()).ToList()!.OptionalAverage();
var download = nodes.Select(n => n.TransferSpeeds.GetDownloadSpeed()).ToList()!.OptionalAverage();
if (upload != null) data.Add("avgupload", upload.ToString());
if (download != null) data.Add("avgdownload", download.ToString());
}
private void DeleteBlockRepo(List<ICodexNode> codexNodes)
{
foreach (var node in codexNodes)
{
2024-06-11 11:33:02 +02:00
try
{
if (node.CrashWatcher.HasContainerCrashed())
{
Log("Crash detected!");
}
2024-06-11 11:33:02 +02:00
node.DeleteRepoFolder();
}
catch (Exception ex)
{
Log($"Failed to delete repo folder for node {node.GetName()} : {ex}");
}
}
}
2023-09-13 14:24:43 +02:00
}
}