JellyfinApplicationFactory.cs 4.5 KB

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