Applies nicely readable byte size notation

This commit is contained in:
benbierens 2023-03-21 09:20:09 +01:00
parent e3fd96605a
commit c422b08c96
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
7 changed files with 99 additions and 23 deletions

View File

@ -24,10 +24,10 @@ namespace CodexDistTests.BasicTests
{ {
var primary = SetupCodexNode() var primary = SetupCodexNode()
.WithLogLevel(CodexLogLevel.Trace) .WithLogLevel(CodexLogLevel.Trace)
.WithStorageQuota(1024 * 1024 * 2) .WithStorageQuota(2.MB())
.BringOnline(); .BringOnline();
var testFile = GenerateTestFile(1024 * 1024); var testFile = GenerateTestFile(1.MB());
var contentId = primary.UploadFile(testFile); var contentId = primary.UploadFile(testFile);

57
TestCore/ByteSize.cs Normal file
View File

@ -0,0 +1,57 @@
namespace CodexDistTests.TestCore
{
public class ByteSize
{
public ByteSize(long sizeInBytes)
{
SizeInBytes = sizeInBytes;
}
public long SizeInBytes { get; }
}
public static class IntExtensions
{
private const long Kilo = 1024;
public static ByteSize KB(this long i)
{
return new ByteSize(i * Kilo);
}
public static ByteSize MB(this long i)
{
return KB(i * Kilo);
}
public static ByteSize GB(this long i)
{
return MB(i * Kilo);
}
public static ByteSize TB(this long i)
{
return GB(i * Kilo);
}
public static ByteSize KB(this int i)
{
return KB(Convert.ToInt64(i));
}
public static ByteSize MB(this int i)
{
return MB(Convert.ToInt64(i));
}
public static ByteSize GB(this int i)
{
return GB(Convert.ToInt64(i));
}
public static ByteSize TB(this int i)
{
return TB(Convert.ToInt64(i));
}
}
}

View File

@ -38,7 +38,7 @@ namespace CodexDistTests.TestCore
} }
if (node.StorageQuota != null) if (node.StorageQuota != null)
{ {
AddVar("STORAGE_QUOTA", node.StorageQuota.ToString()!); AddVar("STORAGE_QUOTA", node.StorageQuota.SizeInBytes.ToString()!);
} }
} }

View File

