| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using System.Diagnostics;
- using DotNet.Testcontainers.Builders;
- using DotNet.Testcontainers.Containers;
- using DotNet.Testcontainers.Images;
- using Renci.SshNet.Abstractions;
- namespace Renci.SshNet.IntegrationTests.TestsFixtures
- {
- public sealed class InfrastructureFixture : IDisposable
- {
- private InfrastructureFixture()
- {
- }
- private static readonly Lazy<InfrastructureFixture> InstanceLazy = new Lazy<InfrastructureFixture>(() => new InfrastructureFixture());
- public static InfrastructureFixture Instance
- {
- get
- {
- return InstanceLazy.Value;
- }
- }
- private IContainer _sshServer;
- private IFutureDockerImage _sshServerImage;
- public string SshServerHostName { get; set; }
- public ushort SshServerPort { get; set; }
- public SshUser AdminUser = new SshUser("sshnetadm", "ssh4ever");
- public SshUser User = new SshUser("sshnet", "ssh4ever");
- public async Task InitializeAsync()
- {
- DiagnosticAbstraction.Source.Switch = new SourceSwitch("sourceSwitch", "Verbose");
- DiagnosticAbstraction.Source.Listeners.Remove("Default");
- DiagnosticAbstraction.Source.Listeners.Add(new ConsoleTraceListener());
- _sshServerImage = new ImageFromDockerfileBuilder()
- .WithName("renci-ssh-tests-server-image")
- .WithDockerfileDirectory(CommonDirectoryPath.GetSolutionDirectory(), Path.Combine("test", "Renci.SshNet.IntegrationTests"))
- .WithDockerfile("Dockerfile")
- .WithDeleteIfExists(true)
-
- .Build();
- await _sshServerImage.CreateAsync();
- _sshServer = new ContainerBuilder()
- .WithHostname("renci-ssh-tests-server")
- .WithImage(_sshServerImage)
- .WithPortBinding(22, true)
- .Build();
- await _sshServer.StartAsync();
- SshServerPort = _sshServer.GetMappedPublicPort(22);
- SshServerHostName = _sshServer.Hostname;
- }
- public async Task DisposeAsync()
- {
- if (_sshServer != null)
- {
- await _sshServer.DisposeAsync();
- }
- if (_sshServerImage != null)
- {
- await _sshServerImage.DisposeAsync();
- }
- }
- public void Dispose()
- {
- }
- }
- }
|