Skip to content

Commit 9c610ac

Browse files
authored
feat: refactor Docker container management to use Testcontainers (#165)
1 parent 70aecee commit 9c610ac

File tree

68 files changed

+639
-1425
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+639
-1425
lines changed

.github/workflows/ci.yml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
name: CI
2-
run-name: CI ${{ github.event.pull_request.title }}
2+
run-name: CI 🔍 ${{ github.event.pull_request.title }}
33

44
on:
55
pull_request:
66

7+
concurrency:
8+
group: ci-${{ github.event.pull_request.number }}
9+
cancel-in-progress: true
10+
711
jobs:
812
tests:
913
runs-on: ubuntu-latest
14+
name: Tests dotnet ${{ matrix.dotnet-version }}
15+
timeout-minutes: 60
16+
strategy:
17+
fail-fast: false
18+
matrix:
19+
dotnet-version: [8, 9, 10]
1020
steps:
1121
- name: Checkout code
1222
uses: actions/checkout@v6
1323

1424
- name: Setup .NET
1525
uses: actions/setup-dotnet@v5
1626
with:
17-
dotnet-version: |
18-
8
19-
9
20-
10
27+
dotnet-version: ${{ matrix.dotnet-version }}
2128

2229
- name: Docker Information
2330
run: |
@@ -28,9 +35,9 @@ jobs:
2835
shell: bash
2936

3037
- name: Build projects
31-
run: dotnet build ./all.csproj
3238
shell: bash
39+
run: dotnet build ./all.csproj -f net${{ matrix.dotnet-version }}.0
3340

3441
- name: Run tests
35-
run: dotnet test ./all.csproj /p:BuildTestOnly=true
36-
shell: bash
42+
shell: bash
43+
run: dotnet test ./all.csproj /p:BuildTestsOnly=true --no-build -f net${{ matrix.dotnet-version }}.0 -- RunConfiguration.MaxCpuCount=1

.github/workflows/release.yml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: Release
2-
run-name: Release ${{ github.ref_name }}
2+
run-name: Release 🚀 ${{ github.ref_name }}
33

44
on:
55
push:
@@ -9,17 +9,20 @@ on:
99
jobs:
1010
release:
1111
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
project: ['.']
1215
steps:
1316
- name: Checkout code
1417
uses: actions/checkout@v6
1518

1619
- name: Setup .NET
1720
uses: actions/setup-dotnet@v5
18-
with:
19-
dotnet-version: |
20-
8
21-
9
22-
10
21+
22+
- name: Build projects
23+
shell: bash
24+
working-directory: ${{ matrix.project }}
25+
run: dotnet build ./all.csproj -c Release
2326

2427
- name: Extract version
2528
id: extract_version
@@ -32,12 +35,9 @@ jobs:
3235
echo "Package Version: $PACKAGE_VERSION"
3336
echo "Assembly Version: $ASSEMBLY_VERSION"
3437
35-
- name: Build projects
36-
shell: bash
37-
run: dotnet build ./all.csproj -c Release
38-
3938
- name: Pack
4039
shell: bash
40+
working-directory: ${{ matrix.project }}
4141
run: |
4242
dotnet pack ./all.csproj \
4343
-c Release \
@@ -52,5 +52,6 @@ jobs:
5252
5353
- name: Push
5454
shell: bash
55+
working-directory: ${{ matrix.project }}
5556
run: |
5657
find . -name '*.nupkg' | grep -v '.sources.nupkg' | parallel --jobs 0 'dotnet nuget push {} --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json'

Directory.Packages.props

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
4242
<PackageVersion Include="xunit" Version="2.9.3" />
4343
<PackageVersion Include="xunit.core" Version="2.9.3" />
44-
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.0" />
44+
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
4545
<PackageVersion Include="FluentAssertions" Version="7.2.0" />
4646
<PackageVersion Include="Moq" Version="4.20.72" />
4747
<PackageVersion Include="coverlet.collector" Version="6.0.4" />
@@ -50,10 +50,16 @@
5050

5151
<PackageVersion Include="Microsoft.Extensions.Configuration.UserSecrets" Version="5.0.0" />
5252

53-
<PackageVersion Include="Docker.DotNet.Enhanced" Version="3.131.0" />
53+
<PackageVersion Include="Testcontainers" Version="4.3.0" />
54+
<PackageVersion Include="Testcontainers.MongoDb" Version="4.3.0" />
55+
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.3.0" />
56+
<PackageVersion Include="Testcontainers.Redis" Version="4.3.0" />
57+
<PackageVersion Include="Testcontainers.RabbitMq" Version="4.3.0" />
58+
<PackageVersion Include="Testcontainers.MySql" Version="4.3.0" />
59+
<PackageVersion Include="Testcontainers.MsSql" Version="4.3.0" />
60+
<PackageVersion Include="Testcontainers.Elasticsearch" Version="4.3.0" />
61+
<PackageVersion Include="Testcontainers.MariaDb" Version="4.3.0" />
5462
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
55-
<PackageVersion Include="Polly" Version="8.5.2" />
56-
<PackageVersion Include="SharpZipLib" Version="1.4.2" />
5763

5864
<PackageVersion Include="MongoDB.Driver" Version="3.0.0" />
5965
<PackageVersion Include="MySqlConnector" Version="2.4.0" />
@@ -83,7 +89,7 @@
8389
<PackageVersion Include="Typesense" Version="7.33.0" />
8490

8591
<PackageVersion Include="FluentFTP" Version="35.0.4" />
86-
<PackageVersion Include="SSH.NET" Version="2023.0.0" />
92+
<PackageVersion Include="SSH.NET" Version="2024.2.0" />
8793

8894

8995
<PackageVersion Include="ServiceStack.Text" Version="8.8.0" />

all.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<Project Sdk="Microsoft.Build.Traversal/4.1.82">
22

33
<PropertyGroup>
4-
<BuildTestOnly>false</BuildTestOnly>
4+
<BuildTestsOnly>false</BuildTestsOnly>
55
<IncludeInSolutionFile>false</IncludeInSolutionFile>
66
</PropertyGroup>
77

8-
<ItemGroup Condition="$(BuildTestOnly) != 'true'">
8+
<ItemGroup Condition="'$(BuildTestsOnly)' != 'true'">
99
<ProjectReference Include="**/*.csproj" />
1010
</ItemGroup>
1111

12-
<ItemGroup Condition="$(BuildTestOnly) != 'true'">
13-
<ProjectReference Include="**/test/*.csproj" />
12+
<ItemGroup Condition="'$(BuildTestsOnly)' == 'true'">
13+
<ProjectReference Include="**/test/**/*.csproj" />
1414
</ItemGroup>
1515

16-
</Project>
16+
</Project>

nuget.config

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<clear />
5+
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
6+
</packageSources>
7+
</configuration>

src/Core/ChmodCommand.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
using System;
3-
using Docker.DotNet.Models;
43

54
namespace Squadron;
65

@@ -17,36 +16,27 @@ public static class ChmodCommand
1716
/// <param name="group"><see cref="Permission"/> for group</param>
1817
/// <param name="public"><see cref="Permission"/> for public</param>
1918
/// <param name="recursive">Apply recursive</param>
20-
public static ContainerExecCreateParameters Set(
19+
public static string[] Set(
2120
string pathInContainer,
2221
Permission owner = Permission.None,
2322
Permission group = Permission.None,
2423
Permission @public = Permission.None,
2524
bool recursive = false)
2625
{
2726
var cmd = $"chmod {(recursive ? "-R " : "")}{owner:d}{group:d}{@public:d} {pathInContainer}";
28-
29-
return new ContainerExecCreateParameters
30-
{
31-
AttachStderr = true,
32-
AttachStdin = false,
33-
AttachStdout = true,
34-
Cmd = cmd.Split(' '),
35-
Privileged = true,
36-
User = "root"
37-
};
27+
return cmd.Split(' ');
3828
}
3929

40-
public static ContainerExecCreateParameters ReadOnly(string pathInContainer, bool recursive = false)
30+
public static string[] ReadOnly(string pathInContainer, bool recursive = false)
4131
=> Set(pathInContainer, Permission.Read, Permission.Read, Permission.Read, recursive);
4232

43-
public static ContainerExecCreateParameters FullAccess(string pathInContainer, bool recursive = false)
33+
public static string[] FullAccess(string pathInContainer, bool recursive = false)
4434
=> Set(pathInContainer, Permission.FullAccess, Permission.FullAccess, Permission.FullAccess, recursive);
4535

46-
public static ContainerExecCreateParameters Execute(string pathInContainer, bool recursive = false)
36+
public static string[] Execute(string pathInContainer, bool recursive = false)
4737
=> Set(pathInContainer, Permission.Execute, Permission.Execute, Permission.Execute, recursive);
4838

49-
public static ContainerExecCreateParameters ReadWrite(string pathInContainer, bool recursive = false)
39+
public static string[] ReadWrite(string pathInContainer, bool recursive = false)
5040
=> Set(pathInContainer, Permission.ReadWrite, Permission.ReadWrite, Permission.ReadWrite, recursive);
5141

5242
[Flags]

src/Core/ContainerInstance.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IO;
4-
using Docker.DotNet;
53

64
namespace Squadron;
75

8-
/// <summary>Respresents a created docker container</summary>
6+
/// <summary>Represents a created docker container</summary>
97
public class ContainerInstance : IDisposable
108
{
119
/// <summary>
@@ -67,10 +65,7 @@ public class ContainerInstance : IDisposable
6765
/// </value>
6866
public IList<string> Logs { get; set; } = new List<string>();
6967

70-
internal MultiplexedStream? LogStream { get; set; }
71-
7268
public void Dispose()
7369
{
74-
LogStream?.Dispose();
7570
}
7671
}

src/Core/ContainerResource.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ protected virtual void SetupContainerResource()
6464
OnSettingsBuilded(Settings);
6565
ValidateSettings(Settings);
6666

67-
DockerConfiguration dockerConfig = Settings.DockerConfigResolver();
68-
69-
Manager = new DockerContainerManager(Settings, dockerConfig);
67+
Manager = new TestcontainersDockerManager(Settings);
7068
Initializer = new ContainerInitializer(Manager, Settings);
7169
}
7270

src/Core/ContainerResourceBuilder.cs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,6 @@ public ContainerResourceBuilder Image(string image)
6060
return this;
6161
}
6262

