From 561024738ae237c1fc50b13679b81b7fcf63824c Mon Sep 17 00:00:00 2001 From: benbierens Date: Thu, 24 Aug 2023 09:33:46 +0200 Subject: [PATCH] Fixes issue with log downloading in holdmybeer test --- ContinuousTests/ContinuousLogDownloader.cs | 99 ---------------------- ContinuousTests/ContinuousTest.cs | 8 ++ ContinuousTests/ContinuousTestRunner.cs | 16 ---- ContinuousTests/Tests/HoldMyBeerTest.cs | 5 +- 4 files changed, 10 insertions(+), 118 deletions(-) delete mode 100644 ContinuousTests/ContinuousLogDownloader.cs diff --git a/ContinuousTests/ContinuousLogDownloader.cs b/ContinuousTests/ContinuousLogDownloader.cs deleted file mode 100644 index 60a2882..0000000 --- a/ContinuousTests/ContinuousLogDownloader.cs +++ /dev/null @@ -1,99 +0,0 @@ -using DistTestCore; -using DistTestCore.Codex; -using KubernetesWorkflow; - -namespace ContinuousTests -{ - public class ContinuousLogDownloader - { - private readonly TestLifecycle lifecycle; - private readonly RunningContainer[] containers; - private readonly string outputPath; - private readonly CancellationToken cancelToken; - - public ContinuousLogDownloader(TestLifecycle lifecycle, RunningContainer[] containers, string outputPath, CancellationToken cancelToken) - { - this.lifecycle = lifecycle; - this.containers = containers; - this.outputPath = outputPath; - this.cancelToken = cancelToken; - } - - public void Run() - { - while (!cancelToken.IsCancellationRequested) - { - UpdateLogs(); - - cancelToken.WaitHandle.WaitOne(TimeSpan.FromSeconds(15)); - } - - // 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(); - } - - private void UpdateLogs() - { - foreach (var container in containers) - { - UpdateLog(container); - } - } - - private void UpdateLog(RunningContainer container) - { - var filepath = Path.Combine(outputPath, GetLogName(container)); - if (!File.Exists(filepath)) - { - File.WriteAllLines(filepath, new[] { container.Name }); - } - - var appender = new LogAppender(filepath); - - lifecycle.CodexStarter.DownloadLog(container, appender, null); - } - - private static string GetLogName(RunningContainer container) - { - return container.Name - .Replace("<", "") - .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; - var line = reader.ReadLine(); - while (line != null) - { - if (recording) - { - File.AppendAllLines(filename, new[] { line }); - } - else - { - recording = line == lastLine; - } - - line = reader.ReadLine(); - } - } - } -} diff --git a/ContinuousTests/ContinuousTest.cs b/ContinuousTests/ContinuousTest.cs index 6818cc4..3e76298 100644 --- a/ContinuousTests/ContinuousTest.cs +++ b/ContinuousTests/ContinuousTest.cs @@ -1,5 +1,7 @@ using DistTestCore; using DistTestCore.Codex; +using DistTestCore.Logs; +using KubernetesWorkflow; using Logging; namespace ContinuousTests @@ -87,6 +89,12 @@ namespace ContinuousTests return file; } + public IDownloadedLog DownloadContainerLog(RunningContainer container, int? tailLines = null) + { + var nodeRunner = new NodeRunner(Nodes, Configuration, TimeSet, Log, Configuration.CodexDeployment.Metadata.KubeNamespace, EthereumAccountIndex); + return nodeRunner.DownloadLog(container, tailLines); + } + private void DownloadToFile(CodexAccess node, string contentId, TestFile file) { using var fileStream = File.OpenWrite(file.Filename); diff --git a/ContinuousTests/ContinuousTestRunner.cs b/ContinuousTests/ContinuousTestRunner.cs index 2f82cbb..bfe6000 100644 --- a/ContinuousTests/ContinuousTestRunner.cs +++ b/ContinuousTests/ContinuousTestRunner.cs @@ -30,9 +30,6 @@ namespace ContinuousTests ClearAllCustomNamespaces(allTests, overviewLog); - // Disabled for now. Still looking for a better way. - // StartLogDownloader(taskFactory); - var testLoops = allTests.Select(t => new TestLoop(taskFactory, config, overviewLog, t.GetType(), t.RunTestEvery, cancelToken)).ToArray(); foreach (var testLoop in testLoops) @@ -64,18 +61,5 @@ namespace ContinuousTests var lifecycle = k8SFactory.CreateTestLifecycle(config.KubeConfigFile, config.LogPath, config.DataPath, test.CustomK8sNamespace, new DefaultTimeSet(), log); lifecycle.WorkflowCreator.CreateWorkflow().DeleteTestResources(); } - - private void StartLogDownloader(TaskFactory taskFactory) - { - if (!config.DownloadContainerLogs) return; - - var path = Path.Combine(config.LogPath, "containers"); - if (!Directory.Exists(path)) Directory.CreateDirectory(path); - - var lifecycle = k8SFactory.CreateTestLifecycle(config.KubeConfigFile, config.LogPath, config.DataPath, config.CodexDeployment.Metadata.KubeNamespace, new DefaultTimeSet(), new NullLog()); - var downloader = new ContinuousLogDownloader(lifecycle, config.CodexDeployment.CodexContainers, path, cancelToken); - - taskFactory.Run(downloader.Run); - } } } diff --git a/ContinuousTests/Tests/HoldMyBeerTest.cs b/ContinuousTests/Tests/HoldMyBeerTest.cs index 09c2338..6d73294 100644 --- a/ContinuousTests/Tests/HoldMyBeerTest.cs +++ b/ContinuousTests/Tests/HoldMyBeerTest.cs @@ -37,7 +37,7 @@ namespace ContinuousTests.Tests var cidTag = cid!.Id.Substring(cid.Id.Length - 6); Stopwatch.Measure(Log, "upload-log-asserts", () => { - var uploadLog = NodeRunner.DownloadLog(Nodes[0].Container, 50000); + var uploadLog = DownloadContainerLog(Nodes[0].Container, 50000); var storeLines = uploadLog.FindLinesThatContain("Stored data", "topics=\"codex node\""); uploadLog.DeleteFile(); @@ -46,13 +46,12 @@ namespace ContinuousTests.Tests AssertStoreLineContains(storeLine, numberOfBlocks, sizeInBytes); }); - var dl = DownloadFile(Nodes[0], cid!); file.AssertIsEqual(dl); Stopwatch.Measure(Log, "download-log-asserts", () => { - var downloadLog = NodeRunner.DownloadLog(Nodes[0].Container, 50000); + var downloadLog = DownloadContainerLog(Nodes[0].Container, 50000); var sentLines = downloadLog.FindLinesThatContain("Sent bytes", "topics=\"codex restapi\""); downloadLog.DeleteFile();