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

177 lines
6.4 KiB
C#
Raw 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()
{
2023-07-18 07:47:44 +00:00
var log = new FixtureLog(new LogConfig(config.LogPath, false), DateTime.UtcNow, "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;
foreach (var container in deployment.CodexContainers)
{
log.Log($"Codex environment variables for '{container.Name}':");
2023-10-08 16:56:00 +00:00
log.Log($"Pod name: {container.Pod.PodInfo.Name} - Deployment name: {container.Pod.DeploymentName}");
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.");
}
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)
{
2023-09-20 11:33:58 +00:00
var nodes = entryPoint.CreateInterface().WrapCodexContainers(config.CodexDeployment.CodexContainers);
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-09-20 11:33:58 +00:00
log.Log($"Checking {n.Container.Name} @ '{n.Container.Address.Host}:{n.Container.Address.Port}'...");
2023-06-25 07:53:10 +00:00
if (EnsureOnline(log, n))
2023-06-25 07:53:10 +00:00
{
log.Log("OK");
}
else
{
2023-09-20 11:33:58 +00:00
log.Error($"No response from '{n.Container.Address.Host}'.");
2023-06-25 07:53:10 +00:00
pass = false;
}
}
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();
if (info == null || string.IsNullOrEmpty(info.id)) return false;
log.Log($"Codex version: '{info.codex.version}' revision: '{info.codex.revision}'");
LogReplacements.Add(new BaseLogStringReplacement(info.id, n.GetName()));
2023-06-25 07:53:10 +00:00
}
catch
{
return false;
}
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.CodexContainers.Length)
{
errors.Add($"Test '{test.Name}' requires {test.RequiredNumberOfNodes} nodes. Deployment only has {config.CodexDeployment.CodexContainers.Length}");
}
}
}
}
2023-06-25 07:53:10 +00:00
}
}