2020-05-03 19:31:17 +00:00
|
|
|
using System;
|
2020-04-20 19:39:55 +00:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.IO;
|
2021-02-23 16:30:24 +00:00
|
|
|
using System.Threading;
|
2020-04-20 19:39:55 +00:00
|
|
|
using Emby.Server.Implementations;
|
|
|
|
using Emby.Server.Implementations.IO;
|
|
|
|
using MediaBrowser.Common;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
|
|
using Microsoft.AspNetCore.TestHost;
|
2021-02-27 20:12:55 +00:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2020-04-20 19:39:55 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Serilog;
|
|
|
|
using Serilog.Extensions.Logging;
|
|
|
|
|
2021-03-01 18:35:58 +00:00
|
|
|
namespace Jellyfin.Server.Integration.Tests
|
2020-04-20 19:39:55 +00:00
|
|
|
{
|
|
|
|
/// <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 16:30:24 +00:00
|
|
|
private readonly ConcurrentBag<IDisposable> _disposableComponents = new ConcurrentBag<IDisposable>();
|
2020-04-20 19:39:55 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2021-03-05 11:34:22 +00:00
|
|
|
/// Initializes static members of the <see cref="JellyfinApplicationFactory"/> class.
|
2020-04-20 19:39:55 +00:00
|
|
|
/// </summary>
|
2021-03-05 11:34:22 +00:00
|
|
|
static JellyfinApplicationFactory()
|
2020-04-20 19:39:55 +00:00
|
|
|
{
|
|
|
|
// Perform static initialization that only needs to happen once per test-run
|
|
|
|
Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateLogger();
|
|
|
|
Program.PerformStaticInitialization();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
protected override IWebHostBuilder CreateWebHostBuilder()
|
|
|
|
{
|
|
|
|
return new WebHostBuilder();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
|
|
|
{
|
|
|
|
// Specify the startup command line options
|
2021-07-05 23:07:10 +00:00
|
|
|
var commandLineOpts = new StartupOptions();
|
2020-04-20 19:39:55 +00:00
|
|
|
|
|
|
|
// 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
|
2020-04-25 22:36:09 +00:00
|
|
|
Program.InitLoggingConfigFile(appPaths).GetAwaiter().GetResult();
|
2020-04-20 19:39:55 +00:00
|
|
|
|
|
|
|
// Create a copy of the application configuration to use for startup
|
|
|
|
var startupConfig = Program.CreateAppConfiguration(commandLineOpts, appPaths);
|
|
|
|
|
|
|
|
ILoggerFactory loggerFactory = new SerilogLoggerFactory();
|
2020-08-16 21:25:14 +00:00
|
|
|
var serviceCollection = new ServiceCollection();
|
2020-05-03 19:31:17 +00:00
|
|
|
_disposableComponents.Add(loggerFactory);
|
|
|
|
|
|
|
|
// Create the app host and initialize it
|
2021-02-22 12:15:29 +00:00
|
|
|
var appHost = new TestAppHost(
|
2020-04-20 19:39:55 +00:00
|
|
|
appPaths,
|
|
|
|
loggerFactory,
|
|
|
|
commandLineOpts,
|
2021-02-27 20:12:55 +00:00
|
|
|
new ConfigurationBuilder().Build(),
|
2020-04-20 19:39:55 +00:00
|
|
|
new ManagedFileSystem(loggerFactory.CreateLogger<ManagedFileSystem>(), appPaths),
|
2020-08-16 21:25:14 +00:00
|
|
|
serviceCollection);
|
2020-05-03 19:31:17 +00:00
|
|
|
_disposableComponents.Add(appHost);
|
2020-08-16 21:25:14 +00:00
|
|
|
appHost.Init();
|
2020-04-20 19:39:55 +00:00
|
|
|
|
|
|
|
// Configure the web host builder
|
|
|
|
Program.ConfigureWebHostBuilder(builder, appHost, serviceCollection, commandLineOpts, startupConfig, appPaths);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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 12:15:29 +00:00
|
|
|
var appHost = (TestAppHost)testServer.Services.GetRequiredService<IApplicationHost>();
|
2020-04-20 19:39:55 +00:00
|
|
|
appHost.ServiceProvider = testServer.Services;
|
2020-04-25 22:36:09 +00:00
|
|
|
appHost.InitializeServices().GetAwaiter().GetResult();
|
2021-02-23 16:30:24 +00:00
|
|
|
appHost.RunStartupTasksAsync(CancellationToken.None).GetAwaiter().GetResult();
|
2020-04-20 19:39:55 +00:00
|
|
|
|
|
|
|
return testServer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
{
|
2020-05-03 19:31:17 +00:00
|
|
|
foreach (var disposable in _disposableComponents)
|
2020-04-20 19:39:55 +00:00
|
|
|
{
|
2020-05-03 19:31:17 +00:00
|
|
|
disposable.Dispose();
|
2020-04-20 19:39:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 19:31:17 +00:00
|
|
|
_disposableComponents.Clear();
|
2020-04-20 19:39:55 +00:00
|
|
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|