InfrastructureFixture.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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")
  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. }
  43. public async Task DisposeAsync()
  44. {
  45. if (_sshServer != null)
  46. {
  47. await _sshServer.DisposeAsync();
  48. }
  49. if (_sshServerImage != null)
  50. {
  51. await _sshServerImage.DisposeAsync();
  52. }
  53. }
  54. public void Dispose()
  55. {
  56. }
  57. }
  58. }