JellyfinApplicationFactory.cs 4.7 KB

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