cs-codex-dist-tests/DistTestCore/CodexNodeGroup.cs

87 lines
2.5 KiB
C#
Raw Normal View History

2023-04-13 07:33:10 +00:00
using DistTestCore.Codex;
using KubernetesWorkflow;
using System.Collections;
namespace DistTestCore
{
public interface ICodexNodeGroup : IEnumerable<IOnlineCodexNode>
{
2023-04-13 09:07:36 +00:00
ICodexSetup BringOffline();
2023-04-13 07:33:10 +00:00
IOnlineCodexNode this[int index] { get; }
}
public class CodexNodeGroup : ICodexNodeGroup
{
private readonly TestLifecycle lifecycle;
2023-04-13 12:36:17 +00:00
public CodexNodeGroup(TestLifecycle lifecycle, CodexSetup setup, RunningContainers containers, ICodexNodeFactory codexNodeFactory)
2023-04-13 07:33:10 +00:00
{
this.lifecycle = lifecycle;
Setup = setup;
Containers = containers;
2023-04-13 12:36:17 +00:00
Nodes = containers.Containers.Select(c => CreateOnlineCodexNode(c, codexNodeFactory)).ToArray();
2023-04-13 07:33:10 +00:00
}
public IOnlineCodexNode this[int index]
{
get
{
return Nodes[index];
}
}
2023-04-13 09:07:36 +00:00
public ICodexSetup BringOffline()
{
lifecycle.CodexStarter.BringOffline(this);
2023-04-13 09:07:36 +00:00
var result = Setup;
2023-04-13 09:07:36 +00:00
// Clear everything. Prevent accidental use.
Setup = null!;
Nodes = Array.Empty<OnlineCodexNode>();
Containers = null!;
2023-04-13 09:07:36 +00:00
return result;
}
2023-04-13 07:33:10 +00:00
2023-04-13 09:07:36 +00:00
public CodexSetup Setup { get; private set; }
public RunningContainers Containers { get; private set; }
public OnlineCodexNode[] Nodes { get; private set; }
2023-04-13 07:33:10 +00:00
public IEnumerator<IOnlineCodexNode> GetEnumerator()
{
return Nodes.Cast<IOnlineCodexNode>().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return Nodes.GetEnumerator();
}
public string Describe()
{
2023-04-14 12:53:39 +00:00
return $"<CodexNodeGroup@{Containers.Describe()}-{Setup.Describe()}>";
2023-04-13 07:33:10 +00:00
}
2023-04-13 12:36:17 +00:00
private OnlineCodexNode CreateOnlineCodexNode(RunningContainer c, ICodexNodeFactory factory)
2023-04-13 07:33:10 +00:00
{
var access = new CodexAccess(c);
EnsureOnline(access);
2023-04-13 12:36:17 +00:00
return factory.CreateOnlineCodexNode(access, this);
2023-04-13 07:33:10 +00:00
}
private void EnsureOnline(CodexAccess access)
{
try
{
var debugInfo = access.GetDebugInfo();
if (debugInfo == null || string.IsNullOrEmpty(debugInfo.id)) throw new InvalidOperationException("Unable to get debug-info from codex node at startup.");
}
catch (Exception e)
{
lifecycle.Log.Error($"Failed to start codex node: {e}");
throw;
}
}
2023-04-13 07:33:10 +00:00
}
}