JellyfinApplicationFactory.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Globalization;
  4. using System.IO;
  5. using Emby.Server.Implementations;
  6. using Jellyfin.Server.Extensions;
  7. using Jellyfin.Server.Helpers;
  8. using MediaBrowser.Common;
  9. using MediaBrowser.Common.Configuration;
  10. using Microsoft.AspNetCore.Hosting;
  11. using Microsoft.AspNetCore.Mvc.Testing;
  12. using Microsoft.Extensions.Configuration;
  13. using Microsoft.Extensions.DependencyInjection;
  14. using Microsoft.Extensions.Hosting;
  15. using Microsoft.Extensions.Logging;
  16. using Microsoft.Extensions.Logging.Abstractions;
  17. using Moq;
  18. using Serilog;
  19. using Serilog.Extensions.Logging;
  20. namespace Jellyfin.Server.Integration.Tests
  21. {
  22. /// <summary>
  23. /// Factory for bootstrapping the Jellyfin application in memory for functional end to end tests.
  24. /// </summary>
  25. public class JellyfinApplicationFactory : WebApplicationFactory<Startup>
  26. {
  27. private static readonly string _testPathRoot = Path.Combine(Path.GetTempPath(), "jellyfin-test-data");
  28. private readonly ConcurrentBag<IDisposable> _disposableComponents = new ConcurrentBag<IDisposable>();
  29. /// <summary>
  30. /// Initializes static members of the <see cref="JellyfinApplicationFactory"/> class.
  31. /// </summary>
  32. static JellyfinApplicationFactory()
  33. {
  34. // Perform static initialization that only needs to happen once per test-run
  35. Log.Logger = new LoggerConfiguration()
  36. .WriteTo.Console(formatProvider: CultureInfo.InvariantCulture)
  37. .CreateLogger();
  38. StartupHelpers.PerformStaticInitialization();
  39. }
  40. /// <inheritdoc/>
  41. protected override IHostBuilder CreateHostBuilder()
  42. {
  43. return new HostBuilder();
  44. }
  45. /// <inheritdoc/>
  46. protected override void ConfigureWebHost(IWebHostBuilder builder)
  47. {
  48. // Skip ffmpeg check for testing
  49. Environment.SetEnvironmentVariable("JELLYFIN_FFMPEG__NOVALIDATION", "true");
  50. // Specify the startup command line options
  51. var commandLineOpts = new StartupOptions();
  52. // Use a temporary directory for the application paths
  53. var webHostPathRoot = Path.Combine(_testPathRoot, "test-host-" + Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
  54. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "logs"));
  55. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "config"));
  56. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "cache"));
  57. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "jellyfin-web"));
  58. var appPaths = new ServerApplicationPaths(
  59. webHostPathRoot,
  60. Path.Combine(webHostPathRoot, "logs"),
  61. Path.Combine(webHostPathRoot, "config"),
  62. Path.Combine(webHostPathRoot, "cache"),
  63. Path.Combine(webHostPathRoot, "jellyfin-web"));
  64. // Create the logging config file
  65. // TODO: We shouldn't need to do this since we are only logging to console
  66. StartupHelpers.InitLoggingConfigFile(appPaths).GetAwaiter().GetResult();
  67. // Create a copy of the application configuration to use for startup
  68. var startupConfig = Program.CreateAppConfiguration(commandLineOpts, appPaths);
  69. ILoggerFactory loggerFactory = new SerilogLoggerFactory();
  70. _disposableComponents.Add(loggerFactory);
  71. // Create the app host and initialize it
  72. var appHost = new TestAppHost(
  73. appPaths,
  74. loggerFactory,
  75. commandLineOpts,
  76. startupConfig);
  77. _disposableComponents.Add(appHost);
  78. builder.ConfigureServices(services => appHost.Init(services))
  79. .ConfigureWebHostBuilder(appHost, startupConfig, appPaths, NullLogger.Instance)
  80. .ConfigureAppConfiguration((context, builder) =>
  81. {
  82. builder
  83. .SetBasePath(appPaths.ConfigurationDirectoryPath)
  84. .AddInMemoryCollection(ConfigurationOptions.DefaultConfiguration)
  85. .AddEnvironmentVariables("JELLYFIN_")
  86. .AddInMemoryCollection(commandLineOpts.ConvertToConfig());
  87. });
  88. }
  89. /// <inheritdoc/>
  90. protected override IHost CreateHost(IHostBuilder builder)
  91. {
  92. var host = builder.Build();
  93. var appHost = (TestAppHost)host.Services.GetRequiredService<IApplicationHost>();
  94. appHost.ServiceProvider = host.Services;
  95. var applicationPaths = appHost.ServiceProvider.GetRequiredService<IApplicationPaths>();
  96. Program.ApplyStartupMigrationAsync((ServerApplicationPaths)applicationPaths, appHost.ServiceProvider.GetRequiredService<IConfiguration>()).GetAwaiter().GetResult();
  97. Program.ApplyCoreMigrationsAsync(appHost.ServiceProvider, Migrations.Stages.JellyfinMigrationStageTypes.CoreInitialisaition).GetAwaiter().GetResult();
  98. appHost.InitializeServices(Mock.Of<IConfiguration>()).GetAwaiter().GetResult();
  99. Program.ApplyCoreMigrationsAsync(appHost.ServiceProvider, Migrations.Stages.JellyfinMigrationStageTypes.AppInitialisation).GetAwaiter().GetResult();
  100. host.Start();
  101. appHost.RunStartupTasksAsync().GetAwaiter().GetResult();
  102. return host;
  103. }
  104. /// <inheritdoc/>
  105. protected override void Dispose(bool disposing)
  106. {
  107. foreach (var disposable in _disposableComponents)
  108. {
  109. disposable.Dispose();
  110. }
  111. _disposableComponents.Clear();
  112. base.Dispose(disposing);
  113. }
  114. }
  115. }