|
2 | 2 | using Docker.DotNet.Models;
|
3 | 3 | using System;
|
4 | 4 | using System.Collections.Generic;
|
| 5 | +using System.Diagnostics; |
| 6 | +using System.Linq; |
| 7 | +using System.Net.NetworkInformation; |
5 | 8 | using System.Runtime.InteropServices;
|
6 | 9 | using System.Text;
|
7 | 10 | using System.Threading;
|
| 11 | +using System.Threading.Tasks; |
8 | 12 |
|
9 | 13 | namespace Docker.Testify
|
10 | 14 | {
|
11 | 15 | 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 | + { |
| 17 | + public abstract string ImageName { get; } |
| 18 | + public virtual string ContainerPrefix => "tests"; |
16 | 19 | public abstract int InternalPort { get; }
|
17 | 20 |
|
18 | 21 | public virtual string ImageTag => "latest";
|
19 | 22 | public virtual TimeSpan TimeOut => TimeSpan.FromSeconds(30);
|
20 | 23 | public virtual IList<string> EnvironmentVariables => new List<string>();
|
| 24 | + public int ExternalPort { get; } |
21 | 25 |
|
22 |
| - public abstract bool TestReady(); |
23 |
| - public abstract void ContainerReady(); |
| 26 | + public abstract bool TestReady(); |
| 27 | + public abstract void PublishConnectionInfo(); |
24 | 28 |
|
25 |
| - protected DockerClient docker; |
| 29 | + protected readonly DockerClient docker; |
26 | 30 | protected string containerId;
|
27 | 31 |
|
28 |
| - public DockerSetup() |
29 |
| - { |
30 |
| - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 32 | + protected DockerSetup() |
| 33 | + { |
| 34 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
31 | 35 | docker = new DockerClientConfiguration(new Uri("npipe://./pipe/docker_engine")).CreateClient();
|
32 | 36 | else
|
33 | 37 | docker = new DockerClientConfiguration(new Uri("unix:///var/run/docker.sock")).CreateClient();
|
34 | 38 |
|
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 |
| - } |
| 39 | + ExternalPort = GetFreePort(); |
| 40 | + |
| 41 | + Debug.WriteLine($"Selected port {ExternalPort}"); |
| 42 | + |
| 43 | + StartContainer().Wait(); |
80 | 44 | }
|
81 | 45 |
|
| 46 | + public async Task StartContainer() |
| 47 | + { |
| 48 | + var hostCfg = new HostConfig(); |
| 49 | + var pb = new PortBinding |
| 50 | + { |
| 51 | + HostIP = "0.0.0.0", |
| 52 | + HostPort = ExternalPort.ToString() |
| 53 | + }; |
| 54 | + |
| 55 | + hostCfg.PortBindings = new Dictionary<string, IList<PortBinding>>(); |
| 56 | + hostCfg.PortBindings.Add($"{InternalPort}/tcp", new PortBinding[] { pb }); |
| 57 | + |
| 58 | + //await docker.Images.PullImageAsync(new ImagesPullParameters() { Parent = ImageName, Tag = ImageTag }, null); |
| 59 | + Process.Start("docker", $"pull {ImageName}:{ImageTag}").WaitForExit(); |
| 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 | + |
82 | 103 | public void Dispose()
|
83 | 104 | {
|
84 |
| - docker.Containers.KillContainerAsync(containerId, new ContainerKillParameters()).Wait(); |
85 |
| - docker.Containers.RemoveContainerAsync(containerId, new ContainerRemoveParameters() { Force = true }).Wait(); |
| 105 | + docker.Containers.KillContainerAsync(containerId, new ContainerKillParameters()).Wait(); |
| 106 | + docker.Containers.RemoveContainerAsync(containerId, new ContainerRemoveParameters() { Force = true }).Wait(); |
86 | 107 | }
|
87 |
| - } |
| 108 | + |
| 109 | + private int GetFreePort() |
| 110 | + { |
| 111 | + const int startRange = 1000; |
| 112 | + const int endRange = 10000; |
| 113 | + var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); |
| 114 | + var tcpPorts = ipGlobalProperties.GetActiveTcpListeners(); |
| 115 | + var udpPorts = ipGlobalProperties.GetActiveUdpListeners(); |
| 116 | + |
| 117 | + var result = startRange; |
| 118 | + |
| 119 | + while (((tcpPorts.Any(x => x.Port == result)) || (udpPorts.Any(x => x.Port == result))) && result <= endRange) |
| 120 | + result++; |
| 121 | + |
| 122 | + if (result > endRange) |
| 123 | + throw new PortsInUseException(); |
| 124 | + |
| 125 | + return result; |
| 126 | + } |
| 127 | + } |
88 | 128 | }
|
0 commit comments