You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api/resource_reuse.md
+11Lines changed: 11 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,6 +15,17 @@ _ = new ContainerBuilder("alpine:3.20.0")
15
15
.WithLabel("reuse-id", "WeatherForecast");
16
16
```
17
17
18
+
To take full control over the hash value, pass a custom function to the `WithReuse` overload. The function receives the resource configuration and returns the hash string used to identify and match the resource.
19
+
20
+
```csharp title="Override the reuse hash calculation"
21
+
_=newContainerBuilder("alpine:3.20.0")
22
+
.WithReuse(_=>"custom-hash");
23
+
```
24
+
25
+
!!! warning
26
+
27
+
A matching custom hash does not guarantee that the resource is compatible. The default implementation derives the hash from the builder configuration, so a match implies an identical configuration. With a custom hash, it is the caller's responsibility to ensure that the hash uniquely and accurately reflects the intended configuration.
28
+
18
29
The current implementation considers the following resource configurations and their corresponding builder APIs when calculating the hash value.
@@ -80,6 +80,26 @@ public interface IAbstractBuilder<out TBuilderEntity, out TResourceEntity, out T
80
80
[PublicAPI]
81
81
TBuilderEntityWithReuse(boolreuse);
82
82
83
+
/// <summary>
84
+
/// Reuses an existing Docker resource with a custom reuse hash.
85
+
/// </summary>
86
+
/// <remarks>
87
+
/// If reuse is enabled, Testcontainers will label the resource with a hash value
88
+
/// according to the respective build/resource configuration. When Testcontainers finds a
89
+
/// matching resource, it will reuse this resource instead of creating a new one. Enabling
90
+
/// reuse will disable the resource reaper, meaning the resource will not be cleaned up
91
+
/// after the tests are finished.
92
+
///
93
+
/// This is an <b>experimental</b> feature. Reuse does not take all builder
94
+
/// configurations into account when calculating the hash value. There might be configurations
95
+
/// where Testcontainers is not, or not yet, able to find a matching resource and
96
+
/// recreate the resource.
97
+
/// </remarks>
98
+
/// <param name="reuseHashProvider">A function that receives the resource configuration and returns the reuse hash, replacing the default SHA-1-based implementation.</param>
99
+
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
Copy file name to clipboardExpand all lines: src/Testcontainers/Configurations/Commons/ResourceConfiguration.cs
+13Lines changed: 13 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -21,19 +21,22 @@ public class ResourceConfiguration<TCreateResourceEntity> : IResourceConfigurati
21
21
/// <param name="labels">The test session id.</param>
22
22
/// <param name="parameterModifiers">A list of low level modifications of the Docker.DotNet entity.</param>
23
23
/// <param name="reuse">A value indicating whether to reuse an existing resource configuration or not.</param>
24
+
/// <param name="reuseHashProvider">A function to override the reuse hash calculation, or <c>null</c> to use the default SHA-1-based implementation.</param>
0 commit comments