22
22
import java .security .Security ;
23
23
import java .util .List ;
24
24
import java .util .UUID ;
25
+ import org .apache .logging .log4j .LogManager ;
26
+ import org .apache .logging .log4j .Logger ;
25
27
import org .bouncycastle .jce .provider .BouncyCastleProvider ;
26
28
import org .junit .Assume ;
27
29
import org .junit .jupiter .api .*;
28
30
29
31
@ TestInstance (TestInstance .Lifecycle .PER_CLASS )
30
32
public abstract class AbstractDockerbasedIT {
33
+ private static final Logger LOGGER = LogManager .getLogger ();
34
+
31
35
private static List <Image > localImages ;
32
36
33
37
private final TlsImplementationType implementation ;
@@ -36,6 +40,10 @@ public abstract class AbstractDockerbasedIT {
36
40
private final TransportType transportType ;
37
41
38
42
private DockerTlsServerInstance dockerInstance ;
43
+ private String serverAddress ;
44
+
45
+ private static final int MAX_TRIES = 10 ;
46
+ private static final int WAIT_TIME_MS = 500 ;
39
47
40
48
public AbstractDockerbasedIT (
41
49
TlsImplementationType implementation ,
@@ -94,6 +102,47 @@ private void createDockerInstance(Image image) throws DockerException, Interrupt
94
102
.additionalParameters (additionalParameters );
95
103
dockerInstance = serverInstanceBuilder .build ();
96
104
dockerInstance .start ();
105
+ saveServerAddress ();
106
+ }
107
+
108
+ private void saveServerAddress () {
109
+ InspectContainerCmd cmd =
110
+ DockerClientManager .getDockerClient ()
111
+ .inspectContainerCmd (this .dockerInstance .getId ());
112
+ InspectContainerResponse response ;
113
+ Ports .Binding serverPortBinding = null ;
114
+ for (int currentTry = 0 ; currentTry < MAX_TRIES ; currentTry ++) {
115
+ response = cmd .exec ();
116
+ Ports .Binding [] serverPortBindings =
117
+ response .getNetworkSettings ().getPorts ().getBindings ().values ().stream ()
118
+ .findFirst ()
119
+ .orElse (new Ports .Binding [] {});
120
+ if (serverPortBindings .length >= 1 ) {
121
+ serverPortBinding = serverPortBindings [0 ];
122
+ break ;
123
+ } else {
124
+ LOGGER .info (
125
+ "Could not determine container port binding. Retrying in {} ms..." ,
126
+ WAIT_TIME_MS );
127
+ try {
128
+ Thread .sleep (WAIT_TIME_MS );
129
+ } catch (InterruptedException e ) {
130
+ Thread .currentThread ().interrupt ();
131
+ throw new RuntimeException ("Interrupted while waiting for port bindings" , e );
132
+ }
133
+ }
134
+ }
135
+
136
+ if (serverPortBinding == null ) {
137
+ Assertions .fail ("Could not load assigned port for docker container." );
138
+ }
139
+
140
+ int serverPort = Integer .parseInt (serverPortBinding .getHostPortSpec ());
141
+ String serverName =
142
+ serverPortBinding .getHostIp ().equals ("0.0.0.0" )
143
+ ? "127.0.0.1"
144
+ : serverPortBinding .getHostIp ();
145
+ this .serverAddress = serverName + ":" + serverPort ;
97
146
}
98
147
99
148
@ AfterEach
@@ -108,20 +157,6 @@ protected void killContainer() {
108
157
}
109
158
110
159
protected String getServerAddress () {
111
- InspectContainerCmd cmd =
112
- DockerClientManager .getDockerClient ()
113
- .inspectContainerCmd (this .dockerInstance .getId ());
114
- InspectContainerResponse response = cmd .exec ();
115
- Ports .Binding serverPortBinding =
116
- response .getNetworkSettings ().getPorts ().getBindings ().values ().stream ()
117
- .findFirst ()
118
- .orElseThrow (IllegalArgumentException ::new )[0 ];
119
-
120
- int serverPort = Integer .parseInt (serverPortBinding .getHostPortSpec ());
121
- String serverName =
122
- serverPortBinding .getHostIp ().equals ("0.0.0.0" )
123
- ? "127.0.0.1"
124
- : serverPortBinding .getHostIp ();
125
- return serverName + ":" + serverPort ;
160
+ return serverAddress ;
126
161
}
127
162
}
0 commit comments