Skip to content

Commit

Permalink
chore: Replace codeinclude with snippets (#1251)
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn authored Sep 2, 2024
1 parent 2da2080 commit 5b3d494
Show file tree
Hide file tree
Showing 32 changed files with 175 additions and 127 deletions.
21 changes: 21 additions & 0 deletions docs/api/create_docker_container.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ using var timeoutCts = new CancellationTokenSource(TimeSpan.FromMinutes(1));
await _container.StartAsync(timeoutCts.Token);
```

## Getting log messages

Testcontainers for .NET provides two approaches for retrieving log messages from containers: `GetLogsAsync` and `WithOutputConsumer`. Each method serves different use cases for handling container logs.

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.

```csharp title="Getting all log messages"
var (stdout, stderr) = await _container.GetLogsAsync();
```

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.

```csharp title="Forwarding all log messages"
using IOutputConsumer outputConsumer = Consume.RedirectStdoutAndStderrToConsole();

_ = new ContainerBuilder()
.WithOutputConsumer(outputConsumer);
```

The static class `Consume` offers pre-configured implementations of the `IOutputConsumer` interface for common use cases. If you need additional functionalities beyond those provided by the default implementations, you can create your own implementations of `IOutputConsumer`.

## Examples

An NGINX container that binds the HTTP port to a random host port and hosts static content. The example connects to the web server and checks the HTTP status code.
Expand Down
24 changes: 15 additions & 9 deletions docs/custom_configuration/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,29 @@ Testcontainers supports various configurations to set up your test environment.
| `ryuk.container.privileged` | `TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED` | Runs Ryuk (resource reaper) in privileged mode. | `false` |
| `ryuk.container.image` | `TESTCONTAINERS_RYUK_CONTAINER_IMAGE` | The Ryuk (resource reaper) Docker image. | `testcontainers/ryuk:0.5.1` |
| `hub.image.name.prefix` | `TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX` | The name to use for substituting the Docker Hub registry part of the image name. | - |
<!-- | `wait.strategy.retries` | `TESTCONTAINERS_WAIT_STRATEGY_RETRIES` | The wait strategy retry count. | `infinite` |
| `wait.strategy.retries` | `TESTCONTAINERS_WAIT_STRATEGY_RETRIES` | The wait strategy retry count. | `infinite` |
| `wait.strategy.interval` | `TESTCONTAINERS_WAIT_STRATEGY_INTERVAL` | The wait strategy interval<sup>1</sup>. | `00:00:01` |
| `wait.strategy.timeout` | `TESTCONTAINERS_WAIT_STRATEGY_TIMEOUT` | The wait strategy timeout<sup>1</sup>. | `01:00:00` |

1) The value represent the string representation of a [TimeSpan](https://learn.microsoft.com/en-us/dotnet/api/system.timespan), for example, `00:00:01` for 1 second. -->
1) The value represent the string representation of a [TimeSpan](https://learn.microsoft.com/en-us/dotnet/api/system.timespan), for example, `00:00:01` for 1 second.

## Configure remote container runtime

To configure a remote container runtime, Testcontainers provides support for Docker's environment variables in addition to the properties file. During initialization, Testcontainers' auto-discovery feature detect and apply custom configurations including container runtimes. If you are running Docker on a remote host, you can configure it using either of the following methods:

```console title="Properties File"
docker.host=tcp://docker:2375
```
=== "Environment Variable"
```
DOCKER_HOST=tcp://docker:2375
```

```console title="Environment Variable"
DOCKER_HOST=tcp://docker:2375
```
=== "Properties File"
```
docker.host=tcp://docker:2375
```

## Enable logging

In .NET logging usually goes through the test framework. Testcontainers is not aware of the project's test framework and may not forward log messages to the appropriate output stream. The default implementation forwards log messages to the `Console` (respectively `stdout` and `stderr`). The output should at least pop up in the IDE running tests in the `Debug` configuration. To override the default implementation, set the `TestcontainersSettings.Logger` property to an instance of an `ILogger` implementation before creating a Docker resource, such as a container.
In .NET logging usually goes through the test framework. Testcontainers is not aware of the project's test framework and may not forward log messages to the appropriate output stream. The default implementation forwards log messages to the `Console` (respectively `stdout` and `stderr`). The output should at least pop up in the IDE running tests in the `Debug` configuration. To override the default implementation, use the builder's `WithLogger(ILogger)` method and provide an `ILogger` instance to replace the default console logger.

[testcontainers.org 00:00:00.34] Connected to Docker:
Host: tcp://127.0.0.1:60706/
Expand All @@ -58,6 +60,10 @@ In .NET logging usually goes through the test framework. Testcontainers is not a
[testcontainers.org 00:00:06.26] Start Docker container 027af397344d08d5fc174bf5b5d449f6b352a8a506306d3d96390aaa2bb0445d
[testcontainers.org 00:00:06.64] Delete Docker container 027af397344d08d5fc174bf5b5d449f6b352a8a506306d3d96390aaa2bb0445d

!!!tip

These log messages are from the Testcontainers library and contain information about the test resources. They do not include log messages from the containers. To get the container log messages, see: [Getting log messages](https://dotnet.testcontainers.org/api/create_docker_container/#getting-log-messages).

To enable debug log messages in the default implementation, set the property `ConsoleLogger.Instance.DebugLogLevelEnabled` to `true`. This will forward messages related to building or pulling Docker images to the output stream.

[properties-file-format]: https://en.wikipedia.org/wiki/.properties
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/aspnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ The following example adds tests to an ASP.NET Core Blazor application. The test

To use Testcontainers' pre-configured Microsoft SQL Server module, add the [Testcontainers.MsSql](https://www.nuget.org/packages/Testcontainers.MsSql) NuGet dependency to your test project:

```console
```shell
dotnet add package Testcontainers.MsSql --version 3.0.0
```

Expand Down
4 changes: 2 additions & 2 deletions docs/examples/dind.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

If you choose to run your tests in a Docker Wormhole configuration, which involves using sibling containers, it is necessary to mount Docker's raw socket `/var/run/docker.sock.raw`. You find more information and an explanation of the Docker bug in this [comment](https://github.com/docker/for-mac/issues/5588#issuecomment-934600089).

```console
```shell
docker run -v /var/run/docker.sock.raw:/var/run/docker.sock $IMAGE dotnet test
```

Expand All @@ -29,7 +29,7 @@ services:
entrypoint: dotnet
command: test
# Uncomment the lines below in the case of Docker Desktop (see note above).
# TESTCONTAINERS_HOST_OVERRIDE is not needed in the case of Docker Engine.
# TESTCONTAINERS_HOST_OVERRIDE is not needed in the case of Docker Engine.
# environment:
# - TESTCONTAINERS_HOST_OVERRIDE=host.docker.internal
volumes:
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Welcome to Testcontainers for .NET!

```console title="Install the NuGet dependency"
```shell title="Install the NuGet dependency"
dotnet add package Testcontainers
```

Expand Down
File renamed without changes.
18 changes: 10 additions & 8 deletions docs/modules/elasticsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@

Add the following dependency to your project file:

```console title="NuGet"
```shell title="NuGet"
dotnet add package Testcontainers.Elasticsearch
```

You can start an Elasticsearch container instance from any .NET application. This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.

<!--codeinclude-->
[Usage Example](../../tests/Testcontainers.Elasticsearch.Tests/ElasticsearchContainerTest.cs) inside_block:UseElasticsearchContainer
<!--/codeinclude-->
=== "Usage Example"
```csharp
--8<-- "tests/Testcontainers.Elasticsearch.Tests/ElasticsearchContainerTest.cs:UseElasticsearchContainer"
```

The test example uses the following NuGet dependencies:

<!--codeinclude-->
[Package References](../../tests/Testcontainers.Elasticsearch.Tests/Testcontainers.Elasticsearch.Tests.csproj) inside_block:PackageReferences
<!--/codeinclude-->
=== "Package References"
```xml
--8<-- "tests/Testcontainers.Elasticsearch.Tests/Testcontainers.Elasticsearch.Tests.csproj:PackageReferences"
```

To execute the tests, use the command `dotnet test` from a terminal.

--8<-- "docs/modules/_call_out_test_projects.md"
--8<-- "docs/modules/_call_out_test_projects.txt"

## A Note To Developers

Expand Down
6 changes: 3 additions & 3 deletions docs/modules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Modules are great examples of Testcontainers' capabilities. To write tests again

Modules are standalone dependencies that can be installed from [NuGet.org](https://www.nuget.org/profiles/Testcontainers). To use a module in your test project, you need to add it as a dependency first:

```console
```shell
dotnet add package Testcontainers.ModuleName
```

Expand Down Expand Up @@ -69,15 +69,15 @@ await moduleNameContainer.StartAsync();

The Testcontainers for .NET repository contains a .NET [template](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Templates) to scaffold advanced modules quickly. To create and add a new module to the Testcontainers solution file, checkout the repository and install the .NET template first:

```console
```shell
git clone --branch develop [email protected]:testcontainers/testcontainers-dotnet.git
cd ./testcontainers-dotnet/
dotnet new --install ./src/Templates
```

The following CLI commands create and add a new PostgreSQL module to the solution file:

```console
```shell
dotnet new tcm --name PostgreSql --official-module true --output ./src
dotnet sln add ./src/Testcontainers.PostgreSql/Testcontainers.PostgreSql.csproj
```
Expand Down
32 changes: 18 additions & 14 deletions docs/modules/mongodb.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,40 @@

Add the following dependency to your project file:

```console title="NuGet"
```shell title="NuGet"
dotnet add package Testcontainers.MongoDb
```

You can start a MongoDB container instance from any .NET application. Here, we create different container instances and pass them to the base test class. This allows us to test different configurations.

<!--codeinclude-->
[Create Container Instance](../../tests/Testcontainers.MongoDb.Tests/MongoDbContainerTest.cs) inside_block:CreateMongoDbContainer
<!--/codeinclude-->
=== "Create Container Instance"
```csharp
--8<-- "tests/Testcontainers.MongoDb.Tests/MongoDbContainerTest.cs:CreateMongoDbContainer"
```

This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.

<!--codeinclude-->
[Usage Example](../../tests/Testcontainers.MongoDb.Tests/MongoDbContainerTest.cs) inside_block:UseMongoDbContainer
<!--/codeinclude-->
=== "Usage Example"
```csharp
--8<-- "tests/Testcontainers.MongoDb.Tests/MongoDbContainerTest.cs:UseMongoDbContainer"
```

The test example uses the following NuGet dependencies:

<!--codeinclude-->
[Package References](../../tests/Testcontainers.MongoDb.Tests/Testcontainers.MongoDb.Tests.csproj) inside_block:PackageReferences
<!--/codeinclude-->
=== "Package References"
```xml
--8<-- "tests/Testcontainers.MongoDb.Tests/Testcontainers.MongoDb.Tests.csproj:PackageReferences"
```

To execute the tests, use the command `dotnet test` from a terminal.

--8<-- "docs/modules/_call_out_test_projects.md"
--8<-- "docs/modules/_call_out_test_projects.txt"

## MongoDb Replica Set

By default, MongoDB runs as a standalone instance. If your tests require a MongoDB replica set, use the following configuration which will initialize a single-node replica set:

<!--codeinclude-->
[Replica Set Configuration](../../tests/Testcontainers.MongoDb.Tests/MongoDbContainerTest.cs) inside_block:ReplicaSetContainerConfiguration
<!--/codeinclude-->
=== "Replica Set Configuration"
```csharp
--8<-- "tests/Testcontainers.MongoDb.Tests/MongoDbContainerTest.cs:ReplicaSetContainerConfiguration"
```
25 changes: 14 additions & 11 deletions docs/modules/mssql.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,34 @@

Add the following dependency to your project file:

```console title="NuGet"
```shell title="NuGet"
dotnet add package Testcontainers.MsSql
```

You can start a MSSQL container instance from any .NET application. Here, we create different container instances and pass them to the base test class. This allows us to test different configurations.

<!--codeinclude-->
[Create Container Instance](../../tests/Testcontainers.MsSql.Tests/MsSqlContainerTest.cs) inside_block:CreateMsSqlContainer
<!--/codeinclude-->
=== "Create Container Instance"
```csharp
--8<-- "tests/Testcontainers.MsSql.Tests/MsSqlContainerTest.cs:CreateMsSqlContainer"
```

This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.

<!--codeinclude-->
[Usage Example](../../tests/Testcontainers.MsSql.Tests/MsSqlContainerTest.cs) inside_block:UseMsSqlContainer
<!--/codeinclude-->
=== "Usage Example"
```csharp
--8<-- "tests/Testcontainers.MsSql.Tests/MsSqlContainerTest.cs:UseMsSqlContainer"
```

The test example uses the following NuGet dependencies:

<!--codeinclude-->
[Package References](../../tests/Testcontainers.MsSql.Tests/Testcontainers.MsSql.Tests.csproj) inside_block:PackageReferences
<!--/codeinclude-->
=== "Package References"
```xml
--8<-- "tests/Testcontainers.MsSql.Tests/Testcontainers.MsSql.Tests.csproj:PackageReferences"
```

To execute the tests, use the command `dotnet test` from a terminal.

--8<-- "docs/modules/_call_out_test_projects.md"
--8<-- "docs/modules/_call_out_test_projects.txt"

## A Note To Developers

Expand Down
18 changes: 10 additions & 8 deletions docs/modules/neo4j.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@

Add the following dependency to your project file:

```console title="NuGet"
```shell title="NuGet"
dotnet add package Testcontainers.Neo4j
```

You can start an Neo4j container instance from any .NET application. This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.

<!--codeinclude-->
[Usage Example](../../tests/Testcontainers.Neo4j.Tests/Neo4jContainerTest.cs) inside_block:UseNeo4jContainer
<!--/codeinclude-->
=== "Usage Example"
```csharp
--8<-- "tests/Testcontainers.Neo4j.Tests/Neo4jContainerTest.cs:UseNeo4jContainer"
```

The test example uses the following NuGet dependencies:

<!--codeinclude-->
[Package References](../../tests/Testcontainers.Neo4j.Tests/Testcontainers.Neo4j.Tests.csproj) inside_block:PackageReferences
<!--/codeinclude-->
=== "Package References"
```xml
--8<-- "tests/Testcontainers.Neo4j.Tests/Testcontainers.Neo4j.Tests.csproj:PackageReferences"
```

To execute the tests, use the command `dotnet test` from a terminal.

--8<-- "docs/modules/_call_out_test_projects.md"
--8<-- "docs/modules/_call_out_test_projects.txt"
18 changes: 10 additions & 8 deletions docs/modules/postgres.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@

Add the following dependency to your project file:

```console title="NuGet"
```shell title="NuGet"
dotnet add package Testcontainers.PostgreSql
```

You can start an PostgreSQL container instance from any .NET application. This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.

<!--codeinclude-->
[Usage Example](../../tests/Testcontainers.PostgreSql.Tests/PostgreSqlContainerTest.cs) inside_block:UsePostgreSqlContainer
<!--/codeinclude-->
=== "Usage Example"
```csharp
--8<-- "tests/Testcontainers.PostgreSql.Tests/PostgreSqlContainerTest.cs:UsePostgreSqlContainer"
```

The test example uses the following NuGet dependencies:

<!--codeinclude-->
[Package References](../../tests/Testcontainers.PostgreSql.Tests/Testcontainers.PostgreSql.Tests.csproj) inside_block:PackageReferences
<!--/codeinclude-->
=== "Package References"
```xml
--8<-- "tests/Testcontainers.PostgreSql.Tests/Testcontainers.PostgreSql.Tests.csproj:PackageReferences"
```

To execute the tests, use the command `dotnet test` from a terminal.

--8<-- "docs/modules/_call_out_test_projects.md"
--8<-- "docs/modules/_call_out_test_projects.txt"
25 changes: 14 additions & 11 deletions docs/modules/pulsar.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,34 @@

Add the following dependency to your project file:

```console title="NuGet"
```shell title="NuGet"
dotnet add package Testcontainers.Pulsar
```

You can start a Apache Pulsar container instance from any .NET application. Here, we create different container instances and pass them to the base test class. This allows us to test different configurations.

<!--codeinclude-->
[Create Container Instance](../../tests/Testcontainers.Pulsar.Tests/PulsarContainerTest.cs) inside_block:CreatePulsarContainer
<!--/codeinclude-->
=== "Create Container Instance"
```csharp
--8<-- "tests/Testcontainers.Pulsar.Tests/PulsarContainerTest.cs:CreatePulsarContainer"
```

This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.

<!--codeinclude-->
[Usage Example](../../tests/Testcontainers.Pulsar.Tests/PulsarContainerTest.cs) inside_block:UsePulsarContainer
<!--/codeinclude-->
=== "Usage Example"
```csharp
--8<-- "tests/Testcontainers.Pulsar.Tests/PulsarContainerTest.cs:UsePulsarContainer"
```

The test example uses the following NuGet dependencies:

<!--codeinclude-->
[Package References](../../tests/Testcontainers.Pulsar.Tests/Testcontainers.Pulsar.Tests.csproj) inside_block:PackageReferences
<!--/codeinclude-->
=== "Package References"
```xml
--8<-- "tests/Testcontainers.Pulsar.Tests/Testcontainers.Pulsar.Tests.csproj:PackageReferences"
```

To execute the tests, use the command `dotnet test` from a terminal.

--8<-- "docs/modules/_call_out_test_projects.md"
--8<-- "docs/modules/_call_out_test_projects.txt"

## Access Apache Pulsar

Expand Down
Loading

0 comments on commit 5b3d494

Please sign in to comment.