JellyfinApplicationFactory.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.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()
  32. .WriteTo.Console(formatProvider: CultureInfo.InvariantCulture)
  33. .CreateLogger();
  34. Program.PerformStaticInitialization();
  35. }
  36. /// <inheritdoc/>
  37. protected override IWebHostBuilder CreateWebHostBuilder()
  38. {
  39. return new WebHostBuilder();
  40. }
  41. /// <inheritdoc/>
  42. protected override void ConfigureWebHost(IWebHostBuilder builder)
  43. {
  44. // Specify the startup command line options
  45. var commandLineOpts = new StartupOptions();
  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. _disposableComponents.Add(loggerFactory);
  65. // Create the app host and initialize it
  66. var appHost = new TestAppHost(
  67. appPaths,
  68. loggerFactory,
  69. commandLineOpts,
  70. startupConfig);
  71. _disposableComponents.Add(appHost);
  72. builder.ConfigureServices(services => appHost.Init(services))
  73. .ConfigureWebHostBuilder(appHost, startupConfig, appPaths)
  74. .ConfigureAppConfiguration((context, builder) =>
  75. {
  76. builder
  77. .SetBasePath(appPaths.ConfigurationDirectoryPath)
  78. .AddInMemoryCollection(ConfigurationOptions.DefaultConfiguration)
  79. .AddEnvironmentVariables("JELLYFIN_")
  80. .AddInMemoryCollection(commandLineOpts.ConvertToConfig());
  81. });
  82. }
  83. /// <inheritdoc/>
  84. protected override TestServer CreateServer(IWebHostBuilder builder)
  85. {
  86. // Create the test server using the base implementation
  87. var testServer = base.CreateServer(builder);
  88. // Finish initializing the app host
  89. var appHost = (TestAppHost)testServer.Services.GetRequiredService<IApplicationHost>();
  90. appHost.ServiceProvider = testServer.Services;
  91. appHost.InitializeServices().GetAwaiter().GetResult();
  92. appHost.RunStartupTasksAsync(CancellationToken.None).GetAwaiter().GetResult();
  93. return testServer;
  94. }
  95. /// <inheritdoc/>
  96. protected override void Dispose(bool disposing)
  97. {
  98. foreach (var disposable in _disposableComponents)
  99. {
  100. disposable.Dispose();
  101. }
  102. _disposableComponents.Clear();
  103. base.Dispose(disposing);
  104. }
  105. }
  106. }