From faca5b284dafa5bb6cdb69c7407fb9ad07872428 Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Wed, 14 May 2025 10:25:16 +1000 Subject: [PATCH 1/3] add a --setup flag to sample ingest --- .../Cli/Commands/Sample/IngestCommand.cs | 21 ++++++++++++++++--- .../Cli/Commands/Sample/SetupCommand.cs | 6 ++++++ .../Sample/SampleIngestTestCase.cs | 21 +++++++++++++++++++ .../Support/CliCommandRunner.cs | 4 ++-- 4 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 test/SeqCli.EndToEnd/Sample/SampleIngestTestCase.cs diff --git a/src/SeqCli/Cli/Commands/Sample/IngestCommand.cs b/src/SeqCli/Cli/Commands/Sample/IngestCommand.cs index 19171ecb..251c00a0 100644 --- a/src/SeqCli/Cli/Commands/Sample/IngestCommand.cs +++ b/src/SeqCli/Cli/Commands/Sample/IngestCommand.cs @@ -32,6 +32,7 @@ class IngestCommand : Command readonly BatchSizeFeature _batchSize; bool _quiet; + bool _setup; int _simulations = 1; public IngestCommand(SeqConnectionFactory connectionFactory) @@ -41,6 +42,7 @@ public IngestCommand(SeqConnectionFactory connectionFactory) _connection = Enable(); Options.Add("quiet", "Don't echo ingested events to `STDOUT`", _ => _quiet = true); + Options.Add("setup", "Configure sample dashboards, signals, users, and so on before starting ingestion", _ => _setup = true); Options.Add("simulations=", "Number of concurrent simulations to run; the default runs a single simulation", v => _simulations = int.Parse(v)); @@ -51,14 +53,27 @@ protected override async Task Run() { var (url, apiKey) = _connectionFactory.GetConnectionDetails(_connection); var batchSize = _batchSize.Value; - - if (!_confirm.TryConfirm($"This will send sample events to the Seq server at {url}.")) - { + + if (!_confirm.TryConfirm(_setup + ? $"This will apply sample configuration and send sample events to the Seq server at {url}." + : $"This will send sample events to the Seq server at {url}." + )) { await Console.Error.WriteLineAsync("Canceled by user."); return 1; } var connection = _connectionFactory.Connect(_connection); + + if (_setup) + { + var setupResult = await SetupCommand.ImportTemplates(connection); + + if (setupResult != 0) + { + return setupResult; + } + } + var simulations = Enumerable.Range(0, _simulations) .Select(_ => Simulation.RunAsync(connection, apiKey, batchSize, echoToStdout: !_quiet)) .ToList(); diff --git a/src/SeqCli/Cli/Commands/Sample/SetupCommand.cs b/src/SeqCli/Cli/Commands/Sample/SetupCommand.cs index 8905c97c..e801ca10 100644 --- a/src/SeqCli/Cli/Commands/Sample/SetupCommand.cs +++ b/src/SeqCli/Cli/Commands/Sample/SetupCommand.cs @@ -22,6 +22,7 @@ using SeqCli.Templates.Ast; using SeqCli.Templates.Import; using SeqCli.Util; +using Seq.Api; // ReSharper disable once UnusedType.Global @@ -58,6 +59,11 @@ protected override async Task Run() return 1; } + return await ImportTemplates(connection); + } + + internal static async Task ImportTemplates(SeqConnection connection) + { var templateArgs = new Dictionary(); templateArgs["ownerId"] = new JsonTemplateNull(); diff --git a/test/SeqCli.EndToEnd/Sample/SampleIngestTestCase.cs b/test/SeqCli.EndToEnd/Sample/SampleIngestTestCase.cs new file mode 100644 index 00000000..f27e951f --- /dev/null +++ b/test/SeqCli.EndToEnd/Sample/SampleIngestTestCase.cs @@ -0,0 +1,21 @@ +using System.Linq; +using System.Threading.Tasks; +using Seq.Api; +using SeqCli.EndToEnd.Support; +using Serilog; +using Xunit; + +namespace SeqCli.EndToEnd.Sample; + +public class SampleIngestTestCase : ICliTestCase +{ + public async Task ExecuteAsync(SeqConnection connection, ILogger logger, CliCommandRunner runner) + { + runner.Exec("sample ingest", "--setup --confirm", timeout: TimeSpan.FromSeconds(3)); + + var sampleWorkspace = (await connection.Workspaces.ListAsync(shared: true)) + .SingleOrDefault(w => w.Title == "Sample"); + + Assert.NotNull(sampleWorkspace); + } +} \ No newline at end of file diff --git a/test/SeqCli.EndToEnd/Support/CliCommandRunner.cs b/test/SeqCli.EndToEnd/Support/CliCommandRunner.cs index 0ee0c4b6..f7853d02 100644 --- a/test/SeqCli.EndToEnd/Support/CliCommandRunner.cs +++ b/test/SeqCli.EndToEnd/Support/CliCommandRunner.cs @@ -10,10 +10,10 @@ public class CliCommandRunner(TestConfiguration configuration) public ITestProcess? LastRunProcess { get; private set; } - public int Exec(string command, string? args = null, bool disconnected = false) + public int Exec(string command, string? args = null, bool disconnected = false, TimeSpan? timeout = null) { using var process = configuration.SpawnCliProcess(command, args, skipServerArg: disconnected); LastRunProcess = process; - return process.WaitForExit(DefaultExecTimeout); + return process.WaitForExit(timeout ?? DefaultExecTimeout); } } \ No newline at end of file From ce981e85e0dda866a1092deb489ebe6ef0aa8e57 Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Wed, 14 May 2025 10:33:45 +1000 Subject: [PATCH 2/3] fix up sample ingest test case --- test/SeqCli.EndToEnd/Sample/SampleIngestTestCase.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/SeqCli.EndToEnd/Sample/SampleIngestTestCase.cs b/test/SeqCli.EndToEnd/Sample/SampleIngestTestCase.cs index f27e951f..d1f2db71 100644 --- a/test/SeqCli.EndToEnd/Sample/SampleIngestTestCase.cs +++ b/test/SeqCli.EndToEnd/Sample/SampleIngestTestCase.cs @@ -1,3 +1,4 @@ +using System; using System.Linq; using System.Threading.Tasks; using Seq.Api; From 2c981a640bdf49979b88de16cee97c581dafae45 Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Wed, 14 May 2025 11:12:30 +1000 Subject: [PATCH 3/3] update ingest case --- test/SeqCli.EndToEnd/Sample/SampleIngestTestCase.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/SeqCli.EndToEnd/Sample/SampleIngestTestCase.cs b/test/SeqCli.EndToEnd/Sample/SampleIngestTestCase.cs index d1f2db71..06e5020e 100644 --- a/test/SeqCli.EndToEnd/Sample/SampleIngestTestCase.cs +++ b/test/SeqCli.EndToEnd/Sample/SampleIngestTestCase.cs @@ -12,7 +12,17 @@ public class SampleIngestTestCase : ICliTestCase { public async Task ExecuteAsync(SeqConnection connection, ILogger logger, CliCommandRunner runner) { - runner.Exec("sample ingest", "--setup --confirm", timeout: TimeSpan.FromSeconds(3)); + try + { + runner.Exec("sample ingest", "--setup --confirm", timeout: TimeSpan.FromSeconds(3)); + } + catch + { + // Ignored + } + + var events = await connection.Events.ListAsync(); + Assert.NotEmpty(events); var sampleWorkspace = (await connection.Workspaces.ListAsync(shared: true)) .SingleOrDefault(w => w.Title == "Sample");