Skip to content

Commit fa65855

Browse files
authored
chore: Add WithAcceptLicenseAgreement(bool) to container builder (#1370)
1 parent 7715969 commit fa65855

File tree

16 files changed

+142
-52
lines changed

16 files changed

+142
-52
lines changed

src/Testcontainers.Db2/Db2Builder.cs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ public sealed class Db2Builder : ContainerBuilder<Db2Builder, Db2Container, Db2C
1414

1515
public const string DefaultPassword = "db2inst1";
1616

17-
private const string AcceptLicenseAgreementEnvVar = "LICENSE";
18-
19-
private const string AcceptLicenseAgreement = "accept";
20-
21-
private const string DeclineLicenseAgreement = "decline";
22-
2317
/// <summary>
2418
/// Initializes a new instance of the <see cref="Db2Builder" /> class.
2519
/// </summary>
@@ -42,6 +36,15 @@ private Db2Builder(Db2Configuration resourceConfiguration)
4236
/// <inheritdoc />
4337
protected override Db2Configuration DockerResourceConfiguration { get; }
4438

39+
/// <inheritdoc />
40+
protected override string AcceptLicenseAgreementEnvVar { get; } = "LICENSE";
41+
42+
/// <inheritdoc />
43+
protected override string AcceptLicenseAgreement { get; } = "accept";
44+
45+
/// <inheritdoc />
46+
protected override string DeclineLicenseAgreement { get; } = "decline";
47+
4548
/// <summary>
4649
/// Accepts the license agreement.
4750
/// </summary>
@@ -50,7 +53,7 @@ private Db2Builder(Db2Configuration resourceConfiguration)
5053
/// </remarks>
5154
/// <param name="acceptLicenseAgreement">A boolean value indicating whether the Db2 license agreement is accepted.</param>
5255
/// <returns>A configured instance of <see cref="Db2Builder" />.</returns>
53-
public Db2Builder WithAcceptLicenseAgreement(bool acceptLicenseAgreement)
56+
public override Db2Builder WithAcceptLicenseAgreement(bool acceptLicenseAgreement)
5457
{
5558
var licenseAgreement = acceptLicenseAgreement ? AcceptLicenseAgreement : DeclineLicenseAgreement;
5659
return WithEnvironment(AcceptLicenseAgreementEnvVar, licenseAgreement);
@@ -94,6 +97,7 @@ public Db2Builder WithPassword(string password)
9497
public override Db2Container Build()
9598
{
9699
Validate();
100+
ValidateLicenseAgreement();
97101
return new Db2Container(DockerResourceConfiguration);
98102
}
99103

@@ -110,16 +114,8 @@ protected override Db2Builder Init() => base.Init()
110114
/// <inheritdoc />
111115
protected override void Validate()
112116
{
113-
const string message = "The image '{0}' requires you to accept a license agreement.";
114-
115117
base.Validate();
116118

117-
Predicate<Db2Configuration> licenseAgreementNotAccepted = value =>
118-
!value.Environments.TryGetValue(AcceptLicenseAgreementEnvVar, out var licenseAgreementValue) || !AcceptLicenseAgreement.Equals(licenseAgreementValue, StringComparison.Ordinal);
119-
120-
_ = Guard.Argument(DockerResourceConfiguration, nameof(DockerResourceConfiguration.Image))
121-
.ThrowIf(argument => licenseAgreementNotAccepted(argument.Value), argument => throw new ArgumentException(string.Format(message, DockerResourceConfiguration.Image.FullName), argument.Name));
122-
123119
_ = Guard.Argument(DockerResourceConfiguration.Username, nameof(DockerResourceConfiguration.Username))
124120
.NotNull()
125121
.NotEmpty();

src/Testcontainers.Neo4j/Neo4jBuilder.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ public sealed class Neo4jBuilder : ContainerBuilder<Neo4jBuilder, Neo4jContainer
1010

1111
public const ushort Neo4jBoltPort = 7687;
1212

13-
private const string AcceptLicenseAgreementEnvVar = "NEO4J_ACCEPT_LICENSE_AGREEMENT";
14-
15-
private const string AcceptLicenseAgreement = "yes";
16-
17-
private const string DeclineLicenseAgreement = "no";
18-
1913
/// <summary>
2014
/// Initializes a new instance of the <see cref="Neo4jBuilder" /> class.
2115
/// </summary>
@@ -38,6 +32,15 @@ private Neo4jBuilder(Neo4jConfiguration resourceConfiguration)
3832
/// <inheritdoc />
3933
protected override Neo4jConfiguration DockerResourceConfiguration { get; }
4034

35+
/// <inheritdoc />
36+
protected override string AcceptLicenseAgreementEnvVar { get; } = "NEO4J_ACCEPT_LICENSE_AGREEMENT";
37+
38+
/// <inheritdoc />
39+
protected override string AcceptLicenseAgreement { get; } = "yes";
40+
41+
/// <inheritdoc />
42+
protected override string DeclineLicenseAgreement { get; } = "no";
43+
4144
/// <summary>
4245
/// Sets the image to the Neo4j Enterprise Edition.
4346
/// </summary>

src/Testcontainers.Pulsar/Usings.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
global using System.Threading;
1010
global using System.Threading.Tasks;
1111
global using Docker.DotNet.Models;
12-
global using DotNet.Testcontainers;
1312
global using DotNet.Testcontainers.Builders;
1413
global using DotNet.Testcontainers.Configurations;
1514
global using DotNet.Testcontainers.Containers;

src/Testcontainers.ServiceBus/ServiceBusBuilder.cs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ public sealed class ServiceBusBuilder : ContainerBuilder<ServiceBusBuilder, Serv
1212

1313
public const ushort ServiceBusPort = 5672;
1414

15-
private const string AcceptLicenseAgreementEnvVar = "ACCEPT_EULA";
16-
17-
private const string AcceptLicenseAgreement = "Y";
18-
19-
private const string DeclineLicenseAgreement = "N";
20-
2115
/// <summary>
2216
/// Initializes a new instance of the <see cref="ServiceBusBuilder" /> class.
2317
/// </summary>
@@ -40,6 +34,15 @@ private ServiceBusBuilder(ServiceBusConfiguration resourceConfiguration)
4034
/// <inheritdoc />
4135
protected override ServiceBusConfiguration DockerResourceConfiguration { get; }
4236

37+
/// <inheritdoc />
38+
protected override string AcceptLicenseAgreementEnvVar { get; } = "ACCEPT_EULA";
39+
40+
/// <inheritdoc />
41+
protected override string AcceptLicenseAgreement { get; } = "Y";
42+
43+
/// <inheritdoc />
44+
protected override string DeclineLicenseAgreement { get; } = "N";
45+
4346
/// <summary>
4447
/// Accepts the license agreement.
4548
/// </summary>
@@ -48,7 +51,7 @@ private ServiceBusBuilder(ServiceBusConfiguration resourceConfiguration)
4851
/// </remarks>
4952
/// <param name="acceptLicenseAgreement">A boolean value indicating whether the Azure Service Bus Emulator license agreement is accepted.</param>
5053
/// <returns>A configured instance of <see cref="ServiceBusBuilder" />.</returns>
51-
public ServiceBusBuilder WithAcceptLicenseAgreement(bool acceptLicenseAgreement)
54+
public override ServiceBusBuilder WithAcceptLicenseAgreement(bool acceptLicenseAgreement)
5255
{
5356
var licenseAgreement = acceptLicenseAgreement ? AcceptLicenseAgreement : DeclineLicenseAgreement;
5457
return WithEnvironment(AcceptLicenseAgreementEnvVar, licenseAgreement);
@@ -85,6 +88,7 @@ public ServiceBusBuilder WithMsSqlContainer(
8588
public override ServiceBusContainer Build()
8689
{
8790
Validate();
91+
ValidateLicenseAgreement();
8892

8993
if (DockerResourceConfiguration.DatabaseContainer != null)
9094
{
@@ -105,20 +109,6 @@ public override ServiceBusContainer Build()
105109
return new ServiceBusContainer(serviceBusContainer.DockerResourceConfiguration);
106110
}
107111

108-
/// <inheritdoc />
109-
protected override void Validate()
110-
{
111-
const string message = "The image '{0}' requires you to accept a license agreement.";
112-
113-
base.Validate();
114-
115-
Predicate<ServiceBusConfiguration> licenseAgreementNotAccepted = value =>
116-
!value.Environments.TryGetValue(AcceptLicenseAgreementEnvVar, out var licenseAgreementValue) || !AcceptLicenseAgreement.Equals(licenseAgreementValue, StringComparison.Ordinal);
117-
118-
_ = Guard.Argument(DockerResourceConfiguration, nameof(DockerResourceConfiguration.Image))
119-
.ThrowIf(argument => licenseAgreementNotAccepted(argument.Value), argument => throw new ArgumentException(string.Format(message, DockerResourceConfiguration.Image.FullName), argument.Name));
120-
}
121-
122112
/// <inheritdoc />
123113
protected override ServiceBusBuilder Init()
124114
{

src/Testcontainers.ServiceBus/Usings.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
global using System.Threading;
55
global using System.Threading.Tasks;
66
global using Docker.DotNet.Models;
7-
global using DotNet.Testcontainers;
87
global using DotNet.Testcontainers.Builders;
98
global using DotNet.Testcontainers.Configurations;
109
global using DotNet.Testcontainers.Containers;

src/Testcontainers/Builders/Base64Provider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ public Base64Provider(JsonElement jsonElement, ILogger logger)
3939
}
4040

4141
/// <summary>
42-
/// Gets a predicate that determines whether or not a <see cref="JsonProperty" /> contains a Docker registry key.
42+
/// Gets a predicate that determines whether a <see cref="JsonProperty" /> contains a Docker registry key.
4343
/// </summary>
4444
public static Func<JsonProperty, string, bool> HasDockerRegistryKey { get; }
4545
= (property, hostname) => property.Name.Equals(hostname, StringComparison.OrdinalIgnoreCase) || property.Name.EndsWith("://" + hostname, StringComparison.OrdinalIgnoreCase);
4646

4747
/// <inheritdoc />
4848
public bool IsApplicable(string hostname)
4949
{
50-
return !default(JsonElement).Equals(_rootElement) && !JsonValueKind.Null.Equals(_rootElement.ValueKind) && _rootElement.EnumerateObject().Any(property => HasDockerRegistryKey(property, hostname));
50+
return !JsonValueKind.Undefined.Equals(_rootElement.ValueKind) && !JsonValueKind.Null.Equals(_rootElement.ValueKind) && _rootElement.EnumerateObject().Any(property => HasDockerRegistryKey(property, hostname));
5151
}
5252

5353
/// <inheritdoc />

src/Testcontainers/Builders/ContainerBuilder`3.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,28 @@ protected ContainerBuilder(TConfigurationEntity dockerResourceConfiguration)
3636
{
3737
}
3838

39+
/// <summary>
40+
/// Gets the name of the environment variable that must be set to accept the image license agreement.
41+
/// </summary>
42+
protected virtual string AcceptLicenseAgreementEnvVar { get; }
43+
44+
/// <summary>
45+
/// Gets the expected value of <see cref="AcceptLicenseAgreementEnvVar" /> that indicates acceptance of the license agreement.
46+
/// </summary>
47+
protected virtual string AcceptLicenseAgreement { get; }
48+
49+
/// <summary>
50+
/// Gets the expected value of <see cref="AcceptLicenseAgreementEnvVar" /> that indicates rejection of the license agreement.
51+
/// </summary>
52+
protected virtual string DeclineLicenseAgreement { get; }
53+
54+
/// <inheritdoc />
55+
public virtual TBuilderEntity WithAcceptLicenseAgreement(bool acceptLicenseAgreement)
56+
{
57+
const string licenseAgreementNotRequired = "The module does not require you to accept a license agreement.";
58+
throw new InvalidOperationException(licenseAgreementNotRequired);
59+
}
60+
3961
/// <inheritdoc />
4062
public TBuilderEntity DependsOn(IContainer container)
4163
{
@@ -374,7 +396,7 @@ public TBuilderEntity WithStartupCallback(Func<TContainerEntity, CancellationTok
374396
/// <inheritdoc />
375397
protected override TBuilderEntity Init()
376398
{
377-
return base.Init().WithImagePullPolicy(PullPolicy.Missing).WithPortForwarding().WithOutputConsumer(Consume.DoNotConsumeStdoutAndStderr()).WithWaitStrategy(Wait.ForUnixContainer()).WithStartupCallback((_, ct) => Task.CompletedTask);
399+
return base.Init().WithImagePullPolicy(PullPolicy.Missing).WithPortForwarding().WithOutputConsumer(Consume.DoNotConsumeStdoutAndStderr()).WithWaitStrategy(Wait.ForUnixContainer()).WithStartupCallback((_, _) => Task.CompletedTask);
378400
}
379401

380402
/// <inheritdoc />
@@ -390,6 +412,21 @@ protected override void Validate()
390412
.NotNull();
391413
}
392414

415+
/// <summary>
416+
/// Validates the license agreement.
417+
/// </summary>
418+
/// <exception cref="ArgumentException">Thrown when the license agreement is not accepted.</exception>
419+
protected virtual void ValidateLicenseAgreement()
420+
{
421+
const string message = "The image '{0}' requires you to accept a license agreement.";
422+
423+
Predicate<TConfigurationEntity> licenseAgreementNotAccepted = value =>
424+
!value.Environments.TryGetValue(AcceptLicenseAgreementEnvVar, out var licenseAgreementValue) || !AcceptLicenseAgreement.Equals(licenseAgreementValue, StringComparison.Ordinal);
425+
426+
_ = Guard.Argument(DockerResourceConfiguration, nameof(DockerResourceConfiguration.Image))
427+
.ThrowIf(argument => licenseAgreementNotAccepted(argument.Value), argument => throw new ArgumentException(string.Format(message, DockerResourceConfiguration.Image.FullName), argument.Name));
428+
}
429+
393430
/// <summary>
394431
/// Clones the Docker resource builder configuration.
395432
/// </summary>

src/Testcontainers/Builders/CredsHelperProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public CredsHelperProvider(JsonElement jsonElement, ILogger logger)
3939
/// <inheritdoc />
4040
public bool IsApplicable(string hostname)
4141
{
42-
return !default(JsonElement).Equals(_rootElement) && !JsonValueKind.Null.Equals(_rootElement.ValueKind) && _rootElement.EnumerateObject().Any(property => Base64Provider.HasDockerRegistryKey(property, hostname));
42+
return !JsonValueKind.Undefined.Equals(_rootElement.ValueKind) && !JsonValueKind.Null.Equals(_rootElement.ValueKind) && _rootElement.EnumerateObject().Any(property => Base64Provider.HasDockerRegistryKey(property, hostname));
4343
}
4444

4545
/// <inheritdoc />

src/Testcontainers/Builders/CredsStoreProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public CredsStoreProvider(JsonElement jsonElement, ILogger logger)
3838
/// <inheritdoc />
3939
public bool IsApplicable(string hostname)
4040
{
41-
return !default(JsonElement).Equals(_rootElement) && !string.IsNullOrEmpty(_rootElement.GetString());
41+
return !JsonValueKind.Undefined.Equals(_rootElement.ValueKind) && !string.IsNullOrEmpty(_rootElement.GetString());
4242
}
4343

4444
/// <inheritdoc />

src/Testcontainers/Builders/IContainerBuilder`2.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ namespace DotNet.Testcontainers.Builders
2121
[PublicAPI]
2222
public interface IContainerBuilder<out TBuilderEntity, out TContainerEntity> : IAbstractBuilder<TBuilderEntity, TContainerEntity, CreateContainerParameters>
2323
{
24+
/// <summary>
25+
/// Accepts the license agreement.
26+
/// </summary>
27+
/// <remarks>
28+
/// Modules that require a license agreement must override and implement this
29+
/// method to enforce proper license acceptance behavior.
30+
/// </remarks>
31+
/// <param name="acceptLicenseAgreement">A boolean value indicating whether the license agreement is accepted.</param>
32+
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
33+
/// <exception cref="InvalidOperationException">Thrown when the module does not require a license agreement.</exception>
34+
TBuilderEntity WithAcceptLicenseAgreement(bool acceptLicenseAgreement);
35+
2436
/// <summary>
2537
/// Sets the dependent container to resolve and start before starting this container configuration.
2638
/// </summary>

0 commit comments

Comments
 (0)