JellyfinApplicationFactory.cs 5.1 KB

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