2
0

JellyfinApplicationFactory.cs 5.0 KB

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