-
-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#242] #IMPLEMENT 'assemblyName: DotNet.Testcontainers; function: Res…
…ourceReaper' {Add ResourceReaper.}
- Loading branch information
Showing
26 changed files
with
664 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"sdk": { | ||
"version": "5.0.100", | ||
"version": "6.0.100", | ||
"rollForward": "latestPatch" | ||
} | ||
} |
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
118 changes: 118 additions & 0 deletions
118
src/DotNet.Testcontainers.Tests/Unit/Services/ResourceReaperTest.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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
namespace DotNet.Testcontainers.Tests.Unit.Services | ||
{ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using Networks.Builders; | ||
using Testcontainers.Containers.Builders; | ||
using Testcontainers.Containers.Modules; | ||
using Testcontainers.Images.Builders; | ||
using Testcontainers.Services; | ||
using Xunit; | ||
|
||
public class ResourceReaperTest | ||
{ | ||
[Fact] | ||
public async Task ShouldReapContainerWhenDisposingResourceReaper() | ||
{ | ||
// Given | ||
await using (var resourceReaper = await ResourceReaper.StartNew()) | ||
{ | ||
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>() | ||
.WithResourceReaperSessionId(resourceReaper.SessionId) | ||
.WithImage("alpine") | ||
.WithCommand("/bin/sh", "-c", "tail -f /dev/null"); | ||
|
||
await using (var testcontainer = testcontainersBuilder.Build()) | ||
{ | ||
await testcontainer.StartAsync(); | ||
var containerId = testcontainer.Id; | ||
|
||
// When | ||
await resourceReaper.DisposeAsync(); | ||
|
||
// Then | ||
var dockerPsStdOut = await DockerPsAqNoTrunc(); | ||
|
||
Assert.DoesNotContain(containerId, dockerPsStdOut); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldReapImageWhenDisposingResourceReaper() | ||
{ | ||
var imageName = $"testimage_{Guid.NewGuid().ToString("D")}"; | ||
|
||
// Given | ||
await using (var resourceReaper = await ResourceReaper.StartNew()) | ||
{ | ||
await new ImageFromDockerfileBuilder() | ||
.WithResourceReaperSessionId(resourceReaper.SessionId) | ||
.WithName(imageName) | ||
.WithDockerfileDirectory("Assets") | ||
.Build(); | ||
|
||
Assert.Contains(imageName, await DockerImages()); | ||
|
||
// When | ||
await resourceReaper.DisposeAsync(); | ||
|
||
// Then | ||
Assert.DoesNotContain(imageName, await DockerImages()); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldReapNetworkWhenDisposingResourceReaper() | ||
{ | ||
var networkName = $"testnetwork_{Guid.NewGuid().ToString("D")}"; | ||
|
||
// Given | ||
await using (var resourceReaper = await ResourceReaper.StartNew()) | ||
{ | ||
await new TestcontainersNetworkBuilder() | ||
.WithResourceReaperSessionId(resourceReaper.SessionId) | ||
.WithName(networkName) | ||
.Build() | ||
.CreateAsync(); | ||
|
||
Assert.Contains(networkName, await DockerNetworks()); | ||
|
||
await resourceReaper.DisposeAsync(); | ||
|
||
Assert.DoesNotContain(networkName, await DockerNetworks()); | ||
} | ||
} | ||
|
||
private static async Task<string> DockerPsAqNoTrunc() | ||
{ | ||
using var dockerPs = new Process { StartInfo = { FileName = "docker", Arguments = "images -aq --no-trunc" } }; | ||
dockerPs.StartInfo.RedirectStandardOutput = true; | ||
Assert.True(dockerPs.Start()); | ||
|
||
var dockerPsStdOut = await dockerPs.StandardOutput.ReadToEndAsync(); | ||
return dockerPsStdOut; | ||
} | ||
|
||
private static async Task<string> DockerImages() | ||
{ | ||
using var dockerImages = new Process { StartInfo = { FileName = "docker", Arguments = "images" } }; | ||
dockerImages.StartInfo.RedirectStandardOutput = true; | ||
Assert.True(dockerImages.Start()); | ||
|
||
var dockerImagesStdOut = await dockerImages.StandardOutput.ReadToEndAsync(); | ||
return dockerImagesStdOut; | ||
} | ||
|
||
private static async Task<string> DockerNetworks() | ||
{ | ||
using var dockerNetworks = new Process { StartInfo = { FileName = "docker", Arguments = "network ls" } }; | ||
dockerNetworks.StartInfo.RedirectStandardOutput = true; | ||
Assert.True(dockerNetworks.Start()); | ||
|
||
var dockerNetworksStdOut = await dockerNetworks.StandardOutput.ReadToEndAsync(); | ||
return dockerNetworksStdOut; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.