2023-03-26 07:41:46 +00:00
|
|
|
|
using CodexDistTestCore.Config;
|
|
|
|
|
using NUnit.Framework;
|
2023-03-17 10:43:29 +00:00
|
|
|
|
|
2023-03-21 12:20:21 +00:00
|
|
|
|
namespace CodexDistTestCore
|
2023-03-17 10:43:29 +00:00
|
|
|
|
{
|
2023-03-21 08:20:09 +00:00
|
|
|
|
[SetUpFixture]
|
2023-03-17 10:43:29 +00:00
|
|
|
|
public abstract class DistTest
|
|
|
|
|
{
|
2023-03-22 08:22:18 +00:00
|
|
|
|
private TestLog log = null!;
|
2023-03-19 09:49:03 +00:00
|
|
|
|
private FileManager fileManager = null!;
|
2023-03-31 06:39:24 +00:00
|
|
|
|
public K8sManager k8sManager = null!;
|
2023-03-17 10:43:29 +00:00
|
|
|
|
|
2023-03-21 08:20:09 +00:00
|
|
|
|
[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-21 08:20:09 +00:00
|
|
|
|
|
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");
|
2023-03-21 08:20:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-19 09:49:03 +00:00
|
|
|
|
[SetUp]
|
|
|
|
|
public void SetUpDistTest()
|
2023-03-17 10:43:29 +00:00
|
|
|
|
{
|
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
|
|
|
|
}
|
2023-03-17 10:43:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-19 09:49:03 +00:00
|
|
|
|
[TearDown]
|
|
|
|
|
public void TearDownDistTest()
|
2023-03-17 10:43:29 +00:00
|
|
|
|
{
|
2023-03-20 09:37:03 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2023-03-26 08:45:01 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2023-03-17 10:43:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 08:20:09 +00:00
|
|
|
|
public TestFile GenerateTestFile(ByteSize size)
|
2023-03-17 10:43:29 +00:00
|
|
|
|
{
|
2023-03-19 09:49:03 +00:00
|
|
|
|
return fileManager.GenerateTestFile(size);
|
|
|
|
|
}
|
2023-03-17 10:43:29 +00:00
|
|
|
|
|
2023-03-21 14:17:48 +00:00
|
|
|
|
public IOfflineCodexNodes SetupCodexNodes(int numberOfNodes)
|
2023-03-19 09:49:03 +00:00
|
|
|
|
{
|
2023-03-21 14:17:48 +00:00
|
|
|
|
return new OfflineCodexNodes(k8sManager, numberOfNodes);
|
2023-03-17 10:43:29 +00:00
|
|
|
|
}
|
2023-03-26 08:45:01 +00:00
|
|
|
|
|
2023-03-27 12:49:34 +00:00
|
|
|
|
private void IncludeLogsAndMetricsOnTestFailure()
|
2023-03-26 08:45:01 +00:00
|
|
|
|
{
|
|
|
|
|
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-26 08:57:54 +00:00
|
|
|
|
{
|
2023-03-27 12:49:34 +00:00
|
|
|
|
log.Log("Downloading all CodexNode logs and metrics because of test failure...");
|
2023-03-26 08:57:54 +00:00
|
|
|
|
k8sManager.ForEachOnlineGroup(DownloadLogs);
|
2023-03-31 08:00:44 +00:00
|
|
|
|
k8sManager.DownloadAllMetrics();
|
2023-03-26 08:57:54 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-03-27 12:49:34 +00:00
|
|
|
|
log.Log("Skipping download of all CodexNode logs and metrics due to [DontDownloadLogsAndMetricsOnFailure] attribute.");
|
2023-03-26 08:57:54 +00:00
|
|
|
|
}
|
2023-03-26 08:45:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-26 08:57:54 +00:00
|
|
|
|
|
2023-03-27 12:49:34 +00:00
|
|
|
|
private bool IsDownloadingLogsAndMetricsEnabled()
|
2023-03-26 08:57:54 +00:00
|
|
|
|
{
|
|
|
|
|
var testProperties = TestContext.CurrentContext.Test.Properties;
|
|
|
|
|
return !testProperties.ContainsKey(PodLogDownloader.DontDownloadLogsOnFailureKey);
|
|
|
|
|
}
|
2023-03-17 10:43:29 +00:00
|
|
|
|
}
|
2023-03-20 09:37:03 +00:00
|
|
|
|
|
|
|
|
|
public static class GlobalTestFailure
|
|
|
|
|
{
|
|
|
|
|
public static bool HasFailed { get; set; } = false;
|
|
|
|
|
}
|
2023-03-17 10:43:29 +00:00
|
|
|
|
}
|