A new `log_janitor` setting added to `settings.yml` to allow the user to specify the maximum number of files to keep when doing log cleanup. Additionally, the way startup tasks occur has been cleaned up / refactored to make it easier to integration test.pull/124/head
parent
af001250f1
commit
c5714f2bed
@ -0,0 +1,92 @@
|
||||
using System.IO.Abstractions;
|
||||
using System.IO.Abstractions.TestingHelpers;
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
using Recyclarr.Command.Setup;
|
||||
using Recyclarr.TestLibrary;
|
||||
using TrashLib.Config.Settings;
|
||||
using TrashLib.Startup;
|
||||
|
||||
namespace Recyclarr.Tests;
|
||||
|
||||
[TestFixture]
|
||||
[Parallelizable(ParallelScope.All)]
|
||||
public class BaseCommandSetupIntegrationTest : IntegrationFixture
|
||||
{
|
||||
[Test]
|
||||
public void Base_command_startup_tasks_are_registered()
|
||||
{
|
||||
var registrations = Resolve<IEnumerable<IBaseCommandSetupTask>>();
|
||||
registrations.Select(x => x.GetType()).Should().BeEquivalentTo(new[]
|
||||
{
|
||||
typeof(JanitorCleanupTask),
|
||||
typeof(AppPathSetupTask)
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Log_janitor_cleans_up_user_specified_max_files()
|
||||
{
|
||||
var paths = Resolve<IAppPaths>();
|
||||
const int maxFiles = 25;
|
||||
|
||||
Fs.AddFile(paths.SettingsPath.FullName, new MockFileData($@"
|
||||
log_janitor:
|
||||
max_files: {maxFiles}
|
||||
"));
|
||||
|
||||
for (var i = 0; i < maxFiles+20; ++i)
|
||||
{
|
||||
Fs.AddFile(paths.LogDirectory.File($"logfile-{i}.log").FullName, new MockFileData(""));
|
||||
}
|
||||
|
||||
var sut = Resolve<JanitorCleanupTask>();
|
||||
sut.OnFinish();
|
||||
|
||||
Fs.AllFiles.Where(x => x.StartsWith(paths.LogDirectory.FullName))
|
||||
.Should().HaveCount(maxFiles);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Log_janitor_cleans_up_default_max_files()
|
||||
{
|
||||
var paths = Resolve<IAppPaths>();
|
||||
var settingsProvider = Resolve<ISettingsProvider>();
|
||||
var maxFiles = settingsProvider.Settings.LogJanitor.MaxFiles;
|
||||
|
||||
for (var i = 0; i < maxFiles+20; ++i)
|
||||
{
|
||||
Fs.AddFile(paths.LogDirectory.File($"logfile-{i}.log").FullName, new MockFileData(""));
|
||||
}
|
||||
|
||||
var sut = Resolve<JanitorCleanupTask>();
|
||||
sut.OnFinish();
|
||||
|
||||
maxFiles.Should().BeGreaterThan(0);
|
||||
Fs.AllFiles.Where(x => x.StartsWith(paths.LogDirectory.FullName))
|
||||
.Should().HaveCount(maxFiles);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void App_paths_setup_creates_initial_directories()
|
||||
{
|
||||
var paths = Resolve<IAppPaths>();
|
||||
|
||||
for (var i = 0; i < 50; ++i)
|
||||
{
|
||||
Fs.AddFile(paths.LogDirectory.File($"logfile-{i}.log").FullName, new MockFileData(""));
|
||||
}
|
||||
|
||||
var sut = Resolve<AppPathSetupTask>();
|
||||
sut.OnStart();
|
||||
|
||||
var expectedDirs = new[]
|
||||
{
|
||||
paths.LogDirectory.FullName,
|
||||
paths.RepoDirectory.FullName,
|
||||
paths.CacheDirectory.FullName
|
||||
};
|
||||
|
||||
expectedDirs.Should().IntersectWith(Fs.AllDirectories);
|
||||
}
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
using System.IO.Abstractions.TestingHelpers;
|
||||
using AutoFixture.NUnit3;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Infrastructure;
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
using Recyclarr.Command;
|
||||
using TestLibrary.AutoFixture;
|
||||
using TrashLib.TestLibrary;
|
||||
|
||||
namespace Recyclarr.Tests.Command;
|
||||
|
||||
[Command]
|
||||
public class StubBaseCommand : BaseCommand
|
||||
{
|
||||
public override string? AppDataDirectory { get; set; }
|
||||
|
||||
public StubBaseCommand(ICompositionRoot compositionRoot)
|
||||
{
|
||||
CompositionRoot = compositionRoot;
|
||||
}
|
||||
|
||||
public override Task Process(IServiceLocatorProxy container)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
// Cannot be parallelized due to static CompositionRoot property
|
||||
public class BaseCommandTest
|
||||
{
|
||||
[Test, AutoMockData]
|
||||
public async Task All_directories_are_created(
|
||||
[Frozen(Matching.ImplementedInterfaces)] MockFileSystem fs,
|
||||
[Frozen(Matching.ImplementedInterfaces)] TestAppPaths paths,
|
||||
IConsole console,
|
||||
StubBaseCommand sut)
|
||||
{
|
||||
await sut.ExecuteAsync(console);
|
||||
|
||||
var expectedDirs = new[]
|
||||
{
|
||||
paths.LogDirectory.FullName,
|
||||
paths.RepoDirectory.FullName,
|
||||
paths.CacheDirectory.FullName
|
||||
};
|
||||
|
||||
expectedDirs.Should().IntersectWith(fs.AllDirectories);
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
using Serilog;
|
||||
using TrashLib.Startup;
|
||||
|
||||
namespace Recyclarr.Command.Setup;
|
||||
|
||||
public class AppPathSetupTask : IBaseCommandSetupTask
|
||||
{
|
||||
private readonly ILogger _log;
|
||||
private readonly IAppPaths _paths;
|
||||
|
||||
public AppPathSetupTask(ILogger log, IAppPaths paths)
|
||||
{
|
||||
_log = log;
|
||||
_paths = paths;
|
||||
}
|
||||
|
||||
public void OnStart()
|
||||
{
|
||||
_log.Debug("App Data Dir: {AppData}", _paths.AppDataDirectory);
|
||||
|
||||
// Initialize other directories used throughout the application
|
||||
_paths.RepoDirectory.Create();
|
||||
_paths.CacheDirectory.Create();
|
||||
_paths.LogDirectory.Create();
|
||||
}
|
||||
|
||||
public void OnFinish()
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
namespace Recyclarr.Command.Setup;
|
||||
|
||||
public interface IBaseCommandSetupTask
|
||||
{
|
||||
void OnStart();
|
||||
void OnFinish();
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
using Recyclarr.Logging;
|
||||
using Serilog;
|
||||
using TrashLib.Config.Settings;
|
||||
|
||||
namespace Recyclarr.Command.Setup;
|
||||
|
||||
public class JanitorCleanupTask : IBaseCommandSetupTask
|
||||
{
|
||||
private readonly ILogJanitor _janitor;
|
||||
private readonly ILogger _log;
|
||||
private readonly ISettingsProvider _settingsProvider;
|
||||
|
||||
public JanitorCleanupTask(ILogJanitor janitor, ILogger log, ISettingsProvider settingsProvider)
|
||||
{
|
||||
_janitor = janitor;
|
||||
_log = log;
|
||||
_settingsProvider = settingsProvider;
|
||||
}
|
||||
|
||||
public void OnStart()
|
||||
{
|
||||
}
|
||||
|
||||
public void OnFinish()
|
||||
{
|
||||
var maxFiles = _settingsProvider.Settings.LogJanitor.MaxFiles;
|
||||
_log.Debug("Cleaning up logs using max files of {MaxFiles}", maxFiles);
|
||||
_janitor.DeleteOldestLogFiles(maxFiles);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue