17
17
18
18
package org .openqa .selenium .grid .docker ;
19
19
20
- import static java .util .Optional .ofNullable ;
21
- import static org .openqa .selenium .docker .ContainerConfig .image ;
22
- import static org .openqa .selenium .remote .Dialect .W3C ;
23
- import static org .openqa .selenium .remote .http .Contents .string ;
24
- import static org .openqa .selenium .remote .http .HttpMethod .GET ;
25
- import static org .openqa .selenium .remote .tracing .Tags .EXCEPTION ;
26
-
27
20
import org .openqa .selenium .Capabilities ;
28
21
import org .openqa .selenium .Dimension ;
29
22
import org .openqa .selenium .ImmutableCapabilities ;
81
74
import java .util .logging .Level ;
82
75
import java .util .logging .Logger ;
83
76
77
+ import static java .util .Optional .ofNullable ;
78
+ import static org .openqa .selenium .docker .ContainerConfig .image ;
79
+ import static org .openqa .selenium .remote .Dialect .W3C ;
80
+ import static org .openqa .selenium .remote .http .Contents .string ;
81
+ import static org .openqa .selenium .remote .http .HttpMethod .GET ;
82
+ import static org .openqa .selenium .remote .tracing .Tags .EXCEPTION ;
83
+
84
84
public class DockerSessionFactory implements SessionFactory {
85
85
86
86
private static final Logger LOG = Logger .getLogger (DockerSessionFactory .class .getName ());
@@ -93,6 +93,7 @@ public class DockerSessionFactory implements SessionFactory {
93
93
private final Capabilities stereotype ;
94
94
private final Image videoImage ;
95
95
private final DockerAssetsPath assetsPath ;
96
+ private final String networkName ;
96
97
97
98
public DockerSessionFactory (
98
99
Tracer tracer ,
@@ -102,12 +103,14 @@ public DockerSessionFactory(
102
103
Image browserImage ,
103
104
Capabilities stereotype ,
104
105
Image videoImage ,
105
- DockerAssetsPath assetsPath ) {
106
+ DockerAssetsPath assetsPath ,
107
+ String networkName ) {
106
108
this .tracer = Require .nonNull ("Tracer" , tracer );
107
109
this .clientFactory = Require .nonNull ("HTTP client" , clientFactory );
108
110
this .docker = Require .nonNull ("Docker command" , docker );
109
111
this .dockerUri = Require .nonNull ("Docker URI" , dockerUri );
110
112
this .browserImage = Require .nonNull ("Docker browser image" , browserImage );
113
+ this .networkName = Require .nonNull ("Docker network name" , networkName );
111
114
this .stereotype = ImmutableCapabilities .copyOf (
112
115
Require .nonNull ("Stereotype" , stereotype ));
113
116
this .videoImage = videoImage ;
@@ -176,9 +179,8 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
176
179
try {
177
180
result = new ProtocolHandshake ().createSession (client , command );
178
181
response = result .createResponse ();
179
- attributeMap .put (
180
- AttributeKey .DRIVER_RESPONSE .getKey (),
181
- EventAttribute .setValue (response .toString ()));
182
+ attributeMap .put (AttributeKey .DRIVER_RESPONSE .getKey (),
183
+ EventAttribute .setValue (response .toString ()));
182
184
} catch (IOException | RuntimeException e ) {
183
185
span .setAttribute ("error" , true );
184
186
span .setStatus (Status .CANCELLED );
@@ -242,15 +244,13 @@ public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sess
242
244
}
243
245
244
246
private Container createBrowserContainer (int port , Capabilities sessionCapabilities ) {
245
- Map <String , String > browserContainerEnvVars =
246
- getBrowserContainerEnvVars (sessionCapabilities );
247
- Map <String , String > devShmMount =
248
- Collections .singletonMap ("/dev/shm" , "/dev/shm" );
249
- return docker .create (
250
- image (browserImage )
251
- .env (browserContainerEnvVars )
252
- .bind (devShmMount )
253
- .map (Port .tcp (4444 ), Port .tcp (port )));
247
+ Map <String , String > browserContainerEnvVars = getBrowserContainerEnvVars (sessionCapabilities );
248
+ Map <String , String > devShmMount = Collections .singletonMap ("/dev/shm" , "/dev/shm" );
249
+ return docker .create (image (browserImage )
250
+ .env (browserContainerEnvVars )
251
+ .bind (devShmMount )
252
+ .map (Port .tcp (4444 ), Port .tcp (port ))
253
+ .network (networkName ));
254
254
}
255
255
256
256
private Map <String , String > getBrowserContainerEnvVars (Capabilities sessionRequestCapabilities ) {
@@ -275,7 +275,10 @@ private Container startVideoContainer(Capabilities sessionCapabilities,
275
275
sessionCapabilities ,
276
276
browserContainerIp );
277
277
Map <String , String > volumeBinds = Collections .singletonMap (hostPath , "/videos" );
278
- Container videoContainer = docker .create (image (videoImage ).env (envVars ).bind (volumeBinds ));
278
+ Container videoContainer = docker .create (image (videoImage )
279
+ .env (envVars )
280
+ .bind (volumeBinds )
281
+ .network (networkName ));
279
282
videoContainer .start ();
280
283
LOG .info (String .format ("Video container started (id: %s)" , videoContainer .getId ()));
281
284
return videoContainer ;
0 commit comments