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

83 lines
2.5 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);
}
public class OnlineCodexNode : IOnlineCodexNode
{
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");
Log("Got DebugInfo with id: " + response.id);
return response;
}
2023-03-21 07:23:15 +00:00
public ContentId UploadFile(TestFile file)
{
2023-03-22 08:22:18 +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);
if (response.StartsWith("Unable to store block"))
{
2023-03-21 07:23:15 +00:00
Assert.Fail("Node failed to store block.");
}
2023-03-22 08:22:18 +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 08:50:24 +00:00
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}");
return file;
}
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)
{
log.Log($"Node {Container.Name}: {msg}");
}
}
public class ContentId
{
public ContentId(string id)
{
Id = id;
}
2023-03-21 12:20:21 +00:00
public string Id { get; }
}
}