jellyfin/tests/Jellyfin.Server.Integration.Tests/JellyfinApplicationFactory.cs

127 lines
5.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Concurrent;
2022-11-27 06:49:21 -07:00
using System.Globalization;
using System.IO;
2021-02-23 09:30:24 -07:00
using System.Threading;
using Emby.Server.Implementations;
using Jellyfin.Server.Extensions;
using Jellyfin.Server.Helpers;
using MediaBrowser.Common;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
2023-01-12 09:51:12 -07:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Serilog;
using Serilog.Extensions.Logging;
namespace Jellyfin.Server.Integration.Tests
{
/// <summary>
/// Factory for bootstrapping the Jellyfin application in memory for functional end to end tests.
/// </summary>
public class JellyfinApplicationFactory : WebApplicationFactory<Startup>
{
private static readonly string _testPathRoot = Path.Combine(Path.GetTempPath(), "jellyfin-test-data");
2021-02-23 09:30:24 -07:00
private readonly ConcurrentBag<IDisposable> _disposableComponents = new ConcurrentBag<IDisposable>();
/// <summary>
/// Initializes static members of the <see cref="JellyfinApplicationFactory"/> class.
/// </summary>
static JellyfinApplicationFactory()
{
// Perform static initialization that only needs to happen once per test-run
2022-11-27 06:49:21 -07:00
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(formatProvider: CultureInfo.InvariantCulture)
.CreateLogger();
StartupHelpers.PerformStaticInitialization();
}
/// <inheritdoc/>
protected override IWebHostBuilder CreateWebHostBuilder()
{
return new WebHostBuilder();
}
/// <inheritdoc/>
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
// Specify the startup command line options
var commandLineOpts = new StartupOptions();
// Use a temporary directory for the application paths
var webHostPathRoot = Path.Combine(_testPathRoot, "test-host-" + Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
Directory.CreateDirectory(Path.Combine(webHostPathRoot, "logs"));
Directory.CreateDirectory(Path.Combine(webHostPathRoot, "config"));
Directory.CreateDirectory(Path.Combine(webHostPathRoot, "cache"));
Directory.CreateDirectory(Path.Combine(webHostPathRoot, "jellyfin-web"));
var appPaths = new ServerApplicationPaths(
webHostPathRoot,
Path.Combine(webHostPathRoot, "logs"),
Path.Combine(webHostPathRoot, "config"),
Path.Combine(webHostPathRoot, "cache"),
Path.Combine(webHostPathRoot, "jellyfin-web"));
// Create the logging config file
// TODO: We shouldn't need to do this since we are only logging to console
StartupHelpers.InitLoggingConfigFile(appPaths).GetAwaiter().GetResult();
// Create a copy of the application configuration to use for startup
var startupConfig = Program.CreateAppConfiguration(commandLineOpts, appPaths);
ILoggerFactory loggerFactory = new SerilogLoggerFactory();
2021-11-02 08:02:52 -07:00
_disposableComponents.Add(loggerFactory);
// Create the app host and initialize it
2021-02-22 05:15:29 -07:00
var appHost = new TestAppHost(
appPaths,
loggerFactory,
commandLineOpts,
2022-02-14 06:39:33 -07:00
startupConfig);
_disposableComponents.Add(appHost);
2023-01-12 09:51:12 -07:00
builder.ConfigureServices(services => appHost.Init(services))
.ConfigureWebHostBuilder(appHost, startupConfig, appPaths, NullLogger.Instance)
2023-01-12 09:51:12 -07:00
.ConfigureAppConfiguration((context, builder) =>
{
builder
.SetBasePath(appPaths.ConfigurationDirectoryPath)
.AddInMemoryCollection(ConfigurationOptions.DefaultConfiguration)
.AddEnvironmentVariables("JELLYFIN_")
.AddInMemoryCollection(commandLineOpts.ConvertToConfig());
});
}
/// <inheritdoc/>
protected override TestServer CreateServer(IWebHostBuilder builder)
{
// Create the test server using the base implementation
var testServer = base.CreateServer(builder);
// Finish initializing the app host
2021-02-22 05:15:29 -07:00
var appHost = (TestAppHost)testServer.Services.GetRequiredService<IApplicationHost>();
appHost.ServiceProvider = testServer.Services;
appHost.InitializeServices().GetAwaiter().GetResult();
2021-02-23 09:30:24 -07:00
appHost.RunStartupTasksAsync(CancellationToken.None).GetAwaiter().GetResult();
return testServer;
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
foreach (var disposable in _disposableComponents)
{
disposable.Dispose();
}
_disposableComponents.Clear();
base.Dispose(disposing);
}
}
}