Skip to content

Commit 14e90a8

Browse files
committed
feat: Add WithPlatform(string) container builder API
1 parent ed192fb commit 14e90a8

File tree

7 files changed

+35
-6
lines changed

7 files changed

+35
-6
lines changed

src/Testcontainers/Builders/ContainerBuilder`3.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ public TBuilderEntity WithImage(IImage image)
9595
return Clone(new ContainerConfiguration(image: image.ApplyHubImageNamePrefix()));
9696
}
9797

98+
/// <inheritdoc />
99+
public TBuilderEntity WithPlatform(string platform)
100+
{
101+
return Clone(new ContainerConfiguration(platform: platform));
102+
}
103+
98104
/// <inheritdoc />
99105
public TBuilderEntity WithImagePullPolicy(Func<ImageInspectResponse, bool> imagePullPolicy)
100106
{

src/Testcontainers/Builders/IContainerBuilder`2.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ public interface IContainerBuilder<out TBuilderEntity, out TContainerEntity, out
8787
[PublicAPI]
8888
TBuilderEntity WithImage(IImage image);
8989

90+
/// <summary>
91+
/// Sets the platform.
92+
/// </summary>
93+
/// <param name="platform">The platform.</param>
94+
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
95+
[PublicAPI]
96+
TBuilderEntity WithPlatform(string platform);
97+
9098
/// <summary>
9199
/// Sets the image pull policy.
92100
/// </summary>

src/Testcontainers/Clients/DockerImageOperations.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ public async Task<bool> ExistsWithIdAsync(string id, CancellationToken ct = defa
5555
}
5656
}
5757

