-
-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Invoke create parameter modifier for image, network, volume bui…
…ld or create
- Loading branch information
1 parent
3eec988
commit cb9b976
Showing
10 changed files
with
139 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 76 additions & 22 deletions
98
tests/Testcontainers.Tests/Unit/Volumes/TestcontainersVolumeBuilderTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,100 @@ | ||
namespace DotNet.Testcontainers.Tests.Unit | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using DotNet.Testcontainers.Builders; | ||
using DotNet.Testcontainers.Clients; | ||
using DotNet.Testcontainers.Configurations; | ||
using DotNet.Testcontainers.Containers; | ||
using DotNet.Testcontainers.Volumes; | ||
using JetBrains.Annotations; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Xunit; | ||
|
||
public sealed class TestcontainersVolumeBuilderTest | ||
public sealed class TestcontainersVolumeBuilderTest : IClassFixture<TestcontainersVolumeBuilderTest.DockerVolume> | ||
{ | ||
private static readonly string VolumeName = Guid.NewGuid().ToString("D"); | ||
|
||
private static readonly KeyValuePair<string, string> Label = new KeyValuePair<string, string>(TestcontainersClient.TestcontainersLabel + ".volume.test", Guid.NewGuid().ToString("D")); | ||
|
||
private static readonly KeyValuePair<string, string> ParameterModifier = new KeyValuePair<string, string>(TestcontainersClient.TestcontainersLabel + ".parameter.modifier", Guid.NewGuid().ToString("D")); | ||
|
||
private readonly IDockerVolume volume; | ||
|
||
public TestcontainersVolumeBuilderTest(DockerVolume volume) | ||
{ | ||
this.volume = volume; | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldCreateVolume() | ||
public void GetIdOrNameThrowsInvalidOperationException() | ||
{ | ||
// Given | ||
var volumeName = Guid.NewGuid().ToString(); | ||
var noSuchVolume = new TestcontainersVolumeBuilder() | ||
.WithName(VolumeName) | ||
.Build(); | ||
|
||
var volumeLabel = Guid.NewGuid().ToString(); | ||
Assert.Throws<InvalidOperationException>(() => noSuchVolume.Name); | ||
} | ||
|
||
[Fact] | ||
public void GetNameReturnsVolumeName() | ||
{ | ||
Assert.Equal(VolumeName, this.volume.Name); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateVolumeAssignsLabels() | ||
{ | ||
// Given | ||
IDockerVolumeOperations volumeOperations = new DockerVolumeOperations(ResourceReaper.DefaultSessionId, TestcontainersSettings.OS.DockerEndpointAuthConfig, NullLogger.Instance); | ||
|
||
// When | ||
var volume = new TestcontainersVolumeBuilder() | ||
.WithName(volumeName) | ||
.WithLabel("label", volumeLabel) | ||
var volumeResponse = await volumeOperations.ByNameAsync(this.volume.Name) | ||
.ConfigureAwait(false); | ||
|
||
// Then | ||
Assert.Equal(Label.Value, Assert.Contains(Label.Key, volumeResponse.Labels)); | ||
Assert.Equal(ParameterModifier.Value, Assert.Contains(ParameterModifier.Key, volumeResponse.Labels)); | ||
} | ||
|
||
[UsedImplicitly] | ||
public sealed class DockerVolume : IDockerVolume, IAsyncLifetime | ||
{ | ||
private readonly IDockerVolume volume = new TestcontainersVolumeBuilder() | ||
.WithName(VolumeName) | ||
.WithLabel(Label.Key, Label.Value) | ||
.WithCreateParameterModifier(parameterModifier => parameterModifier.Labels.Add(ParameterModifier.Key, ParameterModifier.Value)) | ||
.Build(); | ||
|
||
await volume.CreateAsync(); | ||
public string Name | ||
{ | ||
get | ||
{ | ||
return this.volume.Name; | ||
} | ||
} | ||
|
||
// Then | ||
try | ||
public Task InitializeAsync() | ||
{ | ||
Assert.Equal(volumeName, volume.Name); | ||
return this.CreateAsync(); | ||
} | ||
finally | ||
|
||
public Task DisposeAsync() | ||
{ | ||
await volume.DeleteAsync(); | ||
return this.DeleteAsync(); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ShouldThrowInvalidOperationException() | ||
{ | ||
Assert.Throws<InvalidOperationException>(() => new TestcontainersVolumeBuilder() | ||
.WithName(Guid.Empty.ToString()) | ||
.Build() | ||
.Name); | ||
public Task CreateAsync(CancellationToken ct = default) | ||
{ | ||
return this.volume.CreateAsync(ct); | ||
} | ||
|
||
public Task DeleteAsync(CancellationToken ct = default) | ||
{ | ||
return this.volume.DeleteAsync(ct); | ||
} | ||
} | ||
} | ||
} |