Try to set Docker endpoint correctly for TLS

This commit is contained in:
Peter 2021-03-05 21:16:14 +01:00
parent 0e086b3b6c
commit 679c2c9af3
Signed by: prskr
GPG key ID: C1DB5D2E8DB512F9
5 changed files with 67 additions and 3 deletions

View file

@ -18,6 +18,9 @@ test:
PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/docker PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/docker
script: script:
- curl https://download.docker.com/linux/static/stable/x86_64/docker-20.10.4.tgz | tar -xzv -C /usr/local/ - curl https://download.docker.com/linux/static/stable/x86_64/docker-20.10.4.tgz | tar -xzv -C /usr/local/
- mkdir /usr/local/share/ca-certificates/docker-ca
- cp "${DOCKER_CERT_PATH}/ca.pem" /usr/local/share/ca-certificates/docker-ca/
- update-ca-certificates --fresh
- dotnet tool restore - dotnet tool restore
- dotnet nuke Test - dotnet nuke Test

View file

@ -0,0 +1,36 @@
using System;
using System.Globalization;
using System.Runtime.InteropServices;
namespace INetMock.Client.IntegrationTest
{
internal static class DockerEndpoint
{
private const string DockerHostEnvName = "DOCKER_HOST";
private const string DockerTlsVerifyEnvName = "DOCKER_TLS_VERIFY";
internal static string DetermineFromEnv() => DetermineEndpoint(
Environment.GetEnvironmentVariable(DockerHostEnvName),
Environment.GetEnvironmentVariable(DockerTlsVerifyEnvName)
);
internal static string DetermineEndpoint(string? dockerHost, string? tlsVerify)
{
dockerHost ??= "";
var dockerTlsVerify = int.TryParse(tlsVerify, out var verify) && verify == 1;
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
return (dockerHost, dockerTlsVerify, isWindows) switch
{
("", _, true) => "npipe://./pipe/docker_engine",
("", _, false) => "unix:/var/run/docker.sock",
(_, false, _) => dockerHost,
(var h, true, _) when h.StartsWith("tcp") => h.Replace(
"tcp",
"https",
true,
CultureInfo.InvariantCulture),
_ => "",
};
}
}
}

View file

@ -0,0 +1,22 @@
using Xunit;
namespace INetMock.Client.IntegrationTest
{
public class DockerEndpointTests
{
[Theory]
[InlineData(null, null, "unix:/var/run/docker.sock")]
[InlineData("tcp://docker:2375", null, "tcp://docker:2375")]
[InlineData("tcp://docker:2375", "0", "tcp://docker:2375")]
[InlineData("http://docker:2375", null, "http://docker:2375")]
[InlineData("http://docker:2375", "0", "http://docker:2375")]
[InlineData("tcp://docker:2376", "1", "https://docker:2376")]
[InlineData("https://docker:2376", "0", "https://docker:2376")]
public void DetermineDockerEndpoint_Input_ExpectedOutput(string? dockerHost, string? tlsVerify, string expected)
{
var actual = DockerEndpoint.DetermineEndpoint(dockerHost, tlsVerify);
Assert.Equal(expected, actual);
}
}
}

View file

@ -4,6 +4,8 @@
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View file

@ -10,6 +10,7 @@ namespace INetMock.Client.IntegrationTest
{ {
public class INetMockServerFixture : IAsyncLifetime public class INetMockServerFixture : IAsyncLifetime
{ {
private readonly TestcontainersContainer _inetmockContainer; private readonly TestcontainersContainer _inetmockContainer;
public INetMockServerFixture() public INetMockServerFixture()
@ -19,13 +20,13 @@ namespace INetMock.Client.IntegrationTest
.WithCommand("serve") .WithCommand("serve")
.WithPortBinding(80, true) .WithPortBinding(80, true)
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(80)) .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(80))
.WithDockerEndpoint(Environment.GetEnvironmentVariable("DOCKER_HOST") ?? "unix:///var/run/docker.sock") .WithDockerEndpoint(DockerEndpoint.DetermineFromEnv())
.WithMount(Path.GetTempPath(), "/var/run/inetmock") .WithMount(Path.GetTempPath(), "/var/run/inetmock")
.WithCleanUp(true) .WithCleanUp(true)
.Build(); .Build();
} }
public Uri SocketPath => new ($"unix://{Path.Join(Path.GetTempPath(), "inetmock.sock")}", UriKind.Absolute); public Uri SocketPath => new($"unix://{Path.Join(Path.GetTempPath(), "inetmock.sock")}", UriKind.Absolute);
public async Task InitializeAsync() public async Task InitializeAsync()
{ {