Skip to content

Commit 1d8273f

Browse files
committed
[grid] Adding Grid version to GraphQL
This will show the version of the component that is running up front (Router, Hub, or Standalone). There will be a separate commit to add the version of each Node.
1 parent 7de1d2b commit 1d8273f

File tree

8 files changed

+80
-40
lines changed

8 files changed

+80
-40
lines changed

java/server/src/org/openqa/selenium/grid/commands/Hub.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ protected Handlers createHandlers(Config config) {
169169
tracer,
170170
distributor,
171171
queuer,
172-
serverOptions.getExternalUri());
172+
serverOptions.getExternalUri(),
173+
getFormattedVersion());
173174
HttpHandler readinessCheck = req -> {
174175
boolean ready = router.isReady() && bus.isReady();
175176
return new HttpResponse()
@@ -206,11 +207,11 @@ protected void execute(Config config) {
206207

207208
Server<?> server = asServer(config).start();
208209

210+
LOG.info(String.format("Started Selenium Hub %s: %s", getFormattedVersion(), server.getUrl()));
211+
}
212+
213+
private String getFormattedVersion() {
209214
BuildInfo info = new BuildInfo();
210-
LOG.info(String.format(
211-
"Started Selenium hub %s (revision %s): %s",
212-
info.getReleaseLabel(),
213-
info.getBuildRevision(),
214-
server.getUrl()));
215+
return String.format("%s (revision %s)", info.getReleaseLabel(), info.getBuildRevision());
215216
}
216217
}

java/server/src/org/openqa/selenium/grid/commands/Standalone.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ protected Handlers createHandlers(Config config) {
149149
bus,
150150
newSessionQueueOptions.getSessionRequestRetryInterval(),
151151
newSessionQueueOptions.getSessionRequestTimeout());
152+
152153
NewSessionQueuer queuer = new LocalNewSessionQueuer(
153154
tracer,
154155
bus,
@@ -179,7 +180,8 @@ protected Handlers createHandlers(Config config) {
179180
tracer,
180181
distributor,
181182
queuer,
182-
serverOptions.getExternalUri());
183+
serverOptions.getExternalUri(),
184+
getFormattedVersion());
183185

184186
Routable ui;
185187
URL uiRoot = getClass().getResource("/javascript/grid-ui/build");
@@ -213,11 +215,14 @@ protected void execute(Config config) {
213215

214216
Server<?> server = asServer(config).start();
215217

216-
BuildInfo info = new BuildInfo();
217218
LOG.info(String.format(
218-
"Started Selenium standalone %s (revision %s): %s",
219-
info.getReleaseLabel(),
220-
info.getBuildRevision(),
219+
"Started Selenium Standalone %s: %s",
220+
getFormattedVersion(),
221221
server.getUrl()));
222222
}
223+
224+
private String getFormattedVersion() {
225+
BuildInfo info = new BuildInfo();
226+
return String.format("%s (revision %s)", info.getReleaseLabel(), info.getBuildRevision());
227+
}
223228
}

java/server/src/org/openqa/selenium/grid/graphql/GraphqlHandler.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import org.openqa.selenium.json.Json;
3535
import org.openqa.selenium.remote.http.Contents;
3636
import org.openqa.selenium.remote.http.HttpHandler;
37-
import org.openqa.selenium.remote.http.HttpMethod;
3837
import org.openqa.selenium.remote.http.HttpRequest;
3938
import org.openqa.selenium.remote.http.HttpResponse;
4039
import org.openqa.selenium.remote.tracing.AttributeKey;
@@ -49,11 +48,9 @@
4948
import java.net.URI;
5049
import java.util.HashMap;
5150
import java.util.Map;
52-
import java.util.Objects;
5351
import java.util.concurrent.ExecutionException;
5452

5553
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
56-
import static java.net.HttpURLConnection.HTTP_OK;
5754
import static org.openqa.selenium.json.Json.JSON_UTF_8;
5855
import static org.openqa.selenium.json.Json.MAP_TYPE;
5956
import static org.openqa.selenium.remote.http.Contents.utf8String;
@@ -71,14 +68,16 @@ public class GraphqlHandler implements HttpHandler {
7168
private final Distributor distributor;
7269
private final NewSessionQueuer newSessionQueuer;
7370
private final URI publicUri;
71+
private final String version;
7472
private final GraphQL graphQl;
7573

7674

7775
public GraphqlHandler(Tracer tracer, Distributor distributor, NewSessionQueuer newSessionQueuer,
78-
URI publicUri) {
76+
URI publicUri, String version) {
7977
this.distributor = Require.nonNull("Distributor", distributor);
8078
this.newSessionQueuer = Require.nonNull("NewSessionQueuer", newSessionQueuer);
8179
this.publicUri = Require.nonNull("Uri", publicUri);
80+
this.version = Require.nonNull("GridVersion", version);
8281
this.tracer = Require.nonNull("Tracer", tracer);
8382

8483
GraphQLSchema schema = new SchemaGenerator()
@@ -132,7 +131,8 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
132131
}
133132

134133
String query = (String) inputs.get("query");
135-
@SuppressWarnings("unchecked") Map<String, Object> variables = inputs.get("variables") instanceof Map ?
134+
@SuppressWarnings("unchecked")
135+
Map<String, Object> variables = inputs.get("variables") instanceof Map ?
136136
(Map<String, Object>) inputs.get("variables") :
137137
new HashMap<>();
138138

@@ -173,7 +173,11 @@ private RuntimeWiring buildRuntimeWiring() {
173173
.scalar(Types.Uri)
174174
.scalar(Types.Url)
175175
.type("GridQuery", typeWiring -> typeWiring
176-
.dataFetcher("grid", new GridData(distributor, newSessionQueuer, publicUri))
176+
.dataFetcher("grid", new GridData(
177+
distributor,
178+
newSessionQueuer,
179+
publicUri,
180+
version))
177181
.dataFetcher("session", new SessionData(distributor)))
178182
.build();
179183
}

java/server/src/org/openqa/selenium/grid/graphql/Grid.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,25 @@ public class Grid {
3939
private final URI uri;
4040
private final Supplier<DistributorStatus> distributorStatus;
4141
private final NewSessionQueuer newSessionQueuer;
42+
private final String version;
4243

43-
public Grid(Distributor distributor, NewSessionQueuer newSessionQueuer, URI uri) {
44+
public Grid(Distributor distributor, NewSessionQueuer newSessionQueuer, URI uri,
45+
String version) {
4446
Require.nonNull("Distributor", distributor);
4547
this.uri = Require.nonNull("Grid's public URI", uri);
4648
this.newSessionQueuer = Require.nonNull("NewSessionQueuer", newSessionQueuer);
4749
this.distributorStatus = Suppliers.memoize(distributor::getStatus);
50+
this.version = Require.nonNull("Grid's version", version);
4851
}
4952

5053
public URI getUri() {
5154
return uri;
5255
}
5356

57+
public String getVersion() {
58+
return version;
59+
}
60+
5461
public List<Node> getNodes() {
5562
ImmutableList.Builder<Node> toReturn = ImmutableList.builder();
5663

java/server/src/org/openqa/selenium/grid/graphql/GridData.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,18 @@ public class GridData implements DataFetcher {
2929
private final Distributor distributor;
3030
private final NewSessionQueuer newSessionQueuer;
3131
private final URI publicUri;
32+
private final String version;
3233

33-
public GridData(Distributor distributor, NewSessionQueuer newSessionQueuer, URI publicUri) {
34+
public GridData(Distributor distributor, NewSessionQueuer newSessionQueuer, URI publicUri,
35+
String version) {
3436
this.distributor = Require.nonNull("Distributor", distributor);
3537
this.publicUri = Require.nonNull("Grid's public URI", publicUri);
3638
this.newSessionQueuer = Require.nonNull("NewSessionQueuer", newSessionQueuer);
39+
this.version = Require.nonNull("Grid's version", version);
3740
}
3841

3942
@Override
4043
public Object get(DataFetchingEnvironment environment) {
41-
return new Grid(distributor, newSessionQueuer, publicUri);
44+
return new Grid(distributor, newSessionQueuer, publicUri, version);
4245
}
4346
}

java/server/src/org/openqa/selenium/grid/graphql/selenium-grid-schema.graphqls

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ type GridQuery {
4444
type Grid {
4545
uri: Uri!
4646
nodes: [Node!]!
47-
totalSlots: Int
48-
usedSlots: Int
47+
totalSlots: Int!
48+
usedSlots: Int!
4949
sessionCount: Int!
50+
version: String!
5051
}

java/server/src/org/openqa/selenium/grid/router/httpd/RouterServer.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,17 @@ protected Handlers createHandlers(Config config) {
133133
distributorUrl,
134134
secret);
135135

136+
BuildInfo info = new BuildInfo();
137+
String gridVersion = String.format(
138+
"%s (revision %s)",
139+
info.getReleaseLabel(),
140+
info.getBuildRevision());
136141
GraphqlHandler graphqlHandler = new GraphqlHandler(
137142
tracer,
138143
distributor,
139144
queuer,
140-
serverOptions.getExternalUri());
145+
serverOptions.getExternalUri(),
146+
gridVersion);
141147

142148
Route handler = Route.combine(
143149
new Router(tracer, clientFactory, sessions, queuer, distributor).with(networkOptions.getSpecComplianceChecks()),
@@ -153,11 +159,12 @@ protected void execute(Config config) {
153159

154160
Server<?> server = asServer(config).start();
155161

156-
BuildInfo info = new BuildInfo();
157162
LOG.info(String.format(
158-
"Started Selenium router %s (revision %s): %s",
159-
info.getReleaseLabel(),
160-
info.getBuildRevision(),
161-
server.getUrl()));
163+
"Started Selenium Router %s: %s", getFormattedVersion(), server.getUrl()));
164+
}
165+
166+
private String getFormattedVersion() {
167+
BuildInfo info = new BuildInfo();
168+
return String.format("%s (revision %s)", info.getReleaseLabel(), info.getBuildRevision());
162169
}
163170
}

java/server/test/org/openqa/selenium/grid/graphql/GraphqlHandlerTest.java

+24-12
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
import static org.assertj.core.api.InstanceOfAssertFactories.LIST;
7373
import static org.assertj.core.api.InstanceOfAssertFactories.MAP;
7474
import static org.openqa.selenium.json.Json.MAP_TYPE;
75-
import static org.openqa.selenium.remote.http.Contents.asJson;
7675
import static org.openqa.selenium.remote.http.Contents.utf8String;
7776
import static org.openqa.selenium.remote.http.HttpMethod.GET;
7877
import static org.openqa.selenium.remote.http.HttpMethod.POST;
@@ -83,8 +82,8 @@ public class GraphqlHandlerTest {
8382

8483
private final Secret registrationSecret = new Secret("stilton");
8584
private final URI publicUri = new URI("http://example.com/grid-o-matic");
85+
private final String version = "4.0.0";
8686
private Distributor distributor;
87-
private LocalNewSessionQueue localNewSessionQueue;
8887
private NewSessionQueuer queuer;
8988
private Tracer tracer;
9089
private EventBus events;
@@ -107,7 +106,7 @@ public void setupGrid() {
107106
caps = new ImmutableCapabilities("browserName", "cheese");
108107
payload = NewSessionPayload.create(caps);
109108

110-
localNewSessionQueue = new LocalNewSessionQueue(
109+
LocalNewSessionQueue localNewSessionQueue = new LocalNewSessionQueue(
111110
tracer,
112111
events,
113112
Duration.ofSeconds(2),
@@ -130,7 +129,7 @@ public void setupGrid() {
130129

131130
@Test
132131
public void shouldBeAbleToGetGridUri() {
133-
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri);
132+
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri, version);
134133

135134
Map<String, Object> topLevel = executeQuery(handler, "{ grid { uri } }");
136135

@@ -141,9 +140,22 @@ public void shouldBeAbleToGetGridUri() {
141140
"uri", publicUri.toString()))));
142141
}
143142

143+
@Test
144+
public void shouldBeAbleToGetGridVersion() {
145+
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri, version);
146+
147+
Map<String, Object> topLevel = executeQuery(handler, "{ grid { version } }");
148+
149+
assertThat(topLevel).isEqualTo(
150+
singletonMap(
151+
"data", singletonMap(
152+
"grid", singletonMap(
153+
"version", version))));
154+
}
155+
144156
@Test
145157
public void shouldReturnAnEmptyListForNodesIfNoneAreRegistered() {
146-
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri);
158+
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri, version);
147159

148160
Map<String, Object> topLevel = executeQuery(handler, "{ grid { nodes { uri } } }");
149161

@@ -174,7 +186,7 @@ public boolean test(Capabilities capabilities) {
174186
distributor.add(node);
175187
wait.until(obj -> distributor.getStatus().hasCapacity());
176188

177-
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri);
189+
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri, version);
178190
Map<String, Object> topLevel = executeQuery(handler, "{ grid { nodes { uri } } }");
179191

