cs-codex-dist-tests/Tests/DistTestCore/TestLifecycle.cs

102 lines
3.1 KiB
C#
Raw Normal View History

2023-09-12 11:32:06 +00:00
using Core;
2023-09-20 08:51:47 +00:00
using DistTestCore.Logs;
2023-09-13 08:03:11 +00:00
using FileUtils;
using KubernetesWorkflow;
using Utils;
2023-04-12 11:53:55 +00:00
namespace DistTestCore
{
public class TestLifecycle : IK8sHooks
2023-04-12 11:53:55 +00:00
{
2023-09-13 14:06:05 +00:00
private const string TestsType = "dist-tests";
2023-07-31 09:51:29 +00:00
private readonly DateTime testStart;
2023-09-13 08:03:11 +00:00
private readonly EntryPoint entryPoint;
private readonly Dictionary<string, string> metadata;
private readonly List<RunningContainers> runningContainers = new List<RunningContainers>();
2023-04-13 12:36:17 +00:00
2023-09-12 08:31:55 +00:00
public TestLifecycle(TestLog log, Configuration configuration, ITimeSet timeSet, string testNamespace)
2023-04-12 11:53:55 +00:00
{
2023-04-14 12:53:39 +00:00
Log = log;
Configuration = configuration;
TimeSet = timeSet;
testStart = DateTime.UtcNow;
2023-07-18 12:26:21 +00:00
entryPoint = new EntryPoint(log, configuration.GetK8sConfiguration(timeSet, this, testNamespace), configuration.GetFileManagerFolder(), timeSet);
metadata = entryPoint.GetPluginMetadata();
2023-09-13 08:03:11 +00:00
CoreInterface = entryPoint.CreateInterface();
2023-09-11 14:57:57 +00:00
2023-09-12 08:31:55 +00:00
log.WriteLogTag();
2023-04-12 14:06:04 +00:00
}
2023-04-12 11:53:55 +00:00
2023-09-12 08:31:55 +00:00
public TestLog Log { get; }
public Configuration Configuration { get; }
public ITimeSet TimeSet { get; }
public CoreInterface CoreInterface { get; }
2023-09-12 08:31:55 +00:00
public void DeleteAllResources()
{
2023-09-21 08:33:09 +00:00
entryPoint.Decommission(
deleteKubernetesResources: true,
deleteTrackedFiles: true
);
2023-09-13 08:03:11 +00:00
}
public TrackedFile GenerateTestFile(ByteSize size, string label = "")
{
return entryPoint.Tools.GetFileManager().GenerateFile(size, label);
}
2023-09-13 12:24:43 +00:00
public IFileManager GetFileManager()
2023-09-13 08:03:11 +00:00
{
2023-09-13 12:24:43 +00:00
return entryPoint.Tools.GetFileManager();
}
2023-07-31 09:51:29 +00:00
2023-09-13 14:06:05 +00:00
public Dictionary<string, string> GetPluginMetadata()
{
return entryPoint.GetPluginMetadata();
}
2023-09-12 08:31:55 +00:00
public string GetTestDuration()
{
2023-09-12 08:31:55 +00:00
var testDuration = DateTime.UtcNow - testStart;
return Time.FormatDuration(testDuration);
}
public void OnContainersStarted(RunningContainers rc)
{
runningContainers.Add(rc);
}
public void OnContainersStopped(RunningContainers rc)
{
runningContainers.Remove(rc);
}
2023-09-13 14:06:05 +00:00
public void OnContainerRecipeCreated(ContainerRecipe recipe)
{
recipe.PodLabels.Add("tests-type", TestsType);
recipe.PodLabels.Add("runid", NameUtils.GetRunId());
recipe.PodLabels.Add("testid", NameUtils.GetTestId());
recipe.PodLabels.Add("category", NameUtils.GetCategoryName());
recipe.PodLabels.Add("fixturename", NameUtils.GetRawFixtureName());
recipe.PodLabels.Add("testname", NameUtils.GetTestMethodName());
foreach (var pair in metadata)
{
recipe.PodLabels.Add(pair.Key, pair.Value);
}
2023-09-13 14:06:05 +00:00
}
public void DownloadAllLogs()
{
foreach (var rc in runningContainers)
{
foreach (var c in rc.Containers)
{
2023-09-14 13:40:15 +00:00
CoreInterface.DownloadLog(c);
}
}
}
2023-04-12 11:53:55 +00:00
}
}