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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class OnlineCodexNode : IOnlineCodexNode
|
|
|
|
|
{
|
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-17 10:43:29 +00:00
|
|
|
|
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-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 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-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 08:22:18 +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 08:50:24 +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);
|
|
|
|
|
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-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)
|
|
|
|
|
{
|
|
|
|
|
log.Log($"Node {Container.Name}: {msg}");
|
|
|
|
|
}
|
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
|
|
|
|
}
|