JellyfinApplicationFactory.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 Moq;
  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 IHostBuilder CreateHostBuilder()
  41. {
  42. return new HostBuilder();
  43. }
  44. /// <inheritdoc/>
  45. protected override void ConfigureWebHost(IWebHostBuilder builder)
  46. {
  47. // Skip ffmpeg check for testing
  48. Environment.SetEnvironmentVariable("JELLYFIN_FFMPEG__NOVALIDATION", "true");
  49. // Specify the startup command line options
  50. var commandLineOpts = new StartupOptions();
  51. // Use a temporary directory for the application paths
  52. var webHostPathRoot = Path.Combine(_testPathRoot, "test-host-" + Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
  53. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "logs"));
  54. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "config"));
  55. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "cache"));
  56. Directory.CreateDirectory(Path.Combine(webHostPathRoot, "jellyfin-web"));
  57. var appPaths = new ServerApplicationPaths(
  58. webHostPathRoot,
  59. Path.Combine(webHostPathRoot, "logs"),
  60. Path.Combine(webHostPathRoot, "config"),
  61. Path.Combine(webHostPathRoot, "cache"),
  62. Path.Combine(webHostPathRoot, "jellyfin-web"));
  63. // Create the logging config file
  64. // TODO: We shouldn't need to do this since we are only logging to console
  65. StartupHelpers.InitLoggingConfigFile(appPaths).GetAwaiter().GetResult();
  66. // Create a copy of the application configuration to use for startup
  67. var startupConfig = Program.CreateAppConfiguration(commandLineOpts, appPaths);
  68. ILoggerFactory loggerFactory = new SerilogLoggerFactory();
  69. _disposableComponents.Add(loggerFactory);
  70. // Create the app host and initialize it
  71. var appHost = new TestAppHost(
  72. appPaths,
  73. loggerFactory,
  74. commandLineOpts,
  75. startupConfig);
  76. _disposableComponents.Add(appHost);
  77. builder.ConfigureServices(services => appHost.Init(services))
  78. .ConfigureWebHostBuilder(appHost, startupConfig, appPaths, NullLogger.Instance)
  79. .ConfigureAppConfiguration((context, builder) =>
  80. {
  81. builder
  82. .SetBasePath(appPaths.ConfigurationDirectoryPath)
  83. .AddInMemoryCollection(ConfigurationOptions.DefaultConfiguration)
  84. .AddEnvironmentVariables("JELLYFIN_")
  85. .AddInMemoryCollection(commandLineOpts.ConvertToConfig());
  86. });
  87. }
  88. /// <inheritdoc/>
  89. protected override IHost CreateHost(IHostBuilder builder)
  90. {
  91. var host = builder.Build();
  92. var appHost = (TestAppHost)host.Services.GetRequiredService<IApplicationHost>();
  93. appHost.ServiceProvider = host.Services;
  94. appHost.InitializeServices(Mock.Of<IConfiguration>()).GetAwaiter().GetResult();
  95. host.Start();
  96. appHost.RunStartupTasksAsync().GetAwaiter().GetResult();
  97. return host;
  98. }
  99. /// <inheritdoc/>
  100. protected override void Dispose(bool disposing)
  101. {
  102. foreach (var disposable in _disposableComponents)
  103. {
  104. disposable.Dispose();
  105. }
  106. _disposableComponents.Clear();
  107. base.Dispose(disposing);
  108. }
  109. }
  110. }