Improve logfile checking

This commit is contained in:
Ben 2024-03-20 11:11:59 +01:00
parent a6c2bf5230
commit ef53fe02a3
No known key found for this signature in database
GPG Key ID: 541B9D8C9F1426A1
4 changed files with 26 additions and 15 deletions

View File

@ -4,7 +4,7 @@ namespace Core
{ {
public interface IDownloadedLog public interface IDownloadedLog
{ {
bool DoesLogContain(string expectedString); string[] GetLinesContaining(string expectedString);
string[] FindLinesThatContain(params string[] tags); string[] FindLinesThatContain(params string[] tags);
void DeleteFile(); void DeleteFile();
} }
@ -18,20 +18,23 @@ namespace Core
this.logFile = logFile; this.logFile = logFile;
} }
public bool DoesLogContain(string expectedString) public string[] GetLinesContaining(string expectedString)
{ {
using var file = File.OpenRead(logFile.FullFilename); using var file = File.OpenRead(logFile.FullFilename);
using var streamReader = new StreamReader(file); using var streamReader = new StreamReader(file);
var lines = new List<string>();
var line = streamReader.ReadLine(); var line = streamReader.ReadLine();
while (line != null) while (line != null)
{ {
if (line.Contains(expectedString)) return true; if (line.Contains(expectedString))
{
lines.Add(line);
}
line = streamReader.ReadLine(); line = streamReader.ReadLine();
} }
//Assert.Fail($"{owner} Unable to find string '{expectedString}' in CodexNode log file {logFile.FullFilename}"); return lines.ToArray(); ;
return false;
} }
public string[] FindLinesThatContain(params string[] tags) public string[] FindLinesThatContain(params string[] tags)

View File

@ -108,7 +108,7 @@ namespace CodexTests.BasicTests
var request = GetOnChainStorageRequest(contracts); var request = GetOnChainStorageRequest(contracts);
AssertStorageRequest(request, purchase, contracts, buyer); AssertStorageRequest(request, purchase, contracts, buyer);
AssertSlotFilledEvents(contracts, request, seller); AssertSlotFilledEvents(contracts, purchase, request, seller);
AssertContractSlot(contracts, request, 0, seller); AssertContractSlot(contracts, request, 0, seller);
purchaseContract.WaitForStorageContractFinished(); purchaseContract.WaitForStorageContractFinished();
@ -137,17 +137,23 @@ namespace CodexTests.BasicTests
Assert.That(discN, Is.LessThan(bootN)); Assert.That(discN, Is.LessThan(bootN));
} }
private void AssertSlotFilledEvents(ICodexContracts contracts, Request request, ICodexNode seller) private void AssertSlotFilledEvents(ICodexContracts contracts, StoragePurchase purchase, Request request, ICodexNode seller)
{ {
// Expect 1 fulfilled event for the purchase.
var requestFulfilledEvents = contracts.GetRequestFulfilledEvents(GetTestRunTimeRange()); var requestFulfilledEvents = contracts.GetRequestFulfilledEvents(GetTestRunTimeRange());
Assert.That(requestFulfilledEvents.Length, Is.EqualTo(1)); Assert.That(requestFulfilledEvents.Length, Is.EqualTo(1));
CollectionAssert.AreEqual(request.RequestId, requestFulfilledEvents[0].RequestId); CollectionAssert.AreEqual(request.RequestId, requestFulfilledEvents[0].RequestId);
// Expect 1 filled-slot event for each slot in the purchase.
var filledSlotEvents = contracts.GetSlotFilledEvents(GetTestRunTimeRange()); var filledSlotEvents = contracts.GetSlotFilledEvents(GetTestRunTimeRange());
Assert.That(filledSlotEvents.Length, Is.EqualTo(1)); Assert.That(filledSlotEvents.Length, Is.EqualTo(purchase.MinRequiredNumberOfNodes));
var filledSlotEvent = filledSlotEvents.Single(); for (var i = 0; i < purchase.MinRequiredNumberOfNodes; i++)
Assert.That(filledSlotEvent.SlotIndex.IsZero); {
Assert.That(filledSlotEvent.RequestId.ToHex(), Is.EqualTo(request.RequestId.ToHex())); var filledSlotEvent = filledSlotEvents[i];
Assert.That(filledSlotEvent.Host, Is.EqualTo(seller.EthAddress)); Assert.That(filledSlotEvent.SlotIndex, Is.EqualTo(i));
Assert.That(filledSlotEvent.RequestId.ToHex(), Is.EqualTo(request.RequestId.ToHex()));
Assert.That(filledSlotEvent.Host, Is.EqualTo(seller.EthAddress));
}
} }
private void AssertStorageRequest(Request request, StoragePurchase purchase, ICodexContracts contracts, ICodexNode buyer) private void AssertStorageRequest(Request request, StoragePurchase purchase, ICodexContracts contracts, ICodexNode buyer)

View File

@ -92,6 +92,7 @@ namespace CodexTests
public void CheckLogForErrors(ICodexNode node) public void CheckLogForErrors(ICodexNode node)
{ {
Log($"Checking {node.GetName()} log for errors.");
var log = Ci.DownloadLog(node); var log = Ci.DownloadLog(node);
log.AssertLogDoesNotContain("Block validation failed"); log.AssertLogDoesNotContain("Block validation failed");

View File

@ -7,7 +7,7 @@ namespace DistTestCore
{ {
public static void AssertLogContains(this IDownloadedLog log, string expectedString) public static void AssertLogContains(this IDownloadedLog log, string expectedString)
{ {
Assert.That(log.DoesLogContain(expectedString), $"Did not find '{expectedString}' in log."); Assert.That(log.GetLinesContaining(expectedString).Any(), $"Did not find '{expectedString}' in log.");
} }
public static void AssertLogDoesNotContain(this IDownloadedLog log, params string[] unexpectedStrings) public static void AssertLogDoesNotContain(this IDownloadedLog log, params string[] unexpectedStrings)
@ -15,9 +15,10 @@ namespace DistTestCore
var errors = new List<string>(); var errors = new List<string>();
foreach (var str in unexpectedStrings) foreach (var str in unexpectedStrings)
{ {
if (log.DoesLogContain(str)) var lines = log.GetLinesContaining(str);
foreach (var line in lines)
{ {
errors.Add($"Did find '{str}' in log."); errors.Add($"Found '{str}' in line '{line}'.");
} }
} }
CollectionAssert.IsEmpty(errors); CollectionAssert.IsEmpty(errors);