Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Launch configurations for local executable vs Docker/latest vs Docker/preview #340

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions test/SeqCli.EndToEnd/Args.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
using System.Linq;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.RegularExpressions;

#nullable enable

namespace SeqCli.EndToEnd;

public class Args
public class Args(params string[] args)
{
readonly string[] _args;

public Args(params string[] args)
{
_args = args;
}

public Regex[] TestCases() => _args
public Regex[] TestCases() => args
.Where(arg => !arg.StartsWith("--"))
.Select(ToArgRegex)
.ToArray();

// Simple replacement so `Events.*` becomes `Events\..*`
static Regex ToArgRegex(string arg) => new Regex(arg.Replace(".", "\\.").Replace("*", ".*"));
static Regex ToArgRegex(string arg) => new(arg.Replace(".", "\\.").Replace("*", ".*"));

public bool Multiuser() => _args.Any(a => a == "--license-certificate-stdin");
public bool Multiuser() => args.Any(a => a == "--license-certificate-stdin");

public bool UseDockerSeq() => _args.Any(a => a == "--docker-server");
}
public bool UseDockerSeq([NotNullWhen(true)] out string? imageTag)
{
if (args.Any(a => a == "--docker-server"))
{
imageTag = args.Any(a => a == "--pre") ? "preview" : "latest";
return true;
}

imageTag = null;
return false;
}
}
16 changes: 16 additions & 0 deletions test/SeqCli.EndToEnd/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"SeqCli.EndToEnd (Seq Executable)": {
"commandName": "Project"
},
"SeqCli.EndToEnd (datalust/seq:latest)": {
"commandName": "Project",
"commandLineArgs": "--docker-server"
},
"SeqCli.EndToEnd (datalust/seq:preview)": {
"commandName": "Project",
"commandLineArgs": "--docker-server --pre"
}
}
}
10 changes: 2 additions & 8 deletions test/SeqCli.EndToEnd/Support/CliCommandRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,15 @@

namespace SeqCli.EndToEnd.Support;

public class CliCommandRunner
public class CliCommandRunner(TestConfiguration configuration)
{
readonly TestConfiguration _configuration;
static readonly TimeSpan DefaultExecTimeout = TimeSpan.FromSeconds(10);

public ITestProcess? LastRunProcess { get; private set; }

public CliCommandRunner(TestConfiguration configuration)
{
_configuration = configuration;
}

public int Exec(string command, string? args = null, bool disconnected = false)
{
using var process = _configuration.SpawnCliProcess(command, args, skipServerArg: disconnected);
using var process = configuration.SpawnCliProcess(command, args, skipServerArg: disconnected);
LastRunProcess = process;
return process.WaitForExit(DefaultExecTimeout);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@

namespace SeqCli.EndToEnd.Support;

public class LicenseSetup
public class LicenseSetup(Args args)
{
readonly bool _enabled;
readonly bool _enabled = args.Multiuser();

bool _attempted;
string _certificate;

public LicenseSetup(Args args)
{
_enabled = args.Multiuser();
}

public async Task SetupAsync(
SeqConnection connection,
ILogger logger)
Expand Down
19 changes: 6 additions & 13 deletions test/SeqCli.EndToEnd/Support/TestConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@

namespace SeqCli.EndToEnd.Support;

public class TestConfiguration
public class TestConfiguration(Args args)
{
readonly Args _args;

public TestConfiguration(Args args)
{
_args = args;
}

static int ServerListenPort => 9989;

#pragma warning disable CA1822
Expand All @@ -24,7 +17,7 @@ public TestConfiguration(Args args)

public string TestedBinary => Path.Combine(EquivalentBaseDirectory, "seqcli.dll");

public bool IsMultiuser => _args.Multiuser();
public bool IsMultiuser => args.Multiuser();

public CaptiveProcess SpawnCliProcess(string command, string additionalArgs = null, Dictionary<string, string> environment = null, bool skipServerArg = false)
{
Expand All @@ -34,20 +27,20 @@ public CaptiveProcess SpawnCliProcess(string command, string additionalArgs = nu
if (!skipServerArg)
commandWithArgs += $" --server=\"{ServerListenUrl}\"";

var args = $"{TestedBinary} {commandWithArgs}";
return new CaptiveProcess("dotnet", args, environment);
return new CaptiveProcess("dotnet", $"{TestedBinary} {commandWithArgs}", environment);
}

public CaptiveProcess SpawnServerProcess(string storagePath)
{
if (storagePath == null) throw new ArgumentNullException(nameof(storagePath));

var commandWithArgs = $"run --listen=\"{ServerListenUrl}\" --storage=\"{storagePath}\"";
if (_args.UseDockerSeq())
if (args.UseDockerSeq(out var imageTag))
{
var containerName = Guid.NewGuid().ToString("n");
return new CaptiveProcess("docker", $"run --name {containerName} -it --rm -e ACCEPT_EULA=Y -p {ServerListenPort}:80 datalust/seq:latest", stopCommandFullExePath: "docker", stopCommandArgs: $"stop {containerName}");
return new CaptiveProcess("docker", $"run --name {containerName} -it --rm -e ACCEPT_EULA=Y -p {ServerListenPort}:80 datalust/seq:{imageTag}", stopCommandFullExePath: "docker", stopCommandArgs: $"stop {containerName}");
}

return new CaptiveProcess("seq", commandWithArgs);
}
}