InfrastructureFixture.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. var containerLogger = _loggerFactory.CreateLogger("testcontainers");
  30. _sshServerImage = new ImageFromDockerfileBuilder()
  31. .WithName("renci-ssh-tests-server-image")
  32. .WithDockerfileDirectory(CommonDirectoryPath.GetSolutionDirectory(), Path.Combine("test", "Renci.SshNet.IntegrationTests"))
  33. .WithDockerfile("Dockerfile")
  34. .WithDeleteIfExists(true)
  35. .WithLogger(containerLogger)
  36. .Build();
  37. await _sshServerImage.CreateAsync();
  38. _sshServer = new ContainerBuilder()
  39. .WithHostname("renci-ssh-tests-server")
  40. .WithImage(_sshServerImage)
  41. .WithPortBinding(22, true)
  42. .WithLogger(containerLogger)
  43. .Build();
  44. await _sshServer.StartAsync();
  45. SshServerPort = _sshServer.GetMappedPublicPort(22);
  46. SshServerHostName = _sshServer.Hostname;
  47. // Socket fails on Linux, reporting inability early. This is the Linux behavior by design.
  48. // https://github.com/dotnet/runtime/issues/47484#issuecomment-769239699
  49. // At this point we have to wait until the ssh server in the container is available
  50. if (Environment.OSVersion.Platform == PlatformID.Unix)
  51. {
  52. await Task.Delay(300);
  53. }
  54. }
  55. public async Task DisposeAsync()
  56. {
  57. if (_sshServer != null)
  58. {
  59. #pragma warning disable S6966 // Awaitable method should be used
  60. //try
  61. //{
  62. // File.WriteAllBytes(@"C:\tmp\auth.log", await _sshServer.ReadFileAsync("/var/log/auth.log").ConfigureAwait(false));
  63. //}
  64. //catch (Exception ex)
  65. //{
  66. // Console.Error.WriteLine(ex.ToString());
  67. //}
  68. #pragma warning restore S6966 // Awaitable method should be used
  69. await _sshServer.DisposeAsync();
  70. }
  71. if (_sshServerImage != null)
  72. {
  73. await _sshServerImage.DisposeAsync();
  74. }
  75. }
  76. public void Dispose()
  77. {
  78. }
  79. }
  80. }