From e9168dc1686eb4351ef4e9c5cf1807736bbecfe2 Mon Sep 17 00:00:00 2001 From: benbierens Date: Wed, 22 Mar 2023 11:27:51 +0100 Subject: [PATCH] Implements two-client-one-pod test --- CodexDistTestCore/Http.cs | 10 +++++-- CodexDistTestCore/OnlineCodexNode.cs | 41 +++++++++++++++++++++++----- Tests/BasicTests/SimpleTests.cs | 38 +++++++++++++------------- 3 files changed, 60 insertions(+), 29 deletions(-) diff --git a/CodexDistTestCore/Http.cs b/CodexDistTestCore/Http.cs index 1cff3c8..539877e 100644 --- a/CodexDistTestCore/Http.cs +++ b/CodexDistTestCore/Http.cs @@ -20,18 +20,22 @@ namespace CodexDistTestCore if (!this.baseUrl.EndsWith("/")) this.baseUrl += "/"; } - public T HttpGetJson(string route) + public string HttpGetString(string route) { return Retry(() => { using var client = GetClient(); var url = GetUrl() + route; var result = Utils.Wait(client.GetAsync(url)); - var json = Utils.Wait(result.Content.ReadAsStringAsync()); - return JsonConvert.DeserializeObject(json)!; + return Utils.Wait(result.Content.ReadAsStringAsync()); }); } + public T HttpGetJson(string route) + { + return JsonConvert.DeserializeObject(HttpGetString(route))!; + } + public string HttpPostStream(string route, Stream stream) { return Retry(() => diff --git a/CodexDistTestCore/OnlineCodexNode.cs b/CodexDistTestCore/OnlineCodexNode.cs index d8b1f6d..55210e7 100644 --- a/CodexDistTestCore/OnlineCodexNode.cs +++ b/CodexDistTestCore/OnlineCodexNode.cs @@ -7,10 +7,14 @@ namespace CodexDistTestCore CodexDebugResponse GetDebugInfo(); ContentId UploadFile(TestFile file); TestFile? DownloadContent(ContentId contentId); + void ConnectToPeer(IOnlineCodexNode node); } public class OnlineCodexNode : IOnlineCodexNode { + private const string SuccessfullyConnectedMessage = "Successfully connected to peer"; + private const string UploadFailedMessage = "Unable to store block"; + private readonly TestLog log; private readonly IFileManager fileManager; @@ -26,32 +30,55 @@ namespace CodexDistTestCore public CodexDebugResponse GetDebugInfo() { var response = Http().HttpGetJson("debug/info"); - Log("Got DebugInfo with id: " + response.id); + Log($"Got DebugInfo with id: {response.id}."); return response; } public ContentId UploadFile(TestFile file) { - Log($"Uploading file of size {file.GetFileSize()}"); + Log($"Uploading file of size {file.GetFileSize()}..."); using var fileStream = File.OpenRead(file.Filename); var response = Http().HttpPostStream("upload", fileStream); - if (response.StartsWith("Unable to store block")) + if (response.StartsWith(UploadFailedMessage)) { Assert.Fail("Node failed to store block."); } - Log($"Uploaded file. Received contentId: {response}"); + Log($"Uploaded file. Received contentId: {response}."); return new ContentId(response); } public TestFile? DownloadContent(ContentId contentId) { - Log($"Downloading for contentId: {contentId.Id}"); + Log($"Downloading for contentId: {contentId.Id}..."); var file = fileManager.CreateEmptyTestFile(); DownloadToFile(contentId.Id, file); - Log($"Downloaded file of size {file.GetFileSize()} to {file.Filename}"); + Log($"Downloaded file of size {file.GetFileSize()} to {file.Filename}."); return file; } + public void ConnectToPeer(IOnlineCodexNode node) + { + var peer = (OnlineCodexNode)node; + + Log($"Connecting to peer <{peer.Container.Name}>..."); + var peerInfo = node.GetDebugInfo(); + var peerId = peerInfo.id; + var peerMultiAddress = GetPeerMultiAddress(peer, peerInfo); + + var response = Http().HttpGetString($"connect/{peerId}?addrs={peerMultiAddress}"); + + Assert.That(response, Is.EqualTo(SuccessfullyConnectedMessage), "Unable to connect codex nodes."); + Log($"Successfully connected to peer <{peer.Container.Name}>."); + } + + private string GetPeerMultiAddress(OnlineCodexNode peer, CodexDebugResponse peerInfo) + { + // Todo: If peer is in a different pod, we must replace 0.0.0.0 with the address of that pod! + + return peerInfo.addrs.First(); + // Todo: Is there a case where First address in list is not the way? + } + private void DownloadToFile(string contentId, TestFile file) { using var fileStream = File.OpenWrite(file.Filename); @@ -66,7 +93,7 @@ namespace CodexDistTestCore private void Log(string msg) { - log.Log($"Node {Container.Name}: {msg}"); + log.Log($"<{Container.Name}>: {msg}"); } } diff --git a/Tests/BasicTests/SimpleTests.cs b/Tests/BasicTests/SimpleTests.cs index 02fcecf..d780086 100644 --- a/Tests/BasicTests/SimpleTests.cs +++ b/Tests/BasicTests/SimpleTests.cs @@ -23,9 +23,9 @@ namespace Tests.BasicTests public void OneClientTest() { var primary = SetupCodexNodes(1) - .WithLogLevel(CodexLogLevel.Trace) - .WithStorageQuota(2.MB()) - .BringOnline()[0]; + .WithLogLevel(CodexLogLevel.Trace) + .WithStorageQuota(2.MB()) + .BringOnline()[0]; var testFile = GenerateTestFile(1.MB()); @@ -36,26 +36,26 @@ namespace Tests.BasicTests testFile.AssertIsEqual(downloadedFile); } - //[Test] - //public void TwoClientTest() - //{ - // var primary = SetupCodexNode() - // .WithLogLevel(CodexLogLevel.Trace) - // .WithStorageQuota(1024 * 1024 * 2) - // .BringOnline(); + [Test] + public void TwoClientOnePodTest() + { + var group = SetupCodexNodes(2) + .WithLogLevel(CodexLogLevel.Trace) + .WithStorageQuota(2.MB()) + .BringOnline(); - // var secondary = SetupCodexNode() - // .WithLogLevel(CodexLogLevel.Trace) - // .WithBootstrapNode(primary) - // .BringOnline(); + var primary = group[0]; + var secondary = group[1]; - // var testFile = GenerateTestFile(1024 * 1024); + primary.ConnectToPeer(secondary); - // var contentId = primary.UploadFile(testFile); + var testFile = GenerateTestFile(1.MB()); - // var downloadedFile = secondary.DownloadContent(contentId); + var contentId = primary.UploadFile(testFile); - // testFile.AssertIsEqual(downloadedFile); - //} + var downloadedFile = secondary.DownloadContent(contentId); + + testFile.AssertIsEqual(downloadedFile); + } } }