Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add method to set a fixed container mac address #616

Closed
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Testcontainers/Builders/ITestcontainersBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ public interface ITestcontainersBuilder<out TDockerContainer> : IAbstractBuilder
[PublicAPI]
ITestcontainersBuilder<TDockerContainer> WithHostname(string hostname);

/// <summary>
/// Sets the MAC address of the Testcontainer.
/// </summary>
/// <param name="macAddress">Testcontainers MAC address.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{TDockerContainer}" />.</returns>
[PublicAPI]
ITestcontainersBuilder<TDockerContainer> WithMacAddress(string macAddress);

/// <summary>
/// Overrides the working directory of the Testcontainer for the instruction sets.
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion src/Testcontainers/Builders/TestcontainersBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ public ITestcontainersBuilder<TDockerContainer> WithHostname(string hostname)
return this.MergeNewConfiguration(new TestcontainersConfiguration(hostname: hostname));
}

/// <inheritdoc cref="ITestcontainersBuilder{TDockerContainer}" />
public ITestcontainersBuilder<TDockerContainer> WithMacAddress(string macAddress)
{
return this.MergeNewConfiguration(new TestcontainersConfiguration(macAddress: macAddress));
}

/// <inheritdoc cref="ITestcontainersBuilder{TDockerContainer}" />
public ITestcontainersBuilder<TDockerContainer> WithWorkingDirectory(string workingDirectory)
{
Expand Down Expand Up @@ -357,6 +363,7 @@ protected virtual ITestcontainersBuilder<TDockerContainer> MergeNewConfiguration
var name = BuildConfiguration.Combine(dockerResourceConfiguration.Name, this.DockerResourceConfiguration.Name);
var imagePullPolicy = BuildConfiguration.Combine(dockerResourceConfiguration.ImagePullPolicy, this.DockerResourceConfiguration.ImagePullPolicy);
var hostname = BuildConfiguration.Combine(dockerResourceConfiguration.Hostname, this.DockerResourceConfiguration.Hostname);
var macAddress = BuildConfiguration.Combine(dockerResourceConfiguration.MacAddress, this.DockerResourceConfiguration.MacAddress);
var workingDirectory = BuildConfiguration.Combine(dockerResourceConfiguration.WorkingDirectory, this.DockerResourceConfiguration.WorkingDirectory);
var entrypoint = BuildConfiguration.Combine(dockerResourceConfiguration.Entrypoint, this.DockerResourceConfiguration.Entrypoint);
var command = BuildConfiguration.Combine(dockerResourceConfiguration.Command, this.DockerResourceConfiguration.Command);
Expand All @@ -375,7 +382,7 @@ protected virtual ITestcontainersBuilder<TDockerContainer> MergeNewConfiguration
var parameterModifiers = BuildConfiguration.Combine(dockerResourceConfiguration.ParameterModifiers, this.DockerResourceConfiguration.ParameterModifiers);
var startupCallback = BuildConfiguration.Combine(dockerResourceConfiguration.StartupCallback, this.DockerResourceConfiguration.StartupCallback);

var updatedDockerResourceConfiguration = new TestcontainersConfiguration(dockerEndpointAuthConfig, dockerRegistryAuthConfig, image, imagePullPolicy, name, hostname, workingDirectory, entrypoint, command, environments, labels, exposedPorts, portBindings, mounts, networks, networkAliases, outputConsumer, waitStrategies, parameterModifiers, startupCallback, autoRemove, privileged);
var updatedDockerResourceConfiguration = new TestcontainersConfiguration(dockerEndpointAuthConfig, dockerRegistryAuthConfig, image, imagePullPolicy, name, hostname, macAddress, workingDirectory, entrypoint, command, environments, labels, exposedPorts, portBindings, mounts, networks, networkAliases, outputConsumer, waitStrategies, parameterModifiers, startupCallback, autoRemove, privileged);
return new TestcontainersBuilder<TDockerContainer>(updatedDockerResourceConfiguration, this.mergeModuleConfiguration);
}

Expand Down
1 change: 1 addition & 0 deletions src/Testcontainers/Clients/DockerContainerOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public async Task<string> RunAsync(ITestcontainersConfiguration configuration, C
Image = configuration.Image.FullName,
Name = configuration.Name,
Hostname = configuration.Hostname,
MacAddress = configuration.MacAddress,
WorkingDir = configuration.WorkingDirectory,
Entrypoint = converter.Entrypoint,
Cmd = converter.Command,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public interface ITestcontainersConfiguration : IDockerResourceConfiguration
/// </summary>
string Hostname { get; }

/// <summary>
/// Gets the MAC address.
/// </summary>
string MacAddress { get; }

/// <summary>
/// Gets the working directory.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public TestcontainersConfiguration(IDockerResourceConfiguration dockerResourceCo
/// <param name="imagePullPolicy">The image pull policy.</param>
/// <param name="name">The name.</param>
/// <param name="hostname">The hostname.</param>
/// <param name="macAddress">The MAC address.</param>
/// <param name="workingDirectory">The working directory.</param>
/// <param name="entrypoint">The entrypoint.</param>
/// <param name="command">The command.</param>
Expand All @@ -55,6 +56,7 @@ public TestcontainersConfiguration(
Func<ImagesListResponse, bool> imagePullPolicy = null,
string name = null,
string hostname = null,
string macAddress = null,
string workingDirectory = null,
IEnumerable<string> entrypoint = null,
IEnumerable<string> command = null,
Expand All @@ -80,6 +82,7 @@ public TestcontainersConfiguration(
this.ImagePullPolicy = imagePullPolicy;
this.Name = name;
this.Hostname = hostname;
this.MacAddress = macAddress;
this.WorkingDirectory = workingDirectory;
this.Entrypoint = entrypoint;
this.Command = command;
Expand Down Expand Up @@ -118,6 +121,9 @@ public TestcontainersConfiguration(
/// <inheritdoc />
public string Hostname { get; }

/// <inheritdoc />
public string MacAddress { get; }
seb1992 marked this conversation as resolved.
Show resolved Hide resolved

/// <inheritdoc />
public string WorkingDirectory { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ public async Task Hostname()
}
}

[Fact]
public async Task MacAddress()
{
// Given
const string macAddress = "92:95:5e:30:fe:6d";

// When
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("alpine")
.WithMacAddress(macAddress)
.WithEntrypoint(KeepTestcontainersUpAndRunning.Command);

// Then
await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build())
{
await testcontainer.StartAsync();
Assert.Equal(macAddress, testcontainer.MacAddress);
}
}

[Fact]
public async Task WorkingDirectory()
{
Expand Down