From 3c724f62065f9e1db74b6b116f9d8cc6a183d46b Mon Sep 17 00:00:00 2001 From: benbierens Date: Wed, 13 Sep 2023 10:03:11 +0200 Subject: [PATCH] initialize plugins with constructor --- CodexPlugin/CodexPlugin.cs | 21 +++++------ CodexPlugin/CodexPlugin.csproj | 10 ------ CodexPlugin/OnlineCodexNode.cs | 2 +- Core/EntryPoint.cs | 57 +++++------------------------- Core/PluginManager.cs | 25 +++++-------- Core/ToolsProvider.cs | 49 +++++++++++++++++++++++++ DistTestCore/DistTest.cs | 7 ++-- DistTestCore/TestLifecycle.cs | 23 ++++++++---- FileUtils/FileManager.cs | 18 +++++----- MetricsPlugin/GrafanaStarter.cs | 5 ++- MetricsPlugin/MetricsPlugin.cs | 11 ++---- MetricsPlugin/MetricsPlugin.csproj | 14 ++++++++ cs-codex-dist-testing.sln | 8 ++++- 13 files changed, 134 insertions(+), 116 deletions(-) create mode 100644 Core/ToolsProvider.cs diff --git a/CodexPlugin/CodexPlugin.cs b/CodexPlugin/CodexPlugin.cs index 7f20f6f..5602678 100644 --- a/CodexPlugin/CodexPlugin.cs +++ b/CodexPlugin/CodexPlugin.cs @@ -1,26 +1,27 @@ using Core; using KubernetesWorkflow; -using Logging; namespace CodexPlugin { public class CodexPlugin : IProjectPlugin { - private CodexStarter codexStarter = null!; + private readonly CodexStarter codexStarter; + private readonly IPluginTools tools; + + public CodexPlugin(IPluginTools tools) + { + codexStarter = new CodexStarter(tools); + this.tools = tools; + } #region IProjectPlugin Implementation - public void Announce(ILog log) + public void Announce() { - log.Log("hello from codex plugin. codex container info here."); + tools.GetLog().Log("hello from codex plugin. codex container info here."); } - public void Initialize(IPluginTools tools) - { - codexStarter = new CodexStarter(tools); - } - - public void Finalize(ILog log) + public void Decommission() { } diff --git a/CodexPlugin/CodexPlugin.csproj b/CodexPlugin/CodexPlugin.csproj index e17d31e..1f910c4 100644 --- a/CodexPlugin/CodexPlugin.csproj +++ b/CodexPlugin/CodexPlugin.csproj @@ -6,16 +6,6 @@ enable - - - - - - - Never - - - diff --git a/CodexPlugin/OnlineCodexNode.cs b/CodexPlugin/OnlineCodexNode.cs index 7895076..cbd0dcd 100644 --- a/CodexPlugin/OnlineCodexNode.cs +++ b/CodexPlugin/OnlineCodexNode.cs @@ -83,7 +83,7 @@ namespace CodexPlugin { var logMessage = $"Downloading for contentId: '{contentId.Id}'..."; Log(logMessage); - var file = tools.GetFileManager().CreateEmptyTestFile(fileLabel); + var file = tools.GetFileManager().CreateEmptyFile(fileLabel); Stopwatch.Measure(tools.GetLog(), logMessage, () => DownloadToFile(contentId.Id, file)); Log($"Downloaded file {file.Describe()} to '{file.Filename}'."); return file; diff --git a/Core/EntryPoint.cs b/Core/EntryPoint.cs index aa061ad..a9d6d5b 100644 --- a/Core/EntryPoint.cs +++ b/Core/EntryPoint.cs @@ -1,26 +1,16 @@ -using FileUtils; -using KubernetesWorkflow; +using KubernetesWorkflow; using Logging; -using Utils; namespace Core { - public class EntryPoint : IPluginTools + public class EntryPoint { private readonly PluginManager manager = new PluginManager(); - private readonly ILog log; - private readonly ITimeSet timeSet; - private readonly FileManager fileManager; - private readonly WorkflowCreator workflowCreator; - + public EntryPoint(ILog log, Configuration configuration, string fileManagerRootFolder, ITimeSet timeSet) { - this.log = log; - this.timeSet = timeSet; - fileManager = new FileManager(log, fileManagerRootFolder); - workflowCreator = new WorkflowCreator(log, configuration); - - manager.InstantiatePlugins(PluginFinder.GetPluginTypes()); + Tools = new ToolsProvider(log, configuration, fileManagerRootFolder, timeSet); + manager.InstantiatePlugins(PluginFinder.GetPluginTypes(), Tools); } public EntryPoint(ILog log, Configuration configuration, string fileManagerRootFolder) @@ -28,14 +18,11 @@ namespace Core { } + public IPluginTools Tools { get; } + public void Announce() { - manager.AnnouncePlugins(log); - } - - public void Initialize() - { - manager.InitializePlugins(this); + manager.AnnouncePlugins(); } public CoreInterface CreateInterface() @@ -45,38 +32,12 @@ namespace Core public void Decommission() { - manager.FinalizePlugins(log); + manager.DecommissionPlugins(); } internal T GetPlugin() where T : IProjectPlugin { return manager.GetPlugin(); } - - public Http CreateHttp(Address address, string baseUrl, Action onClientCreated, string? logAlias = null) - { - return new Http(log, timeSet, address, baseUrl, onClientCreated, logAlias); - } - - public Http CreateHttp(Address address, string baseUrl, string? logAlias = null) - { - return new Http(log, timeSet, address, baseUrl, logAlias); - } - - public IStartupWorkflow CreateWorkflow(string? namespaceOverride = null) - { - if (namespaceOverride != null) throw new Exception("Namespace override is not supported in the DistTest environment. (It would mess up automatic resource cleanup.)"); - return workflowCreator.CreateWorkflow(); - } - - public IFileManager GetFileManager() - { - return fileManager; - } - - public ILog GetLog() - { - return log; - } } } diff --git a/Core/PluginManager.cs b/Core/PluginManager.cs index c8cf8a4..81dbcee 100644 --- a/Core/PluginManager.cs +++ b/Core/PluginManager.cs @@ -1,7 +1,6 @@ using FileUtils; using KubernetesWorkflow; using Logging; -using System.Reflection; using Utils; namespace Core @@ -10,29 +9,24 @@ namespace Core { private readonly List projectPlugins = new List(); - public void InstantiatePlugins(Type[] pluginTypes) + public void InstantiatePlugins(Type[] pluginTypes, IPluginTools tools) { projectPlugins.Clear(); foreach (var pluginType in pluginTypes) { - var plugin = (IProjectPlugin)Activator.CreateInstance(pluginType)!; + var plugin = (IProjectPlugin)Activator.CreateInstance(pluginType, args: tools)!; projectPlugins.Add(plugin); } } - public void AnnouncePlugins(ILog log) + public void AnnouncePlugins() { - foreach (var plugin in projectPlugins) plugin.Announce(log); + foreach (var plugin in projectPlugins) plugin.Announce(); } - public void InitializePlugins(IPluginTools tools) + public void DecommissionPlugins() { - foreach (var plugin in projectPlugins) plugin.Initialize(tools); - } - - public void FinalizePlugins(ILog log) - { - foreach (var plugin in projectPlugins) plugin.Finalize(log); + foreach (var plugin in projectPlugins) plugin.Decommission(); } public T GetPlugin() where T : IProjectPlugin @@ -43,13 +37,12 @@ namespace Core public interface IProjectPlugin { - void Announce(ILog log); - void Initialize(IPluginTools tools); - void Finalize(ILog log); + void Announce(); + void Decommission(); } public interface IPluginTools : IWorkflowTool, ILogTool, IHttpFactoryTool, IFileTool - { + { } public interface IWorkflowTool diff --git a/Core/ToolsProvider.cs b/Core/ToolsProvider.cs new file mode 100644 index 0000000..81a5010 --- /dev/null +++ b/Core/ToolsProvider.cs @@ -0,0 +1,49 @@ +using FileUtils; +using KubernetesWorkflow; +using Logging; +using Utils; + +namespace Core +{ + public class ToolsProvider : IPluginTools + { + private readonly ILog log; + private readonly ITimeSet timeSet; + private readonly WorkflowCreator workflowCreator; + private readonly IFileManager fileManager; + + public ToolsProvider(ILog log, Configuration configuration, string fileManagerRootFolder, ITimeSet timeSet) + { + this.log = log; + this.timeSet = timeSet; + fileManager = new FileManager(log, fileManagerRootFolder); + workflowCreator = new WorkflowCreator(log, configuration); + } + + public Http CreateHttp(Address address, string baseUrl, Action onClientCreated, string? logAlias = null) + { + return new Http(log, timeSet, address, baseUrl, onClientCreated, logAlias); + } + + public Http CreateHttp(Address address, string baseUrl, string? logAlias = null) + { + return new Http(log, timeSet, address, baseUrl, logAlias); + } + + public IStartupWorkflow CreateWorkflow(string? namespaceOverride = null) + { + if (namespaceOverride != null) throw new Exception("Namespace override is not supported in the DistTest environment. (It would mess up automatic resource cleanup.)"); + return workflowCreator.CreateWorkflow(); + } + + public IFileManager GetFileManager() + { + return fileManager; + } + + public ILog GetLog() + { + return log; + } + } +} diff --git a/DistTestCore/DistTest.cs b/DistTestCore/DistTest.cs index 594b8d3..e5b202f 100644 --- a/DistTestCore/DistTest.cs +++ b/DistTestCore/DistTest.cs @@ -45,7 +45,7 @@ namespace DistTestCore { Stopwatch.Measure(fixtureLog, "Global setup", () => { - globalEntryPoint.CreateWorkflow().DeleteNamespacesStartingWith(TestNamespacePrefix); + globalEntryPoint.Tools.CreateWorkflow().DeleteNamespacesStartingWith(TestNamespacePrefix); }); } catch (Exception ex) @@ -101,7 +101,7 @@ namespace DistTestCore public TrackedFile GenerateTestFile(ByteSize size, string label = "") { - return Get().EntryPoint.GetFileManager().GenerateTestFile(size, label); + return Get().GenerateTestFile(size, label); } /// @@ -110,7 +110,7 @@ namespace DistTestCore /// public void ScopedTestFiles(Action action) { - Get().EntryPoint.GetFileManager().ScopedFiles(action); + Get().ScopedTestFiles(action); } //public IOnlineCodexNode SetupCodexBootstrapNode() @@ -240,7 +240,6 @@ namespace DistTestCore WriteEndTestLog(lifecycle.Log); IncludeLogsAndMetricsOnTestFailure(lifecycle); - lifecycle.EntryPoint.Decommission(); lifecycle.DeleteAllResources(); lifecycle = null!; }); diff --git a/DistTestCore/TestLifecycle.cs b/DistTestCore/TestLifecycle.cs index 66be2e1..53b95e9 100644 --- a/DistTestCore/TestLifecycle.cs +++ b/DistTestCore/TestLifecycle.cs @@ -1,4 +1,5 @@ using Core; +using FileUtils; using Logging; using Utils; @@ -7,6 +8,7 @@ namespace DistTestCore public class TestLifecycle { private readonly DateTime testStart; + private readonly EntryPoint entryPoint; public TestLifecycle(TestLog log, Configuration configuration, ITimeSet timeSet, string testNamespace) { @@ -15,9 +17,8 @@ namespace DistTestCore TimeSet = timeSet; testStart = DateTime.UtcNow; - EntryPoint = new EntryPoint(log, configuration.GetK8sConfiguration(timeSet, testNamespace), configuration.GetFileManagerFolder(), timeSet); - EntryPoint.Initialize(); - CoreInterface = EntryPoint.CreateInterface(); + entryPoint = new EntryPoint(log, configuration.GetK8sConfiguration(timeSet, testNamespace), configuration.GetFileManagerFolder(), timeSet); + CoreInterface = entryPoint.CreateInterface(); log.WriteLogTag(); } @@ -25,13 +26,23 @@ namespace DistTestCore public TestLog Log { get; } public Configuration Configuration { get; } public ITimeSet TimeSet { get; } - public EntryPoint EntryPoint { get; } public CoreInterface CoreInterface { get; } public void DeleteAllResources() { - EntryPoint.CreateWorkflow().DeleteNamespace(); - EntryPoint.GetFileManager().DeleteAllTestFiles(); + entryPoint.Tools.CreateWorkflow().DeleteNamespace(); + entryPoint.Tools.GetFileManager().DeleteAllFiles(); + entryPoint.Decommission(); + } + + public TrackedFile GenerateTestFile(ByteSize size, string label = "") + { + return entryPoint.Tools.GetFileManager().GenerateFile(size, label); + } + + public void ScopedTestFiles(Action action) + { + entryPoint.Tools.GetFileManager().ScopedFiles(action); } //public IDownloadedLog DownloadLog(RunningContainer container, int? tailLines = null) diff --git a/FileUtils/FileManager.cs b/FileUtils/FileManager.cs index 6d389ef..33641ab 100644 --- a/FileUtils/FileManager.cs +++ b/FileUtils/FileManager.cs @@ -6,9 +6,9 @@ namespace FileUtils { public interface IFileManager { - TrackedFile CreateEmptyTestFile(string label = ""); - TrackedFile GenerateTestFile(ByteSize size, string label = ""); - void DeleteAllTestFiles(); + TrackedFile CreateEmptyFile(string label = ""); + TrackedFile GenerateFile(ByteSize size, string label = ""); + void DeleteAllFiles(); void ScopedFiles(Action action); T ScopedFiles(Func action); } @@ -30,7 +30,7 @@ namespace FileUtils this.log = log; } - public TrackedFile CreateEmptyTestFile(string label = "") + public TrackedFile CreateEmptyFile(string label = "") { var path = Path.Combine(folder, Guid.NewGuid().ToString() + "_test.bin"); var result = new TrackedFile(log, path, label); @@ -39,15 +39,15 @@ namespace FileUtils return result; } - public TrackedFile GenerateTestFile(ByteSize size, string label) + public TrackedFile GenerateFile(ByteSize size, string label) { var sw = Stopwatch.Begin(log); - var result = GenerateFile(size, label); + var result = GenerateRandomFile(size, label); sw.End($"Generated file '{result.Describe()}'."); return result; } - public void DeleteAllTestFiles() + public void DeleteAllFiles() { DeleteDirectory(); } @@ -88,9 +88,9 @@ namespace FileUtils } } - private TrackedFile GenerateFile(ByteSize size, string label) + private TrackedFile GenerateRandomFile(ByteSize size, string label) { - var result = CreateEmptyTestFile(label); + var result = CreateEmptyFile(label); CheckSpaceAvailable(result, size); GenerateFileBytes(result, size); diff --git a/MetricsPlugin/GrafanaStarter.cs b/MetricsPlugin/GrafanaStarter.cs index b2f14f7..7a30a65 100644 --- a/MetricsPlugin/GrafanaStarter.cs +++ b/MetricsPlugin/GrafanaStarter.cs @@ -2,13 +2,12 @@ namespace MetricsPlugin { - public class GrafanaStarter : BaseStarter + public class GrafanaStarter { private const string StorageQuotaThresholdReplaceToken = "\"\""; private const string BytesUsedGraphAxisSoftMaxReplaceToken = "\"\""; - public GrafanaStarter(TestLifecycle lifecycle) - : base(lifecycle) + public GrafanaStarter() { } diff --git a/MetricsPlugin/MetricsPlugin.cs b/MetricsPlugin/MetricsPlugin.cs index 8d6fc26..8566a2e 100644 --- a/MetricsPlugin/MetricsPlugin.cs +++ b/MetricsPlugin/MetricsPlugin.cs @@ -1,6 +1,5 @@ using Core; using KubernetesWorkflow; -using Logging; namespace MetricsPlugin { @@ -9,16 +8,12 @@ namespace MetricsPlugin #region IProjectPlugin Implementation - public void Announce(ILog log) + public void Announce() { - log.Log("Hi from the metrics plugin."); + //log.Log("Hi from the metrics plugin."); } - public void Initialize(IPluginTools tools) - { - } - - public void Finalize(ILog log) + public void Decommission() { } diff --git a/MetricsPlugin/MetricsPlugin.csproj b/MetricsPlugin/MetricsPlugin.csproj index cfadb03..ec0e3ef 100644 --- a/MetricsPlugin/MetricsPlugin.csproj +++ b/MetricsPlugin/MetricsPlugin.csproj @@ -6,4 +6,18 @@ enable + + + + + + + Never + + + + + + + diff --git a/cs-codex-dist-testing.sln b/cs-codex-dist-testing.sln index 9cd1c7e..4065079 100644 --- a/cs-codex-dist-testing.sln +++ b/cs-codex-dist-testing.sln @@ -27,7 +27,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FileUtils", "FileUtils\File EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodexPlugin", "CodexPlugin\CodexPlugin.csproj", "{DE4E802C-288C-45C4-84B6-8A5A6A96EF49}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core", "Core\Core.csproj", "{F2BF34B3-C660-43EF-BD42-BC5C60237FC4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Core", "Core\Core.csproj", "{F2BF34B3-C660-43EF-BD42-BC5C60237FC4}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MetricsPlugin", "MetricsPlugin\MetricsPlugin.csproj", "{FCC74AF1-463D-4E5A-9FE7-B4A13F7C8820}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -87,6 +89,10 @@ Global {F2BF34B3-C660-43EF-BD42-BC5C60237FC4}.Debug|Any CPU.Build.0 = Debug|Any CPU {F2BF34B3-C660-43EF-BD42-BC5C60237FC4}.Release|Any CPU.ActiveCfg = Release|Any CPU {F2BF34B3-C660-43EF-BD42-BC5C60237FC4}.Release|Any CPU.Build.0 = Release|Any CPU + {FCC74AF1-463D-4E5A-9FE7-B4A13F7C8820}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FCC74AF1-463D-4E5A-9FE7-B4A13F7C8820}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FCC74AF1-463D-4E5A-9FE7-B4A13F7C8820}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FCC74AF1-463D-4E5A-9FE7-B4A13F7C8820}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE