From f7edfd4eee7cb6928356af59c75774f8c25dcb4f Mon Sep 17 00:00:00 2001 From: benbierens Date: Wed, 28 Jun 2023 11:01:10 +0200 Subject: [PATCH] Checking for collisions on custom k8s namespaces and account indices --- ContinuousTests/ContinuousTest.cs | 2 + ContinuousTests/StartupChecker.cs | 75 +++++++++++++++++++++--- ContinuousTests/Tests/MarketplaceTest.cs | 5 +- 3 files changed, 70 insertions(+), 12 deletions(-) diff --git a/ContinuousTests/ContinuousTest.cs b/ContinuousTests/ContinuousTest.cs index 3f0c90e..1d4fb23 100644 --- a/ContinuousTests/ContinuousTest.cs +++ b/ContinuousTests/ContinuousTest.cs @@ -38,6 +38,8 @@ namespace ContinuousTests public abstract int RequiredNumberOfNodes { get; } public abstract TimeSpan RunTestEvery { get; } public abstract TestFailMode TestFailMode { get; } + public virtual int EthereumAccountIndex { get { return -1; } } + public virtual string CustomK8sNamespace { get { return string.Empty; } } public string Name { diff --git a/ContinuousTests/StartupChecker.cs b/ContinuousTests/StartupChecker.cs index 8000b5b..12e34cc 100644 --- a/ContinuousTests/StartupChecker.cs +++ b/ContinuousTests/StartupChecker.cs @@ -1,6 +1,7 @@ using DistTestCore.Codex; using DistTestCore; using Logging; +using NUnit.Framework.Internal; namespace ContinuousTests { @@ -40,20 +41,12 @@ namespace ContinuousTests handle.GetLastMoment(); } - var errors = new List(); - foreach (var test in tests) - { - if (test.RequiredNumberOfNodes > config.CodexDeployment.CodexContainers.Length) - { - errors.Add($"Test '{test.Name}' requires {test.RequiredNumberOfNodes} nodes. Deployment only has {config.CodexDeployment.CodexContainers.Length}"); - } - } - if (!Directory.Exists(config.LogPath)) { Directory.CreateDirectory(config.LogPath); } + var errors = CheckTests(tests); if (errors.Any()) { throw new Exception("Prerun check failed: " + string.Join(", ", errors)); @@ -98,5 +91,69 @@ namespace ContinuousTests return true; } + private List CheckTests(ContinuousTest[] tests) + { + var errors = new List(); + CheckRequiredNumberOfNodes(tests, errors); + CheckCustomNamespaceClashes(tests, errors); + CheckEthereumIndexClashes(tests, errors); + return errors; + } + + private void CheckEthereumIndexClashes(ContinuousTest[] tests, List errors) + { + var offLimits = config.CodexDeployment.CodexContainers.Length; + foreach (var test in tests) + { + if (test.EthereumAccountIndex != -1) + { + if (test.EthereumAccountIndex <= offLimits) + { + errors.Add($"Test '{test.Name}' has selected 'EthereumAccountIndex' = {test.EthereumAccountIndex}. All accounts up to and including {offLimits} are being used by the targetted Codex net. Select a different 'EthereumAccountIndex'."); + } + } + } + + DuplicatesCheck(tests, errors, + considerCondition: t => t.EthereumAccountIndex != -1, + getValue: t => t.EthereumAccountIndex, + propertyName: nameof(ContinuousTest.EthereumAccountIndex)); + } + + private void CheckCustomNamespaceClashes(ContinuousTest[] tests, List errors) + { + DuplicatesCheck(tests, errors, + considerCondition: t => !string.IsNullOrEmpty(t.CustomK8sNamespace), + getValue: t => t.CustomK8sNamespace, + propertyName: nameof(ContinuousTest.CustomK8sNamespace)); + } + + private void DuplicatesCheck(ContinuousTest[] tests, List errors, Func considerCondition, Func getValue, string propertyName) + { + foreach (var test in tests) + { + if (considerCondition(test)) + { + var duplicates = tests.Where(t => t != test && getValue(t) == getValue(test)).ToList(); + if (duplicates.Any()) + { + duplicates.Add(test); + errors.Add($"Tests '{string.Join(",", duplicates.Select(d => d.Name))}' have the same '{propertyName}'. These must be unique."); + return; + } + } + } + } + + private void CheckRequiredNumberOfNodes(ContinuousTest[] tests, List errors) + { + foreach (var test in tests) + { + if (test.RequiredNumberOfNodes > config.CodexDeployment.CodexContainers.Length) + { + errors.Add($"Test '{test.Name}' requires {test.RequiredNumberOfNodes} nodes. Deployment only has {config.CodexDeployment.CodexContainers.Length}"); + } + } + } } } diff --git a/ContinuousTests/Tests/MarketplaceTest.cs b/ContinuousTests/Tests/MarketplaceTest.cs index 9c9a6a9..5728068 100644 --- a/ContinuousTests/Tests/MarketplaceTest.cs +++ b/ContinuousTests/Tests/MarketplaceTest.cs @@ -13,9 +13,8 @@ namespace ContinuousTests.Tests public override int RequiredNumberOfNodes => 1; public override TimeSpan RunTestEvery => TimeSpan.FromMinutes(15); public override TestFailMode TestFailMode => TestFailMode.StopAfterFirstFailure; - - public const int EthereumAccountIndex = 200; // TODO: Check against all other account indices of all other tests. - public const string MarketplaceTestNamespace = "codex-continuous-marketplace"; // prevent clashes too + public override int EthereumAccountIndex => 200; + public override string CustomK8sNamespace => "codex-continuous-marketplace"; private readonly uint numberOfSlots = 3; private readonly ByteSize fileSize = 10.MB();