- Implemented SetupSmokeTests.cs to validate installation prerequisites and configurations. - Created SetupWizard.cs for user interaction during the setup process, including folder selection and variable input. - Added build.ps1 script to automate the build process and run smoke tests. - Introduced compose.override.template.yml for Docker configuration management. - Defined setup.config.json to manage repository settings, prerequisites, and installation variables.
168 lines
10 KiB
C#
168 lines
10 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Web.Script.Serialization;
|
|
using System.Windows.Forms;
|
|
|
|
namespace SeLtd.Setup
|
|
{
|
|
internal static class SetupSmokeTests
|
|
{
|
|
[STAThread]
|
|
private static void Main(string[] args)
|
|
{
|
|
string source = Path.GetFullPath(args[0]);
|
|
string root = Path.Combine(source, "dist", "smoke-" + Guid.NewGuid().ToString("N"));
|
|
var repo = Directory.CreateDirectory(Path.Combine(root, "fixture repository")).FullName;
|
|
var config = Directory.CreateDirectory(Path.Combine(root, "config")).FullName;
|
|
foreach (string file in new[] { ".env.example", "docker-compose.yml" })
|
|
File.Copy(Path.Combine(source, "..", file), Path.Combine(repo, file));
|
|
var init = Directory.CreateDirectory(Path.Combine(repo, "docker", "mariadb", "init")).FullName;
|
|
File.Copy(Path.Combine(source, "..", "docker", "mariadb", "init", "01-init.sql"), Path.Combine(init, "01-init.sql"));
|
|
Git(repo, "init -b main");
|
|
Git(repo, "add .");
|
|
Git(repo, "-c user.name=Smoke -c user.email=smoke@example.com -c commit.gpgsign=false commit -m fixture");
|
|
File.Copy(Path.Combine(source, "setup.config.json"), Path.Combine(config, "setup.config.json"));
|
|
File.Copy(Path.Combine(source, "compose.override.template.yml"), Path.Combine(config, "compose.override.template.yml"));
|
|
|
|
var setup = new SetupEngine(config);
|
|
setup.Config.repository.url = new Uri(repo).AbsoluteUri;
|
|
setup.Config.install = setup.Config.install.Where(step =>
|
|
step.action == "writeSetup" || (step.action == "command" && step.arguments.Contains("config"))).ToArray();
|
|
foreach (var prerequisite in setup.Config.prerequisites.Where(item => item.id == "git" || item.id == "docker"))
|
|
Assert(setup.Check(prerequisite), prerequisite.id + " prerequisite");
|
|
Application.EnableVisualStyles();
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
using (var wizard = new SetupWizard(setup)) Assert(wizard.Text == "SE-LTD Setup", "wizard initialization");
|
|
|
|
foreach (bool legacy in new[] { false, true })
|
|
{
|
|
string target = Path.Combine(root, legacy ? "legacy install" : "new install");
|
|
setup.Prepare(target, Console.WriteLine);
|
|
Assert(!setup.ExistingEnvironment && !File.Exists(Path.Combine(target, ".env")), "no env before confirmation");
|
|
Assert(setup.Values["ADMIN_USERNAME"] == "admin", "default admin");
|
|
Assert(setup.Values["ADMIN_PASSWORD"].Length == 64, "secure password length");
|
|
Assert(setup.Values["ADMIN_PASSWORD"] != setup.Values["REDIS_PASSWORD"], "independent secrets");
|
|
setup.Legacy = setup.Remote = legacy;
|
|
setup.Values["VITE_API_PORT"] = "18081";
|
|
setup.Install(Console.WriteLine);
|
|
string env = File.ReadAllText(Path.Combine(target, ".env"));
|
|
Assert((setup.Values["MARIADB_PASSWORD"] == "3112") == legacy, "legacy database password");
|
|
Assert(setup.Values["MARIADB_ROOT_PASSWORD"] == setup.Values["DB_PASSWORD_FALLBACK"], "safe fallback");
|
|
Assert(setup.Values["CORS_ORIGIN"] == (legacy ? "*" : setup.LocalOrigin), "CORS policy");
|
|
Assert(!env.Contains("replace-with-a-"), "no insecure placeholders");
|
|
Assert(!File.ReadAllText(Path.Combine(target, ".seltd-setup", "mariadb-init", "01-init.sql")).Contains("IDENTIFIED BY"), "remove fixed SQL credentials");
|
|
Assert(File.ReadAllText(Path.Combine(target, "docker", "mariadb", "init", "01-init.sql")).Contains("IDENTIFIED BY"), "do not edit upstream SQL");
|
|
Assert(Git(target, "status --porcelain").Trim() == "", "generated credentials and overrides are ignored by Git");
|
|
|
|
var model = new JavaScriptSerializer().DeserializeObject(Run(target, "docker.exe", "compose config --format json"));
|
|
var services = Field(model, "services");
|
|
string ip = legacy ? "0.0.0.0" : "127.0.0.1";
|
|
CheckPort(services, "web", ip, "81");
|
|
CheckPort(services, "api", ip, "18081");
|
|
CheckPort(services, "collector", ip, "18110");
|
|
CheckPort(services, "mariadb", ip, "3306");
|
|
CheckPort(services, "dockhand", "127.0.0.1", "3008");
|
|
var db = Field(services, "mariadb");
|
|
Assert((string)Field(Field(db, "environment"), "MARIADB_ROOT_HOST") == "%", "database root accessible in Compose network");
|
|
var initMount = ((IEnumerable)Field(db, "volumes")).Cast<object>()
|
|
.Single(mount => (string)Field(mount, "target") == "/docker-entrypoint-initdb.d");
|
|
Assert(((string)Field(initMount, "source")).Contains(".seltd-setup"), "mount sanitized SQL");
|
|
if (args.Contains("--database")) VerifyDatabase(setup, target);
|
|
|
|
string marker = legacy ? "legacy-update.txt" : "fresh-update.txt";
|
|
File.WriteAllText(Path.Combine(repo, marker), marker);
|
|
Git(repo, "add " + marker);
|
|
Git(repo, "-c user.name=Smoke -c user.email=smoke@example.com -c commit.gpgsign=false commit -m update");
|
|
setup.Prepare(target, Console.WriteLine);
|
|
Assert(setup.ExistingEnvironment && File.Exists(Path.Combine(target, marker)), "fast-forward pull");
|
|
setup.Install(Console.WriteLine);
|
|
Assert(env == File.ReadAllText(Path.Combine(target, ".env")), "existing env remains untouched");
|
|
}
|
|
string absoluteDist = Path.GetFullPath(Path.Combine(source, "dist")) + Path.DirectorySeparatorChar;
|
|
if (Path.GetFullPath(root).StartsWith(absoluteDist, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
foreach (string file in Directory.GetFiles(root, "*", SearchOption.AllDirectories))
|
|
{
|
|
var attributes = File.GetAttributes(file);
|
|
if ((attributes & FileAttributes.ReadOnly) != 0)
|
|
File.SetAttributes(file, attributes & ~FileAttributes.ReadOnly);
|
|
}
|
|
Directory.Delete(root, true);
|
|
}
|
|
Console.WriteLine("SE-LTD setup smoke tests passed.");
|
|
}
|
|
|
|
private static void VerifyDatabase(SetupEngine setup, string cwd)
|
|
{
|
|
Console.WriteLine("Testing MariaDB initialization without network or published ports.");
|
|
string container = "seltd-installer-test-" + Guid.NewGuid().ToString("N");
|
|
string root = setup.Values["MARIADB_ROOT_PASSWORD"];
|
|
string writer = setup.Values["MARIADB_PASSWORD"];
|
|
string sql = Path.Combine(cwd, ".seltd-setup", "mariadb-init");
|
|
Func<string, string> docker = command => Run(cwd, "docker.exe", "--context " + setup.Config.dockerContext + " " + command);
|
|
docker("run --pull never --rm -d --network none --name " + container +
|
|
" --env MARIADB_ROOT_HOST=% --env MARIADB_DATABASE=wago --env MARIADB_USER=wago" +
|
|
" --env MARIADB_ROOT_PASSWORD=" + root + " --env MARIADB_PASSWORD=" + writer +
|
|
" --mount \"type=bind,source=" + sql + ",target=/docker-entrypoint-initdb.d,readonly\" mariadb:11.4");
|
|
try
|
|
{
|
|
var deadline = DateTime.UtcNow.AddSeconds(60);
|
|
do
|
|
{
|
|
try
|
|
{
|
|
string result = docker("exec --env MYSQL_PWD=" + writer + " " + container +
|
|
" mariadb --protocol=tcp -h127.0.0.1 -uwago -N -e \"SHOW TABLES FROM wago\"");
|
|
Assert(result.Contains("app_users"), "schema and writer password");
|
|
result = docker("exec --env MYSQL_PWD=" + root + " " + container +
|
|
" mariadb --protocol=tcp -h127.0.0.1 -uroot -N -e \"SELECT 1\"");
|
|
Assert(result.Trim() == "1", "configured root password");
|
|
return;
|
|
}
|
|
catch (InvalidOperationException) { Thread.Sleep(1500); }
|
|
} while (DateTime.UtcNow < deadline);
|
|
throw new InvalidOperationException("MariaDB did not accept the configured smoke-test credentials.");
|
|
}
|
|
finally { docker("stop " + container); }
|
|
}
|
|
|
|
private static void CheckPort(object services, string service, string ip, string port)
|
|
{
|
|
var entry = ((IEnumerable)Field(Field(services, service), "ports")).Cast<object>().Single();
|
|
Assert((string)Field(entry, "host_ip") == ip && Convert.ToString(Field(entry, "published")) == port, service + " port binding");
|
|
}
|
|
|
|
private static object Field(object obj, string key) { return ((Dictionary<string, object>)obj)[key]; }
|
|
|
|
private static void Assert(bool valid, string feature)
|
|
{
|
|
if (!valid) throw new InvalidOperationException("Smoke test failed: " + feature);
|
|
}
|
|
|
|
private static string Git(string cwd, string args) { return Run(cwd, "git.exe", args); }
|
|
|
|
private static string Run(string cwd, string file, string args)
|
|
{
|
|
using (var process = Process.Start(new ProcessStartInfo(file, args)
|
|
{
|
|
WorkingDirectory = cwd, UseShellExecute = false, CreateNoWindow = true,
|
|
RedirectStandardOutput = true, RedirectStandardError = true,
|
|
StandardOutputEncoding = Encoding.UTF8
|
|
}))
|
|
{
|
|
var output = process.StandardOutput.ReadToEndAsync();
|
|
var error = process.StandardError.ReadToEndAsync();
|
|
process.WaitForExit();
|
|
if (process.ExitCode != 0) throw new InvalidOperationException(file + " failed: " + error.Result);
|
|
return output.Result;
|
|
}
|
|
}
|
|
}
|
|
}
|