@ -2,11 +2,24 @@
namespace CodexDistTests.TestCore namespace CodexDistTests.TestCore
{ {
[SetUpFixture]
public abstract class DistTest public abstract class DistTest
{ {
private FileManager fileManager = null!; private FileManager fileManager = null!;
private K8sManager k8sManager = null!; private K8sManager k8sManager = null!;
[OneTimeSetUp]
public void GlobalSetup()
{
// Previous test run may have been interrupted.
// Begin by cleaning everything up.
fileManager = new FileManager();
k8sManager = new K8sManager(fileManager);
k8sManager.DeleteAllResources();
fileManager.DeleteAllTestFiles();
}
[SetUp] [SetUp]
public void SetUpDistTest() public void SetUpDistTest()
{ {
@ -38,7 +51,7 @@ namespace CodexDistTests.TestCore
} }
} }
public TestFile GenerateTestFile(int size = 1024) public TestFile GenerateTestFile(ByteSize size)
{ {
return fileManager.GenerateTestFile(size); return fileManager.GenerateTestFile(size);
} }

View File

@ -5,7 +5,7 @@ namespace CodexDistTests.TestCore
public interface IFileManager public interface IFileManager
{ {
TestFile CreateEmptyTestFile(); TestFile CreateEmptyTestFile();
TestFile GenerateTestFile(int size = 1024); TestFile GenerateTestFile(ByteSize size);
void DeleteAllTestFiles(); void DeleteAllTestFiles();
} }
@ -30,7 +30,7 @@ namespace CodexDistTests.TestCore
return result; return result;
} }
public TestFile GenerateTestFile(int size = 1024) public TestFile GenerateTestFile(ByteSize size)
{ {
var result = CreateEmptyTestFile(); var result = CreateEmptyTestFile();
GenerateFileBytes(result, size); GenerateFileBytes(result, size);
@ -44,17 +44,18 @@ namespace CodexDistTests.TestCore
activeFiles.Clear(); activeFiles.Clear();
} }
private void GenerateFileBytes(TestFile result, int size) private void GenerateFileBytes(TestFile result, ByteSize size)
{ {
while (size > 0) long bytesLeft = size.SizeInBytes;
while (bytesLeft > 0)
{ {
var length = Math.Min(size, ChunkSize); var length = Math.Min(bytesLeft, ChunkSize);
AppendRandomBytesToFile(result, length); AppendRandomBytesToFile(result, length);
size -= length; bytesLeft -= length;
} }
} }
private void AppendRandomBytesToFile(TestFile result, int length) private void AppendRandomBytesToFile(TestFile result, long length)
{ {
var bytes = new byte[length]; var bytes = new byte[length];
random.NextBytes(bytes); random.NextBytes(bytes);

View File

@ -18,7 +18,6 @@ namespace CodexDistTests.TestCore
private readonly Dictionary<OnlineCodexNode, ActiveNode> activeNodes = new Dictionary<OnlineCodexNode, ActiveNode>(); private readonly Dictionary<OnlineCodexNode, ActiveNode> activeNodes = new Dictionary<OnlineCodexNode, ActiveNode>();
private readonly List<string> knownActivePodNames = new List<string>(); private readonly List<string> knownActivePodNames = new List<string>();
private readonly IFileManager fileManager; private readonly IFileManager fileManager;
private V1Namespace? activeNamespace;
public K8sManager(IFileManager fileManager) public K8sManager(IFileManager fileManager)
{ {
@ -131,7 +130,7 @@ namespace CodexDistTests.TestCore
private void WaitUntilNamespaceDeleted(Kubernetes client) private void WaitUntilNamespaceDeleted(Kubernetes client)
{ {
WaitUntil(() => client.ListNamespace().Items.All(n => n.Metadata.Name != K8sNamespace)); WaitUntil(() => !IsTestNamespaceOnline(client));
} }
private void WaitUntil(Func<bool> predicate) private void WaitUntil(Func<bool> predicate)
@ -251,7 +250,7 @@ namespace CodexDistTests.TestCore
private void EnsureTestNamespace(Kubernetes client) private void EnsureTestNamespace(Kubernetes client)
{ {
if (activeNamespace != null) return; if (IsTestNamespaceOnline(client)) return;
var namespaceSpec = new V1Namespace var namespaceSpec = new V1Namespace
{ {
@ -262,14 +261,15 @@ namespace CodexDistTests.TestCore
Labels = new Dictionary<string, string> { { "name", K8sNamespace } } Labels = new Dictionary<string, string> { { "name", K8sNamespace } }
} }
}; };
activeNamespace = client.CreateNamespace(namespaceSpec); client.CreateNamespace(namespaceSpec);
} }
private void DeleteNamespace(Kubernetes client) private void DeleteNamespace(Kubernetes client)
{ {
if (activeNamespace == null) return; if (IsTestNamespaceOnline(client))
client.DeleteNamespace(activeNamespace.Name(), null, null, gracePeriodSeconds: 0); {
activeNamespace = null; client.DeleteNamespace(K8sNamespace, null, null, gracePeriodSeconds: 0);
}
} }
#endregion #endregion
@ -281,6 +281,11 @@ namespace CodexDistTests.TestCore
return new Kubernetes(config); return new Kubernetes(config);
} }
private static bool IsTestNamespaceOnline(Kubernetes client)
{
return client.ListNamespace().Items.Any(n => n.Metadata.Name == K8sNamespace);
}
private ActiveNode GetAndRemoveActiveNodeFor(IOnlineCodexNode node) private ActiveNode GetAndRemoveActiveNodeFor(IOnlineCodexNode node)
{ {
var n = (OnlineCodexNode)node; var n = (OnlineCodexNode)node;

View File

@ -4,7 +4,7 @@
{ {
IOfflineCodexNode WithLogLevel(CodexLogLevel level); IOfflineCodexNode WithLogLevel(CodexLogLevel level);
IOfflineCodexNode WithBootstrapNode(IOnlineCodexNode node); IOfflineCodexNode WithBootstrapNode(IOnlineCodexNode node);
IOfflineCodexNode WithStorageQuota(int storageQuotaBytes); IOfflineCodexNode WithStorageQuota(ByteSize storageQuota);
IOnlineCodexNode BringOnline(); IOnlineCodexNode BringOnline();
} }
@ -23,7 +23,7 @@
public CodexLogLevel? LogLevel { get; private set; } public CodexLogLevel? LogLevel { get; private set; }
public IOnlineCodexNode? BootstrapNode { get; private set; } public IOnlineCodexNode? BootstrapNode { get; private set; }
public int? StorageQuota { get; private set; } public ByteSize? StorageQuota { get; private set; }
public OfflineCodexNode(IK8sManager k8SManager) public OfflineCodexNode(IK8sManager k8SManager)
{ {
@ -47,9 +47,9 @@
return this; return this;
} }
public IOfflineCodexNode WithStorageQuota(int storageQuotaBytes) public IOfflineCodexNode WithStorageQuota(ByteSize storageQuota)
{ {
StorageQuota = storageQuotaBytes; StorageQuota = storageQuota;
return this; return this;
} }
@ -58,7 +58,7 @@
var result = ""; var result = "";
if (LogLevel != null) result += $"LogLevel={LogLevel},"; if (LogLevel != null) result += $"LogLevel={LogLevel},";
if (BootstrapNode != null) result += "BootstrapNode=set,"; if (BootstrapNode != null) result += "BootstrapNode=set,";
if (StorageQuota != null) result += $"StorageQuote={StorageQuota},"; if (StorageQuota != null) result += $"StorageQuote={StorageQuota.SizeInBytes},";
return result; return result;
} }
} }