CI / Build and test (push) Successful in 10m37s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 9s
Release / Build, test and publish (push) Successful in 16m19s
CI / PostgreSQL 16 integration (push) Successful in 10m19s
CI / PostgreSQL 17 integration (push) Successful in 11m27s
CI / Docker image (push) Successful in 57s
Release / Server image (push) Successful in 1m34s
Owner review 4 and owner review 1 phase 6. - Work board in Videos (desktop and admin panel): Published and Workspace lanes (#29). - Anonymous usage counts removed: Chronos sends nothing about its use (#30). - The archive's whole folder tree is created inside any archive root, on the server and the desktop (#31). - Video cards start with the preset; sizes keep number and unit together (#28). - Deploy guide docs/deploy (Docker, Unraid, desktop), BRIEF §24, original documentation in docs/reference/original (#22). Closes #22 #28 #29 #30 #31 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
214 lines
14 KiB
C#
214 lines
14 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using System.Text.RegularExpressions;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Chronos.Core.Diagnostics;
|
|
using Chronos.Protocol;
|
|
using Chronos.Server.Configuration;
|
|
using Chronos.Server.Hosting;
|
|
using Chronos.Server.Storage;
|
|
using Chronos.Testing;
|
|
using CoreLog = Chronos.Core.Diagnostics.Log;
|
|
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
|
|
|
|
namespace Chronos.Server.Tests.Hosting;
|
|
|
|
/// <summary>
|
|
/// The server's log settings (owner review 2, R11). Serial: the engine's <see cref="CoreLog"/> is one per process, its
|
|
/// sink the last server's started and its level the lower of that server's two.
|
|
/// </summary>
|
|
[Category("server")]
|
|
[Serial]
|
|
public sealed partial class LoggingTests(TestContext context)
|
|
{
|
|
[Test]
|
|
public async Task DebugLinesReachTheFilesWhenTheLevelAsksForThem()
|
|
{
|
|
// The files' logger stopped at Information whatever CHRONOS_LOG_LEVEL said.
|
|
var root = context.CreateTempDirectory("debug-variable");
|
|
var variable = ServerOptions.Load(new Dictionary<string, string?> { ["CHRONOS_DATA_PATH"] = root, ["CHRONOS_DB"] = "postgres", ["CHRONOS_LOG_LEVEL"] = "Debug" });
|
|
Assert.Equal((LogLevel.Debug, LogLevel.Debug), (variable.LogLevel, variable.ConsoleLogLevel));
|
|
|
|
// The console stays at Warning, so the test log stays readable: the files have their own level.
|
|
await using var server = await TestServer.StartAsync(context, o => o.LogLevel = variable.LogLevel);
|
|
var marker = Guid.NewGuid().ToString("N");
|
|
var logger = server.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Chronos.Server.Tests");
|
|
logger.LogDebug("debug {Marker}", marker);
|
|
logger.LogTrace("trace {Marker}", marker);
|
|
CoreLog.Debug("tests", "engine debug " + marker);
|
|
// ASP.NET Core's own lines stay at Warning and above.
|
|
server.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Microsoft.AspNetCore.Routing").LogDebug("routing {Marker}", marker);
|
|
|
|
var text = Logs(server);
|
|
Assert.Contains("Debug Chronos.Server.Tests: debug " + marker, text);
|
|
Assert.Contains("Debug Chronos.tests: engine debug " + marker, text);
|
|
Assert.False(text.Contains("trace " + marker, StringComparison.Ordinal), "Trace is below Debug");
|
|
Assert.False(text.Contains("routing " + marker, StringComparison.Ordinal), "request lines stay out");
|
|
var logging = server.Services.GetRequiredService<ServerLogging>();
|
|
Assert.Equal((LogLevel.Debug, LogLevel.Warning, Chronos.Core.Diagnostics.LogLevel.Debug), (logging.FileLevel, logging.ConsoleLevel, CoreLog.MinimumLevel));
|
|
}
|
|
|
|
[Test]
|
|
public void AValueOutOfRangeIsNamedByItsVariable()
|
|
{
|
|
// Rc.14 review: the start's message named the setting's property, which the deploy guide never mentions.
|
|
ServerOptions Load(params (string Name, string Value)[] variables)
|
|
{
|
|
var environment = new Dictionary<string, string?> { ["CHRONOS_DATA_PATH"] = context.CreateTempDirectory("ranges"), ["CHRONOS_DB"] = "postgres" };
|
|
foreach (var (name, value) in variables) environment[name] = value;
|
|
return ServerOptions.Load(environment);
|
|
}
|
|
var logs = Assert.Throws<InvalidOperationException>(() => Load(("CHRONOS_LOG_MAX_FILE_MB", "50"), ("CHRONOS_LOG_MAX_TOTAL_MB", "10")).Validate());
|
|
Assert.True(logs.Message.StartsWith("CHRONOS_LOG_MAX_TOTAL_MB is out of range", StringComparison.Ordinal), logs.Message);
|
|
var port = Assert.Throws<InvalidOperationException>(() => Load(("CHRONOS_HTTP_PORT", "70000")).Validate());
|
|
Assert.Contains("CHRONOS_HTTP_PORT", port.Message);
|
|
// Parallelism alone is held to its range instead.
|
|
Assert.Equal(ServerOptions.MaxParallelism, Load(("CHRONOS_PARALLELISM", "128")).Parallelism);
|
|
}
|
|
|
|
[Test]
|
|
public async Task TheFileLevelChangesWhileTheServerRuns()
|
|
{
|
|
await using var server = await TestServer.StartAsync(context, o => o.LogLevel = LogLevel.Debug);
|
|
var admin = await server.AdminAsync();
|
|
var logger = server.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Chronos.Server.Tests");
|
|
var marker = Guid.NewGuid().ToString("N");
|
|
logger.LogDebug("before {Marker}", marker);
|
|
|
|
var settings = await SettingsAsync(admin);
|
|
Assert.Equal(("debug", "warning"), (settings.Logs.FileLevel, settings.Logs.ConsoleLevel));
|
|
// The test server listens on ports the system chose (0), which no request may set.
|
|
var edited = settings with { HttpPort = 18080, HttpsPort = 18443, Logs = settings.Logs with { FileLevel = "information" } };
|
|
using (var saved = await admin.PutAsJsonAsync("/api/v1/server/settings", edited, ProtocolJson.Default.ServerSettingsDto))
|
|
Assert.Equal("information", (await saved.ReadAsync(ProtocolJson.Default.ServerSettingsDto)).Logs.FileLevel);
|
|
|
|
logger.LogDebug("after {Marker}", marker);
|
|
CoreLog.Debug("tests", "engine after " + marker);
|
|
logger.LogInformation("information {Marker}", marker);
|
|
var text = Logs(server);
|
|
Assert.Contains("before " + marker, text);
|
|
Assert.Contains("information " + marker, text);
|
|
Assert.False(text.Contains("after " + marker, StringComparison.Ordinal), "Debug stopped at once");
|
|
Assert.Equal((LogLevel.Information, Chronos.Core.Diagnostics.LogLevel.Info), (server.Options.LogLevel, CoreLog.MinimumLevel));
|
|
var file = JsonNode.Parse(await File.ReadAllTextAsync(server.Options.SettingsFile))!.AsObject();
|
|
Assert.Equal("Information", file["LogLevel"]!.GetValue<string>());
|
|
Assert.False(file.ContainsKey("ConsoleLogLevel"), "only what changed is written");
|
|
}
|
|
|
|
[Test]
|
|
public async Task LogSettingsAreSavedAppliedAndValidated()
|
|
{
|
|
// With the archive module: its audit step is what the audit retention, the audit clean-up's age limit, deletes with.
|
|
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
|
var admin = await server.AdminAsync();
|
|
var settings = await SettingsAsync(admin);
|
|
Assert.Equal(new LogSettingsDto("warning", "warning", 14, 50, 1024, "daily", 0, 4), settings.Logs);
|
|
|
|
var edited = settings with { HttpPort = 18080, HttpsPort = 18443, Logs = new LogSettingsDto("Debug", "error", 30, 10, 200, "Hourly", 365, 8) };
|
|
using (var saved = await admin.PutAsJsonAsync("/api/v1/server/settings", edited, ProtocolJson.Default.ServerSettingsDto))
|
|
Assert.Equal(new LogSettingsDto("debug", "error", 30, 10, 200, "hourly", 365, 8), (await saved.ReadAsync(ProtocolJson.Default.ServerSettingsDto)).Logs);
|
|
var o = server.Options;
|
|
Assert.Equal((LogLevel.Debug, LogLevel.Error, 30, 10, 200), (o.LogLevel, o.ConsoleLogLevel, o.LogRetentionDays, o.LogMaxFileMb, o.LogMaxTotalMb));
|
|
Assert.Equal((LogRotation.Hourly, 365, 8), (o.LogRotation, o.AuditRetentionDays, o.DiagnosticsLogMb));
|
|
var logging = server.Services.GetRequiredService<ServerLogging>();
|
|
Assert.Equal(new LogFileOptions { Level = Chronos.Core.Diagnostics.LogLevel.Debug, RetentionDays = 30, MaxFileMb = 10, MaxTotalMb = 200, Rotation = LogRotation.Hourly },
|
|
logging.Sink.Options);
|
|
Assert.Equal((LogLevel.Debug, LogLevel.Error), (logging.FileLevel, logging.ConsoleLevel));
|
|
|
|
// Without a restart, the next line goes to an hourly file.
|
|
var marker = Guid.NewGuid().ToString("N");
|
|
server.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Chronos.Server.Tests").LogWarning("hourly {Marker}", marker);
|
|
Assert.True(Directory.EnumerateFiles(o.LogPath).Any(f => HourlyName().IsMatch(Path.GetFileName(f)) && Read(f).Contains(marker, StringComparison.Ordinal)),
|
|
"an hourly file holds the line");
|
|
|
|
// server.json takes the names it reads back at the next start.
|
|
var file = JsonNode.Parse(await File.ReadAllTextAsync(o.SettingsFile))!.AsObject();
|
|
Assert.Equal(("Debug", "Error", "Hourly", 30, 365), (file["LogLevel"]!.GetValue<string>(), file["ConsoleLogLevel"]!.GetValue<string>(),
|
|
file["LogRotation"]!.GetValue<string>(), file["LogRetentionDays"]!.GetValue<int>(), file["AuditRetentionDays"]!.GetValue<int>()));
|
|
var restarted = ServerOptions.Load(new Dictionary<string, string?> { ["CHRONOS_DATA_PATH"] = o.DataPath, ["CHRONOS_DB"] = "postgres" });
|
|
Assert.Equal((LogLevel.Debug, LogLevel.Error, LogRotation.Hourly, 10, 200, 8), (restarted.LogLevel, restarted.ConsoleLogLevel, restarted.LogRotation,
|
|
restarted.LogMaxFileMb, restarted.LogMaxTotalMb, restarted.DiagnosticsLogMb));
|
|
|
|
// A panel that knows no log group keeps them as they are.
|
|
var older = JsonSerializer.SerializeToNode(edited with { ServerName = "Older panel" }, ProtocolJson.Default.ServerSettingsDto)!.AsObject();
|
|
Assert.True(older.Remove("logs"));
|
|
using (var content = new StringContent(older.ToJsonString(), Encoding.UTF8, "application/json"))
|
|
using (var saved = await admin.PutAsync("/api/v1/server/settings", content))
|
|
Assert.Equal(new LogSettingsDto("debug", "error", 30, 10, 200, "hourly", 365, 8), (await saved.ReadAsync(ProtocolJson.Default.ServerSettingsDto)).Logs);
|
|
|
|
var logs = edited.Logs;
|
|
foreach (var (invalid, setting) in new (LogSettingsDto, string)[]
|
|
{
|
|
(logs with { FileLevel = "verbose" }, "LogLevel"),
|
|
(logs with { ConsoleLevel = "3" }, "ConsoleLogLevel"),
|
|
(logs with { Rotation = "weekly" }, "LogRotation"),
|
|
(logs with { RetentionDays = 0 }, "LogRetentionDays"),
|
|
(logs with { MaxFileMb = -1 }, "LogMaxFileMb"),
|
|
(logs with { MaxFileMb = 300 }, "LogMaxTotalMb"),
|
|
(logs with { MaxTotalMb = -5 }, "LogMaxTotalMb"),
|
|
(logs with { AuditRetentionDays = -1 }, "AuditRetentionDays"),
|
|
(logs with { DiagnosticsLogMb = 0 }, "DiagnosticsLogMb"),
|
|
})
|
|
{
|
|
using var refused = await admin.PutAsJsonAsync("/api/v1/server/settings", edited with { Logs = invalid }, ProtocolJson.Default.ServerSettingsDto);
|
|
Assert.Equal(setting, (await refused.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_setting")).Details);
|
|
}
|
|
// No limits at all are a choice too.
|
|
using (var unlimited = await admin.PutAsJsonAsync("/api/v1/server/settings", edited with { Logs = logs with { MaxFileMb = 0, MaxTotalMb = 0 } },
|
|
ProtocolJson.Default.ServerSettingsDto))
|
|
await unlimited.ExpectAsync(HttpStatusCode.OK);
|
|
Assert.Equal((0, 0), (logging.Sink.Options.MaxFileMb, logging.Sink.Options.MaxTotalMb));
|
|
}
|
|
|
|
[Test]
|
|
public async Task LevelsSetByTheEnvironmentAreLocked()
|
|
{
|
|
// An install upgraded with the earlier compose file or Unraid template: CHRONOS_LOG_LEVEL fixes both levels.
|
|
await using var server = await TestServer.StartAsync(context, o =>
|
|
{
|
|
o.Locked = [nameof(ServerOptions.LogLevel), nameof(ServerOptions.ConsoleLogLevel), nameof(ServerOptions.LogRotation)];
|
|
o.LockedBy = new Dictionary<string, string> { [nameof(ServerOptions.LogLevel)] = "CHRONOS_LOG_LEVEL", [nameof(ServerOptions.ConsoleLogLevel)] = "CHRONOS_LOG_LEVEL" };
|
|
});
|
|
var admin = await server.AdminAsync();
|
|
var settings = await SettingsAsync(admin);
|
|
Assert.Contains("ConsoleLogLevel", settings.Locked);
|
|
// The panel's tooltip names the variable that fixes each; one locked without it is named by the variable that would.
|
|
string[] lockedBy = ["ConsoleLogLevel=CHRONOS_LOG_LEVEL", "LogLevel=CHRONOS_LOG_LEVEL", "LogRotation=CHRONOS_LOG_ROTATION"];
|
|
Assert.SequenceEqual(lockedBy, settings.LockedBy.Select(l => $"{l.Key}={l.Value}").Order(StringComparer.Ordinal));
|
|
var edited = settings with { HttpPort = 18080, HttpsPort = 18443 };
|
|
using (var refused = await admin.PutAsJsonAsync("/api/v1/server/settings", edited with { Logs = edited.Logs with { ConsoleLevel = "debug" } },
|
|
ProtocolJson.Default.ServerSettingsDto))
|
|
Assert.Equal("ConsoleLogLevel", (await refused.ExpectErrorAsync(HttpStatusCode.Conflict, "setting_locked")).Details);
|
|
Assert.Equal(LogLevel.Warning, server.Options.ConsoleLogLevel);
|
|
// The settings no variable fixes still save.
|
|
// What a request says about the locks is ignored.
|
|
using (var saved = await admin.PutAsJsonAsync("/api/v1/server/settings",
|
|
edited with { Logs = edited.Logs with { RetentionDays = 7 }, LockedBy = new Dictionary<string, string>() }, ProtocolJson.Default.ServerSettingsDto))
|
|
Assert.SequenceEqual(lockedBy, (await saved.ReadAsync(ProtocolJson.Default.ServerSettingsDto)).LockedBy.Select(l => $"{l.Key}={l.Value}").Order(StringComparer.Ordinal));
|
|
Assert.Equal(7, server.Options.LogRetentionDays);
|
|
}
|
|
|
|
private static async Task<ServerSettingsDto> SettingsAsync(HttpClient admin)
|
|
{
|
|
using var read = await admin.GetAsync("/api/v1/server/settings");
|
|
return await read.ReadAsync(ProtocolJson.Default.ServerSettingsDto);
|
|
}
|
|
|
|
/// <summary>Every server log file, read beside the sink that may still be writing it.</summary>
|
|
private static string Logs(TestServer server) => string.Concat(Directory.EnumerateFiles(server.Options.LogPath, "server-*.log").Order(StringComparer.Ordinal).Select(Read));
|
|
|
|
private static string Read(string path)
|
|
{
|
|
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|
using var reader = new StreamReader(stream);
|
|
return reader.ReadToEnd();
|
|
}
|
|
|
|
[GeneratedRegex(@"^server-\d{8}-\d{2}(\.\d+)?\.log$")]
|
|
private static partial Regex HourlyName();
|
|
}
|