Skip to content

Commit db64e1f

Browse files
authored
Merge pull request #45 from danielgerlag/SQL-Server-tests
Sql server tests
2 parents cb8451b + e2a0ac6 commit db64e1f

Some content is hidden

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

42 files changed

+555
-200
lines changed

WorkflowCore.sln

Lines changed: 50 additions & 36 deletions
Large diffs are not rendered by default.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.Diagnostics.Process" Version="4.3.0" />
10+
<PackageReference Include="System.Net.NetworkInformation" Version="4.3.0" />
11+
<PackageReference Include="System.Threading.Thread" Version="4.3.0" />
12+
</ItemGroup>
13+
</Project>

test/Docker.Testify/DockerSetup.cs

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
using Docker.DotNet;
2+
using Docker.DotNet.Models;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Net.NetworkInformation;
9+
using System.Runtime.InteropServices;
10+
using System.Text;
11+
using System.Threading;
12+
using System.Threading.Tasks;
13+
14+
namespace Docker.Testify
15+
{
16+
public abstract class DockerSetup : IDisposable
17+
{
18+
public abstract string ImageName { get; }
19+
public virtual string ContainerPrefix => "tests";
20+
public abstract int InternalPort { get; }
21+
22+
public virtual string ImageTag => "latest";
23+
public virtual TimeSpan TimeOut => TimeSpan.FromSeconds(30);
24+
public virtual IList<string> EnvironmentVariables => new List<string>();
25+
public int ExternalPort { get; }
26+
27+
public abstract bool TestReady();
28+
public abstract void PublishConnectionInfo();
29+
30+
protected readonly DockerClient docker;
31+
protected string containerId;
32+
33+
protected DockerSetup()
34+
{
35+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
36+
docker = new DockerClientConfiguration(new Uri("npipe://./pipe/docker_engine")).CreateClient();
37+
else
38+
docker = new DockerClientConfiguration(new Uri("unix:///var/run/docker.sock")).CreateClient();
39+
40+
ExternalPort = GetFreePort();
41+
42+
Debug.WriteLine($"Selected port {ExternalPort}");
43+
44+
StartContainer().Wait();
45+
}
46+
47+
public async Task StartContainer()
48+
{
49+
var hostCfg = new HostConfig();
50+
var pb = new PortBinding
51+
{
52+
HostIP = "0.0.0.0",
53+
HostPort = ExternalPort.ToString()
54+
};
55+
56+
hostCfg.PortBindings = new Dictionary<string, IList<PortBinding>>();
57+
hostCfg.PortBindings.Add($"{InternalPort}/tcp", new PortBinding[] { pb });
58+
59+
await PullImage(ImageName, ImageTag);
60+
61+
var container = await docker.Containers.CreateContainerAsync(new CreateContainerParameters()
62+
{
63+
Image = $"{ImageName}:{ImageTag}",
64+
Name = $"{ContainerPrefix}-{Guid.NewGuid()}",
65+
HostConfig = hostCfg,
66+
Env = EnvironmentVariables
67+
});
68+
69+
Debug.WriteLine("Starting docker container...");
70+
var started = await docker.Containers.StartContainerAsync(container.ID, new ContainerStartParameters());
71+
if (started)
72+
{
73+
containerId = container.ID;
74+
PublishConnectionInfo();
75+
76+
Debug.WriteLine("Waiting service to start in the docker container...");
77+
78+
var ready = false;
79+
var expiryTime = DateTime.Now.Add(TimeOut);
80+
81+
while ((DateTime.Now < expiryTime) && (!ready))
82+
{
83+
await Task.Delay(1000);
84+
ready = TestReady();
85+
}
86+
87+
if (ready)
88+
{
89+
Debug.WriteLine($"Docker container started: {container.ID}");
90+
}
91+
else
92+
{
93+
Debug.WriteLine("Docker container timeout waiting for service");
94+
throw new TimeoutException();
95+
}
96+
}
97+
else
98+
{
99+
Debug.WriteLine("Docker container failed");
100+
}
101+
}
102+
103+
public async Task PullImage(string name, string tag)
104+
{
105+
var images = docker.Images.ListImagesAsync(new ImagesListParameters()).Result;
106+
var exists = images
107+
.Where(x => x.RepoTags != null)
108+
.Any(x => x.RepoTags.Contains($"{name}:{tag}"));
109+
110+
if (exists)
111+
return;
112+
113+
var stream = await docker.Images.PullImageAsync(new ImagesPullParameters() { Parent = name, Tag = tag }, null);
114+
115+
using (StreamReader reader = new StreamReader(stream))
116+
{
117+
while (!reader.EndOfStream)
118+
Debug.WriteLine(reader.ReadLine());
119+
}
120+
}
121+
122+
public void Dispose()
123+
{
124+
docker.Containers.KillContainerAsync(containerId, new ContainerKillParameters()).Wait();
125+
docker.Containers.RemoveContainerAsync(containerId, new ContainerRemoveParameters() { Force = true }).Wait();
126+
}
127+
128+
private int GetFreePort()
129+
{
130+
const int startRange = 1000;
131+
const int endRange = 10000;
132+
var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
133+
var tcpPorts = ipGlobalProperties.GetActiveTcpListeners();
134+
var udpPorts = ipGlobalProperties.GetActiveUdpListeners();
135+
136+
var result = startRange;
137+
138+
while (((tcpPorts.Any(x => x.Port == result)) || (udpPorts.Any(x => x.Port == result))) && result <= endRange)
139+
result++;
140+
141+
if (result > endRange)
142+
throw new PortsInUseException();
143+
144+
return result;
145+
}
146+
}
147+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Docker.Testify
6+
{
7+
public class PortsInUseException : Exception
8+
{
9+
public PortsInUseException()
10+
: base("Ports in range are not available")
11+
{
12+
}
13+
}
14+
}

test/WorkflowCore.IntegrationTests/WorkflowCore.IntegrationTests.csproj

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,8 @@
1919
</ItemGroup>
2020

2121
<ItemGroup>
22-
<PackageReference Include="Machine.Specifications.Runner.Console" Version="0.9.3" />
23-
<PackageReference Include="Machine.Specifications.Runner.VisualStudio" Version="2.2.2" />
2422
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
25-
<PackageReference Include="dotnet-test-mspec" Version="0.2.0" />
2623
<PackageReference Include="FluentAssertions" Version="4.19.2" />
27-
<PackageReference Include="Machine.Fakes" Version="2.8.0" />
28-
<PackageReference Include="Machine.Fakes.Moq" Version="2.8.0" />
29-
<PackageReference Include="Machine.Specifications" Version="0.11.0" />
30-
<PackageReference Include="Machine.Specifications.Should" Version="0.11.0" />
3124
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.1" />
3225
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
3326
<PackageReference Include="Moq" Version="4.7.1" />

test/WorkflowCore.TestAssets/WorkflowCore.TestAssets.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717

1818
<ItemGroup>
1919
<PackageReference Include="FluentAssertions" Version="4.19.0" />
20-
<PackageReference Include="Machine.Fakes" Version="2.8.0" />
21-
<PackageReference Include="Machine.Fakes.Moq" Version="2.8.0" />
22-
<PackageReference Include="Machine.Specifications" Version="0.11.0" />
23-
<PackageReference Include="Machine.Specifications.Should" Version="0.11.0" />
2420
<PackageReference Include="Moq" Version="4.7.1" />
2521
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
2622
<PackageReference Include="NUnit" Version="3.6.1" />

test/WorkflowCore.Tests.MongoDB/DockerSetup.cs

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
public static string ConnectionString { get; set; }
16+
17+
public override string ImageName => "mongo";
18+
public override int InternalPort => 27017;
19+
20+
public override void PublishConnectionInfo()
21+
{
22+
ConnectionString = $"mongodb://localhost:{ExternalPort}";
23+
}
24+
25+
public override bool TestReady()
26+
{
27+
try
28+
{
29+
var client = new MongoClient($"mongodb://localhost:{ExternalPort}");
30+
client.ListDatabases();
31+
return true;
32+
}
33+
catch
34+
{
35+
return false;
36+
}
37+
38+
}
39+
}
40+
41+
[CollectionDefinition("Mongo collection")]
42+
public class MongoCollection : ICollectionFixture<MongoDockerSetup>
43+
{
44+
}
45+
46+
}

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
}

0 commit comments

Comments
 (0)