From 6fe9d38eb36238132ba85ab63a36ba0f4f52945c Mon Sep 17 00:00:00 2001 From: benbierens Date: Tue, 3 Oct 2023 09:53:02 +0200 Subject: [PATCH] Adds filter option for selecting tests. Adds cleanup option for deleting deployment namespace. --- Tests/CodexContinuousTests/Configuration.cs | 6 ++++ .../ContinuousTestRunner.cs | 36 ++++++++++++++++++- Tests/CodexContinuousTests/StartupChecker.cs | 2 ++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Tests/CodexContinuousTests/Configuration.cs b/Tests/CodexContinuousTests/Configuration.cs index f8480b8..bc34739 100644 --- a/Tests/CodexContinuousTests/Configuration.cs +++ b/Tests/CodexContinuousTests/Configuration.cs @@ -27,6 +27,12 @@ namespace ContinuousTests [Uniform("target-duration", "td", "TARGETDURATION", false, "If greater than zero, runner will run for this many seconds before stopping.")] public int TargetDurationSeconds { get; set; } = 0; + [Uniform("filter", "f", "FILTER", false, "If set, runs only tests whose names contain any of the filter strings. Comma-separated. Case sensitive.")] + public string Filter { get; set; } = string.Empty; + + [Uniform("cleanup", "cl", "CLEANUP", false, "If set, the kubernetes namespace will be deleted after the test run has finished.")] + public bool Cleanup { get; set; } = false; + public CodexDeployment CodexDeployment { get; set; } = null!; } diff --git a/Tests/CodexContinuousTests/ContinuousTestRunner.cs b/Tests/CodexContinuousTests/ContinuousTestRunner.cs index d3982a8..233ae39 100644 --- a/Tests/CodexContinuousTests/ContinuousTestRunner.cs +++ b/Tests/CodexContinuousTests/ContinuousTestRunner.cs @@ -46,7 +46,13 @@ namespace ContinuousTests ClearAllCustomNamespaces(allTests, overviewLog); - var testLoops = allTests.Select(t => new TestLoop(entryPointFactory, taskFactory, config, overviewLog, t.GetType(), t.RunTestEvery, startupChecker, cancelToken)).ToArray(); + var filteredTests = FilterTests(allTests, overviewLog); + if (!filteredTests.Any()) + { + overviewLog.Log("No tests selected."); + return; + } + var testLoops = filteredTests.Select(t => new TestLoop(entryPointFactory, taskFactory, config, overviewLog, t.GetType(), t.RunTestEvery, startupChecker, cancelToken)).ToArray(); foreach (var testLoop in testLoops) { @@ -62,6 +68,24 @@ namespace ContinuousTests overviewLog.Log("Cancelling all test-loops..."); taskFactory.WaitAll(); overviewLog.Log("All tasks cancelled."); + + PerformCleanup(overviewLog); + } + + private ContinuousTest[] FilterTests(ContinuousTest[] allTests, ILog log) + { + log.Log($"Available tests: {string.Join(", ", allTests.Select(r => r.Name))}"); + + var result = allTests.ToArray(); + var filters = config.Filter.Split(",", StringSplitOptions.RemoveEmptyEntries); + if (filters.Any()) + { + log.Log($"Applying filters: {string.Join(", ", filters)}"); + result = allTests.Where(t => filters.Any(f => t.Name.Contains(f))).ToArray(); + } + + log.Log($"Selected for running: {string.Join(", ", result.Select(r => r.Name))}"); + return result; } private void WaitUntilFinished(LogSplitter overviewLog, StatusLog statusLog, DateTime startTime, TestLoop[] testLoops) @@ -107,5 +131,15 @@ namespace ContinuousTests var entryPoint = entryPointFactory.CreateEntryPoint(config.KubeConfigFile, config.DataPath, test.CustomK8sNamespace, log); entryPoint.Tools.CreateWorkflow().DeleteNamespacesStartingWith(test.CustomK8sNamespace); } + + private void PerformCleanup(ILog log) + { + if (!config.Cleanup) return; + log.Log("Cleaning up test namespace..."); + + var entryPoint = entryPointFactory.CreateEntryPoint(config.KubeConfigFile, config.DataPath, config.CodexDeployment.Metadata.KubeNamespace, log); + entryPoint.Decommission(deleteKubernetesResources: true, deleteTrackedFiles: true); + log.Log("Cleanup finished."); + } } } diff --git a/Tests/CodexContinuousTests/StartupChecker.cs b/Tests/CodexContinuousTests/StartupChecker.cs index 07ae67f..abc4dba 100644 --- a/Tests/CodexContinuousTests/StartupChecker.cs +++ b/Tests/CodexContinuousTests/StartupChecker.cs @@ -2,6 +2,7 @@ using Core; using DistTestCore.Logs; using Logging; +using Newtonsoft.Json; namespace ContinuousTests { @@ -45,6 +46,7 @@ namespace ContinuousTests foreach (var vars in codexVars) log.Log(vars.ToString()); log.Log(""); } + log.Log($"Deployment metadata: {JsonConvert.SerializeObject(deployment.Metadata)}"); log.Log(""); }