Skip to content

Commit

Permalink
[#393] #CHANGE 'assemblyName: DotNet.Testcontainers; function: IWaitU…
Browse files Browse the repository at this point in the history
…ntil'

{Pass instance of ITestcontainersContainer to the wait strategies.}
  • Loading branch information
HofmeisterAn committed Aug 24, 2021
1 parent d9b1a40 commit 4948e22
Show file tree
Hide file tree
Showing 42 changed files with 160 additions and 122 deletions.
4 changes: 2 additions & 2 deletions .cake-scripts/parameters.cake
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal sealed class BuildParameters

public static BuildParameters Instance(ICakeContext context)
{
const string propertiesFilePath = "DotNet.Testcontainers.props";
const string propertiesFilePath = "Directory.Build.props";
const string solutionFilePath = "DotNet.Testcontainers.sln";

var buildInformation = BuildInformation.Instance(context, propertiesFilePath);
Expand All @@ -52,7 +52,7 @@ internal sealed class BuildParameters
IsReleaseBuild = !buildInformation.IsLocalBuild && buildInformation.IsReleaseBuild,
IsPullRequest = buildInformation.IsPullRequest,
ShouldPublish = !buildInformation.IsLocalBuild && buildInformation.ShouldPublish,
Verbosity = DotNetCoreVerbosity.Quiet,
Verbosity = DotNetCoreVerbosity.Normal,
SonarQubeCredentials = SonarQubeCredentials.GetSonarQubeCredentials(context),
NuGetCredentials = NuGetCredentials.GetNuGetCredentials(context),
Projects = BuildProjects.Instance(context, solutionFilePath),
Expand Down
2 changes: 1 addition & 1 deletion .cake-scripts/version.cake
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ internal sealed class BuildInformation
targetBranch = targetBranch.Replace("refs/heads/", string.Empty);
}

var version = context.XmlPeek(propertiesFilePath, "/Project/PropertyGroup[1]/Version/text()");
var version = context.XmlPeek(propertiesFilePath, "/Project/PropertyGroup[2]/Version/text()");

var isReleaseBuild = GetIsReleaseBuild(branch);

Expand Down
40 changes: 37 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,44 @@
<PropertyGroup>
<SolutionDir Condition=" '$(SolutionDir)' == '' ">$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildProjectDirectory), DotNet.Testcontainers.sln))/</SolutionDir>
<BuildDir>$(SolutionDir)build/</BuildDir>
<BaseIntermediateOutputPath>$(BuildDir)obj/$(Platform)/$(Configuration)/$(MSBuildProjectName)/</BaseIntermediateOutputPath>
<BaseIntermediateOutputPath>$(BuildDir)obj/$(MSBuildProjectName)</BaseIntermediateOutputPath>
<IntermediateOutputPath>$(BaseIntermediateOutputPath)</IntermediateOutputPath>
<MSBuildProjectExtensionsPath>$(BaseIntermediateOutputPath)</MSBuildProjectExtensionsPath>
<OutputPath>$(BuildDir)bin/$(Platform)/$(Configuration)/$(TargetFramework)/</OutputPath>
<OutDir>$(OutputPath)</OutDir>
<OutputPath>$(BuildDir)bin/$(Platform)/$(Configuration)/$(TargetFramework)</OutputPath>
<OutDir>$(OutputPath)/</OutDir>
</PropertyGroup>
<PropertyGroup>
<PackageId>$(AssemblyName)</PackageId>
<Version>1.6.0</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<FileVersion>$(Version)</FileVersion>
<Product>.NET Testcontainers</Product>
<Copyright>Copyright (c) 2019 - 2021 Andre Hofmeister and other authors</Copyright>
<Authors>Andre Hofmeister and contributors</Authors>
<Company>Andre Hofmeister</Company>
<Description>A lightweight library to run tests with throwaway instances of Docker containers.</Description>
<Summary>.NET Testcontainers makes it easy to run tests with Docker containers. Create reliable and fast environments within seconds and throw them away if not needed anymore.</Summary>
<PackageIcon>DotNet.Testcontainers.png</PackageIcon>
<PackageIconUrl>https://github.com/HofmeisterAn/dotnet-testcontainers/raw/develop/docs/DotNet.Testcontainers.png</PackageIconUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://github.com/HofmeisterAn/dotnet-testcontainers</PackageProjectUrl>
<PackageTags>.NET;Docker;Container;Test</PackageTags>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/HofmeisterAn/dotnet-testcontainers</RepositoryUrl>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup>
<NoWarn>CA1716,SA0001,SA1600,SA1633</NoWarn>
</PropertyGroup>
<ItemGroup>
<None Include="$(SolutionDir)LICENSE" Visible="false" Pack="true" PackagePath="" />
<None Include="$(SolutionDir)docs/DotNet.Testcontainers.png" Visible="false" Pack="true" PackagePath="" />
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>DotNet.Testcontainers.Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Project>
37 changes: 0 additions & 37 deletions DotNet.Testcontainers.props

