Skip to content

Commit 1503c67

Browse files
authored
Merge branch 'develop' into ThrowIfExited
2 parents abea539 + c536f49 commit 1503c67

File tree

71 files changed

+768
-225
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+768
-225
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33
<PropertyGroup>
44
<PackageId>$(AssemblyName)</PackageId>
5-
<Version>4.7.0</Version>
5+
<Version>4.8.0</Version>
66
<AssemblyVersion>$(Version)</AssemblyVersion>
77
<FileVersion>$(Version)</FileVersion>
88
<Product>Testcontainers</Product>

Directory.Packages.props

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
</PropertyGroup>
66
<ItemGroup>
77
<PackageVersion Include="BouncyCastle.Cryptography" Version="2.6.1"/>
8-
<PackageVersion Include="Docker.DotNet.Enhanced.X509" Version="3.128.5"/>
9-
<PackageVersion Include="Docker.DotNet.Enhanced" Version="3.128.5"/>
8+
<PackageVersion Include="Docker.DotNet.Enhanced.X509" Version="3.129.0"/>
9+
<PackageVersion Include="Docker.DotNet.Enhanced" Version="3.129.0"/>
1010
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0"/>
1111
<PackageVersion Include="Microsoft.Bcl.HashCode" Version="1.1.1"/>
1212
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.3"/>
@@ -64,7 +64,7 @@
6464
<PackageVersion Include="InfluxDB.Client" Version="4.18.0"/>
6565
<PackageVersion Include="JanusGraph.Net" Version="1.0.0"/>
6666
<PackageVersion Include="Keycloak.Net.Core" Version="1.0.20"/>
67-
<PackageVersion Include="KubernetesClient" Version="15.0.1"/>
67+
<PackageVersion Include="KubernetesClient" Version="17.0.14"/>
6868
<PackageVersion Include="Microsoft.Azure.Cosmos" Version="3.32.1"/>
6969
<PackageVersion Include="Microsoft.Azure.Kusto.Data" Version="12.2.8"/>
7070
<PackageVersion Include="Microsoft.Data.SqlClient" Version="5.2.2"/>

docs/api/create_docker_container.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,30 @@ _ = new ContainerBuilder()
5858
.WithResourceMapping(Encoding.Default.GetBytes("{}"), "/app/appsettings.json");
5959
```
6060

61+
### Specifying file ownership
62+
63+
When copying files into a container, you can specify the user ID (UID) and group ID (GID) to set the correct ownership of the copied files. This is particularly useful when the container runs as a non-root user or when specific file permissions are required for security or application functionality.
64+
65+
```csharp title="Copying a file with specific UID and GID"
66+
_ = new ContainerBuilder()
67+
.WithResourceMapping(new DirectoryInfo("."), "/app/", uid: 1000, gid: 1000);
68+
```
69+
70+
### Specifying file permission
71+
72+
When copying files into a container, you can specify the file mode to set the correct permissions for the copied files.
73+
74+
```csharp title="Copying a script with executable permissions"
75+
_ = new ContainerBuilder()
76+
.WithResourceMapping(new DirectoryInfo("."), "/app/", fileMode: Unix.FileMode755);
77+
```
78+
79+
The `Unix` class provides common permission configurations like `FileMode755` (read, write, execute for owner; read, execute for group and others). For individual permission combinations, you can use the `UnixFileModes` enumeration to create custom configurations.
80+
81+
### Copying files to a running container
82+
83+
The same UID, GID, and file mode arguments are also available when copying files to already running containers using the `IContainer.CopyAsync(...)` APIs.
84+
6185
## Reading files from the container
6286

6387
The `IContainer` interface offers a `ReadFileAsync(string, CancellationToken)` method to read files from the container. The implementation returns the read bytes. Either process the read bytes in-memory or persist them to the disk.
@@ -76,7 +100,8 @@ Starting a container or creating a resource (such as a network or a volume) can
76100

77101
```csharp title="Canceling container start after one minute"
78102
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromMinutes(1));
79-
await _container.StartAsync(timeoutCts.Token);
103+
await _container.StartAsync(timeoutCts.Token)
104+
.ConfigureAwait(false);
80105
```
81106

82107
## Getting log messages
@@ -86,7 +111,8 @@ Testcontainers for .NET provides two approaches for retrieving log messages from
86111
The `GetLogsAsync` method is available through the `IContainer` interface. It allows you to fetch logs from a container for a specific time range or from the beginning until the present. This approach is useful for retrieving logs after a test has run, especially when troubleshooting issues or failures.
87112

88113
```csharp title="Getting all log messages"
89-
var (stdout, stderr) = await _container.GetLogsAsync();
114+
var (stdout, stderr) = await _container.GetLogsAsync()
115+
.ConfigureAwait(false);
90116
```
91117

92118
The `WithOutputConsumer` method is part of the `ContainerBuilder` class and is used to continuously forward container log messages to a specified output consumer. This approach provides real-time access to logs as the container runs.

docs/api/create_docker_image.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@ await futureImage.CreateAsync()
1616
.ConfigureAwait(false);
1717
```
1818

