cs-codex-dist-tests/CodexDistTestCore/TestLog.cs

145 lines
3.8 KiB
C#
Raw Normal View History

2023-03-20 10:37:02 +00:00
using NUnit.Framework;
2023-03-21 12:20:21 +00:00
namespace CodexDistTestCore
2023-03-20 10:37:02 +00:00
{
public class TestLog
{
public const string LogRoot = "D:/CodexTestLogs";
2023-03-22 08:22:18 +00:00
private readonly LogFile file;
2023-03-20 10:37:02 +00:00
2023-03-22 08:22:18 +00:00
public TestLog()
2023-03-20 10:37:02 +00:00
{
var name = GetTestName();
file = new LogFile(name);
Log($"Begin: {name}");
}
2023-03-22 08:22:18 +00:00
public void Log(string message)
2023-03-20 10:37:02 +00:00
{
2023-03-22 08:22:18 +00:00
file.Write(message);
}
2023-03-20 10:37:02 +00:00
2023-03-22 08:22:18 +00:00
public void Error(string message)
{
Log($"[ERROR] {message}");
}
2023-03-21 12:20:21 +00:00
2023-03-22 08:22:18 +00:00
public void EndTest(K8sManager k8sManager)
{
2023-03-20 10:37:02 +00:00
var result = TestContext.CurrentContext.Result;
Log($"Finished: {GetTestName()} = {result.Outcome.Status}");
2023-03-22 08:50:24 +00:00
if (!string.IsNullOrEmpty(result.Message))
{
Log(result.Message);
}
2023-03-20 10:37:02 +00:00
if (result.Outcome.Status == NUnit.Framework.Interfaces.TestStatus.Failed)
{
2023-03-22 08:50:24 +00:00
Log($"{result.StackTrace}");
2023-03-21 15:09:41 +00:00
var logWriter = new PodLogWriter(file);
logWriter.IncludeFullPodLogging(k8sManager);
2023-03-20 10:37:02 +00:00
}
}
private static string GetTestName()
{
var test = TestContext.CurrentContext.Test;
var className = test.ClassName!.Substring(test.ClassName.LastIndexOf('.') + 1);
2023-03-22 09:38:10 +00:00
var args = FormatArguments(test);
return $"{className}.{test.MethodName}{args}";
}
private static string FormatArguments(TestContext.TestAdapter test)
{
if (test.Arguments == null || !test.Arguments.Any()) return "";
return $"[{string.Join(',', test.Arguments)}]";
2023-03-20 10:37:02 +00:00
}
2023-03-21 15:09:41 +00:00
}
public class PodLogWriter : IPodLogsHandler
{
private readonly LogFile file;
2023-03-20 10:37:02 +00:00
2023-03-21 15:09:41 +00:00
public PodLogWriter(LogFile file)
2023-03-20 10:37:02 +00:00
{
2023-03-21 15:09:41 +00:00
this.file = file;
2023-03-20 10:37:02 +00:00
}
2023-03-21 15:09:41 +00:00
public void IncludeFullPodLogging(K8sManager k8sManager)
2023-03-20 10:37:02 +00:00
{
2023-03-22 08:22:18 +00:00
file.Write("Full pod logging:");
2023-03-21 15:09:41 +00:00
k8sManager.FetchAllPodsLogs(this);
2023-03-20 10:37:02 +00:00
}
2023-03-21 15:09:41 +00:00
public void Log(int id, string podDescription, Stream log)
2023-03-20 10:37:02 +00:00
{
2023-03-21 14:44:21 +00:00
var logFile = id.ToString().PadLeft(6, '0');
2023-03-22 08:22:18 +00:00
file.Write($"{podDescription} -->> {logFile}");
2023-03-21 15:09:41 +00:00
LogRaw(podDescription, logFile);
var reader = new StreamReader(log);
2023-03-20 10:37:02 +00:00
var line = reader.ReadLine();
while (line != null)
{
2023-03-21 14:44:21 +00:00
LogRaw(line, logFile);
2023-03-20 10:37:02 +00:00
line = reader.ReadLine();
}
}
2023-03-21 15:09:41 +00:00
private void LogRaw(string message, string filename)
{
file!.WriteRaw(message, filename);
}
2023-03-20 10:37:02 +00:00
}
public class LogFile
{
2023-03-20 14:42:42 +00:00
private readonly string filepath;
2023-03-20 10:37:02 +00:00
private readonly string filename;
public LogFile(string name)
{
var now = DateTime.UtcNow;
2023-03-20 14:42:42 +00:00
filepath = Path.Join(
2023-03-20 10:37:02 +00:00
TestLog.LogRoot,
$"{now.Year}-{Pad(now.Month)}",
Pad(now.Day));
Directory.CreateDirectory(filepath);
2023-03-20 14:42:42 +00:00
filename = Path.Combine(filepath, $"{Pad(now.Hour)}-{Pad(now.Minute)}-{Pad(now.Second)}Z_{name.Replace('.', '-')}");
2023-03-20 10:37:02 +00:00
}
public void Write(string message)
{
WriteRaw($"{GetTimestamp()} {message}");
}
2023-03-20 14:42:42 +00:00
public void WriteRaw(string message, string subfile = "")
2023-03-20 10:37:02 +00:00
{
try
{
2023-03-21 12:20:21 +00:00
File.AppendAllLines(filename + subfile + ".log", new[] { message });
2023-03-20 10:37:02 +00:00
}
catch (Exception ex)
{
Console.WriteLine("Writing to log has failed: " + ex);
}
}
private static string Pad(int n)
{
return n.ToString().PadLeft(2, '0');
}
private static string GetTimestamp()
{
return $"[{DateTime.UtcNow.ToString("u")}]";
}
}
}