Skip to content

Commit

Permalink
feat(#476): Publish official package
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Jun 21, 2022
1 parent ccb3976 commit 1a093fc
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>
<PropertyGroup>
<PackageId>$(AssemblyName)</PackageId>
<Version>1.7.0</Version>
<Version>2.0.0</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<FileVersion>$(Version)</FileVersion>
<Product>Testcontainers</Product>
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![NuGet](https://img.shields.io/nuget/v/DotNet.Testcontainers.svg)](https://www.nuget.org/packages/DotNet.Testcontainers)
[![NuGet](https://img.shields.io/nuget/v/Testcontainers.svg)](https://www.nuget.org/packages/Testcontainers)
[![NuGet](https://img.shields.io/nuget/vpre/Testcontainers.svg)](https://www.nuget.org/packages/Testcontainers)
[![Continuous Integration](https://github.com/testcontainers/testcontainers-dotnet/actions/workflows/cicd.yml/badge.svg?branch=develop)](https://github.com/testcontainers/testcontainers-dotnet/actions/workflows/cicd.yml)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=testcontainers_testcontainers-dotnet&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=testcontainers_testcontainers-dotnet)
Expand Down Expand Up @@ -35,7 +35,7 @@ To configure a container, use the `TestcontainersBuilder<TestcontainersContainer
- `WithBindMount` binds a path of a file or directory into the container e.g. `-v, --volume .:/tmp`.
- `WithVolumeMount` mounts a managed volume into the container e.g. `--mount type=volume,source=.,destination=/tmp`.
- `WithNetwork` assigns a network to the container e.g. `--network="bridge"`.
- `WithNetworkAliases` assigns a network scoped aliases to the container e.g. `--network-alias alias`
- `WithNetworkAliases` assigns a network-scoped aliases to the container e.g. `--network-alias alias`
- `WithDockerEndpoint` sets the Docker API endpoint e.g. `-H tcp://0.0.0.0:2376`.
- `WithRegistryAuthentication` basic authentication against a private Docker registry.
- `WithOutputConsumer` redirects `stdout` and `stderr` to capture the container output.
Expand Down
8 changes: 4 additions & 4 deletions src/Testcontainers/Builders/ITestcontainersBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,17 @@ public interface ITestcontainersBuilder<out TDockerContainer> : IAbstractBuilder
ITestcontainersBuilder<TDockerContainer> WithNetwork(IDockerNetwork dockerNetwork);

/// <summary>
/// Assign specified network aliases to container.
/// Assigns the specified network-scoped aliases to the container.
/// </summary>
/// <param name="networkAliases">Set of network aliases.</param>
/// <param name="networkAliases">Network-scoped aliases.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{TDockerContainer}" />.</returns>
[PublicAPI]
ITestcontainersBuilder<TDockerContainer> WithNetworkAliases(params string[] networkAliases);

/// <summary>
/// Assign specified network aliases to container.
/// Assigns the specified network-scoped aliases to the container.
/// </summary>
/// <param name="networkAliases">Set of network aliases.</param>
/// <param name="networkAliases">Network-scoped aliases.</param>
/// <returns>A configured instance of <see cref="ITestcontainersBuilder{TDockerContainer}" />.</returns>
[PublicAPI]
ITestcontainersBuilder<TDockerContainer> WithNetworkAliases(IEnumerable<string> networkAliases);
Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers/Clients/TestcontainersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace DotNet.Testcontainers.Clients
/// <inheritdoc cref="ITestcontainersClient" />
internal sealed class TestcontainersClient : ITestcontainersClient
{
public const string TestcontainersLabel = "dotnet.testcontainers";
public const string TestcontainersLabel = "testcontainers";

private readonly string osRootDirectory = Path.GetPathRoot(Directory.GetCurrentDirectory());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ public TestcontainersConfigurationConverter(ITestcontainersConfiguration configu
this.ExposedPorts = new ToExposedPorts().Convert(configuration.ExposedPorts)?.ToDictionary(item => item.Key, item => item.Value);
this.PortBindings = new ToPortBindings().Convert(configuration.PortBindings)?.ToDictionary(item => item.Key, item => item.Value);
this.Mounts = new ToMounts().Convert(configuration.Mounts)?.ToList();
this.Networks = new ToNetworks().Convert(configuration.Networks)?.ToDictionary(
network => network.Key,
network =>
{
network.Value.Aliases = configuration.NetworkAliases?.ToArray();
return network.Value;
});
this.Networks = new ToNetworks(configuration).Convert(configuration.Networks)?.ToDictionary(item => item.Key, item => item.Value);
}

public IList<string> Entrypoint { get; }
Expand Down Expand Up @@ -90,14 +84,17 @@ public override IEnumerable<Mount> Convert([CanBeNull] IEnumerable<IMount> sourc

private sealed class ToNetworks : CollectionConverter<IDockerNetwork, KeyValuePair<string, EndpointSettings>>
{
public ToNetworks()
private readonly ITestcontainersConfiguration configuration;

public ToNetworks(ITestcontainersConfiguration configuration)
: base(nameof(ToNetworks))
{
this.configuration = configuration;
}

public override IEnumerable<KeyValuePair<string, EndpointSettings>> Convert([CanBeNull] IEnumerable<IDockerNetwork> source)
{
return source?.Select(network => new KeyValuePair<string, EndpointSettings>(network.Name, new EndpointSettings { NetworkID = network.Id }));
return source?.Select(network => new KeyValuePair<string, EndpointSettings>(network.Name, new EndpointSettings { NetworkID = network.Id, Aliases = this.configuration.NetworkAliases.ToArray() }));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public interface ITestcontainersConfiguration : IDockerResourceConfiguration
IEnumerable<IDockerNetwork> Networks { get; }

/// <summary>
/// Gets a list of container network aliases.
/// Gets a list of network aliases.
/// </summary>
IEnumerable<string> NetworkAliases { get; }

Expand Down
35 changes: 19 additions & 16 deletions src/Testcontainers/Containers/TestcontainersContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace DotNet.Testcontainers.Containers
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Docker.DotNet;
using Docker.DotNet.Models;
using DotNet.Testcontainers.Clients;
using DotNet.Testcontainers.Configurations;
Expand All @@ -28,7 +29,7 @@ public class TestcontainersContainer : ITestcontainersContainer
private readonly ITestcontainersConfiguration configuration;

[NotNull]
private ContainerListResponse container = new ContainerListResponse();
private ContainerInspectResponse container = new ContainerInspectResponse();

/// <summary>
/// Initializes a new instance of the <see cref="TestcontainersContainer" /> class.
Expand Down Expand Up @@ -58,7 +59,7 @@ public string Name
get
{
this.ThrowIfContainerHasNotBeenCreated();
return this.container.Names.First();
return this.container.Name;
}
}

Expand Down Expand Up @@ -120,7 +121,7 @@ public TestcontainersState State
{
try
{
return (TestcontainersState)Enum.Parse(typeof(TestcontainersState), this.container.State, true);
return (TestcontainersState)Enum.Parse(typeof(TestcontainersState), this.container.State.Status, true);
}
catch (Exception)
{
Expand All @@ -146,11 +147,9 @@ public ushort GetMappedPublicPort(string privatePort)
{
this.ThrowIfContainerHasNotBeenCreated();

var mappedPort = this.container.Ports.FirstOrDefault(port => Convert.ToString(port.PrivatePort, CultureInfo.InvariantCulture).Equals(privatePort, StringComparison.Ordinal));

if (mappedPort != null)
if (this.container.NetworkSettings.Ports.TryGetValue($"{privatePort}/tcp", out var portMap) && ushort.TryParse(portMap.First().HostPort, out var publicPort))
{
return mappedPort.PublicPort;
return publicPort;
}
else
{
Expand Down Expand Up @@ -195,6 +194,10 @@ await this.semaphoreSlim.WaitAsync(ct)
this.container = await this.Stop(this.Id, ct)
.ConfigureAwait(false);
}
catch (DockerContainerNotFoundException)
{
this.container = new ContainerInspectResponse();
}
finally
{
this.semaphoreSlim.Release();
Expand Down Expand Up @@ -270,7 +273,7 @@ internal Task<ContainerInspectResponse> InspectContainer(CancellationToken ct =
return this.client.InspectContainer(this.Id, ct);
}

private async Task<ContainerListResponse> Create(CancellationToken ct = default)
private async Task<ContainerInspectResponse> Create(CancellationToken ct = default)
{
if (ContainerHasBeenCreatedStates.Contains(this.State))
{
Expand All @@ -280,19 +283,19 @@ private async Task<ContainerListResponse> Create(CancellationToken ct = default)
var id = await this.client.RunAsync(this.configuration, ct)
.ConfigureAwait(false);

return await this.client.GetContainer(id, ct)
return await this.client.InspectContainer(id, ct)
.ConfigureAwait(false);
}

private async Task<ContainerListResponse> Start(string id, CancellationToken ct = default)
private async Task<ContainerInspectResponse> Start(string id, CancellationToken ct = default)
{
await this.client.AttachAsync(id, this.configuration.OutputConsumer, ct)
.ConfigureAwait(false);

await this.client.StartAsync(id, ct)
.ConfigureAwait(false);

this.container = await this.client.GetContainer(id, ct)
this.container = await this.client.InspectContainer(id, ct)
.ConfigureAwait(false);

await this.configuration.StartupCallback(this, ct)
Expand All @@ -309,7 +312,7 @@ await this.configuration.StartupCallback(this, ct)
await WaitStrategy.WaitUntil(
async () =>
{
this.container = await this.client.GetContainer(id, ct)
this.container = await this.client.InspectContainer(id, ct)
.ConfigureAwait(false);
return await waitStrategy.Until(this, this.Logger)
Expand All @@ -324,20 +327,20 @@ await this.configuration.StartupCallback(this, ct)
return this.container;
}

private async Task<ContainerListResponse> Stop(string id, CancellationToken ct = default)
private async Task<ContainerInspectResponse> Stop(string id, CancellationToken ct = default)
{
await this.client.StopAsync(id, ct)
.ConfigureAwait(false);

return await this.client.GetContainer(id, ct)
return await this.client.InspectContainer(id, ct)
.ConfigureAwait(false);
}

private async Task<ContainerListResponse> CleanUp(string id, CancellationToken ct = default)
private async Task<ContainerInspectResponse> CleanUp(string id, CancellationToken ct = default)
{
await this.client.RemoveAsync(id, ct)
.ConfigureAwait(false);
return new ContainerListResponse();
return new ContainerInspectResponse();
}

private void ThrowIfContainerHasNotBeenCreated()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task CleanUpFalseDoesNotStartDefaultResourceReaper()
{
// Given
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("alpine")
.WithImage("nginx")
.WithAutoRemove(true)
.WithCleanUp(false);

Expand All @@ -38,7 +38,7 @@ public async Task CleanUpTrueDoesStartDefaultResourceReaper()
{
// Given
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("alpine")
.WithImage("nginx")
.WithCleanUp(true);

// When
Expand All @@ -57,7 +57,7 @@ public async Task UsingResourceReaperSessionIdDoesNotStartDefaultResourceReaper(
{
// Given
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("alpine")
.WithImage("nginx")
.WithAutoRemove(true)
.WithResourceReaperSessionId(Guid.NewGuid());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ await resourceReaper.DisposeAsync()
[Fact]
public async Task ResourceReaperShouldTimeoutIfInitializationFails()
{
var resourceReaperTask = ResourceReaper.GetAndStartNewAsync(this.sessionId, null, "alpine");
var resourceReaperTask = ResourceReaper.GetAndStartNewAsync(this.sessionId, null, "nginx");
_ = await Assert.ThrowsAsync<ResourceReaperException>(() => resourceReaperTask);
Assert.Equal(new[] { ResourceReaperState.Created, ResourceReaperState.InitializingConnection }, this.stateChanges);
}
Expand All @@ -51,7 +51,7 @@ public async Task GetAndStartNewAsyncShouldBeCancellableDuringContainerStart()
{
ResourceReaper.StateChanged += this.CancelOnCreated;

var resourceReaperTask = ResourceReaper.GetAndStartNewAsync(this.sessionId, null, "alpine", TimeSpan.FromSeconds(60), this.cts.Token);
var resourceReaperTask = ResourceReaper.GetAndStartNewAsync(this.sessionId, null, "nginx", TimeSpan.FromSeconds(60), this.cts.Token);
_ = await Assert.ThrowsAnyAsync<OperationCanceledException>(() => resourceReaperTask);
Assert.Equal(new[] { ResourceReaperState.Created }, this.stateChanges);
}
Expand All @@ -61,7 +61,7 @@ public async Task GetAndStartNewAsyncShouldBeCancellableDuringInitializingConnec
{
ResourceReaper.StateChanged += this.CancelOnInitializingConnection;

var resourceReaperTask = ResourceReaper.GetAndStartNewAsync(this.sessionId, null, "alpine", TimeSpan.FromSeconds(60), this.cts.Token);
var resourceReaperTask = ResourceReaper.GetAndStartNewAsync(this.sessionId, null, "nginx", TimeSpan.FromSeconds(60), this.cts.Token);
_ = await Assert.ThrowsAsync<ResourceReaperException>(() => resourceReaperTask);
Assert.Equal(new[] { ResourceReaperState.Created, ResourceReaperState.InitializingConnection }, this.stateChanges);
}
Expand Down

0 comments on commit 1a093fc

Please sign in to comment.