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

177 lines
6.4 KiB
C#
Raw Normal View History

using DistTestCore.Logs;
using FileUtils;
using Logging;
2023-04-13 07:33:10 +00:00
using NUnit.Framework;
2023-08-02 13:11:27 +00:00
using Utils;
2023-04-13 07:33:10 +00:00
namespace CodexPlugin
2023-04-13 07:33:10 +00:00
{
public interface IOnlineCodexNode
{
string GetName();
2023-04-13 07:33:10 +00:00
CodexDebugResponse GetDebugInfo();
2023-05-11 10:44:53 +00:00
CodexDebugPeerResponse GetDebugPeer(string peerId);
2023-04-13 07:33:10 +00:00
ContentId UploadFile(TestFile file);
TestFile? DownloadContent(ContentId contentId, string fileLabel = "");
2023-04-13 07:33:10 +00:00
void ConnectToPeer(IOnlineCodexNode node);
2023-08-16 14:13:29 +00:00
IDownloadedLog DownloadLog(int? tailLines = null);
//IMetricsAccess Metrics { get; }
//IMarketplaceAccess Marketplace { get; }
2023-07-31 09:51:29 +00:00
CodexDebugVersionResponse Version { get; }
2023-04-25 10:52:11 +00:00
ICodexSetup BringOffline();
2023-04-13 07:33:10 +00:00
}
public class OnlineCodexNode : IOnlineCodexNode
{
private const string SuccessfullyConnectedMessage = "Successfully connected to peer";
private const string UploadFailedMessage = "Unable to store block";
//private readonly TestLifecycle lifecycle;
2023-04-13 07:33:10 +00:00
public OnlineCodexNode(/*TestLifecycle lifecycle, */CodexAccess codexAccess, CodexNodeGroup group/*, IMetricsAccess metricsAccess, IMarketplaceAccess marketplaceAccess*/)
2023-04-13 07:33:10 +00:00
{
//this.lifecycle = lifecycle;
2023-04-13 07:33:10 +00:00
CodexAccess = codexAccess;
Group = group;
//Metrics = metricsAccess;
//Marketplace = marketplaceAccess;
2023-07-31 09:51:29 +00:00
Version = new CodexDebugVersionResponse();
2023-04-13 07:33:10 +00:00
}
public CodexAccess CodexAccess { get; }
public CodexNodeGroup Group { get; }
//public IMetricsAccess Metrics { get; }
//public IMarketplaceAccess Marketplace { get; }
2023-07-31 09:51:29 +00:00
public CodexDebugVersionResponse Version { get; private set; }
2023-04-13 07:33:10 +00:00
public string GetName()
{
return CodexAccess.Container.Name;
2023-04-13 07:33:10 +00:00
}
public CodexDebugResponse GetDebugInfo()
{
2023-06-29 14:07:49 +00:00
var debugInfo = CodexAccess.GetDebugInfo();
var known = string.Join(",", debugInfo.table.nodes.Select(n => n.peerId));
Log($"Got DebugInfo with id: '{debugInfo.id}'. This node knows: {known}");
2023-04-19 12:57:00 +00:00
return debugInfo;
2023-04-13 07:33:10 +00:00
}
2023-05-11 10:44:53 +00:00
public CodexDebugPeerResponse GetDebugPeer(string peerId)
{
2023-06-29 14:07:49 +00:00
return CodexAccess.GetDebugPeer(peerId);
2023-05-11 10:44:53 +00:00
}
2023-04-13 07:33:10 +00:00
public ContentId UploadFile(TestFile file)
{
//using var fileStream = File.OpenRead(file.Filename);
//var logMessage = $"Uploading file {file.Describe()}...";
//Log(logMessage);
//var response = Stopwatch.Measure(lifecycle.Log, logMessage, () =>
//{
// return CodexAccess.UploadFile(fileStream);
//});
//if (string.IsNullOrEmpty(response)) Assert.Fail("Received empty response.");
//if (response.StartsWith(UploadFailedMessage)) Assert.Fail("Node failed to store block.");
2023-07-11 06:19:14 +00:00
//Log($"Uploaded file. Received contentId: '{response}'.");
//return new ContentId(response);
return null!;
2023-04-13 07:33:10 +00:00
}
public TestFile? DownloadContent(ContentId contentId, string fileLabel = "")
2023-04-13 07:33:10 +00:00
{
//var logMessage = $"Downloading for contentId: '{contentId.Id}'...";
//Log(logMessage);
//var file = lifecycle.FileManager.CreateEmptyTestFile(fileLabel);
//Stopwatch.Measure(lifecycle.Log, logMessage, () => DownloadToFile(contentId.Id, file));
//Log($"Downloaded file {file.Describe()} to '{file.Filename}'.");
//return file;
return null!;
2023-04-13 07:33:10 +00:00
}
public void ConnectToPeer(IOnlineCodexNode node)
{
var peer = (OnlineCodexNode)node;
Log($"Connecting to peer {peer.GetName()}...");
var peerInfo = node.GetDebugInfo();
2023-06-29 14:07:49 +00:00
var response = CodexAccess.ConnectToPeer(peerInfo.id, GetPeerMultiAddress(peer, peerInfo));
2023-04-13 07:33:10 +00:00
Assert.That(response, Is.EqualTo(SuccessfullyConnectedMessage), "Unable to connect codex nodes.");
Log($"Successfully connected to peer {peer.GetName()}.");
}
2023-08-16 14:13:29 +00:00
public IDownloadedLog DownloadLog(int? tailLines = null)
2023-04-13 09:30:19 +00:00
{
return null!; // lifecycle.DownloadLog(CodexAccess.Container, tailLines);
2023-04-13 09:30:19 +00:00
}
2023-04-13 07:33:10 +00:00
2023-04-25 10:52:11 +00:00
public ICodexSetup BringOffline()
{
if (Group.Count() > 1) throw new InvalidOperationException("Codex-nodes that are part of a group cannot be " +
"individually shut down. Use 'BringOffline()' on the group object to stop the group. This method is only " +
"available for codex-nodes in groups of 1.");
2023-04-25 10:52:11 +00:00
return Group.BringOffline();
}
2023-07-31 09:51:29 +00:00
public void EnsureOnlineGetVersionResponse()
{
2023-08-02 13:11:27 +00:00
var debugInfo = Time.Retry(CodexAccess.GetDebugInfo, "ensure online");
2023-07-31 09:51:29 +00:00
var nodePeerId = debugInfo.id;
var nodeName = CodexAccess.Container.Name;
if (!debugInfo.codex.IsValid())
{
throw new Exception($"Invalid version information received from Codex node {GetName()}: {debugInfo.codex}");
}
//lifecycle.Log.AddStringReplace(nodePeerId, nodeName);
//lifecycle.Log.AddStringReplace(debugInfo.table.localNode.nodeId, nodeName);
2023-07-31 09:51:29 +00:00
Version = debugInfo.codex;
}
2023-04-13 07:33:10 +00:00
private string GetPeerMultiAddress(OnlineCodexNode peer, CodexDebugResponse peerInfo)
{
var multiAddress = peerInfo.addrs.First();
// Todo: Is there a case where First address in list is not the way?
// 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.CodexAccess.Container.Pod.PodInfo.Ip);
2023-04-13 07:33:10 +00:00
}
private void DownloadToFile(string contentId, TestFile file)
{
using var fileStream = File.OpenWrite(file.Filename);
2023-04-19 12:57:00 +00:00
try
{
2023-06-29 14:07:49 +00:00
using var downloadStream = CodexAccess.DownloadFile(contentId);
2023-04-19 12:57:00 +00:00
downloadStream.CopyTo(fileStream);
}
catch
{
Log($"Failed to download file '{contentId}'.");
throw;
}
2023-04-13 07:33:10 +00:00
}
private void Log(string msg)
{
//lifecycle.Log.Log($"{GetName()}: {msg}");
2023-04-13 07:33:10 +00:00
}
}
public class ContentId
{
public ContentId(string id)
{
Id = id;
}
public string Id { get; }
}
}