Examples of how to execute commands in a container. Example of setup with Nethereum.

This commit is contained in:
benbierens 2023-03-31 08:39:24 +02:00
parent f70ce8e8bb
commit 14d663143e
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
6 changed files with 240 additions and 89 deletions

View File

@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="KubernetesClient" Version="10.1.4" />
<PackageReference Include="Nethereum.Web3" Version="4.14.0" />
<PackageReference Include="nunit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />

View File

@ -8,7 +8,7 @@ namespace CodexDistTestCore
{
private TestLog log = null!;
private FileManager fileManager = null!;
private K8sManager k8sManager = null!;
public K8sManager k8sManager = null!;
private MetricsAggregator metricsAggregator = null!;
[OneTimeSetUp]

View File

@ -43,6 +43,12 @@
return online.Origin;
}
public void ExampleOfCMD(IOnlineCodexNode node)
{
var n = (OnlineCodexNode)node;
K8s(k => k.ExampleOfCommandExecution(n));
}
public void DeleteAllResources()
{
K8s(k => k.DeleteAllResources());

View File

@ -25,6 +25,29 @@ namespace CodexDistTestCore
client.Dispose();
}
private Task Callback(Stream stdIn, Stream stdOut, Stream stdErr)
{
using var streamReader = new StreamReader(stdOut);
var lines = new List<string>();
var line = streamReader.ReadLine();
while (line != null)
{
lines.Add(line);
line = streamReader.ReadLine();
}
Assert.That(lines.Any(l => l.Contains("FOO76543")));
return Task.CompletedTask;
}
public void ExampleOfCommandExecution(OnlineCodexNode node)
{
Utils.Wait(client.NamespacedPodExecAsync(
node.Group.PodInfo!.Name, K8sNamespace, node.Container.Name, new[] { "echo", "FOO76543" }, false, Callback, new CancellationToken()));
}
public void BringOnline(CodexNodeGroup online, OfflineCodexNodes offline)
{
EnsureTestNamespace();

View File

@ -0,0 +1,100 @@
using Nethereum.Web3;
using Nethereum.ABI.FunctionEncoding.Attributes;
using Nethereum.Contracts.CQS;
using Nethereum.Util;
using Nethereum.Web3.Accounts;
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.Contracts;
using Nethereum.Contracts.Extensions;
using System.Numerics;
using NUnit.Framework;
// https://docs.nethereum.com/en/latest/nethereum-smartcontrats-gettingstarted/
namespace CodexDistTestCore
{
public class TryContract
{
[Test]
public void DoThing()
{
var url = "http://testchain.nethereum.com:8545";
var privateKey = "0x7580e7fb49df1c861f0050fae31c2224c6aba908e116b8da44ee8cd927b990b0";
var account = new Account(privateKey);
var web3 = new Web3(account, url);
// Deploy contract:
var deploymentMessage = new StandardTokenDeployment
{
TotalSupply = 100000
};
var deploymentHandler = web3.Eth.GetContractDeploymentHandler<StandardTokenDeployment>();
var transactionReceipt = Utils.Wait(deploymentHandler.SendRequestAndWaitForReceiptAsync(deploymentMessage));
var contractAddress = transactionReceipt.ContractAddress;
// Get balance:
var balanceOfFunctionMessage = new BalanceOfFunction()
{
Owner = account.Address,
};
var balanceHandler = web3.Eth.GetContractQueryHandler<BalanceOfFunction>();
var balance = Utils.Wait(balanceHandler.QueryAsync<BigInteger>(contractAddress, balanceOfFunctionMessage));
long asInt = ((long)balance);
// Transfer:
var receiverAddress = "0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe";
var transferHandler = web3.Eth.GetContractTransactionHandler<TransferFunction>();
var transfer = new TransferFunction()
{
To = receiverAddress,
TokenAmount = 100
};
var transferReceipt = Utils.Wait(transferHandler.SendRequestAndWaitForReceiptAsync(contractAddress, transfer));
// Signing:
var signedTransaction = Utils.Wait(transferHandler.SignTransactionAsync(contractAddress, transfer));
}
}
public class StandardTokenDeployment : ContractDeploymentMessage
{
public static string BYTECODE = "0x60606040526040516020806106f5833981016040528080519060200190919050505b80600160005060003373ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005081905550806000600050819055505b506106868061006f6000396000f360606040523615610074576000357c010000000000000000000000000000000000000000000000000000000090048063095ea7b31461008157806318160ddd146100b657806323b872dd146100d957806370a0823114610117578063a9059cbb14610143578063dd62ed3e1461017857610074565b61007f5b610002565b565b005b6100a060048080359060200190919080359060200190919050506101ad565b6040518082815260200191505060405180910390f35b6100c36004805050610674565b6040518082815260200191505060405180910390f35b6101016004808035906020019091908035906020019091908035906020019091905050610281565b6040518082815260200191505060405180910390f35b61012d600480803590602001909190505061048d565b6040518082815260200191505060405180910390f35b61016260048080359060200190919080359060200190919050506104cb565b6040518082815260200191505060405180910390f35b610197600480803590602001909190803590602001909190505061060b565b6040518082815260200191505060405180910390f35b600081600260005060003373ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005060008573ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600050819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040518082815260200191505060405180910390a36001905061027b565b92915050565b600081600160005060008673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600050541015801561031b575081600260005060008673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005060003373ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000505410155b80156103275750600082115b1561047c5781600160005060008573ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828282505401925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a381600160005060008673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282825054039250508190555081600260005060008673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005060003373ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828282505403925050819055506001905061048656610485565b60009050610486565b5b9392505050565b6000600160005060008373ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000505490506104c6565b919050565b600081600160005060003373ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600050541015801561050c5750600082115b156105fb5781600160005060003373ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282825054039250508190555081600160005060008573ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828282505401925050819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a36001905061060556610604565b60009050610605565b5b92915050565b6000600260005060008473ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005060008373ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005054905061066e565b92915050565b60006000600050549050610683565b9056";
public StandardTokenDeployment() : base(BYTECODE) { }
[Parameter("uint256", "totalSupply")]
public BigInteger TotalSupply { get; set; }
}
[Function("balanceOf", "uint256")]
public class BalanceOfFunction : FunctionMessage
{
[Parameter("address", "_owner", 1)]
public string Owner { get; set; }
}
[Function("transfer", "bool")]
public class TransferFunction : FunctionMessage
{
[Parameter("address", "_to", 1)]
public string To { get; set; }
[Parameter("uint256", "_value", 2)]
public BigInteger TokenAmount { get; set; }
}
[Event("Transfer")]
public class TransferEventDTO : IEventDTO
{
[Parameter("address", "_from", 1, true)]
public string From { get; set; }
[Parameter("address", "_to", 2, true)]
public string To { get; set; }
[Parameter("uint256", "_value", 3, false)]
public BigInteger Value { get; set; }
}
}

View File

@ -7,105 +7,126 @@ namespace Tests.BasicTests
[TestFixture]
public class SimpleTests : DistTest
{
[Test]
public void GetDebugInfo()
{
var dockerImage = new CodexDockerImage();
//[Test]
//public void GetDebugInfo()
//{
// var dockerImage = new CodexDockerImage();
var node = SetupCodexNodes(1).BringOnline()[0];
// var node = SetupCodexNodes(1).BringOnline()[0];
var debugInfo = node.GetDebugInfo();
// var debugInfo = node.GetDebugInfo();
Assert.That(debugInfo.spr, Is.Not.Empty);
Assert.That(debugInfo.codex.revision, Is.EqualTo(dockerImage.GetExpectedImageRevision()));
}
// Assert.That(debugInfo.spr, Is.Not.Empty);
// Assert.That(debugInfo.codex.revision, Is.EqualTo(dockerImage.GetExpectedImageRevision()));
//}
[Test, DontDownloadLogsAndMetricsOnFailure]
public void CanAccessLogs()
{
var node = SetupCodexNodes(1).BringOnline()[0];
//[Test, DontDownloadLogsAndMetricsOnFailure]
//public void CanAccessLogs()
//{
// var node = SetupCodexNodes(1).BringOnline()[0];
var log = node.DownloadLog();
// var log = node.DownloadLog();
log.AssertLogContains("Started codex node");
}
// log.AssertLogContains("Started codex node");
//}
//[Test]
//public void TwoMetricsExample()
//{
// var group = SetupCodexNodes(2)
// .EnableMetrics()
// .BringOnline();
// var metrics = GatherMetrics(group);
// var group2 = SetupCodexNodes(2)
// .EnableMetrics()
// .BringOnline();
// var metrics2 = GatherMetrics(group2);
// var primary = group[0];
// var secondary = group[1];
// var primary2 = group2[0];
// var secondary2 = group2[1];
// primary.ConnectToPeer(secondary);
// primary2.ConnectToPeer(secondary2);
// Thread.Sleep(TimeSpan.FromMinutes(5));
// metrics.AssertThat(primary, "libp2p_peers", Is.EqualTo(1));
// metrics2.AssertThat(primary2, "libp2p_peers", Is.EqualTo(1));
//}
//[Test]
//public void OneClientTest()
//{
// var primary = SetupCodexNodes(1).BringOnline()[0];
// var testFile = GenerateTestFile(1.MB());
// var contentId = primary.UploadFile(testFile);
// var downloadedFile = primary.DownloadContent(contentId);
// testFile.AssertIsEqual(downloadedFile);
//}
//[Test]
//public void TwoClientsOnePodTest()
//{
// var group = SetupCodexNodes(2).BringOnline();
// var primary = group[0];
// var secondary = group[1];
// PerformTwoClientTest(primary, secondary);
//}
//[Test]
//public void TwoClientsTwoPodsTest()
//{
// var primary = SetupCodexNodes(1).BringOnline()[0];
// var secondary = SetupCodexNodes(1).BringOnline()[0];
// PerformTwoClientTest(primary, secondary);
//}
//[Test]
//public void TwoClientsTwoLocationsTest()
//{
// var primary = SetupCodexNodes(1)
// .At(Location.BensLaptop)
// .BringOnline()[0];
// var secondary = SetupCodexNodes(1)
// .At(Location.BensOldGamingMachine)
// .BringOnline()[0];
// PerformTwoClientTest(primary, secondary);
//}
//[Test]
//public void RequestStorageTest()
//{
// var primary = SetupCodexNodes(1)
// .Enable(startupCoins:100)
// .BringOnline()[0];
// GiveCoins(primary, 0);
// AssertBalance(primary, 100);
//}
[Test]
public void TwoMetricsExample()
{
var group = SetupCodexNodes(2)
.EnableMetrics()
.BringOnline();
var metrics = GatherMetrics(group);
var group2 = SetupCodexNodes(2)
.EnableMetrics()
.BringOnline();
var metrics2 = GatherMetrics(group2);
var primary = group[0];
var secondary = group[1];
var primary2 = group2[0];
var secondary2 = group2[1];
primary.ConnectToPeer(secondary);
primary2.ConnectToPeer(secondary2);
Thread.Sleep(TimeSpan.FromMinutes(5));
metrics.AssertThat(primary, "libp2p_peers", Is.EqualTo(1));
metrics2.AssertThat(primary2, "libp2p_peers", Is.EqualTo(1));
}
[Test]
public void OneClientTest()
public void DoCommand()
{
var primary = SetupCodexNodes(1).BringOnline()[0];
var testFile = GenerateTestFile(1.MB());
var contentId = primary.UploadFile(testFile);
var downloadedFile = primary.DownloadContent(contentId);
testFile.AssertIsEqual(downloadedFile);
}
[Test]
public void TwoClientsOnePodTest()
{
var group = SetupCodexNodes(2).BringOnline();
var primary = group[0];
var secondary = group[1];
PerformTwoClientTest(primary, secondary);
}
[Test]
public void TwoClientsTwoPodsTest()
{
var primary = SetupCodexNodes(1).BringOnline()[0];
var secondary = SetupCodexNodes(1).BringOnline()[0];
PerformTwoClientTest(primary, secondary);
}
[Test]
public void TwoClientsTwoLocationsTest()
{
var primary = SetupCodexNodes(1)
.At(Location.BensLaptop)
.BringOnline()[0];
var secondary = SetupCodexNodes(1)
.At(Location.BensOldGamingMachine)
.BringOnline()[0];
PerformTwoClientTest(primary, secondary);
k8sManager.ExampleOfCMD(primary);
}
private void PerformTwoClientTest(IOnlineCodexNode primary, IOnlineCodexNode secondary)