Skip to content

Commit

Permalink
Added pre-configured database PostgreSql, part of #45.
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Mar 12, 2019
1 parent 8d83d9e commit 95729ac
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 80 deletions.
28 changes: 28 additions & 0 deletions src/DotNet.Testcontainers.Tests/Unit/DatabaseContainerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace DotNet.Testcontainers.Tests.Unit
{
using System.Threading.Tasks;
using DotNet.Testcontainers.Core.Builder;
using DotNet.Testcontainers.Core.Containers.Database;
using Xunit;

public class DatabaseContainerTest
{
[Fact]
public async Task PostgreSqlContainer()
{
var database = string.Empty;

var username = string.Empty;

var password = string.Empty;

var testcontainersBuilder = new TestcontainersBuilder<PostgreSqlContainer>()
.WithDatabase(database, username, password);

using (var testcontainer = testcontainersBuilder.Build())
{
await testcontainer.StartAsync();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace DotNet.Testcontainers.Tests.Unit
using System.Threading.Tasks;
using DotNet.Testcontainers.Clients;
using DotNet.Testcontainers.Core.Builder;
using DotNet.Testcontainers.Core.Containers;
using Xunit;

public class TestcontainersAccessInformationTest
Expand Down Expand Up @@ -39,7 +40,7 @@ public async Task QueryContainerInformationOfRunningContainer()
{
// Given
// When
var testcontainersBuilder = new TestcontainersBuilder()
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("nginx");

// Then
Expand All @@ -58,7 +59,7 @@ public void QueryContainerInformationOfStoppedContainer()
{
// Given
// When
var testcontainersBuilder = new TestcontainersBuilder()
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("nginx");

// Then
Expand Down
21 changes: 11 additions & 10 deletions src/DotNet.Testcontainers.Tests/Unit/TestcontainersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace DotNet.Testcontainers.Tests.Unit
using System.Net;
using System.Threading.Tasks;
using DotNet.Testcontainers.Core.Builder;
using DotNet.Testcontainers.Core.Containers;
using DotNet.Testcontainers.Tests.Fixtures;
using Xunit;
using static LanguageExt.Prelude;
Expand All @@ -20,7 +21,7 @@ public async Task Finalizer()
{
// Given
// When
var testcontainersBuilder = new TestcontainersBuilder()
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("alpine")
.WithLabel("alpine", "latest");

Expand All @@ -35,7 +36,7 @@ public async Task Disposable()
{
// Given
// When
var testcontainersBuilder = new TestcontainersBuilder()
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("alpine");

// Then
Expand All @@ -50,7 +51,7 @@ public async Task GeneratedContainerName()
{
// Given
// When
var testcontainersBuilder = new TestcontainersBuilder()
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("alpine");

// Then
Expand All @@ -68,7 +69,7 @@ public async Task SpecifiedContainerName()
var name = "/alpine";

// When
var testcontainersBuilder = new TestcontainersBuilder()
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("alpine")
.WithName(name);

Expand All @@ -85,7 +86,7 @@ public async Task ExposedPorts()
{
// Given
// When
var testcontainersBuilder = new TestcontainersBuilder()
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("alpine")
.WithExposedPort(80);

Expand All @@ -104,7 +105,7 @@ public void PortBindingsHttpAndHttps()
var https = Tuple(443, 80);

// When
var nginx = new TestcontainersBuilder()
var nginx = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("nginx");

// Then
Expand Down Expand Up @@ -136,7 +137,7 @@ public async Task VolumeAndCommand()
var file = "hostname";

// When
var testcontainersBuilder = new TestcontainersBuilder()
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("nginx")
.WithMount(TempDir, $"/{target}")
.WithCommand("/bin/bash", "-c", $"hostname > /{target}/{file}");
Expand All @@ -161,7 +162,7 @@ public async Task VolumeAndEnvironment()
var dayOfWeek = DateTime.Now.DayOfWeek.ToString();

// When
var testcontainersBuilder = new TestcontainersBuilder()
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("nginx")
.WithMount(TempDir, $"/{target}")
.WithEnvironment("dayOfWeek", dayOfWeek)
Expand All @@ -185,7 +186,7 @@ public async Task OutputConsumer()
using (var output = new DefaultConsumerFixture())
{
// When
var testcontainersBuilder = new TestcontainersBuilder()
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("nginx")
.WithOutputConsumer(output)
.WithCommand("/bin/bash", "-c", "hostname > /dev/stdout && hostname > /dev/stderr");
Expand Down Expand Up @@ -216,7 +217,7 @@ public async Task WaitStrategy()
{
// Given
// When
var testcontainersBuilder = new TestcontainersBuilder()
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("alpine")
.WithWaitStrategy(new WaitStrategyFixture());

Expand Down
79 changes: 41 additions & 38 deletions src/DotNet.Testcontainers/Core/Builder/ITestcontainersBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,146 +1,149 @@
namespace DotNet.Testcontainers.Core.Builder
{
using System;
using DotNet.Testcontainers.Core.Containers;
using DotNet.Testcontainers.Core.Images;
using DotNet.Testcontainers.Diagnostics;

public interface ITestcontainersBuilder
public interface ITestcontainersBuilder<T>
{
ITestcontainersBuilder<T> ConfigureContainer(Action<T> configureContainer);

/// <summary>
/// Sets the Docker image, which is used to create the Testcontainer instances.
/// </summary>
/// <param name="image">Docker image name.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
ITestcontainersBuilder WithImage(string image);
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{T}"/>.</returns>
ITestcontainersBuilder<T> WithImage(string image);

/// <summary>
/// Sets the Docker image, which is used to create the Testcontainer instances.
/// </summary>
/// <param name="image">Docker image instance.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
ITestcontainersBuilder WithImage(IDockerImage image);
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{T}"/>.</returns>
ITestcontainersBuilder<T> WithImage(IDockerImage image);

/// <summary>
/// Sets the name of the Testcontainer.
/// </summary>
/// <param name="name">Testcontainers name.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
ITestcontainersBuilder WithName(string name);
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{T}"/>.</returns>
ITestcontainersBuilder<T> WithName(string name);

/// <summary>
/// Overrides the working directory of the Testcontainer for the instruction sets.
/// </summary>
/// <param name="workingDirectory">Working directory.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
ITestcontainersBuilder WithWorkingDirectory(string workingDirectory);
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{T}"/>.</returns>
ITestcontainersBuilder<T> WithWorkingDirectory(string workingDirectory);

/// <summary>
/// Overrides the entrypoint of the Testcontainer to configure an executable.
/// </summary>
/// <param name="entrypoint">Entrypoint executable.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
ITestcontainersBuilder WithEntrypoint(params string[] entrypoint);
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{T}"/>.</returns>
ITestcontainersBuilder<T> WithEntrypoint(params string[] entrypoint);

/// <summary>
/// Overrides the command of the Testcontainer to provide defaults for an executing.
/// </summary>
/// <param name="command">List of commands, "executable", "param1", "param2" or "param1", "param2".</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
ITestcontainersBuilder WithCommand(params string[] command);
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{T}"/>.</returns>
ITestcontainersBuilder<T> WithCommand(params string[] command);

/// <summary>
/// Exports the environment variable in the Testcontainer.
/// </summary>
/// <param name="name">Environment variable name.</param>
/// <param name="value">Environment variable value.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
ITestcontainersBuilder WithEnvironment(string name, string value);
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{T}"/>.</returns>
ITestcontainersBuilder<T> WithEnvironment(string name, string value);

/// <summary>
/// Adds an user-defined metadata to the Testcontainer.
/// </summary>
/// <param name="name">Label name.</param>
/// <param name="value">Label value.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
ITestcontainersBuilder WithLabel(string name, string value);
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{T}"/>.</returns>
ITestcontainersBuilder<T> WithLabel(string name, string value);

/// <summary>
/// Sets the port of the Testcontainer to expose, without publishing the port to the host system’s interfaces.
/// </summary>
/// <param name="port">Port to expose.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
ITestcontainersBuilder WithExposedPort(int port);
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{T}"/>.</returns>
ITestcontainersBuilder<T> WithExposedPort(int port);

/// <summary>
/// Exposes the port of the Testcontainer, without publishing the port to the host system’s interfaces.
/// </summary>
/// <param name="port">Port to expose.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
ITestcontainersBuilder WithExposedPort(string port);
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{T}"/>.</returns>
ITestcontainersBuilder<T> WithExposedPort(string port);

/// <summary>
/// Binds the port of the Testcontainer to the same port of the host machine.
/// </summary>
/// <param name="port">Port to bind between Testcontainer and host machine.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
ITestcontainersBuilder WithPortBinding(int port);
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{T}"/>.</returns>
ITestcontainersBuilder<T> WithPortBinding(int port);

/// <summary>
/// Binds the port of the Testcontainer to the specified port of the host machine.
/// </summary>
/// <param name="hostPort">Port of the host machine.</param>
/// <param name="containerPort">Port of the Testcontainer.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
ITestcontainersBuilder WithPortBinding(int hostPort, int containerPort);
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{T}"/>.</returns>
ITestcontainersBuilder<T> WithPortBinding(int hostPort, int containerPort);

/// <summary>
/// Binds the port of the Testcontainer to the same port of the host machine.
/// </summary>
/// <param name="port">Port to bind between Testcontainer and host machine.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
ITestcontainersBuilder WithPortBinding(string port);
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{T}"/>.</returns>
ITestcontainersBuilder<T> WithPortBinding(string port);

/// <summary>
/// Binds the port of the Testcontainer to the specified port of the host machine.
/// </summary>
/// <param name="hostPort">Port of the host machine.</param>
/// <param name="containerPort">Port of the Testcontainer.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
ITestcontainersBuilder WithPortBinding(string hostPort, string containerPort);
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{T}"/>.</returns>
ITestcontainersBuilder<T> WithPortBinding(string hostPort, string containerPort);

/// <summary>
/// Binds and mounts the specified host machine volume into the Testcontainer.
/// </summary>
/// <param name="source">An absolute path or a name value within the host machine.</param>
/// <param name="destination">An absolute path as destination in the container.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
ITestcontainersBuilder WithMount(string source, string destination);
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{T}"/>.</returns>
ITestcontainersBuilder<T> WithMount(string source, string destination);

/// <summary>
/// If true, Testcontainer will remove the Testcontainer on finalize. Otherwise, Testcontainer will keep the Testcontainer.
/// </summary>
/// <param name="cleanUp">True, Testcontainer will remove the Testcontainer on finalize. Otherwise, Testcontainer will keep it.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
ITestcontainersBuilder WithCleanUp(bool cleanUp);
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{T}"/>.</returns>
ITestcontainersBuilder<T> WithCleanUp(bool cleanUp);

/// <summary>
/// Sets the output consumer to capture the Testcontainer stdout and stderr messages.
/// </summary>
/// <param name="outputConsumer">Output consumer to capture stdout and strerr.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
ITestcontainersBuilder WithOutputConsumer(IOutputConsumer outputConsumer);
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{T}"/>.</returns>
ITestcontainersBuilder<T> WithOutputConsumer(IOutputConsumer outputConsumer);

/// <summary>
/// Sets the wait strategy to complete the Testcontainer asynchronous start task.
/// </summary>
/// <param name="waitStrategy">Wait strategy to complete the Testcontainer start, default wait strategy implementation <see cref="DefaultWaitStrategy"/>.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
ITestcontainersBuilder WithWaitStrategy(WaitStrategy waitStrategy);
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{T}"/>.</returns>
ITestcontainersBuilder<T> WithWaitStrategy(WaitStrategy waitStrategy);

/// <summary>
/// Builds the instance of <see cref="IDockerContainer"/> with the given configuration.
/// </summary>
/// <returns>A configured instance of <see cref="IDockerContainer"/>.</returns>
IDockerContainer Build();
T Build();
}
}
Loading

0 comments on commit 95729ac

Please sign in to comment.