Skip to content

Commit d5d7126

Browse files
committed
docker tests
1 parent 2bb1585 commit d5d7126

17 files changed

+214
-113
lines changed

WorkflowCore.sln

Lines changed: 44 additions & 37 deletions
Large diffs are not rendered by default.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard1.4</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Docker.DotNet" Version="2.124.3" />
9+
<PackageReference Include="System.Threading.Thread" Version="4.3.0" />
10+
</ItemGroup>
11+
</Project>

test/Docker.Testify/DockerSetup.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using Docker.DotNet;
2+
using Docker.DotNet.Models;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Runtime.InteropServices;
6+
using System.Text;
7+
using System.Threading;
8+
9+
namespace Docker.Testify
10+
{
11+
public abstract class DockerSetup : IDisposable
12+
{
13+
public abstract string ImageName { get; }
14+
public abstract string ContainerName { get; }
15+
public abstract int ExternalPort { get; }
16+
public abstract int InternalPort { get; }
17+
18+
public virtual string ImageTag => "latest";
19+
public virtual TimeSpan TimeOut => TimeSpan.FromSeconds(30);
20+
public virtual IList<string> EnvironmentVariables => new List<string>();
21+
22+
public abstract bool TestReady();
23+
public abstract void ContainerReady();
24+
25+
protected DockerClient docker;
26+
protected string containerId;
27+
28+
public DockerSetup()
29+
{
30+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
31+
docker = new DockerClientConfiguration(new Uri("npipe://./pipe/docker_engine")).CreateClient();
32+
else
33+
docker = new DockerClientConfiguration(new Uri("unix:///var/run/docker.sock")).CreateClient();
34+
35+
HostConfig hostCfg = new HostConfig();
36+
PortBinding pb = new PortBinding();
37+
pb.HostIP = "0.0.0.0";
38+
pb.HostPort = ExternalPort.ToString();
39+
hostCfg.PortBindings = new Dictionary<string, IList<PortBinding>>();
40+
hostCfg.PortBindings.Add($"{InternalPort}/tcp", new PortBinding[] { pb });
41+
42+
docker.Images.PullImageAsync(new ImagesPullParameters() { Parent = ImageName, Tag = ImageTag }, null).Wait();
43+
44+
var container = docker.Containers.CreateContainerAsync(new CreateContainerParameters()
45+
{
46+
Image = $"{ImageName}:{ImageTag}",
47+
Name = $"{ContainerName}-{Guid.NewGuid()}",
48+
HostConfig = hostCfg,
49+
Env = EnvironmentVariables
50+
}).Result;
51+
52+
Console.WriteLine("Starting docker container...");
53+
bool started = docker.Containers.StartContainerAsync(container.ID, new ContainerStartParameters()).Result;
54+
if (started)
55+
{
56+
Console.WriteLine("Waiting service to start in the docker container...");
57+
58+
var counter = 0;
59+
var ready = false;
60+
61+
while ((counter < (TimeOut.TotalSeconds)) && (!ready))
62+
{
63+
Thread.Sleep(1000);
64+
ready = TestReady();
65+
}
66+
if (ready)
67+
{
68+
Console.WriteLine($"Docker container started: {container.ID}");
69+
ContainerReady();
70+
}
71+
else
72+
{
73+
Console.WriteLine("Docker container timeout waiting for service");
74+
}
75+
}
76+
else
77+
{
78+
Console.WriteLine("Docker container failed");
79+
}
80+
}
81+
82+
public void Dispose()
83+
{
84+
docker.Containers.KillContainerAsync(containerId, new ContainerKillParameters()).Wait();
85+
docker.Containers.RemoveContainerAsync(containerId, new ContainerRemoveParameters() { Force = true }).Wait();
86+
}
87+
}
88+
}

