2023-03-21 07:23:15 +00:00
|
|
|
|
using NUnit.Framework;
|
2023-03-17 10:43:29 +00:00
|
|
|
|
|
2023-03-21 12:20:21 +00:00
|
|
|
|
namespace CodexDistTestCore
|
2023-03-17 10:43:29 +00:00
|
|
|
|
{
|
2023-03-19 09:49:03 +00:00
|
|
|
|
public interface IOnlineCodexNode
|
2023-03-17 10:43:29 +00:00
|
|
|
|
{
|
2023-03-19 09:49:03 +00:00
|
|
|
|
CodexDebugResponse GetDebugInfo();
|
2023-03-21 07:23:15 +00:00
|
|
|
|
ContentId UploadFile(TestFile file);
|
2023-03-19 09:49:03 +00:00
|
|
|
|
TestFile? DownloadContent(ContentId contentId);
|
2023-03-22 10:27:51 +00:00
|
|
|
|
void ConnectToPeer(IOnlineCodexNode node);
|
2023-03-19 09:49:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2023-03-19 09:49:03 +00:00
|
|
|
|
private readonly IFileManager fileManager;
|
2023-03-17 10:43:29 +00:00
|
|
|
|
|
2023-03-22 08:22:18 +00:00
|
|
|
|
public OnlineCodexNode(TestLog log, IFileManager fileManager, CodexNodeContainer container)
|
2023-03-17 10:43:29 +00:00
|
|
|
|
{
|
2023-03-22 08:22:18 +00:00
|
|
|
|
this.log = log;
|
2023-03-19 09:49:03 +00:00
|
|
|
|
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; }
|
2023-03-22 13:49:01 +00:00
|
|
|
|
public CodexNodeGroup Group { get; internal set; } = null!;
|
2023-03-21 14:44:21 +00:00
|
|
|
|
|
2023-03-22 12:52:01 +00:00
|
|
|
|
public string GetName()
|
|
|
|
|
{
|
|
|
|
|
return $"<{Container.Name}>";
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-17 10:43:29 +00:00
|
|
|
|
public CodexDebugResponse GetDebugInfo()
|
|
|
|
|
{
|
2023-03-22 08:22:18 +00:00
|
|
|
|
var response = Http().HttpGetJson<CodexDebugResponse>("debug/info");
|
2023-03-22 12:52:01 +00:00
|
|
|
|
Log($"Got DebugInfo with id: '{response.id}'.");
|
2023-03-22 08:22:18 +00:00
|
|
|
|
return response;
|
2023-03-17 10:43:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 07:23:15 +00:00
|
|
|
|
public ContentId UploadFile(TestFile file)
|
2023-03-17 10:43:29 +00:00
|
|
|
|
{
|
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-17 10:43:29 +00:00
|
|
|
|
{
|
2023-03-21 07:23:15 +00:00
|
|
|
|
Assert.Fail("Node failed to store block.");
|
2023-03-17 10:43:29 +00:00
|
|
|
|
}
|
2023-03-22 12:52:01 +00:00
|
|
|
|
Log($"Uploaded file. Received contentId: '{response}'.");
|
2023-03-21 07:23:15 +00:00
|
|
|
|
return new ContentId(response);
|
2023-03-17 10:43:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-19 09:49:03 +00:00
|
|
|
|
public TestFile? DownloadContent(ContentId contentId)
|
2023-03-17 10:43:29 +00:00
|
|
|
|
{
|
2023-03-22 12:52:01 +00:00
|
|
|
|
Log($"Downloading for contentId: '{contentId.Id}'...");
|
2023-03-19 09:49:03 +00:00
|
|
|
|
var file = fileManager.CreateEmptyTestFile();
|
2023-03-22 08:50:24 +00:00
|
|
|
|
DownloadToFile(contentId.Id, file);
|
2023-03-22 12:52:01 +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;
|
|
|
|
|
|
2023-03-22 12:52:01 +00:00
|
|
|
|
Log($"Connecting to peer {peer.GetName()}...");
|
2023-03-22 10:27:51 +00:00
|
|
|
|
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.");
|
2023-03-22 12:52:01 +00:00
|
|
|
|
Log($"Successfully connected to peer {peer.GetName()}.");
|
2023-03-22 10:27:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetPeerMultiAddress(OnlineCodexNode peer, CodexDebugResponse peerInfo)
|
|
|
|
|
{
|
2023-03-22 13:49:01 +00:00
|
|
|
|
var multiAddress = peerInfo.addrs.First();
|
2023-03-22 10:27:51 +00:00
|
|
|
|
// Todo: Is there a case where First address in list is not the way?
|
2023-03-22 13:49:01 +00:00
|
|
|
|
|
|
|
|
|
if (Group == peer.Group)
|
|
|
|
|
{
|
|
|
|
|
return multiAddress;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The peer we want to connect is in a different pod.
|
|
|
|
|
// We must replace the default IP with the pod IP in the multiAddress.
|
|
|
|
|
return multiAddress.Replace("0.0.0.0", peer.Group.PodInfo!.Ip);
|
2023-03-22 10:27:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
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-17 10:43:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 07:23:15 +00:00
|
|
|
|
private Http Http()
|
2023-03-17 10:43:29 +00:00
|
|
|
|
{
|
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-17 10:43:29 +00:00
|
|
|
|
}
|
2023-03-22 08:22:18 +00:00
|
|
|
|
|
|
|
|
|
private void Log(string msg)
|
|
|
|
|
{
|
2023-03-22 12:52:01 +00:00
|
|
|
|
log.Log($"{GetName()}: {msg}");
|
2023-03-22 08:22:18 +00:00
|
|
|
|
}
|
2023-03-17 10:43:29 +00:00
|
|
|
|
}
|
2023-03-19 09:49:03 +00:00
|
|
|
|
|
|
|
|
|
public class ContentId
|
|
|
|
|
{
|
|
|
|
|
public ContentId(string id)
|
|
|
|
|
{
|
|
|
|
|
Id = id;
|
|
|
|
|
}
|
2023-03-21 12:20:21 +00:00
|
|
|
|
|
2023-03-19 09:49:03 +00:00
|
|
|
|
public string Id { get; }
|
|
|
|
|
}
|
2023-03-17 10:43:29 +00:00
|
|
|
|
}
|