19+
To build a Docker image with Testcontainers, it's important to understand the build context. Testcontainers needs three things:
20+
21+
1. **Docker build context**: The directory containing files Docker can use during the build
22+
2. **Dockerfile name**: The name of the Dockerfile to use
23+
3. **Dockerfile directory**: Where the Dockerfile is located
24+
1925
!!!tip
2026

21-
The Dockerfile must be part of the build context, otherwise the build fails.
27+
The build context is optional. If you don't specify one, it defaults to the Dockerfile directory.
28+
29+
Testcontainers creates a tarball with all files and subdirectorys in the build context, incl. the Dockerfile. This tarball is sent to the Docker daemon to build the image. The build context acts as the root for all file operations in the Dockerfile, so all paths (like `COPY` commands) must be relative to it.
2230

23-
It is essential to take into account and comprehend the build context to enable Testcontainers to build the Docker image. Testcontainers generates a tarball that contains all the files and subdirectories within the build context. The tarball is passed to the Docker daemon to build the image. The tarball serves as the new root of the Dockerfile's content. Therefore, all paths must be relative to the new root. If your app or service follows to the following project structure, the build context is `/Users/testcontainers/WeatherForecast/`.
31+
For example, if your project looks like this, the build context would be: `/Users/testcontainers/WeatherForecast/`.
2432

