229 lines
7.2 KiB
C#
Raw Normal View History

using LogosStorageClient;
using StoragePlugin;
using StoragePlugin.OverwatchSupport;
using LogosStorageTests.Helpers;
2023-09-20 13:56:01 +02:00
using Core;
2023-09-13 14:24:43 +02:00
using DistTestCore;
using DistTestCore.Logs;
using Logging;
using MetricsPlugin;
using Newtonsoft.Json;
using NUnit.Framework;
2024-07-25 16:00:51 +02:00
using OverwatchTranscript;
2025-08-13 11:35:03 +02:00
using Utils;
2023-09-13 14:24:43 +02:00
namespace LogosStorageTests
2023-09-13 14:24:43 +02:00
{
public class LogosStorageDistTest : DistTest
2023-09-13 14:24:43 +02:00
{
private readonly List<IStorageNode> nodes = new List<IStorageNode>();
private LogosStorageTranscriptWriter? writer;
2025-01-29 14:44:19 +01:00
public LogosStorageDistTest()
2023-09-20 13:56:01 +02:00
{
ProjectPlugin.Load<StoragePlugin.StoragePlugin>();
2023-09-20 13:56:01 +02:00
ProjectPlugin.Load<MetricsPlugin.MetricsPlugin>();
}
2025-04-25 15:42:13 +02:00
[SetUp]
public void SetupLogosStorageDistTest()
{
2025-04-25 15:42:13 +02:00
writer = SetupTranscript();
}
2025-04-25 15:42:13 +02:00
[TearDown]
public void TearDownLogosStorageDistTest()
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
{
var localBuilder = new LocalNodeBuilder(fixtureLog);
2025-04-25 15:42:13 +02:00
localBuilder.Intialize();
localBuilder.Build();
2025-01-29 14:44:19 +01:00
Ci.AddLogosStorageHooksProvider(new LogosStorageLogTrackerProvider(nodes.Add));
2024-07-25 16:00:51 +02:00
}
public IStorageNode StartLogosStorage()
2023-09-13 14:24:43 +02:00
{
return StartLogosStorage(s => { });
2023-09-13 14:24:43 +02:00
}
public IStorageNode StartLogosStorage(Action<ILogosStorageSetup> setup)
2023-09-13 14:24:43 +02:00
{
return StartLogosStorage(1, setup)[0];
2023-09-13 14:24:43 +02:00
}
public IStorageNodeGroup StartLogosStorage(int numberOfNodes)
2023-09-13 14:24:43 +02:00
{
return StartLogosStorage(numberOfNodes, s => { });
2023-09-13 14:24:43 +02:00
}
public IStorageNodeGroup StartLogosStorage(int numberOfNodes, Action<ILogosStorageSetup> setup)
2023-09-13 14:24:43 +02:00
{
var group = Ci.StartStorageNodes(numberOfNodes, s =>
2023-09-13 14:24:43 +02:00
{
setup(s);
OnLogosStorageSetup(s);
2023-09-13 14:24:43 +02:00
});
2024-06-12 15:28:08 +02:00
2023-09-13 14:24:43 +02:00
return group;
}
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 CheckLogForErrors(params IStorageNode[] nodes)
{
foreach (var node in nodes) CheckLogForErrors(node);
}
public void CheckLogForErrors(IStorageNode 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(IStorageNode node, IMetricsAccess? metrics = null)
{
Log("Status for " + node.GetName() + Environment.NewLine +
GetBasicNodeStatus(node));
}
public void WaitAndCheckNodesStaysAlive(TimeSpan duration, IStorageNodeGroup nodes)
2024-11-21 10:46:11 +01:00
{
WaitAndCheckNodesStaysAlive(duration, nodes.ToArray());
}
public void WaitAndCheckNodesStaysAlive(TimeSpan duration, List<IStorageNode> nodes)
2025-08-13 13:54:33 +02:00
{
WaitAndCheckNodesStaysAlive(duration, nodes.ToArray());
}
public void WaitAndCheckNodesStaysAlive(TimeSpan duration, params IStorageNode[] nodes)
2024-11-21 10:46:11 +01:00
{
2025-08-13 11:35:03 +02:00
Log($"{nameof(WaitAndCheckNodesStaysAlive)} {Time.FormatDuration(duration)}...");
var timeout = TimeSpan.FromSeconds(3.0);
Assert.That(duration.TotalSeconds, Is.GreaterThan(timeout.TotalSeconds));
2024-11-21 10:46:11 +01:00
var start = DateTime.UtcNow;
while ((DateTime.UtcNow - start) < duration)
{
2025-08-13 11:35:03 +02:00
Thread.Sleep(timeout);
2024-11-21 10:46:11 +01:00
foreach (var node in nodes)
{
2025-08-13 11:35:03 +02:00
Assert.That(node.HasCrashed(), Is.False);
2024-11-21 10:46:11 +01:00
var info = node.GetDebugInfo();
Assert.That(!string.IsNullOrEmpty(info.Id));
}
}
2025-08-13 11:35:03 +02:00
Log($"{nameof(WaitAndCheckNodesStaysAlive)} OK");
2024-11-21 10:46:11 +01:00
}
public void AssertNodesContainFile(ContentId cid, IStorageNodeGroup nodes)
2024-11-21 10:46:11 +01:00
{
AssertNodesContainFile(cid, nodes.ToArray());
}
public void AssertNodesContainFile(ContentId cid, params IStorageNode[] nodes)
2024-11-21 10:46:11 +01:00
{
2025-08-13 11:35:03 +02:00
Log($"{nameof(AssertNodesContainFile)} {nodes.Names()} {cid}...");
2024-11-21 10:46:11 +01:00
foreach (var node in nodes)
{
var localDatasets = node.LocalFiles();
CollectionAssert.Contains(localDatasets.Content.Select(c => c.Cid), cid);
}
2025-08-13 11:35:03 +02:00
Log($"{nameof(AssertNodesContainFile)} OK");
2024-11-21 10:46:11 +01:00
}
private string GetBasicNodeStatus(IStorageNode node)
{
return JsonConvert.SerializeObject(node.GetDebugInfo(), Formatting.Indented) + Environment.NewLine +
node.Space().ToString() + Environment.NewLine;
}
protected virtual void OnLogosStorageSetup(ILogosStorageSetup setup)
2023-09-13 14:24:43 +02:00
{
}
private CreateTranscriptAttribute? GetTranscriptAttributeOfCurrentTest()
{
var attrs = GetCurrentTestMethodAttribute<CreateTranscriptAttribute>();
if (attrs.Any()) return attrs.Single();
return null;
}
private LogosStorageTranscriptWriter? SetupTranscript()
{
var attr = GetTranscriptAttributeOfCurrentTest();
2025-04-25 15:42:13 +02:00
if (attr == null) return null;
var config = new LogosStorageTranscriptWriterConfig(
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 LogosStorageTranscriptWriter(log, config, Transcript.NewWriter(log));
Ci.AddLogosStorageHooksProvider(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
}
}