58-
public async Task CreateAsync(IImage image, IDockerRegistryAuthenticationConfiguration dockerRegistryAuthConfig, CancellationToken ct = default)
58+
public async Task CreateAsync(IImage image, IDockerRegistryAuthenticationConfiguration dockerRegistryAuthConfig, string platform = null, CancellationToken ct = default)
5959
{
6060
var createParameters = new ImagesCreateParameters
6161
{
6262
FromImage = image.FullName,
63+
Platform = platform,
6364
};
6465

6566
var authConfig = new AuthConfig

src/Testcontainers/Clients/IDockerImageOperations.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace DotNet.Testcontainers.Clients
88

99
internal interface IDockerImageOperations : IHasListOperations<ImagesListResponse, ImageInspectResponse>
1010
{
11-
Task CreateAsync(IImage image, IDockerRegistryAuthenticationConfiguration dockerRegistryAuthConfig, CancellationToken ct = default);
11+
Task CreateAsync(IImage image, IDockerRegistryAuthenticationConfiguration dockerRegistryAuthConfig, string platform = null, CancellationToken ct = default);
1212

1313
Task DeleteAsync(IImage image, CancellationToken ct = default);
1414

src/Testcontainers/Clients/TestcontainersClient.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ public async Task<string> RunAsync(IContainerConfiguration configuration, Cancel
334334

335335
if (configuration.ImagePullPolicy(cachedImage))
336336
{
337-
await PullImageAsync(configuration.Image, ct)
337+
await PullImageAsync(configuration.Image, configuration.Platform, ct)
338338
.ConfigureAwait(false);
339339
}
340340

@@ -392,7 +392,7 @@ public async Task<string> BuildAsync(IImageFromDockerfileConfiguration configura
392392

393393
var uncachedImages = baseImages.Where(baseImage => !repositoryTags.Contains(baseImage.FullName));
394394

395-
await Task.WhenAll(uncachedImages.Select(image => PullImageAsync(image, ct)))
395+
await Task.WhenAll(uncachedImages.Select(image => PullImageAsync(image, null, ct)))
396396
.ConfigureAwait(false);
397397

398398
_ = await Image.BuildAsync(configuration, dockerfileArchive, ct)
@@ -406,8 +406,9 @@ await Task.WhenAll(uncachedImages.Select(image => PullImageAsync(image, ct)))
406406
/// Pulls an image from a registry.
407407
/// </summary>
408408
/// <param name="image">The image to pull.</param>
409+
/// <param name="platform">The platform.</param>
409410
/// <param name="ct">Cancellation token.</param>
410-
private async Task PullImageAsync(IImage image, CancellationToken ct = default)
411+
private async Task PullImageAsync(IImage image, string platform = null, CancellationToken ct = default)
411412
{
412413
var dockerRegistryServerAddress = image.GetHostname();
413414

@@ -427,7 +428,7 @@ private async Task PullImageAsync(IImage image, CancellationToken ct = default)
427428

428429
var authConfig = _registryAuthenticationProvider.GetAuthConfig(dockerRegistryServerAddress);
429430

430-
await Image.CreateAsync(image, authConfig, ct)
431+
await Image.CreateAsync(image, authConfig, platform, ct)
431432
.ConfigureAwait(false);
432433
}
433434
}

src/Testcontainers/Configurations/Containers/ContainerConfiguration.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class ContainerConfiguration : ResourceConfiguration<CreateContainerParam
2020
/// Initializes a new instance of the <see cref="ContainerConfiguration" /> class.
2121
/// </summary>
2222
/// <param name="image">The image.</param>
23+
/// <param name="platform">The platform.</param>
2324
/// <param name="imagePullPolicy">The image pull policy.</param>
2425
/// <param name="name">The name.</param>
2526
/// <param name="hostname">The hostname.</param>
@@ -43,6 +44,7 @@ public class ContainerConfiguration : ResourceConfiguration<CreateContainerParam
4344
/// <param name="privileged">A value indicating whether the privileged flag is set or not.</param>
4445
public ContainerConfiguration(
4546
IImage image = null,
47+
string platform = null,
4648
Func<ImageInspectResponse, bool> imagePullPolicy = null,
4749
string name = null,
4850
string hostname = null,
@@ -69,6 +71,7 @@ public ContainerConfiguration(
6971
AutoRemove = autoRemove;
7072
Privileged = privileged;
7173
Image = image;
74+
Platform = platform;
7275
ImagePullPolicy = imagePullPolicy;
7376
Name = name;
7477
Hostname = hostname;
@@ -118,6 +121,7 @@ public ContainerConfiguration(IContainerConfiguration oldValue, IContainerConfig
118121
: base(oldValue, newValue)
119122
{
120123
Image = BuildConfiguration.Combine(oldValue.Image, newValue.Image);
124+
Platform = BuildConfiguration.Combine(oldValue.Platform, newValue.Platform);
121125
ImagePullPolicy = BuildConfiguration.Combine(oldValue.ImagePullPolicy, newValue.ImagePullPolicy);
122126
Name = BuildConfiguration.Combine(oldValue.Name, newValue.Name);
123127
Hostname = BuildConfiguration.Combine(oldValue.Hostname, newValue.Hostname);
@@ -153,6 +157,10 @@ public ContainerConfiguration(IContainerConfiguration oldValue, IContainerConfig
153157
/// <inheritdoc />
154158
public IImage Image { get; }
155159

160+
/// <inheritdoc />
161+
[JsonIgnore]
162+
public string Platform { get; }
163+
156164
/// <inheritdoc />
157165
[JsonIgnore]
158166
public Func<ImageInspectResponse, bool> ImagePullPolicy { get; }

src/Testcontainers/Configurations/Containers/IContainerConfiguration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public interface IContainerConfiguration : IResourceConfiguration<CreateContaine
3131
/// </summary>
3232
IImage Image { get; }
3333

34+
/// <summary>
35+
/// Gets the platform.
36+
/// </summary>
37+
string Platform { get; }
38+
3439
/// <summary>
3540
/// Gets the image pull policy.
3641
/// </summary>

0 commit comments

Comments
 (0)