JellyfinApplicationFactory.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.IO;
  4. using System.Threading;
  5. using Emby.Server.Implementations;
  6. using Emby.Server.Implementations.IO;
  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.Server.Integration.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 readonly ConcurrentBag<IDisposable> _disposableComponents = new ConcurrentBag<IDisposable>();
  25. /// <summary>
  26. /// Initializes static members of the <see cref="JellyfinApplicationFactory"/> class.
  27. /// </summary>
  28. static 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. // Use a temporary directory for the application paths
  45. var webHostPathRoot = Path.Combine(_testPathRoot, "test-host-" + Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
  46. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "logs"));
  47. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "config"));
  48. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "cache"));
  49. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "jellyfin-web"));
  50. var appPaths = new ServerApplicationPaths(
  51. webHostPathRoot,
  52. Path.Combine(webHostPathRoot, "logs"),
  53. Path.Combine(webHostPathRoot, "config"),
  54. Path.Combine(webHostPathRoot, "cache"),
  55. Path.Combine(webHostPathRoot, "jellyfin-web"));
  56. // Create the logging config file
  57. // TODO: We shouldn't need to do this since we are only logging to console
  58. Program.InitLoggingConfigFile(appPaths).GetAwaiter().GetResult();
  59. // Create a copy of the application configuration to use for startup
  60. var startupConfig = Program.CreateAppConfiguration(commandLineOpts, appPaths);
  61. ILoggerFactory loggerFactory = new SerilogLoggerFactory();
  62. var serviceCollection = new ServiceCollection();
  63. _disposableComponents.Add(loggerFactory);
  64. // Create the app host and initialize it
  65. var appHost = new TestAppHost(
  66. appPaths,
  67. loggerFactory,
  68. commandLineOpts,
  69. new ConfigurationBuilder().Build(),
  70. new ManagedFileSystem(loggerFactory.CreateLogger<ManagedFileSystem>(), appPaths),
  71. serviceCollection);
  72. _disposableComponents.Add(appHost);
  73. appHost.Init();
  74. // Configure the web host builder
  75. Program.ConfigureWebHostBuilder(builder, appHost, serviceCollection, commandLineOpts, startupConfig, appPaths);
  76. }
  77. /// <inheritdoc/>
  78. protected override TestServer CreateServer(IWebHostBuilder builder)
  79. {
  80. // Create the test server using the base implementation
  81. var testServer = base.CreateServer(builder);
  82. // Finish initializing the app host
  83. var appHost = (TestAppHost)testServer.Services.GetRequiredService<IApplicationHost>();
  84. appHost.ServiceProvider = testServer.Services;
  85. appHost.InitializeServices().GetAwaiter().GetResult();
  86. appHost.RunStartupTasksAsync(CancellationToken.None).GetAwaiter().GetResult();
  87. return testServer;
  88. }
  89. /// <inheritdoc/>
  90. protected override void Dispose(bool disposing)
  91. {
  92. foreach (var disposable in _disposableComponents)
  93. {
  94. disposable.Dispose();
  95. }
  96. _disposableComponents.Clear();
  97. base.Dispose(disposing);
  98. }
  99. }
  100. }