Skip to content

Commit

Permalink
chore: Generate Docker client from OpenAPI specifications
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Sep 16, 2023
1 parent c798c81 commit 9c54684
Show file tree
Hide file tree
Showing 9 changed files with 25,294 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Testcontainers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{673F23AE-769
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DockerEngine", "src\DockerEngine\DockerEngine.csproj", "{64B58ABB-A849-4FC1-9D3B-2620140427A9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Azurite", "src\Testcontainers.Azurite\Testcontainers.Azurite.csproj", "{3F2E254F-C203-43FD-A078-DC3E2CBC0F9F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.ClickHouse", "src\Testcontainers.ClickHouse\Testcontainers.ClickHouse.csproj", "{B061A78E-536E-4CA1-8401-234D5FBFBAB7}"
Expand Down Expand Up @@ -71,6 +73,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.WebDriver",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers", "src\Testcontainers\Testcontainers.csproj", "{EC76857B-A3B8-4B7A-A1B0-8D867A4D1733}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DockerEngine.Tests", "tests\DockerEngine.Tests\DockerEngine.Tests.csproj", "{F54DE0D3-D1C4-4A30-9B78-9F72338B5E6A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Azurite.Tests", "tests\Testcontainers.Azurite.Tests\Testcontainers.Azurite.Tests.csproj", "{B272FDDE-5E01-425D-B9E1-10FF883DDAAA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.ClickHouse.Tests", "tests\Testcontainers.ClickHouse.Tests\Testcontainers.ClickHouse.Tests.csproj", "{9D0A0B32-4921-400C-99CB-8650677E3E44}"
Expand Down Expand Up @@ -146,6 +150,10 @@ Global
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{64B58ABB-A849-4FC1-9D3B-2620140427A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{64B58ABB-A849-4FC1-9D3B-2620140427A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{64B58ABB-A849-4FC1-9D3B-2620140427A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{64B58ABB-A849-4FC1-9D3B-2620140427A9}.Release|Any CPU.Build.0 = Release|Any CPU
{3F2E254F-C203-43FD-A078-DC3E2CBC0F9F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3F2E254F-C203-43FD-A078-DC3E2CBC0F9F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3F2E254F-C203-43FD-A078-DC3E2CBC0F9F}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -262,6 +270,10 @@ Global
{EC76857B-A3B8-4B7A-A1B0-8D867A4D1733}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EC76857B-A3B8-4B7A-A1B0-8D867A4D1733}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EC76857B-A3B8-4B7A-A1B0-8D867A4D1733}.Release|Any CPU.Build.0 = Release|Any CPU
{F54DE0D3-D1C4-4A30-9B78-9F72338B5E6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F54DE0D3-D1C4-4A30-9B78-9F72338B5E6A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F54DE0D3-D1C4-4A30-9B78-9F72338B5E6A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F54DE0D3-D1C4-4A30-9B78-9F72338B5E6A}.Release|Any CPU.Build.0 = Release|Any CPU
{B272FDDE-5E01-425D-B9E1-10FF883DDAAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B272FDDE-5E01-425D-B9E1-10FF883DDAAA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B272FDDE-5E01-425D-B9E1-10FF883DDAAA}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -396,6 +408,7 @@ Global
{EBA72C3B-57D5-43FF-A5B4-3D55B3B6D4C2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{64B58ABB-A849-4FC1-9D3B-2620140427A9} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{3F2E254F-C203-43FD-A078-DC3E2CBC0F9F} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{B061A78E-536E-4CA1-8401-234D5FBFBAB7} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{A724806F-8C94-4438-8011-04A9A1575318} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
Expand Down Expand Up @@ -425,6 +438,7 @@ Global
{C95A3B2F-2B28-49A7-8806-731C158BBC21} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{64A87DE5-29B0-4A54-9E74-560484D8C7C0} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{EC76857B-A3B8-4B7A-A1B0-8D867A4D1733} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{F54DE0D3-D1C4-4A30-9B78-9F72338B5E6A} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{B272FDDE-5E01-425D-B9E1-10FF883DDAAA} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{9D0A0B32-4921-400C-99CB-8650677E3E44} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{2478673C-B063-469D-ABD1-0C3E0A25541B} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
Expand Down
1 change: 1 addition & 0 deletions src/DockerEngine/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
25,168 changes: 25,168 additions & 0 deletions src/DockerEngine/DockerClient.cs

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions src/DockerEngine/DockerEngine.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NSwag.MSBuild" Version="13.20.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
<Target Name="NSwag" BeforeTargets="CoreCompile">
<Exec Command="$(NSwagExe_Net60) run NSwag.json"/>
</Target>
</Project>
18 changes: 18 additions & 0 deletions src/DockerEngine/NSwag.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"runtime": "Net60",
"documentGenerator": {
"fromDocument": {
"url": "https://docs.docker.com/reference/engine/v1.43.yaml"
}
},
"codeGenerators": {
"openApiToCSharpClient": {
"className": "DockerClient",
"namespace": "DockerEngine",
"jsonLibrary": "SystemTextJson",
"generateImmutableArrayProperties": true,
"generateImmutableDictionaryProperties": true,
"output": "DockerClient.cs"
}
}
}
1 change: 1 addition & 0 deletions tests/DockerEngine.Tests/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
53 changes: 53 additions & 0 deletions tests/DockerEngine.Tests/DockerClientTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace DockerEngine;

public sealed class DockerClientTest : IAsyncLifetime
{
private const string DockerHost = "0.0.0.0";

private const ushort DockerPort = 2375;

private readonly IContainer _dockerContainer = new ContainerBuilder()
.WithImage("docker:24.0.5-dind")
.WithEntrypoint("dockerd")
.WithCommand("--host", "tcp://" + DockerHost + ":" + DockerPort, "--debug")
.WithPortBinding(DockerPort, true)
.WithPrivileged(true)
.WithWaitStrategy(Wait.ForUnixContainer().UntilMessageIsLogged("API listen on \\[::\\]:" + DockerPort))
.Build();

public System.Threading.Tasks.Task InitializeAsync()
{
return _dockerContainer.StartAsync();
}

public System.Threading.Tasks.Task DisposeAsync()
{
return _dockerContainer.DisposeAsync().AsTask();
}

[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
public async System.Threading.Tasks.Task EstablishesConnection()
{
// Given
const string repository = "alpine";

const string tag = "latest";

// TODO: Add support for additional schemes such as TCP, SSH, Unix, and Named Pipes (npipe) daemon socket.
var dockerClient = new DockerClient(new UriBuilder(Uri.UriSchemeHttp, _dockerContainer.Hostname, _dockerContainer.GetMappedPublicPort(DockerPort)).ToString(), new HttpClient());

// When
// TODO: Consider creating request and response objects instead of using a lengthy list of arguments.
await dockerClient.ImageCreateAsync(repository, null, null, tag, null, string.Empty, null, null, null);

// TODO: Somehow, the HTTP request terminates too early, and the Docker image cannot be used or is not available right away. The container creation operation returns: no such image.
await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(3));

// TODO: Try to avoid wrapping the actual content within an additional (unnecessary) body type, like `Body : ContainerConfig`.
var response = await dockerClient.ContainerCreateAsync(null, null, new Body { Image = repository + ":" + tag });

// Then
Assert.Equal(64, response.Id.Length);
}
}
18 changes: 18 additions & 0 deletions tests/DockerEngine.Tests/DockerEngine.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<IsPublishable>false</IsPublishable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2"/>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0"/>
<PackageReference Include="xunit" Version="2.5.0"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(SolutionDir)src/DockerEngine/DockerEngine.csproj"/>
<ProjectReference Include="$(SolutionDir)src/Testcontainers/Testcontainers.csproj"/>
<ProjectReference Include="$(SolutionDir)tests/Testcontainers.Commons/Testcontainers.Commons.csproj"/>
</ItemGroup>
</Project>
6 changes: 6 additions & 0 deletions tests/DockerEngine.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
global using System;
global using System.Net.Http;
global using DotNet.Testcontainers.Builders;
global using DotNet.Testcontainers.Commons;
global using DotNet.Testcontainers.Containers;
global using Xunit;

0 comments on commit 9c54684

Please sign in to comment.