InfrastructureFixture.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Diagnostics;
  2. using DotNet.Testcontainers.Builders;
  3. using DotNet.Testcontainers.Containers;
  4. using DotNet.Testcontainers.Images;
  5. using Renci.SshNet.Abstractions;
  6. namespace Renci.SshNet.IntegrationTests.TestsFixtures
  7. {
  8. public sealed class InfrastructureFixture : IDisposable
  9. {
  10. private InfrastructureFixture()
  11. {
  12. }
  13. private static readonly Lazy<InfrastructureFixture> InstanceLazy = new Lazy<InfrastructureFixture>(() => new InfrastructureFixture());
  14. public static InfrastructureFixture Instance
  15. {
  16. get
  17. {
  18. return InstanceLazy.Value;
  19. }
  20. }
  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. DiagnosticAbstraction.Source.Switch = new SourceSwitch("sourceSwitch", "Verbose");
  30. DiagnosticAbstraction.Source.Listeners.Remove("Default");
  31. DiagnosticAbstraction.Source.Listeners.Add(new ConsoleTraceListener());
  32. _sshServerImage = new ImageFromDockerfileBuilder()
  33. .WithName("renci-ssh-tests-server-image")
  34. .WithDockerfileDirectory(CommonDirectoryPath.GetSolutionDirectory(), Path.Combine("test", "Renci.SshNet.IntegrationTests"))
  35. .WithDockerfile("Dockerfile")
  36. .WithDeleteIfExists(true)
  37. .Build();
  38. await _sshServerImage.CreateAsync();
  39. _sshServer = new ContainerBuilder()
  40. .WithHostname("renci-ssh-tests-server")
  41. .WithImage(_sshServerImage)
  42. .WithPortBinding(22, true)
  43. .Build();
  44. await _sshServer.StartAsync();
  45. SshServerPort = _sshServer.GetMappedPublicPort(22);
  46. SshServerHostName = _sshServer.Hostname;
  47. }
  48. public async Task DisposeAsync()
  49. {
  50. if (_sshServer != null)
  51. {
  52. await _sshServer.DisposeAsync();
  53. }
  54. if (_sshServerImage != null)
  55. {
  56. await _sshServerImage.DisposeAsync();
  57. }
  58. }
  59. public void Dispose()
  60. {
  61. }
  62. }
  63. }