This file was deleted.

16 changes: 13 additions & 3 deletions src/DotNet.Testcontainers/Builders/ITestcontainersBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public interface ITestcontainersBuilder<out TDockerContainer>
/// Exposes the port of the Testcontainer, without publishing the port to the host system’s interfaces.
/// </summary>
/// <param name="port">Port to expose.</param>
/// <remarks>Append /tcp|udp|sctp to change the protocol e.g "53/udp". Default: tcp.</remarks>
/// <remarks>Append /tcp|udp|sctp to change the protocol e.g. "53/udp". Default: tcp.</remarks>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{TDockerContainer}" />.</returns>
[PublicAPI]
ITestcontainersBuilder<TDockerContainer> WithExposedPort(string port);
Expand Down Expand Up @@ -137,7 +137,7 @@ public interface ITestcontainersBuilder<out TDockerContainer>
/// </summary>
/// <param name="port">Port to bind between Testcontainer and host machine.</param>
/// <param name="assignRandomHostPort">If true, Testcontainer will bind the port to a random host port, otherwise the host and container ports are the same.</param>
/// <remarks>Append /tcp|udp|sctp to change the protocol e.g "53/udp". Default: tcp.</remarks>
/// <remarks>Append /tcp|udp|sctp to change the protocol e.g. "53/udp". Default: tcp.</remarks>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{TDockerContainer}" />.</returns>
[PublicAPI]
ITestcontainersBuilder<TDockerContainer> WithPortBinding(string port, bool assignRandomHostPort = false);
Expand All @@ -147,7 +147,7 @@ public interface ITestcontainersBuilder<out TDockerContainer>
/// </summary>
/// <param name="hostPort">Port of the host machine.</param>
/// <param name="containerPort">Port of the Testcontainer.</param>
/// <remarks>Append /tcp|udp|sctp to <see cref="containerPort" /> to change the protocol e.g "53/udp". Default: tcp.</remarks>
/// <remarks>Append /tcp|udp|sctp to <see cref="containerPort" /> to change the protocol e.g. "53/udp". Default: tcp.</remarks>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{TDockerContainer}" />.</returns>
[PublicAPI]
ITestcontainersBuilder<TDockerContainer> WithPortBinding(string hostPort, string containerPort);
Expand All @@ -161,6 +161,16 @@ public interface ITestcontainersBuilder<out TDockerContainer>
[PublicAPI]
ITestcontainersBuilder<TDockerContainer> WithMount(string source, string destination);

/// <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>
/// <param name="accessMode">Volume access mode.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{TDockerContainer}" />.</returns>
[PublicAPI]
ITestcontainersBuilder<TDockerContainer> WithMount(string source, string destination, AccessMode accessMode);

