You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
4.8 KiB
121 lines
4.8 KiB
5 years ago
|
using System;
|
||
5 years ago
|
using System.Collections.Concurrent;
|
||
|
using System.IO;
|
||
4 years ago
|
using System.Threading;
|
||
5 years ago
|
using Emby.Server.Implementations;
|
||
|
using Emby.Server.Implementations.IO;
|
||
|
using Jellyfin.Server;
|
||
|
using MediaBrowser.Common;
|
||
|
using Microsoft.AspNetCore.Hosting;
|
||
|
using Microsoft.AspNetCore.Mvc.Testing;
|
||
|
using Microsoft.AspNetCore.TestHost;
|
||
4 years ago
|
using Microsoft.Extensions.Configuration;
|
||
5 years ago
|
using Microsoft.Extensions.DependencyInjection;
|
||
|
using Microsoft.Extensions.Logging;
|
||
|
using Serilog;
|
||
|
using Serilog.Extensions.Logging;
|
||
|
|
||
4 years ago
|
namespace Jellyfin.Server.Integration.Tests
|
||
5 years ago
|
{
|
||
|
/// <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");
|
||
4 years ago
|
private readonly ConcurrentBag<IDisposable> _disposableComponents = new ConcurrentBag<IDisposable>();
|
||
5 years ago
|
|
||
|
/// <summary>
|
||
5 years ago
|
/// Initializes a new instance of the <see cref="JellyfinApplicationFactory"/> class.
|
||
5 years ago
|
/// </summary>
|
||
|
public JellyfinApplicationFactory()
|
||
|
{
|
||
|
// 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
|
||
|
var commandLineOpts = new StartupOptions
|
||
|
{
|
||
4 years ago
|
NoWebClient = true
|
||
5 years ago
|
};
|
||
|
|
||
|
// 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
|
||
5 years ago
|
Program.InitLoggingConfigFile(appPaths).GetAwaiter().GetResult();
|
||
5 years ago
|
|
||
|
// Create a copy of the application configuration to use for startup
|
||
|
var startupConfig = Program.CreateAppConfiguration(commandLineOpts, appPaths);
|
||
|
|
||
|
ILoggerFactory loggerFactory = new SerilogLoggerFactory();
|
||
4 years ago
|
var serviceCollection = new ServiceCollection();
|
||
5 years ago
|
_disposableComponents.Add(loggerFactory);
|
||
|
|
||
|
// Create the app host and initialize it
|
||
4 years ago
|
var appHost = new TestAppHost(
|
||
5 years ago
|
appPaths,
|
||
|
loggerFactory,
|
||
|
commandLineOpts,
|
||
4 years ago
|
new ConfigurationBuilder().Build(),
|
||
5 years ago
|
new ManagedFileSystem(loggerFactory.CreateLogger<ManagedFileSystem>(), appPaths),
|
||
4 years ago
|
serviceCollection);
|
||
5 years ago
|
_disposableComponents.Add(appHost);
|
||
4 years ago
|
appHost.Init();
|
||
5 years ago
|
|
||
|
// 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
|
||
4 years ago
|
var appHost = (TestAppHost)testServer.Services.GetRequiredService<IApplicationHost>();
|
||
5 years ago
|
appHost.ServiceProvider = testServer.Services;
|
||
5 years ago
|
appHost.InitializeServices().GetAwaiter().GetResult();
|
||
4 years ago
|
appHost.RunStartupTasksAsync(CancellationToken.None).GetAwaiter().GetResult();
|
||
5 years ago
|
|
||
|
return testServer;
|
||
|
}
|
||
|
|
||
|
/// <inheritdoc/>
|
||
|
protected override void Dispose(bool disposing)
|
||
|
{
|
||
5 years ago
|
foreach (var disposable in _disposableComponents)
|
||
5 years ago
|
{
|
||
5 years ago
|
disposable.Dispose();
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
_disposableComponents.Clear();
|
||
5 years ago
|
|
||
|
base.Dispose(disposing);
|
||
|
}
|
||
|
}
|
||
|
}
|