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

121 lines
3.7 KiB
C#
Raw Normal View History

2023-03-26 07:41:46 +00:00
using CodexDistTestCore.Config;
using NUnit.Framework;
2023-03-21 12:20:21 +00:00
namespace CodexDistTestCore
{
[SetUpFixture]
public abstract class DistTest
{
2023-03-22 08:22:18 +00:00
private TestLog log = null!;
private FileManager fileManager = null!;
public K8sManager k8sManager = null!;
[OneTimeSetUp]
public void GlobalSetup()
{
// Previous test run may have been interrupted.
// Begin by cleaning everything up.
2023-03-22 08:22:18 +00:00
log = new TestLog();
fileManager = new FileManager(log);
k8sManager = new K8sManager(log, fileManager);
2023-03-22 08:22:18 +00:00
try
{
k8sManager.DeleteAllResources();
fileManager.DeleteAllTestFiles();
}
catch (Exception ex)
{
GlobalTestFailure.HasFailed = true;
log.Error($"Global setup cleanup failed with: {ex}");
throw;
}
log.Log("Global setup cleanup successful");
}
[SetUp]
public void SetUpDistTest()
{
2023-03-20 09:37:03 +00:00
if (GlobalTestFailure.HasFailed)
{
Assert.Inconclusive("Skip test: Previous test failed during clean up.");
}
else
{
2023-03-26 07:41:46 +00:00
var dockerImage = new CodexDockerImage();
2023-03-22 08:22:18 +00:00
log = new TestLog();
2023-03-26 07:41:46 +00:00
log.Log($"Using docker image '{dockerImage.GetImageTag()}'");
2023-03-22 08:22:18 +00:00
fileManager = new FileManager(log);
k8sManager = new K8sManager(log, fileManager);
2023-03-20 09:37:03 +00:00
}
}
[TearDown]
public void TearDownDistTest()
{
2023-03-20 09:37:03 +00:00
try
{
log.EndTest();
2023-03-27 12:49:34 +00:00
IncludeLogsAndMetricsOnTestFailure();
2023-03-20 09:37:03 +00:00
k8sManager.DeleteAllResources();
fileManager.DeleteAllTestFiles();
}
catch (Exception ex)
{
2023-03-22 08:22:18 +00:00
log.Error("Cleanup failed: " + ex.Message);
2023-03-20 09:37:03 +00:00
GlobalTestFailure.HasFailed = true;
}
}
public TestFile GenerateTestFile(ByteSize size)
{
return fileManager.GenerateTestFile(size);
}
2023-03-21 14:17:48 +00:00
public IOfflineCodexNodes SetupCodexNodes(int numberOfNodes)
{
2023-03-21 14:17:48 +00:00
return new OfflineCodexNodes(k8sManager, numberOfNodes);
}
2023-03-27 12:49:34 +00:00
private void IncludeLogsAndMetricsOnTestFailure()
{
var result = TestContext.CurrentContext.Result;
if (result.Outcome.Status == NUnit.Framework.Interfaces.TestStatus.Failed)
{
2023-03-27 12:49:34 +00:00
if (IsDownloadingLogsAndMetricsEnabled())
{
2023-03-27 12:49:34 +00:00
log.Log("Downloading all CodexNode logs and metrics because of test failure...");
k8sManager.ForEachOnlineGroup(DownloadLogs);
2023-03-31 08:00:44 +00:00
k8sManager.DownloadAllMetrics();
}
else
{
2023-03-27 12:49:34 +00:00
log.Log("Skipping download of all CodexNode logs and metrics due to [DontDownloadLogsAndMetricsOnFailure] attribute.");
}
}
}
private void DownloadLogs(CodexNodeGroup group)
{
foreach (var node in group)
{
var downloader = new PodLogDownloader(log, k8sManager);
var n = (OnlineCodexNode)node;
downloader.DownloadLog(n);
}
}
2023-03-27 12:49:34 +00:00
private bool IsDownloadingLogsAndMetricsEnabled()
{
var testProperties = TestContext.CurrentContext.Test.Properties;
return !testProperties.ContainsKey(PodLogDownloader.DontDownloadLogsOnFailureKey);
}
}
2023-03-20 09:37:03 +00:00
public static class GlobalTestFailure
{
public static bool HasFailed { get; set; } = false;
}
}