2
0

InfrastructureFixture.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using DotNet.Testcontainers.Builders;
  2. using DotNet.Testcontainers.Containers;
  3. using DotNet.Testcontainers.Images;
  4. using Microsoft.Extensions.Logging;
  5. namespace Renci.SshNet.IntegrationTests.TestsFixtures
  6. {
  7. public sealed class InfrastructureFixture : IDisposable
  8. {
  9. private InfrastructureFixture()
  10. {
  11. _loggerFactory = LoggerFactory.Create(builder =>
  12. {
  13. builder.SetMinimumLevel(LogLevel.Debug);
  14. builder.AddFilter("testcontainers", LogLevel.Information);
  15. builder.AddConsole();
  16. });
  17. SshNetLoggingConfiguration.InitializeLogging(_loggerFactory);
  18. }
  19. public static InfrastructureFixture Instance { get; } = new InfrastructureFixture();
  20. private readonly ILoggerFactory _loggerFactory;
  21. private IContainer _sshServer;
  22. private IFutureDockerImage _sshServerImage;
  23. public string SshServerHostName { get; set; }
  24. public ushort SshServerPort { get; set; }
  25. public SshUser AdminUser = new SshUser("sshnetadm", "ssh4ever");
  26. public SshUser User = new SshUser("sshnet", "ssh4ever");
  27. public async Task InitializeAsync()
  28. {
  29. // for the .NET Framework Tests in CI, the Container is set up in WSL2 with Podman
  30. #if NETFRAMEWORK
  31. if (Environment.GetEnvironmentVariable("CI") == "true")
  32. {
  33. SshServerPort = 2222;
  34. SshServerHostName = "localhost";
  35. await Task.Delay(1_000);
  36. return;
  37. }
  38. #endif
  39. var containerLogger = _loggerFactory.CreateLogger("testcontainers");
  40. _sshServerImage = new ImageFromDockerfileBuilder()
  41. .WithName("renci-ssh-tests-server-image")
  42. .WithDockerfileDirectory(CommonDirectoryPath.GetSolutionDirectory(), Path.Combine("test", "Renci.SshNet.IntegrationTests"))
  43. .WithDockerfile("Dockerfile")
  44. .WithDeleteIfExists(true)
  45. .WithLogger(containerLogger)
  46. .Build();
  47. await _sshServerImage.CreateAsync();
  48. _sshServer = new ContainerBuilder()
  49. .WithHostname("renci-ssh-tests-server")
  50. .WithImage(_sshServerImage)
  51. .WithPortBinding(22, true)
  52. .WithLogger(containerLogger)
  53. .Build();
  54. await _sshServer.StartAsync();
  55. SshServerPort = _sshServer.GetMappedPublicPort(22);
  56. SshServerHostName = _sshServer.Hostname;
  57. // Socket fails on Linux, reporting inability early. This is the Linux behavior by design.
  58. // https://github.com/dotnet/runtime/issues/47484#issuecomment-769239699
  59. // At this point we have to wait until the ssh server in the container is available
  60. if (Environment.OSVersion.Platform == PlatformID.Unix)
  61. {
  62. await Task.Delay(300);
  63. }
  64. }
  65. public async Task DisposeAsync()
  66. {
  67. if (_sshServer != null)
  68. {
  69. #pragma warning disable S6966 // Awaitable method should be used
  70. //try
  71. //{
  72. // File.WriteAllBytes(@"C:\tmp\auth.log", await _sshServer.ReadFileAsync("/var/log/auth.log").ConfigureAwait(false));
  73. //}
  74. //catch (Exception ex)
  75. //{
  76. // Console.Error.WriteLine(ex.ToString());
  77. //}
  78. #pragma warning restore S6966 // Awaitable method should be used
  79. await _sshServer.DisposeAsync();
  80. }
  81. if (_sshServerImage != null)
  82. {
  83. await _sshServerImage.DisposeAsync();
  84. }
  85. }
  86. public void Dispose()
  87. {
  88. }
  89. }
  90. }