From 8fcf3516136697b8dcf2869caf944c653de5b9c9 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 26 Mar 2024 10:31:49 +0100 Subject: [PATCH] Restores debug/peer --- ProjectPlugins/CodexPlugin/CodexAccess.cs | 21 +++++++++++----- ProjectPlugins/CodexPlugin/CodexTypes.cs | 3 ++- .../Helpers/FullConnectivityHelper.cs | 2 ++ .../Helpers/PeerConnectionTestHelpers.cs | 16 ++++++------ .../LayeredDiscoveryTests.cs | 25 +++++++++---------- 5 files changed, 39 insertions(+), 28 deletions(-) diff --git a/ProjectPlugins/CodexPlugin/CodexAccess.cs b/ProjectPlugins/CodexPlugin/CodexAccess.cs index f576cfd..2d4084a 100644 --- a/ProjectPlugins/CodexPlugin/CodexAccess.cs +++ b/ProjectPlugins/CodexPlugin/CodexAccess.cs @@ -30,20 +30,21 @@ namespace CodexPlugin return Map(OnCodex(api => api.GetDebugInfoAsync())); } - public CodexDebugPeerResponse GetDebugPeer(string peerId) + public DebugPeer GetDebugPeer(string peerId) { - var http = Http(); - var str = http.HttpGetString($"debug/peer/{peerId}"); + // Cannot use openAPI: debug/peer endpoint is not specified there. + var endoint = GetEndpoint(); + var str = endoint.HttpGetString($"debug/peer/{peerId}"); if (str.ToLowerInvariant() == "unable to find peer!") { - return new CodexDebugPeerResponse + return new DebugPeer { IsPeerFound = false }; } - var result = http.Deserialize(str); + var result = endoint.Deserialize(str); result.IsPeerFound = true; return result; } @@ -117,7 +118,8 @@ namespace CodexPlugin private T OnCodex(Func> action) { var address = GetAddress(); - var result = tools.CreateHttp().OnClient(client => + var result = tools.CreateHttp(CheckContainerCrashed) + .OnClient(client => { var api = new CodexApi(client); api.BaseUrl = $"{address.Host}:{address.Port}/api/codex/v1"; @@ -150,6 +152,13 @@ namespace CodexPlugin // return tools.CreateHttp(GetAddress(), baseUrl: "/api/codex/v1", CheckContainerCrashed, new LongTimeSet(), Container.Name); //} + private IEndpoint GetEndpoint() + { + return tools + .CreateHttp(CheckContainerCrashed) + .CreateEndpoint(GetAddress(), "/api/codex/v1/", Container.Name); + } + private Address GetAddress() { return Container.GetAddress(tools.GetLog(), CodexContainerRecipe.ApiPortTag); diff --git a/ProjectPlugins/CodexPlugin/CodexTypes.cs b/ProjectPlugins/CodexPlugin/CodexTypes.cs index 1226a2f..06dfc86 100644 --- a/ProjectPlugins/CodexPlugin/CodexTypes.cs +++ b/ProjectPlugins/CodexPlugin/CodexTypes.cs @@ -46,7 +46,8 @@ namespace CodexPlugin public class DebugPeer { public bool IsPeerFound { get; set; } - + public string PeerId { get; set; } = string.Empty; + public string[] Addresses { get; set; } = Array.Empty(); } public class LocalDataset diff --git a/Tests/CodexTests/Helpers/FullConnectivityHelper.cs b/Tests/CodexTests/Helpers/FullConnectivityHelper.cs index 8db17ed..58474d1 100644 --- a/Tests/CodexTests/Helpers/FullConnectivityHelper.cs +++ b/Tests/CodexTests/Helpers/FullConnectivityHelper.cs @@ -31,6 +31,8 @@ namespace CodexTests.Helpers private void AssertFullyConnected(ICodexNode[] nodes) { Log($"Asserting '{implementation.Description()}' for nodes: '{string.Join(",", nodes.Select(n => n.GetName()))}'..."); + Assert.That(nodes.Length, Is.GreaterThan(1)); + var entries = CreateEntries(nodes); var pairs = CreatePairs(entries); diff --git a/Tests/CodexTests/Helpers/PeerConnectionTestHelpers.cs b/Tests/CodexTests/Helpers/PeerConnectionTestHelpers.cs index d644296..f0261a7 100644 --- a/Tests/CodexTests/Helpers/PeerConnectionTestHelpers.cs +++ b/Tests/CodexTests/Helpers/PeerConnectionTestHelpers.cs @@ -26,12 +26,12 @@ namespace CodexTests.Helpers public string ValidateEntry(Entry entry, Entry[] allEntries) { var result = string.Empty; - foreach (var peer in entry.Response.table.nodes) + foreach (var peer in entry.Response.Table.Nodes) { var expected = GetExpectedDiscoveryEndpoint(allEntries, peer); - if (expected != peer.address) + if (expected != peer.Address) { - result += $"Node:{entry.Node.GetName()} has incorrect peer table entry. Was: '{peer.address}', expected: '{expected}'. "; + result += $"Node:{entry.Node.GetName()} has incorrect peer table entry. Was: '{peer.Address}', expected: '{expected}'. "; } } return result; @@ -39,24 +39,24 @@ namespace CodexTests.Helpers public PeerConnectionState Check(Entry from, Entry to) { - var peerId = to.Response.id; + var peerId = to.Response.Id; var response = from.Node.GetDebugPeer(peerId); if (!response.IsPeerFound) { return PeerConnectionState.NoConnection; } - if (!string.IsNullOrEmpty(response.peerId) && response.addresses.Any()) + if (!string.IsNullOrEmpty(response.PeerId) && response.Addresses.Any()) { return PeerConnectionState.Connection; } return PeerConnectionState.Unknown; } - private static string GetExpectedDiscoveryEndpoint(Entry[] allEntries, CodexDebugTableNodeResponse node) + private static string GetExpectedDiscoveryEndpoint(Entry[] allEntries, DebugInfoTableNode node) { - var peer = allEntries.SingleOrDefault(e => e.Response.table.localNode.peerId == node.peerId); - if (peer == null) return $"peerId: {node.peerId} is not known."; + var peer = allEntries.SingleOrDefault(e => e.Response.Table.LocalNode.PeerId == node.PeerId); + if (peer == null) return $"peerId: {node.PeerId} is not known."; var container = peer.Node.Container; var podInfo = peer.Node.GetPodInfo(); diff --git a/Tests/CodexTests/PeerDiscoveryTests/LayeredDiscoveryTests.cs b/Tests/CodexTests/PeerDiscoveryTests/LayeredDiscoveryTests.cs index 386c3d8..00c0cda 100644 --- a/Tests/CodexTests/PeerDiscoveryTests/LayeredDiscoveryTests.cs +++ b/Tests/CodexTests/PeerDiscoveryTests/LayeredDiscoveryTests.cs @@ -1,5 +1,4 @@ -using CodexPlugin; -using NUnit.Framework; +using NUnit.Framework; namespace CodexTests.PeerDiscoveryTests { @@ -9,10 +8,10 @@ namespace CodexTests.PeerDiscoveryTests [Test] public void TwoLayersTest() { - var root = Ci.StartCodexNode(); - var l1Source = Ci.StartCodexNode(s => s.WithBootstrapNode(root)); - var l1Node = Ci.StartCodexNode(s => s.WithBootstrapNode(root)); - var l2Target = Ci.StartCodexNode(s => s.WithBootstrapNode(l1Node)); + var root = AddCodex(); + var l1Source = AddCodex(s => s.WithBootstrapNode(root)); + var l1Node = AddCodex(s => s.WithBootstrapNode(root)); + var l2Target = AddCodex(s => s.WithBootstrapNode(l1Node)); AssertAllNodesConnected(); } @@ -20,11 +19,11 @@ namespace CodexTests.PeerDiscoveryTests [Test] public void ThreeLayersTest() { - var root = Ci.StartCodexNode(); - var l1Source = Ci.StartCodexNode(s => s.WithBootstrapNode(root)); - var l1Node = Ci.StartCodexNode(s => s.WithBootstrapNode(root)); - var l2Node = Ci.StartCodexNode(s => s.WithBootstrapNode(l1Node)); - var l3Target = Ci.StartCodexNode(s => s.WithBootstrapNode(l2Node)); + var root = AddCodex(); + var l1Source = AddCodex(s => s.WithBootstrapNode(root)); + var l1Node = AddCodex(s => s.WithBootstrapNode(root)); + var l2Node = AddCodex(s => s.WithBootstrapNode(l1Node)); + var l3Target = AddCodex(s => s.WithBootstrapNode(l2Node)); AssertAllNodesConnected(); } @@ -34,10 +33,10 @@ namespace CodexTests.PeerDiscoveryTests [TestCase(10)] public void NodeChainTest(int chainLength) { - var node = Ci.StartCodexNode(); + var node = AddCodex(); for (var i = 1; i < chainLength; i++) { - node = Ci.StartCodexNode(s => s.WithBootstrapNode(node)); + node = AddCodex(s => s.WithBootstrapNode(node)); } AssertAllNodesConnected();