InfrastructureFixture.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using DotNet.Testcontainers.Builders;
  2. using DotNet.Testcontainers.Containers;
  3. using DotNet.Testcontainers.Images;
  4. #if !NET && !NETSTANDARD2_1_OR_GREATER
  5. using Renci.SshNet.Abstractions;
  6. #endif
  7. namespace Renci.SshNet.IntegrationTests.TestsFixtures
  8. {
  9. public sealed class InfrastructureFixture : IDisposable
  10. {
  11. private InfrastructureFixture()
  12. {
  13. }
  14. private static readonly Lazy<InfrastructureFixture> InstanceLazy = new Lazy<InfrastructureFixture>(() => new InfrastructureFixture());
  15. public static InfrastructureFixture Instance
  16. {
  17. get
  18. {
  19. return InstanceLazy.Value;
  20. }
  21. }
  22. private IContainer _sshServer;
  23. private IFutureDockerImage _sshServerImage;
  24. public string SshServerHostName { get; set; }
  25. public ushort SshServerPort { get; set; }
  26. public SshUser AdminUser = new SshUser("sshnetadm", "ssh4ever");
  27. public SshUser User = new SshUser("sshnet", "ssh4ever");
  28. // To get the sshd logs (also uncomment WithOutputConsumer below)
  29. private readonly Stream _fsOut = Stream.Null; // File.Create("fsout.txt");
  30. private readonly Stream _fsErr = Stream.Null; // File.Create("fserr.txt");
  31. public async Task InitializeAsync()
  32. {
  33. _sshServerImage = new ImageFromDockerfileBuilder()
  34. .WithName("renci-ssh-tests-server-image")
  35. .WithDockerfileDirectory(CommonDirectoryPath.GetSolutionDirectory(), Path.Combine("test", "Renci.SshNet.IntegrationTests"))
  36. .WithDockerfile("Dockerfile.TestServer")
  37. .WithDeleteIfExists(true)
  38. .Build();
  39. await _sshServerImage.CreateAsync();
  40. _sshServer = new ContainerBuilder()
  41. .WithHostname("renci-ssh-tests-server")
  42. .WithImage(_sshServerImage)
  43. .WithPortBinding(22, true)
  44. //.WithOutputConsumer(Consume.RedirectStdoutAndStderrToStream(_fsOut, _fsErr))
  45. .Build();
  46. await _sshServer.StartAsync();
  47. SshServerPort = _sshServer.GetMappedPublicPort(22);
  48. SshServerHostName = _sshServer.Hostname;
  49. // Socket fails on Linux, reporting inability early. This is the Linux behavior by design.
  50. // https://github.com/dotnet/runtime/issues/47484#issuecomment-769239699
  51. // At this point we have to wait until the ssh server in the container is available
  52. if (Environment.OSVersion.Platform == PlatformID.Unix)
  53. {
  54. await Task.Delay(300);
  55. }
  56. }
  57. public async Task DisposeAsync()
  58. {
  59. if (_sshServer != null)
  60. {
  61. await _sshServer.DisposeAsync();
  62. }
  63. if (_sshServerImage != null)
  64. {
  65. await _sshServerImage.DisposeAsync();
  66. }
  67. await _fsOut.DisposeAsync();
  68. await _fsErr.DisposeAsync();
  69. }
  70. public void Dispose()
  71. {
  72. }
  73. }
  74. }