|  | 
|  | 1 | +namespace DotNet.Testcontainers.Containers | 
|  | 2 | +{ | 
|  | 3 | +  using System; | 
|  | 4 | +  using System.Collections.Generic; | 
|  | 5 | +  using System.Linq; | 
|  | 6 | +  using Docker.DotNet.Models; | 
|  | 7 | +  using DotNet.Testcontainers.Builders; | 
|  | 8 | +  using DotNet.Testcontainers.Configurations; | 
|  | 9 | +  using JetBrains.Annotations; | 
|  | 10 | + | 
|  | 11 | +  /// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" /> | 
|  | 12 | +  [PublicAPI] | 
|  | 13 | +  public sealed class SocatBuilder : ContainerBuilder<SocatBuilder, SocatContainer, SocatConfiguration> | 
|  | 14 | +  { | 
|  | 15 | +    public const string SocatImage = "alpine/socat:1.7.4.3-r0"; | 
|  | 16 | + | 
|  | 17 | +    /// <summary> | 
|  | 18 | +    /// Initializes a new instance of the <see cref="SocatBuilder" /> class. | 
|  | 19 | +    /// </summary> | 
|  | 20 | +    public SocatBuilder() | 
|  | 21 | +      : this(new SocatConfiguration()) | 
|  | 22 | +    { | 
|  | 23 | +      DockerResourceConfiguration = Init().DockerResourceConfiguration; | 
|  | 24 | +    } | 
|  | 25 | + | 
|  | 26 | +    /// <summary> | 
|  | 27 | +    /// Initializes a new instance of the <see cref="SocatBuilder" /> class. | 
|  | 28 | +    /// </summary> | 
|  | 29 | +    /// <param name="resourceConfiguration">The Docker resource configuration.</param> | 
|  | 30 | +    private SocatBuilder(SocatConfiguration resourceConfiguration) | 
|  | 31 | +      : base(resourceConfiguration) | 
|  | 32 | +    { | 
|  | 33 | +      DockerResourceConfiguration = resourceConfiguration; | 
|  | 34 | +    } | 
|  | 35 | + | 
|  | 36 | +    /// <inheritdoc /> | 
|  | 37 | +    protected override SocatConfiguration DockerResourceConfiguration { get; } | 
|  | 38 | + | 
|  | 39 | +    /// <summary> | 
|  | 40 | +    /// Sets the Socat target. | 
|  | 41 | +    /// </summary> | 
|  | 42 | +    /// <param name="exposedPort">The Socat exposed port.</param> | 
|  | 43 | +    /// <param name="host">The Socat target host.</param> | 
|  | 44 | +    /// <returns>A configured instance of <see cref="SocatBuilder" />.</returns> | 
|  | 45 | +    public SocatBuilder WithTarget(int exposedPort, string host) | 
|  | 46 | +    { | 
|  | 47 | +      return WithTarget(exposedPort, host, exposedPort); | 
|  | 48 | +    } | 
|  | 49 | + | 
|  | 50 | +    /// <summary> | 
|  | 51 | +    /// Sets the Socat target. | 
|  | 52 | +    /// </summary> | 
|  | 53 | +    /// <param name="exposedPort">The Socat exposed port.</param> | 
|  | 54 | +    /// <param name="host">The Socat target host.</param> | 
|  | 55 | +    /// <param name="internalPort">The Socat target port.</param> | 
|  | 56 | +    /// <returns>A configured instance of <see cref="SocatBuilder" />.</returns> | 
|  | 57 | +    public SocatBuilder WithTarget(int exposedPort, string host, int internalPort) | 
|  | 58 | +    { | 
|  | 59 | +      var targets = new Dictionary<int, string> { { exposedPort, $"{host}:{internalPort}" } }; | 
|  | 60 | +      return Merge(DockerResourceConfiguration, new SocatConfiguration(targets)) | 
|  | 61 | +        .WithPortBinding(exposedPort, true); | 
|  | 62 | +    } | 
|  | 63 | + | 
|  | 64 | +    /// <inheritdoc /> | 
|  | 65 | +    public override SocatContainer Build() | 
|  | 66 | +    { | 
|  | 67 | +      Validate(); | 
|  | 68 | + | 
|  | 69 | +      const string argument = "socat TCP-LISTEN:{0},fork,reuseaddr TCP:{1}"; | 
|  | 70 | + | 
|  | 71 | +      var command = string.Join(" & ", DockerResourceConfiguration.Targets | 
|  | 72 | +        .Select(item => string.Format(argument, item.Key, item.Value))); | 
|  | 73 | + | 
|  | 74 | +      var waitStrategy = DockerResourceConfiguration.Targets | 
|  | 75 | +        .Aggregate(Wait.ForUnixContainer(), (waitStrategy, item) => waitStrategy.UntilPortIsAvailable(item.Key)); | 
|  | 76 | + | 
|  | 77 | +      var socatBuilder = WithCommand(command).WithWaitStrategy(waitStrategy); | 
|  | 78 | +      return new SocatContainer(socatBuilder.DockerResourceConfiguration); | 
|  | 79 | +    } | 
|  | 80 | + | 
|  | 81 | +    /// <inheritdoc /> | 
|  | 82 | +    protected override SocatBuilder Init() | 
|  | 83 | +    { | 
|  | 84 | +      return base.Init() | 
|  | 85 | +        .WithImage(SocatImage) | 
|  | 86 | +        .WithEntrypoint("/bin/sh", "-c"); | 
|  | 87 | +    } | 
|  | 88 | + | 
|  | 89 | +    /// <inheritdoc /> | 
|  | 90 | +    protected override void Validate() | 
|  | 91 | +    { | 
|  | 92 | +      const string message = "Missing targets. One target must be specified to be created."; | 
|  | 93 | + | 
|  | 94 | +      base.Validate(); | 
|  | 95 | + | 
|  | 96 | +      _ = Guard.Argument(DockerResourceConfiguration.Targets, nameof(DockerResourceConfiguration.Targets)) | 
|  | 97 | +        .ThrowIf(argument => argument.Value.Count == 0, argument => new ArgumentException(message, argument.Name)); | 
|  | 98 | +    } | 
|  | 99 | + | 
|  | 100 | +    /// <inheritdoc /> | 
|  | 101 | +    protected override SocatBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | 
|  | 102 | +    { | 
|  | 103 | +      return Merge(DockerResourceConfiguration, new SocatConfiguration(resourceConfiguration)); | 
|  | 104 | +    } | 
|  | 105 | + | 
|  | 106 | +    /// <inheritdoc /> | 
|  | 107 | +    protected override SocatBuilder Clone(IContainerConfiguration resourceConfiguration) | 
|  | 108 | +    { | 
|  | 109 | +      return Merge(DockerResourceConfiguration, new SocatConfiguration(resourceConfiguration)); | 
|  | 110 | +    } | 
|  | 111 | + | 
|  | 112 | +    /// <inheritdoc /> | 
|  | 113 | +    protected override SocatBuilder Merge(SocatConfiguration oldValue, SocatConfiguration newValue) | 
|  | 114 | +    { | 
|  | 115 | +      return new SocatBuilder(new SocatConfiguration(oldValue, newValue)); | 
|  | 116 | +    } | 
|  | 117 | +  } | 
|  | 118 | +} | 
0 commit comments