Skip to content

Commit

Permalink
Added default database configuration model, part of #45.
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Mar 13, 2019
1 parent 95729ac commit ca4a74a
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<PackageReference Include="coverlet.msbuild" Version="2.5.1" />
<PackageReference Include="LanguageExt.Core" Version="3.1.15" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Npgsql" Version="4.0.5" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="xunit" Version="2.4.1" />
</ItemGroup>
Expand Down
29 changes: 22 additions & 7 deletions src/DotNet.Testcontainers.Tests/Unit/DatabaseContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,40 @@ namespace DotNet.Testcontainers.Tests.Unit
using System.Threading.Tasks;
using DotNet.Testcontainers.Core.Builder;
using DotNet.Testcontainers.Core.Containers.Database;
using DotNet.Testcontainers.Core.Models;
using Npgsql;
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);
.WithDatabase(new DatabaseConfiguration
{
Database = "db",
Username = "postgres",
Password = "postgres",
});

using (var testcontainer = testcontainersBuilder.Build())
{
await testcontainer.StartAsync();

/*
using (var connection = new NpgsqlConnection(testcontainer.ConnectionString))
{
connection.Open();
using (var cmd = new NpgsqlCommand())
{
cmd.Connection = connection;
cmd.CommandText = "SELECT 1";
cmd.ExecuteReader();
}
}
*/
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace DotNet.Testcontainers.Tests.Unit
using Xunit;
using static LanguageExt.Prelude;

public static class TestcontainersTest
public static class TestcontainersContainerTest
{
private static readonly string TempDir = Environment.GetEnvironmentVariable("AGENT_TEMPDIRECTORY") ?? "."; // We cannot use `Path.GetTempPath()` on macOS, see: https://github.com/common-workflow-language/cwltool/issues/328

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected override async Task<bool> Until()
public class Timeout : WaitStrategy
{
[Fact]
public async Task WhileAfter5ms()
public async Task WhileAfter1ms()
{
await Assert.ThrowsAsync<TimeoutException>(async () =>
{
Expand All @@ -44,7 +44,7 @@ await Assert.ThrowsAsync<TimeoutException>(async () =>
}

[Fact]
public async Task UntilAfter5ms()
public async Task UntilAfter1ms()
{
await Assert.ThrowsAsync<TimeoutException>(async () =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public ITestcontainersBuilder<T> WithExposedPort(string port)
{
return Build(this, new TestcontainersConfiguration
{
Container = new ContainerConfiguration { Labels = new Dictionary<string, string> { { port, port } } },
Container = new ContainerConfiguration { ExposedPorts = new Dictionary<string, string> { { port, port } } },
});
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace DotNet.Testcontainers.Core.Builder
{
using DotNet.Testcontainers.Core.Containers.Database;
using DotNet.Testcontainers.Core.Models;

public static class TestcontainersBuilderPostgreSqlExtension
{
private const string DefaultImage = "postgres:11.2";

private const string DefaultPort = "5432";

private const string Database = "POSTGRES_DB";

private const string Username = "POSTGRES_USER";

private const string Password = "POSTGRES_PASSWORD";

public static ITestcontainersBuilder<T> WithDatabase<T>(this ITestcontainersBuilder<T> builder, DatabaseConfiguration configuration)
where T : PostgreSqlContainer
{
return builder
.WithImage(DefaultImage)
.WithPortBinding(DefaultPort)
.WithEnvironment(Database, configuration.Database)
.WithEnvironment(Username, configuration.Username)
.WithEnvironment(Password, configuration.Password)
.ConfigureContainer((container) =>
{
container.Hostname = configuration.Hostname;
container.Port = configuration.Port;
container.Database = configuration.Database;
container.Username = configuration.Username;
container.Password = configuration.Password;
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ namespace DotNet.Testcontainers.Core.Containers.Database

public abstract class DatabaseContainer : TestcontainersContainer
{
internal DatabaseContainer(TestcontainersConfiguration configuration) : base(configuration)
protected DatabaseContainer(TestcontainersConfiguration configuration) : base(configuration)
{
}

public virtual string Hostname { get; set; }

public virtual string Port { get; set; }

public virtual string Database { get; set; }

public virtual string Username { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ internal PostgreSqlContainer(TestcontainersConfiguration configuration) : base(c
{
}

public override string ConnectionString => $"Server=127.0.0.1;Port=5432;Database={this.Database};User Id={this.Username};Password={this.Password};";
public override string ConnectionString => $"Server={this.Hostname};Port={this.Port};Database={this.Database};User Id={this.Username};Password={this.Password};";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class TestcontainersContainer : IDockerContainer

private Option<ContainerListResponse> container = None;

internal TestcontainersContainer(TestcontainersConfiguration configuration)
protected TestcontainersContainer(TestcontainersConfiguration configuration)
{
this.Configuration = configuration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ private class ToList : CollectionConverter<IList<string>>
{
public override IList<string> Convert(IReadOnlyCollection<string> source)
{
return source.ToList();
return source?.ToList();
}
}

private class ToDictionary : DictionaryConverter<IDictionary<string, string>>
{
public override IDictionary<string, string> Convert(IReadOnlyDictionary<string, string> source)
{
return source.ToDictionary(item => item.Key, item => item.Value);
return source?.ToDictionary(item => item.Key, item => item.Value);
}
}

private class ToMappedList : DictionaryConverter<IList<string>>
{
public override IList<string> Convert(IReadOnlyDictionary<string, string> source)
{
return source.Select(item => $"{item.Key}={item.Value}").ToList();
return source?.Select(item => $"{item.Key}={item.Value}").ToList();
}
}

Expand All @@ -61,7 +61,7 @@ public ToExposedPorts() : base("ExposedPorts")

public override IDictionary<string, EmptyStruct> Convert(IReadOnlyDictionary<string, string> source)
{
return source.ToDictionary(exposedPort => $"{exposedPort.Key}/tcp", exposedPort => default(EmptyStruct));
return source?.ToDictionary(exposedPort => $"{exposedPort.Key}/tcp", exposedPort => default(EmptyStruct));
}
}

Expand All @@ -73,7 +73,7 @@ public ToPortBindings() : base("PortBindings")

public override IDictionary<string, IList<PortBinding>> Convert(IReadOnlyDictionary<string, string> source)
{
return source.ToDictionary(binding => $"{binding.Value}/tcp", binding => new List<PortBinding> { new PortBinding { HostPort = binding.Key } } as IList<PortBinding>);
return source?.ToDictionary(binding => $"{binding.Value}/tcp", binding => new List<PortBinding> { new PortBinding { HostPort = binding.Key } } as IList<PortBinding>);
}
}

Expand All @@ -85,7 +85,7 @@ public ToMounts() : base("Mounts")

public override IList<Mount> Convert(IReadOnlyDictionary<string, string> source)
{
return source.Select(mount => new Mount { Source = Path.GetFullPath(mount.Key), Target = mount.Value, Type = "bind" }).ToList();
return source?.Select(mount => new Mount { Source = Path.GetFullPath(mount.Key), Target = mount.Value, Type = "bind" }).ToList();
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/DotNet.Testcontainers/Core/Models/DatabaseConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace DotNet.Testcontainers.Core.Models
{
public class DatabaseConfiguration
{
public virtual string Hostname { get; set; } = "localhost";

public virtual string Port { get; set; }

public virtual string Database { get; set; }

public virtual string Username { get; set; }

public virtual string Password { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace DotNet.Testcontainers.Core.Models
using System.Linq;
using DotNet.Testcontainers.Diagnostics;

internal class TestcontainersConfiguration
public class TestcontainersConfiguration
{
public ContainerConfiguration Container { get; set; } = new ContainerConfiguration();

Expand Down Expand Up @@ -39,7 +39,7 @@ internal TestcontainersConfiguration Merge(TestcontainersConfiguration old)

this.Host.Mounts = Merge(this.Host.Mounts, old.Host.Mounts);

this.CleanUp = old.CleanUp;
this.CleanUp = this.CleanUp && old.CleanUp;

this.OutputConsumer = Merge(this.OutputConsumer, old.OutputConsumer);

Expand All @@ -57,22 +57,22 @@ private static T Merge<T>(T myself, T old)
private static IReadOnlyCollection<T> Merge<T>(IReadOnlyCollection<T> myself, IReadOnlyCollection<T> old)
where T : class
{
if (myself == null)
if (myself == null || old == null)
{
return old ?? new ReadOnlyCollection<T>(new List<T>());
return myself ?? old;
}
else
{
return myself.Concat(old.Where(x => !myself.Contains(x))).ToList();
return myself.Concat(old).ToList();
}
}

private static IReadOnlyDictionary<T, T> Merge<T>(IReadOnlyDictionary<T, T> myself, IReadOnlyDictionary<T, T> old)
where T : class
{
if (myself == null)
if (myself == null || old == null)
{
return old ?? new ReadOnlyDictionary<T, T>(new Dictionary<T, T>());
return myself ?? old;
}
else
{
Expand All @@ -88,22 +88,22 @@ public class ContainerConfiguration

public string WorkingDirectory { get; set; }

public IReadOnlyCollection<string> Entrypoint { get; set; } = new List<string>();
public IReadOnlyCollection<string> Entrypoint { get; set; }

public IReadOnlyCollection<string> Command { get; set; } = new List<string>();
public IReadOnlyCollection<string> Command { get; set; }

public IReadOnlyDictionary<string, string> Environments { get; set; } = new Dictionary<string, string>();
public IReadOnlyDictionary<string, string> Environments { get; set; }

public IReadOnlyDictionary<string, string> Labels { get; set; } = new Dictionary<string, string>();
public IReadOnlyDictionary<string, string> Labels { get; set; }

public IReadOnlyDictionary<string, string> ExposedPorts { get; set; } = new Dictionary<string, string>();
public IReadOnlyDictionary<string, string> ExposedPorts { get; set; }
}

public class HostConfiguration
{
public IReadOnlyDictionary<string, string> PortBindings { get; set; } = new Dictionary<string, string>();
public IReadOnlyDictionary<string, string> PortBindings { get; set; }

public IReadOnlyDictionary<string, string> Mounts { get; set; } = new Dictionary<string, string>();
public IReadOnlyDictionary<string, string> Mounts { get; set; }
}
}
}

0 comments on commit ca4a74a

Please sign in to comment.