2533
/
2634
└── Users/
@@ -61,6 +69,17 @@ RUN dotnet publish $SLN_FILE_PATH --configuration Release --framework net6.0 --o
6169
ENTRYPOINT ["dotnet", "/app/WeatherForecast.dll"]
6270
```
6371

72+
### Choosing a build context
73+
74+
You can use `WithContextDirectory(string)` to set a build context separate from your Dockerfile. This is useful when the Dockerfile is in one directory but the files you want to include are in another.
75+
76+
```csharp
77+
_ = new ImageFromDockerfileBuilder()
78+
.WithContextDirectory("/path/to/build/context")
79+
.WithDockerfile("Dockerfile")
80+
.WithDockerfileDirectory("/path/to/dockerfile/directory");
81+
```
82+
6483
## Delete multi-stage intermediate layers
6584

6685
A multi-stage Docker image build generates intermediate layers that serve as caches. Testcontainers' Resource Reaper is unable to automatically delete these layers after the test execution. The necessary label is not forwarded by the Docker image build. Testcontainers is unable to track the intermediate layers during the test. To delete the intermediate layers after the test execution, pass the Resource Reaper session to each stage.
@@ -92,8 +111,9 @@ _ = new ImageFromDockerfileBuilder()
92111
| `WithCleanUp` | Will remove the image automatically after all tests have been run. |
93112
| `WithLabel` | Applies metadata to the image e.g. `-l`, `--label "testcontainers=awesome"`. |
94113
| `WithName` | Sets the image name e.g. `-t`, `--tag "testcontainers:0.1.0"`. |
114+
| `WithContextDirectory` | Sets the Docker build context directory. |
95115
| `WithDockerfile` | Sets the name of the `Dockerfile`. |
96-
| `WithDockerfileDirectory` | Sets the build context (directory path that contains the `Dockerfile`). |
116+
| `WithDockerfileDirectory` | Sets the directory path that contains the `Dockerfile`. |
97117
| `WithImageBuildPolicy` | Specifies an image build policy to determine when an image is built. |
98118
| `WithDeleteIfExists` | Will remove the image if it already exists. |
99119
| `WithBuildArgument` | Sets build-time variables e.g `--build-arg "MAGIC_NUMBER=42"`. |

docs/modules/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ await moduleNameContainer.StartAsync();
7373
| Typesense | `typesense/typesense:28.0` | [NuGet](https://www.nuget.org/packages/Testcontainers.Typesense) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Typesense) |
7474
| Weaviate | `semitechnologies/weaviate:1.26.14` | [NuGet](https://www.nuget.org/packages/Testcontainers.Weaviate) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Weaviate) |
7575
| WebDriver | `selenium/standalone-chrome:110.0` | [NuGet](https://www.nuget.org/packages/Testcontainers.WebDriver) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.WebDriver) |
76+
| WireMock | - | [NuGet](https://www.nuget.org/packages/WireMock.Net.Testcontainers) | [Source](https://github.com/wiremock/WireMock.Net) |
7677

7778
## Implement a module
7879

examples/Flyway/Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<ItemGroup>
77
<!-- Unit and integration test dependencies: -->
88
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
9-
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.2.0"/>
9+
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.7.0"/>
1010
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2"/>
1111
<PackageVersion Include="xunit" Version="2.9.2"/>
1212
<!-- Third-party client dependencies to connect and interact with the containers: -->

examples/Respawn/Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<ItemGroup>
77
<!-- Unit and integration test dependencies: -->
88
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
9-
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.2.0"/>
9+
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.7.0"/>
1010
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2"/>
1111
<PackageVersion Include="xunit" Version="2.9.2"/>
1212
<!-- Third-party client dependencies to connect and interact with the containers: -->

examples/WeatherForecast/Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<!-- Unit and integration test dependencies: -->
1111
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
1212
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.13"/>
13-
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.2.0"/>
13+
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.7.0"/>
1414
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2"/>
1515
<PackageVersion Include="xunit" Version="2.9.2"/>
1616
<!-- Third-party client dependencies to connect and interact with the containers: -->

examples/WeatherForecast/tests/WeatherForecast.Tests/WeatherForecastContainer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public WeatherForecastContainer()
4040
.WithEnvironment("ASPNETCORE_Kestrel__Certificates__Default__Path", WeatherForecastImage.CertificateFilePath)
4141
.WithEnvironment("ASPNETCORE_Kestrel__Certificates__Default__Password", WeatherForecastImage.CertificatePassword)
4242
.WithEnvironment("ConnectionStrings__PostgreSQL", postgreSqlConnectionString)
43-
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(WeatherForecastImage.HttpsPort))
43+
.WithWaitStrategy(Wait.ForUnixContainer().UntilInternalTcpPortIsAvailable(WeatherForecastImage.HttpsPort))
4444
.Build();
4545
}
4646

src/Testcontainers.Cassandra/CassandraContainer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public async Task<ExecResult> ExecScriptAsync(string scriptContent, Cancellation
3535
{
3636
var scriptFilePath = string.Join("/", string.Empty, "tmp", Guid.NewGuid().ToString("D"), Path.GetRandomFileName());
3737

38-
await CopyAsync(Encoding.Default.GetBytes(scriptContent), scriptFilePath, Unix.FileMode644, ct)
38+
await CopyAsync(Encoding.Default.GetBytes(scriptContent), scriptFilePath, fileMode: Unix.FileMode644, ct: ct)
3939
.ConfigureAwait(false);
4040

4141
return await ExecAsync(new[] { "cqlsh", "--file", scriptFilePath }, ct)

0 commit comments

Comments
 (0)