JellyfinApplicationFactory.cs 4.8 KB

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