63-
/// <summary>
64-
/// Container registry name as defined in configurations
65-
/// Default is DockerHub
66-
/// </summary>
67-
/// <param name="registryName">Name of the registry.</param>
68-
/// <returns></returns>
69-
public ContainerResourceBuilder Registry(string registryName)
70-
{
71-
_options.RegistryName = registryName;
72-
return this;
73-
}
74-
75-
public ContainerResourceBuilder AddressMode(ContainerAddressMode mode)
76-
{
77-
_options.AddressMode = mode;
78-
return this;
79-
}
80-
8163
/// <summary>
8264
/// Adds an environment variable.
8365
/// </summary>
@@ -342,18 +324,6 @@ public ContainerResourceBuilder WaitTimeout(int seconds)
342324
return this;
343325
}
344326

345-
/// <summary>
346-
/// Sets the docker configuration resolver.
347-
/// </summary>
348-
/// <param name="resolver">The resolver.</param>
349-
/// <returns></returns>
350-
public ContainerResourceBuilder SetDockerConfigResolver(
351-
Func<DockerConfiguration> resolver)
352-
{
353-
_options.DockerConfigResolver = resolver;
354-
return this;
355-
}
356-
357327
/// <summary>
358328
/// Adds a network of which the container should be part of.
359329
/// </summary>
@@ -420,7 +390,6 @@ public virtual ContainerResourceSettings Build()
420390
_logLevel = overriddenLogLevel;
421391
}
422392

423-
_options.DockerConfigResolver ??= ContainerResourceOptions.DefaultDockerConfigResolver;
424393
_options.Logger = new Logger(_logLevel, _options);
425394
_options.Cmd = _cmd;
426395
return _options;

0 commit comments

Comments
 (0)