frostfs-sdk-csharp/src/FrostFS.SDK.Tests/NetworkTest.cs
Pavel Gross ee20798379 [#24] Client: Implement pool part2
Signed-off-by: Pavel Gross <p.gross@yadro.com>
2024-11-01 10:30:28 +03:00

274 lines
No EOL
9.8 KiB
C#

using System.Diagnostics.CodeAnalysis;
using FrostFS.Netmap;
using FrostFS.SDK.ClientV2;
using Google.Protobuf;
namespace FrostFS.SDK.Tests;
[SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "Default Value is correct for tests")]
public class NetworkTest : NetworkTestsBase
{
[Theory]
[InlineData(false)]
[InlineData(true)]
public async void NetworkSettingsTest(bool useContext)
{
Mocker.Parameters.Add("AuditFee", [1]);
Mocker.Parameters.Add("BasicIncomeRate", [2]);
Mocker.Parameters.Add("ContainerFee", [3]);
Mocker.Parameters.Add("ContainerAliasFee", [4]);
Mocker.Parameters.Add("EpochDuration", [5]);
Mocker.Parameters.Add("InnerRingCandidateFee", [6]);
Mocker.Parameters.Add("MaxECDataCount", [7]);
Mocker.Parameters.Add("MaxECParityCount", [8]);
Mocker.Parameters.Add("MaxObjectSize", [9]);
Mocker.Parameters.Add("WithdrawFee", [10]);
Mocker.Parameters.Add("HomomorphicHashingDisabled", [1]);
Mocker.Parameters.Add("MaintenanceModeAllowed", [1]);
var param = useContext ?
new PrmNetworkSettings(new CallContext
{
CancellationToken = Mocker.CancellationTokenSource.Token,
Timeout = TimeSpan.FromSeconds(20),
OwnerId = OwnerId,
Key = ECDsaKey,
Version = Version
})
: new PrmNetworkSettings();
var validTimeoutFrom = DateTime.UtcNow.AddSeconds(20);
var result = await GetClient().GetNetworkSettingsAsync(param);
var validTimeoutTo = DateTime.UtcNow.AddSeconds(20);
Assert.NotNull(result);
Assert.Equal(Mocker.Parameters["AuditFee"], [(byte)result.AuditFee]);
Assert.Equal(Mocker.Parameters["BasicIncomeRate"], [(byte)result.BasicIncomeRate]);
Assert.Equal(Mocker.Parameters["ContainerFee"], [(byte)result.ContainerFee]);
Assert.Equal(Mocker.Parameters["ContainerAliasFee"], [(byte)result.ContainerAliasFee]);
Assert.Equal(Mocker.Parameters["EpochDuration"], [(byte)result.EpochDuration]);
Assert.Equal(Mocker.Parameters["InnerRingCandidateFee"], [(byte)result.InnerRingCandidateFee]);
Assert.Equal(Mocker.Parameters["MaxECDataCount"], [(byte)result.MaxECDataCount]);
Assert.Equal(Mocker.Parameters["MaxECParityCount"], [(byte)result.MaxECParityCount]);
Assert.Equal(Mocker.Parameters["MaxObjectSize"], [(byte)result.MaxObjectSize]);
Assert.Equal(Mocker.Parameters["WithdrawFee"], [(byte)result.WithdrawFee]);
Assert.True(result.HomomorphicHashingDisabled);
Assert.True(result.MaintenanceModeAllowed);
if (useContext)
{
Assert.Equal(Mocker.CancellationTokenSource.Token, Mocker.CancellationToken);
Assert.NotNull(Mocker.DateTime);
Assert.True(Mocker.DateTime.Value >= validTimeoutFrom);
Assert.True(Mocker.DateTime.Value <= validTimeoutTo);
}
else
{
Assert.NotNull(Mocker.NetworkInfoRequest);
Assert.Empty(Mocker.NetworkInfoRequest.MetaHeader.XHeaders);
Assert.Null(Mocker.DateTime);
}
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public async void NetmapSnapshotTest(bool useContext)
{
var body = new NetmapSnapshotResponse.Types.Body
{
Netmap = new Netmap.Netmap { Epoch = 99 }
};
var nodeInfo1 = new NodeInfo
{
State = NodeInfo.Types.State.Online,
PublicKey = ByteString.CopyFrom([1, 2, 3])
};
nodeInfo1.Addresses.Add("address1");
nodeInfo1.Addresses.Add("address2");
nodeInfo1.Attributes.Add(new NodeInfo.Types.Attribute { Key = "key1", Value = "value1" });
nodeInfo1.Attributes.Add(new NodeInfo.Types.Attribute { Key = "key2", Value = "value2" });
var nodeInfo2 = new NodeInfo
{
State = NodeInfo.Types.State.Offline,
PublicKey = ByteString.CopyFrom([3, 4, 5])
};
nodeInfo2.Addresses.Add("address3");
nodeInfo2.Attributes.Add(new NodeInfo.Types.Attribute { Key = "key3", Value = "value3" });
body.Netmap.Nodes.Add(nodeInfo1);
body.Netmap.Nodes.Add(nodeInfo2);
Mocker.NetmapSnapshotResponse = new NetmapSnapshotResponse { Body = body };
PrmNetmapSnapshot param;
if (useContext)
{
var ctx = new CallContext
{
CancellationToken = Mocker.CancellationTokenSource.Token,
Timeout = TimeSpan.FromSeconds(20),
OwnerId = OwnerId,
Key = ECDsaKey,
Version = Version
};
param = new(ctx);
param.XHeaders.Add("headerKey1", "headerValue1");
}
else
{
param = new();
}
var validTimeoutFrom = DateTime.UtcNow.AddSeconds(20);
var result = await GetClient().GetNetmapSnapshotAsync(param);
var validTimeoutTo = DateTime.UtcNow.AddSeconds(20);
Assert.NotNull(result);
Assert.Equal(99u, result.Epoch);
Assert.Equal(2, result.NodeInfoCollection.Count);
var node1 = result.NodeInfoCollection[0];
Assert.Equal(NodeState.Online, node1.State);
Assert.Equal(2, node1.Addresses.Count);
Assert.Equal("address1", node1.Addresses.ElementAt(0));
Assert.Equal("address2", node1.Addresses.ElementAt(1));
Assert.Equal(2, node1.Attributes.Count);
Assert.Equal("key1", node1.Attributes.ElementAt(0).Key);
Assert.Equal("value1", node1.Attributes.ElementAt(0).Value);
Assert.Equal("key2", node1.Attributes.ElementAt(1).Key);
Assert.Equal("value2", node1.Attributes.ElementAt(1).Value);
var node2 = result.NodeInfoCollection[1];
Assert.Equal(NodeState.Offline, node2.State);
Assert.Single(node2.Addresses);
Assert.Equal("address3", node2.Addresses.ElementAt(0));
Assert.Single(node2.Attributes);
Assert.Equal("key3", node2.Attributes.ElementAt(0).Key);
Assert.Equal("value3", node2.Attributes.ElementAt(0).Value);
if (useContext)
{
Assert.NotNull(Mocker.NetmapSnapshotRequest);
Assert.Single(Mocker.NetmapSnapshotRequest.MetaHeader.XHeaders);
Assert.Equal(param.XHeaders.Keys[0], Mocker.NetmapSnapshotRequest.MetaHeader.XHeaders.First().Key);
Assert.Equal(param.XHeaders[param.XHeaders.Keys[0]], Mocker.NetmapSnapshotRequest.MetaHeader.XHeaders.First().Value);
Assert.Equal(Mocker.CancellationTokenSource.Token, Mocker.CancellationToken);
Assert.NotNull(Mocker.DateTime);
Assert.True(Mocker.DateTime.Value >= validTimeoutFrom);
Assert.True(Mocker.DateTime.Value <= validTimeoutTo);
}
else
{
Assert.NotNull(Mocker.NetmapSnapshotRequest);
Assert.Empty(Mocker.NetmapSnapshotRequest.MetaHeader.XHeaders);
Assert.Null(Mocker.DateTime);
}
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public async void NodeInfoTest(bool useContext)
{
var body = new LocalNodeInfoResponse.Types.Body
{
NodeInfo = new NodeInfo()
{
State = NodeInfo.Types.State.Online,
PublicKey = ByteString.CopyFrom([1, 2, 3])
},
Version = new Refs.Version { Major = 2, Minor = 12 }
};
body.NodeInfo.Addresses.Add("address1");
body.NodeInfo.Addresses.Add("address2");
body.NodeInfo.Attributes.Add(new NodeInfo.Types.Attribute { Key = "key1", Value = "value1" });
body.NodeInfo.Attributes.Add(new NodeInfo.Types.Attribute { Key = "key2", Value = "value2" });
Mocker.NodeInfoResponse = new LocalNodeInfoResponse { Body = body };
PrmNodeInfo param;
if (useContext)
{
var ctx = new CallContext
{
CancellationToken = Mocker.CancellationTokenSource.Token,
Timeout = TimeSpan.FromSeconds(20),
OwnerId = OwnerId,
Key = ECDsaKey,
Version = Version
};
param = new(ctx);
param.XHeaders.Add("headerKey1", "headerValue1");
}
else
{
param = new();
}
var validTimeoutFrom = DateTime.UtcNow.AddSeconds(20);
var result = await GetClient().GetNodeInfoAsync(param);
var validTimeoutTo = DateTime.UtcNow.AddSeconds(20);
Assert.NotNull(result);
Assert.Equal(NodeState.Online, result.State);
Assert.Equal(2, result.Addresses.Count);
Assert.Equal("address1", result.Addresses.ElementAt(0));
Assert.Equal("address2", result.Addresses.ElementAt(1));
Assert.Equal(2, result.Attributes.Count);
Assert.Equal("value1", result.Attributes["key1"]);
Assert.Equal("value2", result.Attributes["key2"]);
Assert.NotNull(Mocker.LocalNodeInfoRequest);
if (useContext)
{
Assert.Single(Mocker.LocalNodeInfoRequest.MetaHeader.XHeaders);
Assert.Equal(param.XHeaders.Keys[0], Mocker.LocalNodeInfoRequest.MetaHeader.XHeaders.First().Key);
Assert.Equal(param.XHeaders[param.XHeaders.Keys[0]], Mocker.LocalNodeInfoRequest.MetaHeader.XHeaders.First().Value);
Assert.Equal(Mocker.CancellationTokenSource.Token, Mocker.CancellationToken);
Assert.NotNull(Mocker.DateTime);
Assert.True(Mocker.DateTime.Value >= validTimeoutFrom);
Assert.True(Mocker.DateTime.Value <= validTimeoutTo);
}
else
{
Assert.Empty(Mocker.LocalNodeInfoRequest.MetaHeader.XHeaders);
Assert.Null(Mocker.DateTime);
}
}
}