2023-03-17 10:43:29 +00:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
|
|
|
|
|
namespace CodexDistTests.TestCore
|
|
|
|
|
{
|
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();
|
|
|
|
|
ContentId UploadFile(TestFile file, int retryCounter = 0);
|
|
|
|
|
TestFile? DownloadContent(ContentId contentId);
|
2023-03-19 10:40:05 +00:00
|
|
|
|
IOfflineCodexNode BringOffline();
|
2023-03-19 09:49:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class OnlineCodexNode : IOnlineCodexNode
|
|
|
|
|
{
|
2023-03-19 10:40:05 +00:00
|
|
|
|
private readonly IK8sManager k8SManager;
|
2023-03-19 09:49:03 +00:00
|
|
|
|
private readonly IFileManager fileManager;
|
2023-03-17 10:43:29 +00:00
|
|
|
|
private readonly int port;
|
|
|
|
|
|
2023-03-19 10:40:05 +00:00
|
|
|
|
public OnlineCodexNode(IK8sManager k8SManager, IFileManager fileManager, int port)
|
2023-03-17 10:43:29 +00:00
|
|
|
|
{
|
2023-03-19 10:40:05 +00:00
|
|
|
|
this.k8SManager = k8SManager;
|
2023-03-19 09:49:03 +00:00
|
|
|
|
this.fileManager = fileManager;
|
2023-03-17 10:43:29 +00:00
|
|
|
|
this.port = port;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-19 10:40:05 +00:00
|
|
|
|
public IOfflineCodexNode BringOffline()
|
|
|
|
|
{
|
|
|
|
|
return k8SManager.BringOffline(this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-17 10:43:29 +00:00
|
|
|
|
public CodexDebugResponse GetDebugInfo()
|
|
|
|
|
{
|
|
|
|
|
return HttpGet<CodexDebugResponse>("debug/info");
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-19 09:49:03 +00:00
|
|
|
|
public ContentId UploadFile(TestFile file, int retryCounter = 0)
|
2023-03-17 10:43:29 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var url = $"http://127.0.0.1:{port}/api/codex/v1/upload";
|
|
|
|
|
using var client = GetClient();
|
|
|
|
|
|
2023-03-19 09:49:03 +00:00
|
|
|
|
// Todo: If the file is too large to read into memory, we'll need to rewrite this upload POST to be streaming.
|
|
|
|
|
var byteData = File.ReadAllBytes(file.Filename);
|
2023-03-17 10:43:29 +00:00
|
|
|
|
using var content = new ByteArrayContent(byteData);
|
|
|
|
|
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
|
|
|
|
|
var response = Utils.Wait(client.PostAsync(url, content));
|
|
|
|
|
|
|
|
|
|
var contentId = Utils.Wait(response.Content.ReadAsStringAsync());
|
2023-03-20 08:23:32 +00:00
|
|
|
|
if (contentId.StartsWith("Unable to store block"))
|
|
|
|
|
{
|
|
|
|
|
retryCounter = Timing.HttpCallRetryCount() + 1;
|
|
|
|
|
Assert.Fail("Node failed to store block.");
|
|
|
|
|
}
|
2023-03-19 09:49:03 +00:00
|
|
|
|
return new ContentId(contentId);
|
2023-03-17 10:43:29 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
2023-03-19 10:40:05 +00:00
|
|
|
|
if (retryCounter > Timing.HttpCallRetryCount())
|
2023-03-17 10:43:29 +00:00
|
|
|
|
{
|
|
|
|
|
Assert.Fail(exception.Message);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Timing.RetryDelay();
|
2023-03-19 09:49:03 +00:00
|
|
|
|
return UploadFile(file, retryCounter + 1);
|
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-19 09:49:03 +00:00
|
|
|
|
// Todo: If the file is too large, rewrite to streaming:
|
|
|
|
|
var bytes = HttpGetBytes("download/" + contentId.Id);
|
|
|
|
|
if (bytes == null) return null;
|
|
|
|
|
|
|
|
|
|
var file = fileManager.CreateEmptyTestFile();
|
|
|
|
|
File.WriteAllBytes(file.Filename, bytes);
|
|
|
|
|
return file;
|
2023-03-17 10:43:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private byte[]? HttpGetBytes(string endpoint, int retryCounter = 0)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var client = GetClient();
|
|
|
|
|
var url = $"http://127.0.0.1:{port}/api/codex/v1/" + endpoint;
|
|
|
|
|
var result = Utils.Wait(client.GetAsync(url));
|
|
|
|
|
return Utils.Wait(result.Content.ReadAsByteArrayAsync());
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
2023-03-19 10:40:05 +00:00
|
|
|
|
if (retryCounter > Timing.HttpCallRetryCount())
|
2023-03-17 10:43:29 +00:00
|
|
|
|
{
|
|
|
|
|
Assert.Fail(exception.Message);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Timing.RetryDelay();
|
|
|
|
|
return HttpGetBytes(endpoint, retryCounter + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private T HttpGet<T>(string endpoint, int retryCounter = 0)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var client = GetClient();
|
|
|
|
|
var url = $"http://127.0.0.1:{port}/api/codex/v1/" + endpoint;
|
|
|
|
|
var result = Utils.Wait(client.GetAsync(url));
|
|
|
|
|
var json = Utils.Wait(result.Content.ReadAsStringAsync());
|
|
|
|
|
return JsonConvert.DeserializeObject<T>(json);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
2023-03-19 10:40:05 +00:00
|
|
|
|
if (retryCounter > Timing.HttpCallRetryCount())
|
2023-03-17 10:43:29 +00:00
|
|
|
|
{
|
|
|
|
|
Assert.Fail(exception.Message);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Timing.RetryDelay();
|
|
|
|
|
return HttpGet<T>(endpoint, retryCounter + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private HttpClient GetClient()
|
|
|
|
|
{
|
|
|
|
|
var client = new HttpClient();
|
|
|
|
|
client.Timeout = Timing.HttpCallTimeout();
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class CodexDebugResponse
|
|
|
|
|
{
|
|
|
|
|
public string id { get; set; } = string.Empty;
|
|
|
|
|
public string[] addrs { get; set; } = new string[0];
|
|
|
|
|
public string repo { get; set; } = string.Empty;
|
|
|
|
|
public string spr { get; set; } = string.Empty;
|
|
|
|
|
public CodexDebugVersionResponse codex { get; set; } = new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class CodexDebugVersionResponse
|
|
|
|
|
{
|
|
|
|
|
public string version { get; set; } = string.Empty;
|
|
|
|
|
public string revision { get; set; } = string.Empty;
|
|
|
|
|
}
|
2023-03-19 09:49:03 +00:00
|
|
|
|
|
|
|
|
|
public class ContentId
|
|
|
|
|
{
|
|
|
|
|
public ContentId(string id)
|
|
|
|
|
{
|
|
|
|
|
Id = id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Id { get; }
|
|
|
|
|
}
|
2023-03-17 10:43:29 +00:00
|
|
|
|
}
|