using System.Runtime.Versioning; using Xunit; namespace Deployment.AcceptanceTests; [SupportedOSPlatform("linux")] public sealed class BootstrapTests { [Theory] [InlineData("ubuntu", "24.04", false, false, false)] [InlineData("ubuntu", "24.04", true, true, false)] [InlineData("ubuntu", "24.04", true, true, true)] [InlineData("ubuntu", "26.04", false, true, false)] [InlineData("ubuntu", "26.04", true, true, true)] [InlineData("debian", "12", true, false, false)] [InlineData("debian", "13", false, true, false)] [InlineData("linuxmint", "22", false, true, false, "ubuntu debian")] [InlineData("pop", "future", false, false, false, "ubuntu")] [InlineData("ubuntu", "future", false, false, false)] [InlineData("debian", "13", false, false, false, "", false)] [InlineData("linuxmint", "22", false, false, false, "ubuntu debian", false, true, true, "aspnetcore-runtime-10.0 is unavailable")] [InlineData("debian", "99", false, false, false, "", false, true, false, "No accessible Microsoft Debian 99 repository")] [InlineData("ubuntu", "26.04", true, true, false, "", true, false, true, "Required package postgresql is unavailable")] [InlineData("fedora", "44", true, false, false, "", true, true, true, "requires a Debian/Ubuntu-based distribution")] public async Task Package_provisioning_detects_runtime_and_postgresql_and_installs_only_needed_dependencies( string distribution, string version, bool runtimeAvailable, bool databaseRequired, bool databaseAvailable, string ancestry = "", bool runtimePackage = true, bool postgresPackage = true, bool repositoryAvailable = true, string expectedError = "") { var directory = Path.Combine(Path.GetTempPath(), "healthapp-bootstrap-" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(directory); try { var log = Path.Combine(directory, "commands"); await File.WriteAllTextAsync(log, ""); var installed = Path.Combine(directory, "runtime-installed"); var script = Path.Combine(directory, "test.sh"); await File.WriteAllTextAsync(script, """ set -euo pipefail fail() { printf '%s\n' "$*" >&2; exit 1; } runtime() { if [[ "$RUNTIME_PRESENT" == true || -f "$RUNTIME_INSTALLED" ]]; then printf '%s\n' 'Microsoft.AspNetCore.App 10.0.12 [runtime]'; else return 1; fi; } apt-get() { printf '%s\n' "apt-get $*" >> "$COMMAND_LOG"; if [[ "$*" == *aspnetcore-runtime-10.0* ]]; then touch "$RUNTIME_INSTALLED"; fi; } apt-cache() { if [[ "$2" == aspnetcore-runtime-10.0 && ( "$RUNTIME_PACKAGE" == true || -f "$REPOSITORY_ADDED" ) || "$2" == postgresql && "$POSTGRES_PACKAGE" == true ]]; then printf '%s\n' ' Candidate: 10.0.1'; else printf '%s\n' ' Candidate: (none)'; fi } curl() { [[ "$REPOSITORY_AVAILABLE" == true ]] || return 22; printf '%s\n' "curl $*" >> "$COMMAND_LOG"; } update-ca-certificates() { :; } dpkg() { printf '%s\n' "dpkg $*" >> "$COMMAND_LOG"; touch "$REPOSITORY_ADDED"; } pg_isready() { [[ -f "$DATABASE_READY" ]]; } systemctl() { printf '%s\n' "systemctl $*" >> "$COMMAND_LOG"; touch "$DATABASE_READY"; } source "$BOOTSTRAP" ensure_dependencies "$DISTRO" "$VERSION" /bin/true "$DATABASE_REQUIRED" "$ANCESTRY" """); // Runtime is an executable stub; installed packages switch its simulated inventory. var runtime = Path.Combine(directory, "dotnet"); await File.WriteAllTextAsync(runtime, "#!/bin/bash\nif [[ \"$RUNTIME_PRESENT\" == true || -f \"$RUNTIME_INSTALLED\" ]]; then printf '%s\\n' 'Microsoft.AspNetCore.App 10.0.12 [runtime]'; else exit 1; fi\n"); File.SetUnixFileMode(runtime, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute); var body = (await File.ReadAllTextAsync(script)).Replace("/bin/true", "\"$RUNTIME_EXECUTABLE\""); await File.WriteAllTextAsync(script, body); var environment = new Dictionary { ["COMMAND_LOG"] = log, ["RUNTIME_INSTALLED"] = installed, ["RUNTIME_PRESENT"] = runtimeAvailable ? "true" : "false", ["RUNTIME_EXECUTABLE"] = runtime, ["DATABASE_READY"] = Path.Combine(directory, "pg-ready"), ["DATABASE_REQUIRED"] = databaseRequired ? "true" : "false", ["ANCESTRY"] = ancestry, ["RUNTIME_PACKAGE"] = runtimePackage ? "true" : "false", ["POSTGRES_PACKAGE"] = postgresPackage ? "true" : "false", ["REPOSITORY_AVAILABLE"] = repositoryAvailable ? "true" : "false", ["REPOSITORY_ADDED"] = Path.Combine(directory, "repository-added"), ["BOOTSTRAP"] = Path.Combine(DeploymentFixture.Repository, "deploy/installer/bootstrap.sh"), ["DISTRO"] = distribution, ["VERSION"] = version }; if (databaseAvailable) await File.WriteAllTextAsync(environment["DATABASE_READY"], "ready"); var result = await DeploymentFixture.Run("bash", [script], environment); if (expectedError.Length > 0) { Assert.NotEqual(0, result.Code); Assert.Contains(expectedError, result.Output); var failedCommands = await File.ReadAllTextAsync(log); Assert.DoesNotContain("install -y aspnetcore-runtime-10.0", failedCommands); Assert.DoesNotContain("install -y postgresql", failedCommands); return; } Assert.True(result.Code == 0, result.Output); var commands = await File.ReadAllTextAsync(log); Assert.Equal(!runtimeAvailable, commands.Contains("install -y aspnetcore-runtime-10.0")); Assert.Equal(databaseRequired && !databaseAvailable, commands.Contains("install -y postgresql")); Assert.Equal(databaseRequired && !databaseAvailable, commands.Contains("systemctl enable --now postgresql")); Assert.Equal(!runtimeAvailable && !runtimePackage && distribution == "debian", commands.Contains("packages.microsoft.com/config/debian/" + version)); } finally { Directory.Delete(directory, true); } } }