180192
assertThat(topLevel).describedAs(topLevel.toString()).isEqualTo(
@@ -224,7 +236,7 @@ public void shouldBeAbleToGetSessionInfo() throws URISyntaxException {
224236
String query = String.format(
225237
"{ session (id: \"%s\") { id, capabilities, startTime, uri } }", sessionId);
226238

227-
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri);
239+
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri, version);
228240
Map<String, Object> result = executeQuery(handler, query);
229241

230242
assertThat(result).describedAs(result.toString()).isEqualTo(
@@ -280,7 +292,7 @@ public void shouldBeAbleToGetNodeInfoForSession() throws URISyntaxException {
280292
slot);
281293
String query = String.format("{ session (id: \"%s\") { nodeId, nodeUri } }", sessionId);
282294

283-
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri);
295+
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri, version);
284296
Map<String, Object> result = executeQuery(handler, query);
285297

286298
assertThat(result).describedAs(result.toString()).isEqualTo(
@@ -338,7 +350,7 @@ public void shouldBeAbleToGetSlotInfoForSession() throws URISyntaxException {
338350
String query = String.format(
339351
"{ session (id: \"%s\") { slot { id, stereotype, lastStarted } } }", sessionId);
340352

341-
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri);
353+
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri, version);
342354
Map<String, Object> result = executeQuery(handler, query);
343355

