cs-codex-dist-tests/DistTestCore/Helpers/PeerConnectionTestHelpers.cs

67 lines
2.2 KiB
C#
Raw Normal View History

using DistTestCore.Codex;
2023-07-18 14:07:52 +00:00
using static DistTestCore.Helpers.FullConnectivityHelper;
namespace DistTestCore.Helpers
{
2023-07-18 14:07:52 +00:00
public class PeerConnectionTestHelpers : IFullConnectivityImplementation
{
2023-07-18 14:07:52 +00:00
private readonly FullConnectivityHelper helper;
public PeerConnectionTestHelpers(DistTest test)
{
2023-07-18 14:07:52 +00:00
helper = new FullConnectivityHelper(test, this);
}
public void AssertFullyConnected(IEnumerable<IOnlineCodexNode> nodes)
{
2023-07-18 14:07:52 +00:00
helper.AssertFullyConnected(nodes);
}
2023-07-18 14:07:52 +00:00
public string Description()
{
2023-07-18 14:07:52 +00:00
return "Peer Discovery";
}
2023-07-18 14:07:52 +00:00
public string ValidateEntry(Entry entry, Entry[] allEntries)
{
2023-07-18 14:07:52 +00:00
var result = string.Empty;
foreach (var peer in entry.Response.table.nodes)
{
2023-07-18 14:07:52 +00:00
var expected = GetExpectedDiscoveryEndpoint(allEntries, peer);
if (expected != peer.address)
2023-05-12 08:48:12 +00:00
{
2023-07-18 14:07:52 +00:00
result += $"Node:{entry.Node.GetName()} has incorrect peer table entry. Was: '{peer.address}', expected: '{expected}'. ";
}
}
2023-07-18 14:07:52 +00:00
return result;
}
2023-07-18 14:07:52 +00:00
public PeerConnectionState Check(Entry from, Entry to)
{
2023-07-18 14:07:52 +00:00
var peerId = to.Response.id;
2023-07-18 14:07:52 +00:00
var response = from.Node.GetDebugPeer(peerId);
if (!response.IsPeerFound)
{
2023-07-18 14:07:52 +00:00
return PeerConnectionState.NoConnection;
}
2023-07-18 14:07:52 +00:00
if (!string.IsNullOrEmpty(response.peerId) && response.addresses.Any())
{
2023-07-18 14:07:52 +00:00
return PeerConnectionState.Connection;
2023-05-11 10:44:53 +00:00
}
2023-07-18 14:07:52 +00:00
return PeerConnectionState.Unknown;
2023-05-11 10:44:53 +00:00
}
2023-07-18 14:07:52 +00:00
private static string GetExpectedDiscoveryEndpoint(Entry[] allEntries, CodexDebugTableNodeResponse node)
{
2023-07-18 14:07:52 +00:00
var peer = allEntries.SingleOrDefault(e => e.Response.table.localNode.peerId == node.peerId);
if (peer == null) return $"peerId: {node.peerId} is not known.";
2023-07-18 14:07:52 +00:00
var n = (OnlineCodexNode)peer.Node;
var ip = n.CodexAccess.Container.Pod.PodInfo.Ip;
var discPort = n.CodexAccess.Container.Recipe.GetPortByTag(CodexContainerRecipe.DiscoveryPortTag);
return $"{ip}:{discPort.Number}";
2023-05-12 08:48:12 +00:00
}
}
}