2
0
mirror of synced 2025-01-23 23:08:52 +00:00

Log accessing test passes

This commit is contained in:
benbierens 2023-04-13 11:30:19 +02:00
parent cbf0fbf5b5
commit 7eab4840ef
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
8 changed files with 130 additions and 6 deletions

View File

@ -0,0 +1,35 @@
using Logging;
using NUnit.Framework;
namespace DistTestCore.CodexLogs
{
public interface ICodexNodeLog
{
void AssertLogContains(string expectedString);
}
public class CodexNodeLog : ICodexNodeLog
{
private readonly LogFile logFile;
public CodexNodeLog(LogFile logFile)
{
this.logFile = logFile;
}
public void AssertLogContains(string expectedString)
{
using var file = File.OpenRead(logFile.FullFilename);
using var streamReader = new StreamReader(file);
var line = streamReader.ReadLine();
while (line != null)
{
if (line.Contains(expectedString)) return;
line = streamReader.ReadLine();
}
Assert.Fail($"Unable to find string '{expectedString}' in CodexNode log file {logFile.FilenameWithoutPath}");
}
}
}

View File

@ -0,0 +1,35 @@
using KubernetesWorkflow;
using Logging;
namespace DistTestCore.CodexLogs
{
public class LogDownloadHandler : ILogHandler
{
private readonly string description;
private readonly LogFile log;
public LogDownloadHandler(string description, LogFile log)
{
this.description = description;
this.log = log;
}
public CodexNodeLog CreateCodexNodeLog()
{
return new CodexNodeLog(log);
}
public void Log(Stream stream)
{
log.Write($"{description} -->> {log.FilenameWithoutPath}");
log.WriteRaw(description);
var reader = new StreamReader(stream);
var line = reader.ReadLine();
while (line != null)
{
log.WriteRaw(line);
line = reader.ReadLine();
}
}
}
}

View File

@ -1,5 +1,7 @@
using DistTestCore.Codex; using DistTestCore.Codex;
using DistTestCore.CodexLogs;
using KubernetesWorkflow; using KubernetesWorkflow;
using Nethereum.Merkle.Patricia;
namespace DistTestCore namespace DistTestCore
{ {
@ -37,6 +39,12 @@ namespace DistTestCore
workflow.DeleteAllResources(); workflow.DeleteAllResources();
} }
public void DownloadLog(RunningContainer container, ILogHandler logHandler)
{
var workflow = CreateWorkflow();
workflow.DownloadContainerLog(container, logHandler);
}
private StartupWorkflow CreateWorkflow() private StartupWorkflow CreateWorkflow()
{ {
return workflowCreator.CreateWorkflow(); return workflowCreator.CreateWorkflow();

View File

@ -1,4 +1,5 @@
using DistTestCore.Codex; using DistTestCore.Codex;
using DistTestCore.CodexLogs;
using NUnit.Framework; using NUnit.Framework;
namespace DistTestCore namespace DistTestCore
@ -9,7 +10,7 @@ namespace DistTestCore
ContentId UploadFile(TestFile file); ContentId UploadFile(TestFile file);
TestFile? DownloadContent(ContentId contentId); TestFile? DownloadContent(ContentId contentId);
void ConnectToPeer(IOnlineCodexNode node); void ConnectToPeer(IOnlineCodexNode node);
//ICodexNodeLog DownloadLog(); ICodexNodeLog DownloadLog();
//IMetricsAccess Metrics { get; } //IMetricsAccess Metrics { get; }
//IMarketplaceAccess Marketplace { get; } //IMarketplaceAccess Marketplace { get; }
} }
@ -76,10 +77,10 @@ namespace DistTestCore
Log($"Successfully connected to peer {peer.GetName()}."); Log($"Successfully connected to peer {peer.GetName()}.");
} }
//public ICodexNodeLog DownloadLog() public ICodexNodeLog DownloadLog()
//{ {
// return Group.DownloadLog(this); return lifecycle.DownloadLog(this);
//} }
public string Describe() public string Describe()
{ {

View File

@ -1,4 +1,5 @@
using Logging; using DistTestCore.CodexLogs;
using Logging;
namespace DistTestCore namespace DistTestCore
{ {
@ -20,5 +21,16 @@ namespace DistTestCore
CodexStarter.DeleteAllResources(); CodexStarter.DeleteAllResources();
FileManager.DeleteAllTestFiles(); FileManager.DeleteAllTestFiles();
} }
public ICodexNodeLog DownloadLog(OnlineCodexNode node)
{
var subFile = Log.CreateSubfile();
var description = node.Describe();
var handler = new LogDownloadHandler(description, subFile);
CodexStarter.DownloadLog(node.CodexAccess.Container, handler);
return new CodexNodeLog(subFile);
}
} }
} }

View File

@ -43,6 +43,12 @@ namespace KubernetesWorkflow
WaitUntilPodOffline(pod.Name); WaitUntilPodOffline(pod.Name);
} }
public void DownloadPodLog(RunningPod pod, ContainerRecipe recipe, ILogHandler logHandler)
{
var stream = client.ReadNamespacedPodLog(pod.Name, K8sNamespace, recipe.Name);
logHandler.Log(stream);
}
public void DeleteAllResources() public void DeleteAllResources()
{ {
DeleteNamespace(); DeleteNamespace();

View File

@ -34,6 +34,14 @@
}); });
} }
public void DownloadContainerLog(RunningContainer container, ILogHandler logHandler)
{
K8s(controller =>
{
controller.DownloadPodLog(container.Pod, container.Recipe, logHandler);
});
}
public void DeleteAllResources() public void DeleteAllResources()
{ {
K8s(controller => K8s(controller =>
@ -72,6 +80,10 @@
controller.Dispose(); controller.Dispose();
return result; return result;
} }
}
public interface ILogHandler
{
void Log(Stream log);
} }
} }

View File

@ -1,4 +1,5 @@
using DistTestCore; using DistTestCore;
using DistTestCore.Codex;
using KubernetesWorkflow; using KubernetesWorkflow;
using NUnit.Framework; using NUnit.Framework;
@ -63,6 +64,20 @@ namespace Tests.BasicTests
PerformTwoClientTest(primary, secondary); PerformTwoClientTest(primary, secondary);
} }
[Test]
public void CodexLogExample()
{
var primary = SetupCodexNodes(1)
.WithLogLevel(CodexLogLevel.Trace)
.BringOnline()[0];
primary.UploadFile(GenerateTestFile(5.MB()));
var log = primary.DownloadLog();
log.AssertLogContains("Uploaded file");
}
//[Test] //[Test]
//public void TwoMetricsExample() //public void TwoMetricsExample()
//{ //{