From 5d66e900f17f8f85cc2acf599559518eb74952ee Mon Sep 17 00:00:00 2001 From: benbierens Date: Sun, 26 Mar 2023 10:57:54 +0200 Subject: [PATCH] Allows disabling of log download on test failure with test attirbute --- CodexDistTestCore/DistTest.cs | 16 +++- CodexDistTestCore/PodLogDownloader.cs | 15 ++- Tests/BasicTests/SimpleTests.cs | 130 +++++++++++++------------- 3 files changed, 92 insertions(+), 69 deletions(-) diff --git a/CodexDistTestCore/DistTest.cs b/CodexDistTestCore/DistTest.cs index eecc4eb..1331c6a 100644 --- a/CodexDistTestCore/DistTest.cs +++ b/CodexDistTestCore/DistTest.cs @@ -83,7 +83,15 @@ namespace CodexDistTestCore var result = TestContext.CurrentContext.Result; if (result.Outcome.Status == NUnit.Framework.Interfaces.TestStatus.Failed) { - k8sManager.ForEachOnlineGroup(DownloadLogs); + if (IsDownloadingLogsEnabled()) + { + log.Log("Downloading all CodexNode logs because of test failure..."); + k8sManager.ForEachOnlineGroup(DownloadLogs); + } + else + { + log.Log("Skipping download of all CodexNode logs due to [DontDownloadLogsOnFailure] attribute."); + } } } @@ -96,6 +104,12 @@ namespace CodexDistTestCore downloader.DownloadLog(n); } } + + private bool IsDownloadingLogsEnabled() + { + var testProperties = TestContext.CurrentContext.Test.Properties; + return !testProperties.ContainsKey(PodLogDownloader.DontDownloadLogsOnFailureKey); + } } public static class GlobalTestFailure diff --git a/CodexDistTestCore/PodLogDownloader.cs b/CodexDistTestCore/PodLogDownloader.cs index b6a2ed7..ce379d7 100644 --- a/CodexDistTestCore/PodLogDownloader.cs +++ b/CodexDistTestCore/PodLogDownloader.cs @@ -1,12 +1,25 @@ -namespace CodexDistTestCore +using NUnit.Framework; + +namespace CodexDistTestCore { public interface IPodLogHandler { void Log(Stream log); } + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] + public class DontDownloadLogsOnFailureAttribute : PropertyAttribute + { + public DontDownloadLogsOnFailureAttribute() + : base(Timing.UseLongTimeoutsKey) + { + } + } + public class PodLogDownloader { + public const string DontDownloadLogsOnFailureKey = "DontDownloadLogsOnFailure"; + private readonly TestLog log; private readonly IK8sManager k8SManager; diff --git a/Tests/BasicTests/SimpleTests.cs b/Tests/BasicTests/SimpleTests.cs index 3a072e6..f431f2c 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 @@ -7,87 +8,82 @@ namespace Tests.BasicTests public class SimpleTests : DistTest { [Test] + public void GetDebugInfo() + { + var dockerImage = new CodexDockerImage(); + + var node = SetupCodexNodes(1).BringOnline()[0]; + + var debugInfo = node.GetDebugInfo(); + + Assert.That(debugInfo.spr, Is.Not.Empty); + Assert.That(debugInfo.codex.revision, Is.EqualTo(dockerImage.GetExpectedImageRevision())); + } + + [Test, DontDownloadLogsOnFailure] public void CanAccessLogs() + { + var node = SetupCodexNodes(1).BringOnline()[0]; + + var log = node.DownloadLog(); + + log.AssertLogContains("Started codex node"); + } + + [Test] + public void OneClientTest() + { + var primary = SetupCodexNodes(1).BringOnline()[0]; + + var testFile = GenerateTestFile(1.MB()); + + var contentId = primary.UploadFile(testFile); + + var downloadedFile = primary.DownloadContent(contentId); + + testFile.AssertIsEqual(downloadedFile); + } + + [Test] + public void TwoClientsOnePodTest() { var group = SetupCodexNodes(2).BringOnline(); - var log = group[0].DownloadLog(); + var primary = group[0]; + var secondary = group[1]; - log.AssertLogContains("topics=\"discv5\""); + PerformTwoClientTest(primary, secondary); } - //[Test] - //public void GetDebugInfo() - //{ - // var dockerImage = new CodexDockerImage(); + [Test] + public void TwoClientsTwoPodsTest() + { + var primary = SetupCodexNodes(1).BringOnline()[0]; - // var node = SetupCodexNodes(1) - // .BringOnline()[0]; + var secondary = SetupCodexNodes(1).BringOnline()[0]; - // var debugInfo = node.GetDebugInfo(); + PerformTwoClientTest(primary, secondary); + } - // Assert.That(debugInfo.spr, Is.Not.Empty); - // Assert.That(debugInfo.codex.revision, Is.EqualTo(dockerImage.GetExpectedImageRevision())); - //} + [Test] + public void TwoClientsTwoLocationsTest() + { + var primary = SetupCodexNodes(1) + .At(Location.BensLaptop) + .BringOnline()[0]; - //[Test] - //public void OneClientTest() - //{ - // var primary = SetupCodexNodes(1) - // .BringOnline()[0]; + var secondary = SetupCodexNodes(1) + .At(Location.BensOldGamingMachine) + .BringOnline()[0]; - // var testFile = GenerateTestFile(1.MB()); + var debugInfo = primary.GetDebugInfo(); + Assert.That(debugInfo.spr, Is.Not.Empty); - // var contentId = primary.UploadFile(testFile); + var debugInfo2 = secondary.GetDebugInfo(); + Assert.That(debugInfo2.spr, Is.Not.Empty); - // var downloadedFile = primary.DownloadContent(contentId); - - // testFile.AssertIsEqual(downloadedFile); - //} - - //[Test] - //public void TwoClientsOnePodTest() - //{ - // var group = SetupCodexNodes(2) - // .BringOnline(); - - // var primary = group[0]; - // var secondary = group[1]; - - // PerformTwoClientTest(primary, secondary); - //} - - //[Test] - //public void TwoClientsTwoPodsTest() - //{ - // var primary = SetupCodexNodes(1) - // .BringOnline()[0]; - - // var secondary = SetupCodexNodes(1) - // .BringOnline()[0]; - - // PerformTwoClientTest(primary, secondary); - //} - - //[Test] - //public void TwoClientsTwoLocationsTest() - //{ - // var primary = SetupCodexNodes(1) - // .At(Location.BensLaptop) - // .BringOnline()[0]; - - // var secondary = SetupCodexNodes(1) - // .At(Location.BensOldGamingMachine) - // .BringOnline()[0]; - - // var debugInfo = primary.GetDebugInfo(); - // Assert.That(debugInfo.spr, Is.Not.Empty); - - // var debugInfo2 = secondary.GetDebugInfo(); - // Assert.That(debugInfo2.spr, Is.Not.Empty); - - // PerformTwoClientTest(primary, secondary); - //} + PerformTwoClientTest(primary, secondary); + } private void PerformTwoClientTest(IOnlineCodexNode primary, IOnlineCodexNode secondary) {