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

136 lines
4.4 KiB
C#
Raw Normal View History

using CodexDistTestCore.Config;
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);
CodexNodeLog DownloadLog();
}
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-26 07:41:46 +00:00
private readonly K8sCluster k8sCluster = new K8sCluster();
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; }
2023-03-22 13:49:01 +00:00
public CodexNodeGroup Group { get; internal set; } = null!;
2023-03-21 14:44:21 +00:00
public string GetName()
{
return $"<{Container.Name}>";
}
public CodexDebugResponse GetDebugInfo()
{
2023-03-22 08:22:18 +00:00
var response = Http().HttpGetJson<CodexDebugResponse>("debug/info");
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.");
}
Log($"Uploaded file. Received contentId: '{response}'.");
2023-03-21 07:23:15 +00:00
return new ContentId(response);
}
public TestFile? DownloadContent(ContentId contentId)
{
Log($"Downloading for contentId: '{contentId.Id}'...");
var file = fileManager.CreateEmptyTestFile();
2023-03-22 08:50:24 +00:00
DownloadToFile(contentId.Id, file);
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.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.");
Log($"Successfully connected to peer {peer.GetName()}.");
2023-03-22 10:27:51 +00:00
}
public CodexNodeLog DownloadLog()
{
return Group.DownloadLog(this);
}
public string Describe()
{
return $"{Group.Describe()} contains {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-21 07:23:15 +00:00
private Http Http()
{
2023-03-26 07:41:46 +00:00
return new Http(ip: k8sCluster.GetIp(), port: Container.ServicePort, baseUrl: "/api/codex/v1");
}
2023-03-22 08:22:18 +00:00
private void Log(string msg)
{
log.Log($"{GetName()}: {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; }
}
}