JellyfinApplicationFactory.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 Jellyfin.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. var serviceCollection = new ServiceCollection();
  68. _disposableComponents.Add(loggerFactory);
  69. // Create the app host and initialize it
  70. var appHost = new CoreAppHost(
  71. appPaths,
  72. loggerFactory,
  73. commandLineOpts,
  74. new ManagedFileSystem(loggerFactory.CreateLogger<ManagedFileSystem>(), appPaths),
  75. new NetworkManager(loggerFactory.CreateLogger<NetworkManager>()),
  76. serviceCollection);
  77. _disposableComponents.Add(appHost);
  78. appHost.Init();
  79. // Configure the web host builder
  80. Program.ConfigureWebHostBuilder(builder, appHost, serviceCollection, commandLineOpts, startupConfig, appPaths);
  81. }
  82. /// <inheritdoc/>
  83. protected override TestServer CreateServer(IWebHostBuilder builder)
  84. {
  85. // Create the test server using the base implementation
  86. var testServer = base.CreateServer(builder);
  87. // Finish initializing the app host
  88. var appHost = (CoreAppHost)testServer.Services.GetRequiredService<IApplicationHost>();
  89. appHost.ServiceProvider = testServer.Services;
  90. appHost.InitializeServices().GetAwaiter().GetResult();
  91. appHost.RunStartupTasksAsync().GetAwaiter().GetResult();
  92. return testServer;
  93. }
  94. /// <inheritdoc/>
  95. protected override void Dispose(bool disposing)
  96. {
  97. foreach (var disposable in _disposableComponents)
  98. {
  99. disposable.Dispose();
  100. }
  101. _disposableComponents.Clear();
  102. base.Dispose(disposing);
  103. }
  104. }
  105. }