cs-codex-dist-tests/ContinuousTests/ContinuousLogDownloader.cs

100 lines
2.8 KiB
C#
Raw Normal View History

2023-07-20 08:52:19 +00:00
using DistTestCore;
using DistTestCore.Codex;
2023-07-20 08:52:19 +00:00
using KubernetesWorkflow;
namespace ContinuousTests
2023-07-20 08:52:19 +00:00
{
public class ContinuousLogDownloader
{
private readonly TestLifecycle lifecycle;
private readonly CodexDeployment deployment;
private readonly string outputPath;
private readonly CancellationToken cancelToken;
2023-07-20 08:52:19 +00:00
public ContinuousLogDownloader(TestLifecycle lifecycle, CodexDeployment deployment, string outputPath, CancellationToken cancelToken)
2023-07-20 08:52:19 +00:00
{
this.lifecycle = lifecycle;
this.deployment = deployment;
this.outputPath = outputPath;
this.cancelToken = cancelToken;
2023-07-20 08:52:19 +00:00
}
public void Run()
{
while (!cancelToken.IsCancellationRequested)
2023-07-20 08:52:19 +00:00
{
UpdateLogs();
cancelToken.WaitHandle.WaitOne(TimeSpan.FromSeconds(15));
2023-07-20 08:52:19 +00:00
}
2023-07-21 07:00:07 +00:00
// After testing has stopped, we wait a little bit and fetch the logs one more time.
// If our latest fetch was not recent, interesting test-related log activity might
// not have been captured yet.
Thread.Sleep(TimeSpan.FromSeconds(10));
UpdateLogs();
2023-07-20 08:52:19 +00:00
}
private void UpdateLogs()
{
foreach (var container in deployment.CodexContainers)
2023-07-20 08:52:19 +00:00
{
UpdateLog(container);
}
}
private void UpdateLog(RunningContainer container)
{
var filepath = Path.Combine(outputPath, GetLogName(container));
if (!File.Exists(filepath))
{
File.WriteAllLines(filepath, new[] { container.Name });
}
2023-07-20 08:52:19 +00:00
var appender = new LogAppender(filepath);
lifecycle.CodexStarter.DownloadLog(container, appender);
}
private static string GetLogName(RunningContainer container)
{
return container.Name
.Replace("<", "")
2023-07-20 08:52:19 +00:00
.Replace(">", "")
+ ".log";
}
}
public class LogAppender : ILogHandler
{
private readonly string filename;
public LogAppender(string filename)
{
this.filename = filename;
}
public void Log(Stream log)
{
using var reader = new StreamReader(log);
var lines = File.ReadAllLines(filename);
var lastLine = lines.Last();
var recording = lines.Length < 3;
2023-07-20 08:52:19 +00:00
var line = reader.ReadLine();
while (line != null)
{
if (recording)
{
File.AppendAllLines(filename, new[] { line });
}
else
{
recording = line == lastLine;
}
2023-07-20 08:52:19 +00:00
line = reader.ReadLine();
2023-07-20 08:52:19 +00:00
}
}
}
}