127 lines
3.4 KiB
C#
Raw Normal View History

2023-03-20 11:37:02 +01:00
using NUnit.Framework;
2023-03-21 13:20:21 +01:00
namespace CodexDistTestCore
2023-03-20 11:37:02 +01:00
{
public class TestLog
{
public const string LogRoot = "D:/CodexTestLogs";
private static LogFile? file = null;
public static void Log(string message)
{
file!.Write(message);
}
2023-03-21 13:20:21 +01:00
public static void Error(string message)
2023-03-20 11:37:02 +01:00
{
Log($"[ERROR] {message}");
}
public static void BeginTest()
{
if (file != null) throw new InvalidOperationException("Test is already started!");
var name = GetTestName();
file = new LogFile(name);
Log($"Begin: {name}");
}
public static void EndTest(K8sManager k8sManager)
{
if (file == null) throw new InvalidOperationException("No test is started!");
2023-03-21 13:20:21 +01:00
2023-03-20 11:37:02 +01:00
var result = TestContext.CurrentContext.Result;
Log($"Finished: {GetTestName()} = {result.Outcome.Status}");
if (result.Outcome.Status == NUnit.Framework.Interfaces.TestStatus.Failed)
{
IncludeFullPodLogging(k8sManager);
}
file = null;
}
private static string GetTestName()
{
var test = TestContext.CurrentContext.Test;
var className = test.ClassName!.Substring(test.ClassName.LastIndexOf('.') + 1);
return $"{className}.{test.MethodName}";
}
2023-03-20 15:42:42 +01:00
private static void LogRaw(string message, string filename)
2023-03-20 11:37:02 +01:00
{
2023-03-20 15:42:42 +01:00
file!.WriteRaw(message, filename);
2023-03-20 11:37:02 +01:00
}
private static void IncludeFullPodLogging(K8sManager k8sManager)
{
2023-03-20 15:42:42 +01:00
Log("Full pod logging:");
2023-03-20 11:37:02 +01:00
k8sManager.FetchAllPodsLogs(WritePodLog);
}
2023-03-21 15:44:21 +01:00
private static void WritePodLog(int id, string nodeDescription, Stream stream)
2023-03-20 11:37:02 +01:00
{
2023-03-21 15:44:21 +01:00
var logFile = id.ToString().PadLeft(6, '0');
Log($"{nodeDescription} -->> {logFile}");
LogRaw(nodeDescription, logFile);
2023-03-20 11:37:02 +01:00
var reader = new StreamReader(stream);
var line = reader.ReadLine();
while (line != null)
{
2023-03-21 15:44:21 +01:00
LogRaw(line, logFile);
2023-03-20 11:37:02 +01:00
line = reader.ReadLine();
}
}
}
public class LogFile
{
2023-03-20 15:42:42 +01:00
private readonly string filepath;
2023-03-20 11:37:02 +01:00
private readonly string filename;
public LogFile(string name)
{
var now = DateTime.UtcNow;
2023-03-20 15:42:42 +01:00
filepath = Path.Join(
2023-03-20 11:37:02 +01:00
TestLog.LogRoot,
$"{now.Year}-{Pad(now.Month)}",
Pad(now.Day));
Directory.CreateDirectory(filepath);
2023-03-20 15:42:42 +01:00
filename = Path.Combine(filepath, $"{Pad(now.Hour)}-{Pad(now.Minute)}-{Pad(now.Second)}Z_{name.Replace('.', '-')}");
2023-03-20 11:37:02 +01:00
}
public void Write(string message)
{
WriteRaw($"{GetTimestamp()} {message}");
}
2023-03-20 15:42:42 +01:00
public void WriteRaw(string message, string subfile = "")
2023-03-20 11:37:02 +01:00
{
try
{
2023-03-21 13:20:21 +01:00
File.AppendAllLines(filename + subfile + ".log", new[] { message });
2023-03-20 11:37:02 +01: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")}]";
}
}
}