JellyfinApplicationFactory.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.IO;
  4. using System.Threading;
  5. using Emby.Server.Implementations;
  6. using MediaBrowser.Common;
  7. using Microsoft.AspNetCore.Hosting;
  8. using Microsoft.AspNetCore.Mvc.Testing;
  9. using Microsoft.AspNetCore.TestHost;
  10. using Microsoft.Extensions.DependencyInjection;
  11. using Microsoft.Extensions.Logging;
  12. using Serilog;
  13. using Serilog.Extensions.Logging;
  14. using static MediaBrowser.Controller.Extensions.ConfigurationExtensions;
  15. namespace Jellyfin.Server.Integration.Tests
  16. {
  17. /// <summary>
  18. /// Factory for bootstrapping the Jellyfin application in memory for functional end to end tests.
  19. /// </summary>
  20. public class JellyfinApplicationFactory : WebApplicationFactory<Startup>
  21. {
  22. private static readonly string _testPathRoot = Path.Combine(Path.GetTempPath(), "jellyfin-test-data");
  23. private readonly ConcurrentBag<IDisposable> _disposableComponents = new ConcurrentBag<IDisposable>();
  24. /// <summary>
  25. /// Initializes static members of the <see cref="JellyfinApplicationFactory"/> class.
  26. /// </summary>
  27. static JellyfinApplicationFactory()
  28. {
  29. // Perform static initialization that only needs to happen once per test-run
  30. Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateLogger();
  31. Program.PerformStaticInitialization();
  32. }
  33. /// <inheritdoc/>
  34. protected override IWebHostBuilder CreateWebHostBuilder()
  35. {
  36. return new WebHostBuilder();
  37. }
  38. /// <inheritdoc/>
  39. protected override void ConfigureWebHost(IWebHostBuilder builder)
  40. {
  41. // Specify the startup command line options
  42. var commandLineOpts = new StartupOptions();
  43. // Use a temporary directory for the application paths
  44. var webHostPathRoot = Path.Combine(_testPathRoot, "test-host-" + Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
  45. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "logs"));
  46. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "config"));
  47. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "cache"));
  48. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "jellyfin-web"));
  49. var appPaths = new ServerApplicationPaths(
  50. webHostPathRoot,
  51. Path.Combine(webHostPathRoot, "logs"),
  52. Path.Combine(webHostPathRoot, "config"),
  53. Path.Combine(webHostPathRoot, "cache"),
  54. Path.Combine(webHostPathRoot, "jellyfin-web"));
  55. // Create the logging config file
  56. // TODO: We shouldn't need to do this since we are only logging to console
  57. Program.InitLoggingConfigFile(appPaths).GetAwaiter().GetResult();
  58. // Create a copy of the application configuration to use for startup
  59. var startupConfig = Program.CreateAppConfiguration(commandLineOpts, appPaths);
  60. ILoggerFactory loggerFactory = new SerilogLoggerFactory();
  61. _disposableComponents.Add(loggerFactory);
  62. // Create the app host and initialize it
  63. var appHost = new TestAppHost(
  64. appPaths,
  65. loggerFactory,
  66. commandLineOpts,
  67. startupConfig);
  68. _disposableComponents.Add(appHost);
  69. var serviceCollection = new ServiceCollection();
  70. appHost.Init(serviceCollection);
  71. // Configure the web host builder
  72. Program.ConfigureWebHostBuilder(builder, appHost, serviceCollection, commandLineOpts, startupConfig, appPaths);
  73. }
  74. /// <inheritdoc/>
  75. protected override TestServer CreateServer(IWebHostBuilder builder)
  76. {
  77. // Create the test server using the base implementation
  78. var testServer = base.CreateServer(builder);
  79. // Finish initializing the app host
  80. var appHost = (TestAppHost)testServer.Services.GetRequiredService<IApplicationHost>();
  81. appHost.ServiceProvider = testServer.Services;
  82. appHost.InitializeServices().GetAwaiter().GetResult();
  83. appHost.RunStartupTasksAsync(CancellationToken.None).GetAwaiter().GetResult();
  84. return testServer;
  85. }
  86. /// <inheritdoc/>
  87. protected override void Dispose(bool disposing)
  88. {
  89. foreach (var disposable in _disposableComponents)
  90. {
  91. disposable.Dispose();
  92. }
  93. _disposableComponents.Clear();
  94. base.Dispose(disposing);
  95. }
  96. }
  97. }