1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Jellyfin.Database.Implementations;
using Jellyfin.Database.Implementations.Entities;
using Jellyfin.Database.Implementations.Locking;
using Jellyfin.Database.Providers.Sqlite;
using Jellyfin.Server.Migrations.Routines;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;
namespace Jellyfin.Server.Tests.Migrations;
/// <summary>
/// Covers which items the stale file cleanup is allowed to delete. Resolving no library locations at
/// all puts every file-backed item outside every location, so acting on that emptied whole libraries.
/// </summary>
public sealed class MigrateLinkedChildrenStaleItemsTests : IDisposable
{
private readonly SqliteConnection _connection;
private readonly DbContextOptions<JellyfinDbContext> _dbOptions;
private readonly IApplicationPaths _applicationPaths;
private readonly string _root;
public MigrateLinkedChildrenStaleItemsTests()
{
_applicationPaths = new Mock<IApplicationPaths>().Object;
_root = Path.Combine(Path.GetTempPath(), "jf-stale-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(Path.Combine(_root, "media"));
Directory.CreateDirectory(Path.Combine(_root, "root", "default"));
_connection = new SqliteConnection("Data Source=:memory:");
_connection.Open();
_dbOptions = new DbContextOptionsBuilder<JellyfinDbContext>()
.UseSqlite(_connection)
.Options;
using var context = CreateDbContext();
context.Database.EnsureCreated();
}
private string LibraryDefinitionRoot => Path.Combine(_root, "root", "default");
[Fact]
public void Perform_NoLibraryLocationsResolved_KeepsEverything()
{
var itemId = Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa");
SeedItem(itemId, SeedMediaFile("kept.flac"));
var deleted = Perform(libraryLocations: []);
Assert.Empty(deleted);
}
[Fact]
public void Perform_ItemOutsideEveryLocation_IsRemoved()
{
var itemId = Guid.Parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb");
// An empty location counts as inaccessible, which disables the removal on purpose, so the
// library has to hold something for this case to be reachable at all.
SeedMediaFile("anchor.flac");
SeedItem(itemId, Path.Combine(_root, "removed-media", "gone.flac"));
var deleted = Perform(libraryLocations: [Path.Combine(_root, "media")]);
Assert.Equal([itemId], deleted);
}
[Fact]
public void Perform_ItemInsideLocationWithMissingFile_IsRemoved()
{
var itemId = Guid.Parse("cccccccc-cccc-cccc-cccc-cccccccccccc");
SeedMediaFile("anchor.flac");
SeedItem(itemId, Path.Combine(_root, "media", "deleted.flac"));
var deleted = Perform(libraryLocations: [Path.Combine(_root, "media")]);
Assert.Equal([itemId], deleted);
}
[Fact]
public void Perform_ItemInsideLocationWithFilePresent_IsKept()
{
var itemId = Guid.Parse("dddddddd-dddd-dddd-dddd-dddddddddddd");
SeedItem(itemId, SeedMediaFile("present.flac"));
var deleted = Perform(libraryLocations: [Path.Combine(_root, "media")]);
Assert.Empty(deleted);
}
[Fact]
public void Perform_LibraryDefinitionMissing_KeepsItsItems()
{
var libraryId = Guid.Parse("11111111-1111-1111-1111-111111111111");
var itemId = Guid.Parse("22222222-2222-2222-2222-222222222222");
// The definition folder of "Movies" is gone, so it contributes no locations and everything it
// held reads as unrooted, while "Media" still resolves and makes the run look actionable.
SeedLibrary(libraryId, "Movies", createDefinitionFolder: false);
SeedMediaFile("anchor.flac");
SeedItem(itemId, Path.Combine(_root, "movies", "kept.mkv"), libraryId);
var deleted = Perform(libraryLocations: [Path.Combine(_root, "media")]);
Assert.Empty(deleted);
}
[Fact]
public void Perform_PathRemovedFromSurvivingLibrary_IsRemoved()
{
var libraryId = Guid.Parse("33333333-3333-3333-3333-333333333333");
var itemId = Guid.Parse("44444444-4444-4444-4444-444444444444");
// The library is still there; only one of its media paths went away, which is what this
// branch exists to clean up.
SeedLibrary(libraryId, "Media", createDefinitionFolder: true);
SeedMediaFile("anchor.flac");
SeedItem(itemId, Path.Combine(_root, "removed-media", "gone.flac"), libraryId);
var deleted = Perform(libraryLocations: [Path.Combine(_root, "media")]);
Assert.Equal([itemId], deleted);
}
public void Dispose()
{
_connection.Dispose();
if (Directory.Exists(_root))
{
Directory.Delete(_root, true);
}
}
private JellyfinDbContext CreateDbContext() => new(
_dbOptions,
NullLogger<JellyfinDbContext>.Instance,
new SqliteDatabaseProvider(_applicationPaths, NullLogger<SqliteDatabaseProvider>.Instance),
new NoLockBehavior(NullLogger<NoLockBehavior>.Instance));
private void SeedLibrary(Guid id, string name, bool createDefinitionFolder)
{
var path = Path.Combine(LibraryDefinitionRoot, name);
if (createDefinitionFolder)
{
Directory.CreateDirectory(path);
}
using var context = CreateDbContext();
context.BaseItems.Add(new BaseItemEntity
{
Id = id,
Type = "MediaBrowser.Controller.Entities.CollectionFolder",
Name = name,
Path = path,
IsFolder = true
});
context.SaveChanges();
}
private string SeedMediaFile(string name)
{
var path = Path.Combine(_root, "media", name);
File.WriteAllBytes(path, [0]);
return path;
}
private void SeedItem(Guid id, string path, Guid? libraryId = null)
{
using var context = CreateDbContext();
context.BaseItems.Add(new BaseItemEntity
{
Id = id,
Type = typeof(Audio).FullName!,
Name = Path.GetFileNameWithoutExtension(path),
Path = path,
IsFolder = false,
IsVirtualItem = false
});
context.SaveChanges();
if (libraryId.HasValue)
{
context.Database.ExecuteSql(
$"INSERT INTO AncestorIds (ItemId, ParentItemId) VALUES ({id}, {libraryId.Value})");
}
}
private IReadOnlyList<Guid> Perform(string[] libraryLocations, string libraryName = "Media")
{
var deleted = new List<Guid>();
var libraryManager = new Mock<ILibraryManager>();
libraryManager
.Setup(l => l.GetVirtualFolders())
.Returns(libraryLocations.Length == 0
? []
: [new VirtualFolderInfo { Name = libraryName, Locations = libraryLocations }]);
libraryManager
.Setup(l => l.GetItemById(It.IsAny<Guid>()))
.Returns<Guid>(id => new Audio { Id = id });
libraryManager
.Setup(l => l.DeleteItem(It.IsAny<BaseItem>(), It.IsAny<DeleteOptions>()))
.Callback<BaseItem, DeleteOptions>((item, _) => deleted.Add(item.Id));
var appHost = new Mock<IServerApplicationHost>();
appHost.Setup(h => h.ExpandVirtualPath(It.IsAny<string>())).Returns<string>(p => p);
var appPaths = new Mock<IServerApplicationPaths>();
appPaths.SetupGet(p => p.DataPath).Returns(Path.Combine(_root, "data"));
appPaths.SetupGet(p => p.InternalMetadataPath).Returns(Path.Combine(_root, "metadata"));
appPaths.SetupGet(p => p.DefaultUserViewsPath).Returns(LibraryDefinitionRoot);
var factory = new Mock<IDbContextFactory<JellyfinDbContext>>();
factory.Setup(f => f.CreateDbContext()).Returns(CreateDbContext);
factory.Setup(f => f.CreateDbContextAsync(It.IsAny<CancellationToken>())).ReturnsAsync(CreateDbContext);
new MigrateLinkedChildren(
NullLoggerFactory.Instance,
factory.Object,
libraryManager.Object,
appHost.Object,
appPaths.Object)
.Perform();
return deleted;
}
}
|