cs-codex-dist-tests/Tests/CodexContinuousTests/ContinuousTestRunner.cs

93 lines
3.5 KiB
C#
Raw Normal View History

2023-09-20 11:33:58 +00:00
using DistTestCore.Logs;
2023-06-28 13:11:20 +00:00
using Logging;
using Utils;
2023-06-25 09:06:47 +00:00
namespace ContinuousTests
{
2023-06-25 07:53:10 +00:00
public class ContinuousTestRunner
{
2023-09-20 11:33:58 +00:00
private readonly EntryPointFactory entryPointFactory = new EntryPointFactory();
private readonly ConfigLoader configLoader = new ConfigLoader();
private readonly TestFactory testFactory = new TestFactory();
2023-06-25 07:53:10 +00:00
private readonly Configuration config;
2023-06-28 14:19:37 +00:00
private readonly CancellationToken cancelToken;
2023-06-28 14:19:37 +00:00
public ContinuousTestRunner(string[] args, CancellationToken cancelToken)
{
2023-06-26 12:44:21 +00:00
config = configLoader.Load(args);
2023-06-28 14:19:37 +00:00
this.cancelToken = cancelToken;
}
2023-06-25 07:53:10 +00:00
public void Run()
{
2023-09-21 08:33:09 +00:00
var overviewLog = new LogSplitter(
new FixtureLog(new LogConfig(config.LogPath, false), DateTime.UtcNow, "Overview"),
new ConsoleLog()
);
overviewLog.Log("Initializing...");
2023-09-20 11:33:58 +00:00
var entryPoint = entryPointFactory.CreateEntryPoint(config.KubeConfigFile, config.DataPath, config.CodexDeployment.Metadata.KubeNamespace, overviewLog);
entryPoint.Announce();
2023-09-21 08:33:09 +00:00
overviewLog.Log("Initialized. Performing startup checks...");
2023-09-20 11:33:58 +00:00
var startupChecker = new StartupChecker(entryPoint, config, cancelToken);
2023-06-25 07:53:10 +00:00
startupChecker.Check();
2023-06-28 14:19:37 +00:00
var taskFactory = new TaskFactory();
2023-09-21 08:33:09 +00:00
overviewLog.Log("Startup checks passed. Continuous tests starting...");
overviewLog.Log("");
2023-06-25 07:53:10 +00:00
var allTests = testFactory.CreateTests();
2023-06-28 13:11:20 +00:00
ClearAllCustomNamespaces(allTests, overviewLog);
2023-09-21 08:33:09 +00:00
var testLoops = allTests.Select(t => new TestLoop(entryPointFactory, taskFactory, config, overviewLog, t.GetType(), t.RunTestEvery, startupChecker, cancelToken)).ToArray();
2023-06-28 14:19:37 +00:00
foreach (var testLoop in testLoops)
{
2023-06-29 08:23:04 +00:00
if (cancelToken.IsCancellationRequested) break;
2023-06-28 14:19:37 +00:00
overviewLog.Log("Launching test-loop for " + testLoop.Name);
testLoop.Begin();
Thread.Sleep(TimeSpan.FromSeconds(5));
}
2023-06-27 08:16:59 +00:00
2023-06-29 08:23:04 +00:00
overviewLog.Log("Finished launching test-loops.");
WaitUntilFinished(overviewLog);
2023-06-28 14:19:37 +00:00
overviewLog.Log("Cancelling all test-loops...");
taskFactory.WaitAll();
2023-06-29 08:23:04 +00:00
overviewLog.Log("All tasks cancelled.");
}
2023-06-28 13:11:20 +00:00
private void WaitUntilFinished(LogSplitter overviewLog)
{
if (config.TargetDurationSeconds > 0)
{
var targetDuration = TimeSpan.FromSeconds(config.TargetDurationSeconds);
cancelToken.WaitHandle.WaitOne(targetDuration);
overviewLog.Log($"Congratulations! The targer duration has been reached! ({Time.FormatDuration(targetDuration)})");
}
else
{
cancelToken.WaitHandle.WaitOne();
}
}
2023-09-21 08:33:09 +00:00
private void ClearAllCustomNamespaces(ContinuousTest[] allTests, ILog log)
2023-06-28 13:11:20 +00:00
{
foreach (var test in allTests) ClearAllCustomNamespaces(test, log);
}
2023-09-21 08:33:09 +00:00
private void ClearAllCustomNamespaces(ContinuousTest test, ILog log)
2023-06-28 13:11:20 +00:00
{
if (string.IsNullOrEmpty(test.CustomK8sNamespace)) return;
log.Log($"Clearing namespace '{test.CustomK8sNamespace}'...");
2023-09-20 11:33:58 +00:00
var entryPoint = entryPointFactory.CreateEntryPoint(config.KubeConfigFile, config.DataPath, test.CustomK8sNamespace, log);
entryPoint.Tools.CreateWorkflow().DeleteNamespacesStartingWith(test.CustomK8sNamespace);
2023-06-28 13:11:20 +00:00
}
}
}