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..06e5020e --- /dev/null +++ b/test/SeqCli.EndToEnd/Sample/SampleIngestTestCase.cs @@ -0,0 +1,32 @@ +using System; +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) + { + 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"); + + 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