Skip to content

Commit

Permalink
Add reuse hash label via CreateParameterModifier
Browse files Browse the repository at this point in the history
  • Loading branch information
david-szabo97 committed Nov 24, 2023
1 parent 7c194b3 commit 7994956
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
12 changes: 9 additions & 3 deletions src/Testcontainers/Builders/AbstractBuilder`4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ public TBuilderEntity WithCleanUp(bool cleanUp)
/// <inheritdoc />
public TBuilderEntity WithReuse(bool reuse)
{
return Clone(new ResourceConfiguration<TCreateResourceEntity>(reuse: reuse, labels: new Dictionary<string, string> { { TestcontainersClient.TestcontainersReuseHashLabel, DockerResourceConfiguration.GetHash() } }));
return Clone(new ResourceConfiguration<TCreateResourceEntity>(reuse: reuse, parameterModifiers: new List<Action<TCreateResourceEntity>>()
{
parameter => {
var labelsProperty = parameter.GetType().GetProperty("Labels");
var labels = (IDictionary<string, string>)labelsProperty.GetValue(parameter);
labels[TestcontainersClient.TestcontainersReuseHashLabel] = DockerResourceConfiguration.GetHash();
}
})
);
}

/// <inheritdoc />
Expand Down Expand Up @@ -137,8 +145,6 @@ protected virtual void Validate()
.ThrowIf(argument => argument.Value == null, argument => new ArgumentException(message, argument.Name));

// TODO: Validate WithReuse(), WithAutoRemove() and WithCleanUp() combinations.
Guard.Argument(DockerResourceConfiguration.Reuse, nameof(IResourceConfiguration<TCreateResourceEntity>.Reuse))
.ThrowIf(argument => DockerResourceConfiguration.Labels.ContainsKey(TestcontainersClient.TestcontainersReuseHashLabel) && DockerResourceConfiguration.Labels[TestcontainersClient.TestcontainersReuseHashLabel] != DockerResourceConfiguration.GetHash(), argument => new ArgumentException("ResoureConfiguration hash mismatch, WithReuse(true) must be the last called builder method", argument.Name));
}

/// <summary>
Expand Down
14 changes: 0 additions & 14 deletions tests/Testcontainers.Platform.Linux.Tests/WithReuseTest.cs

This file was deleted.

0 comments on commit 7994956

Please sign in to comment.