344356
assertThat(result).describedAs(result.toString()).isEqualTo(
@@ -381,7 +393,7 @@ public void shouldBeAbleToGetSessionDuration() throws URISyntaxException {
381393

382394
String query = String.format("{ session (id: \"%s\") { sessionDurationMillis } }", sessionId);
383395

384-
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri);
396+
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri, version);
385397
Map<String, Object> result = executeQuery(handler, query);
386398

387399
assertThat(result)
@@ -412,7 +424,7 @@ public void shouldThrowExceptionWhenSessionNotFound() throws URISyntaxException
412424
String randomSessionId = UUID.randomUUID().toString();
413425
String query = "{ session (id: \"" + randomSessionId + "\") { sessionDurationMillis } }";
414426

415-
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri);
427+
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri, version);
416428
Map<String, Object> result = executeQuery(handler, query);
417429
assertThat(result)
418430
.containsEntry("data", null)
@@ -441,7 +453,7 @@ public void shouldThrowExceptionWhenSessionIsEmpty() throws URISyntaxException {
441453

442454
String query = "{ session (id: \"\") { sessionDurationMillis } }";
443455

444-
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri);
456+
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri, version);
445457
Map<String, Object> result = executeQuery(handler, query);
446458
assertThat(result)
447459
.containsEntry("data", null)

0 commit comments

Comments
 (0)