cs-codex-dist-tests/CodexDistTestCore/OnlineCodexNode.cs

110 lines
3.6 KiB
C#
Raw Normal View History

2023-03-21 07:23:15 +00:00
using NUnit.Framework;
2023-03-21 12:20:21 +00:00
namespace CodexDistTestCore
{
public interface IOnlineCodexNode
{
CodexDebugResponse GetDebugInfo();
2023-03-21 07:23:15 +00:00
ContentId UploadFile(TestFile file);
TestFile? DownloadContent(ContentId contentId);
2023-03-22 10:27:51 +00:00
void ConnectToPeer(IOnlineCodexNode node);
}
public class OnlineCodexNode : IOnlineCodexNode
{
2023-03-22 10:27:51 +00:00
private const string SuccessfullyConnectedMessage = "Successfully connected to peer";
private const string UploadFailedMessage = "Unable to store block";
2023-03-22 08:22:18 +00:00
private readonly TestLog log;
private readonly IFileManager fileManager;
2023-03-22 08:22:18 +00:00
public OnlineCodexNode(TestLog log, IFileManager fileManager, CodexNodeContainer container)
{
2023-03-22 08:22:18 +00:00
this.log = log;
this.fileManager = fileManager;
2023-03-22 08:22:18 +00:00
Container = container;
2023-03-19 10:40:05 +00:00
}
2023-03-21 14:44:21 +00:00
public CodexNodeContainer Container { get; }
public CodexDebugResponse GetDebugInfo()
{
2023-03-22 08:22:18 +00:00
var response = Http().HttpGetJson<CodexDebugResponse>("debug/info");
2023-03-22 10:27:51 +00:00
Log($"Got DebugInfo with id: {response.id}.");
2023-03-22 08:22:18 +00:00
return response;
}
2023-03-21 07:23:15 +00:00
public ContentId UploadFile(TestFile file)
{
2023-03-22 10:27:51 +00:00
Log($"Uploading file of size {file.GetFileSize()}...");
2023-03-21 07:23:15 +00:00
using var fileStream = File.OpenRead(file.Filename);
var response = Http().HttpPostStream("upload", fileStream);
2023-03-22 10:27:51 +00:00
if (response.StartsWith(UploadFailedMessage))
{
2023-03-21 07:23:15 +00:00
Assert.Fail("Node failed to store block.");
}
2023-03-22 10:27:51 +00:00
Log($"Uploaded file. Received contentId: {response}.");
2023-03-21 07:23:15 +00:00
return new ContentId(response);
}
public TestFile? DownloadContent(ContentId contentId)
{
2023-03-22 10:27:51 +00:00
Log($"Downloading for contentId: {contentId.Id}...");
var file = fileManager.CreateEmptyTestFile();
2023-03-22 08:50:24 +00:00
DownloadToFile(contentId.Id, file);
2023-03-22 10:27:51 +00:00
Log($"Downloaded file of size {file.GetFileSize()} to {file.Filename}.");
2023-03-22 08:50:24 +00:00
return file;
}
2023-03-22 10:27:51 +00:00
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?
}
2023-03-22 08:50:24 +00:00
private void DownloadToFile(string contentId, TestFile file)
{
2023-03-21 07:23:15 +00:00
using var fileStream = File.OpenWrite(file.Filename);
2023-03-22 08:50:24 +00:00
using var downloadStream = Http().HttpGetStream("download/" + contentId);
2023-03-21 07:23:15 +00:00
downloadStream.CopyTo(fileStream);
}
2023-03-21 07:23:15 +00:00
private Http Http()
{
2023-03-21 14:44:21 +00:00
return new Http(ip: "127.0.0.1", port: Container.ServicePort, baseUrl: "/api/codex/v1");
}
2023-03-22 08:22:18 +00:00
private void Log(string msg)
{
2023-03-22 10:27:51 +00:00
log.Log($"<{Container.Name}>: {msg}");
2023-03-22 08:22:18 +00:00
}
}
public class ContentId
{
public ContentId(string id)
{
Id = id;
}
2023-03-21 12:20:21 +00:00
public string Id { get; }
}
}