b3675112be
An MSI install hands the MSI over with the kept MSI of its version (or the server's, downloaded, checked and kept; or none, said so), keeps what it brought, refuses the zip, and is refused by a portable install; a kept MSI whose bytes are not its folder's hash is not used; an MSI of another version or not the agent's fails as package; msi.json makes it busy; kept packages stay at the newest three plus those still needed. The outcome reporter passes an MSI run's own detail on. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
147 lines
5.7 KiB
C#
147 lines
5.7 KiB
C#
using Agent.Upgrade;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Shared.Contracts.Models;
|
|
using Shared.Contracts.Serialization;
|
|
using Xunit;
|
|
|
|
namespace Agent.Tests;
|
|
|
|
public sealed class UpgradeOutcomeReporterTests : IDisposable
|
|
{
|
|
private readonly string root = Path.Combine(Path.GetTempPath(), "executer-outcome-tests", Guid.NewGuid().ToString("N"));
|
|
private readonly UpgradeFolders folders;
|
|
private readonly RecordingEvents events = new();
|
|
|
|
public UpgradeOutcomeReporterTests() => folders = new UpgradeFolders(Path.Combine(root, "data"), Path.Combine(root, "install"));
|
|
|
|
public void Dispose()
|
|
{
|
|
try
|
|
{
|
|
Directory.Delete(root, recursive: true);
|
|
}
|
|
catch (IOException)
|
|
{
|
|
}
|
|
}
|
|
|
|
private UpgradeOutcomeReporter CreateReporter() => new(folders, events, () => true, "client-1", NullLogger.Instance);
|
|
|
|
private WatchdogOutcome WriteOutcome(string result, string? reason)
|
|
{
|
|
var outcome = new WatchdogOutcome(Guid.NewGuid(), result, reason, "2026.2.1.1000", "2026.1.1.1000",
|
|
["2026-10-23 [ERR] nats: connection refused"], DateTimeOffset.UtcNow);
|
|
WatchdogFileStore.Write(folders.Outcome, outcome, WatchdogJsonContext.Default.WatchdogOutcome);
|
|
return outcome;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReportsARollbackWithItsLogLinesAndDeletesTheOutcome()
|
|
{
|
|
var outcome = WriteOutcome(AgentUpgradeStates.RolledBack, AgentUpgradeReasons.HealthDeadline);
|
|
|
|
Assert.True(await CreateReporter().ReportPendingAsync(CancellationToken.None));
|
|
|
|
var reported = Assert.Single(events.Published);
|
|
Assert.Equal(outcome.UpgradeId, reported.UpgradeId);
|
|
Assert.Equal("client-1", reported.ClientId);
|
|
Assert.Equal(AgentUpgradeStates.RolledBack, reported.State);
|
|
Assert.Equal(AgentUpgradeReasons.HealthDeadline, reported.Reason);
|
|
Assert.Equal("2026.1.1.1000", reported.FromVersion);
|
|
Assert.Equal("2026.2.1.1000", reported.ToVersion);
|
|
Assert.Equal(outcome.LastLog, reported.LastLog);
|
|
Assert.Contains("did not report healthy", reported.Detail);
|
|
Assert.False(File.Exists(folders.Outcome));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnMsiRunsOwnDetailIsWhatIsReported()
|
|
{
|
|
var outcome = new WatchdogOutcome(Guid.NewGuid(), AgentUpgradeStates.Failed, AgentUpgradeReasons.HealthDeadline, "1.4.0", "1.3.0", [],
|
|
DateTimeOffset.UtcNow, "1.4.0 did not report healthy in time; no MSI of 1.3.0 was kept to go back to, so it stays.");
|
|
WatchdogFileStore.Write(folders.Outcome, outcome, WatchdogJsonContext.Default.WatchdogOutcome);
|
|
|
|
Assert.True(await CreateReporter().ReportPendingAsync(CancellationToken.None));
|
|
|
|
Assert.Equal(outcome.Detail, Assert.Single(events.Published).Detail);
|
|
}
|
|
|
|
[Fact]
|
|
public void AnMsiFailureWithoutDetailSaysThePreviousVersionStays()
|
|
{
|
|
var reported = UpgradeOutcomeReporter.ToEvent(new WatchdogOutcome(Guid.NewGuid(), AgentUpgradeStates.Failed, AgentUpgradeReasons.InstallerBusy,
|
|
"1.4.0", "1.3.0", [], DateTimeOffset.UtcNow), "client-1");
|
|
|
|
Assert.Contains("1.3.0 stays", reported.Detail, StringComparison.Ordinal);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReportsASuccess()
|
|
{
|
|
WriteOutcome(AgentUpgradeStates.Succeeded, null);
|
|
|
|
await CreateReporter().ReportPendingAsync(CancellationToken.None);
|
|
|
|
Assert.Equal(AgentUpgradeStates.Succeeded, Assert.Single(events.Published).State);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task KeepsTheOutcomeWhenItCannotBePublished()
|
|
{
|
|
WriteOutcome(AgentUpgradeStates.Failed, AgentUpgradeReasons.Watchdog);
|
|
events.Failure = new InvalidOperationException("disconnected");
|
|
|
|
Assert.False(await CreateReporter().ReportPendingAsync(CancellationToken.None));
|
|
|
|
Assert.True(File.Exists(folders.Outcome));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReportsNothingUntilConnected()
|
|
{
|
|
WriteOutcome(AgentUpgradeStates.Succeeded, null);
|
|
using var cancel = new CancellationTokenSource(TimeSpan.FromMilliseconds(300));
|
|
|
|
await new UpgradeOutcomeReporter(folders, events, () => false, "client-1", NullLogger.Instance).RunAsync(cancel.Token);
|
|
|
|
Assert.Empty(events.Published);
|
|
Assert.True(File.Exists(folders.Outcome));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TidiesOldDownloadsAndStagingKeepingTheNewest()
|
|
{
|
|
Directory.CreateDirectory(folders.Downloads);
|
|
var old = Path.Combine(folders.Downloads, "old.zip");
|
|
var newest = Path.Combine(folders.Downloads, "new.zip");
|
|
File.WriteAllText(old, "old");
|
|
File.WriteAllText(newest, "new");
|
|
File.SetLastWriteTimeUtc(old, DateTime.UtcNow.AddDays(-2));
|
|
var oldStage = Directory.CreateDirectory(Path.Combine(folders.Staging, "a"));
|
|
var newStage = Directory.CreateDirectory(Path.Combine(folders.Staging, "b"));
|
|
Directory.SetLastWriteTimeUtc(oldStage.FullName, DateTime.UtcNow.AddDays(-2));
|
|
|
|
await CreateReporter().ReportPendingAsync(CancellationToken.None);
|
|
|
|
Assert.False(File.Exists(old));
|
|
Assert.True(File.Exists(newest));
|
|
Assert.False(oldStage.Exists && Directory.Exists(oldStage.FullName));
|
|
Assert.True(Directory.Exists(newStage.FullName));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task NeverTidiesWhileASwapIsUnderWay()
|
|
{
|
|
Directory.CreateDirectory(folders.Downloads);
|
|
var old = Path.Combine(folders.Downloads, "old.zip");
|
|
File.WriteAllText(old, "old");
|
|
File.SetLastWriteTimeUtc(old, DateTime.UtcNow.AddDays(-2));
|
|
File.WriteAllText(Path.Combine(folders.Downloads, "new.zip"), "new");
|
|
File.WriteAllText(folders.Journal, "{}");
|
|
|
|
await CreateReporter().ReportPendingAsync(CancellationToken.None);
|
|
|
|
Assert.True(File.Exists(old));
|
|
}
|
|
}
|