Files
gsadmin 05afb16076 Device orphaning toggle with re-enrollment recovery, and S3 download caching
Device lifecycle (DEVICE_ORPHAN_AFTER_DAYS, default 0 = off):
- Default behaviour is unchanged and needs no operator action: a silent device
  ages Online -> Stale -> Offline, keeps its credentials, and reconnects by
  itself whenever it returns
- With a positive window, the sweeper orphans devices silent for that long.
  An orphaned device is refused credential refresh (401) and job creation
  (409), and presence from it is logged and ignored rather than resurrecting it
- Recovery is deliberate: pushing a fresh bootstrap token makes the agent
  re-enroll, which clears the flag and reuses the same device record
- Agent needed no new mode: a bootstrap token is erased after each successful
  enrollment, so finding one on an already-enrolled agent is by definition an
  instruction to re-enroll

S3 download caching (skip work when nothing changed):
- If the destination exists and the caller supplied sha256, the agent hashes
  the local file and skips entirely on a match - no request at all
- Otherwise it sends the previously stored ETag as If-None-Match and treats
  304 Not Modified as "already up to date", letting S3 make the call
- Local cache (destination -> ETag, SHA-256, size) lives in the agent's SQLite
  store so it survives restarts; every completed download is hashed to verify
  the transfer and seed the cache
- 5 download-cache tests (39 agent tests)

Migrations added for all three providers. README documents both flows with
diagrams, plus the deploy script and Gitea secrets bootstrap.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-06 22:18:11 -04:00

87 lines
2.5 KiB
C#

using Agent.Storage;
using Xunit;
namespace Agent.Tests;
public class DownloadCacheTests : IDisposable
{
private readonly string databasePath = Path.Combine(Path.GetTempPath(), $"dl-{Guid.NewGuid():N}.db");
private readonly JobHistoryStore store;
public DownloadCacheTests() => store = new JobHistoryStore(databasePath);
[Fact]
public void UnknownDestinationHasNoCacheEntry()
{
Assert.Null(store.GetDownloadCache(@"C:\ProgramData\ExecuteR\downloads\nothing.exe"));
}
[Fact]
public void RecordedDownloadIsReadBack()
{
const string path = @"C:\ProgramData\ExecuteR\downloads\setup.exe";
store.RecordDownload(path, "installers/setup.exe", "\"abc123\"", "deadbeef", 4096);
var entry = store.GetDownloadCache(path);
Assert.NotNull(entry);
Assert.Equal("\"abc123\"", entry.ETag);
Assert.Equal("deadbeef", entry.Sha256);
Assert.Equal(4096, entry.SizeBytes);
}
[Fact]
public void RecordingSameDestinationTwiceUpdatesInPlace()
{
const string path = @"C:\ProgramData\ExecuteR\downloads\setup.exe";
store.RecordDownload(path, "installers/setup.exe", "\"v1\"", "hash1", 100);
store.RecordDownload(path, "installers/setup.exe", "\"v2\"", "hash2", 200);
var entry = store.GetDownloadCache(path);
Assert.NotNull(entry);
Assert.Equal("\"v2\"", entry.ETag);
Assert.Equal("hash2", entry.Sha256);
Assert.Equal(200, entry.SizeBytes);
}
[Fact]
public void CacheSurvivesReopeningTheStore()
{
const string path = @"C:\ProgramData\ExecuteR\downloads\tool.zip";
store.RecordDownload(path, "tools/tool.zip", "\"etag\"", "hash", 10);
store.Dispose();
using var reopened = new JobHistoryStore(databasePath);
var entry = reopened.GetDownloadCache(path);
Assert.NotNull(entry);
Assert.Equal("\"etag\"", entry.ETag);
}
[Fact]
public void DownloadCacheIsSeparateFromJobIdempotency()
{
var jobId = Guid.NewGuid();
Assert.True(store.TryRecordAccepted(jobId));
Assert.False(store.TryRecordAccepted(jobId));
store.RecordDownload(@"C:\x\y.bin", "k", null, "h", 1);
Assert.NotNull(store.GetDownloadCache(@"C:\x\y.bin"));
Assert.True(store.IsHandled(jobId));
}
public void Dispose()
{
store.Dispose();
try
{
File.Delete(databasePath);
}
catch (IOException)
{
// Best effort.
}
}
}