cs-codex-dist-tests/DistTestCore/Marketplace/ContainerInfoExtractor.cs

156 lines
4.7 KiB
C#
Raw Normal View History

2023-04-25 09:31:15 +00:00
using KubernetesWorkflow;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Utils;
2023-04-14 07:54:07 +00:00
namespace DistTestCore.Marketplace
{
public class ContainerInfoExtractor
2023-04-14 07:54:07 +00:00
{
private readonly StartupWorkflow workflow;
private readonly RunningContainer container;
public ContainerInfoExtractor(StartupWorkflow workflow, RunningContainer container)
2023-04-14 07:54:07 +00:00
{
this.workflow = workflow;
this.container = container;
}
public string ExtractAccount()
{
var account = Retry(FetchAccount);
if (string.IsNullOrEmpty(account)) throw new InvalidOperationException("Unable to fetch account for geth node. Test infra failure.");
return account;
}
public string ExtractPubKey()
{
var pubKey = Retry(FetchPubKey);
if (string.IsNullOrEmpty(pubKey)) throw new InvalidOperationException("Unable to fetch enode from geth node. Test infra failure.");
2023-04-14 07:54:07 +00:00
return pubKey;
2023-04-14 07:54:07 +00:00
}
public string ExtractPrivateKey()
2023-04-18 11:22:41 +00:00
{
var privKey = Retry(FetchPrivateKey);
2023-04-18 11:22:41 +00:00
if (string.IsNullOrEmpty(privKey)) throw new InvalidOperationException("Unable to fetch private key from geth node. Test infra failure.");
return privKey;
}
public string ExtractMarketplaceAddress()
{
var marketplaceAddress = Retry(FetchMarketplaceAddress);
if (string.IsNullOrEmpty(marketplaceAddress)) throw new InvalidOperationException("Unable to fetch marketplace account from codex-contracts node. Test infra failure.");
return marketplaceAddress;
}
public string ExtractMarketplaceAbi()
{
var marketplaceAbi = Retry(FetchMarketplaceAbi);
if (string.IsNullOrEmpty(marketplaceAbi)) throw new InvalidOperationException("Unable to fetch marketplace artifacts from codex-contracts node. Test infra failure.");
return marketplaceAbi;
}
2023-04-14 07:54:07 +00:00
private string Retry(Func<string> fetch)
{
var result = string.Empty;
Time.WaitUntil(() =>
2023-04-14 07:54:07 +00:00
{
result = Catch(fetch);
return !string.IsNullOrEmpty(result);
}, TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(3));
2023-04-14 07:54:07 +00:00
return result;
}
private string Catch(Func<string> fetch)
{
try
{
return fetch();
}
catch
{
return string.Empty;
}
}
2023-04-14 07:54:07 +00:00
private string FetchAccount()
{
return workflow.ExecuteCommand(container, "cat", GethContainerRecipe.AccountFilename);
}
private string FetchPrivateKey()
2023-04-18 11:22:41 +00:00
{
return workflow.ExecuteCommand(container, "cat", GethContainerRecipe.PrivateKeyFilename);
2023-04-18 11:22:41 +00:00
}
private string FetchMarketplaceAddress()
{
var json = workflow.ExecuteCommand(container, "cat", CodexContractsContainerRecipe.MarketplaceAddressFilename);
var marketplace = JsonConvert.DeserializeObject<MarketplaceJson>(json);
return marketplace!.address;
}
private string FetchMarketplaceAbi()
{
var json = workflow.ExecuteCommand(container, "cat", CodexContractsContainerRecipe.MarketplaceArtifactFilename);
var artifact = JObject.Parse(json);
var abi = artifact["abi"];
return abi!.ToString(Formatting.None);
}
private string FetchPubKey()
{
var enodeFinder = new PubKeyFinder();
workflow.DownloadContainerLog(container, enodeFinder);
return enodeFinder.GetPubKey();
}
}
public class PubKeyFinder : LogHandler, ILogHandler
{
2023-04-21 07:11:45 +00:00
private const string openTag = "self=enode://";
private const string openTagQuote = "self=\"enode://";
private string pubKey = string.Empty;
public string GetPubKey()
{
return pubKey;
}
protected override void ProcessLine(string line)
{
if (line.Contains(openTag))
{
2023-04-21 07:11:45 +00:00
ExtractPubKey(openTag, line);
}
else if (line.Contains(openTagQuote))
{
ExtractPubKey(openTagQuote, line);
}
}
2023-04-21 07:11:45 +00:00
private void ExtractPubKey(string tag, string line)
{
2023-04-21 07:11:45 +00:00
var openIndex = line.IndexOf(tag) + tag.Length;
var closeIndex = line.IndexOf("@");
pubKey = line.Substring(
startIndex: openIndex,
length: closeIndex - openIndex);
}
2023-04-14 07:54:07 +00:00
}
public class MarketplaceJson
{
public string address { get; set; } = string.Empty;
}
2023-04-14 07:54:07 +00:00
}