feat: stable server id and per-home folder layout (owner review 5 P3.1)
The desktop keyed every server by the address it was reached at, so one
NAS reached by name and by IP, or after an address change, looked like two
servers, and all servers shared one workspace and one cache. Each server
now announces a stable id, and the desktop's folders are sorted into one
home per server, so that every server can have its own archive of
downloaded originals (or5 answers Q1).
- The server makes a Guid v7 once, keeps it as the app setting server.id
(so it survives restarts and restores, on SQLite and Postgres) and
announces it as ServerInfo.ServerId. Older clients ignore the field, and
older servers read as null.
- ServerIdentity keys a server as id:<N> when it announces an id, and by
its address otherwise. Address keys are kept as aliases.
- HomeFolders names the homes: local, servers/<id> or
servers/addr-<hash16>. The hash equals the 1.0 proxy cache folder name.
It also names the per-server originals archive root.
- WorkspaceHomes splits the workspace into one format-1 store per home
under a format-2 parent manifest, which 1.0 refuses. A 1.0 workspace
stays one store until it is sorted.
- HomeLayoutMigration plans as a dry run and applies through a journal:
- only same-folder renames, each checked by inventory;
- a clash keeps the copy saved last and sends the other to the trash;
- it resumes after a crash at any step;
- it skips leased projects;
- it defers an offline workspace or one being moved, while the cache
part still runs.
- RekeyAsync moves an alias home to the id home: whole folders, or
project-by-project merges. It also rewrites working states under their
lease, and moves sync_links (the newer row wins a clash).
- ProxyCache, the job root resolver, WorkspaceMover and WorkspaceCleaner
understand the split layout. The new layout is opt-in through
SyncOptions.ServersDirectory, so the app sees no change until it is
wired.
Refs #66
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
58ed737310
commit
4d697a4255
27 files changed
+3082
-38
No files matched your search
@@ -0,0 +1,122 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Data.Workspaces;
|
||||
|
||||
namespace Chronos.Data.Tests.Homes;
|
||||
|
||||
/// <summary>Workspaces and caches laid out as 1.0 left them, for the sorting into homes (owner review 5, #66).</summary>
|
||||
internal static class HomeFixtures
|
||||
{
|
||||
public const string Nas = "https://nas.local:8443";
|
||||
public const string NasByIp = "https://192.168.1.5:8443";
|
||||
public const string Other = "https://other.example:8443";
|
||||
|
||||
/// <summary>A working state of <paramref name="home"/> written into <c><root>/<id></c> with some media, versions and a render.</summary>
|
||||
public static Guid Project(string root, BundleHome home, string name = "Main", DateTimeOffset? saved = null, Guid? id = null, int proxies = 3)
|
||||
{
|
||||
var projectId = id ?? Guid.CreateVersion7();
|
||||
var folder = Path.Combine(root, projectId.ToString("N"));
|
||||
Directory.CreateDirectory(folder);
|
||||
var archive = home.IsArchive
|
||||
? new BundleArchive(home.ArchiveId ?? Guid.CreateVersion7(), "This PC", @"D:\ChronosArchive")
|
||||
: new BundleArchive(Data.Projects.ServerProjectHome.ArchiveIdOf(home.Server!), "Studio NAS", null);
|
||||
var bundle = new ProjectBundle(archive, new BundleSequence(home.SequenceId, "2026/2026-11-15 Harbour", "Harbour", proxies), new ProjectSettings(),
|
||||
saved ?? DateTimeOffset.UtcNow)
|
||||
{
|
||||
Workspace = new BundleWorkspace(projectId, name, true, home, null, new BundleMedia(BundleMedia.Proxies, 1)),
|
||||
};
|
||||
bundle.Save(Path.Combine(folder, WorkspaceProject.BundleFile));
|
||||
var media = Path.Combine(folder, WorkspaceProject.MediaFolder, WorkspaceProject.ProxiesFolder);
|
||||
Directory.CreateDirectory(media);
|
||||
for (var i = 0; i < proxies; i++) File.WriteAllBytes(Path.Combine(media, Guid.CreateVersion7().ToString("N") + ".jpg"), Bytes(projectId, i, 500 + i));
|
||||
Directory.CreateDirectory(Path.Combine(folder, WorkspaceProject.VersionsFolder));
|
||||
File.WriteAllText(Path.Combine(folder, WorkspaceProject.VersionsFolder, "1-" + Guid.CreateVersion7().ToString("N") + ".json"), "{}");
|
||||
Directory.CreateDirectory(Path.Combine(folder, WorkspaceProject.ExportsFolder));
|
||||
File.WriteAllBytes(Path.Combine(folder, WorkspaceProject.ExportsFolder, "render.mp4"), Bytes(projectId, 99, 2048));
|
||||
return projectId;
|
||||
}
|
||||
|
||||
public static BundleHome Local() => new(BundleHome.ArchiveKind, Guid.CreateVersion7(), Guid.CreateVersion7());
|
||||
|
||||
public static BundleHome Server(string key) => new(BundleHome.ServerKind, Guid.CreateVersion7(), null, key);
|
||||
|
||||
/// <summary>A 1.0 workspace: its manifest (format 1) and the folder.</summary>
|
||||
public static string LegacyWorkspace(string parent)
|
||||
{
|
||||
var root = Path.Combine(parent, "workspace");
|
||||
_ = new WorkspaceStore(root);
|
||||
return root;
|
||||
}
|
||||
|
||||
/// <summary>A trash entry of <paramref name="projectId"/> holding a working state, as a removal leaves it.</summary>
|
||||
public static string TrashEntry(string root, Guid projectId, BundleHome home)
|
||||
{
|
||||
var trash = Path.Combine(root, WorkspaceTrash.FolderName);
|
||||
var staging = Path.Combine(trash, "staging");
|
||||
Directory.CreateDirectory(staging);
|
||||
Project(staging, home, id: projectId, proxies: 1);
|
||||
var entry = Path.Combine(trash, projectId.ToString("N") + "-638000000000000000");
|
||||
Directory.Move(Path.Combine(staging, projectId.ToString("N")), entry);
|
||||
Directory.Delete(staging);
|
||||
return entry;
|
||||
}
|
||||
|
||||
/// <summary>The 1.0 proxy cache of <paramref name="key"/>'s sequence <paramref name="remoteId"/>.</summary>
|
||||
public static string LegacyProxies(string cache, string key, Guid remoteId, int frames = 2)
|
||||
{
|
||||
var folder = Data.Sync.ProxyCache.LegacyFolder(Path.Combine(cache, "proxies"), key, remoteId);
|
||||
Directory.CreateDirectory(folder);
|
||||
File.WriteAllText(Path.Combine(folder, "remote.json"), "{}");
|
||||
for (var i = 0; i < frames; i++) File.WriteAllBytes(Path.Combine(folder, Guid.CreateVersion7().ToString("N") + ".jpg"), Bytes(remoteId, i, 300));
|
||||
return folder;
|
||||
}
|
||||
|
||||
/// <summary>A live lease on a project folder, as another Chronos working on the same workspace holds it.</summary>
|
||||
public static LeaseFile Hold(string projectFolder) =>
|
||||
LeaseFile.TryAcquire(projectFolder, ".lock", "workspace@other", TimeSpan.FromMinutes(5)) ?? throw new InvalidOperationException("The lease is held.");
|
||||
|
||||
/// <summary>
|
||||
/// The SHA-256 of every file under <paramref name="root"/> by its name relative to the project or entry it belongs to,
|
||||
/// whatever home folder holds it: the same before and after a sorting that only renames (the test-only proof).
|
||||
/// </summary>
|
||||
public static SortedDictionary<string, string> Manifest(string root)
|
||||
{
|
||||
var manifest = new SortedDictionary<string, string>(StringComparer.Ordinal);
|
||||
if (!Directory.Exists(root)) return manifest;
|
||||
foreach (var file in Directory.EnumerateFiles(root, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
var relative = Path.GetRelativePath(root, file).Replace('\\', '/');
|
||||
var name = Path.GetFileName(file);
|
||||
if (name is WorkspaceStore.ManifestFile or "server.json" && !relative.Contains('/', StringComparison.Ordinal)) continue;
|
||||
if (name is WorkspaceStore.ManifestFile or "server.json" && IsHomeLevel(relative)) continue;
|
||||
manifest[Strip(relative)] = Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(file))) + "|" + new FileInfo(file).Length;
|
||||
}
|
||||
return manifest;
|
||||
}
|
||||
|
||||
/// <summary>A path without the home folders it lies in (<c>local/</c>, <c>servers/<folder>/</c>).</summary>
|
||||
private static string Strip(string relative)
|
||||
{
|
||||
if (relative.StartsWith("local/", StringComparison.Ordinal)) return relative["local/".Length..];
|
||||
if (relative.StartsWith("servers/", StringComparison.Ordinal))
|
||||
{
|
||||
var rest = relative["servers/".Length..];
|
||||
var slash = rest.IndexOf('/', StringComparison.Ordinal);
|
||||
return slash < 0 ? rest : rest[(slash + 1)..];
|
||||
}
|
||||
return relative;
|
||||
}
|
||||
|
||||
private static bool IsHomeLevel(string relative) =>
|
||||
relative.Split('/') is { Length: 2 } parts && parts[0] == "local" || relative.Split('/') is { Length: 3 } more && more[0] == "servers";
|
||||
|
||||
private static byte[] Bytes(Guid seed, int index, int length)
|
||||
{
|
||||
var bytes = new byte[length];
|
||||
var hash = SHA256.HashData(Encoding.UTF8.GetBytes(seed.ToString("N") + index));
|
||||
for (var i = 0; i < length; i++) bytes[i] = hash[i % hash.Length];
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,297 @@
|
||||
using Chronos.Core.IO;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Data.Homes;
|
||||
using Chronos.Data.Sync;
|
||||
using Chronos.Data.Workspaces;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.Data.Tests.Homes;
|
||||
|
||||
/// <summary>The one-time sorting of a 1.0 workspace and cache into homes (owner review 5, #66).</summary>
|
||||
[Category("homes")]
|
||||
public sealed class HomeLayoutMigrationTests(TestContext context)
|
||||
{
|
||||
private static readonly Guid Id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01");
|
||||
|
||||
/// <summary>A 1.0 data folder: a workspace with local, server and damaged projects and a trash, and a cache with proxies, render-node files and a staged import.</summary>
|
||||
private sealed class Legacy
|
||||
{
|
||||
public required string Data { get; init; }
|
||||
public required string Workspace { get; init; }
|
||||
public required string Cache { get; init; }
|
||||
public required Guid Local { get; init; }
|
||||
public required Guid OnNas { get; init; }
|
||||
public required Guid OnOther { get; init; }
|
||||
public required Guid Damaged { get; init; }
|
||||
public required Guid NasRemote { get; init; }
|
||||
|
||||
public HomeLayoutMigration Migration(ServerIdentity? identity = null, Action<string, string>? cut = null) =>
|
||||
new(new HomeLayoutOptions
|
||||
{
|
||||
DataDirectory = Data,
|
||||
WorkspaceRoot = Workspace,
|
||||
CacheRoot = Cache,
|
||||
Identity = identity ?? ServerIdentity.None,
|
||||
CurrentServer = HomeFixtures.Nas,
|
||||
})
|
||||
{ Cut = cut };
|
||||
}
|
||||
|
||||
private Legacy Make()
|
||||
{
|
||||
var data = context.CreateTempDirectory("data");
|
||||
var workspace = HomeFixtures.LegacyWorkspace(data);
|
||||
var local = HomeFixtures.Project(workspace, HomeFixtures.Local());
|
||||
var onNas = HomeFixtures.Project(workspace, HomeFixtures.Server(HomeFixtures.Nas));
|
||||
var onOther = HomeFixtures.Project(workspace, HomeFixtures.Server(HomeFixtures.Other));
|
||||
var damaged = Guid.CreateVersion7();
|
||||
Directory.CreateDirectory(Path.Combine(workspace, damaged.ToString("N")));
|
||||
File.WriteAllText(Path.Combine(workspace, damaged.ToString("N"), WorkspaceProject.BundleFile), "{ not json");
|
||||
HomeFixtures.TrashEntry(workspace, onNas, HomeFixtures.Server(HomeFixtures.Nas));
|
||||
File.WriteAllText(Path.Combine(workspace, WorkspaceTrash.FolderName, "loose.txt"), "kept");
|
||||
var cache = Path.Combine(data, "cache");
|
||||
var nasRemote = Guid.CreateVersion7();
|
||||
HomeFixtures.LegacyProxies(cache, HomeFixtures.Nas, nasRemote);
|
||||
HomeFixtures.LegacyProxies(cache, HomeFixtures.Other, Guid.CreateVersion7());
|
||||
Directory.CreateDirectory(Path.Combine(cache, "render-node", "job1"));
|
||||
File.WriteAllText(Path.Combine(cache, "render-node", "job1", "frame.jpg"), "rn");
|
||||
Directory.CreateDirectory(Path.Combine(cache, "server-import"));
|
||||
File.WriteAllText(Path.Combine(cache, "server-import", "card.part"), "import");
|
||||
return new Legacy
|
||||
{
|
||||
Data = data,
|
||||
Workspace = workspace,
|
||||
Cache = cache,
|
||||
Local = local,
|
||||
OnNas = onNas,
|
||||
OnOther = onOther,
|
||||
Damaged = damaged,
|
||||
NasRemote = nasRemote,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>Every file and folder under <paramref name="root"/> with its size and time: what a dry run must leave as it was.</summary>
|
||||
private static List<string> Snapshot(string root) =>
|
||||
[.. new DirectoryInfo(root).EnumerateFileSystemInfos("*", SearchOption.AllDirectories)
|
||||
.Select(e => Path.GetRelativePath(root, e.FullName) + "|" + (e is FileInfo f ? f.Length + "|" + f.LastWriteTimeUtc.Ticks : "dir"))
|
||||
.Order(StringComparer.Ordinal)];
|
||||
|
||||
private static string Home(string workspace, string key) => Path.Combine(workspace, "servers", HomeFolders.ServerFolder(key));
|
||||
|
||||
private static void AssertSorted(Legacy legacy, string nasFolder)
|
||||
{
|
||||
var ws = legacy.Workspace;
|
||||
Assert.True(WorkspaceHomes.IsSplitRoot(ws));
|
||||
Assert.FileExists(Path.Combine(ws, "local", legacy.Local.ToString("N"), WorkspaceProject.BundleFile));
|
||||
Assert.FileExists(Path.Combine(ws, "local", legacy.Damaged.ToString("N"), WorkspaceProject.BundleFile));
|
||||
Assert.FileExists(Path.Combine(ws, "servers", nasFolder, legacy.OnNas.ToString("N"), WorkspaceProject.BundleFile));
|
||||
Assert.FileExists(Path.Combine(Home(ws, HomeFixtures.Other), legacy.OnOther.ToString("N"), WorkspaceProject.BundleFile));
|
||||
Assert.FileExists(Path.Combine(ws, "local", WorkspaceTrash.FolderName, "loose.txt"));
|
||||
Assert.Count(1, Directory.EnumerateDirectories(Path.Combine(ws, "servers", nasFolder, WorkspaceTrash.FolderName)));
|
||||
Assert.False(Directory.EnumerateDirectories(ws).Any(d => Guid.TryParseExact(Path.GetFileName(d), "N", out _)), "no project is left in the parent");
|
||||
Assert.False(Directory.Exists(Path.Combine(ws, WorkspaceTrash.FolderName)), "the parent's emptied trash went");
|
||||
Assert.Equal("Studio NAS", Assert.NotNull(ServerHomeRecord.Read(Path.Combine(ws, "servers", nasFolder))).Name);
|
||||
Assert.FileExists(Path.Combine(legacy.Cache, "servers", nasFolder, "proxies", legacy.NasRemote.ToString("N"), "remote.json"));
|
||||
Assert.True(Directory.Exists(Path.Combine(legacy.Cache, "servers", HomeFolders.ServerFolder(HomeFixtures.Other), "proxies")));
|
||||
Assert.FileExists(Path.Combine(legacy.Cache, "servers", nasFolder, "render-node", "job1", "frame.jpg"));
|
||||
Assert.FileExists(Path.Combine(legacy.Cache, "servers", nasFolder, "import", "card.part"));
|
||||
Assert.False(Directory.Exists(Path.Combine(legacy.Cache, "render-node")));
|
||||
Assert.FileMissing(Path.Combine(legacy.Data, HomeLayoutMigration.JournalFile));
|
||||
var homes = new WorkspaceHomes(ws);
|
||||
Assert.True(homes.IsSplit);
|
||||
Assert.Equal(4, homes.List().Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ThePlanWritesNothing()
|
||||
{
|
||||
var legacy = Make();
|
||||
var before = Snapshot(legacy.Data);
|
||||
var plan = await legacy.Migration().PlanAsync(context.Cancellation);
|
||||
Assert.SequenceEqual(before, Snapshot(legacy.Data));
|
||||
Assert.True(plan.SplitsWorkspace);
|
||||
Assert.Null(plan.Deferred);
|
||||
Assert.Equal(4, plan.Projects);
|
||||
Assert.Equal(2, plan.Entries.Count(e => e.Kind == HomeEntryKind.Trash));
|
||||
Assert.Equal(2, plan.Entries.Count(e => e.Kind == HomeEntryKind.Proxies));
|
||||
Assert.Equal(1, plan.Entries.Count(e => e.Kind == HomeEntryKind.RenderNode));
|
||||
Assert.Equal(1, plan.Entries.Count(e => e.Kind == HomeEntryKind.Import));
|
||||
var damaged = plan.Entries.Single(e => e.Source.EndsWith(legacy.Damaged.ToString("N"), StringComparison.Ordinal));
|
||||
Assert.Equal(Path.Combine(legacy.Workspace, "local", legacy.Damaged.ToString("N")), damaged.Target);
|
||||
Assert.Equal(plan.Fingerprint, (await legacy.Migration().PlanAsync(context.Cancellation)).Fingerprint);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task SortingMovesEveryHomeAndRenamesOnly()
|
||||
{
|
||||
var legacy = Make();
|
||||
var before = HomeFixtures.Manifest(legacy.Workspace);
|
||||
var cacheBefore = HomeFixtures.Manifest(legacy.Cache);
|
||||
var result = await legacy.Migration().ApplyAsync(context.Cancellation);
|
||||
Assert.Equal((4, 0, false, true), (result.ProjectsMoved, result.InUse, result.Resumed, result.Split));
|
||||
AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas));
|
||||
// Every file is there with the same name relative to its project and the same bytes: the sorting only renamed.
|
||||
Assert.SequenceEqual(before, HomeFixtures.Manifest(legacy.Workspace));
|
||||
Assert.SequenceEqual(cacheBefore.Values.Order(), HomeFixtures.Manifest(legacy.Cache).Values.Order());
|
||||
|
||||
// A second run does nothing.
|
||||
var again = await legacy.Migration().ApplyAsync(context.Cancellation);
|
||||
Assert.Equal(HomeLayoutResult.Nothing, again);
|
||||
Assert.True((await legacy.Migration().PlanAsync(context.Cancellation)).IsEmpty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AServerKnownByIdGetsItsIdHome()
|
||||
{
|
||||
var legacy = Make();
|
||||
await legacy.Migration(new ServerIdentity([new KnownServerKey(Id, [HomeFixtures.Nas])])).ApplyAsync(context.Cancellation);
|
||||
AssertSorted(legacy, Id.ToString("N"));
|
||||
Assert.Equal(ServerIdentity.IdKey(Id), Assert.NotNull(ServerHomeRecord.Read(Path.Combine(legacy.Workspace, "servers", Id.ToString("N")))).Key);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ACrashAtAnyStepIsTakenUpToTheSameEnd()
|
||||
{
|
||||
var entries = (await Make().Migration().PlanAsync(context.Cancellation)).Entries.Count;
|
||||
var cuts = new List<(string Step, int Occurrence)> { ("journal", 1), ("commit", 1) };
|
||||
for (var i = 1; i <= entries; i++) cuts.AddRange([("rename", i), ("renamed", i)]);
|
||||
foreach (var (step, occurrence) in cuts)
|
||||
{
|
||||
var legacy = Make();
|
||||
var before = HomeFixtures.Manifest(legacy.Workspace);
|
||||
var seen = 0;
|
||||
var crashing = legacy.Migration(cut: (name, _) =>
|
||||
{
|
||||
if (name == step && ++seen == occurrence) throw new IOException("cut at " + step);
|
||||
});
|
||||
await Assert.ThrowsAsync<IOException>(() => crashing.ApplyAsync(context.Cancellation));
|
||||
Assert.FileExists(Path.Combine(legacy.Data, HomeLayoutMigration.JournalFile));
|
||||
var resumed = await legacy.Migration().ApplyAsync(context.Cancellation);
|
||||
Assert.True(resumed.Resumed, $"{step} #{occurrence}");
|
||||
AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas));
|
||||
Assert.SequenceEqual(before, HomeFixtures.Manifest(legacy.Workspace), $"{step} #{occurrence}");
|
||||
Assert.Equal(HomeLayoutResult.Nothing, await legacy.Migration().ApplyAsync(context.Cancellation));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AProjectInUseIsLeftAndMovedAtALaterStart()
|
||||
{
|
||||
var legacy = Make();
|
||||
var held = HomeFixtures.Hold(Path.Combine(legacy.Workspace, legacy.Local.ToString("N")));
|
||||
HomeLayoutResult result;
|
||||
using (held)
|
||||
{
|
||||
var plan = await legacy.Migration().PlanAsync(context.Cancellation);
|
||||
Assert.SequenceEqual(new[] { legacy.Local }, plan.InUse);
|
||||
result = await legacy.Migration().ApplyAsync(context.Cancellation);
|
||||
Assert.Equal((3, 1, true), (result.ProjectsMoved, result.InUse, result.Split));
|
||||
Assert.True(WorkspaceHomes.IsSplitRoot(legacy.Workspace));
|
||||
var homes = new WorkspaceHomes(legacy.Workspace);
|
||||
Assert.Equal(homes.Root, Path.GetDirectoryName(Assert.NotNull(homes.Find(legacy.Local)).Folder));
|
||||
Assert.Equal(4, homes.List().Count);
|
||||
}
|
||||
result = await legacy.Migration().ApplyAsync(context.Cancellation);
|
||||
Assert.Equal((1, 0), (result.ProjectsMoved, result.InUse));
|
||||
Assert.FileExists(Path.Combine(legacy.Workspace, "local", legacy.Local.ToString("N"), WorkspaceProject.BundleFile));
|
||||
Assert.Null(new WorkspaceHomes(legacy.Workspace).Homes().FirstOrDefault(h => h.IsLegacy));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnUnfinishedMoveDefersTheWorkspaceWhileTheCacheIsSorted()
|
||||
{
|
||||
var legacy = Make();
|
||||
File.WriteAllText(Path.Combine(legacy.Workspace, WorkspaceMover.JournalFile), "{}");
|
||||
var before = Snapshot(legacy.Workspace);
|
||||
var result = await legacy.Migration().ApplyAsync(context.Cancellation);
|
||||
Assert.Equal((HomeLayoutDeferral?)HomeLayoutDeferral.Moving, result.Deferred);
|
||||
Assert.False(result.Split);
|
||||
Assert.SequenceEqual(before, Snapshot(legacy.Workspace));
|
||||
Assert.FileExists(Path.Combine(legacy.Cache, "servers", HomeFolders.ServerFolder(HomeFixtures.Nas), "proxies", legacy.NasRemote.ToString("N"), "remote.json"));
|
||||
|
||||
File.Delete(Path.Combine(legacy.Workspace, WorkspaceMover.JournalFile));
|
||||
await legacy.Migration().ApplyAsync(context.Cancellation);
|
||||
AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AWorkspaceThatWentOfflineWaitsWithItsJournal()
|
||||
{
|
||||
var legacy = Make();
|
||||
var before = HomeFixtures.Manifest(legacy.Workspace);
|
||||
await Assert.ThrowsAsync<IOException>(() => legacy.Migration(cut: (name, _) =>
|
||||
{
|
||||
if (name == "journal") throw new IOException("cut");
|
||||
}).ApplyAsync(context.Cancellation));
|
||||
// The drive goes before the next start.
|
||||
var away = legacy.Workspace + "-unplugged";
|
||||
Directory.Move(legacy.Workspace, away);
|
||||
var waiting = await legacy.Migration().ApplyAsync(context.Cancellation);
|
||||
Assert.Equal((HomeLayoutDeferral?)HomeLayoutDeferral.Offline, waiting.Deferred);
|
||||
Assert.FileExists(Path.Combine(legacy.Data, HomeLayoutMigration.JournalFile));
|
||||
Assert.FileExists(Path.Combine(legacy.Cache, "servers", HomeFolders.ServerFolder(HomeFixtures.Nas), "import", "card.part"));
|
||||
// Back: the workspace is sorted, the cache left as it is now.
|
||||
Directory.Move(away, legacy.Workspace);
|
||||
await legacy.Migration().ApplyAsync(context.Cancellation);
|
||||
AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas));
|
||||
Assert.SequenceEqual(before, HomeFixtures.Manifest(legacy.Workspace));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AProjectInBothPlacesKeepsTheCopySavedLastAndTrashesTheOther()
|
||||
{
|
||||
var data = context.CreateTempDirectory("data");
|
||||
var workspace = HomeFixtures.LegacyWorkspace(data);
|
||||
var id = Guid.CreateVersion7();
|
||||
var home = HomeFixtures.Local();
|
||||
HomeFixtures.Project(workspace, home, "newer", DateTimeOffset.UtcNow, id);
|
||||
HomeFixtures.Project(Path.Combine(workspace, "local"), home, "older", DateTimeOffset.UtcNow.AddDays(-1), id);
|
||||
var files = Directory.EnumerateFiles(workspace, "*", SearchOption.AllDirectories).Count(f => Path.GetFileName(f) != WorkspaceStore.ManifestFile);
|
||||
var result = await new HomeLayoutMigration(new HomeLayoutOptions { DataDirectory = data, WorkspaceRoot = workspace }).ApplyAsync(context.Cancellation);
|
||||
Assert.Equal(1, result.Clashes);
|
||||
var kept = ProjectBundle.Load(Path.Combine(workspace, "local", id.ToString("N"), WorkspaceProject.BundleFile));
|
||||
Assert.Equal("newer", kept.Workspace!.Name);
|
||||
var trashed = Assert.NotNull(Directory.EnumerateDirectories(Path.Combine(workspace, "local", WorkspaceTrash.FolderName)).SingleOrDefault());
|
||||
Assert.Equal("older", ProjectBundle.Load(Path.Combine(trashed, WorkspaceProject.BundleFile)).Workspace!.Name);
|
||||
Assert.Equal(files, Directory.EnumerateFiles(workspace, "*", SearchOption.AllDirectories)
|
||||
.Count(f => Path.GetFileName(f) is not (WorkspaceStore.ManifestFile or "server.json")), "nothing was deleted");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ARenameThatDoesNotCheckStopsWithItsJournal()
|
||||
{
|
||||
var legacy = Make();
|
||||
var tampered = false;
|
||||
var migration = legacy.Migration(cut: (name, path) =>
|
||||
{
|
||||
if (name != "renamed" || tampered || !Directory.Exists(path)) return;
|
||||
tampered = true;
|
||||
File.WriteAllText(Path.Combine(path, "stray.txt"), "written meanwhile");
|
||||
});
|
||||
await Assert.ThrowsAsync<HomeLayoutException>(() => migration.ApplyAsync(context.Cancellation));
|
||||
Assert.FileExists(Path.Combine(legacy.Data, HomeLayoutMigration.JournalFile));
|
||||
// The next start takes it up; the entry found renamed is taken as moved.
|
||||
await legacy.Migration().ApplyAsync(context.Cancellation);
|
||||
AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AWorkspaceNeverMadeOrAlreadySortedHasNothingToDo()
|
||||
{
|
||||
var data = context.CreateTempDirectory("data");
|
||||
var options = new HomeLayoutOptions { DataDirectory = data, WorkspaceRoot = Path.Combine(data, "workspace"), CacheRoot = Path.Combine(data, "cache") };
|
||||
Assert.True((await new HomeLayoutMigration(options).PlanAsync(context.Cancellation)).IsEmpty);
|
||||
Assert.Equal(HomeLayoutResult.Nothing, await new HomeLayoutMigration(options).ApplyAsync(context.Cancellation));
|
||||
Assert.False(Directory.Exists(options.WorkspaceRoot));
|
||||
|
||||
_ = new WorkspaceHomes(options.WorkspaceRoot);
|
||||
Assert.True((await new HomeLayoutMigration(options).PlanAsync(context.Cancellation)).IsEmpty);
|
||||
|
||||
// An empty 1.0 workspace is split by its manifest alone.
|
||||
var empty = HomeFixtures.LegacyWorkspace(context.CreateTempDirectory("empty"));
|
||||
var split = await new HomeLayoutMigration(options with { WorkspaceRoot = empty }).ApplyAsync(context.Cancellation);
|
||||
Assert.True(split.Split);
|
||||
Assert.True(WorkspaceHomes.IsSplitRoot(empty));
|
||||
Assert.Equal(WorkspaceStore.FormatVersion, new WorkspaceStore(Path.Combine(empty, "local")).Manifest.FormatVersion);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
using Chronos.Core.IO;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Data.Homes;
|
||||
using Chronos.Data.Projects;
|
||||
using Chronos.Data.Sync;
|
||||
using Chronos.Data.Tests.Infrastructure;
|
||||
using Chronos.Data.Tests.Repositories;
|
||||
using Chronos.Data.Workspaces;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.Data.Tests.Homes;
|
||||
|
||||
/// <summary>A server's home moves from its address to its id when it first announces one (owner review 5, #66).</summary>
|
||||
[Category("homes")]
|
||||
public sealed class HomeRekeyTests(TestContext context)
|
||||
{
|
||||
private static readonly Guid Id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01");
|
||||
private static string IdFolder => Id.ToString("N");
|
||||
|
||||
private (string Data, WorkspaceHomes Homes, string Cache) Make()
|
||||
{
|
||||
var data = context.CreateTempDirectory("data");
|
||||
return (data, new WorkspaceHomes(Path.Combine(data, "workspace")), Path.Combine(data, "cache"));
|
||||
}
|
||||
|
||||
private static HomeLayoutMigration Migration(string data, WorkspaceHomes homes, string cache) =>
|
||||
new(new HomeLayoutOptions { DataDirectory = data, WorkspaceRoot = homes.Root, CacheRoot = cache });
|
||||
|
||||
private static string AddressHome(string root, string key) => Path.Combine(root, "servers", HomeFolders.ServerFolder(key));
|
||||
|
||||
private static ProjectBundle Bundle(string home, Guid id) => ProjectBundle.Load(Path.Combine(home, id.ToString("N"), WorkspaceProject.BundleFile));
|
||||
|
||||
[Test]
|
||||
public async Task TheHomeOfTheAddressBecomesTheHomeOfTheId()
|
||||
{
|
||||
var (data, homes, cache) = Make();
|
||||
var home = HomeFixtures.Server(HomeFixtures.Nas);
|
||||
var store = homes.StoreFor(home);
|
||||
var (a, b) = (HomeFixtures.Project(store.Root, home), HomeFixtures.Project(store.Root, HomeFixtures.Server(HomeFixtures.Nas)));
|
||||
HomeFixtures.TrashEntry(store.Root, a, home);
|
||||
var before = HomeFixtures.Manifest(homes.Root);
|
||||
var (sorted, old) = (Guid.CreateVersion7(), Guid.CreateVersion7());
|
||||
var cached = Path.Combine(AddressHome(cache, HomeFixtures.Nas), "proxies", sorted.ToString("N"));
|
||||
Directory.CreateDirectory(cached);
|
||||
File.WriteAllText(Path.Combine(cached, "remote.json"), "{}");
|
||||
HomeFixtures.LegacyProxies(cache, HomeFixtures.Nas, old);
|
||||
|
||||
var result = await Migration(data, homes, cache).RekeyAsync(Id, [HomeFixtures.Nas], context.Cancellation);
|
||||
Assert.Equal(new HomeRekeyResult(2, 0, 0, 2, 0, 0, 0), result);
|
||||
var idHome = Path.Combine(homes.Root, "servers", IdFolder);
|
||||
Assert.False(Directory.Exists(AddressHome(homes.Root, HomeFixtures.Nas)));
|
||||
var idKey = ServerIdentity.IdKey(Id);
|
||||
foreach (var id in new[] { a, b })
|
||||
{
|
||||
var bundle = Bundle(idHome, id);
|
||||
Assert.Equal(idKey, bundle.Workspace!.Home!.Server);
|
||||
Assert.Equal(ServerProjectHome.ArchiveIdOf(idKey), bundle.Archive.Id);
|
||||
}
|
||||
var record = Assert.NotNull(ServerHomeRecord.Read(idHome));
|
||||
Assert.Equal((idKey, (Guid?)Id, HomeFixtures.Nas), (record.Key, record.Id, record.Address));
|
||||
Assert.Count(1, Directory.EnumerateDirectories(Path.Combine(idHome, WorkspaceTrash.FolderName)));
|
||||
// Only the working states were rewritten: every other file is as it was.
|
||||
var after = HomeFixtures.Manifest(homes.Root);
|
||||
Assert.SequenceEqual(before.Keys, after.Keys);
|
||||
Assert.SequenceEqual(before.Where(f => !f.Key.EndsWith(WorkspaceProject.BundleFile, StringComparison.Ordinal)),
|
||||
after.Where(f => !f.Key.EndsWith(WorkspaceProject.BundleFile, StringComparison.Ordinal)));
|
||||
|
||||
Assert.FileExists(Path.Combine(cache, "servers", IdFolder, "proxies", sorted.ToString("N"), "remote.json"));
|
||||
Assert.FileExists(Path.Combine(cache, "servers", IdFolder, "proxies", old.ToString("N"), "remote.json"));
|
||||
Assert.False(Directory.Exists(AddressHome(cache, HomeFixtures.Nas)));
|
||||
// The store now finds the id home by either key.
|
||||
var identity = new ServerIdentity([new KnownServerKey(Id, [HomeFixtures.Nas])]);
|
||||
Assert.Equal(idHome, new WorkspaceHomes(homes.Root, identity).ServerRoot(HomeFixtures.Nas));
|
||||
|
||||
// Again: nothing left to do.
|
||||
Assert.Equal(new HomeRekeyResult(0, 0, 0, 0, 0, 0, 0), await Migration(data, homes, cache).RekeyAsync(Id, [HomeFixtures.Nas], context.Cancellation));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TwoAddressesOfOneServerMergeAndTheOlderCopyGoesToTheTrash()
|
||||
{
|
||||
var (data, homes, cache) = Make();
|
||||
var byName = HomeFixtures.Server(HomeFixtures.Nas);
|
||||
var byIp = HomeFixtures.Server(HomeFixtures.NasByIp);
|
||||
var first = homes.StoreFor(byName).Root;
|
||||
var second = homes.StoreFor(byIp).Root;
|
||||
var both = Guid.CreateVersion7();
|
||||
HomeFixtures.Project(first, byName, "older", DateTimeOffset.UtcNow.AddDays(-1), both);
|
||||
HomeFixtures.Project(second, byIp, "newer", DateTimeOffset.UtcNow, both);
|
||||
var onlyFirst = HomeFixtures.Project(first, byName);
|
||||
var onlySecond = HomeFixtures.Project(second, byIp);
|
||||
HomeFixtures.TrashEntry(first, onlyFirst, byName);
|
||||
HomeFixtures.TrashEntry(second, onlyFirst, byIp);
|
||||
var files = Directory.EnumerateFiles(homes.Root, "*", SearchOption.AllDirectories).Count(IsProjectFile);
|
||||
// Each address had a proxy cache too.
|
||||
Directory.CreateDirectory(Path.Combine(AddressHome(cache, HomeFixtures.Nas), "proxies", "a"));
|
||||
Directory.CreateDirectory(Path.Combine(AddressHome(cache, HomeFixtures.NasByIp), "proxies", "a"));
|
||||
Directory.CreateDirectory(Path.Combine(AddressHome(cache, HomeFixtures.NasByIp), "proxies", "b"));
|
||||
|
||||
var result = await Migration(data, homes, cache).RekeyAsync(Id, [HomeFixtures.Nas, HomeFixtures.NasByIp], context.Cancellation);
|
||||
Assert.Equal((2, 2, 1, 3), (result.HomesMoved, result.ProjectsMoved, result.ProjectsTrashed, result.BundlesRewritten));
|
||||
var idHome = Path.Combine(homes.Root, "servers", IdFolder);
|
||||
Assert.Equal("newer", Bundle(idHome, both).Workspace!.Name);
|
||||
foreach (var id in new[] { onlyFirst, onlySecond }) Assert.FileExists(Path.Combine(idHome, id.ToString("N"), WorkspaceProject.BundleFile));
|
||||
var trash = Directory.EnumerateDirectories(Path.Combine(idHome, WorkspaceTrash.FolderName)).ToList();
|
||||
Assert.Equal(3, trash.Count);
|
||||
Assert.Contains(trash, t => ProjectBundle.Load(Path.Combine(t, WorkspaceProject.BundleFile)).Workspace!.Name == "older");
|
||||
// The second address's home held only its manifest and record afterwards: it went. Nothing else was deleted.
|
||||
Assert.False(Directory.Exists(second));
|
||||
Assert.Equal(files, Directory.EnumerateFiles(homes.Root, "*", SearchOption.AllDirectories).Count(IsProjectFile));
|
||||
Assert.Count(1, Directory.EnumerateDirectories(Path.Combine(homes.Root, "servers")));
|
||||
Assert.True(Directory.Exists(Path.Combine(cache, "servers", IdFolder, "proxies", "b")));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AProjectInUseStaysAndKeepsItsAliasWhichStillMatches()
|
||||
{
|
||||
var (data, homes, cache) = Make();
|
||||
var home = HomeFixtures.Server(HomeFixtures.Nas);
|
||||
var root = homes.StoreFor(home).Root;
|
||||
var (held, free) = (HomeFixtures.Project(root, home), HomeFixtures.Project(root, HomeFixtures.Server(HomeFixtures.Nas)));
|
||||
HomeRekeyResult result;
|
||||
using (HomeFixtures.Hold(Path.Combine(root, held.ToString("N"))))
|
||||
{
|
||||
// A project in use keeps the whole home where it is until a later re-key; the id home is made beside it.
|
||||
_ = new WorkspaceStore(Path.Combine(homes.Root, "servers", IdFolder));
|
||||
result = await Migration(data, homes, cache).RekeyAsync(Id, [HomeFixtures.Nas], context.Cancellation);
|
||||
}
|
||||
Assert.Equal((0, 1, 1), (result.HomesMoved, result.ProjectsMoved, result.BundlesRewritten));
|
||||
Assert.FileExists(Path.Combine(root, held.ToString("N"), WorkspaceProject.BundleFile));
|
||||
Assert.Equal(HomeFixtures.Nas, Bundle(root, held).Workspace!.Home!.Server);
|
||||
var idHome = Path.Combine(homes.Root, "servers", IdFolder);
|
||||
Assert.Equal(ServerIdentity.IdKey(Id), Bundle(idHome, free).Workspace!.Home!.Server);
|
||||
Assert.True(ServerProjectHome.IsArchiveIdOf(Bundle(root, held).Archive.Id, [ServerIdentity.IdKey(Id), HomeFixtures.Nas]));
|
||||
Assert.NotNull(homes.Find(held));
|
||||
|
||||
result = await Migration(data, homes, cache).RekeyAsync(Id, [HomeFixtures.Nas], context.Cancellation);
|
||||
Assert.Equal((1, 1), (result.ProjectsMoved, result.BundlesRewritten));
|
||||
Assert.False(Directory.Exists(root));
|
||||
Assert.Equal(ServerIdentity.IdKey(Id), Bundle(idHome, held).Workspace!.Home!.Server);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AWorkingStateInUseInTheIdHomeIsLeftNamingTheAlias()
|
||||
{
|
||||
var (data, homes, cache) = Make();
|
||||
var home = HomeFixtures.Server(HomeFixtures.Nas);
|
||||
var root = homes.StoreFor(home).Root;
|
||||
var id = HomeFixtures.Project(root, home);
|
||||
// Renamed whole first (nothing held), then held while its working state would be rewritten.
|
||||
var idHome = Path.Combine(homes.Root, "servers", IdFolder);
|
||||
Directory.Move(root, idHome);
|
||||
HomeRekeyResult result;
|
||||
using (HomeFixtures.Hold(Path.Combine(idHome, id.ToString("N"))))
|
||||
result = await Migration(data, homes, cache).RekeyAsync(Id, [HomeFixtures.Nas], context.Cancellation);
|
||||
Assert.Equal((0, 1), (result.BundlesRewritten, result.BundlesInUse));
|
||||
Assert.Equal(HomeFixtures.Nas, Bundle(idHome, id).Workspace!.Home!.Server);
|
||||
Assert.Equal(ServerIdentity.IdKey(Id), Assert.NotNull(ServerHomeRecord.Read(idHome)).Key);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnIdKeyIsNeverAnAliasAndAnEmptyIdIsRefused()
|
||||
{
|
||||
var (data, homes, cache) = Make();
|
||||
await Assert.ThrowsAsync<ArgumentException>(() => Migration(data, homes, cache).RekeyAsync(Guid.Empty, [HomeFixtures.Nas], context.Cancellation));
|
||||
var other = ServerIdentity.IdKey(Guid.CreateVersion7());
|
||||
var otherHome = homes.StoreFor(HomeFixtures.Server(other)).Root;
|
||||
Assert.Equal(new HomeRekeyResult(0, 0, 0, 0, 0, 0, 0), await Migration(data, homes, cache).RekeyAsync(Id, [other], context.Cancellation));
|
||||
Assert.True(Directory.Exists(otherHome));
|
||||
}
|
||||
|
||||
private static bool IsProjectFile(string path) => Path.GetFileName(path) is not (WorkspaceStore.ManifestFile or HomeFolders.ServerFile);
|
||||
}
|
||||
|
||||
/// <summary>A re-key moves the server's links in the index too.</summary>
|
||||
[Category("homes")]
|
||||
public abstract class HomeRekeyLinkTests(TestContext context) : RepositoryTest(context)
|
||||
{
|
||||
private static readonly Guid Id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01");
|
||||
|
||||
[Test]
|
||||
public async Task TheLinksOfEveryAliasMoveToTheId()
|
||||
{
|
||||
var db = await OpenChronosAsync();
|
||||
var store = new SyncLinkStore(db);
|
||||
foreach (var key in new[] { HomeFixtures.Nas, HomeFixtures.NasByIp, HomeFixtures.Other })
|
||||
{
|
||||
await store.UpsertAsync(new SyncLink
|
||||
{
|
||||
Server = key,
|
||||
RemoteId = Guid.CreateVersion7(),
|
||||
Title = "Harbour",
|
||||
RemoteUpdatedAt = TestData.Now,
|
||||
SyncedAt = TestData.Now,
|
||||
}, Ct);
|
||||
}
|
||||
var data = Context.CreateTempDirectory("data");
|
||||
var migration = new HomeLayoutMigration(new HomeLayoutOptions { DataDirectory = data, Database = db });
|
||||
var result = await migration.RekeyAsync(Id, [HomeFixtures.Nas, HomeFixtures.NasByIp], Ct);
|
||||
Assert.Equal((2, 0), (result.LinksMoved, result.LinksDropped));
|
||||
Assert.Count(2, await store.ListAsync(ServerIdentity.IdKey(Id), Ct));
|
||||
Assert.Count(1, await store.ListAsync(HomeFixtures.Other, Ct));
|
||||
Assert.Empty(await store.ListAsync(HomeFixtures.Nas, Ct));
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SqliteHomeRekeyLinkTests(TestContext context) : HomeRekeyLinkTests(context)
|
||||
{
|
||||
protected override Backend Backend => Backend.SqliteMemory;
|
||||
}
|
||||
|
||||
public sealed class PostgresHomeRekeyLinkTests(TestContext context) : HomeRekeyLinkTests(context)
|
||||
{
|
||||
protected override Backend Backend => Backend.Postgres;
|
||||
}
|
||||
Reference in new issue
Block a user