From 3359b109299191f394513a7028916eba7fde0bbb Mon Sep 17 00:00:00 2001 From: benbierens Date: Sun, 26 Mar 2023 08:52:53 +0200 Subject: [PATCH] Extracts configurable components to config folder --- CodexDistTestCore/CodexNodeGroup.cs | 7 ++-- .../{ => Config}/CodexDockerImage.cs | 2 +- CodexDistTestCore/Config/FileManagerConfig.cs | 7 ++++ CodexDistTestCore/Config/K8sCluster.cs | 37 +++++++++++++++++++ CodexDistTestCore/Config/LogConfig.cs | 7 ++++ CodexDistTestCore/FileManager.cs | 8 ++-- CodexDistTestCore/K8sOperations.cs | 31 +++++++--------- CodexDistTestCore/OnlineCodexNode.cs | 6 ++- CodexDistTestCore/TestLog.cs | 6 +-- Tests/BasicTests/SimpleTests.cs | 1 + 10 files changed, 82 insertions(+), 30 deletions(-) rename CodexDistTestCore/{ => Config}/CodexDockerImage.cs (98%) create mode 100644 CodexDistTestCore/Config/FileManagerConfig.cs create mode 100644 CodexDistTestCore/Config/K8sCluster.cs create mode 100644 CodexDistTestCore/Config/LogConfig.cs diff --git a/CodexDistTestCore/CodexNodeGroup.cs b/CodexDistTestCore/CodexNodeGroup.cs index 8d8fda5..eb52eca 100644 --- a/CodexDistTestCore/CodexNodeGroup.cs +++ b/CodexDistTestCore/CodexNodeGroup.cs @@ -1,4 +1,5 @@ -using k8s.Models; +using CodexDistTestCore.Config; +using k8s.Models; using System.Collections; namespace CodexDistTestCore @@ -63,7 +64,7 @@ namespace CodexDistTestCore return new V1ObjectMeta { Name = "codex-test-entrypoint-" + OrderNumber, - NamespaceProperty = K8sOperations.K8sNamespace + NamespaceProperty = K8sCluster.K8sNamespace }; } @@ -72,7 +73,7 @@ namespace CodexDistTestCore return new V1ObjectMeta { Name = "codex-test-node-" + OrderNumber, - NamespaceProperty = K8sOperations.K8sNamespace + NamespaceProperty = K8sCluster.K8sNamespace }; } diff --git a/CodexDistTestCore/CodexDockerImage.cs b/CodexDistTestCore/Config/CodexDockerImage.cs similarity index 98% rename from CodexDistTestCore/CodexDockerImage.cs rename to CodexDistTestCore/Config/CodexDockerImage.cs index a7dd353..7cd938e 100644 --- a/CodexDistTestCore/CodexDockerImage.cs +++ b/CodexDistTestCore/Config/CodexDockerImage.cs @@ -1,6 +1,6 @@ using k8s.Models; -namespace CodexDistTestCore +namespace CodexDistTestCore.Config { public class CodexDockerImage { diff --git a/CodexDistTestCore/Config/FileManagerConfig.cs b/CodexDistTestCore/Config/FileManagerConfig.cs new file mode 100644 index 0000000..f7befc2 --- /dev/null +++ b/CodexDistTestCore/Config/FileManagerConfig.cs @@ -0,0 +1,7 @@ +namespace CodexDistTestCore.Config +{ + public class FileManagerConfig + { + public const string Folder = "TestDataFiles"; + } +} diff --git a/CodexDistTestCore/Config/K8sCluster.cs b/CodexDistTestCore/Config/K8sCluster.cs new file mode 100644 index 0000000..8f85b70 --- /dev/null +++ b/CodexDistTestCore/Config/K8sCluster.cs @@ -0,0 +1,37 @@ +using k8s; +using NUnit.Framework; + +namespace CodexDistTestCore.Config +{ + public class K8sCluster + { + public const string K8sNamespace = "codex-test-namespace"; + + public KubernetesClientConfiguration GetK8sClientConfig() + { + // todo: If the default KubeConfig file does not suffice, change it here: + return KubernetesClientConfiguration.BuildConfigFromConfigFile(); + } + + public string GetIp() + { + return "127.0.0.1"; + } + + public string GetNodeLabelForLocation(Location location) + { + switch (location) + { + case Location.Unspecified: + return string.Empty; + case Location.BensLaptop: + return "worker01"; + case Location.BensOldGamingMachine: + return "worker02"; + } + + Assert.Fail("Unknown location selected: " + location); + throw new InvalidOperationException(); + } + } +} diff --git a/CodexDistTestCore/Config/LogConfig.cs b/CodexDistTestCore/Config/LogConfig.cs new file mode 100644 index 0000000..a14470e --- /dev/null +++ b/CodexDistTestCore/Config/LogConfig.cs @@ -0,0 +1,7 @@ +namespace CodexDistTestCore.Config +{ + public class LogConfig + { + public const string LogRoot = "D:/CodexTestLogs"; + } +} diff --git a/CodexDistTestCore/FileManager.cs b/CodexDistTestCore/FileManager.cs index f884b0a..6a2daf7 100644 --- a/CodexDistTestCore/FileManager.cs +++ b/CodexDistTestCore/FileManager.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using CodexDistTestCore.Config; +using NUnit.Framework; namespace CodexDistTestCore { @@ -12,20 +13,19 @@ namespace CodexDistTestCore public class FileManager : IFileManager { public const int ChunkSize = 1024 * 1024; - private const string Folder = "TestDataFiles"; private readonly Random random = new Random(); private readonly List activeFiles = new List(); private readonly TestLog log; public FileManager(TestLog log) { - if (!Directory.Exists(Folder)) Directory.CreateDirectory(Folder); + if (!Directory.Exists(FileManagerConfig.Folder)) Directory.CreateDirectory(FileManagerConfig.Folder); this.log = log; } public TestFile CreateEmptyTestFile() { - var result = new TestFile(Path.Combine(Folder, Guid.NewGuid().ToString() + "_test.bin")); + var result = new TestFile(Path.Combine(FileManagerConfig.Folder, Guid.NewGuid().ToString() + "_test.bin")); File.Create(result.Filename).Close(); activeFiles.Add(result); return result; diff --git a/CodexDistTestCore/K8sOperations.cs b/CodexDistTestCore/K8sOperations.cs index 85c0de7..193c8dd 100644 --- a/CodexDistTestCore/K8sOperations.cs +++ b/CodexDistTestCore/K8sOperations.cs @@ -1,4 +1,5 @@ -using k8s; +using CodexDistTestCore.Config; +using k8s; using k8s.Models; using NUnit.Framework; @@ -6,9 +7,8 @@ namespace CodexDistTestCore { public class K8sOperations { - public const string K8sNamespace = "codex-test-namespace"; - private readonly CodexDockerImage dockerImage = new CodexDockerImage(); + private readonly K8sCluster k8SCluster = new K8sCluster(); private readonly Kubernetes client; private readonly KnownK8sPods knownPods; @@ -16,9 +16,7 @@ namespace CodexDistTestCore { this.knownPods = knownPods; - // todo: If the default KubeConfig file does not suffice, change it here: - var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); - client = new Kubernetes(config); + client = new Kubernetes(k8SCluster.GetK8sClientConfig()); } public void Close() @@ -220,18 +218,12 @@ namespace CodexDistTestCore private IDictionary CreateNodeSelector(OfflineCodexNodes offline) { - switch (offline.Location) - { - case Location.Unspecified: - return new Dictionary(); - case Location.BensLaptop: - return new Dictionary { { "codex-test-location", "worker01" } }; - case Location.BensOldGamingMachine: - return new Dictionary { { "codex-test-location", "worker02" } }; - } + if (offline.Location == Location.Unspecified) return new Dictionary(); - Assert.Fail("Unknown location selected: " + offline.Location); - throw new InvalidOperationException(); + return new Dictionary + { + { "codex-test-location", k8SCluster.GetNodeLabelForLocation(offline.Location) } + }; } private List CreateDeploymentContainers(CodexNodeGroup online, OfflineCodexNodes offline) @@ -293,6 +285,11 @@ namespace CodexDistTestCore } } + private string K8sNamespace + { + get { return K8sCluster.K8sNamespace; } + } + #endregion private bool IsTestNamespaceOnline() diff --git a/CodexDistTestCore/OnlineCodexNode.cs b/CodexDistTestCore/OnlineCodexNode.cs index d783656..76b38d7 100644 --- a/CodexDistTestCore/OnlineCodexNode.cs +++ b/CodexDistTestCore/OnlineCodexNode.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using CodexDistTestCore.Config; +using NUnit.Framework; namespace CodexDistTestCore { @@ -15,6 +16,7 @@ namespace CodexDistTestCore private const string SuccessfullyConnectedMessage = "Successfully connected to peer"; private const string UploadFailedMessage = "Unable to store block"; + private readonly K8sCluster k8SCluster = new K8sCluster(); private readonly TestLog log; private readonly IFileManager fileManager; @@ -101,7 +103,7 @@ namespace CodexDistTestCore private Http Http() { - return new Http(ip: "127.0.0.1", port: Container.ServicePort, baseUrl: "/api/codex/v1"); + return new Http(ip: k8SCluster.GetIp(), port: Container.ServicePort, baseUrl: "/api/codex/v1"); } private void Log(string msg) diff --git a/CodexDistTestCore/TestLog.cs b/CodexDistTestCore/TestLog.cs index 95aad2b..b9694e3 100644 --- a/CodexDistTestCore/TestLog.cs +++ b/CodexDistTestCore/TestLog.cs @@ -1,10 +1,10 @@ -using NUnit.Framework; +using CodexDistTestCore.Config; +using NUnit.Framework; namespace CodexDistTestCore { public class TestLog { - public const string LogRoot = "D:/CodexTestLogs"; private readonly LogFile file; public TestLog() @@ -105,7 +105,7 @@ namespace CodexDistTestCore var now = DateTime.UtcNow; filepath = Path.Join( - TestLog.LogRoot, + LogConfig.LogRoot, $"{now.Year}-{Pad(now.Month)}", Pad(now.Day)); diff --git a/Tests/BasicTests/SimpleTests.cs b/Tests/BasicTests/SimpleTests.cs index 39f1814..a56b09d 100644 --- a/Tests/BasicTests/SimpleTests.cs +++ b/Tests/BasicTests/SimpleTests.cs @@ -1,4 +1,5 @@ using CodexDistTestCore; +using CodexDistTestCore.Config; using NUnit.Framework; namespace Tests.BasicTests