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

194 lines
7.0 KiB
C#
Raw Permalink Normal View History

2023-09-20 11:33:58 +00:00
using CodexPlugin;
using Core;
using DistTestCore.Logs;
2023-06-25 07:53:10 +00:00
using Logging;
using Newtonsoft.Json;
2023-06-25 07:53:10 +00:00
namespace ContinuousTests
{
public class StartupChecker
{
private readonly TestFactory testFactory = new TestFactory();
2023-09-20 11:33:58 +00:00
private readonly EntryPoint entryPoint;
2023-06-25 07:53:10 +00:00
private readonly Configuration config;
2023-07-07 06:52:53 +00:00
private readonly CancellationToken cancelToken;
2023-06-25 07:53:10 +00:00
2023-09-20 11:33:58 +00:00
public StartupChecker(EntryPoint entryPoint, Configuration config, CancellationToken cancelToken)
2023-06-25 07:53:10 +00:00
{
2023-09-20 11:33:58 +00:00
this.entryPoint = entryPoint;
2023-06-25 07:53:10 +00:00
this.config = config;
2023-07-07 06:52:53 +00:00
this.cancelToken = cancelToken;
LogReplacements = new List<BaseLogStringReplacement>();
2023-06-25 07:53:10 +00:00
}
public void Check()
{
var log = new FixtureLog(new LogConfig(config.LogPath), DateTime.UtcNow, config.CodexDeployment.Id,
"StartupChecks");
2023-06-25 07:53:10 +00:00
log.Log("Starting continuous test run...");
IncludeDeploymentConfiguration(log);
2023-06-25 07:53:10 +00:00
log.Log("Checking configuration...");
PreflightCheck(config);
log.Log("Contacting Codex nodes...");
CheckCodexNodes(log, config);
log.Log("All OK.");
}
public List<BaseLogStringReplacement> LogReplacements { get; }
private void IncludeDeploymentConfiguration(ILog log)
{
log.Log("");
var deployment = config.CodexDeployment;
var workflow = entryPoint.Tools.CreateWorkflow();
foreach (var instance in deployment.CodexInstances)
{
2024-04-13 14:09:17 +00:00
foreach (var container in instance.Pod.Containers)
{
var podInfo = workflow.GetPodInfo(container);
log.Log($"Codex environment variables for '{container.Name}':");
log.Log(
2024-04-13 14:09:17 +00:00
$"Namespace: {container.RunningPod.StartResult.Cluster.Configuration.KubernetesNamespace} - " +
$"Pod name: {podInfo.Name} - Deployment name: {instance.Pod.StartResult.Deployment.Name}");
var codexVars = container.Recipe.EnvVars;
foreach (var vars in codexVars) log.Log(vars.ToString());
log.Log("");
}
}
log.Log($"Deployment metadata: {JsonConvert.SerializeObject(deployment.Metadata)}");
log.Log("");
}
2023-06-25 07:53:10 +00:00
private void PreflightCheck(Configuration config)
{
var tests = testFactory.CreateTests();
if (!tests.Any())
{
throw new Exception("Unable to find any tests.");
}
2023-06-25 07:53:10 +00:00
foreach (var test in tests)
{
2023-07-07 06:52:53 +00:00
cancelToken.ThrowIfCancellationRequested();
2023-06-25 07:53:10 +00:00
var handle = new TestHandle(test);
handle.GetEarliestMoment();
handle.GetLastMoment();
}
if (!Directory.Exists(config.LogPath))
{
Directory.CreateDirectory(config.LogPath);
}
var errors = CheckTests(tests);
2023-06-25 07:53:10 +00:00
if (errors.Any())
{
throw new Exception("Prerun check failed: " + string.Join(", ", errors));
}
}
private void CheckCodexNodes(BaseLog log, Configuration config)
{
var nodes = entryPoint.CreateInterface()
2024-04-13 14:09:17 +00:00
.WrapCodexContainers(config.CodexDeployment.CodexInstances.Select(i => i.Pod).ToArray());
2023-06-25 07:53:10 +00:00
var pass = true;
foreach (var n in nodes)
{
2023-07-07 06:52:53 +00:00
cancelToken.ThrowIfCancellationRequested();
2023-11-07 11:02:17 +00:00
var address = n.Container.GetAddress(log, CodexContainerRecipe.ApiPortTag);
log.Log($"Checking {n.Container.Name} @ '{address}'...");
2023-06-25 07:53:10 +00:00
if (EnsureOnline(log, n))
2023-06-25 07:53:10 +00:00
{
log.Log("OK");
}
else
{
log.Error($"No response from '{address}'.");
2023-06-25 07:53:10 +00:00
pass = false;
}
}
2023-06-25 07:53:10 +00:00
if (!pass)
{
throw new Exception("Not all codex nodes responded.");
}
}
2023-09-20 11:33:58 +00:00
private bool EnsureOnline(BaseLog log, ICodexNode n)
2023-06-25 07:53:10 +00:00
{
try
{
var info = n.GetDebugInfo();
2024-04-01 06:29:55 +00:00
if (info == null || string.IsNullOrEmpty(info.Id)) return false;
2024-04-01 06:29:55 +00:00
log.Log($"Codex version: '{info.Version.Version}' revision: '{info.Version.Revision}'");
LogReplacements.Add(new BaseLogStringReplacement(info.Id, n.GetName()));
2023-06-25 07:53:10 +00:00
}
catch
{
return false;
}
2023-06-25 07:53:10 +00:00
return true;
}
private List<string> CheckTests(ContinuousTest[] tests)
{
var errors = new List<string>();
CheckRequiredNumberOfNodes(tests, errors);
CheckCustomNamespaceClashes(tests, errors);
return errors;
}
private void CheckCustomNamespaceClashes(ContinuousTest[] tests, List<string> errors)
{
DuplicatesCheck(tests, errors,
considerCondition: t => !string.IsNullOrEmpty(t.CustomK8sNamespace),
getValue: t => t.CustomK8sNamespace,
propertyName: nameof(ContinuousTest.CustomK8sNamespace));
}
private void DuplicatesCheck(ContinuousTest[] tests, List<string> errors,
Func<ContinuousTest, bool> considerCondition, Func<ContinuousTest, object> getValue, string propertyName)
{
foreach (var test in tests)
{
if (considerCondition(test))
{
var duplicates = tests.Where(t => t != test && getValue(t) == getValue(test)).ToList();
if (duplicates.Any())
{
duplicates.Add(test);
errors.Add(
$"Tests '{string.Join(",", duplicates.Select(d => d.Name))}' have the same '{propertyName}'. These must be unique.");
return;
}
}
}
}
private void CheckRequiredNumberOfNodes(ContinuousTest[] tests, List<string> errors)
{
foreach (var test in tests)
{
if (test.RequiredNumberOfNodes != -1)
{
if (test.RequiredNumberOfNodes < 1)
{
errors.Add(
$"Test '{test.Name}' requires {test.RequiredNumberOfNodes} nodes. Test must require > 0 nodes, or -1 to select all nodes.");
}
else if (test.RequiredNumberOfNodes > config.CodexDeployment.CodexInstances.Length)
{
errors.Add(
$"Test '{test.Name}' requires {test.RequiredNumberOfNodes} nodes. Deployment only has {config.CodexDeployment.CodexInstances.Length}");
}
}
}
}
2023-06-25 07:53:10 +00:00
}
}