cs-codex-dist-tests/Tests/CodexTests/Helpers/PeerConnectionTestHelpers.cs

69 lines
2.2 KiB
C#
Raw Permalink Normal View History

2023-09-13 10:24:46 +00:00
using CodexPlugin;
using Logging;
2023-11-12 09:36:48 +00:00
using static CodexTests.Helpers.FullConnectivityHelper;
2023-09-13 10:24:46 +00:00
2023-11-12 09:36:48 +00:00
namespace CodexTests.Helpers
2023-09-13 10:24:46 +00:00
{
public class PeerConnectionTestHelpers : IFullConnectivityImplementation
{
private readonly FullConnectivityHelper helper;
public PeerConnectionTestHelpers(ILog log)
{
helper = new FullConnectivityHelper(log, this);
}
2023-09-19 09:51:59 +00:00
public void AssertFullyConnected(IEnumerable<ICodexNode> nodes)
2023-09-13 10:24:46 +00:00
{
helper.AssertFullyConnected(nodes);
}
public string Description()
{
return "Peer Discovery";
}
public string ValidateEntry(Entry entry, Entry[] allEntries)
{
var result = string.Empty;
2024-03-26 09:31:49 +00:00
foreach (var peer in entry.Response.Table.Nodes)
2023-09-13 10:24:46 +00:00
{
var expected = GetExpectedDiscoveryEndpoint(allEntries, peer);
2024-03-26 09:31:49 +00:00
if (expected != peer.Address)
2023-09-13 10:24:46 +00:00
{
2024-03-26 09:31:49 +00:00
result += $"Node:{entry.Node.GetName()} has incorrect peer table entry. Was: '{peer.Address}', expected: '{expected}'. ";
2023-09-13 10:24:46 +00:00
}
}
return result;
}
public PeerConnectionState Check(Entry from, Entry to)
{
2024-03-26 09:31:49 +00:00
var peerId = to.Response.Id;
2023-09-13 10:24:46 +00:00
var response = from.Node.GetDebugPeer(peerId);
if (!response.IsPeerFound)
{
return PeerConnectionState.NoConnection;
}
2024-03-26 09:31:49 +00:00
if (!string.IsNullOrEmpty(response.PeerId) && response.Addresses.Any())
2023-09-13 10:24:46 +00:00
{
return PeerConnectionState.Connection;
}
return PeerConnectionState.Unknown;
}
2024-03-26 09:31:49 +00:00
private static string GetExpectedDiscoveryEndpoint(Entry[] allEntries, DebugInfoTableNode node)
2023-09-13 10:24:46 +00:00
{
2024-03-26 09:31:49 +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-09-13 10:24:46 +00:00
var container = peer.Node.Container;
var podInfo = peer.Node.GetPodInfo();
var ip = podInfo.Ip;
2023-09-13 10:24:46 +00:00
var discPort = container.Recipe.GetPortByTag(CodexContainerRecipe.DiscoveryPortTag)!;
return $"{ip}:{discPort.Number}";
}
}
}