2
0

InfrastructureFixture.cs 2.9 KB

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