JellyfinApplicationFactory.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 Jellyfin.Server;
  7. using MediaBrowser.Common;
  8. using Microsoft.AspNetCore.Hosting;
  9. using Microsoft.AspNetCore.Mvc.Testing;
  10. using Microsoft.AspNetCore.TestHost;
  11. using Microsoft.Extensions.DependencyInjection;
  12. using Microsoft.Extensions.Logging;
  13. using Serilog;
  14. using Serilog.Extensions.Logging;
  15. namespace Jellyfin.Api.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 static readonly ConcurrentBag<IDisposable> _disposableComponents = new ConcurrentBag<IDisposable>();
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="JellyfinApplicationFactory"/> class.
  26. /// </summary>
  27. public 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. {
  44. NoWebClient = true
  45. };
  46. // Use a temporary directory for the application paths
  47. var webHostPathRoot = Path.Combine(_testPathRoot, "test-host-" + Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
  48. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "logs"));
  49. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "config"));
  50. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "cache"));
  51. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "jellyfin-web"));
  52. var appPaths = new ServerApplicationPaths(
  53. webHostPathRoot,
  54. Path.Combine(webHostPathRoot, "logs"),
  55. Path.Combine(webHostPathRoot, "config"),
  56. Path.Combine(webHostPathRoot, "cache"),
  57. Path.Combine(webHostPathRoot, "jellyfin-web"));
  58. // Create the logging config file
  59. // TODO: We shouldn't need to do this since we are only logging to console
  60. Program.InitLoggingConfigFile(appPaths).GetAwaiter().GetResult();
  61. // Create a copy of the application configuration to use for startup
  62. var startupConfig = Program.CreateAppConfiguration(commandLineOpts, appPaths);
  63. ILoggerFactory loggerFactory = new SerilogLoggerFactory();
  64. var serviceCollection = new ServiceCollection();
  65. _disposableComponents.Add(loggerFactory);
  66. // Create the app host and initialize it
  67. var appHost = new CoreAppHost(
  68. appPaths,
  69. loggerFactory,
  70. commandLineOpts,
  71. new ManagedFileSystem(loggerFactory.CreateLogger<ManagedFileSystem>(), appPaths),
  72. serviceCollection);
  73. _disposableComponents.Add(appHost);
  74. appHost.Init();
  75. // Configure the web host builder
  76. Program.ConfigureWebHostBuilder(builder, appHost, serviceCollection, commandLineOpts, startupConfig, appPaths);
  77. }
  78. /// <inheritdoc/>
  79. protected override TestServer CreateServer(IWebHostBuilder builder)
  80. {
  81. // Create the test server using the base implementation
  82. var testServer = base.CreateServer(builder);
  83. // Finish initializing the app host
  84. var appHost = (CoreAppHost)testServer.Services.GetRequiredService<IApplicationHost>();
  85. appHost.ServiceProvider = testServer.Services;
  86. appHost.InitializeServices().GetAwaiter().GetResult();
  87. appHost.RunStartupTasksAsync().GetAwaiter().GetResult();
  88. return testServer;
  89. }
  90. /// <inheritdoc/>
  91. protected override void Dispose(bool disposing)
  92. {
  93. foreach (var disposable in _disposableComponents)
  94. {
  95. disposable.Dispose();
  96. }
  97. _disposableComponents.Clear();
  98. base.Dispose(disposing);
  99. }
  100. }
  101. }