2
0
mirror of synced 2025-01-12 17:44:08 +00:00

142 lines
4.8 KiB
C#
Raw Normal View History

2023-09-12 13:32:06 +02:00
using Core;
2023-09-20 10:51:47 +02:00
using DistTestCore.Logs;
2023-09-13 10:03:11 +02:00
using FileUtils;
using KubernetesWorkflow;
using KubernetesWorkflow.Recipe;
using KubernetesWorkflow.Types;
using Utils;
2023-04-12 13:53:55 +02:00
namespace DistTestCore
{
public class TestLifecycle : IK8sHooks
2023-04-12 13:53:55 +02:00
{
2023-09-13 16:06:05 +02:00
private const string TestsType = "dist-tests";
2023-09-13 10:03:11 +02:00
private readonly EntryPoint entryPoint;
private readonly Dictionary<string, string> metadata;
2024-04-13 17:09:17 +03:00
private readonly List<RunningPod> runningContainers = new();
private readonly string deployId;
private readonly List<IDownloadedLog> stoppedContainerLogs = new List<IDownloadedLog>();
2023-04-13 14:36:17 +02:00
public TestLifecycle(TestLog log, Configuration configuration, ITimeSet timeSet, string testNamespace, string deployId, bool waitForCleanup)
2023-04-12 13:53:55 +02:00
{
2023-04-14 14:53:39 +02:00
Log = log;
Configuration = configuration;
TimeSet = timeSet;
2023-12-20 09:48:22 +01:00
TestStart = DateTime.UtcNow;
2023-07-18 14:26:21 +02:00
entryPoint = new EntryPoint(log, configuration.GetK8sConfiguration(timeSet, this, testNamespace), configuration.GetFileManagerFolder(), timeSet);
metadata = entryPoint.GetPluginMetadata();
2023-09-13 10:03:11 +02:00
CoreInterface = entryPoint.CreateInterface();
this.deployId = deployId;
WaitForCleanup = waitForCleanup;
2023-09-12 10:31:55 +02:00
log.WriteLogTag();
2023-04-12 16:06:04 +02:00
}
2023-04-12 13:53:55 +02:00
2023-12-20 09:48:22 +01:00
public DateTime TestStart { get; }
2023-09-12 10:31:55 +02:00
public TestLog Log { get; }
public Configuration Configuration { get; }
public ITimeSet TimeSet { get; }
public bool WaitForCleanup { get; }
public CoreInterface CoreInterface { get; }
2023-09-12 10:31:55 +02:00
public void DeleteAllResources()
{
2023-09-21 10:33:09 +02:00
entryPoint.Decommission(
deleteKubernetesResources: true,
deleteTrackedFiles: true,
waitTillDone: WaitForCleanup
2023-09-21 10:33:09 +02:00
);
2023-09-13 10:03:11 +02:00
}
public TrackedFile GenerateTestFile(ByteSize size, string label = "")
{
return entryPoint.Tools.GetFileManager().GenerateFile(size, label);
}
2024-07-01 15:59:08 +02:00
public TrackedFile GenerateTestFile(Action<IGenerateOption> options, string label = "")
{
return entryPoint.Tools.GetFileManager().GenerateFile(options, label);
}
2023-09-13 14:24:43 +02:00
public IFileManager GetFileManager()
2023-09-13 10:03:11 +02:00
{
2023-09-13 14:24:43 +02:00
return entryPoint.Tools.GetFileManager();
}
2023-07-31 11:51:29 +02:00
2023-09-13 16:06:05 +02:00
public Dictionary<string, string> GetPluginMetadata()
{
return entryPoint.GetPluginMetadata();
}
public TimeSpan GetTestDuration()
{
2023-12-20 09:48:22 +01:00
return DateTime.UtcNow - TestStart;
}
2024-04-13 17:09:17 +03:00
public void OnContainersStarted(RunningPod rc)
{
runningContainers.Add(rc);
}
2024-04-13 17:09:17 +03:00
public void OnContainersStopped(RunningPod rc)
{
runningContainers.Remove(rc);
stoppedContainerLogs.AddRange(rc.Containers.Select(c =>
{
if (c.StopLog == null) throw new Exception("Expected StopLog for stopped container " + c.Name);
return c.StopLog;
}));
}
2023-09-13 16:06:05 +02:00
public void OnContainerRecipeCreated(ContainerRecipe recipe)
{
recipe.PodLabels.Add("tests-type", TestsType);
recipe.PodLabels.Add("deployid", deployId);
2023-09-13 16:06:05 +02:00
recipe.PodLabels.Add("testid", NameUtils.GetTestId());
recipe.PodLabels.Add("category", NameUtils.GetCategoryName());
recipe.PodLabels.Add("fixturename", NameUtils.GetRawFixtureName());
recipe.PodLabels.Add("testname", NameUtils.GetTestMethodName());
recipe.PodLabels.Add("testframeworkrevision", GitInfo.GetStatus());
foreach (var pair in metadata)
{
recipe.PodLabels.Add(pair.Key, pair.Value);
}
2023-09-13 16:06:05 +02:00
}
2024-07-25 16:00:51 +02:00
public IDownloadedLog[] DownloadAllLogs()
{
2024-04-15 11:37:14 +02:00
try
{
var result = new List<IDownloadedLog>();
result.AddRange(stoppedContainerLogs);
2024-04-15 11:37:14 +02:00
foreach (var rc in runningContainers)
{
if (rc.IsStopped)
{
foreach (var c in rc.Containers)
{
if (c.StopLog == null) throw new Exception("No stop-log was downloaded for container.");
result.Add(c.StopLog);
}
}
else
2024-04-15 11:37:14 +02:00
{
foreach (var c in rc.Containers)
{
result.Add(CoreInterface.DownloadLog(c));
}
2024-04-15 11:37:14 +02:00
}
}
return result.ToArray();
}
2024-04-15 11:37:14 +02:00
catch (Exception ex)
{
Log.Error("Exception during log download: " + ex);
2024-07-25 16:00:51 +02:00
return Array.Empty<IDownloadedLog>();
2024-04-15 11:37:14 +02:00
}
}
2023-04-12 13:53:55 +02:00
}
}