2023-04-10 07:05:27 +00:00
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 ) ;
2023-04-10 07:05:27 +00:00
void AssertThatBalance ( IResolveConstraint constraint , string message = "" ) ;
2023-04-12 06:40:23 +00:00
decimal GetBalance ( ) ;
2023-04-10 07:05:27 +00:00
}
public class MarketplaceAccess : IMarketplaceAccess
{
2023-04-10 13:54:13 +00:00
private readonly K8sManager k8sManager ;
2023-04-11 08:12:24 +00:00
private readonly MarketplaceController marketplaceController ;
2023-04-10 13:54:13 +00:00
private readonly TestLog log ;
2023-04-11 08:12:24 +00:00
private readonly CodexNodeGroup group ;
2023-04-11 10:06:33 +00:00
private readonly GethCompanionNodeContainer container ;
2023-04-10 13:54:13 +00:00
2023-04-11 08:12:24 +00:00
public MarketplaceAccess (
K8sManager k8sManager ,
MarketplaceController marketplaceController ,
TestLog log ,
2023-04-11 10:06:33 +00:00
CodexNodeGroup group ,
GethCompanionNodeContainer container )
2023-04-10 13:54:13 +00:00
{
this . k8sManager = k8sManager ;
2023-04-11 08:12:24 +00:00
this . marketplaceController = marketplaceController ;
2023-04-10 13:54:13 +00:00
this . log = log ;
2023-04-11 08:12:24 +00:00
this . group = group ;
2023-04-11 10:06:33 +00:00
this . container = container ;
2023-04-10 13:54:13 +00:00
}
2023-04-11 08:12:24 +00:00
public void Initialize ( )
2023-04-10 13:54:13 +00:00
{
2023-04-11 08:12:24 +00:00
EnsureAccount ( ) ;
2023-04-10 13:54:13 +00:00
2023-04-11 10:06:33 +00:00
marketplaceController . AddToBalance ( container . Account , group . Origin . MarketplaceConfig ! . InitialBalance ) ;
2023-04-11 08:12:24 +00:00
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-10 13:54:13 +00:00
}
2023-04-11 10:06:33 +00:00
public void RequestStorage ( ContentId contentId , int pricePerBytePerSecond , float requiredCollateral , float minRequiredNumberOfNodes )
2023-04-10 07:05:27 +00:00
{
throw new NotImplementedException ( ) ;
}
2023-04-11 10:06:33 +00:00
public void MakeStorageAvailable ( ByteSize size , int minPricePerBytePerSecond , float maxCollateral )
2023-04-10 07:05:27 +00:00
{
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-10 07:05:27 +00:00
{
2023-04-12 06:40:23 +00:00
return marketplaceController . GetBalance ( container . Account ) ;
2023-04-10 07:05:27 +00:00
}
2023-04-11 08:12:24 +00:00
private void EnsureAccount ( )
{
FetchAccount ( ) ;
2023-04-11 10:06:33 +00:00
if ( string . IsNullOrEmpty ( container . Account ) )
2023-04-11 08:12:24 +00:00
{
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." ) ;
2023-04-11 08:12:24 +00:00
}
private void FetchAccount ( )
{
2023-04-11 14:13:39 +00:00
container . Account = k8sManager . ExecuteCommand ( group . GethCompanionGroup ! . Pod ! , container . Name , "cat" , GethDockerImage . AccountFilename ) ;
2023-04-11 08:12:24 +00:00
}
2023-04-10 07:05:27 +00:00
}
public class MarketplaceUnavailable : IMarketplaceAccess
{
2023-04-11 10:06:33 +00:00
public void RequestStorage ( ContentId contentId , int pricePerBytePerSecond , float requiredCollateral , float minRequiredNumberOfNodes )
2023-04-10 07:05:27 +00:00
{
Unavailable ( ) ;
}
2023-04-11 10:06:33 +00:00
public void MakeStorageAvailable ( ByteSize size , int minPricePerBytePerSecond , float maxCollateral )
2023-04-10 07:05:27 +00:00
{
Unavailable ( ) ;
}
public void AssertThatBalance ( IResolveConstraint constraint , string message = "" )
{
Unavailable ( ) ;
}
2023-04-12 06:40:23 +00:00
public decimal GetBalance ( )
2023-04-10 07:05:27 +00:00
{
Unavailable ( ) ;
2023-04-12 06:40:23 +00:00
return 0 ;
2023-04-10 07:05:27 +00:00
}
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 ( ) ;
}
}
}