From d27077be8750c7b6d7b44ae229c30a433d5d9410 Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Tue, 21 Jun 2022 12:25:09 +0200 Subject: [PATCH] feat(#476): Publish official package --- Directory.Build.props | 2 +- README.md | 4 +-- .../Builders/ITestcontainersBuilder.cs | 8 ++--- .../Clients/TestcontainersClient.cs | 2 +- .../TestcontainersConfigurationConverter.cs | 15 ++++---- .../ITestcontainersConfiguration.cs | 2 +- .../Containers/TestcontainersContainer.cs | 35 ++++++++++--------- 7 files changed, 34 insertions(+), 34 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 41962ed08..8b0211237 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -11,7 +11,7 @@ $(AssemblyName) - 1.7.0 + 2.0.0 $(Version) $(Version) Testcontainers diff --git a/README.md b/README.md index 87abeb583..79197fb5b 100755 --- a/README.md +++ b/README.md @@ -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) @@ -35,7 +35,7 @@ To configure a container, use the `TestcontainersBuilder : IAbstractBuilder ITestcontainersBuilder WithNetwork(IDockerNetwork dockerNetwork); /// - /// Assign specified network aliases to container. + /// Assigns the specified network-scoped aliases to the container. /// - /// Set of network aliases. + /// Network-scoped aliases. /// A configured instance of . [PublicAPI] ITestcontainersBuilder WithNetworkAliases(params string[] networkAliases); /// - /// Assign specified network aliases to container. + /// Assigns the specified network-scoped aliases to the container. /// - /// Set of network aliases. + /// Network-scoped aliases. /// A configured instance of . [PublicAPI] ITestcontainersBuilder WithNetworkAliases(IEnumerable networkAliases); diff --git a/src/Testcontainers/Clients/TestcontainersClient.cs b/src/Testcontainers/Clients/TestcontainersClient.cs index baab91d7f..66c23eb58 100644 --- a/src/Testcontainers/Clients/TestcontainersClient.cs +++ b/src/Testcontainers/Clients/TestcontainersClient.cs @@ -17,7 +17,7 @@ namespace DotNet.Testcontainers.Clients /// internal sealed class TestcontainersClient : ITestcontainersClient { - public const string TestcontainersLabel = "dotnet.testcontainers"; + public const string TestcontainersLabel = "testcontainers"; private readonly string osRootDirectory = Path.GetPathRoot(Directory.GetCurrentDirectory()); diff --git a/src/Testcontainers/Clients/TestcontainersConfigurationConverter.cs b/src/Testcontainers/Clients/TestcontainersConfigurationConverter.cs index a6b149623..b75997bc2 100644 --- a/src/Testcontainers/Clients/TestcontainersConfigurationConverter.cs +++ b/src/Testcontainers/Clients/TestcontainersConfigurationConverter.cs @@ -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 Entrypoint { get; } @@ -90,14 +84,17 @@ public override IEnumerable Convert([CanBeNull] IEnumerable sourc private sealed class ToNetworks : CollectionConverter> { - public ToNetworks() + private readonly ITestcontainersConfiguration configuration; + + public ToNetworks(ITestcontainersConfiguration configuration) : base(nameof(ToNetworks)) { + this.configuration = configuration; } public override IEnumerable> Convert([CanBeNull] IEnumerable source) { - return source?.Select(network => new KeyValuePair(network.Name, new EndpointSettings { NetworkID = network.Id })); + return source?.Select(network => new KeyValuePair(network.Name, new EndpointSettings { NetworkID = network.Id, Aliases = this.configuration.NetworkAliases.ToArray() })); } } diff --git a/src/Testcontainers/Configurations/Containers/ITestcontainersConfiguration.cs b/src/Testcontainers/Configurations/Containers/ITestcontainersConfiguration.cs index c6aeb9f55..53e134ffd 100644 --- a/src/Testcontainers/Configurations/Containers/ITestcontainersConfiguration.cs +++ b/src/Testcontainers/Configurations/Containers/ITestcontainersConfiguration.cs @@ -84,7 +84,7 @@ public interface ITestcontainersConfiguration : IDockerResourceConfiguration IEnumerable Networks { get; } /// - /// Gets a list of container network aliases. + /// Gets a list of network aliases. /// IEnumerable NetworkAliases { get; } diff --git a/src/Testcontainers/Containers/TestcontainersContainer.cs b/src/Testcontainers/Containers/TestcontainersContainer.cs index cfb38045f..6cee20348 100644 --- a/src/Testcontainers/Containers/TestcontainersContainer.cs +++ b/src/Testcontainers/Containers/TestcontainersContainer.cs @@ -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; @@ -28,7 +29,7 @@ public class TestcontainersContainer : ITestcontainersContainer private readonly ITestcontainersConfiguration configuration; [NotNull] - private ContainerListResponse container = new ContainerListResponse(); + private ContainerInspectResponse container = new ContainerInspectResponse(); /// /// Initializes a new instance of the class. @@ -58,7 +59,7 @@ public string Name get { this.ThrowIfContainerHasNotBeenCreated(); - return this.container.Names.First(); + return this.container.Name; } } @@ -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) { @@ -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 { @@ -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(); @@ -270,7 +273,7 @@ internal Task InspectContainer(CancellationToken ct = return this.client.InspectContainer(this.Id, ct); } - private async Task Create(CancellationToken ct = default) + private async Task Create(CancellationToken ct = default) { if (ContainerHasBeenCreatedStates.Contains(this.State)) { @@ -280,11 +283,11 @@ private async Task 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 Start(string id, CancellationToken ct = default) + private async Task Start(string id, CancellationToken ct = default) { await this.client.AttachAsync(id, this.configuration.OutputConsumer, ct) .ConfigureAwait(false); @@ -292,7 +295,7 @@ await this.client.AttachAsync(id, this.configuration.OutputConsumer, ct) 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) @@ -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) @@ -324,20 +327,20 @@ await WaitStrategy.WaitUntil( return this.container; } - private async Task Stop(string id, CancellationToken ct = default) + private async Task 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 CleanUp(string id, CancellationToken ct = default) + private async Task CleanUp(string id, CancellationToken ct = default) { await this.client.RemoveAsync(id, ct) .ConfigureAwait(false); - return new ContainerListResponse(); + return new ContainerInspectResponse(); } private void ThrowIfContainerHasNotBeenCreated()