|
| 1 | +// |
| 2 | +// Copyright The Microcks Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License") |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | +// |
| 17 | + |
| 18 | + |
| 19 | +using DotNet.Testcontainers.Networks; |
| 20 | +using Microcks.Testcontainers.Connection; |
| 21 | + |
| 22 | +namespace Microcks.Testcontainers; |
| 23 | + |
| 24 | +/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" /> |
| 25 | +public sealed class MicrocksAsyncMinionBuilder |
| 26 | + : ContainerBuilder<MicrocksAsyncMinionBuilder, MicrocksAsyncMinionContainer, MicrocksAsyncMinionConfiguration> |
| 27 | +{ |
| 28 | + /// <summary> |
| 29 | + /// The default HTTP port for the Microcks async minion. |
| 30 | + /// </summary> |
| 31 | + public const ushort MicrocksAsyncMinionHttpPort = 8081; |
| 32 | + |
| 33 | + private const string MicrocksAsyncMinionFullImageName = "quay.io/microcks/microcks-uber-async-minion"; |
| 34 | + |
| 35 | + private readonly HashSet<string> _extraProtocols = []; |
| 36 | + private readonly INetwork _network; |
| 37 | + |
| 38 | + /// <inheritdoc /> |
| 39 | + protected override MicrocksAsyncMinionConfiguration DockerResourceConfiguration { get; } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Initializes a new instance of the <see cref="MicrocksAsyncMinionBuilder"/> class with the specified network. |
| 43 | + /// </summary> |
| 44 | + /// <param name="network">The network to be used by the Microcks async minion container.</param> |
| 45 | + public MicrocksAsyncMinionBuilder(INetwork network) |
| 46 | + : this(new MicrocksAsyncMinionConfiguration()) |
| 47 | + { |
| 48 | + this._network = network; |
| 49 | + DockerResourceConfiguration = Init().DockerResourceConfiguration; |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Initializes a new instance of the <see cref="MicrocksAsyncMinionBuilder"/> class with the specified resource configuration. |
| 54 | + /// </summary> |
| 55 | + /// <param name="resourceConfiguration">The resource configuration for the Microcks async minion container.</param> |
| 56 | + private MicrocksAsyncMinionBuilder(MicrocksAsyncMinionConfiguration resourceConfiguration) |
| 57 | + : base(resourceConfiguration) |
| 58 | + { |
| 59 | + DockerResourceConfiguration = resourceConfiguration; |
| 60 | + } |
| 61 | + |
| 62 | + /// <inheritdoc /> |
| 63 | + public override MicrocksAsyncMinionContainer Build() |
| 64 | + { |
| 65 | + Validate(); |
| 66 | + |
| 67 | + return new MicrocksAsyncMinionContainer(DockerResourceConfiguration); |
| 68 | + } |
| 69 | + |
| 70 | + /// <inheritdoc /> |
| 71 | + protected override MicrocksAsyncMinionBuilder Init() |
| 72 | + { |
| 73 | + return base.Init() |
| 74 | + .WithImage(MicrocksAsyncMinionFullImageName) |
| 75 | + .WithNetwork(this._network) |
| 76 | + .WithNetworkAliases("microcks-async-minion") |
| 77 | + .WithEnvironment("MICROCKS_HOST_PORT", "microcks:" + MicrocksBuilder.MicrocksHttpPort) |
| 78 | + .WithExposedPort(MicrocksAsyncMinionHttpPort) |
| 79 | + .WithWaitStrategy(Wait.ForUnixContainer().UntilMessageIsLogged(".*Profile prod activated\\..*")); |
| 80 | + } |
| 81 | + |
| 82 | + /// <inheritdoc /> |
| 83 | + protected override MicrocksAsyncMinionBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) |
| 84 | + { |
| 85 | + return Merge(DockerResourceConfiguration, new MicrocksAsyncMinionConfiguration(resourceConfiguration)); |
| 86 | + } |
| 87 | + |
| 88 | + /// <inheritdoc /> |
| 89 | + protected override MicrocksAsyncMinionBuilder Clone(IContainerConfiguration resourceConfiguration) |
| 90 | + { |
| 91 | + return Merge(DockerResourceConfiguration, new MicrocksAsyncMinionConfiguration(resourceConfiguration)); |
| 92 | + } |
| 93 | + |
| 94 | + /// <inheritdoc /> |
| 95 | + protected override MicrocksAsyncMinionBuilder Merge(MicrocksAsyncMinionConfiguration oldValue, MicrocksAsyncMinionConfiguration newValue) |
| 96 | + { |
| 97 | + return new MicrocksAsyncMinionBuilder(new MicrocksAsyncMinionConfiguration(oldValue, newValue)); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Configures the MicrocksAsyncMinionBuilder to use a Kafka connection. |
| 103 | + /// </summary> |
| 104 | + /// <param name="kafkaConnection">The Kafka connection details.</param> |
| 105 | + /// <returns>The updated MicrocksAsyncMinionBuilder instance.</returns> |
| 106 | + public MicrocksAsyncMinionBuilder WithKafkaConnection(KafkaConnection kafkaConnection) |
| 107 | + { |
| 108 | + _extraProtocols.Add("KAFKA"); |
| 109 | + var environments = new Dictionary<string, string> |
| 110 | + { |
| 111 | + { "ASYNC_PROTOCOLS", $",{string.Join(",", _extraProtocols)}" }, |
| 112 | + { "KAFKA_BOOTSTRAP_SERVER", kafkaConnection.BootstrapServers }, |
| 113 | + }; |
| 114 | + |
| 115 | + return Merge(DockerResourceConfiguration, new MicrocksAsyncMinionConfiguration(new ContainerConfiguration(environments: environments))); |
| 116 | + } |
| 117 | +} |
0 commit comments