test/WorkflowCore.Tests.MongoDB/DockerSetup.cs

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Docker.DotNet;
2+
using Docker.DotNet.Models;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Runtime.InteropServices;
6+
using System.Text;
7+
using Docker.Testify;
8+
using MongoDB.Driver;
9+
using Xunit;
10+
11+
namespace WorkflowCore.Tests.MongoDB
12+
{
13+
public class MongoDockerSetup : DockerSetup
14+
{
15+
16+
public static string ConnectionString { get; set; }
17+
18+
public override string ImageName => "mongo";
19+
public override string ContainerName => "mongo-workflow-tests";
20+
public override int ExternalPort => 28017;
21+
public override int InternalPort => 27017;
22+
23+
public override void ContainerReady()
24+
{
25+
ConnectionString = $"mongodb://localhost:{ExternalPort}";
26+
}
27+
28+
public override bool TestReady()
29+
{
30+
try
31+
{
32+
var client = new MongoClient($"mongodb://localhost:{ExternalPort}");
33+
client.ListDatabases();
34+
return true;
35+
}
36+
catch
37+
{
38+
return false;
39+
}
40+
41+
}
42+
}
43+
44+
[CollectionDefinition("Mongo collection")]
45+
public class MongoCollection : ICollectionFixture<MongoDockerSetup>
46+
{
47+
}
48+
49+
}

test/WorkflowCore.Tests.MongoDB/MongoPersistenceProviderFixture.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ namespace WorkflowCore.Tests.MongoDB
1212
[Collection("Mongo collection")]
1313
public class MongoPersistenceProviderFixture : BasePersistenceFixture
1414
{
15-
DockerSetup _dockerSetup;
15+
MongoDockerSetup _dockerSetup;
1616

17-
public MongoPersistenceProviderFixture(DockerSetup dockerSetup)
17+
public MongoPersistenceProviderFixture(MongoDockerSetup dockerSetup)
1818
{
1919
_dockerSetup = dockerSetup;
2020
}
@@ -23,7 +23,7 @@ protected override IPersistenceProvider Subject
2323
{
2424
get
2525
{
26-
var client = new MongoClient(_dockerSetup.ConnectionString);
26+
var client = new MongoClient(MongoDockerSetup.ConnectionString);
2727
var db = client.GetDatabase("workflow-tests");
2828
return new MongoPersistenceProvider(db);
2929
}

test/WorkflowCore.Tests.MongoDB/Scenarios/MongoBasicScenario.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class MongoBasicScenario : BasicScenario
1212
{
1313
protected override void ConfigureServices(IServiceCollection services)
1414
{
15-
services.AddWorkflow(x => x.UseMongoDB($"mongodb://localhost:{DockerSetup.Port}", "integration-tests"));
15+
services.AddWorkflow(x => x.UseMongoDB(MongoDockerSetup.ConnectionString, "integration-tests"));
1616
}
1717
}
1818
}

test/WorkflowCore.Tests.MongoDB/Scenarios/MongoDataScenario.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class MongoDataScenario : DataIOScenario
1212
{
1313
protected override void ConfigureServices(IServiceCollection services)
1414
{
15-
services.AddWorkflow(x => x.UseMongoDB($"mongodb://localhost:{DockerSetup.Port}", "integration-tests"));
15+
services.AddWorkflow(x => x.UseMongoDB(MongoDockerSetup.ConnectionString, "integration-tests"));
1616
}
1717
}
1818
}

test/WorkflowCore.Tests.MongoDB/Scenarios/MongoEventScenario.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class MongoEventScenario : EventScenario
1212
{
1313
protected override void ConfigureServices(IServiceCollection services)
1414
{
15-
services.AddWorkflow(x => x.UseMongoDB($"mongodb://localhost:{DockerSetup.Port}", "integration-tests"));
15+
services.AddWorkflow(x => x.UseMongoDB(MongoDockerSetup.ConnectionString, "integration-tests"));
1616
}
1717
}
1818
}

test/WorkflowCore.Tests.MongoDB/Scenarios/MongoForEachScenario.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class MongoForEachScenario : ForeachScenario
1212
{
1313
protected override void ConfigureServices(IServiceCollection services)
1414
{
15-
services.AddWorkflow(x => x.UseMongoDB($"mongodb://localhost:{DockerSetup.Port}", "integration-tests"));
15+
services.AddWorkflow(x => x.UseMongoDB(MongoDockerSetup.ConnectionString, "integration-tests"));
1616
}
1717
}
1818
}

0 commit comments

Comments
 (0)