/// <summary>
/// Connects to the specified network.
/// </summary>
Expand Down
12 changes: 9 additions & 3 deletions src/DotNet.Testcontainers/Builders/TestcontainersBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public TestcontainersBuilder()
Apply(
endpoint: TestcontainersSettings.OS.DockerApiEndpoint,
dockerRegistryAuthConfig: default(DockerRegistryAuthenticationConfiguration),
labels: new DefaultLabels(),
labels: DefaultLabels.Instance,
outputConsumer: Consume.DoNotConsumeStdoutAndStderr(),
waitStrategies: Wait.ForUnixContainer().Build(),
startupCallback: (testcontainers, ct) => Task.CompletedTask),
Expand Down Expand Up @@ -171,7 +171,13 @@ public ITestcontainersBuilder<TDockerContainer> WithPortBinding(string hostPort,
/// <inheritdoc />
public ITestcontainersBuilder<TDockerContainer> WithMount(string source, string destination)
{
var mounts = new IBindMount[] { new BindMount(source, destination, AccessMode.ReadWrite) };
return this.WithMount(source, destination, AccessMode.ReadWrite);
}

/// <inheritdoc />
public ITestcontainersBuilder<TDockerContainer> WithMount(string source, string destination, AccessMode accessMode)
{
var mounts = new IBindMount[] { new BindMount(source, destination, accessMode) };
return Build(this, Apply(mounts: mounts));
}

Expand All @@ -192,7 +198,7 @@ public ITestcontainersBuilder<TDockerContainer> WithNetwork(IDockerNetwork docke
public ITestcontainersBuilder<TDockerContainer> WithCleanUp(bool cleanUp)
{
return Build(this, Apply(cleanUp: cleanUp))
.WithLabel(TestcontainersClient.TestcontainersCleanUpLabel, cleanUp.ToString().ToLowerInvariant());
.WithLabel(TestcontainersClient.TestcontainersCleanUpLabel, cleanUp.ToString());
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public TestcontainersNetworkBuilder()
: this(
Apply(
endpoint: TestcontainersSettings.OS.DockerApiEndpoint,
labels: new DefaultLabels(),
labels: DefaultLabels.Instance,
driver: NetworkDriver.Bridge))
{
}
Expand Down
5 changes: 4 additions & 1 deletion src/DotNet.Testcontainers/Clients/DefaultLabels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ namespace DotNet.Testcontainers.Clients

internal sealed class DefaultLabels : ReadOnlyDictionary<string, string>
{
public DefaultLabels()
private DefaultLabels()
: base(new Dictionary<string, string>
{
{ TestcontainersClient.TestcontainersLabel, bool.TrueString },
{ TestcontainersClient.TestcontainersCleanUpLabel, bool.TrueString },
})
{
}

public static IReadOnlyDictionary<string, string> Instance { get; }
= new DefaultLabels();
}
}
2 changes: 1 addition & 1 deletion src/DotNet.Testcontainers/Clients/TestcontainersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ await this.containers.RemoveAsync(id, ct)
}
catch (DockerApiException e)
{
// The Docker daemon may already start the progress to removes the container (AutoRemove).
// The Docker daemon may already start the progress to removes the container (AutoRemove):
// https://docs.docker.com/engine/api/v1.41/#operation/ContainerCreate.
if (!e.Message.Contains($"removal of container {id} is already in progress"))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace DotNet.Testcontainers.Configurations
{
using System.IO;

/// <inheritdoc cref="IBindMount" />
internal readonly struct BindMount : IBindMount
{
Expand All @@ -13,7 +11,7 @@ namespace DotNet.Testcontainers.Configurations
/// <param name="accessMode">The Docker volume access mode.</param>
public BindMount(string hostPath, string containerPath, AccessMode accessMode)
{
this.HostPath = Path.GetFullPath(hostPath);
this.HostPath = hostPath;
this.ContainerPath = containerPath;
this.AccessMode = accessMode;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace DotNet.Testcontainers.Configurations
{
using System;
using System.Threading.Tasks;
using DotNet.Testcontainers.Containers;
using Microsoft.Extensions.Logging;

public interface IWaitUntil
{
Task<bool> Until(Uri endpoint, string id, ILogger logger);
Task<bool> Until(ITestcontainersContainer container, ILogger logger);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace DotNet.Testcontainers.Configurations
{
using System;
using System.Threading.Tasks;
using DotNet.Testcontainers.Containers;
using Microsoft.Extensions.Logging;

public interface IWaitWhile
{
Task<bool> While(Uri endpoint, string id, ILogger logger);
Task<bool> While(ITestcontainersContainer container, ILogger logger);
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
namespace DotNet.Testcontainers.Configurations
{
using System;
using System.Linq;
using System.Threading.Tasks;
using DotNet.Testcontainers.Clients;
using DotNet.Testcontainers.Containers;
using Microsoft.Extensions.Logging;

internal class UntilContainerIsRunning : IWaitUntil
{
private static readonly TestcontainersState[] ContainerHasBeenRunningStates = { TestcontainersState.Running, TestcontainersState.Exited };

public async Task<bool> Until(Uri endpoint, string id, ILogger logger)
public Task<bool> Until(ITestcontainersContainer container, ILogger logger)
{
var client = new TestcontainersClient(endpoint, logger);

var container = await client.GetContainer(id)
.ConfigureAwait(false);

var state = (TestcontainersState)Enum.Parse(typeof(TestcontainersState), container.State, true);
return ContainerHasBeenRunningStates.Contains(state);
return Task.FromResult(ContainerHasBeenRunningStates.Contains(container.State));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace DotNet.Testcontainers.Configurations
{
using System;
using System.IO;
using System.Threading.Tasks;
using DotNet.Testcontainers.Containers;
using Microsoft.Extensions.Logging;

internal class UntilFilesExists : IWaitUntil
Expand All @@ -14,7 +14,7 @@ public UntilFilesExists(string file)
this.file = file;
}

public Task<bool> Until(Uri endpoint, string id, ILogger logger)
public Task<bool> Until(ITestcontainersContainer container, ILogger logger)
{
return Task.FromResult(File.Exists(this.file));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace DotNet.Testcontainers.Configurations
{
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using DotNet.Testcontainers.Containers;
using Microsoft.Extensions.Logging;

internal class UntilMessageIsLogged : IWaitUntil
Expand All @@ -19,7 +19,7 @@ public UntilMessageIsLogged(Stream stream, string message)
this.message = message;
}

public async Task<bool> Until(Uri endpoint, string id, ILogger logger)
public async Task<bool> Until(ITestcontainersContainer container, ILogger logger)
{
this.stream.Seek(0, SeekOrigin.Begin);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace DotNet.Testcontainers.Configurations
{
using System;
using System.Threading.Tasks;
using DotNet.Testcontainers.Containers;
using Microsoft.Extensions.Logging;

internal class UntilOperationIsSucceeded : IWaitUntil
Expand All @@ -18,7 +19,7 @@ public UntilOperationIsSucceeded(Func<bool> operation, int maxCallCount)
this.maxCallCount = maxCallCount;
}

public Task<bool> Until(Uri endpoint, string id, ILogger logger)
public Task<bool> Until(ITestcontainersContainer container, ILogger logger)
{
if (++this.tryCount > this.maxCallCount)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
namespace DotNet.Testcontainers.Configurations
{
using System;
using System.Globalization;
using System.Text;
using System.Threading.Tasks;
using DotNet.Testcontainers.Clients;
using DotNet.Testcontainers.Containers;
using Microsoft.Extensions.Logging;

internal class UntilUnixCommandIsCompleted : IWaitUntil
Expand All @@ -19,14 +20,12 @@ public UntilUnixCommandIsCompleted(params string[] command)
this.command = command;
}

public virtual async Task<bool> Until(Uri endpoint, string id, ILogger logger)
public virtual async Task<bool> Until(ITestcontainersContainer container, ILogger logger)
{
var client = new TestcontainersClient(endpoint, logger);

var result = await client.ExecAsync(id, this.command)
var execResult = await container.ExecAsync(this.command)
.ConfigureAwait(false);

return 0L.Equals(result.ExitCode);
return 0L.Equals(execResult.ExitCode);
}
}
}
Loading

0 comments on commit 4948e22

Please sign in to comment.