From c422b08c965d72033c9e5b344a0a2d50b76310cd Mon Sep 17 00:00:00 2001 From: benbierens Date: Tue, 21 Mar 2023 09:20:09 +0100 Subject: [PATCH] Applies nicely readable byte size notation --- BasicTests/DebugEndpointTests.cs | 4 +-- TestCore/ByteSize.cs | 57 ++++++++++++++++++++++++++++++++ TestCore/CodexDockerImage.cs | 2 +- TestCore/DistTest.cs | 15 ++++++++- TestCore/FileManager.cs | 15 +++++---- TestCore/K8sManager.cs | 19 +++++++---- TestCore/OfflineCodexNode.cs | 10 +++--- 7 files changed, 99 insertions(+), 23 deletions(-) create mode 100644 TestCore/ByteSize.cs diff --git a/BasicTests/DebugEndpointTests.cs b/BasicTests/DebugEndpointTests.cs index 956d916..40926e1 100644 --- a/BasicTests/DebugEndpointTests.cs +++ b/BasicTests/DebugEndpointTests.cs @@ -24,10 +24,10 @@ namespace CodexDistTests.BasicTests { var primary = SetupCodexNode() .WithLogLevel(CodexLogLevel.Trace) - .WithStorageQuota(1024 * 1024 * 2) + .WithStorageQuota(2.MB()) .BringOnline(); - var testFile = GenerateTestFile(1024 * 1024); + var testFile = GenerateTestFile(1.MB()); var contentId = primary.UploadFile(testFile); diff --git a/TestCore/ByteSize.cs b/TestCore/ByteSize.cs new file mode 100644 index 0000000..f7edfc5 --- /dev/null +++ b/TestCore/ByteSize.cs @@ -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)); + } + } +} diff --git a/TestCore/CodexDockerImage.cs b/TestCore/CodexDockerImage.cs index 988a0cd..6a85dac 100644 --- a/TestCore/CodexDockerImage.cs +++ b/TestCore/CodexDockerImage.cs @@ -38,7 +38,7 @@ namespace CodexDistTests.TestCore } if (node.StorageQuota != null) { - AddVar("STORAGE_QUOTA", node.StorageQuota.ToString()!); + AddVar("STORAGE_QUOTA", node.StorageQuota.SizeInBytes.ToString()!); } } diff --git a/TestCore/DistTest.cs b/TestCore/DistTest.cs index e7bc7e0..9150ccf 100644 --- a/TestCore/DistTest.cs +++ b/TestCore/DistTest.cs @@ -2,11 +2,24 @@ namespace CodexDistTests.TestCore { + [SetUpFixture] public abstract class DistTest { private FileManager fileManager = 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] 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); } diff --git a/TestCore/FileManager.cs b/TestCore/FileManager.cs index 7caf742..4dbba44 100644 --- a/TestCore/FileManager.cs +++ b/TestCore/FileManager.cs @@ -5,7 +5,7 @@ namespace CodexDistTests.TestCore public interface IFileManager { TestFile CreateEmptyTestFile(); - TestFile GenerateTestFile(int size = 1024); + TestFile GenerateTestFile(ByteSize size); void DeleteAllTestFiles(); } @@ -30,7 +30,7 @@ namespace CodexDistTests.TestCore return result; } - public TestFile GenerateTestFile(int size = 1024) + public TestFile GenerateTestFile(ByteSize size) { var result = CreateEmptyTestFile(); GenerateFileBytes(result, size); @@ -44,17 +44,18 @@ namespace CodexDistTests.TestCore 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); - size -= length; + bytesLeft -= length; } } - private void AppendRandomBytesToFile(TestFile result, int length) + private void AppendRandomBytesToFile(TestFile result, long length) { var bytes = new byte[length]; random.NextBytes(bytes); diff --git a/TestCore/K8sManager.cs b/TestCore/K8sManager.cs index b939bf8..971992e 100644 --- a/TestCore/K8sManager.cs +++ b/TestCore/K8sManager.cs @@ -18,7 +18,6 @@ namespace CodexDistTests.TestCore private readonly Dictionary activeNodes = new Dictionary(); private readonly List knownActivePodNames = new List(); private readonly IFileManager fileManager; - private V1Namespace? activeNamespace; public K8sManager(IFileManager fileManager) { @@ -131,7 +130,7 @@ namespace CodexDistTests.TestCore private void WaitUntilNamespaceDeleted(Kubernetes client) { - WaitUntil(() => client.ListNamespace().Items.All(n => n.Metadata.Name != K8sNamespace)); + WaitUntil(() => !IsTestNamespaceOnline(client)); } private void WaitUntil(Func predicate) @@ -251,7 +250,7 @@ namespace CodexDistTests.TestCore private void EnsureTestNamespace(Kubernetes client) { - if (activeNamespace != null) return; + if (IsTestNamespaceOnline(client)) return; var namespaceSpec = new V1Namespace { @@ -262,14 +261,15 @@ namespace CodexDistTests.TestCore Labels = new Dictionary { { "name", K8sNamespace } } } }; - activeNamespace = client.CreateNamespace(namespaceSpec); + client.CreateNamespace(namespaceSpec); } private void DeleteNamespace(Kubernetes client) { - if (activeNamespace == null) return; - client.DeleteNamespace(activeNamespace.Name(), null, null, gracePeriodSeconds: 0); - activeNamespace = null; + if (IsTestNamespaceOnline(client)) + { + client.DeleteNamespace(K8sNamespace, null, null, gracePeriodSeconds: 0); + } } #endregion @@ -281,6 +281,11 @@ namespace CodexDistTests.TestCore 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) { var n = (OnlineCodexNode)node; diff --git a/TestCore/OfflineCodexNode.cs b/TestCore/OfflineCodexNode.cs index 93c6ee2..7f3511c 100644 --- a/TestCore/OfflineCodexNode.cs +++ b/TestCore/OfflineCodexNode.cs @@ -4,7 +4,7 @@ { IOfflineCodexNode WithLogLevel(CodexLogLevel level); IOfflineCodexNode WithBootstrapNode(IOnlineCodexNode node); - IOfflineCodexNode WithStorageQuota(int storageQuotaBytes); + IOfflineCodexNode WithStorageQuota(ByteSize storageQuota); IOnlineCodexNode BringOnline(); } @@ -23,7 +23,7 @@ public CodexLogLevel? LogLevel { 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) { @@ -47,9 +47,9 @@ return this; } - public IOfflineCodexNode WithStorageQuota(int storageQuotaBytes) + public IOfflineCodexNode WithStorageQuota(ByteSize storageQuota) { - StorageQuota = storageQuotaBytes; + StorageQuota = storageQuota; return this; } @@ -58,7 +58,7 @@ var result = ""; if (LogLevel != null) result += $"LogLevel={LogLevel},"; if (BootstrapNode != null) result += "BootstrapNode=set,"; - if (StorageQuota != null) result += $"StorageQuote={StorageQuota},"; + if (StorageQuota != null) result += $"StorageQuote={StorageQuota.SizeInBytes},"; return result; } }