13
13
14
14
namespace Docker . Testify
15
15
{
16
- public abstract class DockerSetup : IDisposable
17
- {
18
- public abstract string ImageName { get ; }
19
- public virtual string ContainerPrefix => "tests" ;
16
+ public abstract class DockerSetup : IDisposable
17
+ {
18
+ public abstract string ImageName { get ; }
19
+ public virtual string ContainerPrefix => "tests" ;
20
20
public abstract int InternalPort { get ; }
21
21
22
22
public virtual string ImageTag => "latest" ;
23
23
public virtual TimeSpan TimeOut => TimeSpan . FromSeconds ( 30 ) ;
24
24
public virtual IList < string > EnvironmentVariables => new List < string > ( ) ;
25
- public int ExternalPort { get ; }
25
+ public int ExternalPort { get ; }
26
26
27
- public abstract bool TestReady ( ) ;
27
+ public abstract bool TestReady ( ) ;
28
28
public abstract void PublishConnectionInfo ( ) ;
29
29
30
30
protected readonly DockerClient docker ;
31
- protected string containerId ;
31
+ protected string containerId ;
32
32
33
- protected DockerSetup ( )
34
- {
33
+ protected DockerSetup ( )
34
+ {
35
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 ( ) ;
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
39
40
- ExternalPort = GetFreePort ( ) ;
40
+ ExternalPort = GetFreePort ( ) ;
41
41
42
- Debug . WriteLine ( $ "Selected port { ExternalPort } ") ;
42
+ Debug . WriteLine ( $ "Selected port { ExternalPort } ") ;
43
43
44
44
StartContainer ( ) . Wait ( ) ;
45
- }
45
+ }
46
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
- } ;
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
55
56
- hostCfg . PortBindings = new Dictionary < string , IList < PortBinding > > ( ) ;
57
- hostCfg . PortBindings . Add ( $ "{ InternalPort } /tcp", new PortBinding [ ] { pb } ) ;
56
+ hostCfg . PortBindings = new Dictionary < string , IList < PortBinding > > ( ) ;
57
+ hostCfg . PortBindings . Add ( $ "{ InternalPort } /tcp", new PortBinding [ ] { pb } ) ;
58
58
59
59
await PullImage ( ImageName , ImageTag ) ;
60
60
61
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 ( ) ;
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
75
76
76
Debug . WriteLine ( "Waiting service to start in the docker container..." ) ;
77
77
78
- var ready = false ;
79
- var expiryTime = DateTime . Now . Add ( TimeOut ) ;
78
+ var ready = false ;
79
+ var expiryTime = DateTime . Now . Add ( TimeOut ) ;
80
80
81
- while ( ( DateTime . Now < expiryTime ) && ( ! ready ) )
82
- {
83
- await Task . Delay ( 1000 ) ;
84
- ready = TestReady ( ) ;
85
- }
81
+ while ( ( DateTime . Now < expiryTime ) && ( ! ready ) )
82
+ {
83
+ await Task . Delay ( 1000 ) ;
84
+ ready = TestReady ( ) ;
85
+ }
86
86
87
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
- }
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
101
}
102
102
103
103
public async Task PullImage ( string name , string tag )
@@ -119,27 +119,27 @@ public async Task PullImage(string name, string tag)
119
119
}
120
120
}
121
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
- }
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
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 ( ) ;
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
135
136
- var result = startRange ;
136
+ var result = startRange ;
137
137
138
- while ( ( ( tcpPorts . Any ( x => x . Port == result ) ) || ( udpPorts . Any ( x => x . Port == result ) ) ) && result <= endRange )
139
- result ++ ;
138
+ while ( ( ( tcpPorts . Any ( x => x . Port == result ) ) || ( udpPorts . Any ( x => x . Port == result ) ) ) && result <= endRange )
139
+ result ++ ;
140
140
141
- if ( result > endRange )
142
- throw new PortsInUseException ( ) ;
141
+ if ( result > endRange )
142
+ throw new PortsInUseException ( ) ;
143
143
144
144
return result ;
145
145
}
0 commit comments