230 lines
7.2 KiB
C#
Raw Normal View History

using BlockchainUtils;
2025-01-16 13:51:29 +01:00
using CodexClient;
using CodexContractsPlugin;
using CodexNetDeployer;
2023-09-19 16:22:07 +02:00
using CodexPlugin;
2024-07-25 16:00:51 +02:00
using CodexPlugin.OverwatchSupport;
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 GethPlugin;
using Logging;
using MetricsPlugin;
using Newtonsoft.Json;
using NUnit.Framework;
2023-09-19 16:22:07 +02:00
using NUnit.Framework.Constraints;
2024-07-25 16:00:51 +02:00
using OverwatchTranscript;
2023-09-13 14:24:43 +02:00
namespace CodexTests
2023-09-13 14:24:43 +02:00
{
public class CodexDistTest : DistTest
{
2025-04-25 15:42:13 +02:00
private readonly BlockCache blockCache = new BlockCache();
private readonly List<ICodexNode> nodes = new List<ICodexNode>();
private CodexTranscriptWriter? writer;
2025-01-29 14:44:19 +01: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>();
}
2025-04-25 15:42:13 +02:00
[SetUp]
public void SetupCodexDistTest()
{
2025-04-25 15:42:13 +02:00
writer = SetupTranscript();
}
2025-04-25 15:42:13 +02:00
[TearDown]
public void TearDownCodexDistTest()
2024-07-25 16:00:51 +02:00
{
2025-04-25 15:42:13 +02:00
TeardownTranscript();
2024-07-25 16:00:51 +02:00
}
2025-04-25 15:42:13 +02:00
protected override void Initialize(FixtureLog fixtureLog)
2024-07-25 16:00:51 +02:00
{
2025-04-25 15:42:13 +02:00
var localBuilder = new LocalCodexBuilder(fixtureLog);
localBuilder.Intialize();
localBuilder.Build();
2025-01-29 14:44:19 +01:00
2025-04-25 15:42:13 +02:00
Ci.AddCodexHooksProvider(new CodexLogTrackerProvider(nodes.Add));
2024-07-25 16:00:51 +02:00
}
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);
});
2024-06-12 15:28:08 +02:00
2023-09-13 14:24:43 +02:00
return group;
}
public IGethNode StartGethNode(Action<IGethSetup> setup)
{
2025-04-25 15:42:13 +02:00
return Ci.StartGethNode(blockCache, setup);
}
2023-09-13 14:24:43 +02:00
public PeerConnectionTestHelpers CreatePeerConnectionTestHelpers()
{
return new PeerConnectionTestHelpers(GetTestLog());
}
public PeerDownloadTestHelpers CreatePeerDownloadTestHelpers()
{
2025-04-25 15:42:13 +02:00
return new PeerDownloadTestHelpers(GetTestLog(), GetFileManager());
2023-09-13 14:24:43 +02:00
}
public void AssertBalance(ICodexContracts contracts, ICodexNode codexNode, Constraint constraint, string msg)
2023-09-19 16:22:07 +02:00
{
2025-03-11 15:00:27 +01:00
Assert.Fail("Depricated, use MarketplaceAutobootstrapDistTest assertBalances instead.");
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.");
2025-01-16 10:15:02 +01:00
var log = node.DownloadLog();
log.AssertLogDoesNotContain("Block validation failed");
log.AssertLogDoesNotContainLinesStartingWith("ERR ");
}
public void LogNodeStatus(ICodexNode node, IMetricsAccess? metrics = null)
{
Log("Status for " + node.GetName() + Environment.NewLine +
GetBasicNodeStatus(node));
}
2024-11-21 10:46:11 +01:00
public void WaitAndCheckNodesStaysAlive(TimeSpan duration, ICodexNodeGroup nodes)
{
WaitAndCheckNodesStaysAlive(duration, nodes.ToArray());
}
public void WaitAndCheckNodesStaysAlive(TimeSpan duration, params ICodexNode[] nodes)
{
var start = DateTime.UtcNow;
while ((DateTime.UtcNow - start) < duration)
{
Thread.Sleep(5000);
foreach (var node in nodes)
{
var info = node.GetDebugInfo();
Assert.That(!string.IsNullOrEmpty(info.Id));
}
}
}
public void AssertNodesContainFile(ContentId cid, ICodexNodeGroup nodes)
{
AssertNodesContainFile(cid, nodes.ToArray());
}
public void AssertNodesContainFile(ContentId cid, params ICodexNode[] nodes)
{
foreach (var node in nodes)
{
var localDatasets = node.LocalFiles();
CollectionAssert.Contains(localDatasets.Content.Select(c => c.Cid), cid);
}
}
private string GetBasicNodeStatus(ICodexNode node)
{
return JsonConvert.SerializeObject(node.GetDebugInfo(), Formatting.Indented) + Environment.NewLine +
node.Space().ToString() + Environment.NewLine;
}
2023-09-13 14:24:43 +02:00
protected virtual void OnCodexSetup(ICodexSetup setup)
{
}
private CreateTranscriptAttribute? GetTranscriptAttributeOfCurrentTest()
{
var attrs = GetCurrentTestMethodAttribute<CreateTranscriptAttribute>();
if (attrs.Any()) return attrs.Single();
return null;
}
2025-04-25 15:42:13 +02:00
private CodexTranscriptWriter? SetupTranscript()
{
var attr = GetTranscriptAttributeOfCurrentTest();
2025-04-25 15:42:13 +02:00
if (attr == null) return null;
var config = new CodexTranscriptWriterConfig(
2025-04-25 15:42:13 +02:00
attr.OutputFilename,
attr.IncludeBlockReceivedEvents
);
2025-04-25 15:42:13 +02:00
var log = new LogPrefixer(GetTestLog(), "(Transcript) ");
var writer = new CodexTranscriptWriter(log, config, Transcript.NewWriter(log));
2025-01-29 14:44:19 +01:00
Ci.AddCodexHooksProvider(writer);
2025-04-25 15:42:13 +02:00
return writer;
}
2025-04-25 15:42:13 +02:00
private void TeardownTranscript()
{
2025-04-25 15:42:13 +02:00
if (writer == null) return;
2025-04-25 15:42:13 +02:00
var result = GetTestResult();
var log = GetTestLog();
writer.AddResult(result.Success, result.Result);
try
{
2025-04-25 15:42:13 +02:00
Stopwatch.Measure(log, "Transcript.ProcessLogs", () =>
{
2025-04-25 15:42:13 +02:00
writer.ProcessLogs(DownloadAllLogs());
});
2025-04-25 15:42:13 +02:00
Stopwatch.Measure(log, $"Transcript.FinalizeWriter", () =>
{
2025-04-25 16:13:01 +02:00
writer.IncludeFile(log.GetFullName() + ".log");
2025-04-25 15:42:13 +02:00
writer.FinalizeWriter();
});
}
catch (Exception ex)
{
2025-04-25 15:42:13 +02:00
log.Error("Failure during transcript teardown: " + ex);
}
}
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class CreateTranscriptAttribute : PropertyAttribute
{
public CreateTranscriptAttribute(string outputFilename, bool includeBlockReceivedEvents = true)
{
OutputFilename = outputFilename;
IncludeBlockReceivedEvents = includeBlockReceivedEvents;
}
public string OutputFilename { get; }
public bool IncludeBlockReceivedEvents { get; }
2023-09-13 14:24:43 +02:00
}
}