2
0

JellyfinApplicationFactory.cs 4.6 KB

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