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

72 lines
2.0 KiB
C#
Raw Normal View History

2023-03-21 07:23:15 +00:00
using NUnit.Framework;
namespace CodexDistTests.TestCore
{
public interface IOnlineCodexNode
{
CodexDebugResponse GetDebugInfo();
2023-03-21 07:23:15 +00:00
ContentId UploadFile(TestFile file);
TestFile? DownloadContent(ContentId contentId);
2023-03-19 10:40:05 +00:00
IOfflineCodexNode BringOffline();
}
public class OnlineCodexNode : IOnlineCodexNode
{
2023-03-19 10:40:05 +00:00
private readonly IK8sManager k8SManager;
private readonly IFileManager fileManager;
private readonly int port;
2023-03-19 10:40:05 +00:00
public OnlineCodexNode(IK8sManager k8SManager, IFileManager fileManager, int port)
{
2023-03-19 10:40:05 +00:00
this.k8SManager = k8SManager;
this.fileManager = fileManager;
this.port = port;
}
2023-03-19 10:40:05 +00:00
public IOfflineCodexNode BringOffline()
{
return k8SManager.BringOffline(this);
}
public CodexDebugResponse GetDebugInfo()
{
2023-03-21 07:23:15 +00:00
return Http().HttpGetJson<CodexDebugResponse>("debug/info");
}
2023-03-21 07:23:15 +00:00
public ContentId UploadFile(TestFile file)
{
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-21 07:23:15 +00:00
return new ContentId(response);
}
public TestFile? DownloadContent(ContentId contentId)
{
var file = fileManager.CreateEmptyTestFile();
2023-03-21 07:23:15 +00:00
using var fileStream = File.OpenWrite(file.Filename);
using var downloadStream = Http().HttpGetStream("download/" + contentId.Id);
downloadStream.CopyTo(fileStream);
return file;
}
2023-03-21 07:23:15 +00:00
private Http Http()
{
2023-03-21 07:23:15 +00:00
return new Http(ip: "127.0.0.1", port: port, baseUrl: "/api/codex/v1");
}
}
public class ContentId
{
public ContentId(string id)
{
Id = id;
}
public string Id { get; }
}
}