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;
|
2023-10-03 07:53:02 +00:00
|
|
|
|
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;
|
2023-08-30 08:57:20 +00:00
|
|
|
|
LogReplacements = new List<BaseLogStringReplacement>();
|
2023-06-25 07:53:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Check()
|
|
|
|
|
{
|
2024-02-22 13:41:07 +00:00
|
|
|
|
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...");
|
2023-09-27 07:09:42 +00:00
|
|
|
|
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.");
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 08:57:20 +00:00
|
|
|
|
public List<BaseLogStringReplacement> LogReplacements { get; }
|
|
|
|
|
|
2023-09-27 07:09:42 +00:00
|
|
|
|
private void IncludeDeploymentConfiguration(ILog log)
|
|
|
|
|
{
|
|
|
|
|
log.Log("");
|
|
|
|
|
var deployment = config.CodexDeployment;
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var workflow = entryPoint.Tools.CreateWorkflow();
|
2023-10-25 07:14:35 +00:00
|
|
|
|
foreach (var instance in deployment.CodexInstances)
|
2023-09-27 07:09:42 +00:00
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
foreach (var container in instance.Containers.Containers)
|
|
|
|
|
{
|
|
|
|
|
var podInfo = workflow.GetPodInfo(container);
|
|
|
|
|
log.Log($"Codex environment variables for '{container.Name}':");
|
2024-02-22 13:41:07 +00:00
|
|
|
|
log.Log(
|
|
|
|
|
$"Namespace: {container.RunningContainers.StartResult.Cluster.Configuration.KubernetesNamespace} - " +
|
2023-11-07 13:33:45 +00:00
|
|
|
|
$"Pod name: {podInfo.Name} - Deployment name: {instance.Containers.StartResult.Deployment.Name}");
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var codexVars = container.Recipe.EnvVars;
|
|
|
|
|
foreach (var vars in codexVars) log.Log(vars.ToString());
|
|
|
|
|
log.Log("");
|
|
|
|
|
}
|
2023-09-27 07:09:42 +00:00
|
|
|
|
}
|
2024-02-22 13:41:07 +00:00
|
|
|
|
|
2023-10-03 07:53:02 +00:00
|
|
|
|
log.Log($"Deployment metadata: {JsonConvert.SerializeObject(deployment.Metadata)}");
|
2023-09-27 07:09:42 +00:00
|
|
|
|
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.");
|
|
|
|
|
}
|
2024-02-22 13:41:07 +00:00
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-28 09:01:10 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2024-02-22 13:41:07 +00:00
|
|
|
|
var nodes = entryPoint.CreateInterface()
|
|
|
|
|
.WrapCodexContainers(config.CodexDeployment.CodexInstances.Select(i => i.Containers).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);
|
2023-10-19 09:18:59 +00:00
|
|
|
|
log.Log($"Checking {n.Container.Name} @ '{address}'...");
|
2023-06-25 07:53:10 +00:00
|
|
|
|
|
2023-07-13 07:20:24 +00:00
|
|
|
|
if (EnsureOnline(log, n))
|
2023-06-25 07:53:10 +00:00
|
|
|
|
{
|
|
|
|
|
log.Log("OK");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-10-19 09:18:59 +00:00
|
|
|
|
log.Error($"No response from '{address}'.");
|
2023-06-25 07:53:10 +00:00
|
|
|
|
pass = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-22 13:41:07 +00:00
|
|
|
|
|
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();
|
|
|
|
|
if (info == null || string.IsNullOrEmpty(info.id)) return false;
|
2023-07-13 07:20:24 +00:00
|
|
|
|
|
|
|
|
|
log.Log($"Codex version: '{info.codex.version}' revision: '{info.codex.revision}'");
|
2023-08-30 08:57:20 +00:00
|
|
|
|
LogReplacements.Add(new BaseLogStringReplacement(info.id, n.GetName()));
|
2023-06-25 07:53:10 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-02-22 13:41:07 +00:00
|
|
|
|
|
2023-06-25 07:53:10 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-28 09:01:10 +00:00
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-22 13:41:07 +00:00
|
|
|
|
private void DuplicatesCheck(ContinuousTest[] tests, List<string> errors,
|
|
|
|
|
Func<ContinuousTest, bool> considerCondition, Func<ContinuousTest, object> getValue, string propertyName)
|
2023-06-28 09:01:10 +00:00
|
|
|
|
{
|
|
|
|
|
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);
|
2024-02-22 13:41:07 +00:00
|
|
|
|
errors.Add(
|
|
|
|
|
$"Tests '{string.Join(",", duplicates.Select(d => d.Name))}' have the same '{propertyName}'. These must be unique.");
|
2023-06-28 09:01:10 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckRequiredNumberOfNodes(ContinuousTest[] tests, List<string> errors)
|
|
|
|
|
{
|
|
|
|
|
foreach (var test in tests)
|
|
|
|
|
{
|
2023-08-29 14:08:40 +00:00
|
|
|
|
if (test.RequiredNumberOfNodes != -1)
|
2023-06-28 09:01:10 +00:00
|
|
|
|
{
|
2023-08-29 14:08:40 +00:00
|
|
|
|
if (test.RequiredNumberOfNodes < 1)
|
|
|
|
|
{
|
2024-02-22 13:41:07 +00:00
|
|
|
|
errors.Add(
|
|
|
|
|
$"Test '{test.Name}' requires {test.RequiredNumberOfNodes} nodes. Test must require > 0 nodes, or -1 to select all nodes.");
|
2023-08-29 14:08:40 +00:00
|
|
|
|
}
|
2023-10-25 07:14:35 +00:00
|
|
|
|
else if (test.RequiredNumberOfNodes > config.CodexDeployment.CodexInstances.Length)
|
2023-08-29 14:08:40 +00:00
|
|
|
|
{
|
2024-02-22 13:41:07 +00:00
|
|
|
|
errors.Add(
|
|
|
|
|
$"Test '{test.Name}' requires {test.RequiredNumberOfNodes} nodes. Deployment only has {config.CodexDeployment.CodexInstances.Length}");
|
2023-08-29 14:08:40 +00:00
|
|
|
|
}
|
2023-06-28 09:01:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-25 07:53:10 +00:00
|
|
|
|
}
|
2024-02-22 13:41:07 +00:00
|
|
|
|
}
|