cs-codex-dist-tests/CodexDistTestCore/Marketplace/MarketplaceAccess.cs

112 lines
3.9 KiB
C#
Raw Normal View History

using NUnit.Framework;
using NUnit.Framework.Constraints;
namespace CodexDistTestCore.Marketplace
{
public interface IMarketplaceAccess
{
2023-04-11 09:00:39 +00:00
void MakeStorageAvailable(ByteSize size, int minPricePerBytePerSecond, float maxCollateral);
void RequestStorage(ContentId contentId, int pricePerBytePerSecond, float requiredCollateral, float minRequiredNumberOfNodes);
void AssertThatBalance(IResolveConstraint constraint, string message = "");
2023-04-12 06:40:23 +00:00
decimal GetBalance();
}
public class MarketplaceAccess : IMarketplaceAccess
{
private readonly K8sManager k8sManager;
private readonly MarketplaceController marketplaceController;
private readonly TestLog log;
private readonly CodexNodeGroup group;
2023-04-11 10:06:33 +00:00
private readonly GethCompanionNodeContainer container;
public MarketplaceAccess(
K8sManager k8sManager,
MarketplaceController marketplaceController,
TestLog log,
2023-04-11 10:06:33 +00:00
CodexNodeGroup group,
GethCompanionNodeContainer container)
{
this.k8sManager = k8sManager;
this.marketplaceController = marketplaceController;
this.log = log;
this.group = group;
2023-04-11 10:06:33 +00:00
this.container = container;
}
public void Initialize()
{
EnsureAccount();
2023-04-11 10:06:33 +00:00
marketplaceController.AddToBalance(container.Account, group.Origin.MarketplaceConfig!.InitialBalance);
2023-04-11 10:06:33 +00:00
log.Log($"Initialized Geth companion node with account '{container.Account}' and initial balance {group.Origin.MarketplaceConfig!.InitialBalance}");
}
2023-04-11 10:06:33 +00:00
public void RequestStorage(ContentId contentId, int pricePerBytePerSecond, float requiredCollateral, float minRequiredNumberOfNodes)
{
throw new NotImplementedException();
}
2023-04-11 10:06:33 +00:00
public void MakeStorageAvailable(ByteSize size, int minPricePerBytePerSecond, float maxCollateral)
{
throw new NotImplementedException();
}
public void AssertThatBalance(IResolveConstraint constraint, string message = "")
{
throw new NotImplementedException();
}
2023-04-12 06:40:23 +00:00
public decimal GetBalance()
{
2023-04-12 06:40:23 +00:00
return marketplaceController.GetBalance(container.Account);
}
private void EnsureAccount()
{
FetchAccount();
2023-04-11 10:06:33 +00:00
if (string.IsNullOrEmpty(container.Account))
{
Thread.Sleep(TimeSpan.FromSeconds(15));
FetchAccount();
}
2023-04-11 10:06:33 +00:00
Assert.That(container.Account, Is.Not.Empty, "Unable to fetch account for geth companion node. Test infra failure.");
}
private void FetchAccount()
{
container.Account = k8sManager.ExecuteCommand(group.GethCompanionGroup!.Pod!, container.Name, "cat", GethDockerImage.AccountFilename);
}
}
public class MarketplaceUnavailable : IMarketplaceAccess
{
2023-04-11 10:06:33 +00:00
public void RequestStorage(ContentId contentId, int pricePerBytePerSecond, float requiredCollateral, float minRequiredNumberOfNodes)
{
Unavailable();
}
2023-04-11 10:06:33 +00:00
public void MakeStorageAvailable(ByteSize size, int minPricePerBytePerSecond, float maxCollateral)
{
Unavailable();
}
public void AssertThatBalance(IResolveConstraint constraint, string message = "")
{
Unavailable();
}
2023-04-12 06:40:23 +00:00
public decimal GetBalance()
{
Unavailable();
2023-04-12 06:40:23 +00:00
return 0;
}
private void Unavailable()
{
Assert.Fail("Incorrect test setup: Marketplace was not enabled for this group of Codex nodes. Add 'EnableMarketplace(...)' after 'SetupCodexNodes()' to enable it.");
throw new InvalidOperationException();
}
}
}