Skip to content

Commit 7994956

Browse files
committed
Add reuse hash label via CreateParameterModifier
1 parent 7c194b3 commit 7994956

File tree

2 files changed

+9
-17
lines changed

2 files changed

+9
-17
lines changed

src/Testcontainers/Builders/AbstractBuilder`4.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,15 @@ public TBuilderEntity WithCleanUp(bool cleanUp)
6060
/// <inheritdoc />
6161
public TBuilderEntity WithReuse(bool reuse)
6262
{
63-
return Clone(new ResourceConfiguration<TCreateResourceEntity>(reuse: reuse, labels: new Dictionary<string, string> { { TestcontainersClient.TestcontainersReuseHashLabel, DockerResourceConfiguration.GetHash() } }));
63+
return Clone(new ResourceConfiguration<TCreateResourceEntity>(reuse: reuse, parameterModifiers: new List<Action<TCreateResourceEntity>>()
64+
{
65+
parameter => {
66+
var labelsProperty = parameter.GetType().GetProperty("Labels");
67+
var labels = (IDictionary<string, string>)labelsProperty.GetValue(parameter);
68+
labels[TestcontainersClient.TestcontainersReuseHashLabel] = DockerResourceConfiguration.GetHash();
69+
}
70+
})
71+
);
6472
}
6573

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

139147
// TODO: Validate WithReuse(), WithAutoRemove() and WithCleanUp() combinations.
140-
Guard.Argument(DockerResourceConfiguration.Reuse, nameof(IResourceConfiguration<TCreateResourceEntity>.Reuse))
141-
.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));
142148
}
143149

144150
/// <summary>

tests/Testcontainers.Platform.Linux.Tests/WithReuseTest.cs

-14
This file was deleted.

0 commit comments

Comments
 (0)