Adds status log entry for each test run in continuous tests.

This commit is contained in:
benbierens 2023-11-09 11:35:45 +01:00
parent b8ce4c49d6
commit 0301c3b076
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
4 changed files with 44 additions and 9 deletions

View File

@ -57,7 +57,7 @@ namespace ContinuousTests
} }
else else
{ {
var testLoops = filteredTests.Select(t => new TestLoop(entryPointFactory, taskFactory, config, overviewLog, t.GetType(), t.RunTestEvery, startupChecker, cancelToken)).ToArray(); var testLoops = filteredTests.Select(t => new TestLoop(entryPointFactory, taskFactory, config, overviewLog, statusLog, t.GetType(), t.RunTestEvery, startupChecker, cancelToken)).ToArray();
foreach (var testLoop in testLoops) foreach (var testLoop in testLoops)
{ {
@ -124,7 +124,8 @@ namespace ContinuousTests
var result = new Dictionary<string, string>(); var result = new Dictionary<string, string>();
foreach (var testLoop in testLoops) foreach (var testLoop in testLoops)
{ {
result.Add($"ctest-{testLoop.Name}", $"passes: {testLoop.NumberOfPasses} - failures: {testLoop.NumberOfFailures}"); result.Add("testname", testLoop.Name);
result.Add($"summary", $"passes: {testLoop.NumberOfPasses} - failures: {testLoop.NumberOfFailures}");
} }
return result; return result;
} }

View File

@ -16,6 +16,7 @@ namespace ContinuousTests
private readonly TaskFactory taskFactory; private readonly TaskFactory taskFactory;
private readonly Configuration config; private readonly Configuration config;
private readonly ILog overviewLog; private readonly ILog overviewLog;
private readonly StatusLog statusLog;
private readonly TestHandle handle; private readonly TestHandle handle;
private readonly CancellationToken cancelToken; private readonly CancellationToken cancelToken;
private readonly ICodexNode[] nodes; private readonly ICodexNode[] nodes;
@ -23,11 +24,12 @@ namespace ContinuousTests
private readonly string testName; private readonly string testName;
private static int failureCount = 0; private static int failureCount = 0;
public SingleTestRun(EntryPointFactory entryPointFactory, TaskFactory taskFactory, Configuration config, ILog overviewLog, TestHandle handle, StartupChecker startupChecker, CancellationToken cancelToken) public SingleTestRun(EntryPointFactory entryPointFactory, TaskFactory taskFactory, Configuration config, ILog overviewLog, StatusLog statusLog, TestHandle handle, StartupChecker startupChecker, CancellationToken cancelToken)
{ {
this.taskFactory = taskFactory; this.taskFactory = taskFactory;
this.config = config; this.config = config;
this.overviewLog = overviewLog; this.overviewLog = overviewLog;
this.statusLog = statusLog;
this.handle = handle; this.handle = handle;
this.cancelToken = cancelToken; this.cancelToken = cancelToken;
testName = handle.Test.GetType().Name; testName = handle.Test.GetType().Name;
@ -63,13 +65,15 @@ namespace ContinuousTests
private void RunTest(Action<bool> resultHandler) private void RunTest(Action<bool> resultHandler)
{ {
var testStart = DateTime.UtcNow; var testStart = DateTime.UtcNow;
TimeSpan duration = TimeSpan.Zero;
try try
{ {
RunTestMoments(); RunTestMoments();
duration = DateTime.UtcNow - testStart;
var duration = DateTime.UtcNow - testStart;
OverviewLog($" > Test passed. ({Time.FormatDuration(duration)})"); OverviewLog($" > Test passed. ({Time.FormatDuration(duration)})");
UpdateStatusLogPassed(testStart, duration);
if (!config.KeepPassedTestLogs) if (!config.KeepPassedTestLogs)
{ {
@ -81,6 +85,7 @@ namespace ContinuousTests
{ {
fixtureLog.Error("Test run failed with exception: " + ex); fixtureLog.Error("Test run failed with exception: " + ex);
fixtureLog.MarkAsFailed(); fixtureLog.MarkAsFailed();
UpdateStatusLogFailed(testStart, duration, ex.ToString());
DownloadContainerLogs(testStart); DownloadContainerLogs(testStart);
@ -170,6 +175,26 @@ namespace ContinuousTests
throw new Exception(exceptionsMessage); throw new Exception(exceptionsMessage);
} }
private void UpdateStatusLogFailed(DateTime testStart, TimeSpan duration, string error)
{
statusLog.ConcludeTest("Failed", duration, CreateStatusLogData(testStart, error));
}
private void UpdateStatusLogPassed(DateTime testStart, TimeSpan duration)
{
statusLog.ConcludeTest("Passed", duration, CreateStatusLogData(testStart, "OK"));
}
private Dictionary<string, string> CreateStatusLogData(DateTime testStart, string message)
{
return new Dictionary<string, string>
{
{ "teststart", testStart.ToString("o") },
{ "testname", testName },
{ "message", message }
};
}
private string GetCombinedExceptionsMessage(Exception[] exceptions) private string GetCombinedExceptionsMessage(Exception[] exceptions)
{ {
return string.Join(Environment.NewLine, exceptions.Select(ex => ex.ToString())); return string.Join(Environment.NewLine, exceptions.Select(ex => ex.ToString()));

View File

@ -1,4 +1,5 @@
using Logging; using DistTestCore.Logs;
using Logging;
namespace ContinuousTests namespace ContinuousTests
{ {
@ -8,6 +9,7 @@ namespace ContinuousTests
private readonly TaskFactory taskFactory; private readonly TaskFactory taskFactory;
private readonly Configuration config; private readonly Configuration config;
private readonly ILog overviewLog; private readonly ILog overviewLog;
private readonly StatusLog statusLog;
private readonly Type testType; private readonly Type testType;
private readonly TimeSpan runsEvery; private readonly TimeSpan runsEvery;
private readonly StartupChecker startupChecker; private readonly StartupChecker startupChecker;
@ -15,12 +17,13 @@ namespace ContinuousTests
private readonly EventWaitHandle runFinishedHandle = new EventWaitHandle(true, EventResetMode.ManualReset); private readonly EventWaitHandle runFinishedHandle = new EventWaitHandle(true, EventResetMode.ManualReset);
private static object testLock = new object(); private static object testLock = new object();
public TestLoop(EntryPointFactory entryPointFactory, TaskFactory taskFactory, Configuration config, ILog overviewLog, Type testType, TimeSpan runsEvery, StartupChecker startupChecker, CancellationToken cancelToken) public TestLoop(EntryPointFactory entryPointFactory, TaskFactory taskFactory, Configuration config, ILog overviewLog, StatusLog statusLog, Type testType, TimeSpan runsEvery, StartupChecker startupChecker, CancellationToken cancelToken)
{ {
this.entryPointFactory = entryPointFactory; this.entryPointFactory = entryPointFactory;
this.taskFactory = taskFactory; this.taskFactory = taskFactory;
this.config = config; this.config = config;
this.overviewLog = overviewLog; this.overviewLog = overviewLog;
this.statusLog = statusLog;
this.testType = testType; this.testType = testType;
this.runsEvery = runsEvery; this.runsEvery = runsEvery;
this.startupChecker = startupChecker; this.startupChecker = startupChecker;
@ -73,7 +76,7 @@ namespace ContinuousTests
{ {
var test = (ContinuousTest)Activator.CreateInstance(testType)!; var test = (ContinuousTest)Activator.CreateInstance(testType)!;
var handle = new TestHandle(test); var handle = new TestHandle(test);
var run = new SingleTestRun(entryPointFactory, taskFactory, config, overviewLog, handle, startupChecker, cancelToken); var run = new SingleTestRun(entryPointFactory, taskFactory, config, overviewLog, statusLog, handle, startupChecker, cancelToken);
runFinishedHandle.Reset(); runFinishedHandle.Reset();
run.Run(runFinishedHandle, result => run.Run(runFinishedHandle, result =>

View File

@ -1,5 +1,6 @@
using Logging; using Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using Utils;
namespace DistTestCore.Logs namespace DistTestCore.Logs
{ {
@ -15,6 +16,11 @@ namespace DistTestCore.Logs
fixtureName = NameUtils.GetRawFixtureName(); fixtureName = NameUtils.GetRawFixtureName();
} }
public void ConcludeTest(string resultStatus, TimeSpan testDuration, Dictionary<string, string> data)
{
ConcludeTest(resultStatus, Time.FormatDuration(testDuration), data);
}
public void ConcludeTest(string resultStatus, string testDuration, Dictionary<string, string> data) public void ConcludeTest(string resultStatus, string testDuration, Dictionary<string, string> data)
{ {
data.Add("timestamp", DateTime.UtcNow.ToString("o")); data.Add("timestamp", DateTime.UtcNow.ToString("o"));
@ -23,7 +29,7 @@ namespace DistTestCore.Logs
data.Add("testid", NameUtils.GetTestId()); data.Add("testid", NameUtils.GetTestId());
data.Add("category", NameUtils.GetCategoryName()); data.Add("category", NameUtils.GetCategoryName());
data.Add("fixturename", fixtureName); data.Add("fixturename", fixtureName);
data.Add("testname", NameUtils.GetTestMethodName()); if (!data.ContainsKey("testname")) data.Add("testname", NameUtils.GetTestMethodName());
data.Add("testduration", testDuration); data.Add("testduration", testDuration);
Write(data); Write(data);
} }