2023-04-10 14:00:12 +02:00
|
|
|
|
using NUnit.Framework;
|
2023-04-10 14:48:16 +02:00
|
|
|
|
using System.Text;
|
2023-04-10 14:00:12 +02:00
|
|
|
|
|
|
|
|
|
namespace CodexDistTestCore.Marketplace
|
2023-04-10 10:09:41 +02:00
|
|
|
|
{
|
|
|
|
|
public class MarketplaceController
|
|
|
|
|
{
|
|
|
|
|
private readonly TestLog log;
|
|
|
|
|
private readonly K8sManager k8sManager;
|
2023-04-10 14:48:16 +02:00
|
|
|
|
private GethInfo? gethBootstrapNode;
|
2023-04-10 10:09:41 +02:00
|
|
|
|
private string bootstrapAccount = string.Empty;
|
|
|
|
|
private string bootstrapGenesisJson = string.Empty;
|
|
|
|
|
|
|
|
|
|
public MarketplaceController(TestLog log, K8sManager k8sManager)
|
|
|
|
|
{
|
|
|
|
|
this.log = log;
|
|
|
|
|
this.k8sManager = k8sManager;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 11:00:39 +02:00
|
|
|
|
public GethInfo BringOnlineMarketplace(OfflineCodexNodes offline)
|
2023-04-10 10:09:41 +02:00
|
|
|
|
{
|
2023-04-10 14:48:16 +02:00
|
|
|
|
if (gethBootstrapNode != null) return gethBootstrapNode;
|
2023-04-10 10:09:41 +02:00
|
|
|
|
|
|
|
|
|
log.Log("Starting Geth bootstrap node...");
|
|
|
|
|
gethBootstrapNode = k8sManager.BringOnlineGethBootstrapNode();
|
|
|
|
|
ExtractAccountAndGenesisJson();
|
2023-04-11 11:00:39 +02:00
|
|
|
|
log.Log($"Geth boothstrap node started. Initializing companions for {offline.NumberOfNodes} Codex nodes.");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-04-10 14:48:16 +02:00
|
|
|
|
|
|
|
|
|
return gethBootstrapNode;
|
2023-04-10 10:09:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 11:00:39 +02:00
|
|
|
|
|
|
|
|
|
private GethCompanionNodeContainer? CreateGethNodeContainer(OfflineCodexNodes offline, int n)
|
|
|
|
|
{
|
|
|
|
|
return new GethCompanionNodeContainer(
|
|
|
|
|
name: $"geth-node{n}",
|
|
|
|
|
servicePort: numberSource.GetNextServicePort(),
|
|
|
|
|
servicePortName: numberSource.GetNextServicePortName(),
|
|
|
|
|
apiPort: codexPortSource.GetNextNumber(),
|
|
|
|
|
rpcPort: codexPortSource.GetNextNumber(),
|
|
|
|
|
containerPortName: $"geth-{n}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 10:12:24 +02:00
|
|
|
|
public void AddToBalance(string account, int amount)
|
|
|
|
|
{
|
|
|
|
|
if (amount < 1 || string.IsNullOrEmpty(account)) Assert.Fail("Invalid arguments for AddToBalance");
|
|
|
|
|
|
|
|
|
|
// call the bootstrap node and convince it to give 'account' 'amount' tokens somehow.
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 10:09:41 +02:00
|
|
|
|
private void ExtractAccountAndGenesisJson()
|
|
|
|
|
{
|
2023-04-10 15:21:45 +02:00
|
|
|
|
FetchAccountAndGenesisJson();
|
|
|
|
|
if (string.IsNullOrEmpty(bootstrapAccount) || string.IsNullOrEmpty(bootstrapGenesisJson))
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(TimeSpan.FromSeconds(15));
|
|
|
|
|
FetchAccountAndGenesisJson();
|
|
|
|
|
}
|
2023-04-10 14:00:12 +02:00
|
|
|
|
|
2023-04-10 15:54:13 +02:00
|
|
|
|
Assert.That(bootstrapAccount, Is.Not.Empty, "Unable to fetch account for geth bootstrap node. Test infra failure.");
|
|
|
|
|
Assert.That(bootstrapGenesisJson, Is.Not.Empty, "Unable to fetch genesis-json for geth bootstrap node. Test infra failure.");
|
2023-04-10 14:48:16 +02:00
|
|
|
|
|
|
|
|
|
gethBootstrapNode!.GenesisJsonBase64 = Convert.ToBase64String(Encoding.ASCII.GetBytes(bootstrapGenesisJson));
|
2023-04-10 15:54:13 +02:00
|
|
|
|
|
|
|
|
|
log.Log($"Initialized geth bootstrap node with account '{bootstrapAccount}'");
|
2023-04-10 10:09:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 15:21:45 +02:00
|
|
|
|
private void FetchAccountAndGenesisJson()
|
|
|
|
|
{
|
2023-04-10 15:54:13 +02:00
|
|
|
|
bootstrapAccount = ExecuteCommand("cat", GethDockerImage.AccountFilename);
|
|
|
|
|
bootstrapGenesisJson = ExecuteCommand("cat", GethDockerImage.GenesisFilename);
|
2023-04-10 15:21:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 10:09:41 +02:00
|
|
|
|
private string ExecuteCommand(string command, params string[] arguments)
|
|
|
|
|
{
|
2023-04-11 11:00:39 +02:00
|
|
|
|
return k8sManager.ExecuteCommand(gethBootstrapNode!.BootstrapPod, K8sGethBoostrapSpecs.ContainerName, command, arguments);
|
2023-04-10 14:48:16 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class GethInfo
|
|
|
|
|
{
|
2023-04-11 11:00:39 +02:00
|
|
|
|
public GethInfo(K8sGethBoostrapSpecs spec, PodInfo bootstrapPod, PodInfo companionPod)
|
2023-04-10 14:48:16 +02:00
|
|
|
|
{
|
|
|
|
|
Spec = spec;
|
2023-04-11 11:00:39 +02:00
|
|
|
|
BootstrapPod = bootstrapPod;
|
|
|
|
|
CompanionPod = companionPod;
|
2023-04-10 10:09:41 +02:00
|
|
|
|
}
|
2023-04-10 14:48:16 +02:00
|
|
|
|
|
|
|
|
|
public K8sGethBoostrapSpecs Spec { get; }
|
2023-04-11 11:00:39 +02:00
|
|
|
|
public PodInfo BootstrapPod { get; }
|
|
|
|
|
public PodInfo CompanionPod { get; }
|
2023-04-10 14:48:16 +02:00
|
|
|
|
public string GenesisJsonBase64 { get; set; } = string.Empty;
|
2023-04-10 10:09:41 +02:00
|
|
|
|
}
|
|
|
|
|
}
|