Skip to content

Commit

Permalink
XUnit base classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jgozner committed Sep 30, 2021
1 parent 23828e2 commit a9189c7
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Ductus.FluentDocker.XUnit/Ductus.FluentDocker.XUnit.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard1.6;net461</TargetFrameworks>
<Version>1.0.0</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<AssemblyTitle>Ductus.FluentDocker.XUnit</AssemblyTitle>
<Authors>Mario Toffia</Authors>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/mariotoffia/FluentDocker</PackageProjectUrl>
<PackageIconUrl>https://pbs.twimg.com/profile_images/378800000124779041/fbbb494a7eef5f9278c6967b6072ca3e.png</PackageIconUrl>
<PackageId>Ductus.FluentDocker.XUnit</PackageId>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageTags>Docker;Docker-Compose;Docker Compose;Docker-Machine;Docker Machine;Linux;Windows;Mac;Test;NET Core</PackageTags>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/mariotoffia/FluentDocker</RepositoryUrl>
<Description>
XUnit Support to allow for create, run one or more docker images while testing using docker, compose, machine (Linux, Windows, Mac) using netcore or full framework.
Documentation: https://github.com/mariotoffia/FluentDocker
</Description>
<Copyright>© 2016 - 2021 Mario Toffia</Copyright>
<AssemblyOriginatorKeyFile>..\keypair.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Ductus.FluentDocker\Ductus.FluentDocker.csproj" />
</ItemGroup>
</Project>
59 changes: 59 additions & 0 deletions Ductus.FluentDocker.XUnit/FluentDockerTestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using Ductus.FluentDocker.Builders;
using Ductus.FluentDocker.Services;

namespace Ductus.FluentDocker.XUnit
{
public abstract class FluentDockerTestBase : IDisposable
{
protected IContainerService Container;

protected abstract ContainerBuilder Build();

public FluentDockerTestBase()
{
Container = Build().Build();
try
{
Container.Start();
}
catch
{
Container.Dispose();
throw;
}

OnContainerInitialized();
}

public void Dispose()
{
OnContainerTearDown();

var c = Container;
Container = null;
try
{
c?.Dispose();
}
catch
{
// Ignore
}
}

/// <summary>
/// Invoked just before the container is teared down.
/// </summary>
protected virtual void OnContainerTearDown()
{
}

/// <summary>
/// Invoked after a container has been created and started.
/// </summary>
protected virtual void OnContainerInitialized()
{
}
}
}
47 changes: 47 additions & 0 deletions Ductus.FluentDocker.XUnit/PostgresTestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ductus.FluentDocker.Builders;
using Ductus.FluentDocker.Services.Extensions;

namespace Ductus.FluentDocker.XUnit
{
public abstract class PostgresTestBase : FluentDockerTestBase
{
protected const string PostgresConnectionString =
"Server={0};Port={1};Userid={2};Password={3};" +
"Pooling=false;MinPoolSize=1;MaxPoolSize=20;" +
"Timeout=15;SslMode=Disable;Database={4}";

protected const string PostgresUser = "postgres";
protected const string PostgresDb = "postgres";
protected readonly string DockerImage;
protected readonly string PostgresPassword;

protected string ConnectionString;

protected PostgresTestBase(string password = "mysecretpassword", string image = "kiasaki/alpine-postgres")
{
PostgresPassword = password;
DockerImage = image;
}

protected override ContainerBuilder Build()
{
return new Builder().UseContainer()
.UseImage("kiasaki/alpine-postgres")
.WithEnvironment($"POSTGRES_PASSWORD={PostgresPassword}")
.ExposePort(5432)
.WaitForPort("5432/tcp", 30000 /*30s*/);
}

protected override void OnContainerInitialized()
{
var ep = Container.ToHostExposedEndpoint("5432/tcp");
ConnectionString = string.Format(PostgresConnectionString, ep.Address, ep.Port, PostgresUser,
PostgresPassword, PostgresDb);
}
}
}
6 changes: 6 additions & 0 deletions FluentDocker.sln
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "data", "data", "{60257CD9-E
data\docker_network_inspect.json = data\docker_network_inspect.json
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ductus.FluentDocker.XUnit", "Ductus.FluentDocker.XUnit\Ductus.FluentDocker.XUnit.csproj", "{F8020BBA-78F7-426A-BE28-13716CC17966}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -46,6 +48,10 @@ Global
{FA836F5A-6361-4042-9B12-E38EB057CD41}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FA836F5A-6361-4042-9B12-E38EB057CD41}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FA836F5A-6361-4042-9B12-E38EB057CD41}.Release|Any CPU.Build.0 = Release|Any CPU
{F8020BBA-78F7-426A-BE28-13716CC17966}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F8020BBA-78F7-426A-BE28-13716CC17966}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F8020BBA-78F7-426A-BE28-13716CC17966}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F8020BBA-78F7-426A-BE28-13716CC17966}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit a9189c7

Please sign in to comment.