-
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kafka): Add network support for Kafka container
- Loading branch information
1 parent
0d86bda
commit 0e7bb9d
Showing
5 changed files
with
133 additions
and
7 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
60 changes: 60 additions & 0 deletions
60
tests/Testcontainers.Kafka.Tests/KafkaContainerNetworkTest.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,60 @@ | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using DotNet.Testcontainers.Builders; | ||
using DotNet.Testcontainers.Containers; | ||
using DotNet.Testcontainers.Networks; | ||
|
||
namespace Testcontainers.Kafka; | ||
|
||
public sealed class KafkaContainerNetworkTest : IAsyncLifetime | ||
{ | ||
private INetwork _network; | ||
private KafkaContainer _kafkaContainer; | ||
|
||
private IContainer _kCatContainer; | ||
public async Task InitializeAsync() | ||
{ | ||
_network = new NetworkBuilder().Build(); | ||
_kafkaContainer = new KafkaBuilder() | ||
.WithImage("confluentinc/cp-kafka") | ||
.WithNetwork(_network) | ||
.WithListener("kafka:19092") | ||
.Build(); | ||
|
||
_kCatContainer = new ContainerBuilder() | ||
.WithImage("confluentinc/cp-kcat") | ||
.WithNetwork(_network) | ||
.WithCommand("-c", "tail -f /dev/null") | ||
.WithEntrypoint("sh") | ||
.WithResourceMapping(Encoding.Default.GetBytes("Message produced by kcat"), "/data/msgs.txt") | ||
.Build(); | ||
|
||
await _kCatContainer.StartAsync(); | ||
await _kafkaContainer.StartAsync(); | ||
} | ||
|
||
public Task DisposeAsync() | ||
{ | ||
return _kafkaContainer.DisposeAsync().AsTask(); | ||
} | ||
|
||
[Fact] | ||
public async Task TestUsageWithListener() | ||
{ | ||
// kcat producer | ||
await _kCatContainer.ExecAsync(new List<string>() | ||
{ | ||
"kcat", "-b", "kafka:19092", "-t", "msgs", "-P", "-l", "/data/msgs.txt" | ||
}); | ||
|
||
|
||
// kcat consumer | ||
var kCatResult = await _kCatContainer.ExecAsync(new List<string>() | ||
{ | ||
"kcat", "-b", "kafka:19092", "-C", "-t", "msgs", "-c", "1" | ||
}); | ||
|
||
Assert.Contains("Message produced by kcat", kCatResult.Stdout); | ||
} | ||
|
||
} |
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